/*
	The isEmpty and isWhitespace functions are from Netscape's 
	JavaScript dev site, http://developer.netscape.com. Some functions 
	from http://www.4guysfromrolla.com/webtech/091998-1.shtml, which 
	said to take them for free. Otherwise, everything else copyright
	2004 by Jay F. Davis, Amazing Concepts Softwware, 
	http://www.acsoft.biz. Permission to use and modify is granted
	once the associated invoice is paid.
*/

// whitespace characters
var whitespace = " \t\n\r";

/****************************************************************/
// Check whether string s is empty.
function isEmpty(s)
{ return ((s == null) || (s.length == 0)) }

/****************************************************************/
function isWhitespace (s)
{
	var i;

	// Is s empty?
	if (isEmpty(s)) return true;

	// Search through string's characters one by one
	// until we find a non-whitespace character.
	// When we do, return false; if we don't, return true.
	for (i = 0; i < s.length; i++)
	{
			// Check that current character isn't whitespace.
			var c = s.charAt(i);

			if (whitespace.indexOf(c) == -1) return false;
	}

	// All characters are whitespace.
	return true;
}

/****************************************************************/
function isBlank(val, str) {
	var strInput = new String(val.value);

	if (isWhitespace(strInput) || isEmpty(strInput)) {
		alert(str);
		return true;
	} else {
		return false;
	}

}

/****************************************************************/
function isBlank2(val) {
	var strInput = new String(val.value);

	if (isWhitespace(strInput) || isEmpty(strInput)) {
		return true;
	} else {
		return false;
	}

}

/****************************************************************/
function passwordIsMismatched(val1, val2, str) {

	if (val1.value == val2.value) {
		return false;
	}
	else {
		alert(str);
		//alert("str1: " + val1.value + " str2: " + val2.value);
		return true;
	}

}

/****************************************************************/
function bothFilled(val1, val2, str) {
	var strInput1 = new String(val1.value);
	var strInput2 = new String(val2.value);
	
	if (isWhitespace(strInput1) || isWhitespace(strInput2)) {
		return false;
	}
	else {
		alert(str);
		return true;
	}

}

/****************************************************************/
function bothEmpty(val1, val2, str) {
	var strInput1 = new String(val1.value);
	var strInput2 = new String(val2.value);

	if (!isWhitespace(strInput1) || !isWhitespace(strInput2)) {
		return false;
	}
	else {
		alert(str);
		return true;
	}

}

/****************************************************************/
function notEmail(val1, str) {
	var strInput = new String(val1.value);
	
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	
	var len = strInput.length;
	var rt = strInput.substring(len-4).toLowerCase();
	
	if (!supported) {
		if ((rt.substring(0,1) == "." || rt.substring(1,1) == ".") && strInput.indexOf("@") != -1) {
			return false;
		}
		else {
			alert(str);
			return true;
		}
	}
	else {
		// use regexp
		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
		if (!r1.test(strInput) && r2.test(strInput)) {
			return false;
		}
		else {
			alert(str);
			return true;
		}
	}
	
}

/****************************************************************/
function notAllDigits(val1, str) {
	var strInput = new String(val1.value);
	
	if (inValidCharSet(strInput,"0123456789")) {
		return false;
	}
	else {
		alert(str);
		return true;
	}
}

/****************************************************************/
function inValidCharSet(str,charset)
{
	var result = true;
	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

/****************************************************************/
function failLuhnCheck(val1, msg) {
	var str = new String(val1.value);
	var result = false;

  var sum = 0; 
  var mul = 1; 
  var strLen = str.length;
  
  for (i = 0; i < strLen; i++) 
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0) {
  	alert(msg);
    result = true;
  }
    
  return result;
}

/****************************************************************/
function GetRadioValue(rArray)
{
	for (var i=0;i<rArray.length;i++)
	{
		if (rArray[i].checked)
			return rArray[i].value;
	}
	
	return null;
}

/****************************************************************/
function validCCNum(ctlCardType, ctlCardNumber) {
	var cardType = new String(ctlCardType.value);
	var cardNum = new String(ctlCardNumber.value);

	var result = false;
	cardType = cardType.toUpperCase();
	
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);

	switch (cardType)
	{
		case "V":
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "A":
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "M":
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "D":
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "D":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
	}
	return result;
}

/****************************************************************/
function ValidateLogin(frm) {

	// Data validations for checkout login:
	if (isBlank(frm.login,"Email Address or Login ID cannot be blank.")) { 
		frm.login.focus();
		return false; 
	}
	if (frm.action[0].checked) {
		// make sure e-mail address is being used
		if (notEmail(frm.login,"Must enter a valid e-mail address.")) { 
			frm.login.focus();
			return false; 
		}
		else {
			return true;
		}
	}
	/* if (frm.action[1].checked) {
		if (isBlank(frm.password,"Password cannot be blank.")) { 
			return false; 
		}
		else {
			return true;
		}
	} */
}

/****************************************************************/
function ValidateAddress(frm) {

	// Data validations for checkout address page:
	if (isBlank(frm.email,"Email Address cannot be blank.")) { 
		frm.email.focus();
		return false; 
	}
	if (notEmail(frm.email,"Must enter a valid e-mail address.")) { 
		frm.email.focus();
		return false; 
	}
	if (isBlank(frm.bname,"Name cannot be blank.")) { 
		frm.bname.focus();
		return false; 
	}
	if (isBlank(frm.baddr1,"Adress Line 1 cannot be blank.")) { 
		frm.baddr1.focus();
		return false; 
	}
	if (isBlank(frm.bcity,"City cannot be blank.")) { 
		frm.bcity.focus();
		return false; 
	}
	if (isBlank(frm.bcountry,"You must select a country.")) { 
		return false; 
	}
	if (frm.bcountry.selectedIndex == 222) {
		// United States:
		if (frm.bstate.selectedIndex == 0) { 
			alert("You must select a state.");
			return false; 
		}
	}
	else {
		if (isBlank(frm.bstate2,"Province/Region cannot be blank.")) { 
			frm.bstate2.focus();
			return false; 
		}
	}
	if (isBlank(frm.bzip,"Zip/Postal Code cannot be blank.")) { 
		frm.bzip.focus();
		return false; 
	}
	if (isBlank(frm.phone,"Phone Number cannot be blank.")) { 
		frm.phone.focus();
		return false; 
	}
	return true;
}

/****************************************************************/
function ValidateCC(frm) {

	// Data validations for credit card info:
	if (frm.cctype.selectedIndex == 0) {
		alert("You must select a payment method.");
		return false; 
	}
	if (isBlank(frm.cardnumber,"Credit card number cannot be blank.")) { 
		frm.cardnumber.focus();
		return false; 
	}
 	if (notAllDigits(frm.cardnumber, 'Please enter only numbers (no dashes or spaces) for the credit card number.')) {
		frm.cardnumber.focus();
		return false;
	}
	if (!validCCNum(frm.cctype, frm.cardnumber)) {
		alert("The credit card number is invalid.");
		return false;
	}
	if (failLuhnCheck(frm.cardnumber, "The credit card number is not correct. Please check it for errors.")) {
		frm.cardnumber.focus();
		return false;
	}
	if (frm.expmonth.selectedIndex == 0) {
		alert("You must select an expiration month.");
		return false; 
	}
	if (frm.expyear.selectedIndex == 0) {
		alert("You must select an expiration year.");
		return false; 
	}
	if (isBlank2(frm.cvm)) { 
		if (!frm.cvmnotpres.checked) {
			alert("Card code cannot be blank. Check 'Code not present' if your card does not have a code.");
			frm.cvm.focus();
			return false;
		}
	}
	return true;
}

/****************************************************************/
function ValidateGarage(frm) {
	
	// Data validations for ask the expert form:
	if (isBlank(frm.acctnumber,"Account number cannot be blank.")) { 
		frm.acctnumber.focus();
		return false; 
	}
	return true;
}

/****************************************************************/
function ValidateAsk(frm) {

	// Data validations for ask the expert form:
	if (isBlank(frm.fname,"First Name cannot be blank.")) { 
		frm.fname.focus();
		return false; 
	}
	if (isBlank(frm.lname,"Last Name cannot be blank.")) { 
		frm.lname.focus();
		return false; 
	}
	if (isBlank(frm.email,"Email Address cannot be blank.")) { 
		frm.email.focus();
		return false; 
	}
	if (notEmail(frm.email,"Must enter a valid e-mail address.")) { 
		frm.email.focus();
		return false; 
	}
	if (isBlank(frm.year,"Year of vehicle cannot be blank.")) { 
		frm.year.focus();
		return false; 
	}
	if (isBlank(frm.make,"Make of vehicle cannot be blank.")) { 
		frm.make.focus();
		return false; 
	}
	if (isBlank(frm.model,"Model of vechicle cannot be blank.")) { 
		frm.model.focus();
		return false; 
	}
	if (isBlank(frm.engine,"Engine of vehicle cannot be blank.")) { 
		frm.engine.focus();
		return false; 
	}
	if (!frm.disc[0].checked && !frm.disc[1].checked && !frm.disc[2].checked) {
		alert("Must select disc brake type.");
		return false;
	}
	if (!frm.abs[0].checked && !frm.abs[1].checked) {
		alert("Must select ABS type.");
		return false;
	}
	if (!frm.wheeldrive[0].checked && !frm.wheeldrive[1].checked) {
		alert("Must select 2 Wheel or 4 Wheel Drive.");
		return false;
	}
	if (!frm.contactme[0].checked && !frm.contactme[1].checked) {
		alert("Must select Contact Me Via.");
		return false;
	}
	if (isBlank(frm.product,"Product Needed cannot be blank.")) { 
		frm.product.focus();
		return false; 
	}
	if (frm.contactme[0].checked) {
		// wants to be contacted by phone, make sure phone number is supplied:
		if (isBlank(frm.phone, "Phone cannot be blank since you want to be contacted by phone.")) {
			frm.phone.focus();
			return false;
		}
	}
	if (isBlank(frm.problem,"Description of Problem or Request cannot be blank.")) { 
		frm.problem.focus();
		return false; 
	}
	return true;
}
