﻿var ContactInformationModelPropertyValidator = (function() {
    function fnContactInformationModelPropertyValidatorConstructor(oNewModel) {
        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*                 Private Variables                */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        var oModel;
        var hErrorLists;

        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*            Class Level Private Methods           */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        function processValidationError(aErrors, bValid, sMessage) {
            if (!bValid) {
                aErrors.push(sMessage);
            }
        }

        function validateEmailAddress(sValue) {
            var oRegularExpression = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            return oRegularExpression.test(sValue);
        }

        function validateRequiredValue(sValue) {
            try {
                sValue = sValue.replace(/^\s|\s$/, "");
                return sValue.length > 0;
            }
            catch (eException) {
                return false;
            }
        }

        function validateMinimumValueLength(sValue, iLength) {
            try {
                sValue = sValue.replace(/^\s|\s$/, "");
                return sValue.length >= iLength;
            }
            catch (eException) {
                return false;
            }
        }

        function validateValueLength(sValue, iLength) {
            try {
                sValue = sValue.replace(/^\s|\s$/, "");
                return sValue.length == iLength;
            }
            catch (eException) {
                return false;
            }
        }

        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*          Class Level Priveleged Methods          */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        this.getAllErrors = function() {
            return hErrorLists.clone();
        }

        this.getErrorsByName = function(sName) {
            try {
                return hErrorLists.get(sName);
            }
            catch (eException) {

            }
        }

        this.initializeErrorList = function() {
            hErrorLists = $H();
            hErrorLists.set("Email Address", new Array());
            hErrorLists.set("First Name", new Array());
            hErrorLists.set("Last Name", new Array());
            hErrorLists.set("Company", new Array());
            hErrorLists.set("Country", new Array());
            hErrorLists.set("State", new Array());
            hErrorLists.set("Postal Code", new Array());
            hErrorLists.set("Province", new Array());
            hErrorLists.set("Phone Number", new Array());
        }

        this.isValid = function() {
            var aKeys = hErrorLists.keys();
            for (var iKey = 0; iKey < aKeys.length; iKey++) {
                this.validatePropertyByName(aKeys[iKey]);
            }

            var bValid = true;
            var aErrorLists = hErrorLists.values();
            var aErrors;
            for (var iErrorList = 0; iErrorList < aErrorLists.length; iErrorList++) {
                aErrors = aErrorLists[iErrorList];
                if (aErrors.length > 0) {
                    bValid = false;
                    break;
                }
            }

            return bValid;
        }

        this.validatePropertyByName = function(sName) {
            var sRequiredErrorMessage = "Required";
            var aErrors = new Array();

            switch (sName) {
                case "First Name":
                    processValidationError(aErrors, validateRequiredValue(oModel.getFirstName()), sRequiredErrorMessage);
                    break;
                case "Last Name":
                    processValidationError(aErrors, validateRequiredValue(oModel.getLastName()), sRequiredErrorMessage);
                    break;
                case "Company":
                    processValidationError(aErrors, validateRequiredValue(oModel.getCompany()), sRequiredErrorMessage);
                    break;
                case "Email Address":
                    var sEmailAddress = oModel.getEmailAddress();
                    processValidationError(aErrors, validateRequiredValue(sEmailAddress), sRequiredErrorMessage);
                    processValidationError(aErrors, validateEmailAddress(sEmailAddress), "Invalid Email Format");
                    break;
                case "Phone Number":
                    var phoneNumber = oModel.getPhoneNumber();
                    var country = oModel.getCountry();
                    
                    if(((!country || country.length <= 0 || country.value == "Please Select...") && (!phoneNumber || phoneNumber.length <= 0)) || (phoneNumber.replace(/\s/g,"") == ""))
                    {
                        processValidationError(aErrors, false, sRequiredErrorMessage);
                        break;
                    }
                    
                    if(country)
                    {
                        switch(country)
                        {
                            case "United States":
                                if(!/^\d{3}-\d{3}-\d{4}$/.test(phoneNumber))
                                {
                                    processValidationError(aErrors, false, "Invalid Phone Length");
                                    break;
                                }
                            case "Afghanistan":
                            case "American Samoa":
                            case "Antarctica":
                            case "Australia":
                            case "Bangladesh":
                            case "Bhutan":
                            case "Brunei Darussalam":
                            case "Cambodia":
                            case "China":
                            case "Christmas Island":
                            case "Cocos (Keeling) Islands":
                            case "Cook Islands":
                            case "Fiji":
                            case "French Polynesia":
                            case "Guam":
                            case "Heard Island and McDonald Is.":
                            case "Hong Kong":
                            case "India":
                            case "Indonesia":
                            case "Japan":
                            case "Kazakhstan":
                            case "Kiribati":
                            case "Korea Democratic People’s Republic":
                            case "Korea Republic of":
                            case "Kyrgyzstan":
                            case "Lao People’s Democratic Republic":
                            case "Macau":
                            case "Malaysia":
                            case "Maldives":
                            case "Marshall Islands":
                            case "Micronesia Federal State of":
                            case "Mongolia":
                            case "Myanmar":
                            case "Nauru":
                            case "Nepal":
                            case "New Caledonia":
                            case "New Zealand":
                            case "Niue":
                            case "Norfolk Island":
                            case "Northern Mariana Islands":
                            case "Pakistan":
                            case "Palau":
                            case "Papua New Guinea":
                            case "Philippines":
                            case "Pitcairn Island":
                                //case "Samoa":
                            case "Singapore":
                            case "Solomon Islands":
                            case "South Georgia":
                            case "Sri Lanka":
                                //case "Tahiti":
                            case "Taiwan":
                            case "Tajikistan":
                            case "Thailand":
                                //case "Timor-Leste":
                            case "Tokelau":
                            case "Tonga":
                            case "Turkmenistan":
                            case "Tuvalu":
                            case "Uzbekistan":
                            case "Vanuatu":
                            case "Vietnam":
                            case "Wallis and Futuna Islands":
                                if(phoneNumber.length <= 5)
                                {
                                     processValidationError(aErrors, false,  "Invalid Phone Length");
                                     break;
                                }
                            default:
                                if(phoneNumber.replace(/\s/g,"") == "")
                                {
                                    processValidationError(aErrors, false, sRequiredErrorMessage);
                                    break;
                                }    
                        }
                    } 
                    break;
                case "Country":
                    processValidationError(aErrors, validateRequiredValue(oModel.getCountry()), sRequiredErrorMessage);
                    this.validatePropertyByName("State");
                    this.validatePropertyByName("Postal Code");
                    this.validatePropertyByName("Province");
                    break;
                case "State":
                    if (oModel.getCountry() == "United States")
                        processValidationError(aErrors, validateRequiredValue(oModel.getState()), sRequiredErrorMessage);
                    break;
                case "Postal Code":
                    if (oModel.getCountry() == "United States") {
                        var sPostalCode = oModel.getPostalCode();
                        processValidationError(aErrors, validateRequiredValue(sPostalCode), sRequiredErrorMessage);
                        processValidationError(aErrors, validateMinimumValueLength(sPostalCode, 5), "Invalid Postal Code Length");
                    }
                    break;
                case "Province":
                    if (oModel.getCountry() == "Canada")
                        processValidationError(aErrors, validateRequiredValue(oModel.getProvince()), sRequiredErrorMessage);
                    break;
            }

            hErrorLists.set(sName, aErrors);
        }

        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*                 Initialize Class                 */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        oModel = oNewModel;
        this.initializeErrorList();
    }

    return fnContactInformationModelPropertyValidatorConstructor;
})();