﻿var ZipCodeHandler =
{
    serverRequest: null,
    cityNameDiv: null,
    textBox: null,
    countryDropDownList: null,
    
    lastZipCode: null,
    lastCountryID: null,
    
    zipCodeNotFoundMessage: null,
    defaultMessage: null,
    cityNameDivID: null,
    textBoxID: null,
    countryDropDownListID: null,
    
    initialize: function()
    {
        this.cityNameDiv = S.Get(this.cityNameDivID),
        this.textBox = S.Get(this.textBoxID),
        this.countryDropDownList = S.Get(this.countryDropDownListID);
        
        if (this.cityNameDiv == null || this.textBox == null || this.countryDropDownList == null)
			return;
        
        // Set edit data so if the user tabs by the zip code text box, the search http call is not executed.
        if (this.textBox.value.length > 0)
        {
            this.lastZipCode = this.textBox.value;
            this.lastCountryID = this.countryDropDownList.options[this.countryDropDownList.selectedIndex].value;
        }
                
        S.Event.add(this.textBox, "blur", ZipCodeHandler.blur.bind(ZipCodeHandler));
        S.Event.add(this.countryDropDownList, "blur", ZipCodeHandler.blur.bind(ZipCodeHandler));
    },
    
    blur: function()
    {
        var zipCode = this.textBox.value.trim();
        var countryID = "";
        
        if (this.countryDropDownList.selectedIndex < 1)
        {
            this.showMessage(this.defaultMessage);
            return;
        }
        else
        {
            countryID = this.countryDropDownList.options[this.countryDropDownList.selectedIndex].value;
        }
        
        this.textBox.value = zipCode;
        
        if (zipCode != this.lastZipCode || countryID != this.lastCountryID)
        {
            if (zipCode.length > 0 && countryID.length > 0)
            {
                if (this.serverRequest == null)
                    this.serverRequest = new ServerRequest("GET", this.complete.bind(this))
            
                this.serverRequest.send("/Global/ZipCode/Search.aspx", "ZipCode={0}&CountryID={1}".format(zipCode, countryID));
                
                this.lastZipCode = zipCode;
                this.lastCountryID = countryID;
            }
            else
            {
                this.showMessage(this.defaultMessage);
            }
        }
    },
    
    complete: function(output)
    {
        if (output == null)
        {
            this.showMessage(this.zipCodeNotFoundMessage);
        }
        else
        {
            this.showMessage(output.Text);
        }
    },
    
    showMessage: function(message)
    {
        this.cityNameDiv.innerHTML = message;
    }
};

S.Event.add(window, "load", ZipCodeHandler.initialize.bind(ZipCodeHandler));


// Ajax element "region update"
function RegionUpdater(serverRequestUrl, countryDrpDwnID, countryAreaDrpDwnID) {
    this.serverRequestUrl = serverRequestUrl;
    this.serverRequest = null;

    this.countryDrpDwnID = countryDrpDwnID;
    this.countryAreaDrpDwnID = countryAreaDrpDwnID;
    
    this.retreving = false;

    S.Event.add(window, "load", this.initialize.bind(this));
}

RegionUpdater.prototype.initialize = function() {
    S.Event.add(document.getElementById(this.countryDrpDwnID), "change", this.slide.bind(this, "next"));
}

RegionUpdater.prototype.slide = function(action) {
    if (this.retreving) {
        return;
    }


    this.retreving = true;

    if (this.serverRequest == null)
        this.serverRequest = new ServerRequest("GET", this.updatecallback.bind(this, action));

    this.serverRequest.send(this.serverRequestUrl, "C={0}".format(document.getElementById(this.countryDrpDwnID).options[document.getElementById(this.countryDrpDwnID).selectedIndex].value));
}

RegionUpdater.prototype.updatecallback = function(action, data) {
    if (data) {
        var title = null;
        
        var x = document.getElementById(this.countryAreaDrpDwnID);
        var opt = x.options;
        opt.length = 0;

        for (i = 0; i < data.Elements.length; i++) {

            //var y = x[i].cells;
            if (data.Elements[i]) {
                var theOption = new Option;
                theOption.text = data.Elements[i].Name;
                theOption.value = data.Elements[i].ID;
                opt[i] = theOption;

                //y[0].innerHTML = '<h2><a href="/Community/Artikler/Artikel/' + data.Elements[i].ID + '/Default.aspx" title="' + data.Elements[i].Title + '">' + data.Elements[i].Title + '</a></h2><p>' + data.Elements[i].Description.substring(0, 130) + '</p>';
            }
        }

    }

    this.retreving = false;
}