function TrimSpaces(str) {
   
   return str.replace(/^\s*(.*\S|.*)\s*$/, '$1');
}

function ShowLargeImage(url)
{
	window.open(url,'new_win','resizable=no,scrollbars=no,toobar=0,status=0,width=550,height=400');
}

function popUpGOTSurvey()
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open('/bbq/pub/misc/got/survey/', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=540,height=300');");
}

function popUpDRTVSurvey()
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open('/bbq/pub/misc/drtv/survey/survey.html', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=650');");
}

function NavIntl(url)
{
	if(url != 'z')
	{
		window.location.href = url;
	}
}


var debug = false;

function HideAll()
{
	if (document.all)
	{
		if(debug){
			alert("document.all[section]");
		}
			
		document.all['grill'].style.display = 'none'; 
		document.all['support'].style.display = 'none'; 
		document.all['recipe'].style.display = 'none'; 
		document.all['dealer'].style.display = 'none'; 
	}
	else if (document.getElementById)
	{
		if(debug){
			alert("document.getElementById(section)");
		}
		
		document.getElementById('grill').style.display = 'none';
		document.getElementById('support').style.display = 'none'; 
		document.getElementById('recipe').style.display = 'none'; 
		document.getElementById('dealer').style.display = 'none'; 
	}
}

function Show(section)
{
	HideAll();
	
	if(section != 'register' && section != 'contact')
	{	
		if (document.all)
		{
			if(debug){
				alert("document.all[section]");
			}
			document.all[section].style.display = '';
		}
		else if (document.getElementById)
		{
			if(debug){
				alert("document.getElementById(section)");
			}
			document.getElementById(section).style.display = '';
		}
	}
}

function Roll(section)
{
 Show(section);
}
		

/*=================================================================*/
/*Section Home Page Rollovers */
/*=================================================================*/
function doRollOn(obj,divNum)
{
	var roll = "sh_Roll";
	
	if (document.all)
	{
		document.all['Div0'].style.display='none';
		document.all['Div' + divNum].style.display='';
		document.all[roll + divNum].style.color = 'white';
		document.all[roll + divNum].style.backgroundColor = 'orange';
	}
	else if (document.layers)
	{
		document.layers['Div0'].style.display='none';
		document.layers['Div' + divNum].style.display='';
		document.layers[roll + divNum].style.color = 'white';
		document.layers[roll + divNum].style.backgroundColor = 'orange';
	}
	else if (document.getElementById)
	{
		document.getElementById('Div0').style.display='none';
		document.getElementById('Div' + divNum).style.display='';
		document.getElementById(roll + divNum).style.color = 'white';
		document.getElementById(roll + divNum).style.backgroundColor = 'orange';
	}
	
	return true;
}

function doRollOff(divNum)
{
	var roll = "sh_Roll";
	
	if (document.all)
	{
		document.all['Div' + divNum].style.display='none';
		document.all['Div0'].style.display='';
		document.all[roll + divNum].style.color = 'maroon';
		document.all[roll + divNum].style.backgroundColor = 'white';
	}
	else if (document.layers)
	{
		document.layers['Div' + divNum].style.display='none';
		document.layers['Div0'].style.display='';
		document.layers[roll + divNum].style.color = 'maroon';
		document.layers[roll + divNum].style.backgroundColor = 'white';
	}
	else if (document.getElementById)
	{
		document.getElementById('Div' + divNum).style.display='none';
		document.getElementById('Div0').style.display='';
		document.getElementById(roll + divNum).style.color = 'maroon';
		document.getElementById(roll + divNum).style.backgroundColor = 'white';
	}
	return true;
}
/*=================================================================*/



function FindDealers()
{
	var f = document.forms[0];
	var zip = TrimSpaces(f.txtZip.value);
	var rad = f.selRadius.options[f.selRadius.selectedIndex].value;
	var fam = f.selWebFamily.options[f.selWebFamily.selectedIndex].value;
	
	if(zip == "")
	{
		alert("Please enter a zip code.");
		f.txtZip.value = "";
		f.txtZip.focus();
	}
	else
	{
		var url = "results.aspx?zip=" + zip + "&rad=" + rad + "&fam=" + fam;
		window.location.href = url;
	}
}

/*=================================================================*/
/* Domestic Registration Validation																									*/
/*=================================================================*/
function ValidateRegistrationInfo()
{
	var mess = 'Please complete all fields marked with an asterisk (*)';
	var f = document.forms[0];
	
	if(TrimSpaces(f.txtFName.value) == '')
	{alert(mess); f.txtFName.value = ''; f.txtFName.focus(); return;}
	
	if(TrimSpaces(f.txtLName.value) == '')
	{alert(mess); f.txtLName.value = ''; f.txtLName.focus(); return;}
	
	if(TrimSpaces(f.txtAddr1.value) == '')
	{alert(mess); f.txtAddr1.value = ''; f.txtAddr1.focus(); return;}
	
	if(TrimSpaces(f.txtCity.value) == '')
	{alert(mess); f.txtCity.value = ''; f.txtCity.focus(); return;}
	
	if(f.selState.selectedIndex == -1 || f.selState.selectedIndex == 0)
	{alert(mess); f.selState.focus(); return;}
	
	if(TrimSpaces(f.txtZip.value) == '')
	{
		alert(mess); f.txtZip.value = ''; f.txtZip.focus(); return;
	}
	else
	{
		var zipMess = "The Zip Code entered does not appear to be valid.";
		
		if(!ValidZip(f.txtZip.value))
		{
			alert(zipMess);
			f.txtZip.focus();
			return;
		}		
	}
	
	if(TrimSpaces(f.txtPhArea.value) != '' || TrimSpaces(f.txtPhPrefix.value) != '' || TrimSpaces(f.txtPhSuffix.value) != '')
	{
		if(!ValidPhone(f.txtPhArea.value, f.txtPhPrefix.value, f.txtPhSuffix.value))
		{
			alert('The Phone Number entered does not appear to be valid.');
			f.txtPhArea.focus();
			return;			
		}
	}
	
	if(TrimSpaces(f.txtEmail.value) != '')
	{
		if(!ValidEmail(TrimSpaces(f.txtEmail.value)))
		{alert('The E-mail address entered does not appear to be valid.'); f.txtEmail.focus(); return;}
	}
	
	f.submit();
	return;			
}

function ValidEmail(email)
{
	var re = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
	return re.test(email);	
}

function ValidZip(zip)
{
	zip = TrimSpaces(zip);
	var re = /^(\d{5}-\d{4}|\d{5})$|^([a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d)$/;
	//var re = /^(?(^00000(|-0000))|(\d{5}(|-\d{4})))$/;			
	return re.test(zip);
}

function ValidPhone(area, prefix, suffix)
{
	area = TrimSpaces(area);
	prefix = TrimSpaces(prefix);
	suffix = TrimSpaces(suffix);
	
	var re3 = /^\d{3}$/;
	var re4 = /^\d{4}$/;
	
	if(re3.test(area) && re3.test(prefix) && re4.test(suffix))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function SafeText(s)
{
	s = TrimSpaces(s);
	
	var re = /[^A-Za-z\.\,\s]/; //disallow anything other than a-z, A-Z, period, comma, or space
		
	if(re.test(s))
	{
		return false;
	}
	else
	{
		return true;		
	}
}

function SignUpForEmail()
{
	var f = document.forms[0];
	
	if(TrimSpaces(f.txtFname.value) == "")
	{
		alert("Please provide your First Name.");
		f.txtFname.focus();
		f.txtFname.select();
		return;
	}
	else if(!SafeText(TrimSpaces(f.txtFname.value)))
	{
		alert("Your First Name does not appear to be in valid format.");
		f.txtFname.focus();
		f.txtFname.select();
		return;
	}
	
	if(TrimSpaces(f.txtLname.value) == "")
	{
		alert("Please provide your Last Name.");
		f.txtLname.focus();
		f.txtLname.select();
		return;
	}
	else if(!SafeText(TrimSpaces(f.txtLname.value)))
	{
		alert("Your Last Name does not appear to be in valid format.");
		f.txtLname.focus();
		f.txtLname.select();
		return;
	}
	
	if(!ValidEmail(f.txtEmail.value))
	{
		alert("Your E-mail address does not appear to be valid.");
		f.txtEmail.focus();
		f.txtEmail.select();
		return;
	}
	
	f.submit();
}


function SendRecipe()
{
	var f = document.forms[0];
	
	if(TrimSpaces(f.txtToName.value) == "")
	{
		alert("Please provide the Recipient's Name.");
		f.txtToName.focus();
		f.txtToName.select();
		return;
	}
	else if(!SafeText(TrimSpaces(f.txtToName.value)))
	{

		alert("The Recipient's Name does not appear to be valid.");
		f.txtToName.focus();
		f.txtToName.select();
		return;
	}
	
	if(!ValidEmail(f.txtToEmail.value))
	{
		alert("The Recipient's E-mail does not appear to be valid.");
		f.txtToEmail.focus();
		f.txtToEmail.select();
		return;
	}
	
	if(TrimSpaces(f.txtFromName.value) == "")
	{
		alert("Please provide Your Name.");
		f.txtFromName.focus();
		f.txtFromName.select();
		return;
	}
	else if(!SafeText(TrimSpaces(f.txtFromName.value)))
	{
		alert("Your Name does not appear to be valid.");
		f.txtToName.focus();
		f.txtToName.select();
		return;
	}
	
	if(!ValidEmail(f.txtFromEmail.value))
	{
		alert("Your E-mail does not appear to be valid.");
		f.txtFromEmail.focus();
		f.txtFromEmail.select();
		return;
	}
		
	f.submit();
}

function autotab(ctl)
{
	var key = window.event.keyCode; //They key pressed that fired the onKeyUp event
	var sourceindex = ctl.sourceIndex; //The sourceIndex of the calling element
	var maxlength = ctl.maxLength; //The maxlength defined for the calling element
	//	var iDocLength = document.all.length;		//Total number of page elements (IE syntax)
	
	if (key != 8 && key != 9 && key != 16 && key != 17 && key != 18 && key != 35 && key != 36 && key != 45 && key != 46 && key != 144) 
	{
		if (ctl.value.length == maxlength) 
		{
			document.all[sourceindex + 1].select();
  			document.all[sourceindex + 1].focus();
		}
	}
}

function AutoTabPhone(e, ctl, nextElement)
{
	var key;
	var f = document.forms[0];
		
	if(e && e.which)
		key = e.which;
	else if(window.event && window.event.keyCode)
		key = window.event.keyCode; //IE
	
	if(key != 8 && key != 9 && key != 16 && key != 17 && key != 18 && key != 35 && key != 36 && key != 45 && key != 46 && key != 144) 
	{
		if(ctl.value.length == ctl.maxLength && nextElement != null)
		{			
			//f[nextElement].select();
  			f[nextElement].focus();			
		}
	}
}

function AutoTabZip(e)
{	
	var f = document.forms[0];
	var ctl = f["AddressInput1:txtZip"];
	
	if(e && e.which)
		key = e.which;
	else if(window.event && window.event.keyCode)
		key = window.event.keyCode; //IE
	
	if (key != 8 && key != 9 && key != 16 && key != 17 && key != 18 && key != 35 && key != 36 && key != 45 && key != 46 && key != 144) 
	{
		if (ctl.value.length == ctl.maxLength)
		{
			ctl.blur();
			//f["AddressInput1:selCityState"].focus();
		}
	}
}

function LoadCityState(txt)
{
	var f = document.forms[0];
	var zip = txt.value;
	
	if(zip.length == 5)
	{
		if(zip != f["hidZip"].value)
		{
			ClearCityState();
			f["AddressInput1:btnGetCityStates"].click();
		}
	}
	else
	{
		ClearCityState();
	}
}

function ClearCityState()
{
	var f = document.forms[0];
	f["AddressInput1:txtCity"].value = '';
	f["AddressInput1:txtState"].value = '';
}

function SetCityState(sel)
{
	var f = document.forms[0];
	var i = sel.selectedIndex;	
	f["AddressInput1:txtCity"].value = cities[i];	
	f["AddressInput1:txtState"].value = states[i];	
	f["hidZip"].value = f["AddressInput1:txtZip"].value;	
}
