/* Global variables */
var isNav4, isNav6, isIE4, hasDOM;
var intervalID;
var coll = "";
var styleObj = "";
if(parseInt(navigator.appVersion) >= 5 && document.getElementsByTagName("*")) {
	hasDOM = true;
	coll = 'getElementById("';
	styleObj = '").style';
	if(navigator.appName == "Netscape") {
		isNav6 = true;
	}
}else if(parseInt(navigator.appVersion) >= 4){
	if(navigator.appName == "Netscape") {
		isNav4 = true;
	} else {
		isIE4 = true;
		coll = "all.";
		styleObj = ".style";
	}
}

var agt=navigator.userAgent.toLowerCase();
var isMac = (agt.indexOf("mac")!=-1);

var agentP = navigator.userAgent;
var macIE=false;
if ((agentP.indexOf("Mac")!=-1) && isIE4) {
  macIE=true;
}

function addWindowLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function popUp(theURL,name,width,height,more) {
		var popup=window.open(theURL,name,'width='+width+',height='+height+','+more);
		popup.moveTo((screen.width-width)/2, (screen.height-height)/2);		popup.focus();
}

function isValidEmail(email)
{
	var x = email;
	//var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	var filter  = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if (filter.test(x)) return true;
	else return false;
}

function insertImage(sSRC) {
	return "<IMG SRC='" + sSRC + "' BORDER='0'>"
}

function writeFlashContent(swf, width, height) {
	
	var strOut="";
	
	strOut =  "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0' width='" + width + "' height='" + height + "' id='" + swf + "'><param name='movie' value='" + swf + "'><param name='quality' value='high'><embed src='" + swf + "' quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='" + width + "' height='" + height + "'></embed></object>"

	return strOut;
}

function ToggleDivVisibility(divRef) {
	if (document.getElementById(divRef).style.visibility == "visible")
		hide(divRef);
	else
		show(divRef);
}

function ToggleDivDisplay(divRef) {
	if (document.getElementById(divRef).style.display == "block")
		hideDisplay(divRef);
	else
		showDisplay(divRef);
}

//This function is used to store a caret position of a textbox and to insert text at the caret position of a textbox
//Note: To use these functions: ensure the textarea input control contains the attributes:
//								ONSELECT="storeTextBoxCaret(this);"
//								ONCLICK="storeTextBoxCaret(this);"
//								ONKEYUP="storeTextBoxCaret(this);"
//		To execute the insert at the carat, for example if a button where to perform this action, then ensure the button contains the attribute:
//								ONCLICK="insertAtTextBoxCaret(this.form.aTextArea, this.form.aText.value);"
function storeTextBoxCaret (txtTextBox) {
	if (txtTextBox.createTextRange)
		txtTextBox.caretPos = document.selection.createRange().duplicate();
}
function insertAtTextBoxCaret (txtTextBox, text) {
	if (txtTextBox.createTextRange && txtTextBox.caretPos) {
		var caretPos = txtTextBox.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	}
	else
		txtTextBox.value  += text;
}

//This function shifts one table row at a time in a specific direction
function shiftTableRow(tblId, direction, fldCellToSwap) {
    var tblRef = document.getElementById(tblId);
    
    rowIndex = parseInt(window.event.srcElement.parentElement.parentElement.index);
    if((rowIndex == 0 && direction > 0) || (rowIndex == tblRef.rows.length - 1 && direction < 0) || (rowIndex > 0 && rowIndex < tblRef.rows.length - 1)) {
		if(tblRef.rows[rowIndex].cells[fldCellToSwap].swapNode) {
			tblRef.rows[rowIndex + direction].cells[fldCellToSwap].swapNode(tblRef.rows[rowIndex].cells[fldCellToSwap]);
		}
		else {
			swapHTML = tblRef.rows[rowIndex + direction].cells[fldCellToSwap].innerHTML;
			tblRef.rows[rowIndex + direction].cells[fldCellToSwap].innerHTML = tblRef.rows[rowIndex].cells[fldCellToSwap].innerHTML;
			tblRef.rows[rowIndex].cells[fldCellToSwap].innerHTML = swapHTML;
		}
    }
}

//This function takes in an ordered list of table row id's and sets each row to their respective position
function shiftTableRows(tblId, tblPositionIds, fldCellToSwap) {
	var tblRef = document.getElementById(tblId);
	var arrPositionIds = tblPositionIds.split(',');
	
	for(i=0; i<=tblRef.rows.length-1; i++) {
		var curRow = tblRef.rows[i];
		var curRowId = curRow.id;
		
		for(j=0; j<=arrPositionIds.length-1; j++) {
			if (curRowId == arrPositionIds[j]) {
				curRow.cells[fldCellToSwap].swapNode(tblRef.rows[j].cells[fldCellToSwap]);
				
				curRow.id = tblRef.rows[j].id;
				tblRef.rows[j].id = curRowId;
				
				break;
			}
		}
	}
}

function renderProductSearchScript(curPath, resultListCreatedFromSearchQualifier, resultListCreatedFromSearchValue, searchTermQualifier, productCategoryQualifier, resultsPerPageQualifier, txtSearchTerm, productCategoryDropDownListId, resultsPerPageDropDownListId, arrProductPropertiesDropDownClientIds, arrProductPropertiesDropDownDataBaseIds) {
	var searchURL;
	
	searchURL  = curPath + '?' + resultListCreatedFromSearchQualifier + '=' + resultListCreatedFromSearchValue;
	searchURL += '&' + searchTermQualifier + '=' + ((document.getElementById(txtSearchTerm) != null) ? escape(document.getElementById(txtSearchTerm).value) : "");
	searchURL += '&' + productCategoryQualifier + '=' + ((document.getElementById(productCategoryDropDownListId) != null) ? document.getElementById(productCategoryDropDownListId).options[document.getElementById(productCategoryDropDownListId).selectedIndex].value : "");
	searchURL += '&' + resultsPerPageQualifier + '=' + ((document.getElementById(resultsPerPageDropDownListId) != null) ? document.getElementById(resultsPerPageDropDownListId).options[document.getElementById(resultsPerPageDropDownListId).selectedIndex].value : "");
	
	for(i=0; i<arrProductPropertiesDropDownClientIds.length; i++) {
		searchURL += '&' + arrProductPropertiesDropDownDataBaseIds[i] + '=' + document.getElementById(arrProductPropertiesDropDownClientIds[i]).options[document.getElementById(arrProductPropertiesDropDownClientIds[i]).selectedIndex].value;
	}
	
	return searchURL;
}

function getCoordinate(sCollection, delimiter, element) {
	var aCollection = sCollection.split(delimiter);
	return aCollection[element-1];
}

function changeImage(row,s){
	var h = (s==1)?"r":"";
	if(document.images){
		document.images[row].src = eval(h + row + ".src");
	}
}

/*Function to convert object name string or object reference into valid object reference */
function getObjectRef(obj){
	var theObj;
	if(typeof obj == "string"){
		theObj = eval("document." + coll + obj + styleObj);
	}else{
		theObj = obj;
	}
	return theObj;
}

/*Function to convert object name string or object reference into valid object reference */
/* modified to work with nested layers in Netscape 4, if you have more than 8 nested layers, you're insane */
function getNestObjectRef(obj,nst1,nst2,nst3,nst4,nst5,nst6,nst7,nst8){
	var theObj;
	var objNest = '';
	if(typeof obj == "string"){
		theObj = eval("document." + coll + obj + styleObj);
	}else{
		theObj = obj;
	}
	if(isNav4){
		for(i=1;i<=arguments.length-1;i++){
			currArg = eval('nst'+i);
			objNest += 'document.'+currArg+'.';
		}
		theObj = eval(objNest + 'document.' + obj);
	}
	return theObj;
}

/* functions to show an object */
function showDisplay(obj){
	var theObj = getObjectRef(obj);
	theObj.display = "block";
}

/* utility function to hide an object */
function hideDisplay(obj){
	var theObj = getObjectRef(obj);
	if(theObj)theObj.display = "none";
}

/* functions to show an object */
function show(obj){
	var theObj = getObjectRef(obj);
	theObj.visibility = "visible";
}

/* utility function to hide an object */
function hide(obj){
	var theObj = getObjectRef(obj);
	if(theObj)theObj.visibility = "hidden";
}

/* utility function that sets the visibility of an object */
function showHide(obj,v){
	var theObj = getObjectRef(obj);
	if(v == 1){
		theObj.visibility = "visible";
	}else{
		theObj.visibility = "hidden";
	}
}

/* utility fn to set x coordinate of positionable object */
function setObjLeft(obj,x){
	var theObj = getObjectRef(obj);
	if(isNav4||hasDOM){
		theObj.left = x;
	}else{
		theObj.pixelLeft = x;
	}
}


/* utility fn to set y coordinate of positionable object */
function setObjTop(obj,y){
	var theObj = getObjectRef(obj);
	if(isNav4||hasDOM){
		theObj.top = y;
	}else{
		theObj.pixelTop = y;
	}
}

/* function to retrieve the y-coordinate of a positionable object */
function getObjTop(obj){
	var theObj = getObjectRef(obj);
	if(isNav4){
		return theObj.top;
	}else if(hasDOM){
		return parseInt(theObj.top);
	}else{
		return theObj.pixelTop;
	}
}

/* utility function that returns the width of an object in pixels */
function getObjWidth(obj){
	var theObj = getObjectRef(obj);
	if(isNav4){
		return theObj.clip.width;
	}else if(hasDOM){
		return parseInt(theObj.width);
	} else {
		var ox = theObj.pixelWidth;
		return ox;
	}
}

/* utility function that returns the height of the object in pixels */
function getObjHeight(obj){
	var theObj = getObjectRef(obj);
	if(isNav4){
		return theObj.clip.height;
	}else{
		return theObj.clientHeight;
	}
}

/* function to retrieve the x-coordinate of a positionable object */
function getObjLeft(obj){
	var theObj = getObjectRef(obj);
	if(isNav4){
		return theObj.left;
	}else if(hasDOM){
		return parseInt(theObj.left);
	}else{
		var opx1 = theObj.pixelLeft;
		return opx1;
	}
}

/* utility function that moves the specified object by dx pixels to the left;*/
function shiftLeft(obj,dx){
	var theObj = getObjectRef(obj);
	if(isNav4){
		theObj.left -= dx;
	}else if(hasDOM){
		theObj.left = parseInt(theObj.left)-dx;
	}else{
		theObj.pixelLeft -= dx;
	}	
}

/* function to position an object at a specified pixel co-ordinate */
function shiftTo(obj,x,y){
	var theObj = getObjectRef(obj);
	if(isNav4){
		theObj.moveTo(x,y);
	}
	else if(isNav6) {
		theObj.left=x;
		theObj.top=y;
	}
	else{
		theObj.pixelLeft = x;
		theObj.pixelTop = y;
	}
}

/* object clipping function */
function clipObj(obj,cTop,cRight,cBottom,cLeft) {
	var theObj = getObjectRef(obj);
	if(isNav4) {
		theObj.clip.top = cTop;
		theObj.clip.right = cRight;
		theObj.clip.bottom = cBottom;
		theObj.clip.left = cLeft;
	}else {
		theObj.clip = 'rect('+cTop+','+cRight+','+cBottom+','+cLeft+')';
	}
}

/* utility function that returns the available content width space in the browser window */
function getInsideWindowWidth(){
	if(isNav4||isNav6) {
		return window.innerWidth;
	} else {
		var wx = document.body.clientWidth;
		return wx;
	}
}

/* utility function that returns the available content height space in the browser window */
function getInsideWindowHeight(){
	if(isNav4||isNav6) {
		return window.innerHeight;
	} else {
		var wx = document.body.clientHeight;
		return wx;
	}
}

function getLyrHeight(obj){
	var theObj = getObjectRef(obj);
	if(isNav4){
		return theObj.clip.height;
	}else{
		return eval('document.all.' + obj + '.scrollHeight');
	}
}

//This function will set objImg.src to imgSrc
function setImageSrc(objImg, imgSrc) {
	if (!objImg=="")
		window.document.images[objImg].src=imgSrc;
}

//Generic Hex Functions
var hexValues = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","a","b","c","d","e","f");
var intValues = new Array("0","1","2","3","4","5","6","7","8","9");

function validChar(sChar, availChars) {
	for (j = 0; j < availChars.length; j++) {
		if (sChar == availChars[j]) {
			return true;
	   }
	}
	return false;
}
function isHex(string) {
	string = string.replace("#", "");
	
	if (string.length != 6) { 
		return false;
	}
	for (k = 0; k < 6; k++) {
		if (! validChar(string.charAt(k), hexValues)) {
			return false;
	   }
	}
	return true;
}
function changeColor(elementID, sCol) {
	sCol = sCol.toUpperCase();
	sCol = sCol.replace('#', '');
		
	if (isHex(sCol)) {
		eval(elementID + '.style').backgroundColor = sCol;
	}
}

// Since intValues does not contain a period (.) character, this function will return
// whether or not the value is purely numeric (ie: an Integer or not)
function isNumeric(string) {
	if (string.length == 0) { 
		return false;
	}
	for (k = 0; k < string.length; k++) {
		if (! validChar(string.charAt(k), intValues)) {
			return false;
	   }
	}
	return true;
}

function isEmpty(string) {
	if ((string.length==0) || (string==null)) {
		return true;
	}
	else { 
		return false;
	}
}

function validateQuantityInput(sQuantity, sIdentifier) {
	if (isNumeric(sQuantity) && parseInt(sQuantity) > 0) {
		return true;
	}
	else {
		alert('An invalid ' + sIdentifier + ' was entered');
		return false;
	}
}

function pxToNum(pxStr){ // pxStr == 27px, we want 27.	if (pxStr.length > 2) {		n = Number(pxStr.substr(0, pxStr.length-2));		return(n);	}	return(0);}

//Change a forms action property
function changeAction(frmName, newAction) {
	var objForm;
	objForm=eval('document.' + frmName);
	objForm.action=newAction;
}

//Change a forms field property
function changeFormField(frmName, fieldName, newValue) {
	var objForm;
	objForm=eval('document.forms["' + frmName + '"].elements["' + fieldName + '"]');
	objForm.value=newValue;
}

function disableForm(theform) {
	if (document.all || document.getElementById) {
		for (i = 0; i < theform.length; i++) {
			var tempobj = theform.elements[i];
			if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "image" || tempobj.type.toLowerCase() == "reset")
				tempobj.disabled = true;
		}
	}
}

function elementExists(sCollection, delimiter, element) {
	var found = false;
	var aCollection = sCollection.split(delimiter);
	for (var i=0; i<aCollection.length; i++) {
		if (element == aCollection[i]) {
			found = true;
			break;
		}
	}
	
	return found;
}

function dynamicLayerWrite(txt,layerid) {
	if (isNav6)
	{
		var x = document.getElementById(layerid);
		if(x)x.innerHTML = txt;
	}
	else if (isIE4)
	{
		var x = document.all[layerid];
		if(x)x.innerHTML = txt;
	}
}

function dynamicLayerWriteWindowOpener(txt,layerid) {
	if (isNav6)
	{
		var x = window.opener.document.getElementById(layerid);
		if(x)x.innerHTML = txt;
	}
	else if (isIE4)
	{
		var x = window.opener.document.all[layerid];
		if(x)x.innerHTML = txt;
	}
}

//Check or uncheck a range of checkboxes
var allChecked=false;		
function runCheck(frmName) {
	if (allChecked==true) {
		allChecked=false
		chkOnOff=false
	}
	else {
		allChecked=true
		chkOnOff=true
	}
			
	checkAll(frmName, chkOnOff);	
}

function checkAll(field, chkOnOff) {
	for (i = 0; i < field.length; i++)
		field[i].checked = chkOnOff ;
}

//Given a txt, this will find the item in the list with the text txt and set it to selected
function setDropDownSelectedItemByName(dropDown, txt) {
	var i;
	var curSelectedIndex = -1;
	
	for (i=0;i<dropDown.options.length;i++) {
		if (dropDown.options[i].value == txt) {
			curSelectedIndex = i;
			break;
		}
	}
	
	if (curSelectedIndex >= 0)
		dropDown.selectedIndex = curSelectedIndex;
}

function changeWindowFocus(URL)
{
	opener.focus();
	opener.location=URL;
	return false;
}