
function SameAsShipping(IsSame) {

	if (IsSame) {
		BillingForm.BillFirstName.value = BillingForm.ShipFirstName.value;
		BillingForm.BillLastName.value = BillingForm.ShipLastName.value;
		BillingForm.BillAddress1.value = BillingForm.ShipAddress1.value;
		BillingForm.BillAddress2.value = BillingForm.ShipAddress2.value;
		BillingForm.BillAddress3.value = BillingForm.ShipAddress3.value;
		BillingForm.BillCity.value = BillingForm.ShipCity.value;
		BillingForm.BillState.value = BillingForm.ShipState.value;
		BillingForm.BillZip.value = BillingForm.ShipZip.value;
		BillingForm.BillCountry.value = BillingForm.ShipCountry.value;
		BillingForm.BillPhone.value = BillingForm.ShipPhone.value;
	} else {
		BillingForm.BillFirstName.value = '';
		BillingForm.BillLastName.value = '';
		BillingForm.BillAddress1.value = '';
		BillingForm.BillAddress2.value = '';
		BillingForm.BillAddress3.value = '';
		BillingForm.BillCity.value = '';
		BillingForm.BillState.value = '';
		BillingForm.BillZip.value = '';
		BillingForm.BillCountry.value = '';
		BillingForm.BillPhone.value = '';
	}
}

function CheckBilling() {
	if (BillingForm.EmailAddress.value == '') {
		alert('We require a valid email address to send our order confirmation to.');
		return false;
	} else if (BillingForm.ShipFirstName.value == '') {
		alert('You must enter a Shipping First Name');
		return false;
	} else if (BillingForm.ShipLastName.value == '') {
		alert('You must enter a Shipping Last Name');
		return false;
	} else if (BillingForm.ShipAddress1.value == '') {
		alert('You must enter a Shipping Address');
		return false;
	} else if (BillingForm.ShipCity.value == '') {
		alert('You must enter a Shipping City');
		return false;
	} else if (BillingForm.ShipZip.value == '') {
		alert('You must enter a Shipping Zip code (postal code)');
		return false;
	} else if (BillingForm.ShipCountry.value == '') {
		alert('You must enter a Shipping Country');
		return false;
	} else if (BillingForm.BillFirstName.value == '') {
		alert('You must enter a Billing First Name');
		return false;
	} else if (BillingForm.BillLastName.value == '') {
		alert('You must enter a Billing Last Name');
		return false;
	} else if (BillingForm.BillAddress1.value == '') {
		alert('You must enter a Billing Address');
		return false;
	} else if (BillingForm.BillCity.value == '') {
		alert('You must enter a Billing City');
		return false;
	} else if (BillingForm.BillZip.value == '') {
		alert('You must enter a Billing Zip code (postal code)');
		return false;
	} else if (BillingForm.BillCountry.value == '') {
		alert('You must enter a Billing Country');
		return false;
	} else {
		 if (emailCheck(BillingForm.EmailAddress.value) == false) {
			return false;
		} else {
			return true;
		}
	}
}

function CheckPayment() {
	if (PaymentForm.CardType.value == '') {
		alert('You must select a Card Type');
		return false;
	} else if (PaymentForm.ExpMonth.value == '') {
		alert('You must enter an Expiration Month');
		return false;
	} else if (PaymentForm.ExpYear.value == '') {
		alert('You must enter an Expiration Year');
		return false;
	} else if (PaymentForm.CardNumber.value == '') {
		alert('You must enter a Card Number');
		return false;
	} else if (PaymentForm.NameOnCard.value == '') {
		alert('You must enter the name as it appears on your card.');
		return false;
	} else if (PaymentForm.CVVCode.value == '') {
		alert('You must enter a Security Code');
		return false;
	} else {

		var ccnum = PaymentForm.CardNumber.value;

		'Check validity of card number.'
		if (PaymentForm.CardType.value == "VISA") {
			// Visa: length 16, prefix 4, dashes optional.
			var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;

			if (!re.test(ccnum)) {
				alert('Invalid VISA card number.');
				return false;
			}

		} else if (PaymentForm.CardType.value == "MasterCard") {
			// Mastercard: length 16, prefix 51-55, dashes optional.
			var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;

			if (!re.test(ccnum)) {
				alert('Invalid MasterCard number.');
				return false;
			}
		}

		// Remove all dashes for the checksum checks to eliminate negative numbers
		ccnum = ccnum.split("-").join("");

		// Checksum ("Mod 10")
		// Add even digits in even length strings or odd digits in odd length strings.
		var checksum = 0;
		for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
			checksum += parseInt(ccnum.charAt(i-1));
		}

		// Analyze odd digits in even length strings or even digits in odd length strings.
		for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
			var digit = parseInt(ccnum.charAt(i-1)) * 2;
			if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
		}

		if ((checksum % 10) == 0) {
			return true;
		} else {
			alert('Invalid card number.');
			return false;
		}

	}
}

function ShowDetail(url) {
	PopUpWin = window.open('', 'Detail'+Math.floor(Math.random()*10001),'status=0,menubar=0,toolbar=0,location=0,resizable=0,dependent=1,scrollbars=0,width=430,height=430,screenX=100,screenY=200,left=100,top=200');
	PopUpWin.document.write("<html><body bgcolor='#000000'>");
	PopUpWin.document.write('<img src="'+url+'" width=400 height=400 alt="" style="border: 1px solid #ffffff;">');
	PopUpWin.document.write("</body></html>");
	if (window.focus) {
		PopUpWin.focus();
	}
}

function emailCheck(emailStr) {

	/* 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;

	/* 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("Username in email address contains invalid characters.");
			return false;
		}
	}

	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Domain in 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("Email 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("Email address is missing a hostname.");
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	} else {
		countfield.value = maxlimit - field.value.length;
	}
}

function CheckFeedback() {

	if (document.FeedBack.name.value == '') {
		alert('Your name is required.');
		return false;
	}

	if (document.FeedBack.email.value == '') {
		alert('Your email address is required.');
		return false;
	}

	if (document.FeedBack.comments.value == '') {
		alert('Your comments are required.');
		return false;
	}

	if (emailCheck(document.FeedBack.email.value) == false) {
		return false;
	}

	return true;

}

function CheckSignup() {

	if (document.WholeSaleSignup.FirstName.value == '') {
		alert('Your first name is required.');
		return false;
	}

	if (document.WholeSaleSignup.LastName.value == '') {
		alert('Your last name is required.');
		return false;
	}

	if (document.WholeSaleSignup.Email.value == '') {
		alert('Your email address is required.');
	}

	if (emailCheck(document.WholeSaleSignup.Email.value) == false) {
		return false;
	}

	if (document.WholeSaleSignup.LoginName.value.length < 8) {
		alert('A login name of minimal 8 letters and/or numbers is required.');
		return false;
	}

	if (document.WholeSaleSignup.Password.value.length < 8) {
		alert('A password of minimal 8 letters and/or numbers is required.');
		return false;
	}

	if (document.WholeSaleSignup.CompanyName.value == '') {
		alert('Your company name is required.');
		return false;
	}

	if (document.WholeSaleSignup.TaxID.value == '') {
		alert('Your Tax ID is required.');
		return false;
	}

	if (document.WholeSaleSignup.Address1.value == '') {
		alert('Your address is required.');
		return false;
	}

	if (document.WholeSaleSignup.City.value == '') {
		alert('Your city is required.');
		return false;
	}

	if (document.WholeSaleSignup.State.value == '') {
		alert('Your state is required.');
		return false;
	}

	if (document.WholeSaleSignup.Zip.value == '') {
		alert('Your zip code is required.');
		return false;
	}

	if (document.WholeSaleSignup.PhoneAreaCode.value == '' || document.WholeSaleSignup.PhoneNumber.value == '') {
		alert('Your company phone number is required.');
		return false;
	}

	return true;

}

// Limits characters in a text field to numbers.
//----------------------------------------------------------------
function CheckNums() {
	var key = window.event ? event.keyCode : event.which;
	if (key < 48 || key > 57) {
		event.returnValue = false;
	}
}
//----------------------------------------------------------------

// Limits characters in text field to: [0-9] and -
//----------------------------------------------------------------
function CheckSSN() {
	if (event.keyCode < 45 || (event.keyCode > 45 && event.keyCode < 48) || event.keyCode > 57) {
		event.returnValue = false;
	}
}
//----------------------------------------------------------------

// Limits characters in text field to: [0-9] and . and -
//----------------------------------------------------------------
function CheckMoney() {
	if (event.keyCode < 45 || (event.keyCode > 46 && event.keyCode < 48) || event.keyCode > 57) {
		event.returnValue = false;
	}
}
//----------------------------------------------------------------

// Limits characters in text field to: [0-9], -, () and space.
//----------------------------------------------------------------
function CheckPhone() {
	if (event.keyCode < 32 || (event.keyCode > 32 && event.keyCode < 40) || (event.keyCode > 41 && event.keyCode < 45) || (event.keyCode > 45 && event.keyCode < 48) || event.keyCode > 57) {
		event.returnValue = false;
	}
}
//----------------------------------------------------------------

// Limits characters in a text field to: [0-9] and /
// Allows submission of form with enter key.
//----------------------------------------------------------------
function CheckDate() {
	if (event.keyCode == 13) {
		event.returnValue = true;
	} else if (event.keyCode < 47 || event.keyCode > 57) {
		event.returnValue = false;
	}
}
//----------------------------------------------------------------

//Switch the CSS class for an element.
//----------------------------------------------------------------
function Switch(Element,ClassName){
	if (eval(document.getElementById(Element))) {
		document.getElementById(Element).className = ClassName;
	}
}
//----------------------------------------------------------------

// Reverses the string it is given.
//----------------------------------------------------------------
function strrev(str) {
	if (!str) return '';
	var revstr='';
	for (i = str.length-1; i>=0; i--)
	revstr+=str.charAt(i)
	return revstr;
}
//----------------------------------------------------------------

// Converts decimal to hexadecimal.
//----------------------------------------------------------------
function Dec2Hex(decimal) {
	if (decimal > 65535) {
		return ("err!")
	}
	first  = Math.round(decimal/4096 - .5);
	temp1  = decimal - first * 4096;
	second = Math.round(temp1/256 - .5);
	temp2  = temp1 - second * 256;
	third  = Math.round(temp2/16 - .5);
	fourth = temp2 - third * 16;
	return ("" + GetLetter(third) + GetLetter(fourth));
}

function GetLetter(Number) {
	if (Number < 10) {
		return Number;
	} else {
		if (Number == 10) { return "A" }
		if (Number == 11) { return "B" }
		if (Number == 12) { return "C" }
		if (Number == 13) { return "D" }
		if (Number == 14) { return "E" }
		if (Number == 15) { return "F" }
	}
}
//----------------------------------------------------------------

// Converts an ASCII character to hexadecimal.
//----------------------------------------------------------------
function Transform(ascii) {
	var hex=''
	var i
	for (i=0; i<ascii.length; i++) {
		hex += Dec2Hex(ascii.charCodeAt(i))
	}
	return hex
}
//----------------------------------------------------------------

// Limits characters in a text field to those accepted by us.
//----------------------------------------------------------------
function CheckInput() {
	var NotAllowed = "|58|59|60|61|62|63|64|91|92|93|94|95|96|";
	var key = window.event ? event.keyCode : event.which;

	if (key != 13 && ((key < 48 || key > 122) || (NotAllowed.indexOf("|"+key+"|") > -1))) {
		event.returnValue = false;
	}
}
//----------------------------------------------------------------

// Checks if both login and password are filled in and
// that the password is at least 8 characters long when
// the login form is submitted.
//----------------------------------------------------------------
function CheckLogin() {

	if(LoginForm.UserName.value==''){
		alert('You must enter a User ID.');
		return false;
	} else if (LoginForm.PassWord.value.length < 8) {
		alert('You must enter a password of at least 8 characters.');
		return false;
	} else {
		return true;
	}

}
//----------------------------------------------------------------
