﻿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 validateACFormat(sValue) {
            if (!sValue || sValue == '')
                return true;
            var oRegularExpression = /^[0-9]{0,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("Australia State", new Array());
            hErrorLists.set("County", new Array());
            hErrorLists.set("India State", new Array());
            hErrorLists.set("STD Code", new Array());
            hErrorLists.set("Postal Code", new Array());
            hErrorLists.set("Area 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 (!phoneNumber)
                    {
                        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 "Australia State":
                    if (oModel.getCountry() == "Australia")
                        processValidationError(aErrors, validateRequiredValue(oModel.getAusState()), sRequiredErrorMessage);
                    break;
                case "County":
                    if (oModel.getCountry() == "United Kingdom")
                        processValidationError(aErrors, validateRequiredValue(oModel.getCounty()), sRequiredErrorMessage);
                    break;
                case "India State":
                    if (oModel.getCountry() == "India")
                        processValidationError(aErrors, validateRequiredValue(oModel.getIndiaState()), sRequiredErrorMessage);                                              
                    break;
                case "STD Code":
                    if (oModel.getCountry() == "India")                       
                        processValidationError(aErrors, validateACFormat(oModel.getSTDCode()), "Invalid AC Format");  
                    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 "Area Code":                
                    if (showArea(oModel.getCountry())) {
                        var sAreaCode = oModel.getPostalCode();
                        processValidationError(aErrors, validateRequiredValue(sAreaCode), sRequiredErrorMessage);
                        processValidationError(aErrors, validateMinimumValueLength(sAreaCode, 5), "Invalid Area Code Length");
                    }
                    break;
                case "Province":
                    if (oModel.getCountry() == "Canada")
                        processValidationError(aErrors, validateRequiredValue(oModel.getProvince()), sRequiredErrorMessage);
                    break;
            }

            hErrorLists.set(sName, aErrors);
       
        }
        
        function showArea(_country)
        {
         var flag = false;
         switch (sCountry) {
                case "Afghanistan" :
                case "American Samoa" :      
                case "Antarctica" :
                case "Bangladesh" :
                case "Bhutan" :
                case "Brunei Darussalam" :
                case "Cambodia" :
                case "China" :
                case "Christmas Island" :
                case "Cocos (Keeling) Islands" :                
                case "Cook Islands" :
                case "Fiji" :
                case "Guam" :
                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 "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 "Solomon Islands" :                
                case "South Georgia" :
                case "Sri Lanka" :
                case "Taiwan" :
                case "Tajikistan" :
                case "Thailand" :
                case "Tokelau" :
                case "Tonga" :
                case "Turkmenistan" :
                case "Tuvalu" :
                case "Uzbekistan" :                
                case "Vanuatu" :
                case "Vietnam" :
                case "Wallis and Futuna Islands" :
                    flag = false;
                    break;
               }
           return flag;
        }

        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*                 Initialize Class                 */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        oModel = oNewModel;
        this.initializeErrorList();
    }

    return fnContactInformationModelPropertyValidatorConstructor;
})();