var ccErrorNo = 0;
var ccErrors = new Array ()

ccErrors [0] = "Unknown card type";
ccErrors [1] = "No card number provided";
ccErrors [2] = "Credit card number is in invalid format";
ccErrors [3] = "Credit card number is invalid";
ccErrors [4] = "Credit card number has an inappropriate number of digits";

re = /^([a-zA-Z0-9]|\s|\_|\-)+$/; 
validzip = /^([a-zA-Z]|[0-9]|-|\s)+$/;
validcity = /^([a-zA-Z]|\s)+$/; 
var GlobalFormPayment;

function CheckPrivacyTerms()
{ //alert('Inside script!!');
  obj=document.getElementById("PrivacyPolicyCheck");
  if(obj)
	{ if(obj.checked==false){//alert(obj.checked);
	   alert("Please confirm your acceptance of our Terms of Use and Privacy Policy before proceeding to payment.");
	   return false;
	  }
	  else{
		  j=document.getElementById("pay_mode");
		  //alert(j.value+'==='+j.checked);
		  if(j.value=='cheque' && j.checked==false )
		  {
			retVal=finalValidation();
			//alert(retVal);
			return retVal;
		  }
		  else if(j.value=='cheque' && j.checked==true)
		  { //alert("Inside Cheque");
			//obj=document.getElementById("GlobalFormPayment");
			//obj.action="company_representatives.php?action=payment";
			//obj.submit();
			return true;	
		  }
	     }
	}
else
	return false;
}

function SwapClass(formobj,spanobj,flag){

	//alert("inside Function"+formobj.id+"===>"+spanobj.id+"===>"+swapflag);

	if(flag==false)
	{
		
		formobj.className="searchBoxRed";
		spanobj.className="textRed";
		
	}
	else if(flag==true)
	{
		//formobj.className="v2MlUserTxt";
		spanobj.className="noterrormsg";
	}
}

function PaymentJsInitiate()
{
	GlobalFormPayment = document.getElementById("GlobalFormPayment");
	//alert("alert"+GlobalFormPayment.action.value);
}

function ccType_click(){
	var j;
	
	j=document.getElementById("v2mlccType_span");
	if (document.getElementById("v2mlccType").value ==  "" ){
		
		SwapClass(document.getElementById("v2mlccType"),j,false);
		
		j.innerHTML = "Please select card type";
		j.style.position="relative";
		return false;
    }
	else{

		SwapClass(document.getElementById("v2mlccType"),j,true);
		
		j.innerHTML = "";
		j.style.position="absolute";
		return true;
	}

	if (document.getElementById("v2mlccCode").value !=  "" ){
		return ccCodeValidation();
	}

}

function ccType_reclick(){
	if (document.getElementById("v2mlccType").value !=  ""  && document.getElementById("v2mlccNum").value != ""){
		return validatecard();
    }
	if (document.getElementById("v2mlccCode").value !=  "" ){
		return ccCodeValidation();
	}
}

function validatecard ()
	{	//lobal GlobalFormPayment;
		
		document.getElementById("v2mlccNum").value = trim(document.getElementById("v2mlccNum").value, '');
		if (validatecardfunc() == true)
			{
			document.getElementById("v2mlccNum_span").innerHTML = "";
			SwapClass(document.getElementById("v2mlccNum"),document.getElementById("v2mlccNum_span"),true);
			return true;
			}
		else
		{
		SwapClass(document.getElementById("v2mlccNum"),document.getElementById("v2mlccNum_span"),false);
		document.getElementById("v2mlccNum_span").innerHTML = GlobalFormPayment.v2mlccType.options[GlobalFormPayment.v2mlccType.selectedIndex].value+" credit card not valid !";
		return false;
		}
	}

function ccExp_click(){
	var j,yy,mm;
	var str = new String();
	document.getElementById("v2mlccExp").value = trim(document.getElementById("v2mlccExp").value, '');
	var CCexpire=document.getElementById("v2mlccExp").value;
	var d = new Date();
	mm=d.getMonth()+1; //because getmonth starts from 0 to 11 so added 1 for comparison
	str=String(d.getFullYear());
	//to get the current year
	yy = (str.substring(2,4));

	j=document.getElementById("v2mlccExp_span");

	if (document.getElementById("v2mlccExp").value==""){

		SwapClass(document.getElementById("v2mlccExp"),j,false);
		
		j.innerHTML = "Please enter credit card expiry date";
		j.style.position="relative";
		return false;
	}
	else if(CCexpire.length > 0){
		var ArrayCCexpr=CCexpire.split("/");
		if ((ArrayCCexpr.length!=2) || (ArrayCCexpr[0]=="") || (isNaN(ArrayCCexpr[0])) || (ArrayCCexpr[1]=="") || (isNaN(ArrayCCexpr[1])) || (ArrayCCexpr[0]<1) || (ArrayCCexpr[0]>12) || (ArrayCCexpr[1] < yy ) || ((ArrayCCexpr[1] == yy) && (ArrayCCexpr[0] < mm ) ))
		{
			SwapClass(document.getElementById("v2mlccExp"),j,false);

			j.innerHTML = "Please enter valid credit card expiry date";
			j.style.position="relative";
			return false;
		}
		else
		{
			SwapClass(document.getElementById("v2mlccExp"),j,true);
			j.innerHTML = "";
			j.style.position="absolute";
			return true;
		}
	}
	else{
		j.innerHTML = "";
		SwapClass(document.getElementById("v2mlccExp"),j,true);
		j.style.position="absolute";
		return true;
	}
}

function ccCode_click(){
	var j;
	document.getElementById("v2mlccCode").value = trim(document.getElementById("v2mlccCode").value, '');
	j=document.getElementById("v2mlccCode_span");
	if (document.getElementById("v2mlccType").value !=  "" )
		{
			if ((document.getElementById("v2mlccCode").value.length < 3) || ((document.getElementById("v2mlccCode").value.length >= 3) && (isNaN(document.getElementById("v2mlccCode").value))))
				{
					SwapClass(document.getElementById("v2mlccCode"),j,false);

					j.innerHTML = "Please enter proper cvv number";
					j.style.position="relative";
					return false;
				}
		return ccCodeValidation();
		}else
			{
				SwapClass(document.getElementById("v2mlccCode"),j,false);
				j.innerHTML = "Please enter proper cvv number";
				j.style.position="relative";
				return false;
			}



}

function ccName_click(){
	var j;
	document.getElementById("v2mlccName").value = trim(document.getElementById("v2mlccName").value, '');
	j=document.getElementById("v2mlccName_span");
	if (document.getElementById("v2mlccName").value==""){

		SwapClass(document.getElementById("v2mlccName"),j,false);
		j.innerHTML = "Please enter credit card holder's name";
		j.style.position="relative";
		return false;
	}
	else{
		SwapClass(document.getElementById("v2mlccName"),j,true);
		j.innerHTML = "";
		j.style.position="absolute";
		return true;
	}
}

function ccCodeValidation()
{
	j=document.getElementById("v2mlccCode_span");
	if( ( (document.getElementById("v2mlccType").value=="Visa") ) && (document.getElementById("v2mlccCode").value.length!="3") )
						{
							SwapClass(document.getElementById("v2mlccCode"),j,false);
							j.innerHTML = "Please enter only 3 digit cvv number";
							j.style.position="relative";
							return false;
							
						}
						else if( ( (document.getElementById("v2mlccType").value=="MasterCard") ) && (document.getElementById("v2mlccCode").value.length!="3") )
						{
							SwapClass(document.getElementById("v2mlccCode"),j,false);
							j.innerHTML = "Please enter only 3 digit cvv number";
							j.style.position="relative";
							return false;
							
						}
						else if( ( (document.getElementById("v2mlccType").value=="Discover") ) && (document.getElementById("v2mlccCode").value.length!="3") )
						{
							SwapClass(document.getElementById("v2mlccCode"),j,false);
							j.innerHTML = "Please enter only 3 digit cvv number";
							j.style.position="relative";
							return false;
							
						}
						else if ( ((document.getElementById("v2mlccType").value=="American Express"))&&(document.getElementById("v2mlccCode").value.length!="4"))
						{
							SwapClass(document.getElementById("v2mlccCode"),j,false);
							j.innerHTML = "Please enter only 4 digit cvv number";
							j.style.position="relative";
							return false;
							
						}
						else
						{
							j.innerHTML = "";
							SwapClass(document.getElementById("v2mlccCode"),j,true);
							j.style.position="relative";
							return true;
						}
}

function validatecardfunc () {
	validcard = false; 
	ret = stripNonNumbers (document.GlobalFormPayment.v2mlccNum.value);
	result=	document.getElementById('v2mlccType').options[document.getElementById('v2mlccType').selectedIndex].value;
	if (result == "MasterCard") {
	if (ret.length == 16) 
		{
		validcard = true;
		if ((ret.substring (0, 2) >= "51") && 
		(ret.substring (0, 2) <= "55"))
		validcard = true;
		else
		validcard = false;
		}
	else
		{
		validcard = false;
		}
	}
	if (result == "Visa") {
	if ((ret.length == 16) || (ret.length ==13))
	validcard = true;
	if (ret.substring (0, 1) != "4")
	validcard = false;
	}
	if (result == "American Express") {
	if (ret.length == 15) 
	validcard = true;
	if ((ret.substring (0, 2) != "34") && 
	(ret.substring (0, 2) != "37"))
	validcard = false;
	}
	if (result == "Discover") {
	if (ret.length == 16) 
	validcard = true;
	if (ret.substring (0, 4) != "6011")
	validcard = false;
	}
	return (validcard);
	}

	function stripNonNumbers (InString) {
	OutString="";
	for (Count=0; Count < InString.length; Count++) {
	TempChar=InString.substring (Count, Count+1);
	Strip = false;
	CharString="0123456789";
	for (Countx = 0; Countx < CharString.length; Countx++) {
	StripThis = CharString.substring(Countx, Countx+1)
	if (TempChar == StripThis) {
	Strip = true;
	break;
	}
	}
	if (Strip)
	OutString=OutString+TempChar;
	}
	return (OutString);
}


function checksumalgorithm(ccNumb) { // v2.0 
	var valid = "0123456789" // Valid digits in a credit card number 
	var len = ccNumb.length; // The length of the submitted cc number 
	var iCCN = parseInt(ccNumb); // integer of ccNumb 
	var sCCN = ccNumb.toString(); // string of ccNumb 
	sCCN = sCCN.replace (/^\s+|\s+$/g,''); // strip spaces 
	var iTotal = 0; // integer total set at zero 
	var bNum = true; // by default assume it is a number 
	var bResult = false; // by default assume it is NOT a valid cc 
	var temp; // temp variable for parsing string 
	var calc; // used for calculation of each digit 

	// Determine if the ccNumb is in fact all numbers 
	for (var j=0; j<len; j++) { 
	temp = "" + sCCN.substring(j, j+1); 
	if (valid.indexOf(temp) == "-1"){bNum = false;} 
	} 

	// if it is NOT a number, you can either alert to the fact, or just pass a failure 
	if(!bNum){ 
	/*alert("Not a Number");*/bResult = false; 
	} 

	// Determine if it is the proper length 
	if((len == 0)&&(bResult)){ // nothing, field is blank AND passed above # check 
	bResult = false; 
	} else{ // ccNumb is a number and the proper length - let's see if it is a valid card number 
	if(len >= 15){ // 15 or 16 for Amex or V/MC 
	for(var i=len;i>0;i--){ // LOOP throught the digits of the card 
	calc = parseInt(iCCN) % 10; // right most digit 
	calc = parseInt(calc); // assure it is an integer 
	iTotal += calc; // running total of the card number as we loop - Do Nothing to first digit 
	i--; // decrement the count - move to the next digit in the card 
	iCCN = iCCN / 10; // subtracts right most digit from ccNumb 
	calc = parseInt(iCCN) % 10 ; // NEXT right most digit 
	calc = calc *2; // multiply the digit by two 
	// Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7, 
	// I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple. 
	switch(calc){ 
	case 10: calc = 1; break; //5*2=10 & 1+0 = 1 
	case 12: calc = 3; break; //6*2=12 & 1+2 = 3 
	case 14: calc = 5; break; //7*2=14 & 1+4 = 5 
	case 16: calc = 7; break; //8*2=16 & 1+6 = 7 
	case 18: calc = 9; break; //9*2=18 & 1+8 = 9 
	default: calc = calc; //4*2= 8 & 8 = 8 -same for all lower numbers 
	} 
	iCCN = iCCN / 10; // subtracts right most digit from ccNum 
	iTotal += calc; // running total of the card number as we loop 
	} // END OF LOOP 
	if ((iTotal%10)==0){ // check to see if the sum Mod 10 is zero 
	bResult = true; // This IS (or could be) a valid credit card number. 
	} else { 
	bResult = false; // This could NOT be a valid credit card number 
	} 
	} 
	} 
	// change alert to on-page display or other indication as needed. 
	if(bResult) { 
		SwapClass(document.getElementById("v2mlccNum"),document.getElementById("v2mlccNum_span"),true);
	} 
	if(!bResult){ 
		SwapClass(document.getElementById("v2mlccNum"),document.getElementById("v2mlccNum_span"),false);
		document.getElementById("v2mlccNum_span").innerHTML = "Credit card not valid!";
	} 
	return bResult; // Return the results 
	} 


/*********** Credit card algorithum end *******************/


//Genereally should Trigger on onsubmit Action Added by Sag on 5 Sep 08
function finalValidation()
{
var errflag = 0;

if(ccType_click() == false)
	{
		errflag = 1;
	}


if(validatecard() == false)
	{
		errflag = 1;
	}else if(checksumalgorithm(document.getElementById("v2mlccNum").value) == false)
	{
		errflag = 1;
	}

if(ccExp_click() == false)
	{
		errflag = 1;
	}


if(ccCode_click() == false)
	{
		errflag = 1;
	}

if(ccName_click() == false)
	{
		errflag = 1;
	}




if(cBillAddress1_click() == false)
	{
		errflag = 1;
	}

if(cBillCity_click() == false)
	{
		errflag = 1;
	}


if(cBillState_click() == false)
	{
		errflag = 1;
	}


if(cBillZip_click() == false)
	{
		errflag = 1;
	}


/*if(cEmail_click() == false)
	{
		errflag = 1;
	}*/

/*if(cPhone1_click() == false)
	{
		errflag = 1;
	}*/


if(cBillCountry_click() == false)
	{
		errflag = 1;
	}



if(errflag == 0)
	{
		
		return 1;
		
	}
else
	{
		document.getElementById("mainMassage_span").innerHTML = "Please enter correct values in marked fields.";
		window.scroll(0,150); 
		return 0;
		
	}

}



// Used to validate Billing Address //
function cBillAddress1_click(){
	var j;
	document.getElementById("txtbillAddress1").value = trim(document.getElementById("txtbillAddress1").value, '');
	j=document.getElementById("v2mlbilladd1_span");
	if (document.getElementById("txtbillAddress1").value.length < 1){
		SwapClass(document.getElementById("txtbillAddress1"),j,false);
		j.innerHTML = "Please enter address</font>";
		j.style.position="relative";
		return false;
	}
	else{
		SwapClass(document.getElementById("txtbillAddress1"),j,true);
		j.innerHTML = "";
		j.style.position="absolute";
		return true;
	}
}

// Used to validate Billing City //
function cBillCity_click(){
	var j;
	document.getElementById("txtbillCity").value = trim(document.getElementById("txtbillCity").value, '');
	j=document.getElementById("v2mlbillcity_span");
	if (document.getElementById("txtbillCity").value.length < 1){
		SwapClass(document.getElementById("txtbillCity"),j,false);
		j.innerHTML = "Please enter city</b></font>";
		j.style.position="relative";
		return false;
	}else if (!validcity.test(document.getElementById("txtbillCity").value)){
		SwapClass(document.getElementById("txtbillCity"),j,false);
		j.innerHTML = "City name must contain only letters and spaces</font>";
		j.style.position="relative";
		return false;
    }
	else{
		j.innerHTML = "";
		SwapClass(document.getElementById("txtbillCity"),j,true);
		j.style.position="absolute";
		return true;
	}
}

function cBillState_click(){
	var j;
	document.getElementById("billState").value = trim(document.getElementById("billState").value, '');
	j=document.getElementById("v2mlstate_span");
	if (document.getElementById("billState").value==""){
		SwapClass(document.getElementById("billState"),j,false);
		j.innerHTML = "Please enter state";
		j.style.position="relative";
		return false;
	}else
	{
		j.innerHTML = "";
		SwapClass(document.getElementById("billState"),j,true);
		j.style.position="relative";
		return true;
	}
	
}


// Used to validate Billijng Zip //
function cBillZip_click(){
	var j;
	document.getElementById("txtbillZip").value = trim(document.getElementById("txtbillZip").value, '');
	j=document.getElementById("v2mlZip_span");
	if (document.getElementById("txtbillZip").value.length < 1){
		SwapClass(document.getElementById("txtbillZip"),j,false);
		j.innerHTML = "Please enter valid zipcode";
		j.style.position="relative";
		return false;
	}else if (!validzip.test(document.getElementById("txtbillZip").value)){
		SwapClass(document.getElementById("txtbillZip"),j,false);
		j.innerHTML = "Please enter valid zipcode";
		j.style.position="relative";
		return false;
    }
	else{
		j.innerHTML = "";
		SwapClass(document.getElementById("txtbillZip"),j,true);
		j.style.position="absolute";
		return true;
	}
}


// Used to validate e-Mail //
function cEmail_click(){
	document.getElementById("txtbillEmail").value = trim(document.getElementById("txtbillEmail").value, '');
	var j;
	j=document.getElementById("v2mlEmail_span");
	if ( (document.getElementById("txtbillEmail").value.length < 1) || ((document.getElementById("txtbillEmail").value.length >= 1) && (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(document.getElementById("txtbillEmail").value))))){
		SwapClass(document.getElementById("txtbillEmail"),j,false);
		j.innerHTML = "Please enter proper email address</font>";
		j.style.position="relative";
		return false;
	}
	else{
		j.innerHTML = "";
		SwapClass(document.getElementById("txtbillEmail"),j,true);
		j.style.position="absolute";
		return true;
	}
}

// Used to validate Phone //
function cPhone1_click(){
	var j;
	document.getElementById("txtbillPhone").value = trim(document.getElementById("txtbillPhone").value, '');
	j=document.getElementById("v2mlPhone_span");
	if (document.getElementById("txtbillPhone").value.length < 1){
		SwapClass(document.getElementById("txtbillPhone"),j,false);
		j.innerHTML = "Please enter valid phone number";
		j.style.position="relative";
		return false;
	}else if (!(/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/
.test(document.getElementById("txtbillPhone").value))){
		SwapClass(document.getElementById("txtbillPhone"),j,false);
		j.innerHTML = "Please enter valid phone number";
		j.style.position="relative";
		return false;
	}
	else{
		j.innerHTML = "";
		SwapClass(document.getElementById("txtbillPhone"),j,true);
		j.style.position="absolute";
		return true;
	}
}


function cBillCountry_click(){
	var j;
	document.getElementById("billCountry").value = trim(document.getElementById("billCountry").value, '');
	j=document.getElementById("v2mlcountry_span");
	if (document.getElementById("billCountry").value=="0"){
		SwapClass(document.getElementById("billCountry"),j,false);
		j.innerHTML = "Please select country";
		j.style.position="relative";
		return false;
	}else
	{
		j.innerHTML = "";
		SwapClass(document.getElementById("billCountry"),j,true);
		j.style.position="relative";
		return true;
	}
	
}