﻿/*************************************************************************************************
JQUERY EXISTS FUNCTION
*************************************************************************************************/
jQuery.fn.exists = function() { return jQuery(this).length > 0; }


/*************************************************************************************************
LOAD FUNCTIONS
*************************************************************************************************/

//Partial Postback
function pageLoad(sender, args) {
    if (args.get_isPartialLoad()) {
        if ($('.Modal').exists()) {
            Overlay();
            IE6_Resize_Overlay('#Overlay');
        } else {
            JS_Overlay_Toggle(false);
        }
        
        if ($('#Modal').exists()) {
            JS_Modal_Toggle(false);
        }

        DatePickerChk();
        ZebraChk();
        TutorGrpChk();
        WidthChk();
    }
}

//Normal Page Load
$(document).ready(function() {
    if ($('.Modal, #Modal').exists()) {
        Overlay(); //Location: Below
        IE6_Resize_Overlay('#Overlay');
    }
    DatePickerChk();
    ZebraChk();
    TutorGrpChk();
    WidthChk();
});


/*************************************************************************************************
DATEPICKER
*************************************************************************************************/
function DatePickerChk() {
    if ($('input.Date').exists()) {
        BindDateControl();
    }

    if ($('input.SimpleDate').exists()) {
        $(".SimpleDate").datepicker({ dateFormat: 'dd/mm/yy' });
    }
}

//Binding the datepicker
function BindDateControl() {
    $.ajax({
        type: "POST",
        url: "AjaxPostBack.aspx/GetFullAppRoot",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $('input.Date').datepicker({
                dateFormat: 'dd/mm/yy',
                changeMonth: true,
                changeYear: true,
                showOn: 'button',
                buttonImage: msg.d + 'appcontrols/DatePicker/calendar.png',
                buttonImageOnly: false,
                buttonText: 'Show Calender >>',
                // bug fix - Jquery datepicker problem on date select in IE - 'length' is null or not an object.
                onSelect: function() {
                    $(this).trigger("change");
                }
            });
        }
    });
}



/*************************************************************************************************
ZEBRA STUFF HERE
*************************************************************************************************/
function ZebraChk() {
    $('table.Zebra').each(function() {
        $("tr:odd", this).addClass("Darker");
        $("tr:even", this).removeClass("Darker");
    });

    $('div.Zebra').each(function() {
        $("div:odd", this).addClass("Darker");
        $("div:even", this).removeClass("Darker");
    });
}

// <summary>
// This function applys the zebra to a element specified within the first parameter. The second parameter if set to true will remove all classes named 'Darker' and then apply the same class back to a clean slate.
// </summary>
function ApplyZebra(strElement, blnCleanup) {
    if (blnCleanup == true) {
        $(strElement).removeClass("Darker");
    }

    $(strElement + ":odd").addClass("Darker");
}


/*************************************************************************************************
TUTOR GROUP CHECK (paperwork management)
*************************************************************************************************/

// This was developed to tackle a problem with the updatepanel not updating the filter to show the tutor group drop down!
function TutorGrpChk() {
    if ($('select.jCheckSchool').exists()) {
        School = $('select.jCheckSchool option:selected').val();
        Group = $('select.jCheckGrp option').length;

        //if the school dropdown isn't set to 'ALL'
        if (School != -1) {
            if (Group > 1) {
                $('.jCheckGrp').parent('div').fadeIn('fast');
                ApplyZebra("div.FiltersPanelContent div div.Input", true);
            } else {
                $('.jCheckGrp').parent('div').fadeOut('fast');
            }
        }
    }
}

/*************************************************************************************************
OVERLAY
*************************************************************************************************/
// <summary>
    // If IE6 then make the overlay the full size of the screen (Cam) - been modified to take a parameter so it resizes either the Overlay or Modal
// </summary>
function IE6_Resize_Overlay( strElement ) {
    if ($.browser.msie && $.browser.version == "6.0") {
        $('select').hide();  // Select elements appear above the overlay (might need further testing - to make sure that the select elements show again on a partial render)
        $(strElement)
        {
            var overlayHeight = $('body').height();
            $(strElement).css({ 'height': overlayHeight });
        }
    }
}

// <summary>
    // Builds and shows the overlay (this is built on the document and will appear in the generated source code at the bottom)
// </summary>
function Overlay() {
    JS_Overlay_Build(); //Location: Mbox.js
    JS_Overlay_Toggle(true); //Location: Mbox.js
}

function JS_Overlay_Build(blnDisplay) {

    if (!$('#Overlay').exists()) {
        var objModal_Overlay = document.createElement("div");
        objModal_Overlay.setAttribute('id', 'Overlay');
        document.body.appendChild(objModal_Overlay);
        objModal_Overlay.style.display = "none";
    }
}

function JS_Overlay_Toggle(blnDisplay) {
    if (!blnDisplay) blnDisplay = false;
    objModal_Overlay = document.getElementById("Overlay");

    if (blnDisplay == true) {
        objModal_Overlay.style.display = "block";
    }
    else {
        JS_Overlay_Hide();
    }
}

function JS_Overlay_Hide() {
    objModal_Overlay = document.getElementById("Overlay");
    if (objModal_Overlay != null) {
        objModal_Overlay.style.display = "none";
    }
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
/// TABLE WIDTH CHECK - To see if a table is breaking out of a containing div and floating down the page
//-------------------------------------------------------------------------------------------------------------------------------------------------

function WidthChk() {

    if ($('.Jquery_IeSixTable, .Jquery_IeSixTable_two').exists()) {

        if (($.browser.msie && $.browser.version == "6.0")) {
        
            var mainContainer = "";

            if ($('.Jquery_IeSixTable_two').exists()) {
                mainContainer = "div.InnerShell";
            } else {
                mainContainer = "div.MainContentHolder";
            }
        
            var TableWidth = $('.Jquery_IeSixTable, .Jquery_IeSixTable_two').width();
            var LeftColWidth = $('div.LeftColumn').width();
            var screenWidth = $('div.ContentHolder').width();
            var TotalWidth = TableWidth + LeftColWidth;

            if (TotalWidth + 40 >= screenWidth) {

                if ($('.jQuery_WidthMsg').exists()) {
                    $('.jQuery_WidthMsg').show();
                }


                TotalWidth = TotalWidth + 100;
                $(mainContainer).css("width", TotalWidth + "px");

                if ($.browser.msie && $.browser.version == "6.0") {
                    $("div.Header, div.SubMenu").css("width", TotalWidth + 100 + "px"); // this is a IE6 fix  (ie 6 doesn't support the fixed header)
                }

            }

        }
    }
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
/// Do Post Back
//-------------------------------------------------------------------------------------------------------------------------------------------------

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
