
//USAepay Javascript form validation and formatting script
//Developed by Mind2Web for USAepay
//For questions or comments email:
//support@usaepay.com or
//mind2webinfo@yahoo.com

function validateForm()
{
    	var CCN = trimBetweenSpaces(trimBegEndSpaces(stripOffNonDigit(document.ccform.UMcard.value)));
	var name = document.ccform.UMname.value;
	var expireDate = document.ccform.UMexpir.value;
	var street = document.ccform.UMstreet.value;
	var zip = trimBetweenSpaces(trimBegEndSpaces(stripOffNonDigit(document.ccform.UMzip.value)));
	var amount = trimBetweenSpaces(trimBegEndSpaces(stripOffNonDigit(document.ccform.UMamount.value)));
	var cvv = document.ccform.UMcvv2.value;
	
	//alert (UMexpirM);
	//alert (UMexpirY);

	if (CCN.length == 0 || expireDate.length == 0)
	{
		alert ("Please provide a Credit Card Number and Expiration Date.");
		document.ccform.UMcard.focus == true;
		return false;
	}

	else
	{
	 if (expireDate.length < 4)
	 {
	  alert ("Please enter the expiration date in the format of MMYY.\nFor example, for March 2010 enter:\n0310");
		   document.ccform.UMexpir.focus == true;
		   return false;
	 }
	
	if (amount.length == 0)
	{
	 alert ("Please enter an Amount.");
	 document.ccform.UMamount.focus == true;
	 return false;
	}

	if (cvv.length < 3 || cvv.length > 4)
	{
	 alert ("Please enter a 3 or 4 digit CVV number.");
	 document.ccform.UMcvv2.focus == true;
	 return false;
	}

	
	if (name.length == 0)
	{
	 alert ("Please enter the name that appears on the credit card.");
	 document.ccform.UMname.focus == true;
	 return false;
	}

	if (!isAlphaSymbols(name, ".,' "))
	{
	 alert ("Please enter only letters for the Name");
	 document.ccform.UMname.focus == true;
	 return false;
	}
	
	if (street.length == 0)
	{
	 alert ("Please enter your Credit Card Billing Street Address");
	 document.ccform.UMstreet.focus == true;
	 return false;
	}
	
	if (zip.length == 0 || zip.length < 5)
	{
	 alert ("Please enter your Credit Card Billing Zip Code");
	 document.ccform.UMzip.focus == true;
	 return false;
	}
	}
	
	return true;
}

// Return true if a string is combination of alpha and given symbols.
function isAlphaSymbols(objValue, symbols) {
	var ch
	
	for (var i=0; i < objValue.length; i++) {
		ch = objValue.charAt(i)
		if (isAlphaChar(ch) == false) {
			if (symbols.indexOf(ch) < 0) 
				return false
		}
	}
	return true
}

// Return true of a character is an alphabet.
function isAlphaChar( ch ) {
   	return ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z"))
}

// Stiff off any non digit char
function stripOffNonDigit(objValue) {
	var ch
	var tempStr = new String()

	for (var i=0; i<objValue.length; i++)
    {
    	if (isDigitChar(objValue.charAt(i)) == true)
    	    tempStr = tempStr + objValue.charAt(i)
    }
	
	return tempStr
}

// Return true if a character is a digit.
function isDigitChar( ch ) {
	return ( ch >= "0" && ch <= "9" )
}

// Removes leading and trailing blanks from a value
function trimBegEndSpaces(object_value)
{
    var leading_blanks = 0
    var string_end = (object_value.length)-1
    if (string_end < 0) string_end = 0

	// find first nonblank:  start with first character and scan forwards
    while (leading_blanks <= string_end && object_value.charAt(leading_blanks) == " ")
	{leading_blanks++}

	// find last nonblank:  start with last character and scan backwards
    while (string_end > leading_blanks && object_value.charAt(string_end) == " ")
        {string_end--}

	return object_value = object_value.substring(leading_blanks,string_end+1)
}

// Remove any additional spaces
function trimBetweenSpaces(objValue) {
	var blankExists = false
	var newValue = new String()
	var ch
	
	for (var i=0; i < objValue.length; i++) {
		ch = objValue.charAt(i)
		if ( ch == " " ) {
			if ( blankExists == false ) {
				blankExists = true
				newValue = newValue + ch				
			}		
		}
		else {
			newValue = newValue + ch
			blankExists = false
		}
	}
	if ( newValue == null )
		return objValue
	else
		return newValue
}
