//******************************
// All toplevel pages.


function resizeCheck()
{
	if (oldWidth < window.innerWidth || oldWidth > window.innerWidth || oldHeight > window.innerHeight || oldHeight < window.innerHeight ) 
	{
		document.f.randnocache.value=(new Date()).getTime();
		document.f.submit();
	}
}

function doOnload()
{
	//overwrite this function in main component if necessary.
}

var cellHeight = "100%";
function saveWindowDimensions()
{
	if(navigator.appName == 'Netscape')// && !window.oldWidth)
	{
		cellHeight = window.innerHeight-153; //144 = header height + footer height
		window.onresize = resizeCheck;
		window.oldWidth = window.innerWidth;
		window.oldHeight = window.innerHeight;
	}
}

function savePopupWindowDimensions()
{
	if(navigator.appName == 'Netscape')// && !window.oldWidth)
	{
		cellHeight = window.innerHeight-67; //67 = popup header height + popup footer height
		window.onresize = resizeCheck;
		window.oldWidth = window.innerWidth;
		window.oldHeight = window.innerHeight;
	}
}


//***************//
//* SA SCRIPTS *//
//***************//

//<!--Guide-->
var pop = null;
function AgentPopup()
{
	if( pop != null)
	{
		document.all.agentLink.innerText = "Show";
		pop.close();
		pop = null;
	}
	else
	{

		document.all.agentLink.innerText = "Hide";
		var loc = "/msagent/controlcenter.htm";
		var width = 400;
		var height = 400;

		pop = window.open(loc, "controlcenter", 'width=' + width + ',height=' + height + ', status=yes,menubar=no,resizable=no,top=0,left=600,screenX=0,screenY=0');
		pop.focus();

	}
}

function AgentPopupDownload()
{
		var loc = "/msagent/settings.htm";
		var width = 650;
		var height = 550;

		pop = window.open(loc, "controlcenter", 'width=' + width + ',height=' + height + ', status=yes,menubar=no,resizable=no,top=0,left=600,screenX=0,screenY=0');
		pop.focus();
}

//**********************************//
//* Event Scripts :COUNT STUFF *//
//**********************************//

var eventcount = 0;
function disallowChars()
{
	//alert(window.event.keyCode);
	if(isNaN(String.fromCharCode(window.event.keyCode)) && window.event.keyCode != 46)
	{
		event.returnValue = false;
	}
}

function checkEventCount()
{
	if(window.event.repeat)
	{
		eventcount ++;
	}
	if (eventcount>1000)
	{
		alert('Did your key get stuck?');
		eventcount =0;
	}
}

function incrementDecrement(box)
{
	increment = 1;
	if(window.event.keyCode ==38 || window.event.keyCode ==40)
	{
		var currValue = parseFloat(box.value);
		if(isNaN(currValue) || currValue < 0) currValue = 0;
		checkEventCount();
		if(window.event.keyCode ==38)
		{
			box.value = currValue+increment;
			box.select();
			if(!window.event.repeat) eventcount = 0;
		}
		else if(window.event.keyCode ==40 && currValue !=0)
		{
			box.value = currValue-increment;
			box.select();
			if(!window.event.repeat) eventcount = 0;
		}
	}
}

function percentEvent(box)
{
	if(window.event.keyCode ==38 || window.event.keyCode ==40)
	{
		var currValue = parseFloat(box.value);
		if(isNaN(currValue)) currValue = 0;
		checkEventCount();
		if(window.event.keyCode ==38 && currValue < 100)
		{
			box.value = currValue + 1;
			box.select();
			if(!window.event.repeat) eventcount = 0;
		}
		else if(window.event.keyCode ==40 && currValue > 0)
		{
			box.value = currValue - 1;
			box.select();
			if(!window.event.repeat) eventcount = 0;
		}
		
	}
}

function checkAmountAndClear(box, minNum, maxNum, clearIt)
{
	clearIt.value='';
	checkMinMaxAmount(box, minNum, maxNum);
}

//**********************************//
//* Check Scripts                  *//
//**********************************//

function checkMaxAmount(box, num)
{
	if (!isNaN(num) && parseFloat(box.value) > num)
	{
		box.value = num;
	}
}

function checkMinAmount(box, num)
{
	if (!isNaN(num) && parseFloat(box.value) < num)
	{
		box.value = num;
	}
}

function checkMinMaxAmount(box, minNum, maxNum)
{
	checkMinAmount(box, minNum);
	checkMaxAmount(box, maxNum);
}

//******************************
// CONFIRM COMMAND

function ConfirmDelete(description, query)
{
	var sure = confirm("Are you sure you want to permanently delete " + UnescapeDQ(description) + "?");
	if (sure == true) location = query;
}

function ConfirmInactive(description, query)
{
	var sure = confirm("Are you sure you want to inactivate " + UnescapeDQ(description) + "?");
	if (sure == true) location = query;
}

function ConfirmUnlink(description, query)
{
	var sure = confirm("Are you sure you want to unlink " + UnescapeDQ(description) + "?");
	if (sure == true) location = query;
}

//******************************
// POPUP WINDOWS

function SASizablePopup(query, height, width)
{
	sasizablewin = window.open(query, "sapopupsizablewin", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,height=" + height + ",width=" + width);
	if (sasizablewin.opener == null) sasizablewin.opener = self;
	sasizablewin.focus();
}

function SAPleaseWaitPopup(query)
{
	var width = 130;
	var height = 150;
	var x = (window.screen.availWidth/2) - (width/2); 
	var y = (window.screen.availHeight/2) - (height/2);
	var position = "left=" + x + ",top=" + y;
	var size = "width=" + width + ",height=" + height;
	if (query == null)
	{
		query = "BaseAuthServlet?forwardURL=/PleaseWait.jsp";
	}
	sapleasewait = window.open(query, 'sapopuppleasewait', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,' + size + ',' + position);
	if (sapleasewait.opener == null) sapleasewait.opener = self;
	sapleasewait.focus();
}

function ClosePleaseWaitPopup()
{
	var width = 150;
	var height = 50;
	var x = window.screen.width + 1000; 
	var y = window.screen.height + 1000;
	var position = "left=" + x + ",top=" + y;
	//window.alert(position);
	sapleasewait = window.open( '', 'sapopuppleasewait', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,' + position);
	sapleasewait.close();
}

function SASmallPopup(query)
{
	sasmallwin = window.open(query, 'sapopupsmallwin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=300');
	if (sasmallwin.opener == null) sasmallwin.opener = self;
	sasmallwin.focus();
}

function SAMediumPopup(query)
{
	samediumwin = window.open(query, 'sapopupmediumwin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=400,height=350');
	if (samediumwin.opener == null) samediumwin.opener = self;
	samediumwin.focus();
}

function SALargePopup(query)
{
	salargewin = window.open(query, 'sapopuplargewin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=600,height=400');
	if (salargewin.opener == null) salargewin.opener = self;
	salargewin.focus();
}

function SAHelpPopup(query)
{
	salargewin = window.open(query, 'sapopuphelp', 'toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=600,height=400');
	if (salargewin.opener == null) salargewin.opener = self;
	salargewin.focus();
}

function OtherPopup(query)
{
	otherwin = window.open(UnescapeDQ(query), 'sapopupotherwin');
	if (otherwin.opener == null) otherwin.opener = self;
	otherwin.focus();
}

function FlashPopup(filename)
{
	var win = filename;
	var i = win.indexOf(".");
	if(i != -1)
	{
		win = win.substring(0, i);
	}
	win = win.replace(/-| /gi, "");
	filename = escape(filename);

	var loc= "ViewFlash.jsp?fn=" + filename;
	var flashpop = window.open(loc, win, 'width=750,height=500, menubar=no,resizable=yes,screenX=0,screenY=0');

	if (flashpop.opener == null) flashpop.opener = self;
	flashpop.focus();
	flashpop.moveTo(0,0);
}

function CloseWindowAndForwardOpener(location)
{
	window.opener.location = location;
	self.close();

}//	end CloseWindowAndForwardOpener


//******************************
// PICK DATE

function PickDate(form, field)
{
	var nwquery = "CalendarServlet2?frm=" + form + "&fld=" + field + "&dt=" + eval("document." + form + "." + field + ".value");
	datewin = window.open(nwquery, "datewin", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=385,height=320");
	if (datewin.opener == null) datewin.opener = self;
	datewin.focus();
}

//******************************
// PICK PURCHASER

function PickPurchaser(form, field, field2, field3)
{
	var nwquery = "PurchaserServlet?frm=" + form + "&fld=" + field + "&fld2=" + field2 + "&fld3=" + field3;
	purchwin = window.open(nwquery, "sapopuppurch", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=385,height=320");
	if (purchwin.opener == null) purchwin.opener = self;
	purchwin.focus();
}

//******************************

function isDisplayable(filename)
{
	var i	= filename.lastIndexOf(".");
	if (i == -1)
	{
		return false;
	}
	var ext = filename.substring(i,filename.length).toLowerCase();
	if (ext == ".gif" || ext == ".jpg" || ext == ".jpe" || ext == ".jpeg")
	{
		return true;
	}
	return false;
}


//******************************
//  Check Radio Button

function CheckRadioButton(form, element, index)
{
	eval("document." + form + "." + element + "[" + index + "]" + ".checked = true");
}

//******************************



//******************************
//  Submit Opener's Form

function SubmitOpenersForm(form)
{
	eval("window.opener.document." + form + ".submit()");
}

//******************************


//******************************
//  Change Opener's Location

function ChangeOpenersLocation(url)
{
	eval("window.opener.document." + form + ".submit()");
}

//******************************



//******************************
//  Toggle Check Box

function ToggleCheckbox(form, element)
{
	if (eval("document." + form + "." + element + ".checked == true"))
	{
		eval("document." + form + "." + element + ".checked = false");
	}
	else
	{
		eval("document." + form + "." + element + ".checked = true");
	}
}

//******************************

//******************************
//  Forward to Login

    function ForwardToLogin(url)
	{
		winName = window.name;

		if (winName != null)
		{
			//	Check for SA Popup window
			if (winName.length > 7)
			{
				winPrefix = winName.substr(0, 8);
				if(winPrefix == "sapopup")
				{
					window.opener.location = url;
					self.close();
					return;
				}
			}

			//	Check for XactTotal popup
			if (winName.length > 4)
			{
				winPrefix = winName.substr(0, 5);
				if(winPrefix == "xtpop")
				{
					window.opener.location = url;
					self.close();
					return;
				}
			}
		}
		window.location = url;
	}

//******************************

//  Format Phone Number Fields Dynamically

	function dynamicPhoneFieldFormat(textField)
	{
//		alert("Field value before processing = [" + textField.value + "]");
		var origValue	= textField.value;
		var origLength	= origValue.length;
		var newValue	= "";
		var strippedVal	= "";
		var bogus		= false;
		var justNumbers	= true;
		var digitCount	= 0;

		for (i = 0; i < origLength; i++)
		{
			c = origValue.charAt(i);
			if (!isNaN(parseInt(c)))
			{
				digitCount = digitCount + 1;
				strippedVal = strippedVal + c;
			}
		}

		if (origLength > 0 && (isNaN(parseInt(origValue.charAt(origLength-1))) || isBogusPhone(origValue)))
		{
			bogus = true;
			alert("This field accepts only digits.\nPhone number formatting will be added as you type.");
		}
		newValue = strippedVal

		//  Digits between 3 and 8
		if (digitCount > 3 && digitCount < 8)
		{
			newValue = newValue.substr(0, 3) + "-" + newValue.substr(3);
			textField.value = newValue;
    		return;
		}
		else if (digitCount > 7)
		{
			newValue = "(" + newValue.substr(0, 3) + ") " + newValue.substr(3, 3) + "-" + newValue.substr(6);
			textField.value = newValue;
    		return;
		}

		textField.value = newValue;
//		textField.focus();
//		textField.select();
		return;
	}

//******************************
//  isBogusPhone

	function isBogusPhone(str)
	{
		var bogus = false;
		if (str.length == 0)
		{
			return bogus;
		}

		for (i = 0; i < str.length; i++)
		{
			c = str.charAt(i);
			if (isNaN(parseInt(c)))
			{
				switch(c)
				{
					case "(":
					{
						if (i != 0)
						{
							bogus = true;
						}
						break;
					}
					case ")":
					{
						if (i != 4)
						{
							bogus = true;
						}
						break;
					}
					case " ":
					{
						if (i != 5)
						{
							bogus = true;
						}
						break;
					}
					case "-":
					{
						if (i != 3 && i != 9)
						{
							bogus = true;
						}
						break;
					}
					default:
					{
						bogus = true;
						break;
					}
				}
			}
		}
		return bogus;
	}
//******************************
//  Strip non-numeric characters from a String

	function getOnlyDigits(val)
	{
		var newVal = "";
		for (i = 0; i < val.length; i++)
		{
			c = val.charAt(i);
			if (!isNaN(parseInt(c)))
			{
				newVal = newVal + c;
			}
		}
		return newVal;
	}
//******************************

//  Forward Popup or Main Window


    function ForwardPopupOrMain(PopupURL, MainURL)
	{
		var winName = window.name;
		var winPrefix = "";

		if (winName != null)
		{
			if (winName.length > 7)
			{
				winPrefix = winName.substr(0, 8);
				if(winPrefix == "sapopup")
				{
					window.location = PopupURL;
					return;
				}
			}
		}
		
		window.location = MainURL;
	}

//******************************


//******************************
//  Unescape douple quotes

	function UnescapeDQ(str)
	{
		return str.replace(/~!~/gi, "\"");
	}

//******************************


//******************************
//  Check Supported Browser Type

	function isSupportedBrowser()
	{
		var version = parseFloat(navigator.appVersion);
		var browserUAString = navigator.userAgent;

		//  If browser is MS Internet Explorer
		if (browserUAString.indexOf("MSIE") != -1)
		{
			var msieVersion = browserUAString.slice(browserUAString.indexOf("MSIE ") + 5);
			if (parseFloat(msieVersion) >= 5.5)
			{
				return;
			}

			//	Get the actual MSIE version number rather than the number it reports in navigator.appVersion
			version = parseFloat(msieVersion);
		}
		alert("The browser you are using (" + navigator.appName + " " + version + ") is not supported by XactInfo.com.\n\nXactInfo.com supports Microsoft Internet Explorer 5.5 and higher.");
	}

//******************************


//******************************
//  ChangeBrowserNotifyOption

	function ChangeBrowserNotifyOption()
	{
		if (document.popunsupportedbrowser.UPUnsupportedBrowserPopSubmit.checked == true)
		{
			document.popunsupportedbrowser.submit();
		}
		else
		{
			window.close();
		}
		return;
	}

//******************************

//******************************
//  Get the version number of MSIE Browser.
//	If the browser is not MSIE, returns -1
//	Otherwise returns the version as a float.

	function getMSIEVersionNumber()
	{
		var previousString	= "MSIE ";
		var trailingString	= ";";
		var appString		= "Microsoft";
		
		if (navigator.appName.indexOf(appString) == -1)
		{
			return -1;
		}

		var versionString = navigator.appVersion;
		var begin = previousString.length + versionString.indexOf(previousString);
		var end = versionString.indexOf(trailingString, begin);
		versionString = versionString.substring(begin, end);
		return parseFloat(versionString);
	}
//******************************

