// NOTE Using this instead of checkZip in Validation.js in order to customize.
function localcheckZip(ZipFld)
{
	var ZipValue = ZipFld.value;
	var ZipPattern = new RegExp("^[0-9]{5}$");
	var ZipCheck = ZipValue.match(ZipPattern);
	if (!(ZipCheck != null))
	{
		alert("Please enter ZIP code as 12345.");
		//setFocus(ZipFld); //Doesnt work if another error pops up first.
		ZipFld.focus();
		return false;
	}
	else
		return true;
}

function ValidateFranchiseForm()
{
	//				alert("ValidateFranchiseForm");
	var bFocused = false;
	var oGroup = document.FranchiseForm.ZipCodeFranchiseMapID;
	for (i = 0; i != oGroup.length; i++)
		if (document.FranchiseForm.ZipCodeFranchiseMapID[i].checked)
			return true;
	document.FranchiseForm.ZipCodeFranchiseMapID[0].focus();
	var franchiseError = document.getElementById("franchiseError");
	franchiseError.innerHTML = 'Please select a provider';
	franchiseError.style.display = "block";
	return false;
}

function ValidateUserAccountForm()
{
	//				alert("ValidateUserAccountForm");
	var bFocused = false;
	if (!document.UserAccountForm.UserAccountID[0].checked)
		return true;
	document.UserAccountForm.UserAccountID[1].focus();
	alert('Please select an account')
	return false;
}

function ValidateZipCodeForm()
{
//				alert("ValidateZipCodeForm");
	var bFocused = false;
	if(!localcheckZip(document.ZipCodeForm.Zip))
		bFocused = true;
	return !bFocused;
}

function ValidateAddressForm()
{
	//				alert("ValidateAddressForm");
	// Check for required elements
	var sError = "Please enter:&#160;";
	var bFocused = false;
	if(document.AddressForm.StreetName.value.trim() == "Street Address" || document.AddressForm.StreetName.value.trim() == "")
	{
		sError += "&#160;Street Address";
		var addressField = document.getElementById("address");
		addressField.className += " error";
		if(!bFocused) {
			addressField.focus();
			bFocused = true;
		}
	}
	
	if(document.AddressForm.Zip.value.trim() == "ZIP Code" || document.AddressForm.Zip.value.trim() == "")
	{
		if(bFocused)
			sError += ",";
		sError += "&#160;ZIP code";
		var zipField = document.getElementById("zipcode");
		zipField.className += " error";
		if(!bFocused) {
			document.AddressForm.Zip.focus();
			bFocused = true;
		}
	}
	
	

	if(!bFocused)		//Check fields for validity
	{
		if(!localcheckZip(document.AddressForm.Zip))
			bFocused = true;
	}
	else
	{
		var errorList = document.getElementById("addressError");
		errorList.innerHTML = sError;
		errorList.style.display = "block";
	}
	
	if(!bFocused)
	{
		if(document.AddressForm.AptNumber.value.trim() == "Apt #")
			document.AddressForm.AptNumber.value = "";
		if(document.AddressForm.StreetName.value.trim() == "Street Address")
			document.AddressForm.StreetName.value = "";
	}
	
	return !bFocused;
}

function CheckHint(sForm)
{
	sField = sForm.UserName;
	sName = sForm.UserName.value;
	//alert("Look: " + sName);

	if(sName == "") {
		alert("Please enter your user name.");
		sForm.UserName.focus();
		return false;
	}
	else {
		if (checkEmail (sField) ) {
			//alert("yes");
			//sForm.action = "../Membership/CheckHint.asp";
			sForm.submit();
		}
		else {
			//alert("no");
			sForm.UserName.focus();
			return false;
		}
	}
}

function ChangeAccount()
{
	var UserAccountId = document.getElementsByName("UserAccountID");
	var selectedValue;
	for(var i = 0; i < UserAccountId.length; i++)
	{
		if(UserAccountId[i].checked == true)
			selectedValue = UserAccountId[i].value;
	}
	if ((selectedValue > 0) && (selectedValue != GetCookie("CurrentAccount", "ID")))
	{
		return true;
	}
	return false;
}

function ValidAccount()
{
	var UserAccountId = document.getElementsByName("UserAccountID");
	var selectedValue;
	for(var i = 0; i < UserAccountId.length; i++)
	{
		if(UserAccountId[i].checked == true)
			selectedValue = UserAccountId[i].value;
	}
	if(selectedValue < 1)
		return false;
	return true;
}

//<Script Language="JavaScript">
// @doc 
	var sValid = "";
		
	/*
	@func bool | CheckReq | Checks to see if form objects have values
	@parm string | sForm | Name of the form to validate
	@parm string | sInput | List all of the Input object you want to validate
	@rdesc Returns true if all inputs have value, sets sValid if they do not
	*/
	function CheckReq(sForm)
	{
		//alert("Called CheckReq() on " + sForm);
		//alert(arguments.length);
		
		var oItem;
		var sTemp = new String();
		
		// Need to change the FOR loop since only 1 arg?
		//for(var i = 1; i < arguments.length; i++) 
		for(var i = 1; i <= arguments.length; i++) 
		{	
			oItem = eval("document." + sForm + "." + arguments[i]);
						
			//alert("check oItem " + oItem);
			
			// oItem.type error: null or not an object
			switch(oItem.type) {
				case "radio":
					if(oItem.selectedIndex == -1)
					{
						sValid += "Some required fields are missing \n";
						oItem.focus();
						return false;
					} else
						break;
				case "select-multiple":
					if(oItem.selectedIndex == -1)
					{
						sValid += "Some required fields are missing \n";
						oItem.focus();
						return false;
					} else
						break;				
				case "select-one":
					if(oItem.selectedIndex == 0)
					{
						sValid += "Some required fields are missing \n";
						oItem.focus();
						return false;
					} else
						break;
				default :
					if(oItem.type) {
						if(!oItem.value) {
							sValid += "Some required fields are missing \n";
							oItem.focus();
							return false;
						}
					} else {
						var bSelected;
						for(var x=0;x < oItem.length;x++)
						{
							if(oItem[x].checked)
								bSelected = true
						}
						if(!bSelected) {
							sValid += "Some required fields are missing \n";
							oItem[0].focus();
							return false;
						}
					}
					break;
			}
		}
		return true;
	}
	
	/*
	@func bool | checkEmail | Compares the value of the object passed in to a Regular Express
	@parm object | oField | Input object to validate
	@rdesc Returns true if compare works, otherwises adds error message to sValid
	*/
	function checkEmailNoAlert (emailFld, ErrorMessage) {
		// Debug
		//alert('Called regular checkEmail');
		if(ErrorMessage == null)
		{
			ErrorMessage = new Object();
		}
		var emailStr = emailFld.value;
	
		/* The following variable tells the rest of the function whether or not
		to verify that the address ends in a two-letter country or well-known
		TLD.  1 means check it, 0 means don't. */
		var checkTLD=1;

		var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
		
		var emailPat=/^(.+)@(.+)$/;
		
		//	Prohibit: `~!#$%^&*()=+{}[]|\'?		Only allow alphanumerics, dash, underscore, @, and periods.
		var specialChars="\\(\\) `~!#$%^&\*\+=\|\/\}'\? ><@,;:\\\\\\\"\\.\\[\\]";
				
		var validChars="\[^\\s" + specialChars + "\]";


		/* The following pattern applies if the "user" is a quoted string (in
			which case, there are no rules about which characters are allowed
			and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
			is a legal e-mail address. */
		var quotedUser="(\"[^\"]*\")";

		/* The following pattern applies for domains that are IP addresses,
			rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
			e-mail address. NOTE: The square brackets are required. */
		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 + ")*$");

		/* The following pattern describes the structure of a normal symbolic
			domain, as opposed to ipDomainPat, shown above. */
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

		var matchArray=emailStr.match(emailPat);
		if (matchArray==null) {
			ErrorMessage.errorString += "Please check your e-mail address for '@' and '.' characters.";
			setFocus(emailFld);
			return false;
		}

		var user=matchArray[1];
		var domain=matchArray[2];

		// Start by checking that only basic ASCII characters are in the strings (0-127).
		for (i=0; i<user.length; i++) {
			if (user.charCodeAt(i)>127) {
				//alert("The e-mail user name contains invalid characters");
				ErrorMessage.errorString += "Please check your e-mail address for invalid characters.";
			return false;
			}
		}

		// Check domain
		for (i=0; i<domain.length; i++) {
			if (domain.charCodeAt(i)>127) {
				//alert("The e-mail domain name contains invalid characters");
				ErrorMessage.errorString += "Please check the domain name of your e-mail address for invalid characters.";
			return false;
			}
		}

		// Check user
		if (user.match(userPat)==null) {
			//alert("The e-mail user name is invalid");
			ErrorMessage.errorString += "Please check your e-mail user name.";
			return false;
		}

		/* If the e-mail address is at an IP address (as opposed to a symbolic
		host name) make sure the IP address is valid. */
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					//alert("The e-mail destination IP address is invalid");
					ErrorMessage.errorString += "Please check your e-mail destination IP address.";
					return false;
				}
			}
		return true;
		}

		// Domain is symbolic name.  Check if it's valid.
		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 e-mail domain name is invalid");
				ErrorMessage.errorString += "Please check your e-mail domain name.";
				return false;
			}
		}

		/* domain name seems valid, but now make sure that it ends in a
			known top-level domain (like com, edu, gov) or a two-letter word,
			representing country (uk, nl). Added ignoreCase March02. */
		if (checkTLD && domArr[domArr.length-1].length!=2 && 
			domArr[domArr.length-1].search(knownDomsPat).ignoreCase==-1) {
			ErrorMessage.errorString += "The e-mail address must end in a well-known domain or two letter country.";
			return false;
		}

		// Make sure there's a host name preceding the domain.
		if (len<2) {
			//alert("This e-mail address is missing a host name");
			ErrorMessage.errorString += "Please check your e-mail address for the host name.";
			return false;
		}

		// If we've gotten this far, everything's valid!

		return true;
	}
	
	
	/*
	@func bool | checkEmail | Compares the value of the object passed in to a Regular Express
	@parm object | oField | Input object to validate
	@rdesc Returns true if compare works, otherwises adds error message to sValid
	*/
	function checkEmail (emailFld) {
		// Debug
		//alert('Called regular checkEmail');
	
		var emailStr = emailFld.value;
	
		/* The following variable tells the rest of the function whether or not
		to verify that the address ends in a two-letter country or well-known
		TLD.  1 means check it, 0 means don't. */
		var checkTLD=1;

		var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
		
		var emailPat=/^(.+)@(.+)$/;
		
		//	Prohibit: `~!#$%^&*()=+{}[]|\'?		Only allow alphanumerics, dash, underscore, @, and periods.
		var specialChars="\\(\\) `~!#$%^&\*\+=\|\/\}'\? ><@,;:\\\\\\\"\\.\\[\\]";
				
		var validChars="\[^\\s" + specialChars + "\]";


		/* The following pattern applies if the "user" is a quoted string (in
			which case, there are no rules about which characters are allowed
			and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
			is a legal e-mail address. */
		var quotedUser="(\"[^\"]*\")";

		/* The following pattern applies for domains that are IP addresses,
			rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
			e-mail address. NOTE: The square brackets are required. */
		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 + ")*$");

		/* The following pattern describes the structure of a normal symbolic
			domain, as opposed to ipDomainPat, shown above. */
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

		var matchArray=emailStr.match(emailPat);
		if (matchArray==null) {
			alert("Please check your e-mail address for '@' and '.' characters");
			setFocus(emailFld);
			return false;
		}

		var user=matchArray[1];
		var domain=matchArray[2];

		// Start by checking that only basic ASCII characters are in the strings (0-127).
		for (i=0; i<user.length; i++) {
			if (user.charCodeAt(i)>127) {
				//alert("The e-mail user name contains invalid characters");
				alert("Please check your e-mail address for invalid characters");
			return false;
			}
		}

		// Check domain
		for (i=0; i<domain.length; i++) {
			if (domain.charCodeAt(i)>127) {
				//alert("The e-mail domain name contains invalid characters");
				alert("Please check the domain name of your e-mail address for invalid characters");
			return false;
			}
		}

		// Check user
		if (user.match(userPat)==null) {
			//alert("The e-mail user name is invalid");
			alert("Please check your e-mail user name");
			return false;
		}

		/* If the e-mail address is at an IP address (as opposed to a symbolic
		host name) make sure the IP address is valid. */
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					//alert("The e-mail destination IP address is invalid");
					alert("Please check your e-mail destination IP address");
					return false;
				}
			}
		return true;
		}

		// Domain is symbolic name.  Check if it's valid.
		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 e-mail domain name is invalid");
				alert("Please check your e-mail domain name");
				return false;
			}
		}

		/* domain name seems valid, but now make sure that it ends in a
			known top-level domain (like com, edu, gov) or a two-letter word,
			representing country (uk, nl). Added ignoreCase March02. */
		if (checkTLD && domArr[domArr.length-1].length!=2 && 
			domArr[domArr.length-1].search(knownDomsPat).ignoreCase==-1) {
			alert("The e-mail address must end in a well-known domain or two letter country");
			return false;
		}

		// Make sure there's a host name preceding the domain.
		if (len<2) {
			//alert("This e-mail address is missing a host name");
			alert("Please check your e-mail address for the host name");
			return false;
		}

		// If we've gotten this far, everything's valid!

		return true;
	}
	
	/*
	@func bool | checkLength | Compares the length property of the oField.value to the input length
	@parm object | oField | Input object to validate
	@parm string | sInput | Label for input object
	@parm int | iLen | Length the validate against
	@rdesc Returns true if compare works, otherwises adds error message to sValid
	*/
	function checkLength(oField, sInput, iLen)
	{
		if(oField.value.length != iLen) 
		{
			sValid += sInput + " must be " + iLen + " characters long \n";
			setFocus(oField);
			return false;
		} else
			return true;
	
	}
	
	/*
	@func bool | checkPasswords | Compares two values for matching and also makes certain that the correct length, and number of letters and numbers is inputed
	@parm object | oOne | Input object to validate
	@parm object | oTwo | Input object to compare to
	@parm int | iMinNumbers | Minimum number of numeric chars
	@parm int | iMinLetters | Minimum number of alphabetic chars
	@parm int | iMinChar | Minimum number of chars over all
	@rdesc Returns true if compare works, otherwises adds error message to sValid
	*/
	function checkPasswords(oOne,oTwo, iMinNumbers, iMinLetters, iMinChar)
	{
		//alert("Called checkPasswords()");
		
		var sNum, sAlpha
		sNums = stripNumber(oOne.value);
		sAlpha = stripChars(oOne.value);
		
		if(oOne.value != oTwo.value)
		{	
			sValid += "Please enter the same value in both password boxes.\n";
			
			
			
			
			alert(sValid);
			setFocus(oOne);
			oOne.select();
			sValid = "";
			return false;
		} 
		// Added max of 12 characters. 
		else if  (sNums.length > 12 || sNums.length < iMinNumbers || sAlpha.length < iMinLetters || oOne.value.trim().length < iMinChar) {
			//sValid += "Passwords must contain at least " + iMinNumbers + " numeric character, " + iMinLetters + " alpha character, and be at least " + iMinChar + " characters long.\n";
			
			
			sValid += "Please edit the password so that it is:\n- From " + iMinChar + " to 12 characters long";
			
			// Conditional message for min chars and min numbers
			if (iMinNumbers > 0)
				sValid += "\n- Contains at least " + iMinNumbers + " number(s)";
			if (iMinLetters > 0)
				sValid += "\n- Contains at least "  + iMinLetters + " letter(s)\n";
			
			alert(sValid);
			setFocus(oOne);
			oOne.select();
			sValid = "";
			return false;
		}
		return true;
	}

	/*
	@func bool | checkPhone | Makes certain that the input value has at least 10 digits
	@parm object | oField | Input object to validate
	@parm string | sInput | Label for input object
	@rdesc Returns true if compare works, otherwises adds error message to sValid
	*/	
	
	/*	NOTE I added the sInput to the alert message since we have 2 phone numbers on the Member form.
		20Nov01 (BLH)
	*/
	
	function checkPhone(oField, sInput) {
		var oPattern = /^\d{3}-\d{3}-\d{4}$/;
		var oPatternExt = /^\d{3}-\d{3}-\d{4}x\d+$/;
		if(oField.value > "" && !(oPattern.test(oField.value) || oPatternExt.test(oField.value))) {
			alert("Please enter " + sInput +" as:\n123-456-7890 or\n123-456-7890x1234\n");
			setFocus(oField);
			//oField.focus();
			return false;
		}

		return true;
	}

	/*
	@func bool | checkNumber | Makes certain that the input value is numeric
	@parm object | oField | Input object to validate
	@parm string | sInput | Label for input object
	@rdesc Returns true if compare works, otherwises adds error message to sValid
	*/		
	function checkNumber(oField, sInput) {
		var oRegExp = /^\d+$/;
		
		if(!oRegExp.test(oField.value) && oField.value > "") {
			sValid = "Please enter a valid number for " + sInput +"\n";
			setFocus(oField);
			//oField.focus();
			return false;
		}
		return true;
	}
	
	/*
	@func bool | checkNumberLength | Makes certain that the input value is numeric and of minimum length length
	@parm object | oField | Input object to validate
	@parm object | iLength | Input field length to validate
	@rdesc Returns true if compare works
	*/		
	function checkNumberLength(oField, iLength) {
		var oRegExp = /^\d+$/;
		if(!oRegExp.test(oField.value) || oField.value.length < iLength) {
			setFocus(oField);
			return false;
		}
		return true;
	}
	
	/*
	@func bool | checkSS | Makes certain that the input value fits this format (xxx-xx-xxxx)
	@parm object | oField | Input object to validate
	@parm string | sInput | Label for input object
	@rdesc Returns true if compare works, otherwises adds error message to sValid
	*/	
	function checkSS(oField, sInput)
	{
		var oPattern = /^\d{3}-\d{2}-\d{4}$/;
		if(!oPattern.test(oField.value) && oField.value > "") {
			sValid += "Please enter a valid Social Security Number for " + sInput + "\n format:(xxx-xx-xxxx) \n";
			setFocus(oField);
			return false;
		}
		return true;
	}
	
	/*
	@func string | stripChars | removes the everything but alphabetic information from a string
	@parm string | theString | String to process
	@rdesc Returns subset of theString containing only alphabetic information
	*/	
	function stripChars(theString)
	{
		var String;
		
		var re = /\d/gi;
		String = theString.replace(re,'');

		return String;
	}
	
	/*
	@func string | stripNumber | removes the everything but numeric information from a string
	@parm string | theString | String to process
	@rdesc Returns subset of theString containing only numeric information
	*/	
	function stripNumber(theNumber){
		var strNum
		
		var re = /\D/gi;
		strNum = theNumber.replace(re,'');

		return strNum;
	}
	
	/*
	@func bool | checkDate | Checks to see if the input value is a valid date. format: (mm/dd/yyyy)
	@parm object | oField | Input object to validate
	@parm string | sInput | Label for input object
	@rdesc Returns true if compare works, otherwises adds error message to sValid
	*/	
	function checkDate(oField,sInput)
	{
		var sDate = new String(oField.value);
		
		var oPattern = /^(\d{1,2})[\/\.-](\d{1,2})[\/\.-](\d+)$/
		var aDate = sDate.match(oPattern);
		if(sDate > "")
		{
			if(aDate == null){
				sValid += "Please enter a valid date for " + sInput + "\nformat:(mm/dd/yyyy) \n";
				setFocus(oField);
				return false;
			}
			if(aDate[3] < 1000) {
				sValid += "Please enter a four digit year for " + sInput + "\nformat:(mm/dd/yyyy) \n";
				setFocus(oField);
				return false;
			}
			if(aDate[1] > 12)
			{
				sValid += "Please enter a valid month for " + sInput + "\nformat:(mm/dd/yyyy) \n";
				setFocus(oField);
				return false;
			} else {
				if(aDate[1] == 1 || aDate[1] == 3 || aDate[1] == 5 || aDate[1] == 7 || aDate[1] == 8 || aDate[1] == 10 || aDate[1] == 12 )
				{
					if(aDate[2] > 31)
					{
						sValid += "Please enter a valid date for " + sInput + "\nformat:(mm/dd/yyyy) \n";
						setFocus(oField);
						return false;
					}
				} else if (aDate[1] == 4 || aDate[1] == 6 || aDate[1] == 9 || aDate[1] == 11)
				{		
					if(aDate[2] > 30)
					{
						sValid += "Please enter a valid date for " + sInput + "\nformat:(mm/dd/yyyy) \n";
						setFocus(oField);
						return false;
					}
				} else {
					var  nDays
					if (aDate[3] % 4 == 0)
						nDays = 29
					else 
						nDays = 28
					
					if(aDate[2] > nDays)
					{
						sValid += "Please enter a valid date for " + sInput + "\nformat:(mm/dd/yyyy) \n";
						setFocus(oField);
						return false;
					}
				}
			}
			return true;
		}		
	}
	
	/*
	@func bool | checkFutureDate | Checks to see if input value is not only a date value, but if it also occurs in the future
	@parm object | oField | Input object to validate
	@parm string | sInput | Label for input object
	@rdesc Returns true if compare works, otherwises adds error message to sValid
	*/		
	function checkFutureDate(oField,sInput)
	{
		if(!checkDate(oField,sInput)){return false;}
		if(oField.value > "")
		{
			var oToday = new Date();
			var oFixedDate = new Date((oToday.getMonth() + 1)  + "/" + oToday.getDate() + "/" + oToday.getYear())
			var oDate = new Date(oField.value)
			if(oDate.valueOf() < oFixedDate.valueOf())
			{
				sValid += "Please enter a future date for " + sInput + "\n";
				setFocus(oField);
				return false;
			}
			return true;
		}
	}
	
	/*
	@func void | setFocus | Sets cursor focus to the first input object to validate false
	@parm object | oField | Input object to set focus to
	*/	
	var bFocused = false;
	function setFocus(oField)
	{
		if (!bFocused) {
			oField.focus();
			bFocused = true;
		}
	}

	/*
	@func bool | checkCreditCard | Checks to see if the input value is a valid credit card numer
	@parm object | oField | Input object to validate
	@parm object | oType | Select box which returns credit card type (ie MasterCard, Visa, AmEx)
	@rdesc Returns true if compare works, otherwises adds error message to sValid
	*/	
	function checkCreditCard(oField, oType) {
		var chk
		
		chk = CheckCC(oField.value, oType.value)
				
		if(chk > 0 && chk < 8)
		{
			sValid += "Please enter a valid Credit Card # \n";
			setFocus(oField);
			return false;
		}

	}

	/*
	@func int | CheckCC | Actually does credit card validation
	@parm string | ccnumber | number to validate
	@parm string | cctype | type of credit card
	@rdesc If the return value is between 1 & 8 then the validation fails
	*/	
	function CheckCC(ccnumber, cctype)
	{  
		var ctype, cclength, ccprefix, x, ch;
		var prefixes, lengths, number, prefixvalid, lengthvalid;
		var qsum, sum, result;

		ctype=cctype.toUpperCase();
		  
	 	switch (ctype) {
			case "VISA":
				cclength="13;16"
				ccprefix="4"
				break;
			case "MASTER":
				cclength="16"
				ccprefix="51;52;53;54;55"
				break;
			case "AMERICANEXPRESS":
				cclength="15"
				ccprefix="34;37"
				break;
			case "C":
				cclength="14"
				ccprefix="300;301;302;303;304;305;36;38"
				break;
			case "DISCOVER":
				cclength="16"
				ccprefix="6011"
				break;
			case "E":
				cclength="15"
				ccprefix="2014;2149"
				break;
			case "J":
				cclength="15;16"
				ccprefix="3;2131;1800"
				break;
			default:
				cclength=""
				ccprefix=""
		}
		  
		prefixes = ccprefix.split(";");
		lengths = cclength.split(";");
		number = stripNumber(ccnumber)
		  
		prefixvalid = false
		lengthvalid = false
		  
		for(var i=0;i < prefixes.length; i++)
		{
			if (number.indexOf(prefixes[i]) == 0)
				prefixvalid = true;
		} 
		   
		for(var i=0;i < lengths.length; i++)
		{
			if (number.length == lengths[i])
				lengthvalid = true
		}
		  
		result = 0;
		  
		if (!prefixvalid)
			result++;
		    
		if (!lengthvalid)
			result += 2;
		
		  
		qsum=0
		  
		for (x = 1; x <= number.length;x++)
		{
			ch = number.substr( number.length - x, 1)
			if (x % 2 == 0) {
				sum = 2 * ch
				qsum += (sum % 10)
				if (sum > 9) { 
					qsum++
				}
			} else {
				qsum = qsum + Number(ch);
			}
		}
		  
		if (qsum % 10 != 0) {
			result += 4
		}
		  
		if (cclength == "") {
			result += 8
		}
		  
		return result
		  
	}
	
	//Short version of credit card validation
	//Does not check card type
	function CheckCCShort(sCardNumber)
	{
		var qsum = 0;
		var sum = 0;
		var number = stripNumber(sCardNumber);
		
		if(number.length == 16 || number.length == 13)
		{
			for (x = 1; x <= number.length;x++)
			{
				ch = number.substr( number.length - x, 1)
				if (x % 2 == 0) {
					sum = 2 * ch
					qsum += (sum % 10)
					if (sum > 9) { 
						qsum++
					}
				} else {
					qsum = qsum + Number(ch);
				}
			}
			if (qsum % 10 != 0) {
				return false;
			}
			else
				return true;
		}
		else if(number.length == 15)
		{
			for(x = 0; x < 14; x++)
			{
				ch = number.substr(x, 1)
				if((x % 2) == 1)
				{
					sum = 2 * ch;
					qsum += sum % 10;
					if(sum > 9)
						qsum += 1;
				}
				else
					qsum += Number(ch);
			}
			//Validation requires subtracting the total from the next
			//highest multiple of 10 to generate the check digit;
			qsum = 1000 - qsum;
			if((qsum % 10) == Number(number.charAt(14)))
				return true;
			else
				return false;
		}
		else
			return false;
		
	}
	
// Added 10Dec01
function checkCity(CityFld)
{
	var CityValue = CityFld.value;
	var CityPattern = new RegExp("[a-z,A-Z]+");
	var CityCheck = CityValue.match(CityPattern);
	if (CityCheck == null)
	{
		alert("Please check spelling of your city");
		setFocus(CityFld);
		//CityFld.focus();
		return false;
	}
	else
		return true;
	
}

// Added 20Nov01
function checkState(StateFld)
{
	var StateValue = StateFld.value;
	var StatePattern = new RegExp("[a-z,A-Z]{2}");
	var StateCheck = StateValue.match(StatePattern);
	if (StateCheck == null)
	{
		alert("Please enter state as 2-letter code");
		setFocus(StateFld);
		//StateFld.focus();
		StateFld.select();
		return false;
	}
	else
		return true;
	
}

// Added 20Nov01 
// Changed 29Nov05
/// checkZip(ZipFld, bShowAlert <optional>)
function checkZip(ZipFld)
{	
	var ZipValue = ZipFld.value;
	var ZipPattern = new RegExp("^[0-9]{5}$");
	var ZipPlusFourPat = new RegExp("^[0-9]{5}-[0-9]{4}$");
	var ZipCheck = ZipValue.match(ZipPattern);
	var ZipPlusFour = ZipValue.match(ZipPlusFourPat);
	if (!(ZipCheck != null || ZipPlusFour != null))
	{
		//	We aren't allowing zip+4; only 5 digits.
		//alert("Please enter a valid zip code.\nFormat xxxxx or xxxxx-xxxx");	
		if(arguments.length == 1 || arguments[1] == true)
		{
			alert("Please enter ZIP Code as 12345");
			setFocus(ZipFld); //Doesnt work if another error pops up first.
		}
		//ZipFld.focus();
		return false;
	}
	else
		return true;
}

// Added 17June03
function checkZipPlusFour(ZipFld)
{	
	var ZipValue = ZipFld.value;
	var ZipPattern = new RegExp("^[0-9]{5}$");
	var ZipPlusFourPat = new RegExp("^[0-9]{5}-[0-9]{4}$");
	var ZipCheck = ZipValue.match(ZipPattern);
	var ZipPlusFour = ZipValue.match(ZipPlusFourPat);
	if (!(ZipCheck != null || ZipPlusFour != null))
	{	
		alert("Please enter a valid ZIP code.\nFormat xxxxx or xxxxx-xxxx");	
		setFocus(ZipFld); //Doesnt work if another error pops up first.
		//ZipFld.focus();
		return false;
	}
	else
		return true;
}
	
	
	/*****NEED THESE FOR TRIM FUNCTIONS******/	
	String.prototype.rightTrim = rightTrim;
	String.prototype.leftTrim = leftTrim;
	String.prototype.trim = trim;

	/*
	@func String Object | rightTrim | Trims trailing white space from string.  Uses prototype definition  "String.prototype.rightTrim = rightTrim;".
	@rdesc Returns Copy of string with trailing whitespace stripped off.
	*/
	function rightTrim() {
		return this.replace(/\s+$/gi, "");
	}

	/*
	@func String Object | leftTrim | Trims leading white space from string. Uses prototype definition  "String.prototype.leftTrim = leftTrim;".
	@rdesc Returns Copy of string with leading whitespace stripped off.
	*/
	function leftTrim() {
		return this.replace(/^\s*/gi, "");
	}

	/*
	@func String Object | trim | Trims leading and trailing white space from string. Uses prototype definition  "String.prototype.trim = trim;".
	@rdesc Returns Copy of string with leading and trailing whitespace stripped off.
	*/

	function trim() {
		
		return this.replace(/^\s+/,'').replace(/\s+$/,'');
	}

	
	/*
		@func bool | checkSpecificEmail | Compares passed email object to a regular expression
		@parm object | EmailFld | Input email address object to validate
		@rdesc Returns true if passed-in address contains comcast.net or attbi.com; otherwise shows alert box
		
		Used on "Refer a Friend" (Products/FGAF_Form.ashx).
	*/
	function checkSpecificEmail(EmailFld)
	{	
		var bSpecific = true;
		var EmailStr = EmailFld.value;
		//alert(EmailStr);
		
		// First validate against illegal characters
		if (!checkEmail(EmailFld)) {
			setFocus(EmailFld);
			bSpecific = false;
			return bSpecific;
		}
		
		var ValidDomain1 = new RegExp("comcast.net");
		var ValidDomain2 = new RegExp("attbi.com");
		
		if (!EmailStr.match(ValidDomain1) && !EmailStr.match(ValidDomain2))
		{
			alert("You must submit a Comcast e-mail address (comcast.net or attbi.com). ");
			setFocus(EmailFld);
			bSpecific = false;
		}
		return bSpecific;
	}
	
	function checkMoney(oField)
	{
		var oRegExp = /^\$?\d+(\.\d{2})?$/;
		
		if(!oRegExp.test(oField.value) && oField.value > "") {
			sValid = "Please enter a valid number for " + oField.value +"\n";
			setFocus(oField);
			//oField.focus();
			return false;
		}
		return true;
	}
	
function clearValue(input, defaultValue) {
	if(input.value == defaultValue) input.value = "";
}
function testValue(input, defaultValue) {
	if(input.value == "") input.value = defaultValue;
}
function retryAddress() {
	if(document.getElementById("accountChange") != null) {
		document.getElementById("accountChange").style.display = "none";
	}
	if(document.getElementById("chooseFranchise") != null) {
		document.getElementById("chooseFranchise").style.display = "none";
	}
	document.getElementById("serviceAddress").style.display = "block";
	document.getElementById("mainHeader").src = "/images/headers/login_hello.gif";
}
/*
var init = function() {
//	startList();
//	setHeaders();

	// Add rollover to localization button
	var locButton = document.getElementById("locButton");
	locButton.onmouseover = function() {
		locButton.src = "../images/onYouGo_over.gif";	
	}
	locButton.onmouseout = buttonRollOff;

	// set mouse over for sign in button
	var signInButton = document.getElementById("signIn");
	signInButton.onmouseover = buttonRollOn;
	signInButton.onmouseout = buttonRollOff;

	//determineInitalContent();
	var currentSession = GetCookie("Session", "Hash");
				
	if (currentSession) {
		document.getElementById("loginSection").style.display = "none";
		document.getElementById("loginSection_animate").style.display = "none";
		document.getElementById("alreadyLoggedInSection").style.display = "block";
	} else {
		document.getElementById("loginSection").style.display = "block";
		document.getElementById("loginSection_animate").style.display = "none";
		document.getElementById("alreadyLoggedInSection").style.display = "none";
	}
}
window.onload = init;
*/