//Javascript functions for county selector and show/hide for various classes

//Show/hide classes based on county selected
function limitByArea(area) {
    //Show/hide area specific data
    for (var i=0; i<=7; i++) {
        if (area == i) {
            $('.area'+i).show();
        }
        else {
            $('.area'+i).hide();
        }
    }
    if (area == 6) {
        $('.plan1').hide();
        $('.plan2').hide();
        $('.plan3').hide();
        $('.plan4').show();
        //hide the bloody checkboxes as well
        $('.planCompare').hide();
        $('.planTable').show();
        $('.noPlans').hide();
    }
    else if (area == 7) {
        $('.plan1').hide();
        $('.plan2').hide();
        $('.plan3').hide();
        $('.plan4').hide();
        //Hide entire plan comparison table
        $('.planCompare').hide();
        $('.planTable').hide();
        $('.noPlans').show();
    }
    else {
        //show everything hidden above
        $('.plan1').show();
        $('.plan2').show();
        $('.plan3').show();
        $('.plan4').show();
        //hide the bloody checkboxes as well
        $('.planCompare').show();
        $('.planTable').show();
        $('.noPlans').hide();
    }
    $('.stepOne').hide();
    $('.stepTwo').show();
}

//Initialize the county selector and set cookie
function initialize_county_selector() {
    //initialize tables and show/hide steps
    var county = $(document).getUrlParam("county"); //read from param if present
    if (county == null) { //read from cookie if present
        county = $.cookie('county');
    }
    if (county > 0) {
        $.cookie('county', county, {path: '/'}); //set cookie
        //sel = $('[name="select-county"]');
        sel = document.getElementById("select-county")
        sel.options[0].selected = false;
        sel.options[county].selected = true;
        limitByArea(sel.options[county].value)
        $('.stepOne').hide();
        $('.stepTwo').show();
    }
    else {
        $.cookie('county', null); //clear cookie 
        $('#county-selector').show(); 
        $('.planCompare').hide();
        $('.planTable').hide();
        $('.stepTwo').hide();
    }

    $('#select-county').change(function() {
        $.cookie('rates', '', {path: '/'});  //clear the medigap rate cookie
        $.cookie('county', this.selectedIndex, {path: '/'});
        limitByArea(this.value);
        window.focus(); //Make IE give up focus on the select
    });
}

//store compare selections as single cookie where value is binary coded decimal representation of 4 boolean checkboxes
// possible values 0-15
function refresh_compare_cookies() {
    var bcd_val = 0;
    for (i=1;i<=4;i++) {
        if ($('#compare-'+i).is(':checked')) {
            bcd_val = bcd_val + Math.pow(2,(i-1)); //the values 2^0, 2^1, 2^2, 2^3 - (1, 2, 4, 8)
        }
    }
    $.cookie('comparecode', bcd_val, {path: '/'}); //set cookie
}

//The following functions are used by the compare page (/insurance-mi/mapd/compare

//Show/hide comparison table columns based on the checkboxes
function refresh_table() {
    for (i=1;i<=4;i++) {
        var column = i+1;
        if ($('#select-plan-'+i).is(':checked')) {
            $('th:nth-child('+ column +'), td:nth-child('+ column +')').show();
        }
        else {
            $('th:nth-child('+ column +'), td:nth-child('+ column +')').hide();
        }
    }
}

//select plans for comparison based on county (called only from compare page)
function limit_comparison_by_area(area){
    if (area == 6) {
        //uncheck everything except PriorityMedicare Choice
        for (i=1;i<=3;i++) {
            if ($('#select-plan-'+i).is(':checked')) {
                $('#select-plan-'+i).attr('checked', false);
            }
        }
        $('#select-plan-4').attr('checked', true);
    }
    else {
        //check all plans
        for (i=1;i<=4;i++) {
            if (! $('#select-plan-'+i).is(':checked')) {
                $('#select-plan-'+i).attr('checked', true);
            }
        }
    }
    refresh_table();
}

//deselect plans according to comparison cookie if set
function limit_by_cookies(){ 
    var comparecode = $.cookie('comparecode');
    if (comparecode > 0) {  //don't deselect any if none are checked (value is 0)
        for (i=1;i<=4;i++) {
            if (! (comparecode & Math.pow(2,(i-1)))) {  //bitwise comparison with 2^0, 2^1, 2^2, 2^3
                $('#select-plan-'+i).attr('checked', false);
            }
        }
    }
}

//Initialize the rate divs from param and set cookie
function initialize_rates() {
    var rates = $(document).getUrlParam("rates"); //read from param if present
    if (rates == null) { //read from cookie if present
        rates = $.cookie('rates');
    }
    if (rates) {
        var ra = unescape(rates).split('|');
        $(ra).each(function(index,rate) {
            if (! rate.match(/^((\$\d+)|(\w))$/)) {
                ra[index] = '';
            } 
        });
        if (ra.length == 9) {
            var ratetype = ra[0];
            if (ratetype == 'A') {
                $('.rateDesc').html('(<a class="definition" href="/glossary#prepre">Preferred&nbsp;premium</a> /<br /><a class="definition" href="/glossary#nonstd">Non-standard&nbsp;premium</a>)');
                $('.rateA').html(ra[1] +'/'+ ra[2]);
                $('.rateD').html(ra[3] +'/'+ ra[4]);
                if (ra[5] == 'N/A') {   //Format nicely instead of displaying N/A twice for both base and special
                    $('.rateF').html('N/A');
                }
                else {
                    $('.rateF').html(ra[5] +'/'+ ra[6]);
                }
                $('.rateN').html(ra[7]+'/'+ra[8]);
                $('.norates').hide();
            }
            else if (ratetype == 'B') {
                $('.rateDesc').html('(<a class="definition" href="/glossary#prepre">Preferred&nbsp;premium</a>)');
                $('.rateA').html(ra[1]);
                $('.rateD').html(ra[3]);
                $('.rateF').html(ra[5]);
                $('.rateN').html(ra[7]);
                $('.norates').hide();
            } 
            else if (ratetype == 'S') {
                $('.rateDesc').html('(<a class="definition" href="/glossary#nonstd">Non-standard&nbsp;premium</a>)');
                $('.rateA').html(ra[2]);
                $('.rateD').html(ra[4]);
                $('.rateF').html(ra[6]);
                $('.rateN').html(ra[8]);
                $('.norates').hide();
            } 

            $.cookie('rates', rates, {path: '/'});  //set cookie
        }
        else {
            $('.rateDesc').html('');
            $.cookie('rates', '', {path: '/'});  //clear cookie (null doesn't clear FF3 w/ "save tabs" setting)
        }
    }
    else {
        $('.rateDesc').html('');
        $.cookie('rates', '', {path: '/'});  //clear cookie
    }
}

//Override form submit to POST to IKA enrollment app using the proper PBP value (based on plan type and service area)
function initialize_ika_form () {
    $('.ika-form').submit(function() {
        //Find service area from county selector
        sel = document.getElementById("select-county")
        ind = sel.selectedIndex;
        var area = sel.options[ind].value;
        //Find plan type from HTML hidden form field
        var planType = this.PlanType.value;
        if ((planType >=0) && (planType < 5)) { //planType corresponds to the 5 Medicare plans: 0=Value, 1=Medicare, 2=Plus, 3=Choice, 4=Rx
            if ((area > 0) && (area <=7)) { //service areas: 0=no county selected, 1-6 are valid service areas, 7=all other MI counties (Rx only)
                var lookup = new Array(5);
                //PriorityMedicare Value
                lookup[0] = new Array();
                lookup[0][1] = '015';
                lookup[0][2] = '011';
                lookup[0][3] = '012';
                lookup[0][4] = '018';
                lookup[0][5] = '021';
                //PriorityMedicare
                lookup[1] = new Array();
                lookup[1][1] = '013';
                lookup[1][2] = '007';
                lookup[1][3] = '008';
                lookup[1][4] = '016';
                lookup[1][5] = '019';
                //PriorityMecicare Plus
                lookup[2] = new Array();
                lookup[2][1] = '014';
                lookup[2][2] = '009';
                lookup[2][3] = '010';
                lookup[2][4] = '017';
                lookup[2][5] = '020';
                //PriorityMecicare Choice
                lookup[3] = new Array();
                lookup[3][1] = '001';
                lookup[3][2] = '003';
                lookup[3][3] = '005';
                lookup[3][4] = '007';
                lookup[3][5] = '009';
                lookup[3][6] = '003'; //Choice is only plan avail to service area 6, same code/rate as service area 2
                //PriorityMecicare Rx
                lookup[4] = new Array();
                lookup[4][1] = '001';
                lookup[4][2] = '001';
                lookup[4][3] = '001';
                lookup[4][4] = '001';
                lookup[4][5] = '001';
                lookup[4][6] = '001';
                lookup[4][7] = '001';

                //lookup PBP based on planType and service area, then submit the form
                var pbp = lookup[planType][area];
                if (pbp) {
                    this.PBP.value = pbp
                    //For testing purposes:
                    //alert("Submitting with Contract="+this.Contract.value+", PBP="+pbp+", Plan="+planType+", Service Area="+area);
                    return true;
                }
                else {
                    alert("Sorry, this plan is not available in your county.");
                }
            }
            else if ("area == 0") {
                alert("In order to enroll, you must first select your county.");
            }
        }
        else {
            alert("Sorry. Unable to submit form without planType.");
        }
        return false;
    });
}
