﻿function ElectricityConsumption(selectedOption, customValue) {
    this.selectedOption = selectedOption;
    this.customValue = customValue;
}

function ElectricityConsumptionUtility() {
    this.privateConsumption = new ElectricityConsumption();
    this.corporateConsumption = new ElectricityConsumption();
}

$.extend(ElectricityConsumptionUtility.prototype, {

    initialize: function() {
        this.privateConsumption.selectedOption = $('input[id*=uiRbPower2000]');
        this.corporateConsumption.selectedOption = $('input[id*=uiRbPower20000]');
    },

    LoadConsumption: function(electricityConsumption) {
        this.ResetSelection();
        if (!electricityConsumption.selectedOption.is(':checked')) {
            electricityConsumption.selectedOption.get(0).checked = true;
        }

        if (!electricityConsumption.selectedOption.parent().parent().hasClass('selected')) {
            electricityConsumption.selectedOption.parent().parent().addClass('selected');
        }
        $('input[id*=uiTxtAnnualPowerUsage]').val(electricityConsumption.customValue);
    },

    ResetSelection: function() {
        $('div.rightPart input').each(function() {
            this.checked = false;
        });

        $('div.rightPart label.selected').removeClass('selected');

        $('input[id*=uiTxtAnnualPowerUsage]').val('');
    },

    GetConsumptionData: function() {
        var consumption = new ElectricityConsumption();
        consumption.selectedOption = this.GetSelectedConsumptionOption();
        consumption.customValue = $('input[id*=uiTxtAnnualPowerUsage]').val();
        return consumption;
    },

    GetSelectedConsumptionOption: function() {
        return $('div.rightPart input:checked');
    }

});

function FireValidators() {
    return Page_ClientValidate("PowerUsage")
            && Page_ClientValidate("PostalCode")
            && Page_ClientValidate("SelectedNetCompany");
}

function HideSelectNetCompanyWarning() {
    $('span[id*=uiCvNetCompanySelectionRequired]').hide();
    return true;
}

function SelectNetCompany(listItem) {
    var childs = listItem.childNodes;
    for (var i = 0; i < childs.length; i++) {
        if (childs[i].tagName == "A") {
            HideSelectNetCompanyWarning();
            window.location = childs[i].href;
            break;
        }
    }
    
    var jListItem = $(listItem);
    //UncheckNetCompanyRadioButtons(jListItem);
    //jListItem.find('input[type=radio]').attr('checked', 'true');
}

function UncheckNetCompanyRadioButtons(jListItem) {
    jListItem.parent().find('input[type=radio]').each(function(){ $(this).attr('checked', 'false');});
}

function updateNetCompaniesPanel(element) {
    var postbackButtonName = $('input[id*=uiBtnFix]').attr('name');
    if (element.value.length >= 3 && element.value.length <= 4) {
        //  validate number
        if (Page_ClientValidate("PowerUsage") && Page_ClientValidate("PostalCode")) {
            __doPostBack(postbackButtonName, '');
            return true;
        }
    }

    if (element.value.length == "0") {
        __doPostBack(postbackButtonName, '');
        return true;
    }

    return false;
}

function NetCompanySelectionRequired_Validation(source, arguments) {

    var result = $.ajax({
        async: false,
        type: "POST",
        url: "Default.aspx",
        data: { ajax: true,
            IsNetCompanySelectedValidation: true,
            __VIEWSTATE: $("input[id='__VIEWSTATE']").val(),
            __ASYNCPOST: false
        },
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "json"
    });


    var CompanyIsSelectedResponse = eval("(" + result.responseText + ")");
    arguments.IsValid = Boolean(CompanyIsSelectedResponse.Selected);
    return arguments.IsValid;
}

function postnumberValidation(source, arguments) {
    var regex = '^[0-9]+$';
    var control = document.getElementById(source.controltovalidate);
    var parentElement;
    if (control.value && control.value.match(regex) && control.value.length >= 3 && control.value.length <= 4) {
        arguments.IsValid = true;
        return true;
    }

    var postbackButtonName = $('input[id*=uiBtnFix]').attr('name');

    __doPostBack(postbackButtonName, '');

    arguments.IsValid = false;
    $('input[id*=uiTxtZipcode]').addClass("error");
    return false;
}

function powerUsageValidation(source, value) {
    var regex = '^[0-9]+$';
    var control = document.getElementById(source.controltovalidate);
    var parentElement;

    var accosiatedRadioButton = $('input[id*=uiRbAnnualPowerUsage]');
    if (!accosiatedRadioButton.is(':checked')) {
        value.IsValid = true;
        return true;
    }

    if (accosiatedRadioButton.is(':checked') && value.Value && value.Value.match(regex) && (parseInt(value.Value) > 0 )) {
        value.IsValid = true;
        return true;
    }

    value.IsValid = false;
    $('input[id*=uiTxtAnnualPowerUsage]').addClass("error");
    return false;
}

function powerUsageLimitValidation(x, limit) {
    var y = document.getElementById(x).value;
    var accosiatedRadioButton = $('input[id*=uiRbAnnualPowerUsage]');
    if (accosiatedRadioButton.is(':checked')) {
        if (parseInt(document.getElementById(x).value) > limit) {
            $('span[id=uiAnnualPowerUsageLimitErrorMessage]').show();
            return false;
        }
    }

    $('span[id=uiAnnualPowerUsageLimitErrorMessage]').hide();
    return true
}
