$(document).ready(function()
{
     bindPageEvents();
     toggleAdvanceAgendasearch();
    $('.SearchByCountry').change(function()
    {
        if (1 == $(this).attr('showState')) {
            var recId = this.id.replace('SearchByCountry_', '');
            $.get('/states/get_states/' + this.options[this.selectedIndex].value, function(response)
            {
                $('#SearchByState_' + recId).html(response);
            });
        }
    });

    // The common function to clear search form.
    $('.ClearSearch').click(function(){
        clearForm(this.form);
    });

        // When the user selects an parent category option , we display its child caregory a
    $('#ProductSearchProductCategoryId').change(function(){
            var selProdCatId = $('#ProductSearchProductCategoryId').val();
            // Send AJAX request to change status for title
            $.get('/products/get_first_level_cats/' + selProdCatId, function(response)
            {
                $("#product_sub_cat").html(response);
                bindSubCategoryChangeEvent();
            });

            // Send AJAX request to change status for the manufacturer for this category
            $.get('/prodlib_categories/get_product_cat_manufacturers/' + selProdCatId, function(response)
            {
                $("#ProductSearchManufactureId").html(response);
            });
            });

    bindSubCategoryChangeEvent();

     // Set the date format
    Date.format = datePickerDateFormat;

    // Apply date picker
    $('.date-pick').datePicker(
    {
        startDate: datePickerStartDate
    });




});

function clearForm(form) {
    // iterate over all of the inputs for the form
    // element that was passed in
    $(':input', form).each(function() {
    var type = this.type;
    var tag = this.tagName.toLowerCase(); // normalize case
    // it's ok to reset the value attr of text inputs,
    // password inputs, and textareas
    if (type == 'text' || type == 'password' || tag == 'textarea')
           this.value = "";
         // checkboxes and radios need to have their checked state cleared
         // but should *not* have their 'value' changed
     else if (type == 'checkbox' || type == 'radio')
           this.checked = false;
        // select elements need to have their 'selectedIndex' property set to -1
        // (this works for both single and multiple select elements)
     else if (tag == 'select')
           this.selectedIndex = 0;
  });
};

function bindSubCategoryChangeEvent() {
    // When the user selects an sub category option , we display the applicable manufacturers
    $('#ProductSearchProductSubCatId').change(function(){

            var selProdSubCatId = $('#ProductSearchProductSubCatId').val();

            // If the user has use the '-- Please Select --' option
            if ('' == selProdSubCatId) {
                var selProdSubCatId = $('#ProductSearchProductCategoryId').val();
            }

            // Send AJAX request to change status for title
            $.get('/prodlib_categories/get_product_cat_manufacturers/' + selProdSubCatId, function(response)
            {
                $("#ProductSearchManufactureId").html(response);
            });
    });
}

// Function to bind events for page
function bindPageEvents()
{
    // When any pagination link is clicked
    $(".calendarlink").click(function()
    {
        // Send AJAX request to get page
        $.get(this.href, function(response)
        {
            $("#calendarDiv").html(response);
            bindPageEvents();
        });

        // By default return false
        return false;
    });

     // When any pagination link is clicked
    $("#ldtMonth, #ldtYear").change(function()
    {
         var selMonth = parseInt($('#ldtMonth').val()) +1;
          var selYear = $('#ldtYear').val();

        // Send AJAX request to get page
        $.get(baseUrl + 'agendas/pick_a_day/ldt:' + selYear + '-' + selMonth  , function(response)
        {
            $("#calendarDiv").html(response);
            bindPageEvents();
        });

        // By default return false
        return false;
    });

    }
// Function to toggle advance agenda search
function toggleAdvanceAgendasearch()
{
    // When any pagination link is clicked
    $(".toggleDiv").click(function()
    {
            $(".agendaSearch").toggle();

     });
     }

