var arrChanges		= new Array();	//Used in the LogCBChange() and LogValueChange() functions
var lCount			= 0;			//Used in the LogCBChange() and LogValueChange() functions
var sVersion = window.navigator.userAgent; //Get the user again..this is used for mac related detection


	

function ClearCache()
{
	document.location = "/access/frmmessage.aspx?act=42";
}

function XmlEncode(sString)
{
	sString = sString.replace(/&/g,  "&amp;");
	sString = sString.replace(/</g,  "&lt;");
	sString = sString.replace(/>/g,  "&gt;");
	sString = sString.replace(/'/g,  "&apos;");
	return sString;
}

function StripSpecial(sString)
{
	return sString.replace(/[\!\@\#\^\&\*\(\)\_\+\-\=\<\>\?\:\;\'\"{\}\\\/]/g, "")
}





function LogValueChange(lIDVal,bOn)
{
	//lIDVal = value passed in; lOn = whether it's being removed or added (0 or 1).	
	// Build functionID/value string
	var sChange = lIDVal + "," + bOn;
	// this was placed to handle the crazy if problem, when true the IF would not execute
	if (bOn == 1)
		bRev = 0;
	else
		bRev = 1;
	var sRev	=  lIDVal + "," + bRev;
	// Get length of array
	var lLen = arrChanges.length;
	// We first need to check if we have already logged this functionID
	for(var i=0; i<lLen; i++)
	{
		// need to check the control to compare the values
		if (arrChanges[i] != null)
		{
			arrTemp1 = arrChanges[i].split(",");
			arrTemp2 = sRev.split(",");
			if(arrTemp1[0] == arrTemp2[0])
			{
				lLen = i;
				sChange = null;
				break;
			}
		}
	}
	arrChanges[lLen] = sChange
}


function checkBrowser(sSite)
{
	//sSite (community or access) is for community or access
	var sVersion = window.navigator.userAgent;

	if (sSite == 'community')
	{
		//get he browser version
		sRev = sVersion.substring(sVersion.indexOf("MSIE") + 4, sVersion.indexOf(";", sVersion.indexOf("MSIE")));
		if(sRev < 5 || isNaN(sRev)) 
			top.location = "/community/browser.aspx";
	}
	else
	{
		if (sVersion.indexOf("MSIE") == -1)
		{
			//non-microsoft browser
			sRev = parseFloat(sVersion.substring(sVersion.indexOf("Netscape6")+10));
			//check rev
			if (sVersion.indexOf("Netscape/7") == -1) //check for version 7
				if(sRev < 6.2 || isNaN(sRev)) 
					parent.location = "/access/browser.aspx";
		}
		else //Microsoft Internet Explorer
		{
			sRev = sVersion.substring(sVersion.indexOf("MSIE") + 5, sVersion.indexOf(";", sVersion.indexOf("MSIE")));
			//check rev for access we tell them we need version 5 or better but we will allow a 4 of IE
			if(sRev < 4)
				parent.location = "/access/browser.aspx";
		}
	}
}	








function PopUpWin(sPage, lWidth, lHeight,sScroll)
{
	if(!lWidth)
		lWidth = 680;
	if(!lHeight)
		lHeight = 400;
	if(!sScroll)
		sScroll = 'yes';
	mywin = open(sPage,'PopUp','width=' + lWidth + ',height=' + lHeight + ',scrollbars=' + sScroll + ',toolbar=no,location=no,status=no,resizable=yes,menubar=no');
	if(mywin)
		mywin.focus();
}

function setFocus()
{
	var oForm = document.formLogin;
	var oUser = oForm.vsLogonName;
	if(oUser.value == "")
		oUser.focus();
	else
		oForm.vsPassword.focus();
}

function URLEncode(sVal)
{
	// Use regular expressions to replace problem characters
	var regExpr
	// Replace "&"
	regExpr = /&/g
	sVal = sVal.replace(regExpr, "%26")
	// Replace "#"
	regExpr = /#/g
	sVal = sVal.replace(regExpr, "%23")
	// Replace " "
	regExpr = / /g
	sVal = sVal.replace(regExpr, "%20")
	// Replace double quotes
	regExpr = /"/g
	sVal = sVal.replace(regExpr, "%22")
	// Replace single quotes
	regExpr = /'/g
	sVal = sVal.replace(regExpr, "%27")
	regExpr = /\//g
	sVal = sVal.replace(regExpr, "%2F")
	return sVal;
}

function HTMLEncode(sVal)
{
	// Use regular expressions to replace problem characters
	var regExpr
	// Replace "<"
	regExpr = /</g
	sVal = sVal.replace(regExpr, "&lt;")
	// Replace ">"
	regExpr = />/g
	sVal = sVal.replace(regExpr, "&gt;")
	return sVal;
}

function HTMLUnEncode(sVal)
{
	// Use regular expressions to replace problem characters
	var regExpr
	// Replace "<"
	regExpr = /&lt;/g
	sVal = sVal.replace(regExpr, "<")
	// Replace ">"
	regExpr = /&gt;/g
	sVal = sVal.replace(regExpr, ">")
	return sVal;
}

function GetCookie(sName)
{
	var aCookie = document.cookie.split("; "); 
	for (var i=0; i < aCookie.length; i++) 
	{
		//determin if we are working with a cookie Dictionary
		if (aCookie[i].indexOf("&") != -1)
		{
			//with cookie dictionary the cookie name gets mixed up with the first 
			//crumb value when spliting by the "&" so we have to strip off the name
			aCookie[i] = aCookie[i].substring(aCookie[i].indexOf("=") + 1, aCookie[i].length);
			//split into an array
			var aDic = aCookie[i].split("&");
			//look at each value
			for (var x=0; x < aDic.length; x++)
			{
				// split a value into the name and value ex DE=4 aCrumb[0] = DE aCrumb[1] = 4
				var aCrumb = aDic[x].split("=");
				if (sName == aCrumb[0]) 
					return unescape(aCrumb[1]);
			}
		}
		else
		{
			//Not cookie dictionary
			var aCrumb = aCookie[i].split("=");
			//	This was added when we went to dot net...we had added the vitural root to the cookies
			//	and this funcion wouldn't work alert("access_" + sName == aCrumb[0])
			if (sName == aCrumb[0] || "access_" + sName == aCrumb[0]) 
				return unescape(aCrumb[1]);
		}
	}
}
function getCookie2(name)
{ 
	// use: getCookie("name");
	var re = new RegExp(name + "=([^;]+)");
	var value = re.exec(document.cookie);
	return (value != null) ? unescape(value[1]) : null;
}

function setCookie(name, value)
{ 
	// use: setCookie("name", value);
	var today = new Date();
	var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days	
	//The access_ was added when we went to dotnet...the aspx function adds the virtaul root to every cookie and the java script functions 
	//needed to do the same
	document.cookie="access_" + name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
}


function findString(fullS,subS)
{
	// finds subS in fullS
	for( var i=0; i < fullS.length; i++ )
	{
		if(fullS.substring(i,i+subS.length) == subS)
		{
			return true;
		}   
	}
	return false;
}

function GetSelectBoxValue(oSelect)
{
	if(oSelect)
		return oSelect.options[oSelect.selectedIndex].value;
	else
		return "0";
}

function Resubmit10Key(oForm, oSubmitBtn)
{
	oSubmitBtn.disabled = true;
	oForm.submit();
}

//fuction to check or uncheck all checkboxes in a form...pass the form and true to check, false to uncheck
function ModifyAllCheckBoxes(oForm, bCheck)
{
	for (i=0; i<oForm.elements.length; i++)
	{
		sType  = oForm.elements[i].type;
		if (sType == "checkbox")
		{
			if (bCheck)
				oForm.elements[i].checked = true;
			else
				oForm.elements[i].checked = false;
		}
	}
}

function LocateMyIframe(oItem, oFrame, bLeft, lAdjustX, lAdjustYUp, lAdjustYDown)
{
    //oItem is the item your clicking on
    //oFrame is the frame you want to move
    //bLeft is take on the left position of the item you clicked on
    //lAdjustX is a value to move the X positions + or -
    //lAdjustYUp lets you ajust the position for X when the iframe poops up ABOVE the item you clicked on
    //lAdjustYUp lets you ajust the position for X when the iframe poops up BELOW the item you clicked on
    var eL=0;var eT=0;
    for(var p=oItem; p&&p.tagName!='BODY'; p=p.offsetParent)
    {
        eL +=p.offsetLeft;
		eT += p.offsetTop;
    }
    var eH = oItem.offsetHeight;
    var eW = oItem.offsetWidth;
    var dH = oFrame.style.pixelHeight;
    var dW = oFrame.style.pixelWidth;
    var sT = document.body.scrollTop;

    if (bLeft)
		oFrame.style.left=eL;
		
	//if we are off the screen move us back to the left
	if(eL+eW+dW + lAdjustX > document.body.clientWidth)
		oFrame.style.left=eL - dW + eW + lAdjustX
	else
	{
		//Used to tweak the position if needed...some elements have funny 0,0 poistions and you have to addjust a little
		if (lAdjustX != 0)
			oFrame.style.left = eL + lAdjustX
	}
	
		
    if(eT-dH >= sT && eT+eH+dH > document.body.clientHeight+sT)
		oFrame.style.top=eT-dH + lAdjustYUp;
    else
		oFrame.style.top=eT+eH + lAdjustYDown;
}

function ResizeMyIFrame(oFrame, lHeight, lWidth)
{
	//width and height should be a precentage of the window ex 80 would give you 80%
	if(lWidth > 0)
		document.all(oFrame.name).style.width = document.body.offsetWidth * (lWidth / 100); 
	document.all(oFrame.name).style.height = document.body.offsetHeight * (lHeight / 100);
}

function CloseFrameWin()
{
	parent.document.getElementById(this.name).style.display="none";
}

function ToggleImageUp(sLink,bDelay)
{
	var oImage;
	var sString = "oImg" + sLink
	if(document.getElementById(sString))
		oImage = document.getElementById(sString);
	else
		oImage = parent.document.getElementById(sString);
	if(oImage)
	{
		if(bDelay)
		{
			if(document.getElementById(sString))
				setTimeout("document.getElementById(" + sString + ").src = '/assets/images/wiparrow_up.gif';",100);
			else
				setTimeout("parent.document.getElementById(" + sString + ").src = '/assets/images/wiparrow_up.gif';",100);
		}
		else
			oImage.src = "/assets/images/wiparrow_up.gif"
	}
}

function ToggleImageDn(sLink,bDelay)
{
	var oImage;
	var sString = "oImg" + sLink
	if(document.getElementById(sString))
		oImage = document.getElementById(sString);
	else
		oImage = parent.document.getElementById(sString);
	if(oImage)
	{
		if(bDelay)
		{
			if(document.getElementById(sString))
				setTimeout("document.getElementById('" + sString + "').src = '/assets/images/wiparrow_down.gif';",100);
			else
				setTimeout("parent.document.getElementById('" + sString + "').src = '/assets/images/wiparrow_down.gif';",100);
			
		}
		else
			oImage.src = "/assets/images/wiparrow_down.gif"
	}
}

function SetSelectBoxOption(oSelect, sValue)
{
	if(oSelect.options)
	{
		for(var lCount=0; lCount<oSelect.options.length; lCount++)
			if(oSelect.options[lCount].value == sValue)
			{
				oSelect.options[lCount].selected = true;
				return;	
			}
	}
}

function CheckForUndefined(vValue)
{
	if(vValue == "undefined" || typeof vValue == "undefined")
		return ""
	else
		return vValue;
}

function GetOrderEditCookie()
{
	return CheckForUndefined(GetCookie("lOrderEditRecID_" + GetCookie("LI")))
}

function SaveOrderInfo(lRecID)
{
	// At checkout we need the order id and order type
	setCookie("lOrderEditRecID_" + GetCookie("LI"), lRecID);
}

function ShowOrderEditModeSection(lRecID)
{
	// At checkout we need the order id and order type
	parent.frmTop.document.getElementById("oOrderModeID").innerHTML = lRecID;
	parent.frmTop.document.getElementById("oOrderEditStatus").style.display = "block";
}

function ClearOrderInfo()
{
	// At checkout we need the order id and order type
	setCookie("lOrderEditRecID_" + GetCookie("LI"), "");
	// Now hide the order edit mode section in frmTop
	parent.frmTop.document.getElementById("oOrderEditStatus").style.display = "none";
}

function ToggleDotLabel(sLabel, bToggle)
{
	document.getElementById('oDotLabel').innerHTML = sLabel;
	if(bToggle)
	{
		document.getElementById('oDotLabel').style.display = "block";
		document.getElementById('oloading').style.display = "block";
		document.getElementById('oMain').style.display = "none";
	}
	else
	{
		document.getElementById('oDotLabel').style.display = "none";
		document.getElementById('oloading').style.display = "none";
		document.getElementById('oMain').style.display = "block";
	}
	return true;
}

function IsValidParSelection(oParList, oPreviousValue, bUpdatePreviousValue)
{
	if(oParList && oParList.value == "none")
	{
		// Reset the previous value
		if(oPreviousValue.value == "")
			SetSelectBoxOption(oParList, "0");
		else
			SetSelectBoxOption(oParList, oPreviousValue.value);
		return false;
	}
	else
	{
		if(bUpdatePreviousValue)
		// They passed in the hidden field that keeps track of the last valid selection
		// Let's update it if the current selection is valid.
			oPreviousValue.value = oParList.value
		return true;
	}
}
function trim(str)
{
	return((""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
}