// JavaScript Document
function showshipping() { 
	//alert(document.frmRegister.chkshipping.checked);
	var seldiv = document.getElementById('divshipping');
	
	// shipping same
	if (document.frmRegister.chkshipping.checked == true) {
		seldiv.style.display = "none";
		document.frmRegister.SFirstname.value = '';
		document.frmRegister.SLastname.value = '';
		document.frmRegister.SAddress1.value = '';
		document.frmRegister.SAddress2.value = '';
		document.frmRegister.SCity.value = '';
		document.frmRegister.SZip.value = '';
	}
	else {
		seldiv.style.display = "inline";
	}
}

function LoginCheck(){ 
	//Ensures user completes data entry forms
	// regular expression for string that only contains 0 or more whitespace chars
	// i.e. a sophisticated "blank field" check
	var reBlank = /^\s*$/;
	var strValue;
  
	strValue = document.frmLogin.LEmail.value;
	if (reBlank.test(strValue)){
		alert("Please enter your Username.");
		document.frmLogin.LEmail.focus();
		return false;
	}
		
	strValue = document.frmLogin.LPassword.value;
	if (reBlank.test(strValue)){
		alert("Please enter your Password");
		document.frmLogin.LPassword.focus();
		return false;
	}
}

function RegisterCheck(iType){ 
	//Ensures user completes data entry forms
	// regular expression for string that only contains 0 or more whitespace chars
	// i.e. a sophisticated "blank field" check
	var reBlank = /^\s*$/;
	var strValue;
  
 	//full register  or Email/Password only
	if ((iType == 0) || (iType == 2)) {
		strValue = document.frmRegister.Email.value;
		if (reBlank.test(strValue)){
			alert("Please enter your Username.");
			document.frmRegister.Email.focus();
			return false;
		}
		
		strValue = document.frmRegister.Password.value;
		if (reBlank.test(strValue)){
			alert("Please enter your Password");
			document.frmRegister.Password.focus();
			return false;
		}

		
		if (strValue.length < 5 ) {
			alert("Password must be at least 5 characters.");
			document.frmRegister.Password.focus();
			return false;	
		}

		strValue = document.frmRegister.CPassword.value;
		if (reBlank.test(strValue)){
			alert("Please re-enter your Password.");
			document.frmRegister.CPassword.focus();
			return false;
		}

		if (document.frmRegister.Password.value != document.frmRegister.CPassword.value){
			alert("Passwords do not match.");
			document.frmRegister.CPassword.focus();
			return false;
		}

		if (!emailCheck(document.frmRegister.Email.value)){
			alert("Email Address doesn't seem to be valid.");		  
			document.frmRegister.Email.focus();
			return false;
		}
	}
	
	//Full register or enter more info
	if ((iType == 0) || (iType == 1)) { 

		strValue = document.frmRegister.Firstname.value;
		if (reBlank.test(strValue)){
			alert("Please enter FirstName.");
			document.frmRegister.Firstname.focus();
			return false;
		}

		strValue = document.frmRegister.Lastname.value;
		if (reBlank.test(strValue)){
			alert("Please enter Lastname.");
			document.frmRegister.Lastname.focus();
			return false;
		}

		strValue = document.frmRegister.BAddress1.value;
		if (reBlank.test(strValue)){
			alert("Please enter Billing Address 1.");
			document.frmRegister.BAddress1.focus();
			return false;
		}

		strValue = document.frmRegister.BCity.value;
		if (reBlank.test(strValue)){
			alert("Please enter Billing City.");
			document.frmRegister.BCity.focus();
			return false;
		}

		strValue = document.frmRegister.BZip.value;
		if (reBlank.test(strValue)){
			alert("Please enter Billing Zip Code.");
			document.frmRegister.BZip.focus();
			return false;
		}	
		
		//Shipping not the same as billing
		if (document.frmRegister.chkshipping.checked != true) { 
			strValue = document.frmRegister.SFirstname.value;
			if (reBlank.test(strValue)){
				alert("Please enter Shipping FirstName.");
				document.frmRegister.SFirstname.focus();
				return false;
			}

			strValue = document.frmRegister.SLastname.value;
			if (reBlank.test(strValue)){
				alert("Please enter Shipping Lastname.");
				document.frmRegister.SLastname.focus();
				return false;
			}

			strValue = document.frmRegister.SAddress1.value;
			if (reBlank.test(strValue)){
				alert("Please enter Shipping Address 1.");
				document.frmRegister.SAddress1.focus();
				return false;
			}

			strValue = document.frmRegister.SCity.value;
			if (reBlank.test(strValue)){
				alert("Please enter Shipping City.");
				document.frmRegister.SCity.focus();
				return false;
			}

			strValue = document.frmRegister.SZip.value;
			if (reBlank.test(strValue)){
				alert("Please enter Shipping Zip Code.");
				document.frmRegister.SZip.focus();
				return false;
			}	
		}
	}
}

function emailCheck(strEmail) {
	/* 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 emailStr = strEmail.toLowerCase();
				
	var checkTLD=1;

	/* The following is the list of known TLDs that an e-mail address must end with. */
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	var emailPat=/^(.+)@(.+)$/;

	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	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})\]$/;

	/* The following string represents an atom (basically a series of non-special characters.) */
	var atom=validChars + '+';

	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")";

	// The following pattern describes the structure of the user
	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 +")*$");

	/* Finally, let's start trying to figure out if the supplied address is valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		//alert("Email address seems incorrect (check @ and .'s)");
		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 username in the email address contains invalid characters.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			//alert("Ths domain name in the email address contains invalid characters.");
			return false;
		}
	}

	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		//alert("The username in the email address doesn't seem to be valid.");
		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) {
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				//alert("Destination IP address is invalid!");
				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 domain name does not seem to be valid.");
			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), and that there's a hostname preceding 
	the domain or country. */
	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;
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
		//alert("This address is missing a hostname!");
		return false;
	}
	// If we've gotten this far, everything's valid!
	return true;
}

function SubmitOrderCheck(theform){ 
	//Ensures user completes data entry forms
	// regular expression for string that only contains 0 or more whitespace chars
	// i.e. a sophisticated "blank field" check
	var reBlank = /^\s*$/;
	var strValue;
  	
	strValue = document.frmBilling.SFirstname.value;
	if (reBlank.test(strValue)){
		alert("Please enter Shipping FirstName.");
		document.frmBilling.SFirstname.focus();
		return false;
	}

	strValue = document.frmBilling.SLastname.value;
	if (reBlank.test(strValue)){
		alert("Please enter Shipping Lastname.");
		document.frmBilling.SLastname.focus();
		return false;
	}

	strValue = document.frmBilling.SAddress1.value;
	if (reBlank.test(strValue)){
		alert("Please enter Shipping Address 1.");
		document.frmBilling.SAddress1.focus();
		return false;
	}

	strValue = document.frmBilling.SCity.value;
	if (reBlank.test(strValue)){
		alert("Please enter Shipping City.");
		document.frmBilling.SCity.focus();
		return false;
	}

	strValue = document.frmBilling.SZip.value;
	if (reBlank.test(strValue)){
		alert("Please enter Shipping Zip Code.");
		document.frmBilling.SZip.focus();
		return false;
	}

	if (document.frmBilling.CardType.selectedIndex == 0) {
		alert("Please select Card Type.");
		document.frmBilling.CardType.focus();
		return false;	
	}
	
	strValue = document.frmBilling.CardNumber.value;
	if (reBlank.test(strValue)){
		alert("Please enter Card Number.");
		document.frmBilling.CardNumber.focus();
		return false;
	}

	
	if (document.frmBilling.CardExpMonth.selectedIndex == 0) {
		alert("Please select Card Expiration Month.");
		document.frmBilling.CardExpMonth.focus();
		return false;
	}


	if (document.frmBilling.CardExpYear.selectedIndex == 0) {
		alert("Please select Card Expiration Year.");
		document.frmBilling.CardExpYear.focus();
		return false;
	}
	
	strValue = document.frmBilling.NameOnCard.value;
	if (reBlank.test(strValue)){
		alert("Please enter Name On Card.");
		document.frmBilling.NameOnCard.focus();
		return false;
	}

	var iCardType = document.frmBilling.CardType.selectedIndex;
	var sFirstChar = document.frmBilling.CardNumber.value.charAt(0)
	var iCardLength = document.frmBilling.CardNumber.value.length 
	
	if ((iCardType == 3 && iCardLength != 15) || (iCardType != 3 && iCardLength != 16)) {
		alert('Please enter a valid Credit Card Number.');
		document.frmBilling.CardNumber.focus();
		return false;	
	}	
	
	if ( (iCardType == 1 && sFirstChar != '4') || (iCardType == 2 && sFirstChar != '5') || (iCardType == 3 && sFirstChar != '3') || (iCardType == 4 && sFirstChar != '6') ) {
		alert('Card Number does not match Card Type.');
		document.frmBilling.CardNumber.focus();
		return false;
	}	
	
	var ThisDate = new Date();
	var ThisMonth = ThisDate.getMonth() + 1;
	var ThisYear = ThisDate.getYear();
	var CutDate
	
	if (ThisMonth < 10) {
		CutDate =  ThisYear + '0' + ThisMonth; }
	else {
		CutDate =  ThisYear + '' + ThisMonth; }
	
	var CardExpYear = document.frmBilling.CardExpYear.options[document.frmBilling.CardExpYear.selectedIndex].value;
	var CardExpMonth = document.frmBilling.CardExpMonth.options[document.frmBilling.CardExpMonth.selectedIndex].value;

	if ((CardExpYear + CardExpMonth) < CutDate) {
		alert('Card is expired.');
		document.frmBilling.CardExpYear.focus();
		return false;			
	}

	ProcessOrderMessage();
	
	//disableForm(theform);
		
}


function ReSubmitOrderCheck(theform){ 
	//Ensures user completes data entry forms
	// regular expression for string that only contains 0 or more whitespace chars
	// i.e. a sophisticated "blank field" check
	var reBlank = /^\s*$/;
	var strValue;

	if (document.frmBilling2.CardType.selectedIndex == 0) {
		alert("Please select Card Type.");
		document.frmBilling2.CardType.focus();
		return false;	
	}
	
	strValue = document.frmBilling2.CardNumber.value;
	if (reBlank.test(strValue)){
		alert("Please enter Card Number.");
		document.frmBilling2.CardNumber.focus();
		return false;
	}
	

	if (document.frmBilling2.CardExpMonth.selectedIndex == 0) {
		alert("Please select Card Expiration Month.");
		document.frmBilling2.CardExpMonth.focus();
		return false;
	}


	if (document.frmBilling2.CardExpYear.selectedIndex == 0) {
		alert("Please select Card Expiration Year.");
		document.frmBilling2.CardExpYear.focus();
		return false;
	}

	strValue = document.frmBilling2.NameOnCard.value;
	if (reBlank.test(strValue)){
		alert("Please enter Name On Card.");
		document.frmBilling2.NameOnCard.focus();
		return false;
	}

	var iCardType = document.frmBilling2.CardType.selectedIndex;
	var sFirstChar = document.frmBilling2.CardNumber.value.charAt(0)
	var iCardLength = document.frmBilling2.CardNumber.value.length 
	
	if ((iCardType == 3 && iCardLength != 15) || (iCardType != 3 && iCardLength != 16)) {
		alert('Please enter a valid Credit Card Number.');
		document.frmBilling2.CardNumber.focus();
		return false;	
	}	
	
	if ( (iCardType == 1 && sFirstChar != '4') || (iCardType == 2 && sFirstChar != '5') || (iCardType == 3 && sFirstChar != '3') || (iCardType == 4 && sFirstChar != '6') ) {
		alert('Card Number does not match Card Type.');
		document.frmBilling2.CardNumber.focus();
		return false;
	}	
	
	var ThisDate = new Date();
	var ThisMonth = ThisDate.getMonth() + 1;
	var ThisYear = ThisDate.getYear();
	var CutDate
	
	if (ThisMonth < 10) {
		CutDate =  ThisYear + '0' + ThisMonth; }
	else {
		CutDate =  ThisYear + '' + ThisMonth; }
	
	var CardExpYear = document.frmBilling2.CardExpYear.options[document.frmBilling2.CardExpYear.selectedIndex].value;
	var CardExpMonth = document.frmBilling2.CardExpMonth.options[document.frmBilling2.CardExpMonth.selectedIndex].value;


	if ((CardExpYear + CardExpMonth) < CutDate) {
		alert('Card is expired.');
		document.frmBilling2.CardExpYear.focus();
		return false;			
	} 		

	ProcessOrderMessage();
	
	//disableForm(theform);
}


function ShowPasswordHint() {
	var windowSettings="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,"+
								"resizable=no,width=440,height=330,top=200,left=250";
	var strEmail = document.frmLogin.LEmail.value;	
	window.open('passwordhint.asp?u=' + strEmail,'',windowSettings);
}

function PrintOrder() {
	var windowSettings="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,"+
								"resizable=yes,width=660,height=600,top=10,left=250";
	
	window.open('confirmprint.asp','',windowSettings);
}

function ProcessOrderMessage()	{
	document.all.pleasewaitScreen.style.pixelTop = (document.body.scrollTop + 50);
	document.all.pleasewaitScreen.style.visibility="visible";
	return true;
}	

function disableForm(theform) {
if (document.all || document.getElementById) {
for (i = 0; i < theform.length; i++) {
var tempobj = theform.elements[i];
tempobj.disabled = true;
}}}

//-->

  

