var loaded = 0;

function ImagePreload(path)
{
	if(document.images)
	{
		winoff 			= new Image();
		winoff.src 		= path + "nav_enter_off.gif";
		winon 			= new Image();
		winon.src 		= path + "nav_enter_on.gif";
		fairoff 		= new Image();
		fairoff.src 	= path + "nav_fanfair_off.gif";
		fairon 			= new Image();
		fairon.src 		= path + "nav_fanfair_on.gif";
		paradeoff 		= new Image();
		paradeoff.src 	= path + "nav_parade_off.gif";
		paradeon 		= new Image();
		paradeon.src 	= path + "nav_parade_on.gif";
		grilloff 		= new Image();
		grilloff.src 	= path + "nav_grilling_off.gif";
		grillon 		= new Image();
		grillon.src 	= path + "nav_grilling_on.gif";
		funoff 			= new Image();
		funoff.src 		= path + "nav_morefun_off.gif";
		funon 			= new Image();
		funon.src 		= path + "nav_morefun_on.gif";
		shopoff 		= new Image();
		shopoff.src 	= path + "nav_shopping_off.gif";
		shopon 			= new Image();
		shopon.src 		= path + "nav_shopping_on.gif";
		homeoff 		= new Image();
		homeoff.src 	= path + "nav_home_off.gif";
		homeon 			= new Image();
		homeon.src 		= path + "nav_home_on.gif";
		
		loaded = 1;
	}
}

function imageRoll(imagename, state)
{
	if(loaded == 1)
	{
		document.images[imagename].src = eval(imagename + state + ".src");
	}
}

function CheckNumber(item)
{
	if (isNaN(item.value))
	{
		alert("Please enter a numerical value.");
		item.value = "1";
	}
	return true;
}

function ValidateForm(theform)
{
	var errors;
	errors = 0;
	
	if(theform.ffProductSizeID)
	{
		if(theform.ffProductSizeID.length > 0)
		{
			if(theform.ffProductSizeID[0].type)
			{
				for(var i = 0; i < theform.ffProductSizeID.length; i++) 
				{	
					if((GetSelList(theform.ffProductSizeID[i]) == "")&&(errors == 0)&&(theform.ffProductSizeID[i][0]))
					{
						alert("Please select a size.");
						errors = 1;
						theform.ffProductSizeID[i].focus();
					}
				}
			}
			
			else 
			{
				if(theform.ffProductSizeID.type != "hidden")
				{
					if(GetSelList(theform.ffProductSizeID) == "")
					{
						alert("Please select a size.");
						errors = 1;
						theform.ffProductSizeID.focus();	
					}
				}
			}
		}
	}
	
		
	errors = errors + validate(theform.ffFirstName, 1, 40, "Char", "Please enter First Name.", errors);
	errors = errors + validate(theform.ffLastName, 1, 40, "Char", "Please enter Last Name.", errors);
	errors = errors + validate(theform.ffEmail, 1, 40, "Email", "Please enter Email Address.", errors);
	errors = errors + validate(theform.ffPhoneArea, 3, 3, "Number", "Please enter Phone Area Code.", errors);
	errors = errors + validate(theform.ffPhonePrefix, 3, 3, "Number", "Please enter Phone Prefix.", errors);
	errors = errors + validate(theform.ffPhoneSuffix, 4, 4, "Number", "Please enter Phone Suffix.", errors);
	errors = errors + validate(theform.ffBillAddress1, 2, 40, "Char", "Please enter Billing Address 1.", errors);
	//errors = errors + validate(theform.ffBillAddress2, 2, 40, "Char", "Please enter Billing Address 2.", errors);
	errors = errors + validate(theform.ffBillCity, 2, 40, "Char", "Please enter Billing City.", errors);
	errors = errors + validate(theform.ffBillState, 0, 0, "", "Please select Billing State.", errors);
	errors = errors + validate(theform.ffBillZip, 5, 10, "Char", "Please enter Billing Zip.", errors);
	errors = errors + validate(theform.ffShipAddress1, 2, 40, "Char", "Please enter Shipping Address 1.", errors);
	//errors = errors + validate(theform.ffShipAddress2, 2, 40, "Char", "Please enter Billing Address 2.", errors);
	errors = errors + validate(theform.ffShipCity, 2, 40, "Char", "Please enter Shipping City.", errors);
	errors = errors + validate(theform.ffShipState, 0, 0, "", "Please select Shipping State.", errors);
	errors = errors + validate(theform.ffShipZip, 5, 10, "Char", "Please enter Shipping Zip.", errors);
	errors = errors + validate(theform.ffCreditCardType, 0, 0, "", "Please select Credit Card Type.", errors);
	
	if (theform.ffCreditCardType.value != '5' && theform.ffCreditCardType.value != '6'){
		errors = errors + validate(theform.ffCreditCardNumber, 13, 16, "Number", "Please enter a valid Credit Card Number (No Spaces or Dashes).", errors);
	}
	errors = errors + validate(theform.ffCreditCVV, 3, 10, "Char", "Please enter Card Code.", errors);
	/*
	if (! validateZIP(theform.ffBillZip,"Please enter Billing Zip.")){
		errors = errors + 1
	}

	if (! validateZIP(theform.ffShipZip,"Please enter Shipping Zip.")){
		errors = errors + 1
	}
		*/
	if (errors == 0)
	{
		theform.submit();
	}
	
}

function validateZIP(field,msg) {
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
alert(msg);
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert(msg);
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
return false;
   }
}
return true;
}




function UpdateShip()
{
	var frm = document.forms[0];
	var BillAddr1 = frm.ffBillAddress1.value;
	var BillAddr2 = frm.ffBillAddress2.value;
	var BillCity = frm.ffBillCity.value;
	var BillState = GetSelList(frm.ffBillState);
	var BillZip = frm.ffBillZip.value;
	
	if (frm.ffSameAsBill.checked)
	{
		frm.ffShipAddress1.value = BillAddr1;
		frm.ffShipAddress2.value = BillAddr2;
		frm.ffShipCity.value = BillCity;
		SetSelList(frm.ffShipState, BillState);
		frm.ffShipZip.value = BillZip;
	}
	
	else
	{
		frm.ffShipAddress1.value = "";
		frm.ffShipAddress2.value = "";
		frm.ffShipCity.value = "";
		SetSelList(frm.ffShipState, "");
		frm.ffShipZip.value = "";
	}
}

function validate(obj, MinLength, MaxLength, InputType, Message, Errors)
{
	if (Errors == 0){
	
		if ((obj.type == "text")||(obj.type == "password"))
		{
			if ((obj.value.length < MinLength)||(obj.value.length > MaxLength)) 
			{
				alert(Message);
				obj.focus();
				return 1;
			}
	
			if (InputType == "Number")
			{	
				if (isNaN(obj.value))
				{
					alert("Please enter a numerical value.");
					obj.focus();
					obj.selected;
					return 1;
				}
				if (obj.value == "")
				{
					alert(Message);
					obj.focus();
					return 1;
				}
			}
	
			else if (InputType == "Email")
			{
				if (!isEmail(obj.value))
				{
					alert("Please enter a valid email address.");
					obj.focus();
					obj.selected;
					return 1;
				}
				if (obj.value == "")
				{
					alert(Message);
					obj.focus();
					return 1;
				}
			}
			
			else if ((InputType == "Date"))
			{
				if (!IsDate(obj.value))
				{
					alert("Please enter a valid date (MM/DD/YYYY).");
					obj.focus();
					obj.selected;
					return 1;
				}
				if (obj.value == "")
				{
					alert(Message);
					obj.focus();
					return 1;
				}
			}
		}
		
		else if (obj.type == "select-one")
		{
			if (GetSelList(obj).length == 0)
			{
				alert(Message);
				obj.focus();
				return 1;
			}
		}
	}
	return 0;	
}

function GetSelList(list) 
{
	var value = "";
	for (var i = 0; i < list.length; i++) 
	{
		if (list[i].selected) 
		{
			value = list[i].value;
		}
	}
	return value;
}

function SetSelList(list, setval) 
{
	// Set the selected list item's value
	
	if (setval == "")
	{
		list[0].selected = true;
	}
	else
	{
		for (var i = 0; i < list.length; i++) 
		{
			if (list[i].value == setval) 
			{
				list[i].selected = true;
			}
		}
	}
}

function isEmail(emailStr) 
{
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		//alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}

	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			//alert("The username contains invalid characters.");
			return false;
		}
	}

	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			//alert("The domain name contains invalid characters.");
			return false;
		}
	}
 
	if (user.match(userPat)==null) {
		//alert("The username doesn't seem to be valid.");
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	
	if (IPArray!=null) {

		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				//alert("Destination IP address is invalid!");
				return false;
			}
		}
	
		return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			//alert("The domain name does not seem to be valid.");
			return false;
		}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		//alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}

	if (len<2) {
		//alert("This address is missing a hostname!");
		return false;
	}

	return true;
}

var newWindow;

function PopupPage(url, width, height)
{
	// Find left and top coordinates to center window
	if (navigator.appVersion.indexOf("MSIE") > -1 || (navigator.appName == "Netscape" && parseInt(navigator.appVersion.charAt(0)) >= 4))
	{
		var h = (screen.availHeight - height) / 2;
		var w = (screen.availWidth - width) / 2;
		var center = ",top=" + h + ",left=" + w;
	}
		
	// Is there a search window open now?
	if (!newWindow || newWindow.closed)
	{
		winSurvey = window.open(url,"newWindow","resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,height=" + height + ",width=" + width + center);
	}
	else	
	{
		// Close current search window and spawn another
		newWindow.close();
		newWindow = window.open(url,"newWindow","resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,height=" + height + ",width=" + width + center);
	}
}

function EmptyForm()
{

	var frm = document.forms[0];

	frm.ffFirstName.value = "";
	frm.ffLastName.value = "";
	frm.ffEmail.value = "";
	frm.ffPhoneArea.value = "";
	frm.ffPhonePrefix.value = "";
	frm.ffPhoneSuffix.value = "";
	frm.ffBillAddress1.value = "";
	frm.ffBillAddress2.value = "";
	frm.ffBillCity.value = "";
	SetSelList(frm.ffBillState, "");
	frm.ffBillZip.value = "";
	frm.ffShipAddress1.value = "";
	frm.ffShipAddress2.value = "";
	frm.ffShipCity.value = "";
	SetSelList(frm.ffShipState, "");
	frm.ffShipZip.value = "";
	SetSelList(frm.ffCreditCardType, "");
	frm.ffCreditCardNumber.value = "";
	SetSelList(frm.ffCreditExpMonth, "");
	SetSelList(frm.ffCreditExpYear, "");

}
