﻿function Localize(Id, AutoLocalize, AptElement, RedirectUrl, ErrorDiv, InputClass, ErrorInputClass,
 LocModMessageDiv, LocModFormDiv, VisitorId, ErrorMessage, RefererUrl, StreetElement, ZipCodeElemet,
 ZipCodeErrorMessage, LocalizePageName, LocalizeSuccessPageName, LocalizeLocationPickerPageName,
 LocationPickerType, LocalizeMarketPickerPageName, MarketPickerType, DisplayType, ShopSource, IsPermanentChk, LocalizeMainDiv) {
    this.Id = Id;
    this.AutoLocalize = AutoLocalize;
    this.Validate = new Event();
    this.Submit = new Event();
    this.Localize = new Event();
    this.LocalizeFailed = new Event();
    this.AptElement = $(AptElement);
    this.RedirectUrl = RedirectUrl;
    this.Overlay = null;
    this.ErrorDiv = $(ErrorDiv);
    this.LocationId = "";
    this.MarketId = "";
    this.FranchiseMapId = "";
    this.PreviousLocalizetype = "";
    this.ProgressBar = new Progress();
    this.ProgressBar.Id = "localizeProgressBar";
    this.Mask = new Mask();
    this.Mask.Id = "localizeMask";
    this.ErrorInputClass = ErrorInputClass;
    this.InputClass = InputClass;
    this.LocModMessageDiv = $(LocModMessageDiv);
    this.LocModFormDiv = $(LocModFormDiv);
    this.VisitorId = VisitorId;
    this.LocalizePageName = LocalizePageName;
    this.LocalizeSuccessPageName = LocalizeSuccessPageName;
    this.LocalizeLocationPickerPageName = LocalizeLocationPickerPageName;
    this.LocationPickerType = LocationPickerType;
    this.LocalizeMarketPickerPageName = LocalizeMarketPickerPageName;
    this.MarketPickerType = MarketPickerType;
    this.ShopSource = ShopSource;
    if (DisplayType == "PageView")
        this.TrackOnLoad();
    this.LocalizeResponseType = {
        localized: "localized",
        multiplemarketsfound: "multiplemarketsfound",
        multiplefranchisesfound: "multiplefranchisesfound",
        multiplelocationsfound: "multiplelocationsfound",
        multiplemarketsfound: "multiplemarketsfound",
        notlocalized: "notlocalized",
        notserviceable: "notserviceable"
    }
    this.StreetAddress = "";
    this.AptNumber = "";
    this.City = "";
    this.ZipCode = "";
    this.ErrorMessage = ErrorMessage;
    this.RefererUrl = RefererUrl;
    this.StreetElement = $(StreetElement);
    this.ZipCodeElement = $(ZipCodeElemet);
    this.ZipCodeErrorMessage = ZipCodeErrorMessage;

    if (this.AutoLocalize == "1")
        addEvent(window, "load", function() { Auto(Id) });
    else {
        if (DisplayType == "PageView")
            addEvent(window, "load", function() { Localize.SetFocusOnStreet(Id) });
    }

    this.PermanentLocalization = "0";
    if (IsPermanentChk != null && IsPermanentChk != "") {
        this.IsPermanentChk = $(IsPermanentChk);
        if (this.IsPermanentChk.checked)
            this.PermanentLocalization = "1";
    }
    this.LocalizeMainDiv = $(LocalizeMainDiv);
    return this;
}
Localize.SetFocusOnStreet = function(id) {
    var ctl = eval(id);
    if (ctl != null) {
        if (ctl.LocModFormDiv == null || ctl.LocModFormDiv.className.indexOf("visible") > -1)
            ctl.StreetElement.focus();
    }
}

function LocalizationRequest() {
    this.StreetAddress = "";
    this.Apt = "";
    this.Zip = "";
    this.LocationId = "";
    this.MarketId = "";
    this.FranchiseMapId = "";
    this.Id;
    this.RememberMyInformation = "0";
}
function LocalizationResponse() {
    this.ErrorMessage = "";
    this.RedirectUrl = "";
    this.HTML = "";
    this.ResponseTypeString = "";
    this.OverlayId = "";
    this.Scripts = new Array();
    this.ScriptLibraries = new Array();
    this.StyleSheets = new Array();
    this.StreetAddress = "";
    this.AptNumber = "";
    this.City = "";
    this.ZipCode = "";
    this.RememberMyInformation = "0";
}
Localize.prototype.addError = function(message) {
    if (message != "") {
        this.ErrorDiv.innerHTML = message;
        this.ErrorDiv.className = "errorSectionVisible";
        TrackInteraction('{DCF58750-705D-478d-9B1C-451BCD67B8DC}'); //Validation Error
    }
    return;
}
Localize.prototype.SetPermanentLocalizeCookie = function(isPermanentChk) {
    if (this.Id == undefined || this.Id == null || null == this.StreetElement || null == this.ZipCodeElement)
        return;
    this.PermanentLocalization = "0";
    if (isPermanentChk.checked) {
        this.PermanentLocalization = "1";
    }
}
Localize.prototype.clearErrors = function() {
    this.ErrorDiv.innerHTML = "";
    this.ErrorDiv.className = "errorSection";
    return;
}
Localize.prototype.OnLocalize = function() {
    this.Localize.Execute(this);
    if (this.RedirectUrl != "")
        location.href = this.RedirectUrl;
}
Localize.prototype.OnLocalizeFailed = function() {
    this.LocalizeFailed.Execute();
    if (this.RedirectUrl != "")
        location.href = this.RedirectUrl;
}
function Auto(Id) {
    var c = eval(Id);
    if (null != c)
        c.OnSubmit();
}
Localize.prototype.OnValidate = function() {
    if (this.Id == '' || this.Id == undefined)
        return;
    var ValidationStatus = true;
    var StreetElement = this.StreetElement;
    var ZipCodeElement = this.ZipCodeElement;

    StreetElement.className = this.InputClass;
    ZipCodeElement.className = this.InputClass;
    this.clearErrors();
    if (!this.ValidateStreetAddress(StreetElement)) {
        ValidationStatus = false;
        StreetElement.className = this.ErrorInputClass;
        this.addError(this.ErrorMessage);
        if (!StreetElement.disabled)
            StreetElement.focus();
    }
    else if (!ZipCodeElement.disabled)
        ZipCodeElement.focus();

    if (!this.ValidateZipCode(ZipCodeElement)) {
        ValidationStatus = false;
        ZipCodeElement.className = this.ErrorInputClass;
    }

    this.Validate.Execute();
    return ValidationStatus;
}
Localize.prototype.ValidateStreetAddress = function(element) {
    var valid = true;
    if (element.value == null || element.value == undefined || element.value == '' ||
        element.value.length == 0)
        valid = false;
    else if (element.value.trim().length < 3)
        valid = false;
    else if (element.value.trim().indexOf(' ') < 0)
        valid = false;
    else if (element.value.indexOf('%') > -1)
        valid = false;

    return valid;
}
Localize.prototype.ValidateZipCode = function(element) {
    var zipRegExpression = /^\d{5}$/;
    var ValidationStatus = true;
    if (element.value == null || element.value.trim() == '') {
        ValidationStatus = false;
        this.addError(this.ErrorMessage);
    }
    else if (!element.value.match(zipRegExpression) || element.value == "00000") {
        ValidationStatus = false;
        this.clearErrors();
        this.addError(this.ZipCodeErrorMessage);
        if (!element.disabled)
            element.focus();
    }
    return ValidationStatus;
}
Localize.prototype.OnSubmit = function() {
    if (!this.OnValidate())
        return false;
    this.DisableFields(true);

    var localizeControl = eval(this.Id);
    this.Mask.Show();
    this.ProgressBar.Show();

    try {
        var request = new LocalizationRequest();
        request.StreetAddress = this.StreetElement.value;
        request.Apt = this.AptElement.value;
        request.Zip = this.ZipCodeElement.value;
        request.Id = this.Id;
        var controlId = this.Id;
        if (this.PermanentLocalization == "1")
            request.RememberMyInformation = "1";
        var action = new RemoteMethod('Localize', 'LocalizeByAddress', request);
        action.LocalAction = function(args) {
            Localize.LocalizeComplete(args, controlId);
        }
        action.Invoke();
    }
    catch (e) { }
    this.Submit.Execute();
    return false;
}
Localize.LocalizeComplete = function(response, Id) {
    if (response != null) {
        var control = eval(Id);
        control.clearErrors();
        if (response.ResponseTypeString != null || response.ResponseTypeString != "") {
            if (response.ResponseTypeString != control.LocalizeResponseType.localized) {
                control.ProgressBar.Hide();
                control.Mask.Hide();
            }
            switch (response.ResponseTypeString) {
                case control.LocalizeResponseType.localized:
                    {
                        control.TrackOnSuccess();
                        control.StreetAddress = response.StreetAddress;
                        control.AptNumber = response.AptNumber;
                        control.City = response.City;
                        control.ZipCode = response.ZipCode;
                        if (control.RefererUrl != undefined && control.RefererUrl != "")
                            control.RedirectUrl = control.RefererUrl;
                        else if (response.RedirectUrl != "")
                            control.RedirectUrl = response.RedirectUrl;
                        control.OnLocalize();
                    }
                    break;
                case control.LocalizeResponseType.multiplefranchisesfound:
                    control.ShowPicker(response);
                    control.TrackOnPicker(control.LocalizeMarketPickerPageName, control.MarketPickerType);
                    control.PreviousLocalizetype = control.LocalizeResponseType.multiplefranchisesfound;
                    break;
                case control.LocalizeResponseType.multiplelocationsfound:
                    control.ShowPicker(response);
                    control.TrackOnPicker(control.LocalizeLocationPickerPageName, control.LocationPickerType);
                    control.PreviousLocalizetype = control.LocalizeResponseType.multiplelocationsfound;
                    break;
                case control.LocalizeResponseType.multiplemarketsfound:
                    control.ShowPicker(response);
                    control.TrackOnPicker(control.LocalizeMarketPickerPageName, control.MarketPickerType);
                    control.PreviousLocalizetype = control.LocalizeResponseType.multiplemarketsfound;
                    break;
                case control.LocalizeResponseType.notlocalized:
                    control.addError(response.ErrorMessage);
                    control.DisableFields(false);
                    control.StreetElement.select();
                    break;
                case control.LocalizeResponseType.notserviceable:
                    control.RedirectUrl = response.RedirectUrl;
                    control.OnLocalizeFailed();
                    break;
            }
        }
    }
}
Localize.prototype.DisableFields = function(disable) {
    var container = this.LocalizeMainDiv;
    if (container != null) {
        var fields = container.getElementsByTagName("input");
        for (var i = 0; i < fields.length; i++)
            fields[i].disabled = disable;
    }
}
Localize.prototype.ShowPicker = function(response) {
    if (response != null && response.HTML != '')
        EmbedHtml(response);
}
Localize.prototype.AddHandlers = function(picker) {
    var id = this.Id;
    this.Overlay = picker;

    if (!(picker == undefined || picker == null)) {
        picker.Confirm.AddHandler(function() { Localize.GetPickerSelection(id); });
        picker.Validate.AddHandler(function() { Localize.ValidatePicker(id); });
        picker.Cancel.AddHandler(function() { Localize.PickerCancel(picker, id, true); });
        GetOverallBase(picker).Hide.AddHandler(function() { Localize.PickerCancel(picker, id, false); });
    }
}
Localize.PickerCancel = function(picker, id, userAction) {
    var ctl = eval(id);
    if (userAction) {
        if (GetOverallBase(picker).Id == "locationPicker") {
            TrackInteraction('{DB63C588-B8D3-4b57-86B5-BD31E3818EFD}');
        }
        else if (GetOverallBase(picker).Id == "marketPicker") {
            TrackInteraction('{41051E38-73BB-4626-84F2-7926A60EDE55}');
        }
        else if (GetOverallBase(picker).Id == "franchisePicker") {
            TrackInteraction('{A23391C4-A9CE-4908-B5F3-98E53C1734E0}');
        }
    }
    GetOverallBase(picker).RemoveFromDOM();
    ctl.DisableFields(false);
    ctl.StreetElement.select();
}
Localize.ValidatePicker = function(Id) {
    var validationStatus = true;
    var localizeControl = eval(Id);
    var picker = eval(localizeControl.Overlay);
    if (picker.ReturnValue == "" && picker.PickerErrMsg != null) {
        picker.ShowValidationMessage();
        validationStatus = false;
    }
    return validationStatus;
}
Localize.GetPickerSelection = function(Id) {
    var localizeControl = eval(Id);
    var picker = eval(localizeControl.Overlay);
    if (localizeControl.PreviousLocalizetype == localizeControl.LocalizeResponseType.multiplelocationsfound) {
        localizeControl.LocationId = picker.ReturnValue;
    }
    else if (localizeControl.PreviousLocalizetype == localizeControl.LocalizeResponseType.multiplemarketsfound) {
        localizeControl.MarketId = picker.ReturnValue;
    }
    else if (localizeControl.PreviousLocalizetype == localizeControl.LocalizeResponseType.multiplefranchisesfound) {
        localizeControl.FranchiseMapId = picker.ReturnValue;
    }
    localizeControl.LocalizeByFullAddress();
    GetOverallBase(picker).RemoveFromDOM();
}
Localize.prototype.LocalizeByFullAddress = function() {
    var localizeControl = eval(this.Id);
    var request = new LocalizationRequest();
    var action;
    request.StreetAddress = this.StreetElement.value.trim();
    request.Apt = this.AptElement.value.trim();
    request.Zip = this.ZipCodeElement.value.trim();

    this.Mask.Show();
    this.ProgressBar.Show();
    try {

        var controlId = this.Id;
        if (this.PreviousLocalizetype == this.LocalizeResponseType.multiplelocationsfound) {
            action = new RemoteMethod('Localize', 'LocalizeByLocationId', request);
            request.LocationId = this.LocationId;
        }
        else if (this.PreviousLocalizetype == this.LocalizeResponseType.multiplemarketsfound) {
            action = new RemoteMethod('Localize', 'LocalizeByMarketId', request);
            request.MarketId = this.MarketId;
        }
        else if (this.PreviousLocalizetype == this.LocalizeResponseType.multiplefranchisesfound) {
            action = new RemoteMethod('Localize', 'LocalizeByFranchiseMapId', request);
            request.FranchiseMapId = this.FranchiseMapId;
        }

        if (this.PermanentLocalization == "1")
            request.RememberMyInformation = "1";
        action.LocalAction = function(args) {
            Localize.LocalizeComplete(args, controlId);
        }
        action.Invoke();
    }
    catch (e) { }
}

Localize.prototype.InitializeFormView = function() {
    if (!this.StreetElement.disabled)
        this.StreetElement.focus();
    this.StreetElement.value = "";
    this.AptElement.value = "";
    this.ZipCodeElement.value = "";
}
Localize.prototype.ResetLocMod = function() {
    if ((this.LocModMessageDiv != null || this.LocModMessageDiv != undefined) && (!this.StreetElement.disabled))
        this.LocModMessageDiv.style.display = 'none';
    if ((this.LocModFormDiv != null || this.LocModFormDiv != undefined) && (!this.StreetElement.disabled)) {
        this.LocModFormDiv.style.display = 'block';
        this.InitializeFormView();
    }
}
Localize.prototype.SetDefaultButton = function(e) {
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    if (KeyID == 13) {
        this.OnSubmit();
    }
}

Localize.prototype.SetOmnitureVariables = function() {
    s.eVar7 = "shop";
    s.prop17 = "Comcast - Home";
    if (this.ShopSource != "")
        s.prop24 = this.ShopSource;
    s.prop31 = "com";
    s.eVar32 = "localization";
    s.prop32 = "shop";
    s.eVar33 = s.prop33 = "comcast.com";
    s.eVar34 = s.prop34 = "localize";
    s.eVar35 = s.prop35 = "localization-home";
    s.eVar36 = s.prop36 = "localization";
    s.prop37 = "com";
    s.eVar50 = s.prop50 = this.VisitorId;
}

Localize.prototype.TrackOnLoad = function() {
    TrackInteraction('{81F4D15A-B4AE-4005-BF54-57C7EB3E52C2}');
    s.pageName = this.LocalizePageName;
    s.events = "event11";
    this.SetOmnitureVariables();
    s.t();
}

Localize.prototype.TrackOnSuccess = function() {
    s.pageName = this.LocalizeSuccessPageName;
    s.events = "event1,event11";
    this.SetOmnitureVariables();
    s.t();
}

Localize.prototype.TrackOnPicker = function(PickerPageName, PickerType) {
    s.pageName = PickerPageName;
    s.prop35 = s.eVar35 = PickerType;
    s.events = "event11";
    s.prop31 = "com";
    s.prop32 = s.eVar32 = "shop";
    s.prop33 = s.eVar33 = "comcast.com";
    s.prop34 = s.eVar34 = "localization";
    s.prop36 = s.eVar36 = "localization";
    s.prop37 = "com";
    s.t();
}
Localize.prototype.Redirect = function() {
    var controlId = this.Id;
    var control = eval(controlId);

    if (control.StreetElement != null && control.StreetElement.value != "")
        control.StreetAddress = control.StreetElement.value.trim();
    else {
        var streetAddress = GetCookie("Serviceability", "StreetAddress");
        if (streetAddress != "")
            control.StreetAddress = streetAddress.trim().replaceAll("+", " ");
    }
    if (control.AptElement != null && control.AptElement.value != "")
        control.AptNumber = control.AptElement.value.trim();
    else {
        var AptNumber = GetCookie("Serviceability", "AptNumber");
        if (AptNumber != "")
            control.AptNumber = AptNumber.trim();
    }
    if (control.ZipCodeElement != null && control.ZipCodeElement.value != "")
        control.ZipCode = control.ZipCodeElement.value.trim();
    else {
        var ZipCode = GetCookie("Serviceability", "Zip");
        if (ZipCode != "")
            control.ZipCode = ZipCode.trim();
    }

    control.OnLocalize();
}