// USEFUL FUNCTIONS

Array.prototype.inArray = function (value)
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
{
	var i;
	for (i=0; i < this.length; i++) {
		// Matches identical (===), not just similar (==).
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

// MOUSE OVER CODE

over = new Array() 
out = new Array()

function imageHover(id) { 
	document.getElementById(id).src = over[id].src; 
} 
 
function imageReset(id) {
	if (typeof(activePages) != "undefined") {
		if (!activePages.inArray(id)) {
			document.getElementById(id).src = out[id].src;
		}
	} else {
		document.getElementById(id).src = out[id].src;
	}
} 

function highlightActivePages(ap) {
	if (ap.length > 0) {
		for (i = 0; i < ap.length; i++) {
			if (document.getElementById(ap[i]) != null) {
				document.getElementById(ap[i]).src = over[ap[i]].src;
			}
		}
	}
}

function showActiveSubCat(asc) {
	if (asc.length > 0) {
		for (i = 0; i < asc.length; i++) {
			document.getElementById(asc[i]).style.display = "block";
			document.getElementById(asc[i]).style.zIndex = 50;
		}
	}
}

// SIMPLE BROWSER CHECK
var isNS4 = (document.layers) ? true : false;
var isIE4 = (document.all && !document.getElementById) ? true : false;
var isIE5 = (document.all && document.getElementById) ? true : false;
var isNS6 = (!document.all && document.getElementById) ? true : false;

var lastDisplayedLayer = "";

// SUBCAT DISPLAY CODE
function showSubMenu(layerName) {
	if (typeof(delayTimer) != "undefined") {
		clearTimeout(delayTimer);
		if (lastDisplayedLayer != layerName) {
			hideSubMenu(lastDisplayedLayer);
		}
	}
	document.getElementById(layerName).style.display = "block";
	lastDisplayedLayer = layerName;
}

function delayHideSubMenu(layerName) {
	if (typeof(activeSubCat) != "undefined") {
		if (!activeSubCat.inArray(layerName)) {
			delayTimer = window.setTimeout("hideSubMenu('" + layerName + "')", 1000);
		}
	} else {
		delayTimer = window.setTimeout("hideSubMenu('" + layerName + "')", 1000);
	}
}

function hideSubMenu(layerName) {
	if (typeof(activeSubCat) != "undefined") {
		if (!activeSubCat.inArray(layerName)) {
			document.getElementById(layerName).style.display = "none";
		}
	} else {
		document.getElementById(layerName).style.display = "none";
	}
}

// FORM PROCESSING
function checkForm () {
	failed = 0;
	for (x = 0; x < requiredFields.length; x++) {
		field = eval("document.Form1." + requiredFields[x])
		if (field.value == '') {
			field.style.background = '#B40A0A';
			failed = 1;
			if (typeof(focusField) == "undefined") {
				focusField = field;
			}
		}
	}
	if (failed == 1) {
		focusField.focus();
		return false;
	} else {
		return true;
	}
}
function resetBackground (element) {
	element.style.background = "#f6f6f6";
}

function resetText (element) {
	element.value = "";
	element.style.color = "#000000";
}

function highlightField (element) {
	element.style.border = "1px solid #B40A0A";
}

function resetField (element) {
	element.style.border = "1px solid black";
}

function toggleDiv (div) {
	var tempDiv = document.getElementById(div);
	if (tempDiv.style.display == "block") {
		tempDiv.style.display = "none";
	} else {
		tempDiv.style.display = "block";
	}
}

plusIcon = new Image()
plusIcon.src = "Assets/images/zinnia/buttons/plus.gif" 
minusIcon = new Image()
minusIcon.src = "Assets/images/zinnia/buttons/minus.gif" 

function toggleIcon (icon) {
	var tempImg = document.getElementById(icon);
	if (tempImg.src.indexOf("plus.gif") != -1) {
		tempImg.src = minusIcon.src; 
	} else {
		tempImg.src = plusIcon.src; 
	}
}

// CART IMAGE POPUP FUNCTIONS
document.onmousemove = getMousePos;

function getMousePos(e){
	if(isNS6){mouseX = e.layerX; mouseY = e.layerY;}
	if(isIE5){mouseX = event.x; mouseY = event.y;}
}

function showCartImage(layerName) {
	moveCartImage(layerName);
	document.getElementById(layerName).style.display = "block";
}

function hideCartImage(layerName) {
	document.getElementById(layerName).style.display = "none";
}

function moveCartImage(layerName) {
	if (isIE5) {
		document.getElementById(layerName).style.left = mouseX - document.body.scrollLeft + 15 + "px";
		document.getElementById(layerName).style.top = mouseY + document.body.scrollTop + 0 + "px";
	}
	if (isNS6) {
		document.getElementById(layerName).style.left = mouseX - document.body.scrollLeft + 15 + "px";
		document.getElementById(layerName).style.top = mouseY + document.body.scrollTop + 0 + "px";
	}
}

function retFalse (src, arg) {
  arg.IsValid = false;
}

	
function onEnterKeyDown(btn,e){
     var keyCode = null;
     if(e.which ) {
       keyCode = e.which;
        if(13 == keyCode ) {
		var button = document.getElementById(btn);
		button.click();
		return false;
		}
     } else if(e.keyCode ) {
       keyCode = e.keyCode;
        if(13 == keyCode ) {
        e.returnValue=false;
	    e.cancel = true;
		var button = document.getElementById(btn);
		button.click();
		}
	}
     return true;
}

function embedObject(filename, flashName, bgColor, width, height, variableName, variableValue) {
	document.write('<object style="vertical-align: bottom" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="https://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="'+width+'" height="'+height+'" id="'+flashName+'Flash" align="middle">\n');
	document.write('<param name="allowScriptAccess" value="sameDomain" />\n');
	document.write('<param name="movie" value="'+filename+'" />\n');
	document.write('<param name="quality" value="high" />\n');
	document.write('<param name="WMode" value="transparent">\n');
	document.write('<param name="bgcolor" value="'+bgColor+'" />\n');
	if (variableName != '')	{
		document.write('<param name="flashVars" value="'+variableName+'='+variableValue+'" />');
		document.write('<embed src="'+filename+'" flashVars="'+variableName+'='+variableValue+'" quality="high" bgcolor="'+bgColor+'" width="'+width+'" height="'+height+'" name="'+flashName+'Flash" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" />');
	} else {
		document.write('<embed src="'+filename+'" quality="high" bgcolor="'+bgColor+'" width="'+width+'" height="'+height+'" name="'+flashName+'Flash" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" />');
	}
	document.write('</object>\n');
	ff3hack();
}

function ff3hack() {
	var agt = navigator.userAgent.toLowerCase();
	if (agt.indexOf("firefox/3.0") >= 0) {
		document.getElementById("ProductImage").style.marginTop = "160px";
	}
}

function get_from_QueryString(item){ 
	var theQueryString = window.location.search.substring(1);
	var theQueryStringArray = theQueryString.split('&'); 
	
	for (theItem in theQueryStringArray) {	
		//find 
		if(String(theQueryStringArray[theItem]).indexOf(item+"=",0)!= -1){
			return theQueryStringArray[theItem].substring(theQueryStringArray[theItem].indexOf("=")+1); 				
			//return value if found
		}
	} //end for
	return false //if not found
}

function DrawAnchorLink(Anchor,Text,Class){
                
	// Get page url
	var Dir = location.href.substring(0,location.href.lastIndexOf('/')+1);
	var URL = location.href.substring(Dir.length,location.href.length+1);

	// Draw a link to an on-page anchor
	document.write('<a href="' + URL + '#' + Anchor + '" class="' + Class + '">' + Text + '</a>');
}

/* called by draw_leaf_tree function in depstku.js which passes the department that should be active */
function updateHeader(whichDept){
	if(document.getElementById("MainNav_"+whichDept)){
		//change header nav image
		document.getElementById("MainNav_"+whichDept).src="Assets/images/zinnia/MainNav/MainNav_"+whichDept+"On.gif";
	}
} 