
//
// Cookie Managament
//
function GetCookie(cookieName)
{
	if (document.cookie != "")
	{
		cookieArray = document.cookie.split("; ");
		for(i = 0; i < cookieArray.length; i++)
		{
			if (cookieArray[i].split("=")[0] == cookieName)
				return cookieArray[i].split("=")[1];
		}
	}
	return "";
}

//
// Leave off the third argument completely to set browser cookie
//
function SetCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire + "; path = /";
}

function SetInnerHtmlValue(objId, txt)
{
	if(document.getElementById)
	{
		var obj = document.getElementById(objId);
		if (obj)
		{
			obj.innerHTML = txt;
			return true;
		}
		else return false;
	}
	else if(document.all)
	{
		obj = document.all[objId]
		if (obj)
		{
			obj.innerHTML = txt;
			return true;
		}
		else return false;
	}
	else if(document.layers)
	{
		obj = document.layers[objId];
		if (obj != null)
		{
			with(obj.document)
			{
				open();
				write(txt);
				close();
				return true;
			}
		}
		else return false;
	}
	else return false;
}

//
// Product rotations
//
function NewImage(arg)
{
	if (document.images)
	{
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function ChangeImage()
{
	if (document.images && (preloadFlag == true))
	{
		for (var i=0; i<ChangeImage.arguments.length; i+=2)
		{
			document[ChangeImage.arguments[i]].src = ChangeImage.arguments[i+1];
		}
	}
}

function CheckPreload()
{
	PreloadImages();
}

/*
var preloadFlag = false;
function PreloadImages()
{
	if (document.images)
	{
		//image1 = NewImage("rotationimages/rt2.jpg");
		//image2 = NewImage("rotationimages/rt3.jpg");
		//image3 = NewImage("rotationimages/rt4.jpg");
		//preloadFlag = true;
	}
}
*/

function OpenWin(theURL,winName, wWidth, wHeight, withScrollbars, withResizeable, horzPos, vertPos, boolSetCookie)
{
	winHandle = '';
	vAdjustment = -50;
	if (horzPos == "" && vertPos == "")
	{
		if (screen)
		{
			horzPos = Math.ceil((screen.width - wWidth)/2);
			vertPos = Math.ceil((screen.height - wHeight)/2) + vAdjustment;
		}
		else
		{
			horzPos = 0;
			vertPos = 0;
		}
	}
	winChild = window.open(theURL, winName,'toolbar=no,location=no,scrollbars='+withScrollbars+',resizable='+withResizeable+',width='+wWidth+',height='+wHeight+', left='+horzPos+', top='+vertPos+'');
	setTimeout("winChild.focus()", 500);
	if (boolSetCookie) SetCookie("childWinUrl", theURL, 24);
}

//
// Especially for Disclaimer
//
function OpenDisclaimerWin(theURL,winName, wWidth, wHeight, withScrollbars, withResizeable, horzPos, vertPos, boolChangeDecision)
{
	if(GetCookie("__DISCLAIMER") == "" || boolChangeDecision == true)
	{
		winHandle = '';
		vAdjustment = 0;
		if (horzPos == "" && vertPos == "")
		{
			if (screen)
			{
				horzPos = Math.ceil((screen.width - wWidth)/2);
				vertPos = Math.ceil((screen.height - wHeight)/2) - vAdjustment;
			}
			else
			{
				horzPos = 0;
				vertPos = 0;
			}
		}
		winHandle = window.open(theURL, winName,'toolbar=no,location=no,scrollbars='+withScrollbars+',resizable='+withResizeable+',width='+wWidth+',height='+wHeight+', left='+horzPos+', top='+vertPos+'');
		setTimeout("winHandle.focus()", 250);
		return false;
	}
	else
	{
		return true;
	}
}

//
// Typical Usage
//
/*
var page = new PageQuery(location.search);
var myValue = page.getValue("a");
if (myValue == "-1") myValue = "history.html";
*/
function PageQuery(q)
{
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q)
	{
		for(var i=0; i < this.q.split("&").length; i++)
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++)
		{
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return -1;
	}
	this.getParameters = function()
	{
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++)
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
     this.getLength = function() { return this.keyValuePairs.length; }
}

function GetUrl(strUrl)
{
	location.href = strUrl;
	return false;
}

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}


function validRequired(formField,fieldLabel,msg)
{
	var result = true;

	if (formField.value == "")
	{
		alert(fieldLabel + " - " + msg);
		formField.focus();
		result = false;
	}

	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}

	return result;
}

function validEmail(formField,fieldLabel,required, msg)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel, msg))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert(fieldLabel + " - " + msg);
		formField.focus();
		result = false;
	}

  return result;

}


function validNum(formField,fieldLabel,required, msg)
{

	formField.value = formField.value.replace(' ', '');

	var result = true;

	if (required && !validRequired(formField,fieldLabel, msg))
		result = false;

 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert(fieldLabel + '' + msg);
			formField.focus();
			result = false;
		}
	}

	return result;
}

function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
	}

	return result;
}

function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

 	if (result)
 	{
 		var elems = formField.value.split("/");

 		result = (elems.length == 3); // should be three components

 		if (result)
 		{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}

  		if (!result)
 		{
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();
		}
	}

	return result;
}

function validLength(formField,fieldLabel,fieldLengthMax,fieldLengthMin,required)
{

	formLen = formField.value.length;
	//alert ("Form Length = " + formField.value.length);

	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

 	if (result)
 	{
 		if (formLen < fieldLengthMin)
 		{
 			alert('Your entry is too short for "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}

 		if (formLen > fieldLengthMax)
 		{
 			alert('Your entry is too long for "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
	}

	return result;
}

function SubmitForm(strFrmNm)
{
	eval("document.forms['" + strFrmNm + "'].submit();");
}

// MENU SYSTEM

function Toggle(item)
{
	obj=document.getElementById(item);
	visible=(obj.style.display!="none")
	if (visible)
	{
		obj.style.display="none";
		SetCookie(item, "closed", 1);
	}
	else
	{
		obj.style.display="block";
		SetCookie(item, "open", 1);
	}
	return false;
}

function InitMenu(item)
{
	obj=document.getElementById(item);
	menustatus = GetCookie(item);
	if (menustatus == "open")
	{
		obj.style.display="block";
	}
	else
	{
		obj.style.display="none";
	}
}

var myArray = new Array();
function InitMenuSystem()
{
	myArraySize = myArray.length;
	for(var i = 0; i < myArraySize; i++)
	{
        InitMenu(myArray[i]);
	}
}

function Trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

//
// Typical usage
// onClick="Animator(this, 'PLEASE WAIT')"
//
var strAni = ".";
var intDot = 0;
var intInterval = 315;
var ObjBtnRef = null;
var boolBtnClicked = false;
var strMsgRef = "";
function Animator(objBtn, strMsg, bgcolor, forecolor)
{
	if (!boolBtnClicked)
	{
		boolBtnClicked = true;
		ObjBtnRef = objBtn;
		// ObjBtnRef.disabled = true;
		ObjBtnRef.style.cursor = "wait";
		if (bgcolor != null) ObjBtnRef.style.backgroundColor = bgcolor;
		if (forecolor != null) ObjBtnRef.style.color= forecolor;
		strMsgRef = strMsg;
		setInterval('RunAnima()', intInterval);
	}
}

function RunAnima()
{
	switch(intDot )
	{
		case 1:
			ObjBtnRef.value = strMsgRef+strAni+"  ";
			break;
		case 2:
			ObjBtnRef.value = strMsgRef+strAni+strAni+" ";
			break;
		case 3:
			ObjBtnRef.value = strMsgRef+strAni+strAni+strAni;
			intDot = -1;
			break;
		default:
			ObjBtnRef.value = strMsgRef+"   ";
	}
	intDot++;
}

// Typical usage
// var messages = new MakeArray(COMMA SEPARATED LIST AS ARGUMENTS HERE);
function MakeArray()
{
	for (i = 0; i < MakeArray.arguments.length; i++)
	{
		this[i] = MakeArray.arguments[i];
	}
}

// Don't use for floats
function isInteger(value)
{
  return (parseInt(value) == value);
}

function SetAllCheckBoxes(formName, boolCheck)
{
	objFrm = document.forms[formName];
	if (!objFrm) return;

	for (var i = 0; i < objFrm.elements.length; i++)
	{
		if(objFrm.elements[i].type)
		{
			objFrm.elements[i].checked = boolCheck;
		}
	}
}

function BtnExecute(obj, strMsg)
{
	obj.style.cursor = 'wait';
	obj.disabled = true;
	Animator(obj, strMsg);
	return;
}

function CheckEmail(strEmail)
{
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=strEmail.match(emailPat);

	if (matchArray==null)
	{
		// alert("Email address seems incorrect (check @ and dots)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	if (user.match(userPat)==null)
	{
		// user is not valid
		// alert("Email address username doesn't seem to be valid.");
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null)
	{
		for (var i=1;i<=4;i++)
		{
			if (IPArray[i]>255)
			{
				// alert("Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}

	var domainArray=domain.match(domainPat);
	if (domainArray==null) {
		// alert("Email address domain name doesn't seem to be valid.");
		return false;
	}

	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
	{
		// alert("Email address must end in a three-letter domain, or two letter country.");
		return false;
	}

	if (len<2)
	{
		// var errStr="Email address is missing a hostname!";
		// alert(errStr);
		return false;
	}
	return true;
}

//
// arrExempt is a comma separated list of fields to ignore
//
function ValidateForm(objFrm, arrExempt, arrInts, arrEmails, arrCurrency)
{
	// Only 1 click
	// if (boolBtnClicked) return false;

	// Check exemptions array
	if (arrExempt == null || arrExempt == "")
	{
		arrExempt = new Array();
	}

	// Check ints array
	if (arrInts == null || arrInts == "")
	{
		arrInts = new Array();
	}

	// Check email array
	if (arrEmails == null || arrEmails == "")
	{
		arrEmails = new Array();
	}

	// Check Currency array
	if (arrCurrency == null || arrCurrency == "")
	{
		arrCurrency = new Array();
	}

	// Strip out span tags
	// var re1 = new RegExp("<span.*?>.*?<\/span>", "gi")
	var _strMsgNeedsInt = "Please use an number for";
	var _strMsgNeedsValue = "Please complete";
	var _strMsgNeedsEmail = "Format looks incorrect for ";
	var _strMsgNeedsCurrency = "Please ensure currency format for ";

	for (var i = 0; i < objFrm.elements.length; i++)
	{
		var boolEmptyTest = true;
		var boolIntTest = true;
		var boolEmailTest = true;
		var boolCurrencyText = true;
		var strMsg = "";

		// These must have a value of some kind
		if (objFrm.elements[i].value == "" && !InArray(objFrm.elements[i].name, arrExempt))
		{
			boolEmptyTest = false;
			strMsg = _strMsgNeedsValue;
		}

		// Does this need to be an integers
		else if (objFrm.elements[i].value != "" && InArray(objFrm.elements[i].name, arrInts) && !isInteger(objFrm.elements[i].value))
		{
			boolIntTest = false;
			strMsg = _strMsgNeedsInt;
		}

		// Check for email
		else if (InArray(objFrm.elements[i].name, arrEmails) && !CheckEmail(objFrm.elements[i].value))
		{
			boolEmailTest = false;
			strMsg = _strMsgNeedsEmail;
		}

		// Check for currency
		else if (InArray(objFrm.elements[i].name, arrCurrency) && !IsCurrency(objFrm.elements[i].value))
		{
			boolCurrencyText = false;
			strMsg = _strMsgNeedsCurrency;
		}

		//
		// Final test
		//
		if (!boolEmptyTest || !boolIntTest || !boolEmailTest || !boolCurrencyText)
		{
			objFrm.elements[i].style.border = "1px solid #ff0000";
			objFrm.elements[i].style.margin = "0px";
			objFrm.elements[i].focus();
			if (document.getElementById)
			{
				var itemName = "LABEL_"+objFrm.elements[i].name;
				obj = document.getElementById(itemName);
				if (obj != null)
				{
					alert(strMsg+" '"+obj.innerHTML.replace(/<span.*?>.*?<\/span>/gim, "")+"'");
				}
				else
				{
					alert(strMsg+" '"+objFrm.elements[i].name+"'");
				}
			}
			// Older Browsers only
			else
			{
				alert(strMsg+" '"+objFrm.elements[i].name+"'");
			}
			return false;
		}
	}
	return true;
}

function InArray(strValue, arrArray)
{
	for(i = 0; i < arrArray.length; i++)
	{
		if (strValue == arrArray[i]) return true;
	}
	return false;
}

function IsCurrency(fValue)
{
	if (fValue == "" || fValue == null) return false;
	else if (fValue == 0) return true;
	else if (!parseFloat(fValue)) return false;


	// Test values
	var intTest = fValue;
	if (parseInt(intTest) == fValue)
	{
		return true;
	}

	// Perhaps a float
	else
	{
		fValue = fValue.toString();
		var arrParts = fValue.split(".");

		if (parseInt(arrParts[0]) == arrParts[0] && parseInt(arrParts[1]) == arrParts[1])
		{
			if (parseInt(arrParts[1]) < 100) return true;
			else return false;
		}
		else
		{
			return false;
		}
	}
}

// if (IsCurrency(1.7)) alert("Yes");
// else alert("No");

//
// Typical usage
//  onChange="GoTo(this, 'index.html?changethisTo=')"
//
function GoTo(selObj, strBaseUrl)
{
	var indexVal = eval("selObj.options[selObj.selectedIndex].value");
	if (strBaseUrl != null) eval("location='"+strBaseUrl+indexVal+"'");
	else eval("location='"+indexVal+"'");
}

function StyleObject(layerID) //access a CSS property
{
	if(document.getElementById) return document.getElementById(layerID).style;
	else if(document.all) return document.all[layerID].style;
	else if(document.layers) return document.layers[layerID];
}

// Example
function ToggleVis(layerID)
{
	if(StyleObject(layerID).display=="none")
	{
		StyleObject(layerID).display= "block";
	}
	else
	{
		StyleObject(layerID).display= "none";
	}
}

function UpdateOpenerUrl(strNewUrl, boolClosePopupWindow)
{
	if (window.opener != null)
	{
		window.opener.location.href = strNewUrl;
		window.opener.focus();
		setTimeout('window.close();', 1000);
	}
}

function OpenCertificate(url, name){
	//alert(name);
	urlArray = url.split('#');

	url = urlArray[0];
	subject = urlArray[1];
	//alert (urlArray[1]);
	switch (name){

		case "information":
		iwindow = window.open(url+'?'+subject, name, "status=0, toolbar=0, location=0, menubar=0, directories=0,scrollbars=1,resizable=0,width=500,height=600");
		iwindow.moveTo(10,30);
		iwindow.focus();
		break;

		case "certificate":
		cwindow = window.open(url, name, "status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=1,width=750,height=700");
		cwindow.moveTo(100, 50);
		cwindow.focus();
		break;

		default:
		nw = window.open(url+'#'+subject, name);
		nw.focus();

		break;
	}

}

// FlashManager

function FlashManager(strFile, intWidth, intHeight, strBackgroundColour)
{
	var window_mode = 'transparent' //set this to transparent to make flash movie sit in the document order correctly
	
	document.write('<object\n');
	document.write('id="RokVSA"\n');
	document.write('classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"\n');
	document.write('codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"\n');
	document.write('width="'+intWidth+'"\n');
	document.write('height="'+intHeight+'"\n');
	document.write('align="center">\n');
	document.write('<param name="allowScriptAccess" value="sameDomain" />\n');
	document.write('<param name="movie" value="'+strFile+'" />\n');
	document.write('<param name="quality" value="high" />\n');
	document.write('<param name="bgcolor" value="'+strBackgroundColour+'" />\n');
	document.write('<param name="wmode" value="'+window_mode+'" />\n');	
	document.write('<embed\n');
	document.write('src="'+strFile+'"\n');
	document.write('quality="high"\n');
	document.write('bgcolor="'+strBackgroundColour+'"\n');
	document.write('width="'+intWidth+'"\n');
	document.write('height="'+intHeight+'"\n');
	document.write('name="rokvsa"\n');
	//document.write('align="left"\n');
	document.write('wmode="'+window_mode+'"\n');
	document.write('allowScriptAccess="sameDomain"\n');
	document.write('type="application/x-shockwave-flash"\n');
	document.write('pluginspage="http://www.macromedia.com/go/getflashplayer"\n');
	document.write('/>\n');
	document.write('</object>\n');
}

