﻿var ContactInformationFormViewHelper = (function() {
    function fnContactInformationFormViewHelperConstructor(oNewView) {
        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*                 Private Variables                */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        var oView = oNewView;
        var bValidated = false;

        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*                  Event Handlers                  */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        function initializeListBoxes_onSuccess(oEvent) {
            var oLocationData = eval("(" + oEvent.responseText + ")");
            oView.addListBoxOptions($("lstCountry"), oLocationData.aCountries, "Please Select...");
            oView.addListBoxOptions($("lstProvince"), oLocationData.aProvinces, "Please Select...");
            oView.addListBoxOptions($("lstState"), oLocationData.aStates, "Pick State");
        }

        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*            Class Level Private Methods           */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        function getDisplayValue(sValue) {
            return sValue ? sValue : "";
        }

        function getFormElementByName(sName) {
            var sName = sName.replace(/\s/, "");
            var oFormElement = $("txt" + sName);
            return oFormElement ? oFormElement : $("lst" + sName);
        }

        function showErrorMessagesByField(oFormElement, aErrors) {
            try {
                if (aErrors.length == 0) {
                    oView.hideErrorMessage(oFormElement);
                }
                else {
                    var sError = aErrors.first();
                    switch (sError) {
                        case "Required":
                            oView.setAndShowErrorMessage(oFormElement, "&nbsp;- required!");
                            break;
                        case "Invalid Phone Length":
                            if($("lstCountry").value =="United States")
                                oView.setAndShowErrorMessage(oFormElement, "&nbsp;- U.S. requires 10 digits!");
                            else oView.setAndShowErrorMessage(oFormElement, "&nbsp;- Must enter 6 numbers or more for APAC countries!");
                            break;
                        case "Invalid Postal Code Length":
                            oView.setAndShowErrorMessage(oFormElement, "&nbsp;- U.S. requires at least 5 digits!");
                            break;
                        case "Invalid Email Format":
                            oView.setAndShowErrorMessage(oFormElement, "&nbsp;- invalid format!");
                            break;
                    }
                }
            }
            catch (eException) {
                oView.hideErrorMessage(oFormElement);
            }
        }

        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*          Class Level Priveleged Methods          */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        this.initializeListBoxes = function() {
            //new Ajax.Request("http://tul-wdsrv-01/register/GetLocationData.aspx", {asynchronous: false, method: "get", onSuccess: initializeListBoxes_onSuccess.bind(this) });
            //new Ajax.Request("http://wwwstage1.tul.solarwinds.net/register/GetLocationData.aspx", { asynchronous: false, method: "get", onSuccess: initializeListBoxes_onSuccess.bind(this) });
            //new Ajax.Request("http://wwwstage2.tul.solarwinds.net/register/GetLocationData.aspx", {asynchronous: false, method: "get", onSuccess: initializeListBoxes_onSuccess.bind(this) });
            //new Ajax.Request("http://www.solarwinds.com/register/GetLocationData.aspx", { asynchronous: false, method: "get", onSuccess: initializeListBoxes_onSuccess.bind(this) });
            new Ajax.Request("/register/GetLocationData.aspx", { asynchronous: false, method: "get", onSuccess: initializeListBoxes_onSuccess.bind(this) });
        }

        this.loadValues = function(oValues) {
            var sEmailAddress = oValues.sEmailAddress;
            if (sEmailAddress && sEmailAddress.length > 0) {
                oView.setValue($("txtEmailAddress"), getDisplayValue(sEmailAddress));
            }
        
            oView.setValue($("txtFirstName"), getDisplayValue(oValues.sFirstName));
            oView.setValue($("txtLastName"), getDisplayValue(oValues.sLastName));
            oView.setValue($("txtCompany"), getDisplayValue(oValues.sCompany));

            var sCountry = getDisplayValue(oValues.sCountry);
            oView.setValue($("lstCountry"), sCountry);
            if (sCountry == "United States") {
                oView.setValue($("lstState"), getDisplayValue(oValues.sState));
                oView.setValue($("txtPostalCode"), getDisplayValue(oValues.sPostalCode));
            }
            else {
                oView.setValue($("lstProvince"), getDisplayValue(oValues.sProvince));
            }
            this.showLocationFields(sCountry);

            oView.setValue($("txtPhoneNumber"), getDisplayValue(oValues.sPhoneNumber));
            if (sCountry.length > 0)
                oView.enablePhoneNumber();
        }

        this.showErrorMessages = function(hErrorLists) {
            bValidated = true;
            this.updateErrorMessages(hErrorLists);
        }

        this.showLocationFields = function(sCountry) {
            switch (sCountry) {
                case "Canada":
                    oView.hideFormElement($("lstState"));
                    oView.hideFormElement($("txtPostalCode"));
                    oView.showFormElement($("lstProvince"));
                    break;
                case "United States":
                    oView.hideFormElement($("lstProvince"));
                    oView.showFormElement($("lstState"));
                    oView.showFormElement($("txtPostalCode"));
                    break;
                default:
                    oView.hideFormElement($("lstState"));
                    oView.hideFormElement($("txtPostalCode"));
                    oView.hideFormElement($("lstProvince"));
            }
        }
        
        this.togglePhoneNumber = function(sCountry) {
            if (sCountry.length == 0)
                oView.disablePhoneNumber();
            else
                oView.enablePhoneNumber();
        }

        this.updateErrorMessages = function(hErrorLists) {
            if (bValidated) {
                var aKeys = hErrorLists.keys();
                var aErrors;
                var sErrorListName;
                for (var iKey = 0; iKey < aKeys.length; iKey++) {
                    sErrorListName = aKeys[iKey];
                    showErrorMessagesByField(getFormElementByName(sErrorListName), hErrorLists.get(sErrorListName));
                }
            }
        }

        /****************************************************/
        /*                                                  */
        /*                                                  */
        /*                 Initialize Class                 */
        /*                                                  */
        /*                                                  */
        /****************************************************/

        this.initializeListBoxes();
    }

    return fnContactInformationFormViewHelperConstructor;
})();