﻿(function(){ // Import GET Vars
   document.$_GET = [];
   var urlHalves = String(document.location).split('?');
   if(urlHalves[1]){
      var urlVars = urlHalves[1].split('&');
      for(var i=0; i<=(urlVars.length); i++){
         if(urlVars[i]){
            var urlVarPair = urlVars[i].split('=');
            document.$_GET[urlVarPair[0]] = urlVarPair[1];
         }
      }
   }
})();

function post_to_url(path, params, method) {
    method = method || "get"; // Set method to get by default, if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for (var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }
    document.body.appendChild(form);
    form.submit();
}

$('#chooseZip').keypress(function (e) {
    if (e.which == 13) {
        e.preventDefault();
        ContinueZip();
        return false;
    }
});

function ContinueZip() {
    var queryType = "";
    if ($('#rateType').val() == '5' || $('#customerType').val() == '5') {
        post_to_url('/Home/EnterpriseGasServices/RequestQuote.aspx', {});
    } else {
        if ($('#rateType').val() == null || $('#rateType').val() == "") {
            if ($('#customerType').val() == "" || $('#customerType').val() == null) {
                alert("Please select a rate type before proceeding");
            } else {
                if ($('#chooseZip').val() == null || $('#chooseZip').val() == "") {
                    alert("Please enter a zip code before proceeding");
                } else {
                    queryType = $('#customerType').val();
                }
            }
        } else {
            if ($('#chooseZip').val() == null || $('#chooseZip').val() == "") {
                alert("Please enter a zip code before proceeding");
            } else {
                queryType = $('#rateType').val();
            }
        }
        if (queryType != "" && queryType != null) {
            post_to_url('/RatesServices.aspx', { 'rate': queryType, 'zip': $('#chooseZip').val() });
        }
    }
}


function ContinueLink() {
    var queryType = "";
    if ($('#chooseRate').val()) {
        queryType = $('#chooseRate').val();
    } else {
        if ($('#rateType').val() == null) 
            queryType = 1;
        else
            queryType = $('#rateType').val();
    }
    post_to_url('/RatesServices.aspx', { 'rate': queryType, 'state': $('#chooseState').val(), 'utility': $('#chooseUtility').val() });
}

function BuildRatesMenu() {
    $("#chooseState").children().remove().end().append("<option value=\"\" selected>Select Your State</option>");
    $("#chooseUtility").children().remove().end().append("<option value=\"\" selected>Select Your Utility</option>");
    if ($('select#chooseRate').val() != '' && $('select#chooseRate').val() != null) {
        $.ajax({
            data: "{}",
            type: "POST",
            // you need a serversite script which checks the username
            url: "/RateFinder.asmx/States_Select",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data, textStatus, jqXHR) {
                var d = data.hasOwnProperty("d") ? data.d : data;
                States_Select_success(d);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var error = eval("(" + jqXHR.responseText + ")");
                if (error.hasOwnProperty("d"))
                    States_Select_error(error.d);
                else
                    States_Select_error(error);
            },
            complete: function (jqXHR, textStatus) { },
            timeout: function () { }
        });
        $('select#chooseState').removeAttr('disabled');
        $('select#chooseUtility').attr('disabled', true);
        $('#Continue').attr('disabled', true).attr("src", "/img/rightCol-continueBTdisabled.png");
    } else {
        $('select#chooseState').attr('disabled', true);
        $('select#chooseUtility').attr('disabled', true);
        $('#Continue').attr('disabled', true).attr("src", "/img/rightCol-continueBTdisabled.png");
    }
}


function BuildStatesMenu(defaultID) {
    if ($('select#chooseState').val() != '' && $('select#chooseState').val() != null) {
        $.ajax({
            type: "POST",
            url: "/RateFinder.asmx/Utilities_SelectByState",
            data: "{stateId: " + $('#chooseState').val() + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data, textStatus, jqXHR) {
                var d = data.hasOwnProperty("d") ? data.d : data;
                Utilities_Select_success(d);
                if (defaultID != null) {
                    $('#chooseUtility').selectBox('destroy');
                    $('#chooseUtility').val(defaultID);
                    $('#chooseUtility').selectBox('create');
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var error = eval("(" + jqXHR.responseText + ")");
                if (error.hasOwnProperty("d"))
                    Utilities_Select_error(error.d);
                else
                    Utilities_Select_error(error);
            },
            complete: function (jqXHR, textStatus) { },
            timeout: function () { }
        });
        $('select#chooseUtility').removeAttr('disabled');
        $('#Continue').attr('disabled', true).attr("src", "/img/rightCol-continueBTdisabled.png");
    } else {
        $('select#chooseUtility').attr('disabled', true);
        $('#Continue').attr('disabled', true).attr("src", "/img/rightCol-continueBTdisabled.png");
    }
}
function BuildUtilitiesMenu() {
    if ($('select#chooseUtility').val() != '' && $('select#chooseUtility').val() != null) {
        $.ajax({
            type: "POST",
            url: "/RateFinder.asmx/Rates_SelectByUtility",
            data: "{utilityId: " + $('#chooseUtility').val() + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data, textStatus, jqXHR) {
                var d = data.hasOwnProperty("d") ? data.d : data;
                Rates_SelectByUtility_success(d);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var error = eval("(" + jqXHR.responseText + ")");
                if (error.hasOwnProperty("d"))
                    Rates_SelectByUtility_error(error.d);
                else
                    Rates_SelectByUtility_error(error);
            },
            complete: function (jqXHR, textStatus) { },
            timeout: function () { }
        });
        $('#Continue').removeAttr('disabled').attr("src", "/img/rightCol-continueBT.png");
    } else {
        $('#Continue').attr('disabled', true).attr("src", "/img/rightCol-continueBTdisabled.png");
    }
}

$(document).ready(function () {

    BuildRatesMenu();
    BuildStatesMenu();
    BuildUtilitiesMenu();
    
    if ($('#chooseRate').val() != '') {
        $('select#chooseState').removeAttr('disabled');
    } else {
        $('select#chooseState').attr('disabled', true);
        $('select#chooseUtility').attr('disabled', true);
        $('#Continue').attr('disabled', true).attr("src", "/img/rightCol-continueBTdisabled.png");
    }

    if ($('#chooseState').val() != '' && $('#chooseRate').val() != '') {
        $('select#chooseUtility').removeAttr('disabled');
    } else {
        $('select#chooseUtility').attr('disabled', true);
        $('#Continue').attr('disabled', true).attr("src", "/img/rightCol-continueBTdisabled.png");
    }

    if ($('#chooseUtility').val() != '' && $('#chooseState').val() != '' && $('#chooseRate').val() != '') {
        $('#Continue').removeAttr('disabled').attr("src", "/img/rightCol-continueBT.png");
    } else {
        $('#Continue').attr('disabled', true).attr("src", "/img/rightCol-continueBTdisabled.png");
    }

});
//  $(document).ready(States_SelectAll);

/********** Form Events **********/

//        function btnLoadStates_Click() {
//            States_SelectAll();
//        }

//        function btnLoadUtilitiesByState_Click() {
//            Utilities_Select($('#states').val());
//        }

//        function btnLoadRatesByUtility_Click(sender) {
//            Rates_SelectByUtility(sender.value);
//        }

//        function btnLoadRatesByPostalCodeAndRateType_Click() {
//            Rates_SelectByPostalCodeAndRateType($('#txtZipCode').val(), $('#rateType').val());
//        }

/********** Asynchronous Events **********/

function States_Select_success(msg) {
    $("#chooseState").selectBox('destroy');
    $("#chooseState").children().remove().end().append("<option value=\"\" selected>Choose State</option>");
    $.each(msg, function () { $("#chooseState").append("<option value=\"" + this.StateId + "\">" + this.Value + "</option>"); });
    $("#chooseState").selectBox('create');
}

function States_Select_error(msg) {
    debugger;
    var html = "";
    html += "Message: " + msg.Message + "<br />";
    $('#error').html(html);
}

function Utilities_Select_success(msg) {
    $("#chooseUtility").selectBox('destroy');
    $("#chooseUtility").children().remove().end().append("<option value=\"\" selected>Choose Utility</option>");
    $.each(msg, function () { $("#chooseUtility").append("<option value=\"" + this.UtilityId + "\">" + this.Value + "</option>"); });
    $("#chooseUtility").selectBox('create');
}

function Utilities_Select_error(msg) {
    debugger;
    var html = "";
    html += "Message: " + msg.Message + "<br />";
    $('#error').html(html);
}

function Offers_Select_error(msg) {
    debugger;
    var html = "";
    html += "Message: " + msg.Message + "<br />";
    $('#error').html(html);
}
        function Rates_SelectByUtility_success(msg) {
//            var html = "";
//            $.each(msg, function () { html += "<li>Utility: " + this.Utility.Value + "/ RateType: " + this.Rate_Type + " / Unit: " + this.Unit + " / Jan_L: " + this.Jan_L + "</li>"; });
//            $('#rates').html(html);
//            $('#error').html("");
        }

//        function Rates_SelectByUtility_error(msg) {
//            var html = "";
//            html += "Message: " + msg.Message + "<br />";
//            $('#rates').html("");
//            $('#error').html(html);
//        }

//        function Rates_SelectByPostalCodeAndRateType_success(msg) {
//            var html = "";
//            $.each(msg, function () { html += "<li>Utility: " + this.Utility.Value + "/ RateType: " + this.Rate_Type + " / Unit: " + this.Unit + " / Jan_L: " + this.Jan_L + "</li>"; });
//            $('#rates').html(html);
//            $('#error').html("");
//        }

//        function Rates_SelectByPostalCodeAndRateType_error(msg) {
//            var html = "";
//            html += "Message: " + msg.Message + "<br />";
//            $('#rates').html("");
//            $('#error').html(html);
//        }


