﻿$(function() {
    $("div#searchTable ol.form li.help").addClass("quiet");
    $("div#searchTable ol.form")
		.hover(function() { $(this).children("li.help").removeClass("quiet") }, function() { $(this).children("li.help").addClass("quiet") })
		.focusin(function() { $(this).children("li.help").removeClass("quiet") })
		.focusout(function() { $(this).children("li.help").addClass("quiet") });

    $("tr.other").show();

    $("tr.other td.othergeneral a").click(function() {
        var id = $(this).attr("id").substring(13);
        $("div#modal div.dialog div.close").unbind('click');

        $("div#modal div.dialog").html('<h3></h3><h4></h4><h5></h5><div id="firstDiv"><p><img src="/dynamic/assets/img/load.gif" alt="Loading..." /></p></div><div id="secondDiv"><h5></h5><p></p></div><div class="close"><a href="#" title="Close this dialog">close</a></div>');
        $("div#modal div.dialog div.close").hide();
        ModalDialog.show("modal");
        $.getJSON('/dynamic/otherfeatures.ashx?id=' + id + '&type=general', function(data) {
            $("div#modal div.dialog h3").html(data.ProductTitle);
            $("div#modal div.dialog h4").html(data.FundTitle);
            
            //$("div#modal div.dialog h5:first").html("Special features");
            /*if (data.SpecialFeatures == "")
                $("div#modal div.dialog p:first").html("No special features have been specified for this product.");
            else
                $("div#modal div.dialog p:first").html(data.SpecialFeatures);*/
            if (data.SpecialFeatures == "") 
            {
                if (data.OtherFeatures == "") 
                {
                    $("div#modal div.dialog p:first").html("No other features have been specified for this product.");
                }
                else 
                {
                    $("div#modal div.dialog p:first").html(data.OtherFeatures);
                }
            }
            else {
                $("div#modal div.dialog p:first").html(data.SpecialFeatures);

                /*$("div#modal div.dialog h5:last").html("Other features");*/
                if (data.OtherFeatures == "")
                    $("div#modal div.dialog p:last").html("No other general treatment features have been specified for this product.");
                else
                    $("div#modal div.dialog p:last").html(data.OtherFeatures);   
            }            
            

            $("div#modal div.dialog div.close").show();
            $("div#modal div.dialog div.close").click(function() { ModalDialog.close(); if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; } });
            ModalDialog.resize();
        });

        if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; }
    });
    
    $("tr.preferredprovider td a").click(function() {
        var id = $(this).attr("id").substring(18);
        $("div#modal div.dialog div.close").unbind('click');

        $("div#modal div.dialog").html('<h3></h3><h4></h4><p><img src="/dynamic/assets/img/load.gif" alt="Loading..." /></p><div class="close"><a href="#" title="Close this dialog">close</a></div>');
        $("div#modal div.dialog div.close").hide();
        ModalDialog.show("modal");            
        $.getJSON('/dynamic/preferredprovider.ashx?id=' + id, function(data) {
            $("div#modal div.dialog h3").html(data.ProductTitle);
            $("div#modal div.dialog h4").html(data.FundTitle);
            $("div#modal div.dialog p").html(data.ProviderText);
            $("div#modal div.dialog div.close").show();
            $("div#modal div.dialog div.close").click(function() { ModalDialog.close(); if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; } });
            ModalDialog.resize();
        });

        if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; } 
    });    

    $("tr.other td.otherhospital a").click(function() {
        var id = $(this).attr("id").substring(14);
        $("div#modal div.dialog div.close").unbind('click');

        $("div#modal div.dialog").html('<h3></h3><h4></h4><p><img src="/dynamic/assets/img/load.gif" alt="Loading..." /></p><div class="close"><a href="#" title="Close this dialog">close</a></div>');
        $("div#modal div.dialog div.close").hide();
        ModalDialog.show("modal");
        $.getJSON('/dynamic/otherfeatures.ashx?id=' + id + '&type=hospital', function(data) {
            $("div#modal div.dialog h3").html(data.ProductTitle);
            $("div#modal div.dialog h4").html(data.FundTitle);

            if (data.OtherFeatures == "")
                $("div#modal div.dialog p").html("No other hospital features have been specified for this product.");
            else
                $("div#modal div.dialog p").html(data.OtherFeatures);

            $("div#modal div.dialog div.close").show();
            $("div#modal div.dialog div.close").click(function() { ModalDialog.close(); if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; } });
            ModalDialog.resize();
        });

        if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; }
    });
});

function enableSearchButton(btnSearchId) 
{    
    
    var stateList = $('#searchTable span[group="State"] input[type="radio"]');
    var adultList = $('#searchTable span[group="Adults"] input[type="radio"]');
    var dependentList = $('#searchTable span[group="Dependants"] input[type="radio"]');
    var policyList = $('#searchTable span[group="Policy"] input[type="radio"]');
    var hospitalList = $('#searchTable span[group="Hospital"] input[type="radio"]');
    var generalList = $('#searchTable span[group="General"] input[type="radio"]');
    var excessList = $('#searchTable span[group="ExcessSingle"] input[type="radio"], #searchTable span[group="ExcessDouble"] input[type="radio"]');
    var coPaymentList = $('#searchTable span[group="CoPayment"] input[type="radio"]');
    var searchButton = $(btnSearchId);
    var floatingText = $('#floatingText');
    
    var stateAnswered = false;
    var adultAnswered = false;
    var dependentAnswered = false;
    var policyAnswered = false;
    var hospitalAnswered = false;
    var generalAnswered = false;
    var excessAnswered = false;
    var coPaymentAnswered = false;
    
    stateAnswered = isGroupChecked(stateList);    
    adultAnswered = isGroupChecked(adultList);
    dependentAnswered = isGroupChecked(dependentList);
    policyAnswered = isGroupChecked(policyList);
    hospitalAnswered = isGroupChecked(hospitalList);
    generalAnswered = isGroupChecked(generalList);
    excessAnswered = isGroupChecked(excessList);
    coPaymentAnswered = isGroupChecked(coPaymentList);
    
    //alert(stateAnswered + " " + adultAnswered + " " + dependentAnswered+ " " + policyAnswered+ " " + hospitalAnswered+ " " + generalAnswered+ " " + excessAnswered+ " " + coPaymentAnswered);
    
    if (stateAnswered && adultAnswered && dependentAnswered && policyAnswered)
    {
        searchButton.removeAttr("disabled");
        floatingText.css("visibility", "hidden");
    }
    else 
    {
        searchButton.attr("disabled", "disabled");
        floatingText.css("visibility", "visible");
    }
    
}

function isGroupChecked(radioButtonList)
{
    var radioButtonAnswered = false;
    
    if (radioButtonList != null) 
    {        
        radioButtonList.each(function() {            
            if ($(this).attr("checked")) {
                radioButtonAnswered = true;
            }    
        });
    }
    else 
    {
        radioButtonAnswered = true;
    }
    
    return radioButtonAnswered;
}

function showFloatingText(btnSearchId, spFloatingText)
{

    var search = $(btnSearchId);
    var floatingText = $(spFloatingText);
    
    
    if (search.attr("disabled")==true) 
    {
    
        floatingText.css("visibility", "visible");
    }
    else 
    {
        floatingText.css("visibility", "hidden");
    }
}

function hideFloatingText(btnSearchId, spFloatingText)
{
    var search = $(btnSearchId);
    var floatingText = $(spFloatingText);
    
    
    floatingText.css("visibility", "hidden");
    
}

function logicalChoice(rbAdultsNone, rbDependantsYes, rbDependantsNo)
{
    var AdultsNone = $(rbAdultsNone);
    var DependantsYes = $(rbDependantsYes);
    var DependantsNo = $(rbDependantsNo);
    
    if (AdultsNone.attr("checked"))
    {        
        DependantsYes.attr("checked", "true");
        DependantsYes.attr("disabled", "disabled");
        DependantsNo.attr("disabled", "disabled");
    }
    else 
    {
        DependantsYes.removeAttr("disabled");
        DependantsNo.removeAttr("disabled");
    }
}

function initialListShow()
{
    
    var restrictedList = $('#searchTable span input[type="checkbox"]');
    var restrictedAnswered = isGroupChecked(restrictedList);
    var row = $('#trRestrictedList');
    
    if (row.hasClass("hide"))
    {
        if (restrictedAnswered) 
        {
            row.removeClass("hide");
        }
    }
    else 
    {
        if (!restrictedAnswered) 
        {
            row.addClass("hide");
        }
    }
}