function specialCase(e, form) {
	if ((e.name == "Ecom_Payment_Card_Name")||(e.name == "Ecom_Payment_Card_Number")||(e.name == "Ecom_Payment_Card_ExpDate_Month")||(e.name == "Ecom_Payment_Card_ExpDate_Year")) {
		if (((form.Ecom_Payment_Card_Name.value.length <= 0)||(form.Ecom_Payment_Card_Number.value.length <= 0)||(form.Ecom_Payment_Card_ExpDate_Month.value <= 0)||(form.Ecom_Payment_Card_ExpDate_Year.value <= 0))
		 && ((form.Ecom_Payment_Card_Name.value.length > 0)||(form.Ecom_Payment_Card_Number.value.length > 0)||(form.Ecom_Payment_Card_ExpDate_Month.value > 0)||(form.Ecom_Payment_Card_ExpDate_Year.value > 0))) {
			return "Please enter all Credit Card Information.";
		}
		if ((form.Ecom_Payment_Card_Name.value.length > 0)&&(form.Ecom_Payment_Card_Number.value > 0)&&(form.Ecom_Payment_Card_ExpDate_Month.value > 0)&&(form.Ecom_Payment_Card_ExpDate_Year.value > 0)) {
			if (!isCardDateValid(form.Ecom_Payment_Card_ExpDate_Year.value, form.Ecom_Payment_Card_ExpDate_Month.value)) {
				return "The Credit Card has Expired.";
			}
			if (isCardNumValid(form.Ecom_Payment_Card_Number.value)) {
				return "The Credit Card Number is invalid.";
			}
		}
	}
}


function stripChar(sValue, sChar) {
	var i, tempChar, buildString;
	buildString = ""
	for (var i=0; i<sValue.length; i++) {
		tempChar = sValue.charAt(i);
		if (tempChar != sChar) {
			buildString = buildString + tempChar;
		}
	}
	return buildString;
}

function isCardDateValid(year, month) {
	var dateCheck, now;
	now = new Date();
	dateCheck = new Date(year, month);
	if (now > dateCheck) {
		return false;
	}
	else {
		return true;
	}
}

function isCardNumValid(num) {
	var num1, num2, tempNum;
	if (!isNumber(num)) {
		return true;
	}
	num1 = ""
	if (!(num.length%2==0)) {
		for(var j=0; j < num.length; j++) {
			if ((j+1)%2==0){
				tempNum = 2 * num.charAt(j);
			}
			else {
				tempNum = 1 * num.charAt(j);
			}
			num1 = num1 + tempNum.toString();
		}
	}
	else{
		for(var j=0; j < num.length; j++){
			if ((j+1)%2==0){
				tempNum = 1 * num.charAt(j);
			}
			else{
				tempNum = 2 * num.charAt(j);
			}
			num1 = num1 + tempNum.toString();
		}
	}
	num2 = 0;
	for (var j = 0; j < num1.length; j++) {
		num2 = num2 + parseInt(num1.charAt(j));
	}
	if (num2%10==0) {
		return false;
	}
	else {
		return true;
	}
}

function isNumber(value) {
	for (var i=0; i < value.length; i++) {
		a = parseInt(value.charAt(i));
		if (isNaN(a)) {
			return false;			
			break;
		}
	}
	return true;
}


// E-mail Validation Function posted by Ed Garcia 20/6/02:

function isEmail(str)
{
   var at="@";
   var dot=".";
   var lat=str.indexOf(at);
   var lstr=str.length;
   var ldot=str.indexOf(dot);

   if (str.indexOf(at)==-1)
   {
      return false;
   }

   if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
   {
      return false;
   }

   if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
   {
      return false;
   }

   if (str.indexOf(at,(lat+1))!=-1)
   {
      return false;
   }

   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
   {
      return false;
   }

   if (str.indexOf(dot,(lat+2))==-1)
   {
      return false;
   }

   if (str.indexOf(" ")!=-1)
   {
      return false;
   }

   return true;
}


function sfCheck(form) {
	var e, title, empty_fields, char_check, invalid_card, invalid_cc_type, month, year, invalid_date, cvv_check, eMail, invalid_eMail, tmpStr, tmpVal
	var checkSpecial, tempError, special_Error, msg, upperLine, lowerLine
	var num, invalid_phoneNumber, passwd_mismatch, mycardexpiry_select, mycardtype_select, mycountry_select, myspecialinstr_check, mygiftmsg_check, tmpNum1
	msg = "";
	empty_fields = "";
	char_check = "";
	special_Error = "";
	tempError = "";
	for (var i = 0; i < form.elements.length; i++) {
		e = form.elements[i]
		if ((e.title == null)||(e.title == "")) {
			title = e.name;
		}
		else {
			title = e.title;
		}
		if (((e.type == "text") || (e.type == "textarea")||(e.type == "password")) && !e.special && !e.disabled) {
			if (e.value.length <= 0 && !e.optional) {
				empty_fields += "\n            " + title;
				continue;
			}
			if (e.number) {
				num = e.value;
				num = stripChar(num, ".");
				num = stripChar(num, ",");
				if (!isNumber(num)) {
					char_check += "\n             " + title;
				}
			}
			if (e.creditCardNumber) {
				e.value = stripChar(e.value, " "); 
				e.value = stripChar(e.value, "-"); 
				invalid_card = isCardNumValid(e.value);
				tmpNum1 = e.value
				if (tmpNum1.length != 16 && tmpNum1.length != 13) {
					invalid_card = true; }
				else {
				if ((tmpNum1.indexOf("4") != 0) && (tmpNum1.indexOf("5") != 0) && (tmpNum1.length > 0)) {
				invalid_cc_type = true; }
				}
			}
			

			if (e.eMail) {
				eMail = e.value;
				if (isEmail(eMail)) {
					invalid_eMail = false;
				}
				else {
					invalid_eMail = true;
				}
			}

			if (e.name == "txtEmail") {
				eMail = e.value;
				if (isEmail(eMail)) {
					invalid_eMail = false;
				}
				else {
					invalid_eMail = true;
				}
			}

			if (e.name == "txtFriend") {
				eMail = e.value;
				if (isEmail(eMail)) {
					invalid_eMail = false;
				}
				else {
					invalid_eMail = true;
				}
			}

			if (e.name == "txtfbEmail") {
				eMail = e.value;
				if (isEmail(eMail)) {
					invalid_eMail = false;
				}
				else {
					invalid_eMail = true;
				}
			}


			if (e.phoneNumber) {
				num = e.value;
				num = stripChar(num, " ");
				num = stripChar(num, "-");
				num = stripChar(num, "+");
				if (num.length < 8) {
					invalid_phoneNumber = true;
				}	
			}
		}
		if (e.password) {
			if (form.Password.value != form.Password2.value) {
					passwd_mismatch = true;
			}
		}

		if (e.name == "Ecom_Payment_Card_Type") {
			tmpindx = e.selectedIndex
			if (tmpindx == -1) {
				tmpindx = 0;
			}
			if (e.options[tmpindx].value < 1) {
				mycardtype_select = true;
			}
		}


		if (e.name == "Instructions") {
			tmpStr = e.value
			if (tmpStr.length > 240) {
				myspecialinstr_check = true;
			}
		}


		if (e.name == "GiftMsg") {
			tmpStr = e.value
			if (tmpStr.length > 240) {
				mygiftmsg_check = true;
			}
		}

		if ((e.name == "Billing_CountryName") && (e.type != "hidden")) {
			tmpindx = e.selectedIndex;
			if (tmpindx == -1) {
				tmpindx = 0;
			}
			if (e.options[tmpindx].value < 1) {
				mycountry_select = true;
			}
			else {
			if (window.document.form1.Billing_CountryName.value == "US" && (window.document.form1.Billing_StateProv.value == "" || window.document.form1.Billing_PostalCode.value == ""))
			  {
			  window.alert("For the US you must enter both a State and Zip Code.");
			  return false;
			  }
			  else
			  {
			if ((window.document.form1.Shipping_CountryName.value == "US" || window.document.form1.Shipping_CountryName.value == "AU" || window.document.form1.Shipping_CountryName.value == "CA") && (window.document.form1.Shipping_StateProv.value == "" || window.document.form1.Shipping_PostalCode.value == ""))
			  {
			  window.alert("For the selected country you must enter both a ShipTo State/Province and Zip/Postal Code.");
			  return false;
			  }
			  else
			  {
			if ((window.document.form1.Shipping_CountryName.value == "GB" || window.document.form1.Shipping_CountryName.value == "EN" || window.document.form1.Shipping_CountryName.value == "WL" || window.document.form1.Shipping_CountryName.value == "SF" || window.document.form1.Shipping_CountryName.value == "NB") && (window.document.form1.Shipping_PostalCode.value == ""))
			  {
			  window.alert("For the selected country you must enter a ShipTo Postcode.");
			  return false;
			  }
			 }
			  }
			}
		}

		if ((e.name == "Shipping_CountryName") && (e.type != "hidden") && (!empty_fields)) {
			tmpindx = e.selectedIndex;
			if (tmpindx == -1) {
				tmpindx = 0;
			}
			if (e.options[tmpindx].value < 1) {
				mycountry_select = true;
				tmpVal = window.document.form1.Billing_CountryName.value;
				tmpStr = "EN:BE:CD:CH:CY:DE:DK:ES:FI:FR:GB:GI:GR:IE:IR:IS:IT:LU:MC:MT:NB:NL:NO:PL:PT:SE:SF:VA:WL";
				if (tmpStr.indexOf(tmpVal) >= 0) {
				  window.alert("The Ship To Country is blank.\n\nIf your country is not in the Ship To Country drop-down list, it may be due\nto the currency selected. For shipping to European destinations, you must \nhave UK pounds or Euro currency selected.\n\nIf you need to change the currency, use the \'Select Currency\' link next to Ship Country.\n");
				  return false;
				}
			}
		}

		if (e.name == "Ecom_Payment_Card_ExpDate_Month") {
			tmpindx = e.selectedIndex
			if (tmpindx == -1) {
				tmpindx = 0;
			}
			month = e.options[tmpindx].value
			if (month < 1) {
				mycardexpiry_select = true;
				month = "";
			}
		}
		if (e.name == "Ecom_Payment_Card_ExpDate_Year") {
			tmpindx = e.selectedIndex
			if (tmpindx == -1) {
				tmpindx = 0;
			}
			year = e.options[tmpindx].value
			if (year < 1) {
				mycardexpiry_select = true;
				year = "";
			}
			if ((month != "") && (year != "")) {
				if(!isCardDateValid(year, month)) {
					invalid_date = true;
					}	
				}
		}
		if (e.name == "Ecom_Payment_Card_Verification") {
			tmpStr = e.value
			if (tmpStr.length != 3) {
				cvv_check = true;
			}
			else
			{
				if (!isNumber(tmpStr)) {
				cvv_check = true; }
			}
		}
		if (e.special) {
			checkSpecial = specialCase(e, form);
			if (tempError != checkSpecial) {
				special_Error = special_Error + checkSpecial
			}
			tempError = checkSpecial;
		}
		if (e.type == "select-one" && !e.optional) {
			if (e.value == "") {
				empty_fields += "\n            " + title;
				continue;
			}
		}
	}

	if (!empty_fields && !char_check && !special_Error && !invalid_card && !invalid_cc_type && !invalid_date && !cvv_check && !invalid_eMail && !invalid_phoneNumber && !passwd_mismatch && !mycardexpiry_select && !mycardtype_select && !mycountry_select && !myspecialinstr_check && !mygiftmsg_check) {return true}
	msg = "Please correct the following and re-submit the form:\n";
	
	upperLine = "\n_________________________________________________________\n\n";
	lowerLine = "_________________________________________________________\n";
	
	if (empty_fields) {
		msg += upperLine;
		msg += "The following field(s) must be filled in:\n";
		msg += empty_fields;
		msg += "\n" + lowerLine;
	}
	if (char_check) {
		msg += upperLine;
		msg += "The following field(s) need a numeric value:\n";
		msg += lowerLine;
		msg += char_check;
	}
	if (invalid_card) {
		msg += upperLine;
		msg += "The Credit Card Number is invalid.\n";
		msg += lowerLine;
	}
	if (invalid_cc_type) {
		msg += upperLine;
		msg += "The Credit Card Number must be a Visa or Mastercard.\n";
		msg += lowerLine;
	}
	if (invalid_date) {
		msg += upperLine;
		msg += "The Credit Card has Expired.\n";
		msg += lowerLine;
	}
	if (cvv_check) {
		msg += upperLine;
		msg += "The Card Verification Number needs to be a 3 digit number.\n";
		msg += lowerLine;
	}
	if (invalid_eMail) {
		msg += upperLine;
		msg += "The Email Address is in an invalid format.\n";
		msg += lowerLine;
	}
	if (invalid_phoneNumber) {
		msg += upperLine;
		msg += "Please enter a valid Phone Number with area code.\n";
		msg += lowerLine;
	}
	if (special_Error) {
		msg += upperLine;
		msg += special_Error + "\n";
		msg += lowerLine;
	}
	if (passwd_mismatch) {
		msg += upperLine;
		msg += "Your passwords did not match. Please enter them again.\n";
		msg += lowerLine;
	}		
	if (mycardexpiry_select) {
		msg += upperLine;
		msg += "Please select a credit card expiry month and year.\n"
		msg += lowerLine;
	}
	if (mycardtype_select) {
		msg += upperLine;
		msg += "Please select a credit card type.\n"
		msg += lowerLine;
	}
	if (mycountry_select) {
		msg += upperLine;
		msg += "Please make a selection from the country list.\n"
		msg += lowerLine;
	}
	if (myspecialinstr_check) {
		msg += upperLine;
		msg += "Please enter no more than 225 characters in the Special Instructions field.\n"
		msg += lowerLine;
	}
	if (mygiftmsg_check) {
		msg += upperLine;
		msg += "Please enter no more than 225 characters in the Gift Message field.\n"
		msg += lowerLine;
	}
	alert(msg);
	return false;
}	
