
//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 validate_shipzip()
{
	var zip = trimBetweenSpaces(trimBegEndSpaces(stripOffNonDigit(document.ccform.UMshipzip.value)));
	if (zip.length == 0 || zip.length < 5)
	{
	 alert ("Error: Please fill out the 5 digit Shipping \"zip\" field.");
	 document.ccform.UMshipzip.focus == true;
	 return false;
	}
	return true;
}

function validate_ship_info()
{
	var shipname = document.ccform.UMname.value;
	var street = document.ccform.UMshipstreet.value;
	var state = document.ccform.UMshipstate.value;
	var country = document.ccform.UMshipcountry.value;
	var city = document.ccform.UMshipcity.value;
	var zip = trimBetweenSpaces(trimBegEndSpaces(stripOffNonDigit(document.ccform.UMshipzip.value)));
        var email1 = document.ccform.UMemail.value;
        valid_email(email1)

	
	if (shipname.length == 0 || !isAlphaSymbols(shipname, ".,' "))
	{
	 alert ("Error: Please fill out the \"Customer Name\" field.\nThe Customer Name field can only have Alpha Characters!");
	 document.ccform.UMname.focus == true;
	 return false;
	}

	if (street.length == 0)
	{
	 alert ("Error: Please fill out the Shipping street field.");
	 document.ccform.UMshipstreet.focus == true;
	 return false;
	}
	if (city.length == 0)
	{
	 alert ("Error: Please fill out the Shipping City field.");
	 document.ccform.UMshipcity.focus == true;
	 return false;
         }
	if (state.length == 0)
	{
	 alert ("Error: Please fill out the Shipping State field.");
	 document.ccform.UMshipstate.focus == true;
	 return false;
         }
	if (country.length == 0)
	{
	 alert ("Error: Please fill out the Shipping Country field.");
	 document.ccform.UMshipcountry.focus == true;
	 return false;
         }

	if (zip.length == 0 || zip.length < 5)
	{
	 alert ("Error: Please fill out the 5 digit shipping \"zip\" field.");
	 document.ccform.UMshipzip.focus == true;
	 return false;
	}
	return true;
}



function validate_credit_Form()
{
    var CCN = trimBetweenSpaces(trimBegEndSpaces(stripOffNonDigit(document.ccform.UMcard.value)));
	var expireDate = trimBetweenSpaces(trimBegEndSpaces(stripOffNonDigit(document.ccform.UMexpirM.value + document.ccform.UMexpirY.value)));
	var name = document.ccform.UMname.value;
	var street = document.ccform.UMstreet.value;
	var zip = trimBetweenSpaces(trimBegEndSpaces(stripOffNonDigit(document.ccform.UMzip.value)));

	if (name.length == 0 || !isAlphaSymbols(name, ".,' "))
	{
	 alert ("Error: Please fill out the \"Customer Name\" field.\nThe Customer Name field can only have Alpha Characters!");
	 document.ccform.UMname.focus == true;
	 return false;
	}

	if (CCN.length == 0)
	{
		   alert ("Error: Please input your credit card number");
		   document.ccform.UMcard.focus == true;
		   return false;
		}
	 if (expireDate.length < 4)
	 {
	  alert ("Error: Incorrect expire date.\nThere should be 4 digits in the expire date idicating mm/yy.\nIt appears that you have less than 4.");
		   document.ccform.UMexpirM.focus == true;
		   return false;
	 }

	if (street.length == 0)
	{
	 alert ("Error: Please fill out the Credit Card street field.");
	 document.ccform.UMstreet.focus == true;
	 return false;
	}

	if (zip.length == 0 || zip.length < 5)
	{
	 alert ("Error: Please fill out the 5 digit Credit Card \"zip\" field.");
	 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
}

function valid_email(email_address) {

    // Check the length
    if (email_address.length < 5) {
	 document.ccform.UMemail.focus == true;
		   alert ("Error: Invalid email address");
        return false
    }
    
    // Check @ and .
    at_location = email_address.indexOf("@")
    dot_location = email_address.lastIndexOf(".")
    
    if (at_location == -1 || dot_location == -1 || at_location > dot_location ) {
	 document.ccform.UMemail.focus == true;
		   alert ("Error: Invalid email address");
        return false
    }

    // Is there at least one character before @?
    if (at_location == 0) {
	 document.ccform.UMemail.focus == true;
		   alert ("Error: Invalid email address");
        return false
    }
    
    // Is there at least one character between @ and .?
    if (dot_location - at_location < 2 ) {
	 document.ccform.UMemail.focus == true;
		   alert ("Error: Invalid email address");
        return false
    }

    // Is there at least one character after .?
    if (email_address.length - dot_location < 2) {
        return false
	 document.ccform.UMemail.focus == true;
		   alert ("Error: Invalid email address");
    }

    // Otherwise, it's a valid address, so return true
    return true
}

