var sTDStay;
var tSlider, tMenu;
var sGC_Images = "/UC_Images/";

//==================================================================================
// Body onload utility (supports multiple onload functions)
var UC_arrOnLoad = new Array();
function UC_AddOnLoad(f) {
	if  (window.onload)	{
		if (window.onload != UC_OnLoad) {
			UC_arrOnLoad[0] = window.onload;
			window.onload = UC_OnLoad;
		}		
		UC_arrOnLoad[UC_arrOnLoad.length] = f;
	} else
		window.onload = f;
}
function UC_OnLoad() {
	for (var i=0;i<UC_arrOnLoad.length;i++)
		UC_arrOnLoad[i]();
}
//==================================================================================
// Go to a link
function go(sLink) {document.location = sLink;}
// Trim a string
function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { 
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);}
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { 
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);}
   while (retValue.indexOf(" ") != -1) {
      retValue = retValue.substring(0, retValue.indexOf(" ")) + retValue.substring(retValue.indexOf(" ")+1, retValue.length);}
   return retValue;}
// Send form named "frm" to page
function goForm(sPage) {
	document.frm.action = sPage;
	document.frm.submit();}
// Move a <DIV> to a position
var	bXStop = false;
var	bYStop = false;
var tMove;
function moveDivTo(oDiv, iLeft, iTop, iSpeed, iSpeedTop, bVanish) {
	clearTimeout(tMove);
	if (!bXStop || !bYStop) {
		if (!bXStop) oDiv.style.pixelLeft	+= iSpeed;
		if (!bYStop) oDiv.style.pixelTop	+= iSpeedTop;
		if (iSpeed > 0) {
			if (oDiv.style.pixelLeft >= iLeft) bXStop = true;} else {
			if (oDiv.style.pixelLeft <= iLeft) bXStop = true;}
		if (iSpeedTop > 0) {	
			if (oDiv.style.pixelTop >= iTop) bYStop = true;} else {
			if (oDiv.style.pixelTop <= iTop) bYStop = true;}	
		var sTimeout =	"moveDivTo(" + oDiv.id + ", " + iLeft + ", " + 
						iTop + ", " + iSpeed + ", " + iSpeedTop + ", " + bVanish + ")";
		tMove = setTimeout(sTimeout, 1);} else {
		clearTimeout(tMove);
		oDiv.style.pixelLeft = iLeft;
		oDiv.style.pixelTop = iTop;
		if (bVanish) oDiv.style.display = "none";}}
//==============================================================
// Manages the appearance of a DIV from a random location
// outside the page to given X & Y coordinates in a given
// speed
function flashDiv(oDiv, iLeft, iTop, iSpeed, bNotRandom, iFromLeft, iFromTop, bVanish) {

	if (!bNotRandom) {
		with (Math) {
			var iDivLeft = -400 + parseInt(random() * 1900);
			var iDivTop  = -200 + parseInt(random() * 1100);
			
			if (iDivLeft > 0 && iDivLeft < 1100)
				iDivTop =	(-1) * parseInt(random() * 100);
		}
	} else {
		var iDivLeft = iFromLeft;
		var iDivTop = iFromTop;
	}
	oDiv.style.display = "inline";
	oDiv.style.pixelLeft = iDivLeft;
	oDiv.style.pixelTop = iDivTop;
	
	var iToGoLeft	= iLeft - iDivLeft;
	var iToGoTop	= iTop - iDivTop;
	var iSpeedTop	= parseInt(iToGoTop / iToGoLeft * iSpeed);
	
	if (iToGoLeft < 0) iSpeed = -iSpeed;
	if (iToGoLeft < 0) iSpeedTop = -iSpeedTop;
	
	bXStop = false;
	bYStop = false;
	
	moveDivTo(oDiv, iLeft, iTop, iSpeed, iSpeedTop, bVanish);
}
// Shows a floating div according to mouse position
function showFloatingDiv(oDiv) {
oDiv.style.left		= window.event.clientX + 20;
oDiv.style.top		= window.event.clientY - 100 + document.body.scrollTop;
oDiv.style.display	= "";}
// Hides a floating div
function hideFloatingDiv(oDiv) {
	oDiv.style.display = "none";
}
// Hides a floating div
function hideDiv(oDiv) {
	oDiv.style.display = "none";
}
// Switches a picture on
function on(oImg) {	var src = String(oImg.src).replace(/_off/, "_on");oImg.src = src;}
// Switches a picture off
function off(oImg) {var src = String(oImg.src).replace(/_on/, "_off");oImg.src = src;}
var oLastImg;
// Switches a picture on and stays on until another is on
function onStay(oImg) {
	if (oImg != oLastImg) {
		var src = String(oImg.src).replace(/_off/, "_on");
		oImg.src = src;
		off(oLastImg);
		oLastImg = oImg;}}
var oLastDiv;
// Paging function goes to next page
function pageNext(frmName) {
	with (eval('document.' + frmName)) {
		iPage.value = parseInt(iPage.value) + 1;
		submit();}}
// Paging function goes to previous page
function pagePrevious(frmName) {
	with (eval('document.' + frmName)) {
		iPage.value = parseInt(iPage.value) - 1;
		submit();}}
var iActiveN;
// Open the uploader window
function openUploader(sNotDefault) {
	if (!sNotDefault)
		var y = window.open('/UC/Uploader.asp','Uploader', 'top=250, left=250, width=300, height=300, menubar=0, status=0');
	else
		var y = window.open(sNotDefault,'Uploader', 'top=250, left=250, width=300, height=300, menubar=0, status=0');}
// Open and close static <DIV> of tree
function switchDiv(sDiv) {

	var oDiv = document.getElementById(sDiv);
	if (oDiv) {oDiv.style.display = (oDiv.style.display == "none") ? "inline" : "none"; }
}
// Open and close static <DIV> of tree
function toggleDiv(sDiv, bOn) {

		var oDiv = document.getElementById(sDiv);
		if (bOn)
			oDiv.style.display = "inline";
		else
			oDiv.style.display = "none";
}

// Highlight a category (put it in bold);
var oLastHighlight = "";
function highlight(oA) { 
	if (oLastHighlight != oA) {
		oA.className = (oA.className == "Txt_Blue_12") ? "Txt_Blue_15B" : "Txt_Blue_12";
		oLastHighlight.className = (oLastHighlight.className == "Txt_Blue_12") ? "Txt_Blue_15B" : "Txt_Blue_12";}
	oLastHighlight = oA;}
// Highlight a TD
var oLastHighlightTD = "";
var oLastHighlightA = "";
function highlightTD(oTD, oA, sColorOn, sColorOff) { 
	if (oLastHighlightTD != oTD) {
		oTD.style.backgroundColor = sColorOn;
		oTD.style.fontWeight = "bold";
		if (oA) oA.style.color = "white";
		if (oLastHighlightTD) {
			oLastHighlightTD.style.backgroundColor = sColorOff;
			oLastHighlightTD.style.fontWeight = "normal";
			if (oA)  oLastHighlightA.style.color = "";}}
	oLastHighlightTD 	= oTD;
	oLastHighlightA		= oA;}
	
// Highlight an item with class
var oLastElement = "";
function onClass(oElement, sClassOn, sClassOff, sClassOffAlternate) { 

	if (oLastElement != oElement) {
		oElement.className = sClassOn;
		oElement.onmouseout = null;
		oElement.onmouseover = null;

		if (oLastElement) {
			if (oLastElement.name != "ProductViewed") {
				oLastElement.className = (oLastElement.alternate == "1") ? sClassOffAlternate : sClassOff;
				oLastElement.onmouseover = function() { onData(this,1) }
				oLastElement.onmouseout = (oLastElement.alternate == "1") ? function() { onData(this,0,true) } : function() { onData(this,0) };
			} else
				oLastElement.className = 'Data_On';
			
		}
		oLastElement = oElement;
	}
	
}

	// Switch data TR on/off
function onData(oTR, iSwitch, bAlternate) {
	if (iSwitch==1)
		oTR.className = 'Data_On';
	else {
		if (!bAlternate)
			oTR.className = 'Data';
		else
			oTR.className = 'Data_Alternate';
	}
}

// Make a button in the action bar
function navBarInsert(sHTML) {

	var oNavBar = document.getElementById("NavBar")
	if (oNavBar) oNavBar.innerHTML += sHTML;
}

// Fly a message
function flyer(str, iSleep, oDiv) {

	if (oDiv == null) {
		var sDiv =	"<DIV  id=flyerDiv style=position:absolute><TABLE width=300 class=Admin_DataTable style=\"border:1px solid lightblue\"><TR><TD class=Data align=center style=\"font-size:18px;height:80px;background-color:lightyellow;\" valign=middle><B>" + str + "</B></TD>" + 
								"</TR></TABLE></DIV>";
		document.write(sDiv);
		var oDiv = document.getElementById("flyerDiv");} 
	oDiv.style.display = "inline";
	if (iSleep==null || iSleep == "")
		iSleep = 3500;
	var iLeftTo = 	document.body.offsetWidth / 2 - 400 / 2;
	var iTopTo = 	390 + document.body.scrollTop;
	flashDiv(oDiv, iLeftTo+40, iTopTo, 10, true, -400, iTopTo);
	var tFlyer = setTimeout("flashDiv(" + oDiv.id + ", 1500, " + iTopTo + ", 40,  true, " + iLeftTo  + ", " + iTopTo + ", " + true + ");", iSleep);
}
// Fly a message
function flyerCustom(iSleep, oDiv, iLeft, iTop, bOneWay) {
	if (iSleep==null)
		iSleep = 3500;
	var iLeftTo = 	iLeft;
	var iTopTo = 	iTop;
	flashDiv(oDiv, iLeftTo+40, iTopTo, 10, true, -400, iTopTo);
	if (!bOneWay)
		setTimeout("flashDiv(" + oDiv.id + ", 1500, " + iTopTo + ", 40,  true, " + iLeftTo  + ", " + iTopTo + ", " + true + ");", iSleep);}
//==============================================================
// Fly a message from right
function flyerCustomRight(iSleep, oDiv, iLeft, iTop, bOneWay) {
	if (iSleep==null)
		iSleep = 3500;
	var iLeftTo = 	iLeft;
	var iTopTo = 	iTop;
	flashDiv(oDiv, iLeftTo+40, iTopTo, 15, true, document.body.offsetWidth + 10, iTopTo);
	if (!bOneWay)
		setTimeout("flashDiv(" + oDiv.id + ", 1500, " + iTopTo + ", 40,  true, " + iLeftTo  + ", " + iTopTo + ", " + true + ");", iSleep);}
function popup(file, width, height, top, left, image)
	{	var str, oWin;
		//str = '" width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + ', resizable=0, scrollbars=1"';
		str = ' width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + ', resizable=0, scrollbars=1';
		(image!=null) ? file = file + '?image=' + image : "" ;
		oWin = window.open(file, '_blank', str);}
function hrefPopup(sFile) {
	var oWin = window.open(sFile, 'hrefPopup', 'width=900,height=350,top=200,left=100,resizable=1,toolbar=1');
}
var oLastBold = "";
// Bold an element
function boldThis(sID) {
	
	var oElement = document.getElementById(sID);
	if (oLastBold != "") {
		oLastBold.style.fontWeight = "normal";
		oLastBold.style.fontSize = "";
	}
	oElement.style.fontWeight = "bold";
	oElement.style.fontSize = "12px";
	oLastBold = oElement;
}

// Highlights a TD
function onTD(oTD) {
	oTD.style.backgroundImage = 'url("' + sGC_Images + 'on_bg.gif")';
	document.getElementById('a' + oTD.id).style.color = "#0D2D50";}
// Off highlights a TD
function offTD(oTD) {
	if (sTDStay != oTD.id) {
		oTD.style.backgroundImage = "";
		document.getElementById('a' + oTD.id).style.color = "";}}
// Highlights a TD with a letter
function onLetterTD(oTD) {oTD.style.backgroundImage = "url(" + letter_bg.src + ")"}
// Off highlights a TD with a letter
function offLetterTD(oTD) {
	if (sTDStay != oTD.id) oTD.style.backgroundImage = "";}
// Change status of order
function updateOrder(oSelect, OID, iOldStatus, sAction) {
	if (oSelect.value.split('~')[1] == 99) {
		if (confirm('Delete order no. ' + OID + '?')) {
			frmOrders.iOID_ToChange.value = OID;
			frmOrders.iStatus.value = oSelect.value;
			frmOrders.action = sAction;
			frmOrders.submit()
		} else
			oSelect.value = OID + "~" + iOldStatus;
	} else {
		if (confirm('Change status of order no. ' + OID + '?')) {
			frmOrders.iOID_ToChange.value = OID;
			frmOrders.iStatus.value = oSelect.value;
			frmOrders.action = sAction;
			frmOrders.submit()
		} else
			oSelect.value = OID + "~" + iOldStatus;	}}
// Check length of keyword string
function checkTxt() {
	with (document.frmSearch) {
		if (Keyword.value != "" && trim(Keyword.value).length < 3) {
			alert("You must enter a keyword of at least 3 letters if you want to search by keyword.");
			Keyword.select();
			Keyword.focus();
			return false;
		} else if (Keyword.value == "" && Price.value == "" && sCID.value == "") {
			alert("You must choose at least one search criteria before searching");
			return false;
		} else if (Keyword.value == "" && (Price.value == "" || sCID.value == "")) {
			alert("If you are not searching with a keyword, you must choose a price range AND a department");
			return false;
		}
		LID.value = "";	}}
// Check Email address
function emailCheck(oField) {
	
	var bOK = oField.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);

	if (bOK)
	   return true;
	else {
	   alert('Please enter a valid e-mail address.')
	   oField.focus()
	   oField.select()
	   return false;
	}
}
// Hide / Show select boxes when div pops up
function hideSelect(bHide) {
  	
	var oElements = document.getElementsByTagName('SELECT'); 
	// loop through all select elements in page
	 for(var i = 0; i < oElements.length; i++)
		oElements.item(i).style.visibility = (bHide) ?  "hidden" : "visible";
}
	
// Switch Menu On/Off
function menuSwitch(iSwitch, bDelayDone) {

	if (iSwitch == 0 && bDelayDone == null) {
		tMenu = setTimeout('menuSwitch(0, true)', 500);
		return false;
	} else if (iSwitch == 1) {
		clearTimeout(tMenu);
	}
	
	clearTimeout(tSlider);	
	var oMenu = new xLayerFromObj("divMenu");
	if (oMenu) {
		
		if (iSwitch == 1) {
			hideSelect(true);
			tSlider = setTimeout("slideLayer('divMenu', " + oMenu.getX() + ", " + oMenu.getY() + ", 0, 85, 15)", 1);
		} else {
			hideSelect(false);
			tSlider = setTimeout("slideLayer('divMenu', " + oMenu.getX() + ", " + oMenu.getY() + ", -150, 85, 15)", 1);
		}
	}
}

// Check the length of a TEXTAREA
function checklength(oField, sFieldName, iMaxLength) {

	if (oField.value.length > iMaxLength) {
		alert("The field '" + sFieldName + "' is limited to " + iMaxLength + " characters, and has been trimmed accordingly.");
		oField.value = oField.value.substring(0, iMaxLength);
		return false;
	}
}

// Slide a layer nicely
function slideLayer(sLayer, iFromX, iFromY, iToX, iToY, iStep) {

	var oLayer = new xLayerFromObj(sLayer);
	var iStepX = ((iToX - iFromX) >= 0) ? iStep : -iStep;
	var iStepY = ((iToY - iFromY) >= 0) ? iStep : -iStep;
	var iNewX = oLayer.getX() + iStepX;
	var iNewY = oLayer.getY() + iStepY;
	
	if (iStepX > 0) {
		if (iNewX > iToX) iNewX = iToX;
	} else {
		if (iNewX < iToX) iNewX = iToX;
	}
	
	if (iStepY > 0) {
		if (iNewY > iToY) iNewY = iToY;
	} else {
		if (iNewY < iToY) iNewY = iToY;
	}
	
	oLayer.moveTo(iNewX, iNewY);
	
	if (iNewX == iToX && iNewY == iToY)
		clearTimeout(tSlider);	
	else
		tSlider = setTimeout("slideLayer('" + sLayer + "', " + iFromX + ", " + iFromY + ", " + iToX + ", " + iToY + ", " + iStep + ")", 1);
}

// Switch Menu Item Background
function onMenu(oMenuItem, iSwitch) {
	oMenuItem.style.backgroundImage = (iSwitch==1) ? 'url("' + sGC_Images + 'menu_itemon.gif")' : '';
}

 function getXPosOf(eElement){
	if(!eElement) 
		return 0;
	return getXPosOf(eElement.offsetParent)+eElement.offsetLeft;
}

function getYPosOf(eElement){
	if(!eElement) 
		return 0;
	return getYPosOf(eElement.offsetParent)+eElement.offsetTop;
}
// Show absolutely positioned <div> in area of the screen of the Positioned Element
function toggleHelpDiv(sDivID, sPositionedElementID, iFactorX, iFactorY) {

	var oDiv = document.getElementById(sDivID);
	var oElementID = document.getElementById(sPositionedElementID);
	
	oDiv.style.left = getXPosOf(oElementID) + iFactorX;
	oDiv.style.top = getYPosOf(oElementID) + iFactorY;
	oDiv.style.display = (oDiv.style.display == "none") ? "inline" : "none";
	
}
function test(obj) {
	var out = '';
	for (var k in obj) {
		out += k +' = '+ obj[k] +String.fromCharCode(10);
	}
}

// Make sure it's a number
function validateNum( field , fieldName) {

	var valid   = "0123456789"
	var ok      = "yes";
	var temp    = "";
	for ( var i = 0; i < field.value.length; i++ ) {
		temp = "" + field.value.substring( i, i + 1 );
		if ( valid.indexOf( temp ) == "-1" ) ok = "no";
	}
	if ( ok == "no" ) {
		alert( "Sorry, the " + fieldName + " field accepts only NUMBERS - please do not use any letters or characters except numbers." );
		field.select();
		field.focus();
		return ( false );
	}
	return ( true );
}

function setCookie(name, value) { 
		var today = new Date();
		var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000); // plus 365 days
    	document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
  }
  function getCookie(name) { 
	    var re = new RegExp(name + "=([^;]+)");
	    var value = re.exec(document.cookie);
	    return (value != null) ? unescape(value[1]) : null;
  }
  
  // Switch to the entry with the first letter as keyed
function findFirstLetter(sForm) {

	
	// Find the key to start with
	if (window.event) {
		var sKeyCode = window.event.keyCode;
		// Eliminate ctrl, alt, shift, etc. keys
		switch(parseInt(sKeyCode)) {
			case 8: case 9: case 13: case 16: case 17: case 18: case 27: case 32: case 33: 
			case 34: case 35: case 36: case 37: case 38: case 39: case 40: case 46: 
			case 91: case 93: case 18: case 18: case 18: case 18: case 18: case 18: 
				return false;
		};
		if (event.ctrlKey) return false;
		var sLetter = String.fromCharCode(sKeyCode)		
	}
	/*
	var oTRs = oTable.getElementsByTagName('TR');

	var bFound = false;
	var sFirstLetterOnPage = "";
	
	for (var i =0; i < oTRs.length; i++) {

		if (oTRs.item(i).getAttribute('id') != "") {

			var oTR = oTRs.item(i);
			var sHTML = oTR.childNodes[0].innerHTML; 
			
			// Remember the first letter on this page
			if (sFirstLetterOnPage == "") sFirstLetterOnPage = sHTML.substring(0,1).toUpperCase();
			// Starts with the same letter
			if (sStartWith == sHTML.substring(0,1).toUpperCase()) {
				
				// Has not been clicked yet
				if (!oTR.getAttribute('bClicked')) {
					oTR.click();
					oTR.setAttribute ("bClicked", true);
					bFound = true;
					break;
				}
			}
		}
	}*/
	
	// If not found the letter on this page, start resubmitting the page
	/*if (!bFound) {
		if (sFirstLetterOnPage < sStartWith) {
			document.frmSearch.sStartWith.value = sStartWith;
			document.frmSearch.submit();
		}
	}*/
	with (eval('document.' + sForm)) {
		sStartWith.value = sLetter;
		// Reset "order by" values
		iPage.value = 1;
		iOrderBy.value = 0;
		iOrderDir.value = 0;
		submit();
	}
}

// Toggle "wait" cursor. Action: 0 - Show, 1 - Hide
function showWaitCursor (oDocument, iAction) {
	
	var oImg = oDocument.getElementById("divWaitingCursor")
	if (iAction == 0) {
		
		//sHTML = "<div style=\"width:100%;height:100%;position:absolute;background-color:yellow;cursor:wait;z-index:120\"></div>";
		if (oImg == null) {
			var sHTML = '<img style="position:absolute;left:0px;right:0px;z-index:99;cursor:wait" id=divWaitingCursor src="/Images/header_client1.gif" width=30 height=30>';
			oDocument.body.insertAdjacentHTML('afterBegin', sHTML);
		}
			oDocument.onmousemove = getMouseXY;
			
	} else {
		oImg.style.display = "none";
		oDocument.onmousemove = null;
	}
}

function getMouseXY(e) {
	var tempX = event.clientX + document.body.scrollLeft;
	var tempY = event.clientY + document.body.scrollTop;
	var oImg = document.getElementById('divWaitingCursor');
	oImg.style.left = tempX - 15;
	oImg.style.top = tempY - 15;
  }  


// Simulate a click on an object
function simClick(sID) {
	document.getElementById(sID).click();
}

// Colors all TDs of a TR or sets the color off (for onmouseover effect)
function TR_colorTDs(oTR, sBackColor, sColor, bOff) {
	
	for (var i=0; i < oTR.childNodes.length; i++) {
	
		if (bOff == null) {
			oTR.childNodes[i].style.backgroundColor = sBackColor;
			oTR.childNodes[i].style.color = sColor;
		} else {
			oTR.childNodes[i].style.backgroundColor = "";
			oTR.childNodes[i].style.color = "";
		}
	}
}
					