// Struts-based Navigation Javascript

if (!document.getElementById) {
    document.getElementById = function() { return null; }
}

function initMenu() {
    var uls = document.getElementsByTagName("ul");
    for (i = 0; i < uls.length; i++) {
        if (uls[i].className == "MainNav") {
            decorateMenu(uls[i]);
        }
    }
}

function decorateMenu(menu) {
    var items = menu.getElementsByTagName("li");
    for (var i=0; i < items.length; i++) {
        items[i].firstChild.myIndex = i;
        // retain any existing onclick handlers from menu-config.xml
        if (items[i].firstChild.onclick) {
            items[i].firstChild.onclick=function() {
                eval(items[this.myIndex].firstChild.getAttribute("onclick"));
                setCookie("menuSelected", this.myIndex);
                };
        } else {
            items[i].firstChild.onclick=function() {
                setCookie("menuSelected", this.myIndex);
            };
        }
    }
    activateMenu(items);
}

function activateMenu(items) {
    var activeMenu;
    var found = 0;
    for (var i=0; i < items.length; i++) {
        var url = items[i].firstChild.getAttribute("href");
        var current = document.location.toString();
         var questionmarkLoc = current.indexOf("?");
         if (questionmarkLoc > 0) current = current.substring (0, questionmarkLoc);
         var questionmarkLoc1 = url.indexOf("?");
         if (questionmarkLoc1 > 0) url = url.substring (0, questionmarkLoc1);
        if (current.indexOf(url) != -1) {
            found++;
        }
    }
    if(found == 0){
        for (var i=0; i < items.length; i++) {
            var url = items[i].firstChild.getAttribute("href");
            var previous = document.referrer.toString();
	 var questionmarkLoc = previous.indexOf("?");
         if (questionmarkLoc > 0) previous = previous.substring (0, questionmarkLoc);
         var questionmarkLoc1 = url.indexOf("?");
         if (questionmarkLoc1 > 0) url = url.substring (0, questionmarkLoc1);
            if (previous.indexOf(url) != -1) {
                found++;
            }
        }
   }

    // more than one found, use cookies
    var currentCheck = false;
    var previousCheck = false;
    if (found > 1) {
        var menuSelected = getCookie("menuSelected");
        if (items[menuSelected].parentNode.className == "SubNav") {
            items[menuSelected].firstChild.className="selected";
            items[menuSelected].parentNode.parentNode.className="selected";
        } else {
            items[menuSelected].className+="selected";
        }
    } else {
        // only one found, match on URL
        for (var i = 0; i < items.length; i++) {
            var url = items[i].firstChild.getAttribute("href");
            var current = document.location.toString();
            var questionmarkLoc = current.indexOf("?");
            if (questionmarkLoc > 0) current = current.substring(0, questionmarkLoc);
            var questionmarkLoc1 = url.indexOf("?");
            if (questionmarkLoc1 > 0) url = url.substring(0, questionmarkLoc1);
            if (current.indexOf(url) != -1) {
                currentCheck = true;
                if (items[i].parentNode.className == "SubNav") {
                    items[i].firstChild.className = "selected";
                    items[i].parentNode.parentNode.className = "selected";
                } else {
                    items[i].className += "selected";
                }
            }
        }
        if (!currentCheck) {
            for (var i = 0; i < items.length; i++) {
                var url = items[i].firstChild.getAttribute("href");
                var previous = document.referrer.toString();
                var questionmarkLoc = previous.indexOf("?");
                if (questionmarkLoc > 0) previous = previous.substring(0, questionmarkLoc);
                var questionmarkLoc1 = url.indexOf("?");
                if (questionmarkLoc1 > 0) url = url.substring(0, questionmarkLoc1);
                if (previous.indexOf(url) != -1) {
                    previousCheck = true;
                    if (items[i].parentNode.className == "SubNav") {
                        items[i].firstChild.className = "selected";
                        items[i].parentNode.parentNode.className = "selected";
                    } else {
                        items[i].className += "selected";
                    }
                }
            }
        }
    }

    if(!currentCheck && !previousCheck){
        // no match found. match the closest.        
        var current = document.location.toString();
        current = current.toLowerCase();
        var previous = document.referrer.toString();
        if(previous!="") previous = previous.toLowerCase();

        var matchedSubNav = "";

        var referenceArray = new Array("home","viewinkpaper","report","printerprofile","location","printergroup","printeradmin","deactivateprinter","addprinter","userprofile","businesscost","inkcost","mediacost","inkslider","jobactivity","alert","useraccount");
		var userAccountArray2 = new Array("edituserassignprinters","editusersendreminders");
        var homeArray = new Array("userinvitation","userinbox");

        //alert("matchedSubNav 126:"+matchedSubNav);
        // checking w/ current url
        for(var j=0; j<referenceArray.length; j++){
            if(current.indexOf(referenceArray[j]) != -1){
                 matchedSubNav = referenceArray[j];
                {break}
            }
        }

        //alert("matchedSubNav 135:"+matchedSubNav);
        // checking w/ previous url
        if(matchedSubNav == ""){
            for(var j=0; j<referenceArray.length; j++){
                    if(previous.indexOf(referenceArray[j]) != -1){
                        matchedSubNav = referenceArray[j];
                        {break}
                    }
            }
        }

        //alert("matchedSubNav 146:"+matchedSubNav);
        // checking to see in useraccount module.
        if (matchedSubNav == "") {
            for (var k = 0; k < userAccountArray2.length; k++) {
                if (current.indexOf(userAccountArray2[k]) != -1) {
                    matchedSubNav = "useraccount";
                    {break}
                }
            }
        }

        if (matchedSubNav == "") {
            for (var k = 0; k < homeArray.length; k++) {
                if (current.indexOf(homeArray[k]) != -1) {
                    matchedSubNav = "home";
                    {break}
                }
            }
        }

        //alert("matchedSubNav 157:"+matchedSubNav);
        if(matchedSubNav!=""){
            for (var i=0; i < items.length; i++) {
                var url = items[i].firstChild.getAttribute("href");
                url = url.toLowerCase();
                if(url.indexOf(matchedSubNav) != -1){
                    if (items[i].parentNode.className == "SubNav") {
                            items[i].firstChild.className="selected";
                            items[i].parentNode.parentNode.className="selected";
                        } else {
                            items[i].className+="selected";
                        }
                    }
                }
         }
        //alert("matchedSubNav 172:"+matchedSubNav);                
            //alert("current:"+current+"printerSelectorArray[k] --"+printerSelectorArray[k]);
        if (matchedSubNav == "") {
            if (current.indexOf("printerpreference") != -1) {
                var body = document.getElementsByTagName("body") [0];
                //alert("body.id" + body.id);
                var uls = document.getElementsByTagName("ul");
                for (var i = 0; i < uls.length; i++) {
                    if (uls[i].className == "MainNav") {
                        //alert("length 182:" + uls.length);
                        var items1 = uls[i].getElementsByTagName("li");
                        for (var j = 0; j < items1.length; j++) {
                            var mainurl = items1[j].firstChild.getAttribute("href");
                            mainurl = mainurl.toLowerCase();
                            var questionmarkLoc5 = mainurl.indexOf("?");
                            if (questionmarkLoc5 > 0) mainurl = mainurl.substring(0, questionmarkLoc5);
                            //alert("mainurl:" + mainurl +"body.id" + body.id);
                            if (mainurl.indexOf(body.id) != -1) {
                                items1[j].className += "selected";
                                {break}
                            }
                        }
                    }
                }
            }
        }
    }
}
// Select the menu that matches the URL when the page loads
window.onload=initMenu;

// =========================================================================
//                          Cookie functions
// =========================================================================
/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
	var prefix = name + "="
	var start = document.cookie.indexOf(prefix)

	if (start==-1) {
		return null;
	}

	var end = document.cookie.indexOf(";", start+prefix.length)
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end)
	return unescape(value);
}

/* This function is used to delete cookies */
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}






// Button Rollover

function BtnRollOver(btn_id, btn_name, btn_state, btn_type) {
	var btn_path = "/edm/e1_img/e1_buttons/e1_btn_" + btn_name;

	if (btn_type == null) {
		btn_type = "gif";
	}

	if (btn_state == 1) {
		btn_path = btn_path + "_on." + btn_type;
	} else {
		btn_path = btn_path + "_off." + btn_type;
	}
	btn_id.src = btn_path;
}


/*
	showResellerFields()
	This function shows/hides divs that display different form fields for reseller information
	based on where the user purchased their printer from.

*/
function showResellerFields(resellerCase) {
	if (resellerCase == 1) {
		document.getElementById('LocalReseller').style.display = "block";
		document.getElementById('MailorOnline').style.display = "none";
	} else if (resellerCase == 2) {
		document.getElementById('LocalReseller').style.display = "none";
		document.getElementById('MailorOnline').style.display = "block";
	} else {
		document.getElementById('LocalReseller').style.display = "none";
		document.getElementById('MailorOnline').style.display = "none";
	}
}

/*
	showLengthLabel()
	This function changes the label of the length field on the Edit Paper page based on whether
	the user selects sheets or rolls for the paper count
*/

function showLengthLabel(id) {
	var myPaper = id.selectedIndex;
	var myPaperCount = id.options[myPaper].value;

	if (myPaperCount == "sheet") {
		document.getElementById('len_in').style.display = "inline";
		document.getElementById('len_ft').style.display = "none";
		
		if (document.getElementById('sheet_cnt')) {
			document.getElementById('sheet_cnt').disabled = false;
		}
	} else {
		document.getElementById('len_in').style.display = "none";
		document.getElementById('len_ft').style.display = "inline";
		if (document.getElementById('sheet_cnt')) {
			document.getElementById('sheet_cnt').disabled = true;
		}
	}
}

// Popup Window

function edmWindow(url) {
	if (arguments.length > 1) {
		var width = String(arguments[1]);
		var height = String(arguments[2]);
		if ((loc = width.indexOf("%")) != -1) {
			width = width.slice(0, loc);
			width = Number(width);
			width = screen.width * (width / 100);
		}
		if ((loc = height.indexOf("%")) != -1) {
			height = height.slice(0, loc);
			height = Number(height);
			height = screen.height * (height / 100);
		}
	} else {
		var width = 450;
		var height = 300;
	}
	var attributeString = 'height=' + height + ',width=' + width + ',menubar=no,scrollbars=yes,resizable=yes,screenX=10,screenY=10,top=10,left=10';
	var winpop = open(url, '', attributeString);
	winpop.focus();
}



/*
	showFields(toggle)
	This function shows/hides the specified div.


function showFields(val, total, prefix, toggle) {
	var showdiv = prefix + val;
	var hidediv;

	if (toggle == 1) {
		document.getElementById(prefix).style.display = "block";

	} else if (toggle == 0) {
		document.getElementById(prefix).style.display = "none";

	} else {
		for (var x = 1; x <= total ; x++) {
			hidediv = prefix + x;
			document.getElementById(hidediv).style.display = "none";
		}
		if (document.getElementById(showdiv) != null) {
			document.getElementById(showdiv).style.display = "block";
		}
	}
} */

function showFields(prefix, toggle) {
	if (toggle == 1) {
		document.getElementById(prefix).style.display = "block";

	} else if (toggle == 0) {
		document.getElementById(prefix).style.display = "none";
	}
}


/* ShowUserLocation() */
function showUserLocation(val, total, prefix) {
	var showdiv = prefix + val;

	var divarray = document.getElementsByTagName('div');
	for (x=0; x < divarray.length; x++) {
		if (divarray[x].className == "UserLocation") {
			var test = divarray[x].id;
			document.getElementById(test).style.display = "none";
		}
	}

	if (document.getElementById(showdiv) != null) {
		document.getElementById(showdiv).style.display = "block";
	}
}


/* showTimeInterval()
	displays time interval options based on user input
*/
function showTimeInterval(toggle) {
	if (toggle == 1) {
		document.getElementById('datepredefined').style.display = "block";
		document.getElementById('datecustomrange').style.display = "none";
	} else if (toggle == 2) {
		document.getElementById('datepredefined').style.display = "none";
		document.getElementById('datecustomrange').style.display = "block";
	}
}


/* Check all Printers checkboxes */
function checkAll() {
	for (var i = 0; i < document.forms[0].printers.length; i++) {
		document.forms[0].printers[i].checked = true;
	}
}

/* Uncheck all Printers checkboxes */
function unCheckAll() {
	var unchecked = false;
	for (var i = 1; i < document.forms[0].printers.length; i++) {
		if (document.forms[0].printers[i].checked == false)
			unchecked = true;
		}
	if (unchecked)
		document.forms[0].printers[0].checked = false;
}
	
