// Trim string function
function trimString (str) {
		  str = this != window? this : str;
		  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
	}

// reformat the numeric phone value into the US phone format
function formatPhone(paramValue)
{
	return   paramValue.replace(/^(\d\d\d)(\d\d\d)(\d\d\d\d)$/, "($1)$2-$3");

}

// reformat the numeric FAX value into the US FAX format
function formatFax(paramValue)
{
	return   paramValue.replace(/^(\d\d\d)(\d\d\d)(\d\d\d\d)$/, "($1)$2-$3");

}

//Trim off the characters ( ) - from the Credit Card Number Field before Submission
function isValidCCInfo(ccType, ccNo)
{
	return true;

}

//Trim off the characters ( ) - from the Phone Number Field before Submission
function replaceNonNumeric(phoneNo)
{
	phoneNo = phoneNo.replace(/-/g,"");
	phoneNo = phoneNo.replace(/\(/g,"");
	phoneNo = phoneNo.replace(/\)/g,"");
	phoneNo = phoneNo.replace(/ /g,"");
	return phoneNo;

}

//Checks whether the form input element is a valid Phone Number (USA)
//paramValue-Value of the Form Item
function isValidPhone(paramValue)
{
	paramValue = trimString(paramValue);
	var expIsAllowedChars=/[^0-9\)\(\.\- ]/;
	if(expIsAllowedChars.test(paramValue))
		return false;

	var expIsPhoneFormat1 = /^\(\d{3}\)\d{3}\-\d{4}$/;
	var expIsPhoneFormat2 = /^\d{3}\-\d{3}\-\d{4}$/;
	var expIsPhoneFormat3 = /^\(\d{3}\) \d{3}\-\d{4}$/;
	var expIsPhoneFormat4 = /\d{10}/;
	var expIsPhoneFormat5 = /^\d{3}\.\d{3}\.\d{4}$/;
	var expIsPhoneFormat6 = /^\d{3}\d{3}\-\d{4}$/;
	var expIsPhoneFormat7 = /^\d{3} \d{3}\-\d{4}$/;

	if( (expIsPhoneFormat1.test(paramValue) || 
		expIsPhoneFormat2.test(paramValue) || 
		expIsPhoneFormat3.test(paramValue) ||
		expIsPhoneFormat5.test(paramValue) ||
		expIsPhoneFormat6.test(paramValue) ||
		expIsPhoneFormat7.test(paramValue) ||
		(paramValue.length <=10 && expIsPhoneFormat4.test(paramValue))))
		return true;
		
	return false;
	
}

//Checks whether the form input element is a valid FAX Number (USA)
//paramValue-Value of the Form Item
function isValidFax(paramValue)
{
	paramValue = trimString(paramValue);
	var expIsAllowedChars=/[^0-9\)\(\.\- ]/;
	if(expIsAllowedChars.test(paramValue))
		return false;

	var expIsPhoneFormat1 = /^\(\d{3}\)\d{3}\-\d{4}$/;
	var expIsPhoneFormat2 = /^\d{3}\-\d{3}\-\d{4}$/;
	var expIsPhoneFormat3 = /^\(\d{3}\) \d{3}\-\d{4}$/;
	var expIsPhoneFormat4 = /\d{10}/;
	var expIsPhoneFormat5 = /^\d{3}\.\d{3}\.\d{4}$/;
	var expIsPhoneFormat6 = /^\d{3}\d{3}\-\d{4}$/;
	var expIsPhoneFormat7 = /^\d{3} \d{3}\-\d{4}$/;

	if( (expIsPhoneFormat1.test(paramValue) || 
		expIsPhoneFormat2.test(paramValue) || 
		expIsPhoneFormat3.test(paramValue) ||
		expIsPhoneFormat5.test(paramValue) ||
		expIsPhoneFormat6.test(paramValue) ||
		expIsPhoneFormat7.test(paramValue) ||
		(paramValue.length <=10 && expIsPhoneFormat4.test(paramValue))))
		return true;
		
	return false;
 	
}


//Checks whether the form input element is empty
//paramValue-Value of the Form Item
function isEmpty(paramValue)
{
	var expIsEmpty=/[^ ]/;
	return  ! expIsEmpty.test(paramValue);
}

//Function checks for the Valid GroupName
function isValidEntity(paramValue)
{
	var expIsValidEntity=/[^a-z0-9A-Z \'\-\(\)\.\_\$\&\/]/;
  	return ! expIsValidEntity.test(paramValue);
}


//Checks whether the form input element is numeric
//paramValue-Value of the Form Item
function isNumeric(paramValue)
{
  	var expIsNumeric=/[^0-9]/;
  	return ! expIsNumeric.test(paramValue);
}


//Checks whether the form input element is float
//paramValue-Value of the Form Item
function isFloat(paramValue)
{
                   if (isEmpty(paramValue)) return false ;
	var arrValue = paramValue.split(".");

	if (arrValue.length  > 2)
	{
		return false;
	}
	else
	{
  		if(arrValue.length == 1)
		{
			return isNumeric(arrValue[0]);
		}
		else
		{	
		var isIntergral = isNumeric(arrValue[0]);
		var isFloating = isNumeric(arrValue[1]);
		if (isIntergral && isFloating)
			return true;
		else
			return false;
		}			
	}
}

//Checks whether the form input element is a String
//paramValue-Value of the Form Item
function isChar(paramValue)
{
  	var expIsChar=/[^a-zA-Z ]/;
  	return ! expIsChar.test(paramValue);
}

//Checks whether the form input element is a valid AlphaNumeric
function isValidAlphaNumeric(paramValue)
{
  	var expisValidAlphaNumeric = /[^a-zA-Z1-9]/;
  	return ! expisValidAlphaNumeric.test(paramValue);
}

//Checks whether the form input element is a Valid Domain
//domainIn-Value of the Email Domain
function  isStandardDomain(domainIn)  
{
	/// The value to return, start out assuming invalid domain.
	var  isStandardReturn = false;

	/// Holds the last 4 characters of domain name.
	var  last4chars  =  domainIn.substring( domainIn.length-4, domainIn.length );

	/// Holds the last 3 characters of domain name.
	var  last3chars  =  domainIn.substring( domainIn.length-3, domainIn.length );

	/// Uppercase it for comparison purposes.
	last4chars = last4chars.toUpperCase();

	/// A regular expression pattern to match country code domains.
	///  In otherwords a Dot character followed by two alphabetic characters.
	/// NOTE:  This line doesn't work at all in Opera3.5 and prevents the
	///  entire script from running!!!  BUMMER!!!
	///  It also seems to not work in Opera4.02 but only prevents
	///  the 2 letter codes from working but doesn't crash the script.
	var  countryCodePattern = /\.[a-zA-Z][a-zA-Z]/;


	if      ( last4chars == ".COM" ) isStandardReturn = true;
	else if ( last4chars == ".EDU" ) isStandardReturn = true;
	else if ( last4chars == ".GOV" ) isStandardReturn = true;
	else if ( last4chars == ".NET" ) isStandardReturn = true;
	else if ( last4chars == ".MIL" ) isStandardReturn = true;
	else if ( last4chars == ".ORG" ) isStandardReturn = true;

	else if ( last3chars.search( countryCodePattern )   !=  -1 )
		isStandardReturn = true;
	return  isStandardReturn;
} 

//Checks whether the form input element is a Valid Email
//emailID-Value of the Form Item
function  isValidEmail(emailID)  
{
	//var expIsSpecialChar=/[\&\;\"\*\~\|\$\%\^\#\!\`\<\>\\\+\=\?]/
	var expIsSpecialChar=/[^a-zA-Z0-9_@.]/   			
	var expFirstCharOfEmail = /[^0-9a-zA-Z]/

	if(expIsSpecialChar.test(emailID))
	{
		return false;
	}

	/// Number of '@' chars present in input string.
	var  numAtChars;

	/// The part of input address before the '@' character.
	var  userNameIn;

	/// The part of input address after the '@' character.
	var  domainNameIn;

	/// Holds the fields of the entered address, delimitted by '@' chars.
	var  addressFields = new Array();

	/// Divide the input email address into fields.
	///  Note that the array "addressFields" will have one more element
	///  than the number of '@' signs in the input string.
	/// IE4 handles this OK with '@' as last character but NS4,4.5 and Opera 3.5 don't.
	
	addressFields = emailID.split('@');
	numAtChars = addressFields.length - 1;

	// Has to be added in the email validation 
	// This checks the dot at the last of the user name 
	// Also  this checks for the first entered character 
	// of email is a charactre or not
	//============================
	var dotPosition;
	dotPosition = addressFields[0].length-1;			
	if(addressFields[0].lastIndexOf(".") == dotPosition) 
		return false;	
	if(expFirstCharOfEmail.test(addressFields[0].substring(0,1)))	
		return false;	
	//===============================
	

	if (emailID == "") 	 		//  is EMPTY
		return false;

	else if (numAtChars  ==  0) 		//  contains no '@' character 
		return false;

	else if (numAtChars  >  1)		//  more than one '@' characters 
		return false;

	else if (addressFields[0] == "")	// has no Username before the '@' character 
		return false;

	else if (addressFields[1] == "")	// has no Domain Name after the '@' character
		return false;

	else
	{
		userNameIn   = addressFields[0];
		domainNameIn = addressFields[1];

		if (userNameIn.indexOf( " " ) != -1)		// has one or more Spaces in the Username before the '@'  					
			return false;
		else if (domainNameIn.indexOf( " " ) != -1) 	// has one or more Spaces in the Domain Name after the '@' character
			return false;
		else if (isStandardDomain( domainNameIn ) == false) 	//does not end with a '.com' style domain or two letter country code domain
			return false;

	}
	return true;           
}

//Checks whether the form input element is a Valid Date (mm/dd/yy)
//dateStr-Value of the Form Item
function isValidDate(dateStr) 
{
	var expIsChar=/[^0-9\-]/;
	if(expIsChar.test(dateStr))
	  	return ! expIsChar.test(dateStr);

	var datePat = /^(\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/; // requires 4 digit year
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) 
	{
		//alert(dateStr + " Date is not in a valid format.")
		return false;
	}
	month = matchArray[3]; // parse date into variables
	day = matchArray[5];
	year = matchArray[1];

	if (year <= 1753)
	{
		//alert("year must be greater than 1753.");
		return false;
	}	

	if (month < 1 || month > 12)
	{
		//alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31) 
	{
		//alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		//alert("Month "+month+" doesn't have 31 days!")
		return false;
	}
	if (month == 2) 
	{	 // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) 
		{
			//alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
	return true;
}
//Checks the Date and returns the boolean value
//Checks whether the Given Date is not greater than the Current Date
//paramValue-Value of the Form Item
function checkDate(paramValue)
{
	var curDate=new Date();
	var dtToCompare=new Date(paramValue);
	if(dtToCompare>curDate)
	{
		return false;
	}
	return true;
}

//Decodes the Special Characters in the Query String Passed to CallInfo.asp which is then
//decoded in that file.
function encode(str)
{
	var dest="";
	var len=str.length;
   	var index=0;
   	var code=null;

   	for(var i=0;i<len;i++)
   	{
      		var ch=str.charAt(i);
      		if(ch==" ") code="%20";	
      		else if(ch=="%") code="%25";
      		else if(ch==",") code="%2C";
      		else if(ch==";") code="%3B";
      		else if(ch=="\b") code="%08";
      		else if(ch=="\t") code="%09";
      		else if(ch=="\n") code="%0A";
      		else if(ch=="\f") code="%0C";
      		else if(ch=="\r") code="%0D";
    		if(code!=null)
		{
       			dest+=str.substring(index,i) + code;
		       	index=i+1;
		       	code=null;
	       	}
   	}
	 if(index<len) dest+=str.substring(index,len);
		 return dest;
}


/* validate billing form*/
function validate_billing(sfrm)
{
	var frm = document.forms[sfrm];
	var oControl = null;
	var errtext = "";
	
	if(isEmpty(frm.txtfirstname.value) || !isValidEntity(frm.txtfirstname.value))
	{
		errtext = "Please enter a valid First Name.";
		if(oControl == null)
			oControl = frm.txtfirstname;
	}
	
	if(isEmpty(frm.txtlastname.value) || !isValidEntity(frm.txtlastname.value))
	{
		errtext += "\r\nPlease enter a valid Last Name.";
		if(oControl == null)
			oControl = frm.txtlastname;
	}
	
	/*if(!isEmpty(frm.txtsuiteno.value) && !isValidAlphaNumeric(frm.txtsuiteno.value))
	{
		errtext += "\r\nPlease enter a valid SUITE/APT #.";
		if(oControl == null)
			oControl = frm.txtsuiteno;
	}*/
	
	if(isEmpty(frm.txtaddress.value) || !isValidEntity(frm.txtaddress.value))
	{
		errtext += "\r\nPlease enter a valid Address";
		if(oControl == null)
			oControl = frm.txtaddress;
	}
	
	if(isEmpty(frm.txtcity.value) || !isValidEntity(frm.txtcity.value))
	{
		errtext += "\r\nPlease enter a valid City.";
		if(oControl == null)
			oControl = frm.txtcity;
	}
	
	if(isEmpty(frm.txtstate.value) || !isValidEntity(frm.txtstate.value))
	{
		errtext += "\r\nPlease enter a valid State.";
		if(oControl == null)
			oControl = frm.txtstate;
	}

	if(isEmpty(frm.txtzip.value) || !isNumeric(frm.txtzip.value))
	{
		errtext += "\r\nPlease enter a valid Zip.";
		if(oControl == null)
			oControl = frm.txtzip;
	}
	
	if(isEmpty(frm.txtphone.value) || !isValidPhone(frm.txtphone.value))
	{
		errtext += "\r\nPlease enter a valid Phone Number.";
		if(oControl == null)
			oControl = frm.txtphone;
	}
	
	if(isEmpty(frm.txtemail.value) || !isValidEmail(frm.txtemail.value))
	{
		errtext += "\r\nPlease enter a valid Email ID.";
		if(oControl == null)
			oControl = frm.txtemail;
	}
	
	if(!isEmpty(frm.txtfax.value) && !isValidFax(frm.txtfax.value))
	{
		errtext += "\r\nPlease enter a valid Fax Number.";
		if(oControl == null)
			oControl = frm.txtfax;
	}
	
	
	/*if(!isEmpty(frm.txtshipsuiteno.value) && !isValidAlphaNumeric(frm.txtshipsuiteno.value))
	{
		errtext += "\r\nPlease enter a valid Shipping SUITE/APT #.";
		if(oControl == null)
			oControl = frm.txtshipsuiteno;
	}*/
	
	if(isEmpty(frm.txtshipaddress.value) || !isValidEntity(frm.txtshipaddress.value))
	{
		errtext += "\r\nPlease enter a valid Shipping Address";
		if(oControl == null)
			oControl = frm.txtshipaddress;
	}
	
	if(isEmpty(frm.txtshipcity.value) || !isValidEntity(frm.txtshipcity.value))
	{
		errtext += "\r\nPlease enter a valid Shipping City.";
		if(oControl == null)
			oControl = frm.txtshipcity;
	}
	
	if(isEmpty(frm.txtshipstate.value) || !isValidEntity(frm.txtshipstate.value))
	{
		errtext += "\r\nPlease enter a valid Shipping State.";
		if(oControl == null)
			oControl = frm.txtshipstate;
	}

	if(isEmpty(frm.txtshipzip.value) || !isNumeric(frm.txtshipzip.value))
	{
		errtext += "\r\nPlease enter a valid Shipping Zip.";
		if(oControl == null)
			oControl = frm.txtshipzip;
	}
	
	if(isEmpty(frm.txtcreditcard.value) || !isValidCCInfo(frm.txtcreditcard.value))
	{
		errtext += "\r\nPlease enter a valid Credit Card Number.";
		if(oControl == null)
			oControl = frm.txtcreditcard;
	}
		
	if(isEmpty(frm.cbomonth.value) || !isValidCCInfo(frm.cbomonth.value))
	{
		errtext += "\r\nPlease select Credit Card expiry month.";
		if(oControl == null)
			oControl = frm.cbomonth;
	}
	
	if(isEmpty(frm.cboyear.value) || !isValidCCInfo(frm.cboyear.value))
	{
		errtext += "\r\nPlease select Credit Card expiry year.";
		if(oControl == null)
			oControl = frm.cboyear;
	}
	
	if(isEmpty(frm.cbocardtype.value) || !isValidCCInfo(frm.cbocardtype.value))
	{
		errtext += "\r\nPlease select a Card Type.";
		if(oControl == null)
			oControl = frm.cbocardtype;
	}
	
	if(oControl == null){
	var ctype = frm.cbocardtype.value;
	ctype = ctype.substring(0,1).toLowerCase();
	
	if(!validateCard(frm.txtcreditcard.value,ctype,frm.cbomonth.value,frm.cboyear.value))
	{
		errtext = "Invalid Credit Card Number. Please enter again.";
		if(oControl == null)
			oControl = frm.txtcreditcard;
	}}
	
	
	if(oControl != null)
	{
		alert(errtext);
		oControl.focus();
	}
	
	return (oControl == null);
}


/* checking for valid userid */
function isValidUserID(sVal)
{
	if(sVal.length > 15 || sVal.length < 1)
		return false;
	if(sVal.indexOf(" ") != -1)
		return false;
	return true;
}


/* validate signup form*/
function validate_signup(sfrm)
{
	var frm = document.forms[sfrm];
	var oControl = null;
	var errtext = "";
	
	
	if(isEmpty(frm.txtuserid.value) || !isValidUserID(frm.txtuserid.value))
	{
		errtext = "Please enter a valid UserID(max. 15 characters and no spaces).";
		if(oControl == null)
			oControl = frm.txtuserid;
	}
	
	if(isEmpty(frm.txtpasswd.value) || !isValidEntity(frm.txtpasswd.value))
	{
		errtext += "\r\nPlease enter a valid Password.";
		if(oControl == null)
			oControl = frm.txtpasswd;
	}
	
	if(isEmpty(frm.txtconfpasswd.value) || !isValidEntity(frm.txtconfpasswd.value) 
	|| frm.txtpasswd.value != frm.txtconfpasswd.value)
	{
		errtext += "\r\nPlease confirm Password.";
		if(oControl == null)
			oControl = frm.txtconfpasswd;
	}
		
	if(isEmpty(frm.txtfirstname.value) || !isValidEntity(frm.txtfirstname.value))
	{
		errtext += "\r\nPlease enter a valid First Name.";
		if(oControl == null)
			oControl = frm.txtfirstname;
	}
	
	if(isEmpty(frm.txtlastname.value) || !isValidEntity(frm.txtlastname.value))
	{
		errtext += "\r\nPlease enter a valid Last Name.";
		if(oControl == null)
			oControl = frm.txtlastname;
	}
	
	if(!isEmpty(frm.txtcompany.value) && !isValidEntity(frm.txtcompany.value))
	{
		errtext += "\r\nPlease enter a valid Company.";
		if(oControl == null)
			oControl = frm.txtcompany;
	}
	
	if(!isEmpty(frm.txttitle.value) && !isValidEntity(frm.txttitle.value))
	{
		errtext += "\r\nPlease enter a valid Title.";
		if(oControl == null)
			oControl = frm.txttitle;
	}
	
	/*if(!isEmpty(frm.txtsuiteno.value) && !isValidAlphaNumeric(frm.txtsuiteno.value))
	{
		errtext += "\r\nPlease enter a valid SUITE/APT #.";
		if(oControl == null)
			oControl = frm.txtsuiteno;
	}*/
	
	if(isEmpty(frm.txtaddress.value) || !isValidEntity(frm.txtaddress.value))
	{
		errtext += "\r\nPlease enter a valid Address";
		if(oControl == null)
			oControl = frm.txtaddress;
	}
	
	if(isEmpty(frm.txtcity.value) || !isValidEntity(frm.txtcity.value))
	{
		errtext += "\r\nPlease enter a valid City.";
		if(oControl == null)
			oControl = frm.txtcity;
	}
	
	if(isEmpty(frm.txtstate.value) || !isValidEntity(frm.txtstate.value))
	{
		errtext += "\r\nPlease enter a valid State.";
		if(oControl == null)
			oControl = frm.txtstate;
	}

	if(isEmpty(frm.txtzip.value) || !isNumeric(frm.txtzip.value))
	{
		errtext += "\r\nPlease enter a valid Zip.";
		if(oControl == null)
			oControl = frm.txtzip;
	}
	
	if(isEmpty(frm.txtbusphone.value) || !isValidPhone(frm.txtbusphone.value))
	{
		errtext += "\r\nPlease enter a valid Business Phone Number.";
		if(oControl == null)
			oControl = frm.txtbusphone;
	}
	
	if(!isEmpty(frm.txtphone.value) && !isValidPhone(frm.txtphone.value))
	{
		errtext += "\r\nPlease enter a valid Phone Number.";
		if(oControl == null)
			oControl = frm.txtphone;
	}
	
	if(isEmpty(frm.txtemail.value) || !isValidEmail(frm.txtemail.value))
	{
		errtext += "\r\nPlease enter a valid Email ID.";
		if(oControl == null)
			oControl = frm.txtemail;
	}
	
	if(oControl != null)
	{
		alert(errtext);
		oControl.focus();
	}
	
	return (oControl == null);
}



/* validate signup form*/
function validate_profile(sfrm)
{
	var frm = document.forms[sfrm];
	var oControl = null;
	var errtext = "";
	
	if(isEmpty(frm.txtfirstname.value) || !isValidEntity(frm.txtfirstname.value))
	{
		errtext = "\r\nPlease enter a valid First Name.";
		if(oControl == null)
			oControl = frm.txtfirstname;
	}
	
	if(isEmpty(frm.txtlastname.value) || !isValidEntity(frm.txtlastname.value))
	{
		errtext += "\r\nPlease enter a valid Last Name.";
		if(oControl == null)
			oControl = frm.txtlastname;
	}
	
	if(!isEmpty(frm.txtcompany.value) && !isValidEntity(frm.txtcompany.value))
	{
		errtext += "\r\nPlease enter a valid Company.";
		if(oControl == null)
			oControl = frm.txtcompany;
	}
	
	if(!isEmpty(frm.txttitle.value) && !isValidEntity(frm.txttitle.value))
	{
		errtext += "\r\nPlease enter a valid Title.";
		if(oControl == null)
			oControl = frm.txttitle;
	}
	
	if(!isEmpty(frm.txtsuiteno.value) && !isValidAlphaNumeric(frm.txtsuiteno.value))
	{
		errtext += "\r\nPlease enter a valid SUITE/APT #.";
		if(oControl == null)
			oControl = frm.txtsuiteno;
	}
	
	if(isEmpty(frm.txtaddress.value) || !isValidEntity(frm.txtaddress.value))
	{
		errtext += "\r\nPlease enter a valid Address";
		if(oControl == null)
			oControl = frm.txtaddress;
	}
	
	if(isEmpty(frm.txtcity.value) || !isValidEntity(frm.txtcity.value))
	{
		errtext += "\r\nPlease enter a valid City.";
		if(oControl == null)
			oControl = frm.txtcity;
	}
	
	if(isEmpty(frm.txtstate.value) || !isValidEntity(frm.txtstate.value))
	{
		errtext += "\r\nPlease enter a valid State.";
		if(oControl == null)
			oControl = frm.txtstate;
	}

	if(isEmpty(frm.txtzip.value) || !isNumeric(frm.txtzip.value))
	{
		errtext += "\r\nPlease enter a valid Zip.";
		if(oControl == null)
			oControl = frm.txtzip;
	}
	
	if(isEmpty(frm.txtbusphone.value) || !isValidPhone(frm.txtbusphone.value))
	{
		errtext += "\r\nPlease enter a valid Business Phone Number.";
		if(oControl == null)
			oControl = frm.txtbusphone;
	}
	
	if(!isEmpty(frm.txtphone.value) && !isValidPhone(frm.txtphone.value))
	{
		errtext += "\r\nPlease enter a valid Phone Number.";
		if(oControl == null)
			oControl = frm.txtphone;
	}
	
	if(isEmpty(frm.txtemail.value) || !isValidEmail(frm.txtemail.value))
	{
		errtext += "\r\nPlease enter a valid Email ID.";
		if(oControl == null)
			oControl = frm.txtemail;
	}
		
	if(oControl != null)
	{
		alert(errtext);
		oControl.focus();
	}
	
	return (oControl == null);
}



/* Code for Credit Card Validation*/
function mod10( cardNumber ) { // LUHN Formula for validation of credit card numbers.
	var ar = new Array( cardNumber.length );
	var i = 0,sum = 0;


    	for( i = 0; i < cardNumber.length; ++i ) {
    		ar[i] = parseInt(cardNumber.charAt(i));
    	}
    	for( i = ar.length -2; i >= 0; i-=2 ) { // you have to start from the right, and work back.
    		ar[i] *= 2;							 // every second digit starting with the right most (check digit)
    		if( ar[i] > 9 ) ar[i]-=9;			 // will be doubled, and summed with the skipped digits.
    	}										 // if the double digit is > 9, ADD those individual digits together 


        	for( i = 0; i < ar.length; ++i ) {
        		sum += ar[i];						 // if the sum is divisible by 10 mod10 succeeds
        	}
        	return (((sum%10)==0)?true:false);	 	
    }


        function expired( month, year ) {
        	var now = new Date();							// this function is designed to be Y2K compliant.
        	var expiresIn = new Date(year,month,0,0,0);		// create an expired on date object with valid thru expiration date
        	expiresIn.setMonth(expiresIn.getMonth()+1);		// adjust the month, to first day, hour, minute & second of expired month
        	if( now.getTime() < expiresIn.getTime() ) return false;
        	return true;									// then we get the miliseconds, and do a long integer comparison
}


function validateCard(cardNumber,cardType,cardMonth,cardYear) {
		if( cardNumber.length == 0 ) {						//most of these checks are self explanitory
		return false;				
		}
		for( var i = 0; i < cardNumber.length; ++i ) {		// make sure the number is all digits.. (by design)
		var c = cardNumber.charAt(i);


		if( c < '0' || c > '9' ) {
		alert("Please enter a valid card number. Use only digits. do not use spaces or hyphens.");
		return false;
		}
		}
		var length = cardNumber.length;			//perform card specific length and prefix tests


		switch( cardType ) {
		case 'a':


		if( length != 15 ) {
		
		return;
		}
		var prefix = parseInt( cardNumber.substring(0,2));


		if( prefix != 34 && prefix != 37 ) {
		
		return;
		}
		break;
		case 'd':


		if( length != 16 ) {
		
		return;
		}
		var prefix = parseInt( cardNumber.substring(0,4));


		if( prefix != 6011 ) {
		
			return;
		}
		break;
		case 'm':


	if( length != 16 ) {
		
		return;
	}
	var prefix = parseInt( cardNumber.substring(0,2));


		if( prefix < 51 || prefix > 55) {
			
			return;
		}
		break;
	case 'v':


		if( length != 16 && length != 13 ) {
			return;
		}
		var prefix = parseInt( cardNumber.substring(0,1));


    		if( prefix != 4 ) {
    			return;
    		}
    		break;
    }
    	if( !mod10( cardNumber ) ) { 		// run the check digit algorithm
    		return false;
    	}
    	if( expired( cardMonth, cardYear ) ) {							// check if entered date is already expired.
    		return false;
    	}
                                                	
    	return true; // at this point card has not been proven to be invalid
}

window.defaultStatus = 'LabCommerce - Providing Products, Solutions and value to laboratories';

/* sets the text on status bar*/
function setStatus(sVal)
{
	window.status = sVal;
}
