/**
 *   consolidated.js
 *
 *   This file consolidates commonly used JavaScript functions
 *   found in these files previously: top.js, rollImg.js, 
 *   openwin.js, popupwin.js, and euiwindow.js
 */



/*
 *   Force the page to the top frame of the window.
 */

 self.name="topFrame";



/**
 *  Function:		rollImgSetup
 *
 *  Version History:	2001-04-02, Richard Coles, Initial Version
 *
 *  Description:	Use this function in a page body 'onLoad' event handler 
 *			to initialize variables rollover image processing via 'rollImg'.
 *			The document.images array is parsed.  Variables for image rollover
 *			are create for all images with names ending in '_on', '_off',
 *			'_at' and '_over' (Note: image name does not include the 
 *			file-type; e.g.: .gif, .jpg, etc).  Complementary images 
 *			(e.g.: an '_on' for each '_off') must exist in the same 
 *			source directory.
 *
 *  Syntax:		rollImgSetup()
 *
 *  Parameters:		none
 *
 *  Return:		none
 */

function rollImgSetup() {
  var boolArrayDefined;
  var i;
  var result;
  var strOnImgSrc;
  var strOffImgSrc;
  var strMsg = "";
  for (i = 0; (i < document.images.length); i++) {
    strOnImgSrc = "";
    strOffImgSrc = "";
    if (result = document.images[i].src.match(/(.+)_on\.(.+)$/i)) { // "on" image
      strOnImgSrc = document.images[i].src;
      strOffImgSrc = result[1] + "_on." + result[2];
    }
    else if (result = document.images[i].src.match(/(.+)_off\.(.+)$/i)) { // "off" image
      strOnImgSrc = result[1] + "_on." + result[2];
      strOffImgSrc = document.images[i].src;
    }
    if ((strOnImgSrc != "") && (strOffImgSrc != "") && (typeof document.images[i].name != undef)) {
      eval("((typeof " + document.images[i].name + "0) == \"object\") ? boolArrayDefined = 1 : boolArrayDefined = 0;");
      if (boolArrayDefined) continue;
      if ((document.images[i].height > 0) &&
	  (document.images[i].width > 0)) {
        eval(document.images[i].name + "0 = new Image(document.images[i].height,document.images[i].width);");
        eval(document.images[i].name + "1 = new Image(document.images[i].height,document.images[i].width);");
      }
      else {
	eval(document.images[i].name + "0 = new Image();");
	eval(document.images[i].name + "1 = new Image();");
      }
      eval(document.images[i].name + "0.src = strOffImgSrc;");
      eval(document.images[i].name + "1.src = strOnImgSrc;");
    }
  }
}



/**
 *  Function:		rollImg
 *
 *  Version History:	2001-04-02, Richard Coles, Initial Version
 *
 *  Description:	Use this function in an 'onMouseOver' or 'onMouseOut'
 *			event handler for rollover image processing.
 *
 *  Special Notes:	You must call 'imgRollSetup()' from the page body
 *			'onLoad' event for variable initialization.
 *
 *  Syntax:		rollImg(image-name, state)
 *
 *  Parameters:		image-name: (input) 'name' value from image tag
 *			state: (input) 1 for 'on'; 0 for 'off'
 *
 *  Returns:		void
 */

function rollImg(strImgName, intState) {
  var boolArrayDefined = 0;
  if (intState <= 0)
    intState = 0;
  else
    intState = 1;
  eval("((typeof " + strImgName + intState + ") == \"object\") ? boolArrayDefined = 1 : boolArrayDefined = 0;");
  if (!boolArrayDefined) return;
  eval("document.images." + strImgName + ".src = " + strImgName + intState + ".src;");
}



/**
 *  Function:		openWindow / popupWindow
 *
 *  Version History:	?
 *
 *  Description:	Use this function to open/popup a new window to display a link in.
 *
 *  Special Notes:	none
 *
 *  Syntax:		openWindow(url, w, h)
 *
 *  Parameters:		url: (input) target link
 *			w: (input) width of the popup window in pixels
 *			h: (input) height of the popup window in pixels
 *
 *  Returns:		void
 */

  function openWindow(url, w, h) {
    var options  = "width=" + w + ",height=" + h + ",";
        options += "resizable=yes,scrollbars=yes,status=yes,";
        options += "menubar=yes,toolbar=yes,location=yes,directories=no";
    var newWin = window.open(url, 'newWin', options);
    newWin.focus();
  }

  function popupWindow(url, w, h) {
    openWindow(url, w, h);
  }

  function videoWindow(url, w, h) {
    var options  = "width=" + w + ",height=" + h + ",";
        options += "resizable=no,scrollbars=no,status=no,";
        options += "menubar=no,toolbar=no,location=no,directories=no";
    var videoWin = window.open(url, 'videoWin', options);
    videoWin.focus();
  }



/**
 *  Function:		createHelpWindow
 *
 *  Version History:	?
 *
 *  Description:	Use this function to open/popup a new window to display a link in.
 *
 *  Special Notes:	none
 *
 *  Syntax:		openWindow(url, w, h)
 *
 *  Parameters:		newURL: (input) target link -- defaults to "/sc2000/blank.htm"
 *			newWin: (input) name of the window -- defaults to "EUIWin"
 *
 *  Returns:		false
 */

function createHelpWindow(newURL, newWin) {

  var maxWidth, winWidth;
  var maxHeight, winHeight;
  var positionString = "";

  screenWidth = 200;
  screenHeight = 300;

  winArgs = "width=520,height=460,toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars=1,copyhistory=0";

  if (newURL == "") {
    newURL = "/sc2000/blank.htm";
  }

  if (newWin == "") {
    newWin = "EUIWin";
  }

  var catalogWin = window.open(newURL, newWin, winArgs);

  if (catalogWin != null) {
    catalogWin.focus();
  }

  return false;
}



/**
 *  Function:		createCatalogWindow
 *
 *  Version History:	?
 *
 *  Description:	Use this function to open/popup a new window, with
 *                      calcualted size and positioning, to display a link in.
 *
 *  Special Notes:	none
 *
 *  Syntax:		openWindow(url, w, h)
 *
 *  Parameters:		newURL: (input) target link
 *			newWin: (input) width of the popup window in pixels
 *
 *  Returns:		false
 */

function createCatalogWindow(newURL, newWin) {

  var maxWidth, winWidth;
  var maxHeight, winHeight;
  var positionString = "";

  screenWidth = screen.width - 15;
  screenHeight = screen.height - 25;

  if(navigator.userAgent.indexOf("MSIE") < 0) {
    newX = window.screenX + 45;
    newY = window.screenY + 45;
    positionString = ",SCREENX=" + newX;
    positionString += ",SCREENY=" + newY;
  }
  
  if(screenWidth > 700) {
    screenWidth = 700;
  }
  if(screenHeight > 520) {
    screenHeight = 520;
  }

  if (newURL == ""){
    newURL = "/sc2000/blank.htm";
  }

  if (newWin == "") {
    newWin = "EUIWin";
  }

  var catalogWin = window.open(newURL, newWin, "HEIGHT=" + screenHeight +",WIDTH=" + screenWidth + ",SCROLLBARS,TOOLBAR,STATUS" + positionString);
  if (catalogWin != null) {
    catalogWin.focus();
  }

  return false;
}


/**
 *  Function:		loadFormDataFromCookie
 *
 *  Version History:	2007-03-16, Richard Coles, Initial Version
 *
 *  Description:	Loads form data from a cookie value that is the same as the form's name.
 *
 *  Syntax:		loadFormDataFromCookie(formName)
 *
 *  Parameters:		formName -- the name of the form (e.g. <form name="formName">).
 *
 *  Return:		boolean true
 */

 function loadFormDataFromCookie(formName) {

    var boolDebug = false;

    form = document.forms[formName];
    if (typeof form  == "undefined") {
	return(true);
    }

    var cookie = document.cookie;
    if (boolDebug) {
	alert("Cookie = " + document.cookie);
    }
    var cookieStart = cookie.indexOf(formName);
    if (cookieStart < 0) {
	return(true);
    }
    cookieStart += formName.length + 1;
    var cookieEnd = cookie.indexOf(";", cookieStart);
    if (cookieEnd < cookieStart) {
	cookieEnd = cookie.length;
    }
    var cookieValue = unescape(cookie.substring(cookieStart, cookieEnd));
    var semiColonLoc = -1;
    var nextSemiColonLoc = cookieValue.indexOf(";");
    var cookieFields = "";
    while (nextSemiColonLoc > 0) {
	var equalsLoc = cookieValue.indexOf("=", semiColonLoc);
	var name = cookieValue.substring(semiColonLoc + 1, equalsLoc);
	var value = unescape(cookieValue.substring(equalsLoc + 1, nextSemiColonLoc));
	if (typeof form[name] != "undefined") {		
	    //  Text
	    if (form[name].type == "text") {
	        form[name].value = value;
	    }
	    //  Select-One
	    else if (form[name].type == "select-one") {
	        var singleSelect = form[name];
		for (k = 0; (k < singleSelect.length); k++) {
		    if (singleSelect.options[k].value == value) {
			singleSelect.options[k].selected = true;
		    }
		}
	    }
	    //  Select-Multiple
	    else if (form[name].type == "select-multiple") {
		var multiSelect = form[name];
		for (k = 0; (k < multiSelect.length); k++) {
		    if (multiSelect.options[k].value == value) {
			multiSelect.options[k].selected = true;
		    }
		}	            
	    }
	    //  Checkbox or Radio
	    else if (typeof form[name].length != "undefined") {
		var nodeList = form[name];
		for (k = 0; (k < nodeList.length); k++) {
		    if (nodeList[k].value == value) {
		   	nodeList[k].checked = true;
		    }
		}
	    }
	    //  Unhandled type -- ignore hidden input types
	    else if (form[name].type != "hidden") {
	//	alert ("Name = " + name + "   Type = " + form[name].type + "   typeof = " + typeof form[name] + "   Value = " + form[name]);
	    }
	}
	cookieFields += "\n" + name + " : " + value;
	semiColonLoc = nextSemiColonLoc;
	nextSemiColonLoc = cookieValue.indexOf(";", semiColonLoc + 1);
    }
    if (boolDebug) {
	alert("cookieFields = " + cookieFields);
    }
	
    return(true);
}


/**
 *  Function:		persistFormDataInCookie
 *
 *  Version History:	2007-03-16, Richard Coles, Initial Version
 *
 *  Description:	Writes form data to a session cookie.
 *
 *  Syntax:		persistFormDataInCookie(formObject [,include:field1,field2,... | exclude:field1,field2,...])
 *
 *  Parameters:		formObject -- e.g.: <form onSubmit="return(persistFormDataInCookie(this))">
 *  Optional Params:    String, comma seperated field names (no spaces) prefixed with 
 *  			"include:" or "exclude:".
 *  			When "include:" is the prefix, only the listed named field's values will be stored.
 *  			When "exclude:" is the prefix, the listed named field's values will not be stored.
 *  			When the optional parameter is omitted, all named field values are stored. 
 *
 *  Return:		boolean true
 */

function persistFormDataInCookie(formObj) {

    var boolDebug = false;
    var cookieValue = "";
    var includeFields;
    var excludeFields;
    if (arguments.length > 1) {
	var colonLoc = arguments[1].indexOf(":");
	if (colonLoc > 0) {
	    var includeOrExclude = arguments[1].substring(0,colonLoc);
	    var namedFields = arguments[1].substring(colonLoc + 1);
	    if (includeOrExclude.toLowerCase() == "include") {
		includeFields = namedFields.split(",")
	    }
	    else if (includeOrExclude.toLowerCase() == "exclude") {
		excludeFields = namedFields.split(",")
	    }
	}
    }
    if (boolDebug) {
	alert ("Exclude Fields = " + excludeFields + "   Include Fields = " + includeFields);
    }
    for (j = 0; (j < formObj.elements['length']); j++) {
	var fieldName = "";
	var fieldValue = ""; 
	if ((formObj.elements[j].type != "submit") && 
	    (formObj.elements[j].type != "reset")) {
	    //  Text
	    if (formObj.elements[j].type == "text") {
		cookieValue += addToCookieValue(formObj.elements[j].name, fieldValue = formObj.elements[j].value, includeFields, excludeFields);
	    }
	    // Select-One
	    else if (formObj.elements[j].type == "select-one") {
		var singleSelect = formObj.elements[j];
		for (k = 0; (k < singleSelect.length); k++) {
		    if (singleSelect.options[k].selected == true) {
			cookieValue += addToCookieValue(singleSelect.name, singleSelect.options[k].value, includeFields, excludeFields);
			break;
		    }
		}
	    }
	    //  Select-Multiple
	    else if (formObj.elements[j].type == "select-multiple") {
		var multiSelect = formObj.elements[j];
		for (k = 0; (k < multiSelect.length); k++) {
		    if (multiSelect.options[k].selected == true) {
			cookieValue += addToCookieValue(multiSelect.name, multiSelect.options[k].value, includeFields, excludeFields);
		    }
		}	            
	    }
	    //  Checkbox or Radio
	    else if ((formObj.elements[j].type == "checkbox") || (formObj.elements[j].type == "radio")) {
		if (formObj.elements[j].checked == true) {
		    cookieValue += addToCookieValue(formObj.elements[j].name, fieldValue = formObj.elements[j].value, includeFields, excludeFields);
		}
	    }
	    //  Unhandled type -- ignore hidden input types
	    else {
		if (formObj.elements[j].type != "hidden") {
		//    alert("Name = " + formObj.elements[j].name + "   Type = " + formObj.elements[j].type + "   typeof = " + typeof formObj.elements[j]);
		}
	    }
	}
    }	
    if (boolDebug) {
	alert("cookieName = " + formObj.name + "\ncookieValue = " + cookieValue);
    }
    document.cookie = formObj.name + "=" + escape(cookieValue) + ";path=/";
        
    return(true);
}


/**
 *  Function:		addToCookieValue
 *
 *  Version History:	2007-03-16, Richard Coles, Initial Version
 *
 *  Description:	Returns a string in the format "fieldName=escaped(fieldValue)" 
 * 			or "" if the include/exclude rules require that that field be excluded.
 *
 *  Syntax:		addToCookieValue(fieldName, fieldVaue, includeFields, excludeFields)
 *
 *  Parameters:		fieldName     -- name of field
 *  			fieldValue    -- value of the named field
 *  			includeFields -- array of field names which should be included, or undefined var
 *  			excludeFields -- array of field names which should be excluded, or undefined var 
 *
 *  Return:		boolean true
 */

function addToCookieValue(fieldName, fieldValue, includeFields, excludeFields) {
    var cookieValueAddOn = "";
    if (fieldName != "") {
	if ((typeof includeFields != "undefined") && (includeFields.length > 0)) {
	    var ki = 0;
	    for (; (ki < includeFields.length); ki++) {
		if (includeFields[ki] == fieldName) {
		    cookieValueAddOn = fieldName + "=" + escape(fieldValue) + ";";
		    break;
		}
	    } 
	}
	else if ((typeof excludeFields != "undefined") && (excludeFields.length > 0)) {
	    var ke = 0;
	    for (; (ke < excludeFields.length); ke++) {
		if (excludeFields[ke] == fieldName) {
		    break;
		}
	    } 
	    if (ke >= excludeFields.length) {
		cookieValueAddOn = fieldName + "=" + escape(fieldValue) + ";";
	    }
	}
	else {
	    cookieValueAddOn = fieldName + "=" + escape(fieldValue) + ";";
	}
    }
    return(cookieValueAddOn);
}
