function do_validate(){
	var order_form = window.order_form;
	//validate contact info
	if(order_form.Contact_FullName.value == ""){
		alert("Please input Valid Contact Name!");
		return false;
	}
	if(order_form.Contact_Organization.value == ""){
		alert("Please input Valid Organization!");
		return false;
	}
	if(order_form.Contact_WorkPhone.value == ""){
		alert("Please input Valid Work Phone!");
		return false;
	}
	if(!emailValidate(order_form.Contact_Email.value)){
		alert("Please input Valid E-mail address!");
		return false;
	}
	//validate order info
	if(order_form.Ordering_OrderQty0.value == ""){
		alert("Please input at least one ordering information (QTY/DESCRIPTION)!");
		return false;
	}
	if(order_form.Ordering_OrderDesc0.value == ""){
		alert("Please input at least one ordering information (QTY/DESCRIPTION)!");
		return false;
	}
	//validate credit card info
	if(order_form.Ordering_CardHolderName.value == ""){
		alert("Please input Valid Cardholder Name!");
		return false;
	}
	
	if(!isValidCreditCardNumber(order_form.Ordering_CardNumber, order_form.Ordering_CardType.value, " Card Number ", true)){
		return false;
	}
	
	if(!isValidExpDate(order_form.Ordering_CardExpiration,"Expiration Date", true)){
		return false;
	}
	//validate shipping info
	if(order_form.Ordering_StreetAddress.value == ""){
		alert("Please input Valid Street Address!");
		return false;
	}
	if(order_form.Ordering_City.value == ""){
		alert("Please input Valid City!");
		return false;
	}
	if(order_form.Ordering_State.value == ""){
		alert("Please input Valid State/Province!");
		return false;
	}
	if(order_form.Ordering_ZipCode.value == ""){
		alert("Please input Valid Zip/Postal Code!");
		return false;
	}
	if(order_form.Ordering_Country.value == ""){
		alert("Please input Valid Country!");
		return false;
	}
	//validate others
	if(order_form.insurance[1].checked){
		if(order_form.Declared_value.value == ""){
			alert("Please input Valid  Declared Value!");
			return false;
		}
	}
	return true;
}


//function to validate email address
function emailValidate(emailAddress) {
    if (emailAddress==null || emailAddress=="")
		return false;
    var rx =/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    if(rx.exec(emailAddress)==null)
		return false;
    else
	    return true;
}

//functions for validating credit card information



//This script verifies whether a credit card number follows some well known rules for various card types.  
//For example, it checks the number of digits in the card and compares the starting digits for that card against 
//the known digits for that card type.  It also uses an algorithm called the Luhn algorithm that does 
//a kind of checksum on the card number.  Note that the functions do not verify whether the credit card number 
//is an actual billable account number with funds -- only whether the number complies with standard credit card rules.  
//Finally, the expiration date is verified against the current date

// Form Field Validation Functions:
//
// isValidExpDate(formField,fieldLabel,required)
//   -- checks for date in the format MM/YY or MM/YYYY against the current date
// isValidCreditCardNumber(formField,ccType,fieldLabel,required)
//   -- checks for valid credit card format using the Luhn check and known digits about various cards
//

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

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 isValidExpDate(formFieldExp,fieldLabel,required)
{
	var result = true;
	var formValue = formFieldExp.value;

	if (required && !(validRequired(formFieldExp,fieldLabel)))
		result = false;
  
 	if (result && (formValue.length>1))
 	{
 		var elems = formValue.split("/");
 		
 		result = (elems.length == 2); // should be two components
 		var expired = false;
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
 			var year = parseInt(elems[1],10);
 			
 			if (elems[1].length == 2)
 				year += 2000;
 			
 			var now = new Date();
 			
 			var nowMonth = now.getMonth() + 1;
 			var nowYear = now.getFullYear();
 			
 			expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
 			
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && ((elems[1].length == 2) || (elems[1].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/YY for the "' + fieldLabel +'" field.');
			//formFieldMonth.focus();
		}
		else if (expired)
		{
 			result = false;
 			alert('The date for "' + fieldLabel +'" has expired.');
			//formFieldMonth.focus();
		}
	} 
	
	return result;
}

function isValidCreditCardNumber(formField,ccType,fieldLabel,required)
{
	var result = true;
 	var ccNum = formField.value;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
 
  	if (result && (formField.value.length>0))
 	{ 
 		if (!allDigits(ccNum))
 		{
 			alert('Please enter only numbers (no dashes or spaces) for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}	

		if (result)
 		{ 
 			if (!LuhnCheck(ccNum) || !validateCCNum(ccType,ccNum))
 			{
 				alert('Please enter a valid card number for the "' + fieldLabel +'" field.');
				formField.focus();
				result = false;
			}	
		} 

	} 
	
	return result;
}

function LuhnCheck(str) 
{
  var result = true;

  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)
    result = false;

  return result;
}

function validateCCNum(cardType,cardNum)
{
	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 "VISA":	//VS
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AE":	//AE
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "MC":	//MC
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "DISCOVERY":	//DISCOVER
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "DINERS":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
	}
	return result;
}

