﻿var showAllContracts = false;

$(document).ready(function() {

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-16766448-1']);
    _gaq.push(['_trackPageview']);

    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();

  
    $("#loading").loaderToggler({
        loaderDiv: $("#loading")
    });

    //$("select, input:checkbox, input:radio, input:text").uniform();
    $("#login_container").login({
        geturl: "/ca/account/logon?decorator=false",
        formId: "syswideInlog"
    });

    $("#zipcode").addressLookup({
        posturl: "/ca/register/getAddress?decorator=false",
        zipcodeInput: $("#zipcode"),
        numberInput: $("#number"),
        streetInput: $("#street"),
        streetFieldInput: $("#streetField"),
        cityInput: $("#city"),
        cityFieldInput: $("#cityField")
    });


    $("#viewDontCallMePart").dontCallMePane({
        dontCallMeView: $("#viewDontCallMePart"),
        dontCallMeYes: $("#DontCallMeYes"),
        dontCallMeNo: $("#DontCallMeNo"),
        dontCallMeState: $("input:radio[name='dontcallmestate']:checked")
    });

    $("#myForm").myAdvice({
        baseurl: "/ca"
    });


    $("#changeContractGrid").changeContractGrid({
        contractGrid: $("#changeContractGrid"),
        startDate: $("#StartDate"),
        url: "/ca/MyPage/MyAdvice/GetChosenContract?debug=true"
    });

    $("#recentList").recentTotalList({
        recentGrid: $("#recentList"),
        totalGrid: $("#totalList"),
        totalListWrapper: $("#totalListWrapper"),
        totalListEmpty: $("#totalListEmpty"),
        url: "/ca/MyPage/MyAdvice/GetTotalSupplierContracts?debug=true"
    });

    $("#listForm").filterTable({
        totalGrid: $("#totalList"),
        totalListWrapper: $("#totalListWrapper"),
        totalListEmpty: $("#totalListEmpty"),
        listForm: $("#listForm"),
        url: "/ca/MyPage/MyAdvice/FilterSupplierContracts?debug=true"
    });

    // Infobox
    $(".infobox").hover(
	    function() {
	        $('.uitleg_container', this).fadeIn('fast');
	    },
	    function() {
	        $('.uitleg_container', this).fadeOut('fast');
	    }
    );

    $("#StartDate").calenderOption({
        radioControl: $("input:radio[name='StartPerDate']"),
        calendarfield: $("#StartDate")
    })

    if ($('#slideshow')) {
        $('#slideshow').jshowoff({ speed: 8000, links: true, effect: 'none', autoplay: true, controls: false, changeSpeed: 1000, animatePause: false });
    }

    $(".goedkoopstePrijs").calculationSummary({
        clickCell: $(".goedkoopstePrijs span"),
        title: "Prijsberekening goedkoopste contract",
        hideContractData: true,
        url: "/ca/MyPage/MyAdvice/GetCalculationSummary/" + $("#CheapestContractId").val() + "?debug=true"
        
    });

    $(".huidigePrijs").calculationSummary({
        clickCell: $(".huidigePrijs span"),
        title: "Prijsberekening huidige contract",
        hideContractData: true,
        url: "/ca/MyPage/MyAdvice/GetCalculationSummary/" + $("#RecentContractId").val() + "?debug=true"
    });

    $(".mycontract").contractForm({
        elecSupplierId: $("#ElecSupplierId"),
        elecContractId: $("#ElecContractId"),
        elecSupplierIdHidden: $("#ElecSupplierIdHidden"),
        selectedContractElectraId: $("#ContractElectraIdHidden"),
        gasSupplierId: $("#GasSupplierId"),
        gasContractId: $("#GasContractId"),
        gasSupplierIdHidden: $("#GasSupplierIdHidden"),
        selectedContractGasId: $("#ContractGasIdHidden"),
        elecStartDate: $("#ElecStartDate"),
        elecEndDate: $("#ElecEndDate"),
        gasStartDate: $("#GasStartDate"),
        gasEndDate: $("#GasEndDate"),
        localDateFormat: "dd-mm-yy"
    })

    $("#contractTransferForm").contractTransfer({
        submitButton: $("#buttonConfirmTransfer"),
        backButton: $("#buttonBack"),
        form: $("#contractTransferForm"),
        popup: $(".popup"),
        cancelButton: $("#cancelButton"),
        transferButton: $("#transferButton")

    })

});




if (jQuery) (function($) {
    $.extend($.fn, {
        contractTransfer: function(o) {
            if (o.submitButton == undefined) return false;
            if (o.backButton == undefined) return false;
            if (o.form == undefined) return false;
            if (o.popup == undefined) return false;
            if (o.cancelButton == undefined) return false;
            if (o.transferButton == undefined) return false;

            o.backButton.click(function() {
                history.back();
            })


            o.cancelButton.click(function() {                
                o.popup.dialog("destroy");
            })

            o.transferButton.click(function() {                
                o.form.submit();
            })

            o.submitButton.click(function() {
                o.popup.dialog({
                    modal: true,
                    title: "Overstappen",
                    draggable: true,
                    resizable: false,
                    modal: true,
                    width: 420,
                    height: 220,
                    close: function(event, ui) {
                        $(this).dialog("destroy");
                    }
                });
            })
        }
    })
})(jQuery);

if (jQuery) (function($) {
    $.extend($.fn, {
        contractForm: function(o) {
            if (o.elecContractId == undefined) return false;
            if (o.elecSupplierId == undefined) return false;
            if (o.elecSupplierIdHidden == undefined) return false;
            if (o.selectedContractElectraId == undefined) return false;
            if (o.gasSupplierId == undefined) return false;
            if (o.gasContractId == undefined) return false;
            if (o.gasSupplierIdHidden == undefined) return false;
            if (o.selectedContractGasId == undefined) return false;
            if (o.elecStartDate == undefined) return false;
            if (o.elecEndDate == undefined) return false;
            if (o.gasStartDate == undefined) return false;
            if (o.gasEndDate == undefined) return false;
            if (o.localDateFormat == undefined) return false

            o.elecStartDate.datepicker({ dateFormat: o.localDateFormat, constrainInput: true });
            o.elecEndDate.datepicker({ dateFormat: o.localDateFormat, constrainInput: true });
            o.gasStartDate.datepicker({ dateFormat: o.localDateFormat, constrainInput: true });
            o.gasEndDate.datepicker({ dateFormat: o.localDateFormat, constrainInput: true });

            //fill contracts
            o.elecSupplierId.contractSelector({
                url: "/ca/MyAdvice/GetContractsElectra",
                selectedContractId: o.selectedContractElectraId,
                contractList: o.elecContractId
            });

            //fill contracts
            o.gasSupplierId.contractSelector({
                url: "/ca/MyAdvice/GetContractsGas",
                selectedContractId: o.selectedContractGasId,
                contractList: o.gasContractId
            });

            o.elecSupplierId.val(o.elecSupplierIdHidden.val());
            o.gasSupplierId.val(o.gasSupplierIdHidden.val());

            o.elecSupplierId.change();
            o.gasSupplierId.change();

        }
    })
})(jQuery);


// MYADVICE //
if (jQuery) (function($) {
    $.extend($.fn, {
        contractSelector: function(o) {

            if (o.contractList == undefined) return false;
            if (o.selectedContractId == undefined) return false;
            if (o.url == undefined) return false;

            var f = [];
            f.supplierList = $(this);
            f.contractList = o.contractList;
            f.selectedContractId = o.selectedContractId;

            f.supplierList.change(function(e) {

                var supplierId = ($(this).val() == "" ? "0" : $(this).val())
                var $options = $('option', f.contractList);

                $options.each(function() {
                    $(this).remove();
                });

                $.ajax({
                    type: "POST",
                    url: o.url + "/" + supplierId + "?debug=true&selectedContractId=" + f.selectedContractId.val() + "&showAll=" + showAllContracts.toString(),
                    dataType: "json",
                    async: true,
                    success: function(data) {
                        $.each(data, function(i, item) {

                            var option = $(document.createElement("option"));
                            option.attr("value", item.Value);
                            option.text(item.Text);
                            if (item.Selected) {
                                option.attr("selected", "selected");
                            }
                            f.contractList.append(option);
                        });
                        $(this).setControls();

                    }
                });
            });

        },
        setControls: function(o) {

            var arrMessages = [];
            arrMessages["LabelUsageElectra"] = $("#hiddenLabelUsageElectra").val();
            arrMessages["LabelUsageElectraLow"] = $("#hiddenLabelUsageElectraLow").val();
            arrMessages["LabelUsageElectraHigh"] = $("#hiddenLabelUsageElectraHigh").val();

            var f = [];
            f.compareMode = $("input[name='Compare']");
            f.differentSupplier = $("input[name='DifferentSupplier']");
            f.supplierList = $("#SupId");
            f.supplierListGas = $("#SupGasId");
            f.supplierListElectra = $("#SupElectraId");
            f.contractList = $("#SupContractId");
            f.contractListGas = $("#SupContractGasId");
            f.contractListElectra = $("#SupContractElectraId");
            f.electraMeterType = $("input[name='ElectraMeterType']");
            f.rowSupplier = $("#rowSupplier");
            f.rowSupplierGas = $("#rowSupplierGas");
            f.rowSupplierElectra = $("#rowSupplierElectra");
            f.rowDifferentSupplier = $("#rowDifferentSupplier");
            f.rowContract = $("#rowContract");
            f.rowElecType = $("#rowElecType");
            f.rowHouseKeeping = $("#rowHouseKeeping");
            f.rowGasUsage = $("#rowGasUsage");
            f.rowElectraUsage = $("#rowElectraUsage");
            f.rowElectraUsageLow = $("#rowElectraUsageLow");
            f.gasUsage = $("#GasUsage");
            f.electraUsage = $("#ElectraUsage");
            f.electraUsageHigh = $("#ElectraUsageHigh");
            f.electraUsageLow = $("#ElectraUsageLow");
            f.rowUsageTitle = $("#rowUsageTitle");
            f.labelElectra = $("#lblUsageTypeElec");
            f.labelElectraLow = $("#lblUsageTypeElecLow");
            f.labelElectra.text(arrMessages["LabelUsageElectra"]);
            f.labelElectraLow.text(arrMessages["LabelUsageElectraLow"]);

            switch (f.compareMode.filter(":checked").val()) {
                case "both":

                    f.rowDifferentSupplier.show();

                    if (f.differentSupplier.attr("checked") == true) {
                        //set Different Supplier Mode
                        f.rowSupplierGas.show();
                        f.rowSupplierElectra.show();
                        f.rowSupplier.hide();
                        f.rowContract.hide();

                        if (f.contractListElectra.val() > 0) {
                            f.rowElectraUsage.show();
                            f.rowElecType.show();
                            f.rowHouseKeeping.show();
                            f.rowUsageTitle.show();

                            if (f.electraMeterType.filter(':checked').val() == "double") {
                                f.rowElectraUsageLow.show();
                                f.electraUsageHigh.show();
                                f.electraUsage.hide();
                            }
                            else {
                                f.rowElectraUsageLow.hide();
                                f.electraUsageHigh.hide();
                                f.electraUsage.show();
                            }
                        }
                        else {
                            f.rowElectraUsage.hide();
                            f.rowElectraUsageLow.hide();
                            f.rowElecType.hide();
                            f.rowHouseKeeping.hide();
                            f.rowUsageTitle.hide();
                        }

                        if (f.contractListGas.val() > 0) {
                            f.rowGasUsage.show();
                            f.rowHouseKeeping.show();
                            f.rowUsageTitle.show();
                        }
                        else {
                            f.rowGasUsage.hide();
                            f.rowHouseKeeping.hide();
                            f.rowUsageTitle.hide();
                        }


                    } else {
                        f.rowSupplierGas.hide();
                        f.rowSupplierElectra.hide();
                        f.rowSupplier.show();
                        f.rowContract.show();

                        if (f.contractList.val() > 0) {
                            f.rowElectraUsage.show();
                            f.rowElecType.show();
                            f.rowHouseKeeping.show();
                            f.rowUsageTitle.show();
                            f.rowGasUsage.show();

                            if (f.electraMeterType.filter(':checked').val() == "double") {
                                f.rowElectraUsageLow.show();
                                f.electraUsageHigh.show();
                                f.electraUsage.hide();
                                f.labelElectra.text(arrMessages["LabelUsageElectraHigh"]);
                                f.labelElectraLow.text(arrMessages["LabelUsageElectraLow"]);
                                ///
                            }
                            else {
                                f.rowElectraUsageLow.hide();
                                f.electraUsageHigh.hide();
                                f.electraUsage.show();
                                f.labelElectra.text(arrMessages["LabelUsageElectra"]);
                                f.labelElectraLow.text(arrMessages["LabelUsageElectra"]);
                                ///
                            }
                        }
                        else {
                            f.rowElectraUsage.hide();
                            f.rowElectraUsageLow.hide();
                            f.rowElecType.hide();
                            f.rowHouseKeeping.show();
                            f.rowUsageTitle.show();
                            f.rowGasUsage.hide();
                            f.rowHouseKeeping.hide();
                            f.rowUsageTitle.hide();
                        }

                    }
                    break;
                case "gas":
                    f.rowDifferentSupplier.hide();
                    f.differentSupplier.attr("checked", false);

                    f.rowSupplierGas.show();
                    f.rowSupplierElectra.hide();
                    f.rowSupplier.hide();
                    f.rowContract.hide();

                    if (f.contractListGas.val() > 0) {
                        f.rowGasUsage.show();
                        f.rowHouseKeeping.show();
                        f.rowUsageTitle.show();
                    }
                    else {
                        f.rowGasUsage.hide();
                        f.rowHouseKeeping.hide();
                        f.rowUsageTitle.hide();
                    }

                    f.rowElectraUsage.hide();
                    f.rowElectraUsageLow.hide();
                    f.rowElecType.hide();

                    break;
                case "electra":
                    f.rowDifferentSupplier.hide();
                    f.differentSupplier.attr("checked", false);

                    f.rowSupplierGas.hide();
                    f.rowSupplierElectra.show();
                    f.rowSupplier.hide();
                    f.rowContract.hide();

                    if (f.contractListElectra.val() > 0) {
                        f.rowElectraUsage.show();
                        f.rowElecType.show();
                        f.rowHouseKeeping.show();
                        f.rowUsageTitle.show();

                        if (f.electraMeterType.filter(':checked').val() == "double") {
                            f.rowElectraUsageLow.show();
                            f.electraUsageHigh.show();
                            f.electraUsage.hide();
                            f.labelElectra.text(arrMessages["LabelUsageElectraHigh"]);
                            f.labelElectraLow.text(arrMessages["LabelUsageElectraLow"]);
                            ///
                        }
                        else {
                            f.rowElectraUsageLow.hide();
                            f.electraUsageHigh.hide();
                            f.electraUsage.show();
                            f.labelElectra.text(arrMessages["LabelUsageElectra"]);
                            f.labelElectraLow.text(arrMessages["LabelUsageElectra"]);
                            ///
                        }
                    }
                    else {
                        f.rowElectraUsage.hide();
                        f.rowElectraUsageLow.hide();
                        f.rowElecType.hide();
                        f.rowUsageTitle.hide();
                        f.rowHouseKeeping.hide();
                    }
                    f.rowGasUsage.hide();
                    break;
            }


        },
        myAdvice: function(o) {
            if (o.baseurl == undefined) return false;

            var f = [];

            f.compareMode = $("input[name='Compare']");
            f.differentSupplier = $("input[name='DifferentSupplier']");
            f.supplierList = $("#SupId");

            f.contractList = $("#SupContractId");

            f.supplierListGas = $("#SupGasId");
            f.contractListGas = $("#SupContractGasId");
            f.supplierListElectra = $("#SupElectraId");
            f.contractListElectra = $("#SupContractElectraId");

            f.houseKeeping = $("input[name='Housekeeping']");
            f.electraMeterType = $("input[name='ElectraMeterType']");
            f.zipcodeNumbers = $("#ZipcodeNumbers");
            f.zipcodeLetters = $("#ZipcodeLetters");

            f.gasUsage = $("#GasUsage");
            f.electraUsage = $("#ElectraUsage");
            f.electraUsageHigh = $("#ElectraUsageHigh");
            f.electraUsageLow = $("#ElectraUsageLow");

            f.gasPeriod = $("#GasPeriod");
            f.electraPeriod = $("#ElectraPeriod");
            f.electraPeriodLow = $("#ElectraPeriodLow");

            f.rowSupplierGas = $("#rowSupplierGas");
            f.rowSupplierElectra = $("#rowSupplierElectra");
            f.rowSupplier = $("#rowSupplier");
            f.rowContract = $("#rowContract");
            f.rowElecType = $("#rowElecType");
            f.rowHouseKeeping = $("#rowHouseKeeping");

            f.rowGasUsage = $("#rowGasUsage");
            f.rowElectraUsage = $("#rowElectraUsage");
            f.rowElectraUsageLow = $("#rowElectraUsageLow");

            f.rowDifferentSupplier = $("#rowDifferentSupplier");

            f.selectedContractGasId = $("#SupContractGasIdHidden");
            f.selectedContractElectraId = $("#SupContractElectraIdHidden");
            f.selectedContractId = $("#SupContractIdHidden");


            //events===============================================================

            //fill contracts
            f.supplierList.contractSelector({
                url: o.baseurl + "/MyAdvice/getContracts",
                contractList: f.contractList,
                selectedContractId: f.selectedContractId
            });
            //fill contracts
            f.supplierListElectra.contractSelector({
                url: o.baseurl + "/MyAdvice/getContractsElectra",
                selectedContractId: f.selectedContractElectraId,
                contractList: f.contractListElectra
            });


            //fill contracts
            f.supplierListGas.contractSelector({
                url: o.baseurl + "/MyAdvice/getContractsGas",
                selectedContractId: f.selectedContractGasId,
                contractList: f.contractListGas
            });


            f.zipcodeNumbers.keyup(function(e) {

                var re = /^[0-9]{4}$/

                if (re.test(f.zipcodeNumbers.val()) &&        //check for 4 numbers
                        (
                            (e.which >= 96 && e.which <= 105) ||    //0-9 keypad
                            (e.which >= 48 && e.which <= 57)        //0-9 
                        )
                   ) {
                    f.zipcodeLetters.select();
                }
            });


            f.compareMode.click(function() {
                $(this).setControls();
            });

            f.differentSupplier.click(function() {
                $(this).setControls();
            });
            f.electraMeterType.click(function() {
                $(this).setControls();
            });

            f.contractList.change(function() {
                $(this).setControls();
            });
            f.contractListGas.change(function() {
                $(this).setControls();
            });
            f.contractListElectra.change(function() {
                $(this).setControls();
            });


            //event click housekeeping
            f.houseKeeping.click(function() {

                var id = f.houseKeeping.filter(":checked").val();

                $.ajax({
                    type: "POST",
                    url: o.baseurl + '/MyAdvice/SelectHousekeeping/' + id + '?debug=true',
                    dataType: "json",
                    async: true,
                    success: function(data) {

                        f.gasUsage.val(data[0].Value);
                        f.electraUsage.val(data[1].Value);
                        f.electraUsageHigh.val(data[2].Value);
                        f.electraUsageLow.val(data[3].Value);
                    }
                });
            });

            f.supplierList.change();
            f.supplierListGas.change();
            f.supplierListElectra.change();

            $(this).setControls();
        }
    });
})(jQuery);



if (jQuery) (function($) {

    $.extend($.fn, {

        loaderToggler: function(o) {
            if (o.loaderDiv == undefined) return false;

            

            o.loaderDiv.ajaxStart(function() {

                o.loaderDiv.show();
            });

            o.loaderDiv.ajaxStop(function() {
                o.loaderDiv.hide();
            });
        }
    });
})(jQuery);


if (jQuery) (function($) {



    $.extend($.fn, {

        recentTotalList: function(o) {
            if (o.recentGrid.length == 0) return false;
            if (o.totalGrid.length == 0) return false;
            if (o.url == undefined) return false;
            if (o.totalListWrapper == undefined) return false;
            if (o.totalListEmpty == undefined) return false;

            o.recentGrid.jqGrid({
                datatype: "local",
                width: 886,
                height: 'auto',
                colNames: ['Leverancier', 'Productnaam', 'Herkomst', 'Looptijd', 'Tarief', ' ', 'Prijs', '        '],
                colModel: [
				{ name: 'Supplier', index: 'Supplier', resizable: false, sortable: false, width: 100 },
				{ name: 'ProductName', index: 'ProductName', resizable: false, sortable: false, width: 180 },
				{ name: 'Energy', index: 'Energy', resizable: false, sortable: false, align: 'right', width: 60 },
				{ name: 'Duration', index: 'Duration', resizable: false, sortable: false, align: 'center', width: 60 },
				{ name: 'TypeContract', index: 'TypeContract', resizable: false, sortable: false, align: 'center', width: 60 },
				{ name: 'ActionProduct', index: 'ActionProduct', align: "left",sortable: false, resizable: false, align: 'center', width: 45 },
				{ name: 'TotalPrice', index: 'TotalPrice', align: "left", resizable: false, sortable: false, align: 'center', width: 60 },
                //{ name: 'Saving', index: 'Saving', width: 81, align: "left", sorttype: "string" },
                //{ name: 'Price', index: 'Price', sorttype: "string", resizable: false },
                //{ name: 'Service', index: 'Service', resizable: false, sortable: false, resizable: false, align: 'center', width: 90 },
                //{ name: 'Administration', index: 'Administration', resizable: false, sortable: false, align: 'center', resizable: false, width: 90 },
                //{ name: 'AdditionalFee', index: 'AddtionalFee', resizable: false, sortable: false, align: 'center', resizable: false, width: 90 }
				{name: 'ChangeContract', index: 'ChangeContract', resizable: false, sortable: false, width: 160 }
				],
                multiselect: false,
                loadonce: false
                //caption: "Leverancier overzicht"
            });
            $('#gbox_recentList th:eq(0)').attr('title', 'De aanbieder van het contract');
            $('#gbox_recentList th:eq(1)').attr('title', 'De contractnaam');
            $('#gbox_recentList th:eq(2)').attr('title', 'Herkomst: De herkomst van elektriciteit en gas');
            $('#gbox_recentList th:eq(3)').attr('title', 'Looptijd: De looptijd van het contract');
            $('#gbox_recentList th:eq(4)').attr('title', 'Tarief: Vast of variabel tarief per kWh/m3');
            $('#gbox_recentList th:eq(6)').attr('title', 'Prijs: De totaalprijs per jaar inclusief btw');
            $('#gbox_recentList th:eq(7)').attr('title', 'Besparing: De besparing t.o.v. uw huidige contract');

            o.totalGrid.jqGrid({
                datatype: "local",
                altRows: false,
                width: 886,
                height: 'auto',
                colNames: ['Id', 'Leverancier', 'Productnaam', 'Herkomst', 'Looptijd', 'Tarief', '&nbsp;', 'Prijs', 'Besparing', '       '],
                colModel: [
				{ name: 'Id', index: 'Id', sorttype: "int", hidden: true, resizable: false },
				{ name: 'Supplier', index: 'Supplier', sortable: false, resizable: false, width: 100 },
				{ name: 'ProductName', index: 'ProductName', sorttype: "string", resizable: false, width: 180 },
				{ name: 'Energy', index: 'Energy', sortable: false, resizable: false, align: 'right', width: 60 },
				{ name: 'Duration', index: 'Duration', sorttype: "string", resizable: false, align: 'center', width: 60 },
				{ name: 'TypeContract', index: 'TypeContract', sorttype: "string", resizable: false, align: 'center', width: 60 },
				{ name: 'ActionProduct', index: 'ActionProduct', align: "left", resizable: false, align: 'center', width: 45 },
				{ name: 'TotalPrice', index: 'TotalPrice', align: "left", sorttype: "int", resizable: false, align: 'center', width: 60 },
				{ name: 'Saving', index: 'Saving', align: "left", sorttype: "int", resizable: false, align: 'center', width: 81 },
                //{ name: 'Price', index: 'Price', sorttype: "string", resizable: false },
                //{name: 'Service', index: 'Service', sorttype: "string", resizable: false, align: 'center', width: 20 },
                //{ name: 'Administration', index: 'Administration', sorttype: "float", resizable: false, align: 'center', width: 20 },
                //{ name: 'AdditionalFee', index: 'AddtionalFee', sorttype: "int", resizable: false, align: 'center', width: 20 },
				{name: 'ChangeContract', index: 'ChangeContract', sortable: false, resizable: false, width: 100 }
				],
                multiselect: false,
                loadonce: false

            });
            $('#gbox_totalList th:eq(1)').attr('title', 'De aanbieder van het contract');
            $('#gbox_totalList th:eq(2)').attr('title', 'De contractnaam');
            $('#gbox_totalList th:eq(3)').attr('title', 'Herkomst: De herkomst van elektriciteit en gas');
            $('#gbox_totalList th:eq(4)').attr('title', 'Looptijd: De looptijd van het contract');
            $('#gbox_totalList th:eq(5)').attr('title', 'Tarief: Vast of variabel tarief per kWh/m3');
            $('#gbox_totalList th:eq(7)').attr('title', 'Prijs: De totaalprijs per jaar inclusief btw');
            $('#gbox_totalList th:eq(8)').attr('title', 'Besparing: De besparing t.o.v. uw huidige contract');


            $.ajax({
                type: "POST",
                url: o.url,
                dataType: "json",
                async: true,
                success: function(data) {

                    var dataRecent = data.RowRecent;
                    for (var i = 0; i <= dataRecent.length; i++)
                        o.recentGrid.jqGrid('addRowData', i + 1, dataRecent[i]);

                    $('tr', o.recentGrid).each(function(i, item) {
                        $('td:eq(6)', item).addClass('totalprice');
                    });

                    var dataRows = data.Rows;

                    if (dataRows.length == 0) {
                        o.totalListWrapper.hide();
                        o.totalListEmpty.show();
                    }
                    else {

                        for (var i = 0; i <= dataRows.length; i++)
                            o.totalGrid.jqGrid('addRowData', i + 1, dataRows[i]);

                        $('tr', o.totalGrid).each(function(i, item) {

                            $('td:eq(2)', item).addClass('productname');
                            $('td:eq(7)', item).addClass('totalprice');
                            $('td:eq(8)', item).addClass('saving');

                            $(this).calculationSummary({
                                clickCell: $('td:eq(7)', item),
                                title: "Prijsberekening",
                                hideContractData: false,
                                url: "/ca/MyPage/MyAdvice/GetCalculationSummary/" + dataRows[i].Id + "?debug=true"
                            });
                        });

                        o.totalListEmpty.hide();
                        o.totalListWrapper.show();

                    }
                }
            });
        },
        calculationSummary: function(o) {
            if (o.url == undefined) return false;
            if (o.title == undefined) return false;
            if (o.clickCell == undefined) return false;
            if (o.hideContractData == undefined) return false;

            o.container = $(this);

            var myPopup = $("<div class='popup'></div>");
            myPopup.appendTo($('body', o.container));

            o.clickCell.click(function() {

                $.ajax({
                    type: "POST",
                    url: o.url,
                    dataType: "html",
                    async: true,
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                    },
                    success: function(data) {

                        myPopup.html(data);

                        if (o.hideContractData == true) {
                            $("#MoreInfo", myPopup).hide();
                        }
                        else {
                            o.title = $("input[name='ContractTitle']", myPopup).val();
                        }

                        $(myPopup).dialog({
                            modal: true,
                            title: o.title,
                            draggable: true,
                            resizable: false,
                            modal: true,
                            width: 620,
                            close: function(event, ui) {
                                $(this).dialog("destroy");
                            }
                        });

                    }
                })
            });
        },
        filterTable: function(o) {
            if (o.totalGrid.length == 0) return false;
            if (o.listForm.length == 0) return false;
            if (o.url == undefined) return false;
            if (o.totalListWrapper == undefined) return false;
            if (o.totalListEmpty == undefined) return false;

            var f = [];
            f.form = o.listForm;
            f.table = o.totalGrid;
            f.totalListWrapper = o.totalListWrapper;
            f.totalListEmpty = o.totalListEmpty;

            o.listForm.ajaxStart(function() {
                $(":input", $(this)).each(function(i, item) {
                    $(item).attr('disabled', 'disabled');
                })
            });

            o.listForm.ajaxStop(function() {
                $(":input", $(this)).each(function(i, item) {
                    $(item).removeAttr('disabled');
                })
            });

            if ($.browser.msie) {
                $('input:radio').click(function() {
                    this.blur();
                    this.focus();
                });
            }

            
            $(":input", this).change(function(e) {
                f.table.text('');

                $.ajax({
                    type: "POST",
                    url: o.url,
                    dataType: "json",
                    data: f.form.serialize(),
                    async: true,
                    success: function(data) {
                        var mydata = data.Rows;

                        if (mydata.length == 0) {
                            o.totalListWrapper.hide();
                            f.totalListEmpty.show();
                        }
                        else {
                            for (var i = 0; i <= mydata.length; i++)
                                f.table.jqGrid('addRowData', i + 1, mydata[i]);

                            $('tr', o.totalGrid).each(function(i, item) {

                                $('td:eq(2)', item).addClass('productname');
                                $('td:eq(7)', item).addClass('totalprice');
                                $('td:eq(8)', item).addClass('saving');

                                $(this).calculationSummary({
                                    clickCell: $('td:eq(7)', item),
                                    title: "Prijsberekening",
                                    hideContractData: false,
                                    url: "/ca/MyPage/MyAdvice/GetCalculationSummary/" + mydata[i].Id + "?debug=true"
                                });
                            });

                            f.totalListEmpty.hide();
                            o.totalListWrapper.show();

                        }
                    }
                });
            });

        }
    });
})(jQuery);

// //
if (jQuery) (function($) {
    $.extend($.fn, {
        calenderOption: function(o) {
            if (o.radioControl.length == 0) return false;
            if (o.calendarfield.length == 0) return false;

            o.calendarfield.datepicker({ clickSelect: true, dateFormat: 'dd-mm-yy', constrainInput: true });

            o.radioControl.change(function() {

                if (o.radioControl.filter(':checked').val() == "true") {
                    o.calendarfield.show();
                } else {
                    o.calendarfield.hide();
                }
            });

            o.radioControl.change();
        }
    });
})(jQuery);

//  //
if (jQuery) (function($) {
    $.extend($.fn, {
        changeContractGrid: function(o) {
            if (o.contractGrid.length == 0) return false;
            if (o.url == undefined) return false;

            o.contractGrid.jqGrid({
                datatype: "local",
                width: 886,
                height: 'auto',
                
                colNames: ['Leverancier', 'Productnaam', 'Herkomst', 'Looptijd', 'Tarief', ' ', 'Prijs', '        '],
                colModel: [
				{ name: 'Supplier', index: 'Supplier', resizable: false, sortable: false, width: 100 },
				{ name: 'ProductName', index: 'ProductName', resizable: false, sortable: false, width: 180 },
				{ name: 'Energy', index: 'Energy', resizable: false, sortable: false, align: 'center', width: 60 },
				{ name: 'Duration', index: 'Duration', resizable: false, sortable: false, align: 'center', width: 60 },
				{ name: 'TypeContract', index: 'TypeContract', resizable: false, sortable: false, align: 'center', width: 60 },
				{ name: 'ActionProduct', index: 'ActionProduct', align: "left", sorttype: "string", resizable: false, align: 'center', width: 45 },
				{ name: 'TotalPrice', index: 'TotalPrice', align: "left", resizable: false, sortable: false, align: 'center', width: 60 },
                //{ name: 'Saving', index: 'Saving', width: 81, align: "left", sorttype: "string" },
                //{name: 'Price', index: 'Price', sorttype: "string", resizable: false },
				//{name: 'Service', index: 'Service', resizable: false, sortable: false, resizable: false, align: 'center', width: 90 },
				//{ name: 'Administration', index: 'Administration', resizable: false, sortable: false, align: 'center', resizable: false, width: 90 },
				//{ name: 'AdditionalFee', index: 'AddtionalFee', resizable: false, sortable: false, align: 'center', resizable: false, width: 90 }
				{name: 'ChangeContract', index: 'ChangeContract', resizable: false, sortable: false, width: 160 }
                ],
                multiselect: false,
                loadonce: false
            });

            $('#gbox_changeContractGrid th:eq(1)').attr('title', 'De aanbieder van het contract');
            $('#gbox_changeContractGrid th:eq(2)').attr('title', 'De contractnaam');
            $('#gbox_changeContractGrid th:eq(3)').attr('title', 'Herkomst: De herkomst van elektriciteit en gas');
            $('#gbox_changeContractGrid th:eq(4)').attr('title', 'Looptijd: De looptijd van het contract');
            $('#gbox_changeContractGrid th:eq(5)').attr('title', 'Tarief: Vast of variabel tarief per kWh/m3');
            $('#gbox_changeContractGrid th:eq(7)').attr('title', 'Prijs: De totaalprijs per jaar inclusief btw');
            $('#gbox_changeContractGrid th:eq(8)').attr('title', 'Besparing: De besparing t.o.v. uw huidige contract');
            $.ajax({
                type: "POST",
                url: o.url,
                dataType: "json",
                async: true,
                success: function(data) {

                    for (var i = 0; i <= data.length; i++)
                        o.contractGrid.jqGrid('addRowData', i + 1, data[i]);

                    $('tr', o.contractGrid).each(function(i, item) {
                        $('td:eq(6)', item).addClass('totalprice');
                        
                    });
                }
            });



        }
    });
})(jQuery);



// DONTCALLME PANE //
if (jQuery) (function($) {
    $.extend($.fn, {
        dontCallMePane: function(o) {
            if (o.dontCallMeView.length == 0) return false;
            if (o.dontCallMeYes.length == 0) return false;
            if (o.dontCallMeNo.length == 0) return false;
            if (o.dontCallMeState.length == 0) return false;

            //Check state and open or close pane
            o.dontCallMeState.each(function(i, item) {
                if ($(item).val() == "open") {
                    o.dontCallMeView.show();
                } else {
                    o.dontCallMeView.hide();
                }

            });

            //add handlers for the (radio) buttons
            o.dontCallMeYes.click(function() {
                o.dontCallMeView.show(400);
            });
            o.dontCallMeNo.click(function() {
                o.dontCallMeView.hide(400);
            });
        }
    });
})(jQuery);

// LOGIN //
if (jQuery) (function($) {
    $.extend($.fn, {
        login: function(o) {
            if (o.geturl == undefined) return false;
            if (o.formId == undefined) return false;
            o.container = $(this);

            $.ajax({
                type: "GET",
                url: o.geturl,
                dataType: "html",
                async: true,
                success: function(data) {
                
                    o.container.html(data);

                    $('.buttonOranje.logon').click(function(e) {
                        $('form', o.container).each(function(i, item) {
                            $("form", o.container).submit();
                        });
                        return false;
                    });

                    $("input", o.container).keypress(function(e) {
                        if (e.which == 13) {
                            $('.buttonOranje.logon').focus().click();
                        }
                    });

                }
            });



        }
    });
})(jQuery);

// VALUEVALIDATOR //
if (jQuery) (function($) {
    $.extend($.fn, {

        valueValidator: function(o) {
            if (o.re == undefined) return false;
            o.container = $(this);

            //bind handler
            o.container.change(function() {
                if (o.re.test(o.container.val()) || o.container.val().length == 0) {
                    o.container.removeClass("field-validation-error");
                }
                else {
                    o.container.addClass("field-validation-error");
                }
            });
        }
    })
})(jQuery);

// ADDRESSLOOKUP //
if (jQuery) (function($) {
    $.extend($.fn, {

        addressLookup: function(o) {
            if (o.posturl == undefined) return false;
            if (o.zipcodeInput.length == 0) return false;
            if (o.numberInput.length == 0) return false;
            if (o.streetInput.length == 0) return false;
            if (o.cityInput.length == 0) return false;
            if (o.streetFieldInput.length == 0) return false;
            if (o.cityFieldInput.length == 0) return false;

            o.container = $(this);

            o.zipcodeInput.valueValidator({
                re: /^[0-9]{4}\s{0,2}[a-zA-z]{2}$/
            });

            o.numberInput.valueValidator({
                re: /^[0-9]{1,6}$/
            });

            lookup = function() {

                if (!(o.zipcodeInput.is('.field-validation-error') || o.zipcodeInput.val().length == 0) &&
                        !(o.numberInput.is('.field-validation-error') || o.numberInput.val().length == 0)) {

                    inputData = {
                        "zipcode": o.zipcodeInput.val(),
                        "number": o.numberInput.val()
                    }

                    $.ajax({
                        type: "POST",
                        url: o.posturl,
                        dataType: "json",
                        async: true,
                        data: inputData,
                        success: function(data) {

                            if (data.street) {
                                o.streetInput.val(data.street);
                                o.streetFieldInput.val(data.street);
                            }
                            else {
                                o.streetInput.val("");
                                o.streetFieldInput.val("");
                                alert("Postcode/huisnummer combinatie bestaat niet.");
                            }

                            if (data.city) {
                                o.cityInput.val(data.city);
                                o.cityFieldInput.val(data.city);
                            }
                            else {
                                o.cityInput.val("");
                                o.cityFieldInput.val("");
                            }
                        }
                    });

                }
                else if (o.zipcodeInput.val().length > 0 || o.numberInput.val().length > 0) {
                    o.cityInput.val("");
                    o.streetInput.val("");
                }
            }

            $(o.zipcodeInput).change(lookup);
            $(o.numberInput).change(lookup);

        }
    });
})(jQuery);

