﻿/// <reference path="/JavaScript/jquery-1.3.2-vsdoc.js"/>


//Should validate against the North American Numbering Plan
var defaultPhoneRegEx = /^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/;
var defaultPhoneRegMsg = "North American Numbering Plan #-###-###-###x####";
//Validate emails: http://xyfer.blogspot.com/2005/01/javascript-regexp-email-validator.html
var defaultEmailRegEx = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
var defaultEmailRegMsg = "user@domain.com";

//Used so IE doesn't fire the resize event forever
var curWindowHeight;
var curWindowWidth;
var resizeCount = 0;
var resizeTimer = null;
var isResizing = false;
var gridClassName = ".fullWidthTable";
var gridOptionStretch = true;
var gridColumnWidths = new Array();
$(document).ready(function () {
    $("input[type=text]").css("background-color", "#ffffff");
    $("input[type=select]").css("background-color", "#ffffff");

    //Make the parent form visible, so content can be arranged
    $("#aspnetForm").css("display", "block");
});

//Run this function on a timer so we don't infinate loop when resizing in certain browsers
//This function does need to run at least twice for consistant results across browsers
function resizePage() {
    //Don't run this function again if it's already running
    if (isResizing) { return; }
    //alert("RESIZE trying : " + resizeCount + " isResizing : " + isResizing + " windowWidth(): " + windowWidth() + " windowHeight(): " + windowHeight() + " curWindowHeight: " + curWindowHeight + " curWindowWidth: " + curWindowWidth);
    resizeCount = resizeCount + 1;
    if ((curWindowHeight != windowHeight() || curWindowWidth != windowWidth()) && resizeCount < 2) {
        isResizing = false;
        resizeTimer = setTimeout('setStickyFooter()', 100);
    } else if ((curWindowHeight != windowHeight() || curWindowWidth != windowWidth()) && resizeCount >= 2) {
        resizeTimer = setTimeout('setStickyFooter()', 100);
        resizeCount = 0;
    }
}

//Set the footer to the bottom of the window
function setStickyFooter() {
    //alert("trying : " + resizeCount + " isResizing : " + isResizing + " windowWidth(): " + windowWidth() + " windowHeight(): " + windowHeight() + " curWindowHeight: " + curWindowHeight + " curWindowWidth: " + curWindowWidth);
    if (curWindowHeight == windowHeight() && curWindowWidth == windowWidth()) { return; }

    if (isResizing) { return; }
    isResizing = true;
    resizeCount = resizeCount + 1;
    if (curWindowHeight == windowHeight() && curWindowWidth == windowWidth() && resizeCount >= 10) {
        resizeTimer = null;
        resizeCount = 0;
    } else {
        resizeTimer = setTimeout('setStickyFooter()', 100);
    }
    curWindowHeight = windowHeight();
    curWindowWidth = windowWidth();

    //Make screen not visible (do first)
    setCssForEachOfClass(".outerContainer", "visibility", "hidden");

    //Figure out height requirements (start chain)
    setOuterContainerHeightReqs(true);
}

//Set the height reqs for the width and height functions
//Because we need to account for scroll bars
var origHeight = 0; //This is the minimum height of the content-div
var minHeight = 0; //min height the window needs to be before pulling down the footer
var isUsingMinHeight = false; //are we using the minimum height (means there is a veritcal scrollbar)
var cssBoxHeight = 0; //the sum border padding and margin of the content boxes
var allBoxHeight = 0;
var nonContentHeight = 0; //the height of real-estate that is not the content div
function setOuterContainerHeightReqs(chain) {
    minHeight = 0;
    isUsingMinHeight = false;
    allBoxHeight = 0;
    cssBoxHeight = 0;
    nonContentHeight = 0;

    $(".outerContainer").each(function () {
        allBoxHeight += cssOuterBoxSize(this, "height") + $(this).height();
        cssBoxHeight += cssOuterBoxSize(this, "height");
        if (this.id != "content") {
            minHeight += cssOuterBoxSize(this, "height") + $(this).height();
            nonContentHeight += cssOuterBoxSize(this, "height") + $(this).height();
        } else {
            if (origHeight == 0) {
                origHeight = $("#content").height() - cssOuterBoxSize($("#content"), "height");
            }

            if ($(gridClassName).height() != 0) {
                minHeight += $(gridClassName).height() + cssOuterBoxSize($("#content"), "height");
            } else {
                minHeight += origHeight + cssOuterBoxSize($("#content"), "height");
            }

        }
    });

    if (minHeight >= windowHeight()) {
        isUsingMinHeight = true;
    }

    if (chain) {
        //Set container widths (continue chain)
        setOuterContainerWidth();
    }
}

//Make container divs 100% wide
var minWindowWidth = 960;
var isUsingMinWidth = false;
function setOuterContainerWidth() {
    var excessAmount = 0;

    $(".outerContainer").each(function () {
        //Is there a vertical scroll bar? If so, don't add content under it
        if (windowWidth() > minWindowWidth) {
            excessAmount = 0;
            if (isUsingMinHeight) {
                if ((allBoxHeight + excessAmount - cssBoxHeight - $("#footer").height() + cssOuterBoxSize($("#footer"), "height")) < minHeight) {
                    //excessAmount = scrollBarWidth();
                } else if ((allBoxHeight + excessAmount - cssBoxHeight - $("#footer").height() + cssOuterBoxSize($("#footer"), "height")) > minHeight + 100) {
                    if ($.browser.mozilla) {
                        //excessAmount = scrollBarWidth();
                    }
                }
            }
            if ($.browser.msie && $.browser.version < 7) { excessAmount = -17; }
            $(this).css("width", windowWidth() - cssOuterBoxSize(this, "width") - excessAmount);
            isUsingMinWidth = false;
        } else {
            excessAmount = 0;
            if (isUsingMinHeight) {
                if (!$.browser.mozilla) {
                    excessAmount = scrollBarWidth();
                }
            }

            if ($.browser.msie && $.browser.version < 7) { excessAmount = -17; }
            //We know there is a vertical scroll bar, so don't put content behind it
            $(this).css("width", minWindowWidth - cssOuterBoxSize(this, "width") - excessAmount);
            isUsingMinWidth = true;
        }
    });

    //Set container heights
    setOuterContainerHeight(); //continue chain
}

//Make content divs 100% high
function setOuterContainerHeight() {
    var excessAmount;

    if (minHeight > (windowHeight() - 165)) { isUsingMinHeight = true; }

    if (!isUsingMinHeight) {
        excessAmount = 0;
        if (!isUsingMinWidth) {
            //excessAmount = scrollBarWidth();
        }
        $(".stickyFooter").css("top", windowHeight() - cssOuterBoxSize($("#footer"), "height") - $("#footer").height() - excessAmount);

    } else {
        excessAmount = 0;
        if (!isUsingMinWidth) {
            excessAmount = scrollBarWidth();
        }

        if ($.browser.opera) {
            var newMinHeight = 0;
            $(".outerContainer").each(function () {
                newMinHeight += cssOuterBoxSize($(this), "height") + $(this).height();
            });

            //Content height increases
            if (newMinHeight > minHeight) {
                excessAmount += (newMinHeight - minHeight);
            }
        }

        $(".stickyFooter").css("top", (allBoxHeight + excessAmount - cssBoxHeight - $("#footer").height() + cssOuterBoxSize($("#footer"), "height")));

        if (parseInt($(".stickyFooter").css("top")) < minHeight) {
            $(".stickyFooter").css("top", minHeight - cssOuterBoxSize($("#footer"), "height"));
        }
    }

    //Fix Grid widths
    if (gridClassName != null) {
        resizeGrid(gridClassName, gridOptionStretch);
    }


    //Center navigation
    centerThis('#navInnerDiv');

    //Make screen visible (do last)
    setCssForEachOfClass(".outerContainer", "visibility", "visible");

    //End resizing
    isResizing = false;
}












/*Common Functions*/
////GRID FUNCTIONS ///
//Just call resize grid with the class of your table
function resizeGrid(gridClass, stretch) {
    var endWidth = $("#content").width() + cssOuterBoxSize($("#content"), "width") + cssOuterBoxSize($("#content"), "width");

    //Don't stretch to page width
    if (!stretch) {
        endWidth = $(gridClass).width() + cssOuterBoxSize($(gridClass), "width");
    }
    stretchGrid(gridClass, endWidth);
}
//Stretch a grid to the available content width
function stretchGrid(gridClass, endWidth) {
    var origEndWidth = endWidth;
    var widths = new Array();
    var curWid = 0;
    var intX = 0;
    $("th.fixedColumnWidth").each(function () {
        if (intX + 1 == $("th.fixedColumnWidth").size()) {
            $(this).css("padding-right", 5);
        }
        $(this).css("width", 20);
        endWidth = endWidth - 20;
        intX++;
    });
    intX = 0;
    $("th.flexColumnWidth").each(function () {
        if (gridColumnWidths[intX] != null) {
            //Use percent widths on resize columns
            curWid = origEndWidth * (gridColumnWidths[intX] * ".01");
        } else {
            //Keep halfing until all space is used
            if (intX + 1 == $("th.flexColumnWidth").size()) { endWidth = endWidth * 2; }
            curWid = endWidth / 2;
        }
        $(this).css("width", curWid);
        widths[intX] = curWid;
        endWidth = endWidth - (curWid);
        intX++;
    });
    intX = 0;
    $("td.flexColumnWidth").each(function () {
        $(this).css("width", widths[intX]);
        endWidth = endWidth - widths[intX];
        intX++;
    });

    //Fix Browser quirks
    fixGrid(gridClass, origEndWidth);
}
//Fix quirks in a semantic grid across browsers
function fixGrid(gridClass, endWidth) {
    //Fix the 1px - short caption issue in FireFox, Opera and Safari
    if ($.browser.mozilla) {
        $(gridClass + " caption").css("width", endWidth - (cssOuterBoxSize($(gridClass + " caption") * 2), "width"));
        $(gridClass + " caption").css("margin-left", "-1px");
    }
    else if ($.browser.safari || $.browser.opera) {
        $(gridClass + " caption").css("width", endWidth - cssOuterBoxSize($("#content"), "width") - (34 * 2) - 5);
    }
}
//Set all cells in row to a color
function setGridRowColor(obj, color) {
    $.each($(obj).children("td"), function () {
        $(this).css("background-color", color);
    });
    $.each($(obj).children("th"), function () {
        $(this).css("background-color", color);
    });
}

//Normal, Alt, Hover, Selected, SelectedHover
var gridRowColors = new Array("#ffffff", "#eaf4fd", "#6ea6d1", "#fbec88", "#fbec00");
var isGridWired = false;
//Color our Grid
function colorAndWireGrid(gridClass) {
    if (!isGridWired && $(gridClass + " thead tr th .chkAll").children("input").length >= 0) {
        //Wire up check all rows checkbox
        $(gridClass + " thead tr th .chkAll").children("input").toggle(function () {
            $(gridClass + " tbody tr th .chkRow").children("input").attr('checked', true);
            colorAndWireGrid(gridClass);
        }, function () {
            $(gridClass + " tbody tr th .chkRow").children("input").attr('checked', false);
            colorAndWireGrid(gridClass);
        });
    }

    //Wire up rest of grid
    var intZ = 0;
    var curColor = gridRowColors[0];
    var hoverColor = gridRowColors[2];
    if ($(gridClass + " tbody tr th .chkRow").length == 0) {
        $.each($(gridClass + " tbody tr"), function () {
            if (intZ % 2 == 0) {
                curColor = gridRowColors[0];
            } else {
                curColor = gridRowColors[1];
            }
            hoverColor = gridRowColors[2];

            //Zebra stripe and hook up tds hover and click events
            $.each($(this).children("td"), function () {
                $(this).css("background-color", curColor);
                if (!isGridWired) {
                    //row click option
                    //                    $(this).bind('click', function(e) {
                    //                        $(gridClass + " tbody tr th").children("editRow").click();
                    //                    });
                    $(this).mouseenter(function () {
                        setGridRowColor($(this).parent(), gridRowColors[2]);
                    });
                    $(this).mouseleave(function () {
                        if (($(this).parent().children("th").children(".txtRow").val() - 1) % 2 == 0) {
                            curColor = gridRowColors[0];
                        } else {
                            curColor = gridRowColors[1];
                        }
                        setGridRowColor($(this).parent(), curColor);
                    });
                }
            });

            //Hook up hover events for th cells
            $.each($(this).children("th"), function () {
                $(this).css("background-color", curColor);
                if (!isGridWired) {
                    $(this).mouseenter(function () {
                        setGridRowColor($(this).parent(), gridRowColors[2]);
                    });
                    $(this).mouseleave(function () {
                        if (($(this).parent().children("th").children(".txtRow").val() - 1) % 2 == 0) {
                            curColor = gridRowColors[0];
                        } else {
                            curColor = gridRowColors[1];
                        }
                        setGridRowColor($(this).parent(), curColor);
                    });
                }
            });

            intZ++;
        });
    }
    else {

        var curChkObj;
        $.each($(gridClass + " tbody tr th .chkRow"), function () {
            curChkObj = $(this).children("input");
            //What color should the cells be?
            if ($(curChkObj).attr("checked")) {
                curColor = gridRowColors[3];
                hoverColor = gridRowColors[4];
            } else {
                if (intZ % 2 == 0) {
                    curColor = gridRowColors[0];
                } else {
                    curColor = gridRowColors[1];
                }
                hoverColor = gridRowColors[2];
            }

            //Zebra stripe and hook up tds hover and click events
            $.each($(this).parent().parent().children("td"), function () {
                $(this).css("background-color", curColor);
                if (!isGridWired) {
                    $(this).bind('click', function (e) {
                        $(this).parent().children("th").children(".chkRow").children("input").click();
                    });
                    $(this).mouseenter(function () {
                        if ($(this).parent().children("th").children(".chkRow").children("input").attr("checked")) {
                            setGridRowColor($(this).parent(), gridRowColors[4]);
                        } else {
                            setGridRowColor($(this).parent(), gridRowColors[2]);
                        }
                    });
                    $(this).mouseleave(function () {
                        if ($(this).parent().children("th").children(".chkRow").children("input").attr("checked")) {
                            setGridRowColor($(this).parent(), gridRowColors[3]);
                        } else {
                            if (($(this).parent().children("th").children(".txtRow").val() - 1) % 2 == 0) {
                                curColor = gridRowColors[0];
                            } else {
                                curColor = gridRowColors[1];
                            }
                            setGridRowColor($(this).parent(), curColor);
                        }
                    });
                }
            });

            //Hook up hover events for th cells
            $.each($(this).parent().parent().children("th"), function () {
                $(this).css("background-color", curColor);
                if (!isGridWired) {
                    $(this).mouseenter(function () {
                        if ($(this).parent().children("th").children(".chkRow").children("input").attr("checked")) {
                            setGridRowColor($(this).parent(), gridRowColors[4]);
                        } else {
                            setGridRowColor($(this).parent(), gridRowColors[2]);
                        }
                    });
                    $(this).mouseleave(function () {
                        if ($(this).parent().children("th").children(".chkRow").children("input").attr("checked")) {
                            setGridRowColor($(this).parent(), gridRowColors[3]);
                        } else {
                            if (($(this).parent().children("th").children(".txtRow").val() - 1) % 2 == 0) {
                                curColor = gridRowColors[0];
                            } else {
                                curColor = gridRowColors[1];
                            }
                            setGridRowColor($(this).parent(), curColor);
                        }
                    });
                }
            });

            if (!isGridWired) {
                //Wire up checkbox events
                $(this).children("input").bind('click', function (e) {
                    if ($(this).parent().children("th").children(".chkRow").children("input").attr("checked")) {
                        setGridRowColor($(this).parent(), gridRowColors[2]);
                    } else {
                        setGridRowColor($(this).parent(), gridRowColors[3]);
                    }
                });
            }

            intZ++;
        });
    }
    isGridWired = true;
}







//Get the outer width or height of an element (the width or height of padding, border and margin)
//Everything but the content in CSS box model; t = "height" or "width"
function cssOuterBoxSize(o, t) {
    var r = 0;
    if (o != null) {
        switch (t) {
            case "height":
                r += parseInt($(o).css("margin-top"));
                r += parseInt($(o).css("margin-bottom"));
                r += parseInt($(o).css("padding-top"));
                r += parseInt($(o).css("padding-bottom"));
                r += parseInt($(o).css("border-top-width"));
                r += parseInt($(o).css("border-bottom-width"));
                break;
            case "width":
                r += parseInt($(o).css("margin-left"));
                r += parseInt($(o).css("margin-right"));
                r += parseInt($(o).css("padding-left"));
                r += parseInt($(o).css("padding-right"));
                r += parseInt($(o).css("border-left-width"));
                r += parseInt($(o).css("border-right-width"));
                break;
            default:
        }
    }
    return r;
}

//Get the width of the content available space in the browser window
function windowWidth() {
    // handle IE 6
    if ($.browser.msie && $.browser.version < 7) {
        var scrollWidth = Math.max(
				document.documentElement.scrollWidth,
				document.body.scrollWidth
			);
        var offsetWidth = Math.max(
				document.documentElement.offsetWidth,
				document.body.offsetWidth
			);

        if (scrollWidth < offsetWidth) {
            return $(window).width() - scrollBarWidth();
        } else {
            return scrollWidth - scrollBarWidth();
        }
        // handle "good" browsers
    } else {
        return $(window).width();
    }
}

//Get the height of the content available space in the browser window
function windowHeight() {
    // handle IE 6
    if ($.browser.msie && $.browser.version < 7) {
        var scrollHeight = Math.max(
    		document.documentElement.scrollHeight,
    		document.body.scrollHeight
    	);
        var offsetHeight = Math.max(
    		document.documentElement.offsetHeight,
    		document.body.offsetHeight
    	);

        if (scrollHeight < offsetHeight) {
            return $(window).height();
        } else {
            return scrollHeight;
        }
        // handle "good" browsers
    } else {
        return $(window).height();
    }
}

//Set a css property for all objects having class
function setCssForEachOfClass(className, key, val) {
    if ($(className) == null) { return; }
    $(className).each(function () {
        if ($(this).css(key) != val) {
            $(this).css(key, val);
        }
    });
}

//Caculate scroll bar width: http://jdsharp.us/jQuery/minute/calculate-scrollbar-width.php
//Modified
function scrollBarWidth() {
    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
    // Append our div, do our calculation and then remove it 
    $('body').append(div);
    var w1 = $('div', div).innerWidth();
    div.css('overflow-y', 'scroll');
    var w2 = $('div', div).innerWidth();
    $(div).remove();
    //Function doesn't work with safari, use default width
    if ($.browser.safari) { return 17; }
    return (w1 - w2);
}

//Prefil textbox with search label
function setCustomSearchInputMask(obj) {
    $(obj).focus(function (e) { if ($(this).attr("value") == $(this).attr("title")) $(this).attr("value", ""); });
    $(obj).blur(function (e) { if ($(this).attr("value") == "") $(this).attr("value", $(this).attr("title")); });
}

//For dialogs
function openModalDiv(divname, ajaxUrl, intWidth, intHeight) {
    $('#' + divname).load(ajaxUrl).dialog({ minWidth: intWidth, minHeight: intHeight, width: intWidth, height: intHeight, autoOpen: false, draggable: true, resizable: true, bgiframe: true, modal: false });
    $('#' + divname).dialog('open');
    $('#' + divname).parent().appendTo($("form:first"));
}
function closeModalDiv(divname) {
    $('#' + divname).dialog('close');
}



function doErrors(errMsgs, names, shouldAlert) {
    var valid = true;
    //Loop through return results
    var completeErrMsg = "";
    for (i in errMsgs) {
        //Handle real errors
        if (errMsgs[i] != "") {
            $('#' + names[i]).css("background-color", "#ee0000");
            $('#' + names[i]).css("color", "#fbec88");

            //Attach popup error div
            $('#' + names[i]).attr("title", errMsgs[i]);
            completeErrMsg += errMsgs[i] + "<br /><br />";

            if ($.browser.msie && $.browser.version < 9) {
                //MSIE less than 9
                if ($('#' + names[i]).find('option').length > 0) {
                    //select box <-- no popup bubbles for selects due to how IE6, 7, 8 display drop downs.
                } else {
                    $('#' + names[i]).bt({
                        fill: '#fbec88',
                        cssStyles: { color: 'black', padding: '5px 5px 5px 5px' },
                        trigger: ['focus', 'blur'],
                        shrinkToFit: true,
                        width: '240px',
                        cornerRadius: 5,
                        spikeLength: 14,
                        spikeGirth: 10,
                        positions: ['right', 'left', 'top', 'bottom']
                    }); 
                }
            } else {
                $('#' + names[i]).bt({
                    fill: '#fbec88',
                    cssStyles: { color: 'black', padding: '5px 5px 5px 5px' },
                    trigger: ['focus', 'blur'],
                    shrinkToFit: true,
                    width: '240px',
                    cornerRadius: 5,
                    spikeLength: 14,
                    spikeGirth: 10,
                    positions: ['right', 'left', 'top', 'bottom']
                });
            }
            valid = false;
        } else {
            $('#' + names[i]).css("background-color", "Transparent");
            $('#' + names[i]).css("color", "#000000");
        }
    }
    if (valid == false && shouldAlert) {
        $.alerts.okButton = 'Continue';
        jAlert('You cannot save because there is missing or invalid data on the page.<br /><br />Please fix the fields marked in red.<br /><br />' + completeErrMsg, 'Unable to Save');
    }
    return valid;
}
function ErrObjName(objFriendlyName) {
    if (!$.browser.msie || ($.browser.msie && $.browser.version > 8)) {
        objFriendlyName = "<strong>" + objFriendlyName + "</strong>";
    }
    return objFriendlyName;
}
function val_RegEx(obj, objFriendlyName, pattern, format) {
    objFriendlyName = ErrObjName(objFriendlyName);
    if ($(obj).attr("value") == null || $(obj).attr("value") == "" || $(obj).attr("value").match(pattern)) {
        return "";
    } else {
        return objFriendlyName + " must be written in " + format + " format.";
    }
}
function val_MinMax(obj, objFriendlyName, min, max) {
    objFriendlyName = ErrObjName(objFriendlyName);
    if ($(obj).attr("value") == null || $(obj).attr("value") == "" || ($(obj).attr("value") > min && $(obj).attr("value") < max == true)) {
        return "";
    } else {
        return objFriendlyName + " must be between<br />" + min + " and " + max + ".";
    }
}
function val_Req(obj, objFriendlyName) {
    objFriendlyName = ErrObjName(objFriendlyName);
    if ($(obj).attr("value") != null && $(obj).attr("value") != "") {
        return "";
    } else {
        return objFriendlyName + " is required and must be filled in.";
    }
}
function val_Compare(obj, objCompare, objFriendlyName) {
    objFriendlyName = ErrObjName(objFriendlyName);
    if ($(obj).attr("value") != null && $(objCompare).attr("value") != null) {

        if ($(obj).attr("value") == $(objCompare).attr("value")) {
            return "";
        } else {
            return objFriendlyName + " fields must match.";
        }
    } else {
        return objFriendlyName + " fields must match.";
    }
}
function val_MinLength(obj, objFriendlyName, minLen) {
    objFriendlyName = ErrObjName(objFriendlyName);
    if ($(obj).attr("value") != null && $(obj).attr("value") != "") {
        if ($(obj).attr("value").length < minLen) { return objFriendlyName + " have a minimum length of " + minLen + " characters"; }
    }
    return "";
}
function val_MaxLength(obj, objFriendlyName, maxLen) {
    objFriendlyName = ErrObjName(objFriendlyName);
    if ($(obj).attr("value") != null && $(obj).attr("value") != "") {
        if ($(obj).attr("value").length > maxLen) { return objFriendlyName + " have a maximum length of " + maxLen + " characters."; }
    }
    return "";
}

function fixDropDowns() {
    $('select').each(function () {
        $(this).attr("oldWidth", $(this).width() + 6);
        $(this).css("min-width", $(this).width() + 6);
        $(this).focusin(function () {
            $(this).css("width", "auto");
        });
        $(this).blur(function () {
            $(this).css("width", $(this).attr("oldWidth"));
        });
        $(this).css("position", "relative").css("z-index", "1000");
    });
}

//Center element
function centerThis(idToCenter) {
    jQuery.each($(idToCenter), function () {
        if (windowWidth() > minWindowWidth && $(this).width() > minWindowWidth) {
            if ($(this) != null) {
                if ($.browser.version < 7 && $.browser.msie) {
                    $(this).css("margin-left", Math.round(((windowWidth() - $(this).width() - getLeftRightPadding(this)) / 2) / 2) + "px");
                }
                else {
                    $(this).css("margin-left", Math.round((windowWidth() - $(this).width() - getLeftRightPadding(this)) / 2) + "px");
                }
            }
        }
        else if ($(this).attr("id") == "navInnerDiv" && windowWidth() >= minWindowWidth) {
            if ($(this) != null) {
                if ($.browser.version < 7 && $.browser.msie) {
                    $(this).css("margin-left", Math.round(((windowWidth() - $(this).width() - getLeftRightPadding(this)) / 2) / 2) + "px");
                }
                else {
                    $(this).css("margin-left", Math.round((windowWidth() - $(this).width() - getLeftRightPadding(this)) / 2) + "px");
                }
            }
        }
        else {
            if ($(this) != null) {
                $(this).css("margin-left", "40px");
            }
        }
    });
}
function getLeftRightPadding(idToGet) {
    var ret = 0;
    if ($(idToGet).css("padding-right") != null) { ret = ret - parseInt($(idToGet).css("padding-right").replace("px", ''), 0); }
    if ($(idToGet).css("padding-left") != null) { ret = ret - parseInt($(idToGet).css("padding-left").replace("px", ''), 0); }
    return ret;
}

function openPopupFromPopup(url) {
    openwin(url, 860, 390);
}

//Open a pop up
var ctr = 0;
function openwin(url, strWidth, strHeight) {
    var winName = "win_" + (ctr++);
    leftStr = (screen.width - strWidth) / 2;
    topStr = 200 + $(window).scrollTop();  //(screen.height - strHeight) / 2 - 50;
    windowProperties = "toolbar=no,menubar=no,scrollbars=yes,statusbar=no,height=" + strHeight + ",width=" + strWidth + ",left=" + leftStr + ",top=" + topStr + "";
    winName = window.open(url, winName, windowProperties);
}


function loadingDiv(open) {
    if (open) {
        $(".fullWidthTableLoading").css("left", (windowWidth() - $(".fullWidthTableLoading").width()) / 2);
        $(".fullWidthTableLoading").css("top", 200 + $(window).scrollTop());
        setCssForEachOfClass(".fullWidthTableLoading", "display", "block");
    } else {
        refreshGrid();
        setCssForEachOfClass(".fullWidthTableLoading", "display", "none");
    }
}
function ReloadGrid() {

    if ($("table tfoot tr td div a[title='Goto Page']").length > 0) {
        var jumpToPageLink = $("table tfoot tr td div a[title='Goto Page']").get(0);
        __doPostBack(jumpToPageLink.id.replace(/_/g, "$"), '');
    }
}
function refreshGrid() {

    resizeGrid(gridClassName, gridOptionStretch);
    isGridWired = false;
    colorAndWireGrid(gridClassName);
    customSearchOptions();
    setOuterContainerHeightReqs(true);
}


function loadNewParent(url) {
    opener.location.href = url;
}

function doCheckBoxTree(id) {
    var curParentChk;

    $.each($(id + " .chkListGroup"), function () {
        curParentChk = $(this).children("span").children("input");
        //$(curParentChk).css("visibility", "hidden");
        $(curParentChk).addClass("opacity");
        doCheckGroup($(this));

        //Set up parent checkbox select all
        if (!isBound) {
            $(this).children("span").click(function () {
                var isChecked = $(this).hasClass("cbChecked");
                if (isChecked) { isChecked = false; } else { isChecked = true; }
                $.each($(this).parent().children("ul").children("li"), function () {
                    $(this).children("span").children("input").attr("checked", isChecked);
                });

                doCheckGroup($(this).parent());

            });
        }

    });

    $(id).css("display", "block");
    isBound = true;
}
var curCount = 0;
var totalCount = 0;
var curChk;
var curParentChk;
var isBound = false;
function doCheckGroup(obj) {
    curCount = 0;
    totalCount = 0;
    $.each($(obj).children("ul").children("li"), function () {
        curChk = $(this).children("span").children("input");

        if (curChk.attr("checked")) {
            curCount += 1;
        }

        if (!isBound) {
            $(this).bind('click', function (e) {
                doCheckGroup($(this).parent().parent());
            });
        }

        totalCount += 1;
    });

    //Set up child handlers
    if (!isBound) {

        $.each($(obj).children("ul").children("li"), function () {
            $(this).css("display", "none");
        });

        $(obj).children("a").keydown(function (event) {

            if (event.keyCode != '9' && event.keyCode != '16' && event.keyCode != '38' && event.keyCode != '40')//don't hide/show on tab or shift tab or the cursor up/down (user just wants to move on)
            {
                if ($(this).parent().children("ul").children("li:visible").length == 0) {
                    $(this).children("img").attr("src", $(this).children("img").attr("src").replace("minus", "plus"));
                    $.each($(this).parent().children("ul").children("li"), function () {
                        $(this).css("display", "block");
                    });
                }
                else {
                    $(this).children("img").attr("src", $(this).children("img").attr("src").replace("plus", "minus"));
                    $.each($(this).parent().children("ul").children("li"), function () {
                        $(this).css("display", "none");
                    });
                }
            }
        });

        $(obj).children("a").click(function () {

            if ($(this).parent().children("ul").children("li:visible").length == 0) {
                $(this).children("img").attr("src", $(this).children("img").attr("src").replace("minus", "plus"));
                $.each($(this).parent().children("ul").children("li"), function () {
                    $(this).css("display", "block");
                });
            }
            else {
                $(this).children("img").attr("src", $(this).children("img").attr("src").replace("plus", "minus"));
                $.each($(this).parent().children("ul").children("li"), function () {
                    $(this).css("display", "none");
                });
            }

        });
    }

    $(obj).children("span").removeClass("cbChecked");
    $(obj).children("span").removeClass("cbIntermediate");
    $(obj).children("span").removeClass("cbUnchecked");
    if (curCount == totalCount) {
        $(obj).children("span").addClass("cbChecked");
    } else if (curCount > 0) {
        $(obj).children("span").addClass("cbIntermediate");
    } else {
        $(obj).children("span").addClass("cbUnchecked");
    }
}



function getCheckedBoxes() {
    var ary = "";
    $.each($(".fullWidthTable tbody tr"), function () {
        if ($(this).children("th").children(".chkRow").children("input").attr("checked")) {
            ary += $(this).children("th").children(".txtRowId").val() + ",";
        }
    });
    ary = ary.substring(0, ary.length - 1);
    return ary;
}
function treeGetCheckedBoxes() {
    var ary = "";
    $.each($("#chkList li ul li"), function () {
        if ($(this).children("span").children("input").attr("checked")) {
            ary += $(this).attr("class") + ",";
        }
    });
    ary = ary.substring(0, ary.length - 1);
    return ary;
}


function openExcel(url) {
    window.location = url;
}

//Open a file for download
function launchDownload(objExcelFNameTextBox) {
    if ($(objExcelFNameTextBox).val() != "") {
        //openExcel(root + 'Xml/UserDocs/' + $(objExcelFNameTextBox).val());
        openExcel('/Xml/UserDocs/' + $(objExcelFNameTextBox).val());
        $(objExcelFNameTextBox).val("");
    }
}



function filtersGetCheckedBoxes() {
    var ary = "";
    $.each($("#matchedLocations tbody tr"), function () {
        if ($(this).children("td").children(".chkRow").children("input").attr("checked")) {
            ary += $(this).children("td").children(".txtRowId").val() + ",";
        }
    });
    ary = ary.substring(0, ary.length - 1);
    return ary;
}

function querySt(key) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == key) {
            return ft[1];
        }
    }
}


function showTip(objId) {
    if ($('#' + objId).next().css("display") == "block") {
        $('#' + objId).next().css("display", "none");
    } else if ($('#' + objId).next().html() != "") {
        $('#' + objId).next().css("display", "block");
    }
    setOuterContainerHeightReqs(true);
}
