addLoadEvent(initializeMenu);
addLoadEvent(activateTabs);
addLoadEvent(highlightTable);
addLoadEvent(launchFavorites);
addLoadEvent(focusFields);
addLoadEvent(setDisabledStyle);
addLoadEvent(setDefaultInputFocus);
addLoadEvent(distributeTasks);

var oldScroll;
function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function(){
            oldonload();
            func();
        }
    }
}

function popup(url){
    GB_showFullScreen("Preview", url)
}

function removeLoaders(){
    var loader = window.parent.getClasses("loader", "img");
    for (var i = 0; i < loader.length; i++) {
        loader[i].parentNode.removeChild(loader[i]);
    }
}

function distributeTasks(){
    var help = getClasses("help", "a");
    for (var i = 0; i < help.length; i++) {
        help[i].onclick = function(){
            var helpWin = window.parent.window.open(this.href);
            helpWin.focus();
            return false;
        }
    }
}

function openMe(url, skip, height, width){
    if (height == undefined) {
        GB_showFullScreen("Preview", url)
    }
    else {
        GB_show("Preview", url, 600, 900)
    }
    if (!skip) {
        reloadPage();
    }
}

function reloadPage(){
    window.location = "/orders/maintenance/incomplete/completed.aspx";
}

function initializeMenu(){
    var menu = document.getElementById("menu");
    if (menu) {
        var links = getTag("a", menu);
        for (var i = 0; i < links.length; i++) {
            links[i].onclick = function(){
                var clickedLink = this.href;
                if (clickedLink.charAt(clickedLink.length - 1) != "#") {
                    var activeTitle = this.getAttribute("programcode")
                    
                    createTabs(activeTitle, this.href, activeTitle);
                    var innerLists = getTag("ul", menu);
                    for (var j = 0; j < innerLists.length; j++) {
                        innerLists[j].style.display = "none";
                    }
                    return false;
                }
                return false;
            }
        }
    }
}


function activateTabs(){
    var section_tab = getId("section_tab");
    if (section_tab) {
        var section_tabs = getTag("a", section_tab);
        for (var i = 0; i < section_tabs.length; i++) {
            section_tabs[i].onclick = function(){
                var clicked = this.id;
                
                if (clicked == "viewall") {
                    var sections = new Array();
                    sections = arrayCollection(section_tabs);
                    for (var j = 0; j < sections.length; j++) {
                        var div = document.getElementById(sections[j]);
                        if (div) {
                            div.className = "shown";
                        }
                    }
                    resizeIframe();
                }
                
                //for new orders tab - they wanted the submit button to be disabled - this section needs to be cleaned up, if and else is getting way to messy- freakin changes every single day - software planning goes to hell
                else 
                    if (clicked == "specialdetails") {
                        var activateSpecialDetail = document.getElementById("ctl00_ContentPlaceHolder1_SpecialDetailsClicked");
                        if (activateSpecialDetail) {
                            if (activateSpecialDetail.value == "true") {
                                //document.getElementById("ctl00_ContentPlaceHolder1_Save").disabled = false;
                                document.getElementById("ctl00_ContentPlaceHolder1_Save").style.visibility = "visible";
                                var addserve = document.getElementById("ctl00_ContentPlaceHolder1_AddServe");
                                if (addserve) {
                                    addserve.style.visibility = "visible";
                                }
                                var addcopy = document.getElementById("ctl00_ContentPlaceHolder1_AddCopy")
                                if (addcopy) {
                                    addcopy.style.visibility = "visible";
                                }
                            }
                        }
                        var sections = getTag("div", getId("content"));
                        var section_array = new Array();
                        for (var j = 0; j < sections.length; j++) {
                            var section_id = sections[j].id;
                            if (section_id.split("_")[0] == "section") {
                                section_array.push(sections[j]);
                            }//end of if
                        }//end of gathering new tabsdivs
                        for (var k = 0; k < section_array.length; k++) {
                            if (section_array[k].id.split("_")[1] != clicked) {
                                section_array[k].className = "hidden";
                            }
                            else {
                                section_array[k].className = "shown";
                                resizeIframe();
                                var tabfocus = $j("#section_" + clicked + " .tabfocus");
                                
                                if (tabfocus.length > 0) {
                                
                                    if ((tabfocus[0].getAttribute("readonly") == true) || (tabfocus[0].getAttribute("readonly") == "readonly") || (tabfocus[0].getAttribute("disabled") == true) || (tabfocus[0].getAttribute("disabled") == "disabled")) {
                                    
                                    }
                                    else {
                                        tabfocus[0].focus();
                                    }
                                }
                                resizeIframe();
                            }
                        }//end of hiding
                    }
                    else 
                        if (clicked == "closeorder") {
                        
                            var sections = getTag("div", getId("content"));
                            var section_array = new Array();
                            for (var j = 0; j < sections.length; j++) {
                                var section_id = sections[j].id;
                                if (section_id.split("_")[0] == "section") {
                                    section_array.push(sections[j]);
                                }//end of if
                            }//end of gathering new tabsdivs
                            for (var k = 0; k < section_array.length; k++) {
                                if (section_array[k].id.split("_")[1] != clicked) {
                                    section_array[k].className = "hidden";
                                }
                                else {
                                    section_array[k].className = "shown";
                                    
                                    resizeIframe();
                                    var tabfocus = $j("#section_" + clicked + " .tabfocus");
                                    
                                    if (tabfocus.length > 0) {
                                        if ((tabfocus[0].getAttribute("readonly") == true) || (tabfocus[0].getAttribute("readonly") == "readonly") || (tabfocus[0].getAttribute("disabled") == true) || (tabfocus[0].getAttribute("disabled") == "disabled")) {
                                        
                                        }
                                        else {
                                            tabfocus[0].focus();
                                        }
                                    }
                                    
                                }
                            }//end of hiding
                        }
                        
                        else {
                        
                            var sections = getTag("div", getId("content"));
                            var section_array = new Array();
                            for (var j = 0; j < sections.length; j++) {
                                var section_id = sections[j].id;
                                if (section_id.split("_")[0] == "section") {
                                    section_array.push(sections[j]);
                                }//end of if
                            }//end of gathering new tabsdivs
                            for (var k = 0; k < section_array.length; k++) {
                            
                                if (section_array[k].id.split("_")[1] != clicked) {
                                    section_array[k].className = "hidden";
                                }
                                else {
                                
                                    section_array[k].className = "shown";
                                    resizeIframe();
                                    //var tabfocus = $j("#section_" + clicked + " input");
                                    //alert(tabfocus.length);
                                    var tabfocus = $j("#section_" + clicked + " .tabfocus");
                                    
                                    if (tabfocus.length > 0) {
                                    
                                        if ((tabfocus[0].getAttribute("readonly") == true) || (tabfocus[0].getAttribute("readonly") == "readonly") || (tabfocus[0].getAttribute("disabled") == true) || (tabfocus[0].getAttribute("disabled") == "disabled")) {
                                        
                                        }
                                        else {
                                            tabfocus[0].focus();
                                        }
                                    }
                                    resizeIframe();
                                }
                            }//end of hiding
                        }
                setTabHighlight(clicked);
                return false;
            }//end of onclick
        }
    }
}

function setTabHighlight(tab){
    var section_tab = getId("section_tab");
    var section_tabs = getTag("a", section_tab);
    
    for (var i = 0; i < section_tabs.length; i++) {
        if (section_tabs[i].id == tab) {
            section_tabs[i].className = "on";
        }
        else {
            section_tabs[i].className = null;
        }
    }
}

function setActiveTab(tab){
	var section_tab = getId("section_tab");
    var section_tabs = getTag("a", section_tab);
    var shown = getClasses('shown','div');
	for(var i=0; i<shown.length; i++){
		shown[i].className = "hidden";
	}
	document.getElementById('section_' + tab).className = "shown";

    for (var i = 0; i < section_tabs.length; i++) {
        if (section_tabs[i].id == tab) {
            section_tabs[i].className = "on";


        }
        else {
            section_tabs[i].className = null;
        }
    }
}

function setQuery(q){
    var query = parent.document.getElementById("ctl00_RelatedPRograms_ItemQuery");
    if (query) {
        query.value = q;
    }
}

function highlightTable(){
    var tables = getClasses("display", "table");
    for (var i = 0; i < tables.length; i++) {
        var tbodies = getTag("tbody", tables[i]);
        for (var j = 0; j < tbodies.length; j++) {
            var rows = getTag("tr", tbodies[j]);
            for (var k = 0; k < rows.length; k++) {
                rows[k].onmouseover = function(){
                    if ((this.className == "alt") || (this.className == "")) {
                        this.className = "focus";
                    }
                }
                rows[k].onmouseout = function(){
                    if ((this.className == "focus") || (this.className == "alt") || (this.className == "")) {
                        this.className = null;
                    }
                }
            }
        }
    }
}

function setNewTab(id){
    var section_tab = document.getElementById("inactive_section_tab");
    if (section_tab) {
        var section_tab = section_tab.getElementsByTagName("a");
        for (var i = 0; i < section_tab.length; i++) {
            if (section_tab[i].id == id) {
                section_tab[i].className = "on";
            }
            else {
                section_tab[i].className = null;
            }
        }
    }
}


function launchFavorites(){
    var favorites = getId("favorites");
    if (favorites) {
        var links = getTag("a", favorites);
        for (var i = 0; i < links.length; i++) {
            links[i].onclick = function(){
                if (this.className != "add") {
                    var title = this.getAttribute("title");
                    window.parent.createTabs(title, this.href, title);
                }
                return false;
            }
        }
    }
}

function arrayCollection(array){
    var arr = new Array();
    for (var i = 0; i < array.length; i++) {
        var sectionname = "section_" + array[i].id;
        if (sectionname != "section_viewall") {
            arr.push(sectionname);
        }
    }
    return arr;
}


function setDefaultInputFocus(){
    var focuses = $j(".focus");
    if (focuses.length > 0) {
        var mainfocus = focuses[0];
        if ((mainfocus.getAttribute("readonly") == true) || (mainfocus.getAttribute("readonly") == "readonly") || (mainfocus.getAttribute("disabled") == true) || (mainfocus.getAttribute("disabled") == "disabled")) {
            $j(mainfocus).removeClass("focus");
        }
        else {
            mainfocus.focus();
        }
    }
}

function setDisabledStyle(){
    var nonactive = $j("input, select, textarea");
    for (var i = 0; i < nonactive.length; i++) {
        if ((nonactive[i].getAttribute("disabled") == true) || (nonactive[i].getAttribute("disabled") == "disabled") || (nonactive[i].getAttribute("readonly") == true) || (nonactive[i].getAttribute("readonly") == "readonly")) {
            $j(nonactive[i]).addClass("disabled");
        }
    }
}



function setButtonFocus(e, buttonId){
    var keynum
    if (window.event) {
        keynum = e.keyCode;
    }
    else 
        if (e.which) {
            keynum = e.which;
        }
    
    if (keynum == 13) {
        var button = document.getElementById(buttonId);
        button.focus();
    }
}

function focusFields(){
    $j("input, textarea, select").bind("focus", function(){
        $j(this).addClass("focus");
    });
    
    $j("input, textarea, select").bind("blur", function(){
        $j(this).removeClass("focus");
    });
    
    if ($j.browser.msie) {
        $j("select").bind("focusin", function(){
            $j(this).addClass("focus");
        });
        
        $j("select").bind("focusout", function(){
            $j(this).removeClass("focus");
        });
    }
}

function closeGB_Window(){
    var iframe = window.parent.window.parent.document.getElementsByTagName("iframe");
    if (iframe) {
        iframe = iframe[0];
    }
    
    var overlay = window.parent.window.parent.document.getElementById("GB_overlay");
    var windowgb = window.parent.window.parent.document.getElementById("GB_window");
    if (windowgb) {
        var selects = window.parent.window.parent.document.getElementsByTagName("select");
        for (var i = 0; i < selects.length; i++) {
            selects[i].style.visibility = "visible";
        }
    }
    
    if (iframe) {
        iframe.parentNode.removeChild(iframe);
    }
    if (overlay) {
        overlay.parentNode.removeChild(overlay);
    }
    if (windowgb) {
        windowgb.parentNode.removeChild(windowgb);
    }
    
}

function cancelGB_Window(){
    window.parent.window.parent.location.reload();
}

function expandIt(id){
    var div = document.getElementById(id);
    var classes = getClasses(id, "tbody");
    for (var i = 0; i < classes.length; i++) {
        if (classes[i].style.display == "none") {
            classes[i].style.display = ""
        }
        else {
            classes[i].style.display = "none"
        }
    }
}

function setDefaultButton(){

}

function showPopup(a, top){
    if (a != null) {
        var type = a.getAttribute("type");
        var staff = a.getAttribute("staff");
        var adjustment = a.getAttribute("adjustment");
        if (adjustment) {
            setAdjustmentId(adjustment);
        }
        
        if (staff != null) {
            type.value = staff;
            if (document.getElementById("barId") != null) {
                var bId = document.getElementById("barId");
                if (staff == "Staff") {
                    bId.style.display = "none";
                }
                else {
                    bId.style.display = "";
                }
            }
        }
        else {
            if (document.getElementById("barId") != null) {
                var bId = document.getElementById("barId");
                bId.style.display = "none";
                type.value = "Staff";
            }
        }
    }
    
    var selects = getTag("select");
    
    for (var i = 0; i < selects.length; i++) {
        selects[i].style.visibility = "hidden";
    }
    
    var quickedit = document.getElementById("quickedit");
    if (quickedit) {
        var left;
        var windowWidth;
        var quickeditselects = getTag("select", quickedit);
        
        for (var i = 0; i < quickeditselects.length; i++) {
            quickeditselects[i].style.visibility = "visible";
        }
        var top = $j(a).position().top;
        if ($j.browser.msie) {
            oldScroll = window.parent.document.documentElement.scrollTop;
        }
        else {
            oldScroll = window.parent.pageYOffset;
        }
        
        
        left = ($j("#content").width() + 200 - $j("#quickedit").width()) / 2;
        quickedit.style.display = "block";
        var overlay = document.getElementById("overlay");
        window.parent.scrollTo(0, top);
        if (top) {
            quickedit.style.top = top + "px";
            quickedit.style.left = left + "px";
        }
        if (overlay != null) {
            overlay.style.display = "block";
        }
        
        return false;
    }
}

function hidePopup(){
    var quickedit = document.getElementById("quickedit");
    quickedit.style.display = "none";
    var overlay = document.getElementById("overlay");
    overlay.style.display = "none";
    var selects = getTag("select");
    
    for (var i = 0; i < selects.length; i++) {
        selects[i].style.visibility = "visible";
    }
    window.parent.scrollTo(0, oldScroll);
    return false;
}


function showHiddenPopup(a, id){
    var left;
    var windowWidth;
    var top = $j("#" + a).position().top;
    if ($j.browser.msie) {
        oldScroll = window.parent.document.documentElement.scrollTop;
    }
    else {
        oldScroll = window.parent.pageYOffset;
    }
    var selects = getTag("select");
    
    for (var i = 0; i < selects.length; i++) {
        selects[i].style.visibility = "hidden";
    }
    
    var quickedit = document.getElementById(id);
    left = ($j("#content").width() + 200 - $j("#" + id).width()) / 2;
    if (quickedit) {
        var quickeditselects = getTag("select", quickedit);
        
        for (var i = 0; i < quickeditselects.length; i++) {
            quickeditselects[i].style.visibility = "visible";
        }
        
        quickedit.style.display = "block";
        var overlay = document.getElementById("overlay");
        window.parent.scrollTo(0, top);
        if (top) {
            quickedit.style.top = top + "px";
            quickedit.style.left = left + "px";
        }
        overlay.style.display = "block";
        return false;
    }
}

function hideHiddenPopups(id){
    var quickedit = document.getElementById(id);
    quickedit.style.display = "none";
    var overlay = document.getElementById("overlay");
    overlay.style.display = "none";
    var selects = getTag("select");
    
    for (var i = 0; i < selects.length; i++) {
        selects[i].style.visibility = "visible";
    }
    window.parent.scrollTo(0, oldScroll);
    return false;
}

function Querystring(qs){
    this.params = new Object()
    this.get = Querystring_get
    
    if (qs == null) 
        qs = location.search.substring(1, location.search.length)
    
    if (qs.length == 0) 
        return qs = qs.replace(/\+/g, ' ')
    var args = qs.split('&')
    
    for (var i = 0; i < args.length; i++) {
        var value;
        var pair = args[i].split('=')
        var name = unescape(pair[0])
        
        if (pair.length == 2) 
            value = unescape(pair[1])
        else 
            value = name
        
        this.params[name] = value
    }
}

function Querystring_get(key, default_){
    if (default_ == null) 
        default_ = null;
    
    var value = this.params[key]
    if (value == null) 
        value = default_;
    
    return value
}

function tabbing(){
    var nexttab = document.getElementById("nexttab");
    nexttab.onclick = function(){
        var sectionttab = document.getElementById("section_tab");
        var sectionlinks = sectionttab.getElementsByTagName("a");
        for (var i = 0; i < sectionlinks.length; i++) {
            if (sectionlinks[i].className == "on") {
                var id = sectionlinks[i].id;
                
                var nextSibling = sectionlinks[i].parentNode.nextSibling;
                if (nextSibling == null) {
                    break;
                }
                
                if (nextSibling.nodeName.toUpperCase() != "LI") {
                    nextSibling = nextSibling.nextSibling;
                }
                
                if (nextSibling != null) {
                    if (nextSibling.nodeName.toUpperCase() == "LI") {
                        nextSibling.getElementsByTagName("a")[0].className = "on";
                        var nextid = nextSibling.getElementsByTagName("a")[0].id;
                        if (nextid == "specialdetails") {
                            document.getElementById("ctl00_ContentPlaceHolder1_Save").style.visibility = "visible";
                            var addserve = document.getElementById("ctl00_ContentPlaceHolder1_AddServe");
                            if (addserve) {
                                addserve.style.visibility = "visible";
                            }
                            var addcopy = document.getElementById("ctl00_ContentPlaceHolder1_AddCopy")
                            if (addcopy) {
                                addcopy.style.visibility = "visible";
                            }
                        }
                        document.getElementById("section_" + nextid).className = "shown";
                        resizeIframe();
                        document.getElementById("section_" + id).className = "hidden";
                        sectionlinks[i].className = "";
                        var previousimg = document.getElementById("previoustab").getElementsByTagName("img")[0];
                        previousimg.src = "/webresources/themes/default/images/buttons/previous.gif"
                        checkPaging("next", this, nextSibling);
                        break;
                    }
                }
            }
        }
        return false;
    }
    
    var previoustab = document.getElementById("previoustab");
    previoustab.onclick = function(){
        var sectionttab = document.getElementById("section_tab");
        var sectionlinks = sectionttab.getElementsByTagName("a");
        for (var i = 0; i < sectionlinks.length; i++) {
            if (sectionlinks[i].className == "on") {
                var id = sectionlinks[i].id;
                
                var previousSibling = sectionlinks[i].parentNode.previousSibling;
                if (previousSibling == null) {
                    break;
                }
                
                if (previousSibling.nodeName.toUpperCase() != "LI") {
                    previousSibling = previousSibling.previousSibling;
                }
                
                if (previousSibling != null) {
                    if (previousSibling.nodeName.toUpperCase() == "LI") {
                        previousSibling.getElementsByTagName("a")[0].className = "on";
                        var nextid = previousSibling.getElementsByTagName("a")[0].id;
                        document.getElementById("section_" + nextid).className = "shown";
                        resizeIframe();
                        document.getElementById("section_" + id).className = "hidden";
                        sectionlinks[i].className = "";
                        var nextimg = document.getElementById("nexttab").getElementsByTagName("img")[0];
                        nextimg.src = "/webresources/themes/default/images/buttons/next.gif"
                        checkPaging("previous", this, previousSibling);
                        break;
                    }
                    
                }
                
                
            }
            
        }
        return false;
    }
}


function checkPaging(direction, link, sibling){
    if (direction == "next") {
        sibling = sibling.nextSibling;
        
        if (sibling == null) {
        
            var nextimg = link.getElementsByTagName("img")[0];
            nextimg.src = "/webresources/themes/default/images/buttons/next_grey.gif";
            
        }
        else {
        
            if ((sibling.nodeName.toUpperCase() != "LI") && (sibling != null)) {
                sibling = sibling.nextSibling;
            }
            if (sibling == null) {
                var nextimg = link.getElementsByTagName("img")[0];
                nextimg.src = "/webresources/themes/default/images/buttons/next_grey.gif";
                
            }
        }
    }
    
    if (direction == "previous") {
        sibling = sibling.previousSibling;
        if (sibling == null) {
            var previousimg = link.getElementsByTagName("img")[0];
            previousimg.src = "/webresources/themes/default/images/buttons/previous_grey.gif";
            
        }
        else {
        
            if ((sibling.nodeName.toUpperCase() != "LI") && (sibling != null)) {
                sibling = sibling.previousSibling;
            }
            if (sibling == null) {
                var previousimg = link.getElementsByTagName("img")[0];
                previousimg.src = "/webresources/themes/default/images/buttons/previous_grey.gif";
            }
        }
    }
}

function defaultKeypress(div, elem){
    $j(div).keydown(function(event){
        if(event.keyCode == 13 ){
            $j(elem, this).focus();
            $j(elem, this).click();
            return false;
        }
    });
}
