// Load common .js files here
/*=========================================================================*/
document.write('<script type="text/javascript" src="/js/cls/prototype.js"><\/script>');
document.write('<script type="text/javascript" src="/js/cls/scriptaculous.js"><\/script>');

// And now the global scripts
/*=========================================================================*/
var W3CDOM = (document.createElement && document.getElementsByTagName);


/* Rollovers for navigation
****************************************************************************/
var mouseOvers = new Array();
var mouseOuts = new Array();

AddLoadEvent(InitRollovers);
function InitRollovers(){
	AddRollovers('PrimaryNav');
}

function AddRollovers(container){
	// check for objects
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	// check the container element exists and get it
	if(!document.getElementById(container)) return false;
	var nav = document.getElementById(container);
	// find all images in the container element and loop through
	var imgs = nav.getElementsByTagName('img');
	for (var i=0; i<imgs.length; i++){
		AddRolloverEvents(imgs[i]);
	}
}

function AddRolloverEvents(x){
	var j = mouseOuts.length;
	// add event handler functions
	if (x.src.indexOf('_on.') < 0){
		x.onmouseover = function(){ this.src = mouseOvers[this.number].src; };
		x.onmouseout = function(){ this.src = mouseOuts[this.number].src; };
		// create pre-loading arrays
		var suffix = x.src.substring(x.src.lastIndexOf('.'));
		mouseOuts[j] = new Image();
		mouseOuts[j].src = x.src;
		mouseOvers[j] = new Image();
		mouseOvers[j].src = x.src.substring(0, x.src.lastIndexOf('.')) + '_over' + suffix;
		// add identifiers to the images for rollover functions to reference
		x.number = j;
	}
}


/***************************************************************************/
/* Form handling - Mailing list
****************************************************************************/
AddLoadEvent(InitFormValidation);

function InitFormValidation(){
	// Add validation function to user add/edit form
	if(document.getElementById('MailingListForm')){
		var form = document.getElementById('MailingListForm');
		form.onsubmit = function(){return MailingListForm(this)}
	}
	$('Email').onfocus = function(){this.value = '';this.style.color = '#000';}
}

function MailingListForm(theForm){
	// check the email
	if(document.getElementById('Email')){
		if ($('Email').value == ''){
			$('Email').focus();
			return false;
		}
	}
	// check the email
	if(document.getElementById('Email')){
		if (!IsValidEmail($('Email').value)){
			// bad email - display message
			$('Email').style.color = '#F55F00';
			$('Email').value = 'Invalid email address';
			$('Email').blur();
			return false;
		}
	}
        theForm.target = 'EmailSignup';
        win = window.open('','EmailSignup','width=700,height=400,scrollbars=1,status=0,toolbars=0');
        if(typeof(focus) == 'function') win.focus();
        return true;


/* can't do the ajax anymore as it links to another form
	// AJAX request options
	var opt = {
		method:'post',
		postBody:'cmd=mailinglist&m=' + $F('m') + '&p=' + $F('p') + '&ea=' + $F('Email'),
		onSuccess:function(t){$('Submit').value = 'Submitted'; MailingListResponse(t);},
		onLoading:function(){$('Submit').value = 'Submitting...'; $('Submit').blur(); $('Submit').disabled = true;}
	}
	// Send request
	new Ajax.Request('form.process.php', opt);
	return false;
*/
}

function MailingListResponse(t){
	if(t.responseText == 1){
		//Show main screen
		$('MailingListInput').style.display = 'none';
		$('MailingListFeedback').style.display = 'block';
		$('MailingListFeedback').style.color = '#000';
		$('MailingListFeedback').innerHTML = 'Email successfully added';
		new Effect.Morph('MailingListFeedback', {style:'background:#DDD;color:#A5ACB2;', delay:2, duration:1});
	}else{
		$('MailingListInput').style.display = 'none';
		$('MailingListFeedback').style.display = 'block';
		$('MailingListFeedback').innerHTML = 'Submission error!<br />Please contact <a href="mailto:support@smartypants.com.hk" style="color:#F55F00;text-decoration:none;">web support</a>';
	}
}

function IsValidEmail(email){
	var emailExp = new RegExp("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$");
	if (emailExp.test(email)){return (email)}
}

/* Top 10 Helper Functions - and a few more
===========================================================================*/
// Allows you to add multiple function to the onload event
function AddLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	}else{
		window.onload = function(){
			oldonload();
			func();
		}
	}
}
// Returns an array of elements that have a specific class
function GetElementsByClass(searchClass, domNode, tagName){
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var elem = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " " + searchClass + " ";
	for(i=0, j=0; i<tags.length; i++){
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1)
			elem[j++] = tags[i];
	}
	return elem;
}
/***************************************************************************/