// JavaScript Document
//define variables - will these values overwrite functions?
global_luhn="";
invoice_flag=""; //not working - dont use
global_invoice1="";
global_invoice2="";
global_invoice3="";
global_invoice4="";
global_email="";
global_amount="";
global_VAR_REF="";
//
error_invoice1="";// we need an error flag for each field will all be empty if no errors
error_invoice2="";
error_invoice3="";
error_invoice4="";
error_email="";
error_VAR_REF="";

function validate(){

////////////////////////////////////////////////////////luhn_check(ccnum) ///////////////////////
ccnum=document.form1.luhn_account.value;
//alert(ccnum);
ccnum = "" + ccnum;
var i, sum, weight;
sum=0;
for (i = 0; i < ccnum.length - 1; i++) {
	weight = ccnum.substr(ccnum.length - (i + 2), 1) * (2 - (i % 2));
	sum += ((weight < 10) ? weight : (weight - 9));
}
if ( (parseInt(ccnum.substr(ccnum.length-1)) == ((10 - sum % 10) % 10)) && (ccnum.length==11 ) ) {
//alert(ccnum);
		//set all divs - copy from validate.php
		document.getElementById('account_error').style.display = 'none';
		document.getElementById('account_tick').style.display = 'block';
		document.form1.CUST_NUM.value=ccnum;//    was $doubledNumber ************
		document.form1.COMMENT2.value=ccnum;//CUST_NUM is not passed back
		//
		if(ccnum.substr(0,2)=="81"){
			
			c_account="lfdn";//
		}else{
			
			c_account="nan";//
		}
		//
		document.form1.ACCOUNT.value=c_account;
		//set global variable with card_number
		global_luhn=ccnum
		//return (global_luhn);
} else {
		//alert("not valid");
		//set all divs - copy from validate.php
		document.getElementById('account_error').style.display = 'block';//show error
		document.getElementById('account_tick').style.display = 'none';//show opposite
		document.form1.CUST_NUM.value="";//    clear hidden fields
		document.form1.COMMENT2.value="";//CUST_NUM is not passed back
		global_luhn="";
		//return (global_luhn);
}
///////////////////////////////////////////////////////invoice1///////////////////////////////////////////////

	invoice1=document.form1.invoice1.value;
	//alert(invoice1);
	myRegExp = /[0-9]+/;
	//
	if(myRegExp.test(invoice1)){//there are numbers in myNumber
		document.getElementById('invoice_error').style.display = 'none';
		document.getElementById('invoice_tick').style.display = 'block';//opposite
		error_invoice1="";//no error for invoice1
		global_invoice1=invoice1;
	}else{//there are no numbers in myNumber
		document.getElementById('invoice_error').style.display = 'block';
		document.getElementById('invoice_tick').style.display = 'none';//opposite
		document.form1.COMMENT1.value='';
		global_invoice1="";
		error_invoice1="invoice1";//no error for invoice1
	}

	myRegExp2 = /[a-z]/;//check for letters
	//
	if(myRegExp2.test(invoice1)){//there are letters in myNumber
		document.getElementById('invoice_error').style.display = 'block';
		document.getElementById('invoice_tick').style.display = 'none';//opposite
		document.form1.COMMENT1.value='';
		global_invoice1="";
		error_invoice1="invoice1";//no error for invoice1
	}

	// user may want to remove invoice - so if invoice empty reset error message to hidden				
	if(invoice1==""){
		global_invoice1="";
		error_invoice1="";//no error for invoice1
		document.getElementById('invoice_error').style.display = 'none';
		document.getElementById('invoice_tick').style.display = 'none'; //dont show tick if no email
		document.form1.COMMENT1.value='';
	}

//////////////////////////////////////////////invoice2////////////////////////////////////////////////////////////////////

	invoice=document.form1.invoice2.value;
	//alert(invoice);
	myRegExp = /[0-9]+/;
	if(myRegExp.test(invoice)){//there are numbers in myNumber
		document.getElementById('invoice_error2').style.display = 'none';
		document.getElementById('invoice_tick2').style.display = 'block';//opposite
		error_invoice2="";//no error for invoice2
		global_invoice2=", "+invoice;
	}else{//there are no numbers in myNumber
		document.getElementById('invoice_error2').style.display = 'block';
		document.getElementById('invoice_tick2').style.display = 'none';//opposite
		error_invoice2="invoice2";// error for invoice2
		global_invoice2="";
	}
	myRegExp2 = /[a-z]/;//check for letters
	//
	if(myRegExp2.test(invoice)){//there are letters in myNumber
		document.getElementById('invoice_error2').style.display = 'block';
		document.getElementById('invoice_tick2').style.display = 'none';//opposite
		global_invoice2="";
		error_invoice2="invoice2";//no error for invoice1
	}	
	// user may want to remove invoice - so if invoice empty reset error message to hidden
	if(invoice==""){
		error_invoice2="";//no error for invoice2
		global_invoice2="";
		document.getElementById('invoice_error2').style.display = 'none';
		document.getElementById('invoice_tick2').style.display = 'none'; //dont show tick if no email
	}

//////////////////////////////////////////invoice3//////////////////////////////////////////////////

	invoice=document.form1.invoice3.value;
	myRegExp = /[0-9]+/;
	if(myRegExp.test(invoice)){//there are numbers in myNumber
		document.getElementById('invoice_error3').style.display = 'none';
		document.getElementById('invoice_tick3').style.display = 'block';//opposite
		error_invoice3="";//no error for invoice3
		global_invoice3=", "+invoice;
	}else{//there are no numbers in myNumber
		document.getElementById('invoice_error3').style.display = 'block';
		document.getElementById('invoice_tick3').style.display = 'none';//opposite
		error_invoice3="invoice3";// error for invoice3
		global_invoice3="";
	}
	myRegExp2 = /[a-z]/;//check for letters
	//
	if(myRegExp2.test(invoice)){//there are letters in myNumber
		document.getElementById('invoice_error3').style.display = 'block';
		document.getElementById('invoice_tick3').style.display = 'none';//opposite
		global_invoice3="";
		error_invoice3="invoice3";//no error for invoice1
	}	
	// user may want to remove invoice - so if invoice empty reset error message to hidden
	if(invoice==""){
		invoice_flag="yes";
		global_invoice3="";
		error_invoice3="";//no error for invoice3
		document.getElementById('invoice_error3').style.display = 'none';
		document.getElementById('invoice_tick3').style.display = 'none'; //dont show tick if no email
	}

/////////////////////////////invoice4/////////////////////////////////////////////////////

	invoice=document.form1.invoice4.value;
	myRegExp = /[0-9]+/;
	if(myRegExp.test(invoice)){//there are numbers in myNumber
		document.getElementById('invoice_error4').style.display = 'none';
		document.getElementById('invoice_tick4').style.display = 'block';//opposite
		invoice_flag="yes";//this is picked up by check_all function - so is return needed?
		global_invoice4=", "+invoice;
		error_invoice4="";//no error for invoice4
	}else{//there are no numbers in myNumber
		document.getElementById('invoice_error4').style.display = 'block';
		document.getElementById('invoice_tick4').style.display = 'none';//opposite
		global_invoice4="";
		error_invoice4="invoice4";// error for invoice4
	}
	myRegExp2 = /[a-z]/;//check for letters
	//
	if(myRegExp2.test(invoice)){//there are letters in myNumber
		document.getElementById('invoice_error4').style.display = 'block';
		document.getElementById('invoice_tick4').style.display = 'none';//opposite
		global_invoice4="";
		error_invoice4="invoice4";//no error for invoice1
	}	
	// user may want to remove invoice - so if invoice empty reset error message to hidden
	if(invoice==""){
		invoice_flag="yes";
		global_invoice4="";
		error_invoice4="";//no error for invoice4
		document.getElementById('invoice_error4').style.display = 'none';
		document.getElementById('invoice_tick4').style.display = 'none'; //dont show tick if no email
	}

/////////////////////////////VAR_REF/////////////////////////////////////////////////////

	VAR_REF=document.form1.VAR_REF.value;
	myRegExp = /[0-9a-zA-Z]+/
	if(myRegExp.test(VAR_REF)){//there are numbers in myNumber
		document.getElementById('VAR_REF_error').style.display = 'none';
		document.getElementById('VAR_REF_tick').style.display = 'block';//opposite
		VAR_REF_flag="yes";//this is picked up by check_all function - so is return needed?
		
		error_VAR_REF="";//no error 
	}else{//there are no numbers in myNumber
		document.getElementById('VAR_REF_error').style.display = 'block';
		document.getElementById('VAR_REF_tick').style.display = 'none';//opposite
		global_VAR_REF="";
		error_VAR_REF="VAR_REF";// error 
	}
	//
	myRegExp2 = /[^0-9a-zA-Z ]/;//check for non alphanumeric
	//
	if(myRegExp2.test(VAR_REF)){//there are non alphanumeric in myNumber
		document.getElementById('VAR_REF_error').style.display = 'block';
		document.getElementById('VAR_REF_tick').style.display = 'none';//opposite
		global_VAR_REF="";
		error_VAR_REF="VAR_REF";//no error for invoice1
	}	
	// user may want to remove invoice - so if invoice empty reset error message to hidden
	if(VAR_REF==""){
		
		global_VAR_REF="";
		error_VAR_REF="";//no error for invoice4
		document.getElementById('VAR_REF_error').style.display = 'none';
		document.getElementById('VAR_REF_tick').style.display = 'none'; //dont show tick if no email
	}



/////////////////////////////////////email_check////////////////////////////////////////////////////
	email=document.form1.email.value;
	email=email.toLowerCase();//realex doesnt accept upper case emails 6/8/07   
		email_pattern=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;

			
			if ( (email.length>=8) && ( email_pattern.test(email) ) ){//if email matches the pattern
			

				document.getElementById('email_error').style.display = 'none';	
				document.getElementById('email_tick').style.display = 'block';
				document.form1.ANYTHING_ELSE.value=email;	
				global_email=email;
				error_email="";//no error for email
				//email ok
			}else{//if email is not correct format//////////////////
				document.getElementById('email_error').style.display = 'block';
				document.getElementById('email_tick').style.display = 'none';
				document.form1.ANYTHING_ELSE.value='';		
				global_email="";
				error_email="email";// error for email
			}
			
			// user may want to remove email - so if email empty reset cookie and error message to hidden
			
			if(email==""){
				document.getElementById('email_error').style.display = 'none';	
				document.getElementById('email_tick').style.display = 'none';	//dont show tick if no email
				document.form1.ANYTHING_ELSE.value='';
				global_email="";
				error_email="";//no error for email
			}

///////////////////////////////////////////////amount_check//////////////////////////////////////////
vamount=document.form1.vamount.value;
	//if (eregi($pattern, $_GET[value])){
	//	( amount == strval( floatval(amount) ) ) &&
	decimal_pos=vamount.indexOf(".");
	amount_length=vamount.length;
	
	decimal_check=amount_length-decimal_pos;//if correct decimal this will be 3
	
	wrong_decimal="";
	if ( (decimal_pos>0) && (decimal_check!=3) ){
		
		wrong_decimal="yes";
	}
	//alert( amount_length-decimal_pos );
	
	if ( (vamount>0) && (wrong_decimal!="yes")  ) {
		//alert("valid" + vamount);
		vamount = vamount*100;//no decimals    - amount must be £ with decimals
		//vamount =Math.ceil(vamount);//round up as js adds unwanted decimal
		vamount =Math.round(vamount);//round down as js adds unwanted decimal
		document.getElementById('amount_error').style.display = 'none';
		document.getElementById('amount_tick').style.display = 'block';
		document.form1.AMOUNT.value=vamount;//amount not passed back from realex - so put in comment field below
		document.form1.COMMENTS3.value=vamount;
		global_amount=vamount;
	}else{
		//alert("not valid"+vamount);
		global_amount="";
		document.getElementById('amount_error').style.display = 'block';
		document.getElementById('amount_tick').style.display = 'none';
		document.form1.AMOUNT.value='';
	}









//alert(global_luhn+' '+global_invoice1+' '+global_invoice2+' '+global_invoice3+' '+global_invoice4+' '+global_email+' '+global_amount);
error_summary=error_invoice1+error_invoice2+error_invoice3+error_invoice4+error_email+error_VAR_REF;
//alert(error_summary);
//error_summary should be empty if no errors
	if( (global_luhn!="")&&(global_amount!="")&&(error_summary=="") ){// nothing else is obligatory - invoices and email can be empty
		//alert("All valid");
		//put all invoice numbers into hidden field
		document.form1.COMMENT1.value=global_invoice1+global_invoice2+global_invoice3+global_invoice4;
		//
		document.getElementById('submit_button').innerHTML="<input type='button' name='Button' value='Please Wait..' />";
		//
		getAjax('validate.php?c_amount=' + global_amount); // add document.form1.submit to end of validate.php
		//
	}else{//make sure next button not shown
		//document.getElementById('submit_button').innerHTML="<input type='button' name='Button' value='Please Complete Mandatory Fields' />";
	}


}//function validate()

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////







