// /studio/studioCommon.js

var timeoutObject = null;


// Check the cookie set on login that is deleted when the user logs or times out.

function checkForExpiredSession() {
	if(cookieRead("FireSignLogin") == null) {
		window.location = "/jsp/login/login.jsp";
		return(true);
	}
	else
		return(false);
}


function cookieDelete(cookieName) {
	document.cookie = cookieName + "=;expires=Thu, 01-Jan-70 00:00:01 GMT;path=/;";
}


function cookieRead(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while(c.charAt(0) == ' ')
			c = c.substring(1, c.length);
		if(c.indexOf(nameEQ) == 0)
			return(c.substring(nameEQ.length, c.length));
	}
	return(null);
}


function cookieSet(cookieName, cookieValue, nDays) {
	var today = new Date();
	var expire = new Date();
	if(nDays==null || nDays==0)
		nDays=1;
	expire.setTime(today.getTime() + 3600000 * 24 * nDays);
	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + ";path=/;";
}


function disablePageTimeout() {
	if(timeoutObject != null)
		clearTimeout(timeoutObject);
}


function getNumRecsSelected(theTable) {
	var selCount = 0;

	records = theTable.getRecordSet().getRecords();
	for(var i = 0;i < records.length;i++) {
		var rec = records[i];
		var flag = rec.getData("checked");
		if(flag)
			++selCount;
	}

	return(selCount);
}


function getSelectedRecordIds(theTable) {
	var ids = "";

	records = theTable.getRecordSet().getRecords();
	for(var i = 0;i < records.length;i++) {
		var rec = records[i];
		var flag = rec.getData("checked");
		if(flag) {
			if(ids.length > 0)
				ids = ids + ",";
			ids = ids + rec.getData("id");
		}
	}

	return(ids);
}


function resetPageTimeout() {
	if(timeoutObject != null)
		clearTimeout(timeoutObject);
	timeoutMinutesStr = cookieRead("user_timeout_minutes");
	timeoutObject = setTimeout("sessionTimedOut()", parseInt(timeoutMinutesStr) * 60000);
}


function returnBrowserName() {
	var detect = navigator.userAgent.toLowerCase();
	var ndx;
	var majorVersNbr;

	if(detect.indexOf("chrome") >= 0)
		return("Chrome");

	if((ndx = detect.indexOf("msie")) >= 0) {
		majorVersNbr = parseInt(detect.charAt(ndx+5));
		if(majorVersNbr < 7)
			return("unsupported");
		else
			return("IE");
	}

	if(detect.indexOf("firefox") >= 0)
		return("Firefox");

	if(detect.indexOf("safari") >= 0)
		return("Safari");

	if((ndx = detect.indexOf("opera")) >= 0) {
		majorVersNbr = parseInt(detect.charAt(ndx+6));
		if(majorVersNbr < 9)
			return("unsupported");
		else
			return("Opera");
	}

	return("unsupported");
}


function returnPlatform() {
	var detect = navigator.userAgent.toLowerCase();
	if(detect.indexOf("macintosh") >= 0)
		return("Mac");
	if(detect.indexOf("windows") >= 0)
		return("Windows");
	if(detect.indexOf("linux") >= 0)
		return("Linux");
	return("unknown");
}


function sessionTimedOut() {
	document.forms[0].target = "_top";
	document.forms[0].action = "/SessionTimedOut.do";
	document.forms[0].submit();
}


function submitenter(myfield, e) {
	var keycode;

	resetPageTimeout();

	if(window.event)
		keycode = window.event.keyCode;
	else if(e)
		keycode = e.which;
	else
		return(true);

	if(keycode == 13) {
		findButtonClicked();
		return(false);
	}
	else {
		var s = String.fromCharCode(keycode);
		myfield.value += s;
		return(true);
	}
}


/*
==================================================================
ltrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function ltrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
rtrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function rtrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return rtrim(ltrim(str));
}


/*
 * Disallow punctuation characters in input fields, like quotes, colons etc.
 */

function FilterKeyPress() {
	var inputKey = event.keyCode;
	var evtRetVal = true;

	if((inputKey >= 33) && (inputKey <= 39))		// ! " # $ % & '
		evtRetVal = false;
	if((inputKey >= 42) && (inputKey <= 44))		// * + ,
		evtRetVal = false;
	if((inputKey >= 46) && (inputKey <= 47))		// . /
		evtRetVal = false;
	if((inputKey >= 58) && (inputKey <= 64))		// : ; < = > ? @
		evtRetVal = false;
	if(inputKey == 92)
		evtRetVal = false;
	if((inputKey == 94) || (inputKey == 96))		// ^ '
		evtRetVal = false;
	if((inputKey == 124) || (inputKey == 126))		// | ~
		evtRetVal = false;

	event.returnValue = evtRetVal;
	return(evtRetVal);
}


/* stuff moved here from header.jsp */

var currentSection;

function setMenuItemImage(section, state) {
	var pic = document.getElementById("nav_" + section);
	var src = "/images/nav_" + section + "_";

	if((section == currentSection) && (section != "home") && (state == "off"))
		src = src + "on";			// current section, show as selected
	else
		src = src + state;
	pic.src = src + ".png";
}

function setTrailPath(itemsArray) {
	var txt = "&nbsp;&nbsp;";
	var nbrItems = itemsArray.length;
	for(var i = 0;i < nbrItems;++i) {
		if(i > 0)
			txt = txt + "&nbsp;>&nbsp;";
		txt = txt + itemsArray[i];
	}
	document.getElementById("trailLabel").innerHTML = txt;
}

