﻿function DOMReady()
{

    /*************************************************************************************
    * Show and Hide Any Div with the class name "showDiv"
    ************************************************************************************/
    $(".showDiv").click(function (e)
    {
        var targetdiv = '#div' + e.target.id;
        if ($(targetdiv).css('display') == 'none')
        {
            // hide all other visible menus
            $(".menum").each(function ()
            {
                var elementdiv = '#' + this.id;
                if (elementdiv != targetdiv)
                    $(elementdiv).hide(500);
            });
            $(targetdiv).show(200);
        }
        else
            $(targetdiv).hide(200);
    });

    /*************************************************************************************
    * mouseleave event handler for Divs with class of name "menum"    
    ************************************************************************************/
    $(".menum").mouseleave(function (e)
    {
        if (e.target.id != "" && e.target.id != "TogglePanel")//hacked because the intial code is stupid 
        {
            var targetdiv = '#' + e.target.id;
            if ($(targetdiv).css('display') != 'none')
                $(targetdiv).hide(500);
        }
    });

    /*************************************************************************************
    * Autoformat for Date Input
    ************************************************************************************/
    $("input[name*='Date'], input[name*='_dt']").keypress(function (e)
    {
        if (e.target.value.length == 2)
            e.target.value += '/';
        if (e.target.value.length == 5)
            e.target.value += '/20';
    });
    /*************************************************************************************
    * Autoformat for Time Input
    ************************************************************************************/
    $("input[name*='Time'], input[name*='_time']").keypress(function (e)
    {
        if (e.target.value.length == 2)
            e.target.value += ':';
    });

    /*************************************************************************************
    * Clear Excel and Print from hidden field TrekGridCommand to reset behavior. 
    ************************************************************************************/
    $("input, button, image, a").focus(function (e)
    {
        if (document.forms[0].TrekGridCommand != null)
        {
            document.forms[0].TrekGridCommand.value = '';
        }
    });


    $(".Grid").keypress(function (e)
    {
        if (e.keyCode == 13)
            return false;
    });
    //set header spacer image depending on browser
    jQuery.each(jQuery.browser, function (i, val)
    {
        if (i == 'msie' && val == true) //internet exploder
        {
            $('#headerSpacer').attr({ src: '/Include/Images/topBarHeaderSpacer3ie.png' });
        }
        else if (val == true) //any other browser
        {
            $('#headerSpacer').attr({ src: '/Include/Images/topBarHeaderSpacer3.png' });
        }
    });

    $("form").submit(function (event, type)
    {
        var sourceID = '';
 
        jQuery.each(jQuery.browser, function (i, val)
        {
            if (i == 'msie' && val == true && document.activeElement != null) //internet exploder
                sourceID = document.activeElement.id
            else if (val == true && event.originalEvent != null)
                sourceID = event.originalEvent.explicitOriginalTarget.id;
        });

        //hack for the double postback the the gridview does for an excel output.
        if (0 <= sourceID.indexOf("ExcelButton"))
        {
            $("#TrekGridCommand").val("excel");
        }
        else if (0 <= sourceID.indexOf("PrinterButton"))
        {
            $("#TrekGridCommand").val("printer");
        }
    });
}

function ToggleMenu(flip)
{
    if (flip)
        $("#MenuState").val(($("#MenuState").val() == "off" ? "on" : "off"));

    var display = ($("#MenuState").val() != "off");
    if (!display)
    {
        //hide menu cell
        $("#MenuCell").attr("style", "vertical-align: top; display:none; width:0px");
        $("#jGlide_001").removeClass("jGM_box"); //("style", "display:none; position:relative;");
        $("#ContentCell").attr("style", "width:100%");
        $(".MainContainer,.HelpContainer,.FooterContainer").addClass("FullScreen");
    }
    else
    {
        $("#MenuCell").attr("style", "vertical-align: top; padding:0px;");
        $("#jGlide_001").addClass("jGM_box");
        $("#ContentCell").attr("style", "padding:0px;");
        $(".MainContainer,.HelpContainer,.FooterContainer").removeClass("FullScreen");
    }
}

function ChangeCustomer(newCust)
{
    $("#NewCustomer").val(newCust);
    $("form").trigger('submit');
}

function RequiredFields()
{
    if ($(":input[required='true'][value=], select[required='true'][selectedIndex=0]").css("background-color", "#FF1A1A").length > 0)
    {
        alert("Please fill out fields that are highlighted in red");
        return false;
    }
    else
    {
        document.forms[0].pageCommand.value = 'save';
        return true;
    }

}



var prm = Sys.WebForms.PageRequestManager.getInstance();
// Using that prm reference, hook _initializeRequest and _endRequest, to run our code at the begin and end of any async postbacks that occur.
prm.add_beginRequest(InitializeRequest);
prm.add_endRequest(EndRequest);

// Executed anytime an async postback occurs.
function InitializeRequest(sender, args)
{
    $("input[type=button], input[type=submit]").attr("disabled", "true");
}
// Executed when the async postback completes
function EndRequest(sender, args)
{
    $("input[type=button], input[type=submit]").attr("disabled", "");
    $(".Grid").keypress(function (e)
    {
        if (e.keyCode == 13)
            return false;
    });
    if ($(".MainContainer").innerWidth() < $(".Grid").innerWidth())
        $(".GridStatus td:last-child").css("width", "95%").css("text-align", "left");
}

function UpdateVisible(strName)
{
    if (document.getElementById(strName).style.visibility == "visible")
        document.getElementById(strName).style.visibility = "hidden";
    else
        document.getElementById(strName).style.visibility = "visible";
}
function HideDiv(e)
{

    if (e.target != null)//firefox
    {
        if (e.target.id != 'imgMain' || e.target.id == null)
        {
            if (document.getElementById('mainMenuDiv').style.visibility == "visible")
                document.getElementById('mainMenuDiv').style.visibility = "hidden";
        }
        if (e.target.id != 'divContextMenu' || e.target.id == null)
        {
            if (document.getElementById('divContextMenu').style.visibility == "visible")
                document.getElementById('divContextMenu').style.visibility = "hidden";
        }
    }
    if (e.srcElement != null) //IE
    {

        if (e.srcElement.id != 'imgMain' || e.srcElement.id == null)
        {
            if (document.getElementById('mainMenuDiv').style.visibility == "visible")
                document.getElementById('mainMenuDiv').style.visibility = "hidden";
        }
        if (e.srcElement.id != 'imgContext' || e.srcElement.id == null)
        {
            if (document.getElementById('contextMenuDiv').style.visibility == "visible")
                document.getElementById('contextMenuDiv').style.visibility = "hidden";
        }
    }
}


function ShowDiv(divID, imgID)
{
    if (document.getElementById(divID).style.visibility == 'visible')
    {
        document.getElementById(divID).style.visibility = 'hidden';
        document.getElementById(divID).style.display = 'none';
        document.getElementById(imgID).src = '/Include/Images/plus.gif';
    }
    else
    {
        document.getElementById(divID).style.visibility = 'visible';
        document.getElementById(divID).style.display = 'block';
        document.getElementById(imgID).src = '/Include/Images/minus.gif';
    }
}


function AutoFormat(type, control)
{
    var datestring = control.value;
    if (type == 'date')
    {
        //after the first two characters, eg, "09" add a "/", eg, "09/"
        if (datestring.length == 2)
            control.value = datestring + '/';
        //after the first 5 characters, eg, "09/05" add a "/20", eg, "09/05/"
        if (datestring.length == 5)
            control.value = datestring + '/20';
    }
    if (type == 'time')
    {
        //after the 2nd character, eg, "04" add a ":", eg, "04:"
        if (datestring.length == 2)
            control.value = datestring + ':';
    }
}

function compareDates(value1, value2)
{
    var date1, date2;
    var month1, month2;
    var year1, year2;

    month1 = value1.substring(0, value1.indexOf("/"));
    date1 = value1.substring(value1.indexOf("/") + 1, value1.lastIndexOf("/"));
    year1 = value1.substring(value1.lastIndexOf("/") + 1, value1.length);
    alert(month1 + " - " + date1 + " - " + year1);
    month2 = value2.substring(0, value2.indexOf("/"));
    date2 = value2.substring(value2.indexOf("/") + 1, value2.lastIndexOf("/"));
    year2 = value2.substring(value2.lastIndexOf("/") + 1, value2.length);
    if (year1 > year2)
        return 1;
    else if (year1 < year2)
        return -1;
    else if (month1 > month2)
        return 1;
    else if (month1 < month2)
        return -1;
    else if (date1 > date2)
        return 1;
    else if (date1 < date2)
        return -1;
    else return 0;
}

function ButtonSwitch()
{
    document.forms[0].formSubmit.disabled = true;
    document.forms[0].submit();
}
function Check(parentChk, dtgName)
{
    var elements = document.getElementsByTagName("INPUT");

    for (i = 0; i < elements.length; i++)
    {
        if (parentChk.checked)
        {
            if (IsCheckBox(elements[i]) && IsMatch(elements[i].id, dtgName))
                elements[i].checked = true;
        }
        else
            elements[i].checked = false;
    }
}

function IsMatch(id, pattern)
{
    if (id.match(pattern) == pattern)
        return true;
    else
        return false;
}
function IsCheckBox(chk)
{
    if (chk.type == 'checkbox')
        return true;
    else
        return false;
}
function ToggleDiv(targetDiv)
{
    if ($("#" + targetDiv).css('display') == 'none')
        $("#" + targetDiv).show(200);
    else
        $("#" + targetDiv).hide(200);
}
