/**
* Script contains all functions for dealing with formatting. 
*/

/**
 * Creates a class called FormatUtils.
 */
var FormatUtils = {
    version : "1.0.0"
};

/**
 * 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
 */
FormatUtils.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.phoneFormatReplacementString) {
        alert("Required variable not defined. Variable: [LocalizationUtils.phoneFormatReplacementString]");
        return false;
    }

    return true;
}


/**
 * 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 phoneNumber to format
 * @return already formatted phone number
 */
FormatUtils.formatPhoneNumberWithLocale = function(phoneNumber) {

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

    if (!LocalizationUtils.phoneFormatRegExp.test(phoneNumber)) {
        return phoneNumber.replace(LocalizationUtils.phoneFormatRegExp, LocalizationUtils.phoneFormatReplacementString);
    }

    return phoneNumber;

}
