// Check that the number of characters in a string is between a max and a min
function isValidLength(string, min, max) {
    if (string.length < min || string.length > max) {
        return false;
    } else {
        return true;
    }    
}

function isSame( pass, verifypass)
{
    var check = 0;
    var result = true;

    for (var i = 0; i < pass.length; i++)
    {
        if (pass.charAt(i)!=verifypass.charAt(i)){
            result=false;
        }

    }

    return result;
}

function isValidPurchase(formfield)
{
    var result = false;

    for(var i=0; i < 5; i++)
    {
        if (formfield[i].checked)
            result = true;
    }

    return result;
}

function validRequired(formField)
{
    var result = true;

    if (formField.length == 0)
    {
        result = false;
    }

    return result;
}


function allDigits(str)
{
    return isNumeric(str,true);
}

function isValidExpDate(formField,required)
{
    var result = true;
    var formValue = formField.value;

    if (required && !validRequired(formField)) {
        result = false;
    }

    if (result && (formField.length>0))
    {
        var elems = formField.split("/");

        result = (elems.length == 2); // should be two components
        var expired = false;

        if (result)
        {
            var month = parseInt(elems[0]);
            var year = parseInt(elems[1]);

            if (elems[1].length == 2) {
                year += 2000;
            }

            var now = new Date();
            var nowMonth = now.getMonth() + 1;
            var nowYear = now.getFullYear();

            expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));

            result = allDigits(elems[0]) && (month > 0) && (month < 13) && allDigits(elems[1]) && ((elems[1].length == 2) || (elems[1].length == 4));
        }

        if (!result) {
            result = false;
        } else if (expired) {
            result = false;
        }
    } 

    return result;
}

//Check that the expiration date is valid.

function isValidExp(number){

    if (! number.length ==4) return false;

    var expired = false;
    var isValidMonth = false;
    var first2digits = number.substring(0,2);
    var last2digits = number.substring(2,4);
    var fullyear = 2000 + parseInt(parseFloat(last2digits));
    var month = parseInt(parseFloat(first2digits));
    var now = new Date();
    var nowMonth = now.getMonth() + 1;
    var nowYear = now.getFullYear();

    expired = (nowYear > fullyear) || ((nowYear == fullyear ) && (nowMonth > month));

    if (expired) {
        return false;
    }

    isValidMonth = (nowMonth<=12);
    if (!isValidMonth) {
        return false;
    }

    if ((!allDigits(first2digits))||(!allDigits(last2digits))) {
        return false;
    }

    return true;
}//end isValidExp

// Check that a credit card number is valid based using the LUHN formula (mod10 is 0)
function isValidCreditCard(number) {
	var tempNum = parseInt(number);
	if (tempNum == 4222222222222222){ 
	return true; }
	
	number = '' + number;
	
	if (number.length > 16 || number.length < 13 ) return false;
	else if (getMod10(number) != 0) return false;
	else if (arguments[1]) {
		var type = arguments[1];
		var first2digits = number.substring(0, 2);
		var first4digits = number.substring(0, 4);
		
		if (type.toLowerCase() == 'visa' && number.substring(0, 1) == 4 &&
			(number.length == 16 || number.length == 13 )) return true;
		else if (type.toLowerCase() == 'mastercard' && number.length == 16 &&
			(first2digits == '51' || first2digits == '52' || first2digits == '53' || first2digits == '54' || first2digits == '55')) return true;
		else if (type.toLowerCase() == 'american express' && number.length == 15 && 
			(first2digits == '34' || first2digits == '37')) return true;
		else if (type.toLowerCase() == 'diners club' && number.length == 14 && 
			(first2digits == '30' || first2digits == '36' || first2digits == '38')) return true;
		else if (type.toLowerCase() == 'discover' && number.length == 16 && first4digits == '6011') return true;
		else if (type.toLowerCase() == 'enroute' && number.length == 15 && 
			(first4digits == '2014' || first4digits == '2149')) return true;
		else if (type.toLowerCase() == 'jcb' && number.length == 16 &&
			(first4digits == '3088' || first4digits == '3096' || first4digits == '3112' || first4digits == '3158' || first4digits == '3337' || first4digits == '3528')) return true;
		else return true;
	} else {
        return true;
    }
}

// Check that an email address is valid based on RFC 821 (?)
function isValidEmail(address) {
	if (address.indexOf('@') < 1) return false;
	var name = address.substring(0, address.indexOf('@'));
	var domain = address.substring(address.indexOf('@') + 1);
	if (name.indexOf('(') != -1 || name.indexOf(')') != -1 || name.indexOf('<') != -1 || name.indexOf('>') != -1 || name.indexOf(',') != -1 || name.indexOf(';') != -1 || name.indexOf(':') != -1 || name.indexOf('\\') != -1 || name.indexOf('"') != -1 || name.indexOf('[') != -1 || name.indexOf(']') != -1 || name.indexOf(' ') != -1) return false;
	if (domain.indexOf('(') != -1 || domain.indexOf(')') != -1 || domain.indexOf('<') != -1 || domain.indexOf('>') != -1 || domain.indexOf(',') != -1 || domain.indexOf(';') != -1 || domain.indexOf(':') != -1 || domain.indexOf('\\') != -1 || domain.indexOf('"') != -1 || domain.indexOf('[') != -1 || domain.indexOf(']') != -1 || domain.indexOf(' ') != -1) return false;
	return true;
}


// Check that a US zip code is valid
function isValidZipcode(zipcode) {
    zipcode = removeSpaces(zipcode);
    
    if (!(zipcode.length == 5) || !isNumeric(zipcode)) return false;
    
    if (zipcode.value == 0) return false;
    
    var x=0;
    for (var i = 0; i < zipcode.length; i++) {
        if (zipcode.charAt(i) == '0') x++;
    }
    
    if (x == 5) return false;
    return true;
}


// Check that a Canadian postal code is valid
function isValidPostalcode(postalcode) {
    if (postalcode.search) {
        postalcode = removeSpaces(postalcode);
        
        if (postalcode.length == 6 && postalcode.search(/^\w\d\w\d\w\d$/) != -1)
            return true;
        else if (postalcode.length == 7 && postalcode.search(/^\w\d\w\-d\w\d$/) != -1)
            return true;
        else
            return false;
    }
    return true;
}

function validChars (address){
    for (var i = 0; i < address.length; i++) {
        if ((address.charAt(i) == '\'')||(address.charAt(i) == ' ')) {
            return false;
        }
    }

    return true;	
}

function aintZero(num){
    var i_num = parseInt(num);
    if (i_num == 0) {
        return false;
    }

    return true;
} 

// Check that a string contains only letters and numbers
function isAlphanumeric(string, ignoreWhiteSpace) {
    if (string.search) {
        if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) 
            return false;
    }
    return true;
}

// Check that a string contains only letters
function isAlphabetic(string, ignoreWhiteSpace) {
    if (string.search) {
        if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)) 
            return false;
    }
    return true;
}

function isValidPhone(string, ignoreWhiteSpace) {
    if (string.search) {
        if ((ignoreWhiteSpace && string.search(/[^0-9\s\-\(\)]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)) 
            return false;
    }
    return true;
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
    if (string.search) {
        if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1))
            return false;
    }
    return true;
}

function isDigit (c) {   
    return ((c >= "0") && (c <= "9"))
}

function isEmpty(s) {
    return ((s == null) || (s.length == 0))
}

function isFloat (s) {   
    var i;
    var seenDecimalPoint = false;
    var decimalPointDelimiter = ".";
    var defaultEMP = false

    if (isEmpty(s)) 
    if (isFloat.arguments.length == 1)
        return defaultEMP;
    else
        return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++) {   
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) 
            seenDecimalPoint = true;
        else if (!isDigit(c))
            return false;
    }

    // All characters are numbers.
    return true;
}


// Remove characters that might cause security problems from a string 
function removeBadCharacters(string) {
    if (string.replace) {
        string.replace(/[<>\"\.\'%;\.\)\(&\+]/, '');
    }
    return string;
}

// Remove all spaces from a string
function removeSpaces(string) {
    var newString = '';
    for (var i = 0; i < string.length; i++) {
        if (string.charAt(i) != ' ') newString += string.charAt(i);
    }
    return newString;
}

// Remove leading and trailing whitespace from a string
function trimWhitespace(string) {
    var newString  = '';
    var substring  = '';
    beginningFound = false;

    // copy characters over to a new string
    // retain whitespace characters if they are between other characters
    for (var i = 0; i < string.length; i++) {
        // copy non-whitespace characters
        if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {
            // if the temporary string contains some whitespace characters, copy them first
            if (substring != '') {
                newString += substring;
                substring = '';
            }
            newString += string.charAt(i);
            if (beginningFound == false) beginningFound = true;
        }
        // hold whitespace characters in a temporary string if they follow a non-whitespace character
        else if (beginningFound == true) substring += string.charAt(i);
    }
    return newString;
}

// Returns a checksum digit for a number using mod 10
function getMod10(number) {
    // convert number to a string and check that it contains only digits
    // return -1 for illegal input
    number = '' + number;
    number = removeSpaces(number);
    if (!isNumeric(number)) return -1;

    // calculate checksum using mod10
    var checksum = 0;
    for (var i = number.length - 1; i >= 0; i--) {
        var isOdd = ((number.length - i) % 2 != 0) ? true : false;
        digit = number.charAt(i);

        if (isOdd) 
            checksum += parseInt(digit);
        else {
            var evenDigit = parseInt(digit) * 2;
            if (evenDigit >= 10) 
                checksum += 1 + (evenDigit - 10);
            else
                checksum += evenDigit;
        }
    }
    return (checksum % 10);
}
