﻿//Begin - These are the client ids of the Hidden Localization control texboxes
var localizeStreetNameId = 'ctl00__column1CPH_shortForm_localize__streetNameTxt';
var localizeAptNumberId = 'ctl00__column1CPH_shortForm_localize__aptNumberTxt';
var localizeZipCodeId = 'ctl00__column1CPH_shortForm_localize__zipCodeTxt';
var localizeSubmitIbtnId = 'ctl00__column1CPH_shortForm_localize__submitIbtn_button';
//End - These are the client ids of the Hidden Localization control texboxes
var lableControls = ['_streetAddressForBuyflow', '_zipFiveForBuyflow', '_streetAddressForLocalization', '_zipFiveForLocalization'];
var lableControlsNewCustomer = ['_streetAddressForBuyflow', '_zipFiveForBuyflow'];
var lableControlsComcastCustomer = ['_streetAddressForLocalization', '_zipFiveForLocalization'];

var labelControlsLocalization = ['ctl00__column1CPH_shortForm__streetAddressForBuyflow', 'ctl00__column1CPH_shortForm__zipFiveForBuyflow', 'ctl00__column1CPH_shortForm__streetAddressForLocalization', 'ctl00__column1CPH_shortForm__zipFiveForLocalization'];
var labelControlsLocalizationComcastCustomer = ['ctl00__column1CPH_shortForm__streetAddressForLocalization', 'ctl00__column1CPH_shortForm__zipFiveForLocalization'];
var labelControlsLocalizationNewCustomer = ['ctl00__column1CPH_shortForm__streetAddressForBuyflow', 'ctl00__column1CPH_shortForm__zipFiveForBuyflow'];

function Submit() {

    //Read user values
    var address = Value(streetAddressForLocalizationId);
    var aptNbr = Value(aptNumberForLocalizationId);
    var city = Value(cityForLocalizationId);
    var state = ListValue(stateListForLocalizationId);
    var zip = Value(zipFiveForLocalizationId);

    //Populate Localization control
    Element(localizeStreetNameId).value = address;
    Element(localizeAptNumberId).value = aptNbr;
    Element(localizeZipCodeId).value = zip;


    var isValidStreetAddress = ValidateStreetAddressInLocalization();
    var isValidZipcode = ValidateZipCodeInLocalization();

    //if street address not valid as per localization validation, clear its value so that it fails page validation
    if (!isValidStreetAddress) {
        Element(streetAddressForLocalizationId).value = '';
        if (!Element(streetAddressForLocalizationId).disabled)
            Element(streetAddressForLocalizationId).focus();
    }

    //if Zip code not valid as per localization validation, clear its value so that it fails page validation
    if (!isValidZipcode) {
        Element(zipFiveForLocalizationId).value = '';
    }

    var isValid = Page_ClientValidate("ComcastCustomerRFValidation");
    if (!isValid) {
        document.getElementById("_errorDisplay").className = "ErrorDisplayPosition";
        if (FreeFormHTMLAllowedNewCustomer == 'False' && FreeFormHTMLAllowedComcastCustomer == 'False') {
            SetRequiredFieldStyle(labelControlsLocalization, "Localization");
        }
        else if (FreeFormHTMLAllowedNewCustomer == 'False') {
            SetRequiredFieldStyle(labelControlsLocalizationNewCustomer, "Localization");
        }
        else {
            SetRequiredFieldStyle(labelControlsLocalizationComcastCustomer, "Localization");
        }

        //if street address not valid as per localization validation, set it back so that user can see invalid value entered
        if (!isValidStreetAddress) {
            Element(streetAddressForLocalizationId).value = address;
        }

        //if Zip code not valid as per localization validation, clear its value so that it fails page validation
        if (!isValidZipcode) {
            Element(zipFiveForLocalizationId).value = zip;
        }
        return;
    }
    else {
        document.getElementById("_errorDisplay").className = "HideErrorDisplay";
        if (FreeFormHTMLAllowedNewCustomer == 'False' && FreeFormHTMLAllowedComcastCustomer == 'False') {
            SetRequiredFieldStyle(labelControlsLocalization, "Localization");
        }
        else if (FreeFormHTMLAllowedNewCustomer == 'False') {
            SetRequiredFieldStyle(labelControlsLocalizationNewCustomer, "Localization");
        }
        else {
            SetRequiredFieldStyle(labelControlsLocalizationComcastCustomer, "Localization");
        }
    }

    //Attache localization success and failure events
    AttachLocalizationEvents();


    if (parseInt(zip, 10) == 0) {
        Element(zipFiveForLocalizationId).value = '';
        if (!Page_ClientValidate("ComcastCustomerRFValidation")) {
            document.getElementById("_errorDisplay").className = "ErrorDisplayPosition";
            if (FreeFormHTMLAllowedNewCustomer == 'False' && FreeFormHTMLAllowedComcastCustomer == 'False') {
                SetRequiredFieldStyle(labelControlsLocalization, "Localization");
            }
            else if (FreeFormHTMLAllowedNewCustomer == 'False') {
                SetRequiredFieldStyle(labelControlsLocalizationNewCustomer, "Localization");
            }
            else {
                SetRequiredFieldStyle(labelControlsLocalizationComcastCustomer, "Localization");
            }
            return;
        }
    }

    //Trigger click event.
    Element(localizeSubmitIbtnId).disabled = false;
    Element(localizeSubmitIbtnId).click();

}

function ValidateStreetAddressInLocalization() {
    return localize.ValidateStreetAddress(Element(localizeStreetNameId));
}

function ValidateZipCodeInLocalization() {
    return localize.ValidateZipCode(Element(zipFiveForLocalizationId));
}

function Value(controlId) {
    var input = '';
    var textBox = document.getElementById(controlId)
    if (textBox != null) {
        input = textBox.value;
    }
    return input;
}

function ListValue(controlId) {
    var input = '';
    var list = document.getElementById(controlId)
    if (list != null && list.selectedIndex >= 0) {
        input = list.options[list.selectedIndex].value;
    }
    return input;
}

function Element(controlId) {
    var ctrl = document.getElementById(controlId)
    return ctrl;
}

function LocalizeSuccess() {
    var localizedResponseHidElement = $(localizedResponseHiddenId);
    if (localizedResponseHidElement != null)
        localizedResponseHidElement.value = "";
    SetCookie("localizedResponse", false, "0", false, false);
    Element(hiddenSubmitBtnId).click();
}

function LocalizeFailure() {
    var errorMessagesElement = document.getElementById("_errorMessages");
    var errorHeadingElement = document.getElementById("_errorHeading");
    var errorSubHeadingElement = document.getElementById("_errorSubHeading");
    var errorDisplayElement = document.getElementById("_errorDisplay");
    var localizedResponseHidElement = $(localizedResponseHiddenId);

    if (errorDisplayElement != null && errorMessagesElement != null && errorMessagesElement != 'undefined' && errorHeadingElement != null && errorSubHeadingElement != null) {
        errorHeadingElement.className = "HideErrorDisplay";
        errorSubHeadingElement.className = "HideErrorDisplay";
        errorDisplayElement.className = "ErrorDisplayPosition";

        if (localize.LocalizedResponse == localize.LocalizeResponseType.notlocalized)
            errorMessagesElement.innerHTML = notLocalizedErrorMessage;
        else if (localize.LocalizedResponse == localize.LocalizeResponseType.notserviceable) {
            localize.RedirectUrl = "";
            //Populate values from localization control to URL
            notServiceableErrorMessage = notServiceableErrorMessage.replace('$address', Element(localizeStreetNameId).value);
            notServiceableErrorMessage = notServiceableErrorMessage.replace('$zip', Element(localizeZipCodeId).value);
            notServiceableErrorMessage = notServiceableErrorMessage.replace('$apt', Element(localizeAptNumberId).value);
            errorMessagesElement.innerHTML = notServiceableErrorMessage;
        }
    }
}

function AttachLocalizationEvents() {
    localize.ZipCodeElement.disabled = "true";
    localize.StreetElement.disabled = "true";
    localize.AptElement.disabled = "true";
    localize.ResetLocMod();
    localize.Localize.AddHandler(LocalizeSuccess);
    localize.LocalizeFailed.AddHandler(LocalizeFailure);
}

function NewCustomerSubmit() {
    var isValid = Page_ClientValidate("NewCustomerRFValidation");
    if (!isValid) {
        document.getElementById("_errorDisplay").className = "ErrorDisplayPosition";
    }
    else {
        document.getElementById("_errorDisplay").className = "HideErrorDisplay";
    }

    if (FreeFormHTMLAllowedNewCustomer == 'False' && FreeFormHTMLAllowedComcastCustomer == 'False') {
        SetRequiredFieldStyle(lableControls, "Buyflow");
    }
    else if (FreeFormHTMLAllowedNewCustomer == 'False') {
        SetRequiredFieldStyle(lableControlsNewCustomer, "Buyflow");
    }
    else {
        SetRequiredFieldStyle(lableControlsComcastCustomer, "Buyflow");
    }

    return isValid;
}

function SetRequiredFieldStyle(controlIds, source) {
    for (i in controlIds) {
        if (typeof (controlIds[i]) == "string") {
            if (document.getElementById(controlIds[i]) != null) {
                    document.getElementById(controlIds[i]).className = "formRow";
                    if (txtBoxIds[i].indexOf('zip') > 0)
                        document.getElementById(txtBoxIds[i]).className = "textBox smallText";
                    else
                        document.getElementById(txtBoxIds[i]).className = "textBox";                    
                }

            if (!Page_Validators[i].IsValid && !RequiredFieldValidatorEvaluateIsValid(Page_Validators[i]) && Page_Validators[i].controltovalidate.indexOf(source) > 0) {
                if (document.getElementById(controlIds[i]) != null) {
                    document.getElementById(controlIds[i]).className = "ErrorHighlight";
                    if (txtBoxIds[i].indexOf('zip') > 0)
                        document.getElementById(txtBoxIds[i]).className = "textBox smallText ErrorHighlightText";
                    else
                        document.getElementById(txtBoxIds[i]).className = "textBox ErrorHighlightText";
                }
            }
        }
    }
}


