/**
*
* Script contains all functions for validating the following objects:
*   - credit card.
*   - ip address. 
*   - email address.
*/

/**
 * Creates a class called ValidateUtils.
 */
var ValidateUtils = {
    version : "1.0.0",
    ipAddressRegExp : /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,
    emailAddressRegExp : /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
};

/**
 * Validates all required objects are defined for the current locale.
 * This should be called first in every script defined in this file that is locale aware.
 *
 * @return true if specified localization properties are defined; false otherwise
 */
ValidateUtils.validateObjects = function() {

    if (!LocalizationUtils) {
        alert("Required LocalizationUtils class no defined.");
        return false;
    }

    if (!LocalizationUtils.phoneFormatRegExp) {
        alert("Required variable not defined. Variable: [LocalizationUtils.phoneFormatRegExp]");
        return false;
    }
    
    if (!LocalizationUtils.streetAddressFormatRegExp) {
        alert("Required variable not defined. Variable: [LocalizationUtils.streetAddressFormatRegExp]");
        return false;
    }

    if (!LocalizationUtils.postalCodeValidationRegExp) {
        alert("Required variable not defined. Variable: [LocalizationUtils.postalCodeValidationRegExp]");
        return false;
    }

    return true;
}

/**
 * Validates the credit card number
 *
 * @param card value to validate
 * @return true if the credit card number is valid; false, otherwise.
 */
ValidateUtils.isValidCreditCard = function(card) {
  var val, i, digits = 0, check_sum = 0;
  var num;
  var weight = 1;
  var cnum = new Array();

  // this will parse out all non-digit chars.
  for (i=0; i<card.length; i++) {
    num = card.charAt(i);
    if (num>='0' && num<='9') {
      cnum[digits] = num; //Integer.parseInt(card.substring(i,i+1));
      digits++;
    }
  }

  // this verifies that the numbers are MOD 10
  for (i=digits-1; i>=0; i--) {
    val = (cnum[i] * weight);
    if (val>9) val -= 9;
    check_sum += val;
    weight = (weight == 1) ? 2 : 1;
  }
  // returns true or false depending on the card number.
  var result = ((check_sum % 10) == 0);

  return (result);
}


/**
 * Validates if a string value is an ip address.
 *
 * @param value to validate
 * @return true if the ip address format is valid; false, otherwise.
 */
ValidateUtils.isIPAddress = function(value) {
	if (window.RegExp) {
		var ipAddress = value.replace(".","");
		if (NumberUtils.isInteger(ipAddress)) {
			return true;
		}
	}  else {
		if (ValidateUtils.ipAddressRegExp.test(value)) {
			return true;
		}
	}

	return false;
}

/**
 * Validates an e-mail address is in the correct format.
 *
 * @param str to validate
 * @return true if the email address format is valid; false, otherwise.
 */
ValidateUtils.isValidEmailAddress = function(str) {

    if (window.RegExp) {
        return ValidateUtils.emailAddressRegExp.test(str);
    } else {
        return ( str.indexOf("@") >= 0 );
    }
    
}


/**
 * Validates phone number is in correct format and values are correct using regular
 * the defined expression.
 * Phone number validation expression parser used to parse and validate a given date.
 *
 * @param strValue to validate as proper phone number
 * @return true if the phone number format is valid; false, otherwise.
 */
ValidateUtils.validatePhoneNumber = function( strValue ) {

    // Validate objects have been defined.
    if (!ValidateUtils.validateObjects()) {
        return false;
    }

    return LocalizationUtils.phoneFormatRegExp.test(strValue);
    
}


/**
 * Validate street address does not contain invalid values.
  *
 * @param strValue to validate as a valid street address
 * @return true if the street address format is valid; false, otherwise.
 */
ValidateUtils.validateStreetAddress = function( strValue ) {

    // Validate objects have been defined.
    if (!ValidateUtils.validateObjects()) {
        return false;
    }

    return LocalizationUtils.streetAddressFormatRegExp.test(strValue);
    
}

/**
 * Validates postal code format.
 *
 * @param strValue to validate as a valid postal code
 * @return true if postal code format is valid; false, otherwise.
 */
ValidateUtils.validatePostalCode = function( strValue ) {

    // Validate objects have been defined.
    if (!ValidateUtils.validateObjects()) {
        return false;
    }

    return LocalizationUtils.postalCodeValidationRegExp.test(strValue);
    
}


/**
 * Determine if the incoming URL is in a valid format. Requires the prefix of http:// or similar.
 *
 * @param url to validate as a valid URL
 * @return true if the url format is valid; false, otherwise.
 */
ValidateUtils.isValidURL = function(url) {
    var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");

    if (!v.test(url)) {
        return false;
    } else{
        return true;
    }

}



