﻿function  validateCC(CardType,CardNumber,expireMonth,expireYear,CvvCode,ExpireMsg,NumOfDigitsMsg,WrongNumMsg,WrongCompanyMsg,CvvMsg)
{	
	// card Type:	1: visa 
	//		2: isracard 
	//		3: mastercard 
	//		4: American Express 
	//		5: Diners 
	//		6: Ashmoret

	var errDesc,lsNumber;
	var lsChar;		//an individual character
	var lnTotal;	//Sum of all calculations
	var lnDigit;	//A digit found within a credit card number
	var lnPosition; //identifies a character position in a string
	var lnSum ;		//Sum of calculations for a specific set
	var lnMultiplier;
	var isValid=true;
	
	var arrErr=new Array();
	arrErr[0]="";
	arrErr[1]=ExpireMsg;
	arrErr[2]=NumOfDigitsMsg;
	arrErr[3]=WrongNumMsg;
	arrErr[4]=WrongCompanyMsg;
	arrErr[5]=CvvMsg;
	var toDay=new Date();
	errDesc="";
	
	if (expireYear < toDay.getYear() || (expireYear ==toDay.getYear() && expireMonth < (toDay.getMonth()+1))) 
	{
		 isValid=false;
	     errDesc=arrErr[1];	     
	     alert(errDesc);
	     return isValid;
	}
    
	
   	// Strip all characters that are not numbers.
   	// Loop through each character in the card number submited
    	var pos=0;	
	var lsNumber="";
	lnTotal=0;
	while(pos<=CardNumber.length){
		lsChar=CardNumber.substr(pos,1 ); //Grab the current character
		if (!lsChar.isNaN){
			lsNumber = lsNumber +lsChar;
		}
		pos=pos+1;		
	}
	//The credit card number must be between 13 and 16 digits.
		 
	// If the length of the number is less then 13 digits:

	if (lsNumber.length < 13 && lsNumber.length != 8 && lsNumber.length != 9 )
	{ 	   
		isValid=false;
		errDesc=arrErr[2];   
		alert(errDesc);
		return isValid;		
	}
	//If the length of the number is more then 16 digits, then returns error
    	if (lsNumber.length > 16 )
	{
		isValid=false;
		errDesc=arrErr[2]; 
	    alert(errDesc);
		return isValid;
	}
	
	// CvvCode of exactly 3 digits, only for Visa cards at the moment
	if (CardType=="1"){
		if (CvvCode.length != 3)
		{
				isValid=false;
				errDesc=arrErr[5]; 
				alert(errDesc);
				return isValid;
		}
	}
	
    //********************************************
    //**   -- The credit card number must start with:
    //**   -- 4 for Visa Cards
    //**   -- 34,37 for American Express Cards
    //**   -- 51 to 55 for MasterCards
    //**   -- 30,36,38 for Diners
    //********************************************   
    
    //Choose action based on type of card
	// visa:
	if (CardType=="1"){
		if  (lsNumber.substr(0,1) != "4"){
			isValid=false;
			errDesc=arrErr[4]; 
			alert(errDesc);
			return isValid;
		}          	
	}
    //American Express
    if (CardType=="4" ){
	    if (lsNumber.substr(0,2) !="37" && lsNumber.substr(0,2) !="34"){ 
			isValid=false;
			errDesc=arrErr[4]; 
	        alert(errDesc);
			return isValid;
	    }
	}
 
    //Mastercard
    if (CardType=="3" ){  
		if (lsNumber.substr(0,2) !="51" && lsNumber.substr(0,2) !="52" && lsNumber.substr(0,2) !="53" && lsNumber.substr(0,2) !="54" && lsNumber.substr(0,2) !="55"){
			isValid=false;
			errDesc=arrErr[4]; 
			alert(errDesc);
			return isValid;            
        }
	}
     //Diners
    if (CardType=="5"){
        if (lsNumber.substr(0,2) !="30" && lsNumber.substr(0,2) !="36" && lsNumber.substr(0,2) !="38"){ 
			isValid=false;
			errDesc=arrErr[4];
			alert(errDesc);
			return isValid;             
        }
	}
        
 	
   // If the credit card number is less then 16 digits add zeros
   // to the beginning to make it 16 digits.
    if (lsNumber.length > 9 && CardType != "6")
    {	
		while  (lsNumber.length < 16)
		{    
		    //Insert 0 to the beginning of the number
		    lsNumber = "0" + lsNumber;    
		}
    
   	
    	//Multiply each digit of the credit card number by the corresponding digit of
    	//the mask, and sum the results together.
    
    
    	//Loop through each digit
  		lnPosition = 0; 
		while (lnPosition<16)
		{
		  	//Parse a digit from a specified position in the number
		    	lnDigit = lsNumber.substr(lnPosition,1);
		         
		        //Determine if we multiply by:
		    	// 1 (Even)
		        // 2 (Odd)
		        // based on the position that we are reading the digit from
		    	lnMultiplier = 1 + ((lnPosition+1) % 2);
		    
		    	// Calculate the sum by multiplying the digit and the Multiplier
		        lnSum = lnDigit * lnMultiplier;
		    
		    	// (Single digits roll over to remain single. We manually have to do this.)
		    	// If the Sum is 10 or more, subtract 9
		       	if (lnSum > 9 )
				{
				lnSum =lnSum - 9;
		    	}
			   	// Add the sum to the total of all sums
		   		lnTotal = lnTotal + lnSum;
				lnPosition++;
		}

		//Once all the results are summed divide
		// by 10, if there is no remainder then the credit card number is valid.
   
    	if ((lnTotal % 10) == 0){ 
    		errDesc=arrErr[0]; 
    	}
		else{        
		isValid=false;
		errDesc=arrErr[3];
		alert(errDesc);
	    	return isValid;             
    	}
    }
    else
    {
	while  (lsNumber.length < 9)
	{    
	    	//Insert 0 to the beginning of the number
	    	lsNumber = "0" + lsNumber;    
	}
	// 	multiply number by increasing numbers e.g.
	//	0 1 0 8 3 0 5 2 9	-- the number
	//	9 8 7 6 5 4 3 2 1	-- the multiply
	lnMultiplier = 9
	lnPosition = 0 
	lnTotal=0;
	while (lnPosition<9)
	{
		lnDigit = parseInt(lsNumber.substr(lnPosition,1));     
		lnTotal = lnTotal + (lnDigit * lnMultiplier);
		lnPosition=lnPosition+1;
		lnMultiplier = lnMultiplier-1;
	}
	if ((lnTotal % 11) == 0)
	{ 
	        IerrDesc=arrErr[0]; 
	}
	else
	{
	      	isValid=false;
		errDesc=arrErr[3];
		alert(errDesc);
	   		return isValid;  
	}
    }	
    if (errDesc!="")
   {
	alert(errDesc);
   }
   errDesc="";
   return isValid;	
}