function CartValidation() {
	var fields = document.form1.elements;
	for(var i=0; i < fields.length; i++) {
		var field = fields(i);
		if(field.name.substring(0, 3) == 'Qty') {
			if (!checkInteger('Quantity', field)) return false;
		}
	}
	return true;
}

function PartDetailValidation() {
	if (!checkPositiveInteger('Quantity', document.form1.Quantity)) return false;
	return true;
}


function LoginValidation() {
	if (!validateEmail(document.form1.email)) return false;
	if (!checkRequired('Password', document.form1.password))  return false;
	return true;
}

function ChangePasswordValidation() {
	if (!validateEmail(document.form1.email)) return false;
	if (!checkRequired('Password', document.form1.password))  return false;
	if (!validatePassword(document.form1.Password1, document.form1.Password2)) return false;
	return true;
}

function UpdateEmailValidation() {
	if (!validateEmail(document.form1.email)) return false;
	if (!checkRequired('Password', document.form1.password))  return false;
	if (!checkRequired('New Email Address', document.form1.NewEmail)) return false;
	return true;
}
function SignOnvalidation() {
	if (!validateEmail(document.form1.email)) return false;
	if (!validatePassword(document.form1.Password, document.form1.Password2)) return false;
	if (!checkRequired('First Name', document.form1.firstname))  return false;
	if (!checkRequired('Last Name', document.form1.lastname))  return false;
//	if (!checkRequired('Company Name', document.form1.company))  return false;
	if (!checkRequired('Address Line 1', document.form1.address1))  return false;
	if (!checkRequired('City', document.form1.city))  return false;
	if (document.form1.Country.value == 'US') {
//		if (!checkRequired('State', document.form1.State))  return false;
		if (!validateZIP(document.form1.zip)) return false;
	}
	if (document.form1.Country.value == 'CA') {
//		if (!checkRequired('Province', document.form1.State))  return false;
		if (!checkRequired('Postal Code', document.form1.zip)) return false;
	}
	if (!checkRequired('Phone', document.form1.phone))  return false;
	return true;
}

function ForgetPasswordValidation() {
	if (!validateEmail(document.form1.email)) return false;
	if (!checkRequired('First Name', document.form1.FirstName))  return false;
	if (!checkRequired('Last Name', document.form1.LastName))  return false;
	return true;
}

function ShippingValidation() {
	if (!checkSelected('Shipping Method', document.form1.ShippingMethod))  return false;
	if (!checkRequired('First Name', document.form1.firstname))  return false;
	if (!checkRequired('Last Name', document.form1.lastname))  return false;
	if (!checkRequired('Company Name', document.form1.company))  return false;
	if (!checkRequired('Address Line 1', document.form1.address1))  return false;
	if (!checkRequired('City', document.form1.city))  return false;
	if (document.form1.Country.value == 'US') {
//		if (!checkRequired('State', document.form1.State))  return false;
		if (!validateZIP(document.form1.zip)) return false;
	}
	if (document.form1.Country.value == 'CA') {
//		if (!checkRequired('Province', document.form1.State))  return false;
		if (!checkRequired('Postal Code', document.form1.zip)) return false;
	}
//	if (!checkSelected('State', document.form1.state))  return false;
//	if (!validateZIP(document.form1.zip)) return false;
	if (!checkRequired('Phone', document.form1.phone))  return false;
	return true;
}

function validateZIP(field) {
	if (!checkRequired('ZIP/Post Code', field))  return false;
	var valid = "0123456789-";
	var hyphencount = 0;
	var ZIP = field.value;
	var nLength = ZIP.length;
	if (nLength != 5 && nLength != 10) return warning('Please enter your 5 digit or 5 digit+4 zip code.',field);
	for (var i=0; i < nLength; i++) {
		var temp = "" + ZIP.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") return warning('Invalid characters in your zip code.  Please try again.',field);
		if (temp == "-") hyphencount++;
		if ((hyphencount > 1) || ((nLength==10) && "" + ZIP.charAt(5)!= "-")) return warning('The hyphen character should be used with a properly formatted 5 digit+four zip code, like \'12345-6789\'.   Please try again.',field);
	}
	return true;
}

function validatePassword(field1, field2) {
	var invalid = " "; // Invalid character is a space
	var minLength = 6; // Minimum length
	var pw1 = field1.value;
	var pw2 = field2.value;
	if (pw1 == '' || pw2 == '') return warning('Please enter your password twice.', field1);
	if (pw1 != pw2)  return warning('You did not enter the same new password twice. Please re-enter your password.', field1);
	if (pw1.length < minLength) return warning('Your password must be at least ' + minLength + ' characters long. Try again.', field1);
	if (pw1.indexOf(invalid) > -1) return warning('Sorry, spaces are not allowed.', field1);
	return true;
}


function validateEmail(field) {
	if (!checkRequired('Email', field))  return false;

	var emailStr = field.value.toLowerCase();
	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) return warning("Email address seems incorrect (check @ and .'s)",field);

	var user=matchArray[1];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) return warning("Ths username contains invalid characters.",field);
	}

	var domain=matchArray[2];
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) return warning("Ths domain name contains invalid characters.",field);
	}

	if (user.match(userPat)==null) return warning("The username doesn't seem to be valid.",field);

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) return warning("Destination IP address is invalid!",field);
		}
		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) return warning("The domain name does not seem to be valid.",field);
	}


	if (checkTLD && domArr[len-1].length!=2 && domArr[len-1].search(knownDomsPat)==-1) 
		 return warning("The address must end in a well-known domain or two letter country.",field);


	if (len<2) return warning("This address is missing a hostname!",field);

	return true;
}

function checkRequired(sName, field) {
	if (field.value.length == 0)
		return warning(sName + ' is a required field. Please enter your information.',field)
	else
		return true;
}

function checkSelected(sName, field) {
	if (field.selectedIndex <= 0)
		return warning(sName + ' is a required field. Please Select one.',field)
	else
		return true;
}

function checkPositiveInteger(sName, field) {
	if (!checkInteger(sName, field)) return false;
	if (field.value.valueOf() <= 0) return warning('Please enter a positive integer value in the ' + sName + ' field.' ,field);
	return true;
}

function checkInteger(sName, field) {
	if (!checkRequired(sName, field)) return false;
	var valid = "-0123456789";
	var hyphencount = 0;
	var sValue = field.value;
	var nLength = sValue.length;
	for (var i=0; i < nLength; i++) {
		var temp = "" + sValue.substring(i, i+1);
		if (valid.indexOf(temp) == -1) return warning('Please enter a integer value in the ' + sName + ' field.',field);
	}
	return true;
}

function warning(sMessage, field) {
	alert(sMessage);
	field.focus();
}
