﻿/*****************************************************************************************
* Copyright (c) 2009 Virgin Mobile
* All rights reserved.
*
* History of changes
* 
* Version        Author                      Date                Work Item
* 1.0            Nicolas Smith               3/08/2009          
* 1.1            Nicolas Smith               26/08/2009  
* 1.2            Nicolas Smith               03/09/2009          

* 
*****************************************************************************************
*
* Description of changes
*
* Version    Description
* 1.0        Initial Version
* 1.1        refactored whilst adding new broadband, handset and rateplan restrictions.
* 1.2        further refactored whilst adding new broadband restrictions.
* 
*****************************************************************************************/

$(document).ready(function() {


    //--------------------------------------------------------------------- //
    // DEFAULT SETTINGS ONLOAD PRODUCT CHOOSER
    //--------------------------------------------------------------------- //

    //////////////////////////////////////////////////////////////////////////
    // Generic Loading settings
    //////////////////////////////////////////////////////////////////////////
    $("#div" + currentSearchGroup + "Options").show();

    $("input[name='searchGroup']").each(function() {

        var search = defaultSearchGroupOptions;
        if (currentSearchGroup.length > 0) {
            var search = currentSearchGroup + options;
        }

        if ($(this).val() == search) {
            $(this).attr('checked', true);
        }
    });

    //MS AJAX attaching JQUERY POST to form submit
    $("form").not("#frmSearch").submit(function(event) {
        eval($(this).attr("onsubmit"));
        return false;
    });

    // Atttach Scroll  
    $(window).scroll(function() {

        $('#modalWindow').animate({ top: $(window).scrollTop() + "px" }, { queue: false, duration: 100 });
    });

    //////////////////////////////////////////////////////////////////////////
    // Handset Loading settings
    //////////////////////////////////////////////////////////////////////////

    $("#divHandsetOptionsSliderValue").html('<strong> '
                    + (handsetUpperPrice == handsetMaxMonthlySpend ? "ALL" : '$ ' + handsetUpperPrice) + '</strong>');

    $("input#HandsetOptionsUpperPriceRange").val(handsetUpperPrice);

    $("input[name='" + handsetOptions + "']").each(function() {
        if ($(this).val() == handsetPlanOptionCategory) {
            $(this).attr('checked', true);

            if ($(this).val() == prePaid) {
                $("#divHandsetMonthlyCost").css('display', 'none');
            }

        }
    });

    //////////////////////////////////////////////////////////////////////////
    // Rateplan Loading settings
    //////////////////////////////////////////////////////////////////////////

    $("#divRatePlanOptionsSliderValue").html('<strong> '
                    + (ratePlanUpperPrice == rateplanMaxMonthlySpend ? "ALL" : '$ ' + ratePlanUpperPrice) + '</strong>');

    $("input#RatePlanOptionsUpperPriceRange").val(ratePlanUpperPrice);

    $("input[name='" + rateplanOptions + "']").each(function() {
        if ($(this).val() == ratePlanOptionCategory) {
            $(this).attr('checked', true);

            if ($(this).val() == prePaid) {
                $("#divRatePlanMonthlyCost").css('display', 'none');
            }

        }
    });

    //////////////////////////////////////////////////////////////////////////
    // Broadband Loading settings
    //////////////////////////////////////////////////////////////////////////

    $("input[name='" + broadbandOptions + "']").each(function() {
        if ($(this).val() == broadbandOptionCategory) {
            $(this).attr('checked', true);

        }
    });


    //--------------------------------------------------------------------- //
    // Event Model Attachments
    //--------------------------------------------------------------------- //

    //////////////////////////////////////////////////////////////////////////
    // Generic Event Models
    //////////////////////////////////////////////////////////////////////////
    $("input[name='searchGroup']").click(function() {

        $("input[name='searchGroup']").each(function() {
            $("#div" + $(this).val()).hide();
        });

        $("#div" + $(this).val()).show();
        $("form").not("#frmSearch").submit();
    });

    $("input:radiobutton").click(function() {
        //Event will also attach to checkboxes.
        if ($(this).attr('name').match(attributeTypeFeature) == attributeTypeFeature) {
            setTimeout(function() { $("form").not("#frmSearch").submit(); }, 500);

        }
    });

    //////////////////////////////////////////////////////////////////////////
    // Handset Event Models
    //////////////////////////////////////////////////////////////////////////
    $("#HandsetOptionsMake").change(function() {

        $("form").not("#frmSearch").submit();
    });

    $("input[name='HandsetOptionsPlanOption']").click(function() {

        if ($(this).val() == hideSliderOption) {

            $("input#HandsetOptionsUpperPriceRange").val(handsetMaxMonthlySpend);
            $("#divHandsetMonthlyCost").css('display', 'none');

        } else {

            if ($(this).val() == all) {

                handsetAllSearch = true;

                $("input#HandsetOptionsUpperPriceRange").val(handsetMaxMonthlySpend);
                $("#divHandsetOptionsSliderValue").html('<strong> ALL </strong>');
                // triggers event
                $("#divHandsetOptionsSlider").slider('value', handsetMaxMonthlySpend);
                selectAllPlanOptionsByName('HandsetOptionsPlanOption');

            } else {

                $("input#HandsetOptionsUpperPriceRange").val($("#divHandsetOptionsSlider").slider('value'));
            }
            $("#divHandsetMonthlyCost").css('display', 'block');
        }
        setTimeout(function() { $("form").not("#frmSearch").submit(); }, 100);
    });

    //////////////////////////////////////////////////////////////////////////
    // RatePlan Event Models
    //////////////////////////////////////////////////////////////////////////
    $("input[name='RatePlanOptionsPlanOption']").click(function() {

        if ($(this).val() == hideSliderOption) {

            $("input#RatePlanOptionsUpperPriceRange").val(rateplanMaxMonthlySpend);
            $("#divRatePlanMonthlyCost").css('display', 'none');

        } else {
            if ($(this).val() == all) {

                rateplanAllSearch = true;
                $("input#RatePlanOptionsUpperPriceRange").val(rateplanMaxMonthlySpend);
                $("#divRatePlanOptionsSliderValue").html('<strong> ALL </strong>');

                // triggers event
                $("#divRatePlanOptionsSlider").slider('value', rateplanMaxMonthlySpend);
                selectAllPlanOptionsByName('RatePlanOptionsPlanOption');

            } else {

                $("input#RatePlanOptionsUpperPriceRange").val($("#divRatePlanOptionsSlider").slider('value'));
            }
            $("#divRatePlanMonthlyCost").css('display', 'block');
        }
        setTimeout(function() { $("form").not("#frmSearch").submit(); }, 100);
    });

    //////////////////////////////////////////////////////////////////////////
    // Broadband Event Models
    //////////////////////////////////////////////////////////////////////////
    $("input[name='BroadbandOptionsPlanOption']").click(function() {

        setTimeout(function() { $("form").not("#frmSearch").submit(); }, 100);
    });

    //////////////////////////////////////////////////////////////////////////
    // END JQUERY
    //////////////////////////////////////////////////////////////////////////
});  // JQUERY END $(document).ready(function()

  //--------------------------------------------------------------------- //
  // NON-JQUERY AJAX-DISPLAY HANDLING FUNCTIONS
  //--------------------------------------------------------------------- //

   function SelectRadioButton(href, selectedOption) {
    
        $('div.MenuTabs ul.productChooserNavigationTab a.selected').removeClass('selected');
        $(href).addClass('selected');
        //click the radio button to force re-selection
        $("input[name='searchGroup'][value='" + selectedOption + "']").attr('checked', true);
        $("input[name='searchGroup'][value='" + selectedOption + "']").click();

    }

    function selectAllPlanOptionsByName(optionsName) {
    
        $("input[name='"+optionsName+"']").each(function() {

            if ($(this).val() == all) {
                $(this).attr('checked', true);
            }
        });
    }
    
   

    function displayRatePlanDetails(div, button) {

        if ($(div).css('display') == 'none') {
            $(div).fadeIn(500).css('display', 'block');
            $(button).attr("class", "minusSign");
            

        } else {
            $(div).fadeOut("slow");
            $(button).attr("class", "plusSign");
        }
        
        
    }
    
