var divs_to_swapfade 	= new Array('gm-panel-1', 'gm-panel-2', 'gm-panel-3', 'gm-panel-4');
var cross_sell_panels = new Array('crosssellpanel_1', 'crosssellpanel_2', 'crosssellpanel_3', 'crosssellpanel_4', 'crosssellpanel_5');
var i = 0;
var wait = 6000;

function gminit(){
	for(j = 1; j < divs_to_swapfade.length; j++){
		document.getElementById(divs_to_swapfade[j]).style.display = 'none';
	}
	setInterval('swapfade()',wait);
}

function swapfade(){
	// Panel swapfade
	Effect.Fade(divs_to_swapfade[i], {duration:1, from:1.0, to:0.0, transition: Effect.Transitions.sinoidal});
	i++;
	if(i==divs_to_swapfade.length){
		i=0;
	}
	Effect.Appear(divs_to_swapfade[i], {duration:1, from:0.0, to:1.0, transition: Effect.Transitions.sinoidal});
}

function randomnumber(lowerbound, upperbound){
	var range = upperbound - lowerbound + 1;
	return Math.floor(Math.random() * range) + lowerbound;
}

function showCrossSellPanel(){
	for(i = 0; i < cross_sell_panels.length; i++){
		document.getElementById(cross_sell_panels[i]).style.display = "none";
	}
	document.getElementById("crosssellpanel_" + randomnumber(1, cross_sell_panels.length)).style.display = "block";
}

function validateTAF(){
	if (trim(document.getElementById('frm-FriendName').value) == '') {
		return showError(document.getElementById('frm-FriendName'), 'Please enter your friend\'s name.');
	}
	if (trim(document.getElementById('frm-FriendEmail').value) == '') {
		return showError(document.getElementById('frm-FriendEmail'), 'Please enter your friend\'s email.');
	}
	if (trim(document.getElementById('frm-YourName').value) == '') {
		return showError(document.getElementById('frm-YourName'), 'Please enter your name.');
	}
	if (trim(document.getElementById('frm-Message').value) == '') {
		return showError(document.getElementById('frm-Message'), 'Please enter your message.');
	}
	document.tellafriend.submit();
	return true;
}

function validateContact(){
	if (trim(document.getElementById('frm-ContFName').value) == '') {
		return showError(document.getElementById('frm-ContFName'), 'Please enter your first name.');
	}
	if (trim(document.getElementById('frm-ContLName').value) == '') {
		return showError(document.getElementById('frm-ContLName'), 'Please enter your last name.');
	}
	if (!isEmailFormatCorrect(document.getElementById('frm-ContEmail').value)) {
		return showError(document.getElementById('frm-ContEmail'), 'Please enter a valid email address.');
	}
	if (trim(document.getElementById('frm-ContMessage').value) == '') {
		return showError(document.getElementById('frm-ContMessage'), 'Please enter your message.');
	}
	document.contactus.submit();
	return true;
}

function trim(sVal) {
	while (sVal.charAt(sVal.length - 1) == " ") {
			sVal = sVal.substring(0, sVal.length - 1);
	}
	while (sVal.charAt(0) == " ") {
			sVal = sVal.substr(1, sVal.length);
	}
	return sVal;
}

// email checker
function isEmailFormatCorrect(strAddr) {
	var ptn = /^[\w\d._%-]+@[\w\d._%-]+\.[\w\d._%-]{2,4}$/;
	return ptn.test(strAddr);
}

// generic error message display
function showError(fld, msg){
	fld.focus();
	alert(msg);
	return false;
}

function popcustomwindow(surl, width, height) {
	if(width == ''){
		width = 800;
	}
	if(height == ''){
		height = 600;
	}
	window.open(surl, 'newwin', 'height=' + height + ',width=' + width + ',dependent=yes,directories=no,location=no,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=no,modal=yes'); 
	return false;	
}
