//SCRIPT TO CHECK CREDIT CARD INFORMATION
//[1]'ccNum' and 'ccExpire' ARE REFERENCES TO THE TEXT OBJECTS WHICH HOLD THE INPUT CREDIT CARD NUMBER AND EXPIRATION.  
//[2]'ccType' IS A REF TO THE SELECT OBJECT CONTAINING THE CREDIT CARD SELECTION.
//[3] 'currY' and 'currM' ARE THE CURRENT YEAR & MONTH (INTEGERS)...IF NULL, CLIENT VALUES ARE USED
function CheckCCInfo(ccNum,ccExpire,ccCVV2, ccType,currY,currM)
{
	//REMOVE UNWANTED CHARS
	var removeREGX = /('|"|'|\n|\r|\t)/g;
	ccNum.value = ccNum.value.replace(removeREGX," ");
	ccExpire.value = ccExpire.value.replace(removeREGX," ");

	//GET YY & MM IF NULL
	var today = new Date();
	if(!currY) currY = today.getFullYear();
	if(!currM) currM = today.getMonth() + 1;

	if(ccType.selectedIndex == 0)
	{
		alert("Please select a credit card.");
		ccType.focus();
		return false;
	}
	var ccTypeValue = ccType.value;
	
	var cardNbrLen = {
						"MasterCard"       : 16,
						"Visa"             : 16,
						"Discover"         : 16,
						"American Express" : 15
	                  };
	var cardFirstNbr = {
						"MasterCard"       : 5,
						"Visa"             : 4,
						"Discover"         : 6,
						"American Express" : 3
	                    };
	//CHECK CC NBR
	var arrDigits = ccNum.value.match(/\d/g);
	if(!(arrDigits))
	{
		alert("Please enter a value for the Credit Card Number");
		ccNum.focus();
		return false;
	}
	else
	{
		//REFORMAT NBRS
		var dstr = new String("");
		for(i=1; i<arrDigits.length+1; i++)
		{
			dstr += ((i!=1)&&(i%4==0)) ? arrDigits[i-1]+" " : arrDigits[i-1] ;
		}
		ccNum.value = dstr;
		if(cardNbrLen[ccTypeValue])
		{
			if(arrDigits.length != cardNbrLen[ccTypeValue])
			{
				alert(ccTypeValue+" must have "+cardNbrLen[ccTypeValue]+" digits.\n\nDigits found : "+arrDigits.length);
				ccNum.focus();
				return false;
			}
			if(arrDigits[0] != cardFirstNbr[ccTypeValue])
			{
				alert("Valid "+ccTypeValue+" numbers must begin with : \""+cardFirstNbr[ccTypeValue]+"\".\n\nFirst digit found : \""+arrDigits[0]+"\"");
				ccNum.focus();
				return false;
			}
		}
	}
	//CK EXPIRATION
	arrDigits = ccExpire.value.match(/\d+/g);
	if(!(arrDigits))
	{
		alert("Credit Card Expiration appears invalid.\n\nPlease enter a value of the form 'mm/yy'");
		ccExpire.focus();
		return false;
	}
	else if(arrDigits.length != 2) 
	{
		alert("Credit Card Expiration appears invalid.\n\nPlease enter a value of the form 'mm/yy'");
		ccExpire.focus();
		return false;	
	}
	else
	{
		var m = arrDigits[0];	
		if((arrDigits[0] < 1) || (arrDigits[0] > 12) || m.length != 2)
		{
			alert("Expiration month appears invalid.\n\nIt should be a number ranging from 1 - 12.\n\nValue found : "+arrDigits[0]);
			ccExpire.focus();
			return false;	
		}
		var y = arrDigits[1];
		if((y.length != 2) && (y.length != 4))
		{
			alert("Expiration year appears invalid.\n\nIt should be either 2 or 4 digits long.\n\nDigits found : "+y.length);
			ccExpire.focus();
			return false;	
		}
		else
		{
			if(y.length == 2) y = "20" + y;
			y = parseInt(y);
			if((y < currY) || (y > currY+10))
			{
				alert("Expiration year appears invalid.\n\nIt should be greater than "+(currY-1)+" and less than "+(currY+10)+".\n\nValue found : "+y);
				ccExpire.focus();
				return false;	
			}
			if((y <= currY) && (arrDigits[0] < currM))
			{
				alert("Your credit card appears expired.");
				return false;
			}
			
			var arrDigits = new String(ccCVV2.value.match(/\d+/g));
			
			if(!(arrDigits))
			{
				alert("The CVV2 code appears to be invalid.\n\nPlease enter a value.");
				ccCVV2.focus();
				return false;
			}
			else if(arrDigits.length < 3 || arrDigits.length > 4)
			{
				alert("The CVV2 code appears invalid.\n\nIt should be 3 digits long.\n\nDigits found : "+arrDigits.length);
				ccCVV2.focus();
				return false;
			}
		}
	}
	return true;
}

