/*
Copyright 2002 Critical Path, Inc. All Rights Reserved.
Build : $Name: PS-7_3_106 $
*/
function filterXML(xmlString)
{
	if (xmlString == null) return null;
	var result = xmlString;
	result = result.replace(/&/gi,"&amp;");
	result = result.replace(/'/gi,"&#39;");
	result = result.replace(/"/gi,"&quot;");
	result = result.replace(/>/gi,"&gt;");
	result = result.replace(/</gi,"&lt;");
	return result;
}

function doRemotePost(formAction, paramNames, params, postWindow, target)
{
	if(target == null)
		target = "_self";

	var doc = postWindow.document;

	if(doc.all)
	{
		var code = "";
		code += "<html>\n";
		code += "<head><title>post</title></head>\n";
		code += "<body>\n";
		code += "<form name=\"postForm\" action=\""+formAction+"\" target=\""+target+"\" method=\"POST\">\n";
		for(var i=0; i<paramNames.length; i++)
		{
		  if (isArray(params[i]))
		  {
			for (var j = 0; j < params[i].length; j ++)
			{
				code += "<input type=\"hidden\" name=\""+filterXML(paramNames[i])+"\" value=\""+filterXML(params[i][j])+"\">\n";
			  }
			}
			else
			{
			  code += "<input type=\"hidden\" name=\""+filterXML(paramNames[i])+"\" value=\""+filterXML(params[i])+"\">\n";
			}
		}
		code += "</form>\n";
		code += "</body>\n";
		code += "</html>\n";
	
		doc.open();
		doc.write(code);
	}
	else
	{
		var i = doc.body.childNodes.length;
		while(i--)
		{
			doc.body.removeChild(doc.body.childNodes[i]);
		}
		
		var formElmnt = doc.createElement("FORM");
		formElmnt.name = "postForm";
		formElmnt.action = formAction;
		formElmnt.target = target;
		formElmnt.method = "POST";
		
		doc.body.appendChild(formElmnt);
		
		for(var i=0; i<paramNames.length; i++)
		{
			if(isArray(params[i]))
			{
				for (var j = 0; j < params[i].length; j ++)
				{
					var inputElmnt = doc.createElement("INPUT");
					inputElmnt.type = "hidden";
					inputElmnt.name = filterXML(paramNames[i]);
					inputElmnt.value = filterXML(params[i][j]);
					formElmnt.appendChild(inputElmnt);
				}
			}
			else
			{
				var inputElmnt = doc.createElement("INPUT");
				inputElmnt.type = "hidden";
				inputElmnt.name = filterXML(paramNames[i]);
				inputElmnt.value = filterXML(params[i]);
				formElmnt.appendChild(inputElmnt);
			}
		}
	}

	doc.forms[0].submit();
}


// Unchecks the check boxes in form with name checkboxName.
function deselectedCheckboxList (form, checkboxName)
{
	var elements = form.elements;
	for (var x=0; x < elements.length; x++)
	{
	if (elements[x].name == checkboxName && elements[x].checked == true)
	{
		elements[x].checked = false;
	}
	}
}


function getSelectedNumberInCheckList(paramName,form)
{
	var num=0;
	var elements=form.elements;
	for (var x=0; x < elements.length; x++)
	{
	if (elements[x].name==paramName && elements[x].checked)
	{
		num++;
	}
	}
	return num;
}

function isUndefined(obj)
{
	return (obj == null || typeof obj == "undefined");
}

function loadForm(form)
{
	var context = JSContext.getInstance();
	if (context.get(form.name) == null)
	{
	return true;
	}
	var params = context.get(form.name);
	fillForm(form,params);
	return true;
}

function openHelp(helpURL,helpTarget,helpProperties)
{
	var win = window.open(helpURL,helpTarget,helpProperties);
	JSContext.getInstance().registerPopup(win);
	win.focus();
}

function unloadForm(formName)
{
	var context = JSContext.getInstance();
	context.remove(formName);
}

function storeForm(form)
{
	var context = JSContext.getInstance();
	var params = JSContext.getContainer().readForm(form,true);
	context.put(form.name,params);
	return true;
}

function appendParameter(url,names,values)
{
	var namesArray = null;
	var valuesArray = null;
	if (isArray(names))
	{
	namesArray = names;
	}
	else
	{
	namesArray = [names];
	}
	if (isArray(values))
	{
	valuesArray = values;
	}
	else
	{
	valuesArray = [values];
	}
	var result = url;
	var thelength = namesArray.length;
	for (var i = 0; i < thelength; i++)
	{
	var name = namesArray[i];
	var value = valuesArray[i];
	var questionIndex = result.indexOf('?');
	if (questionIndex < 0)
	{
		result+="?"+name+"="+encodeParameter(value);
	}
	else if (questionIndex == url.length-1)
	{
		result+=name+"="+encodeParameter(value);
	}
	else
	{
		result+="&"+name+"="+encodeParameter(value);
	}
	}
	return result;
}

function encodeParameter(param)
{
	return escape(param);
}

function getEmailSelectList(form)
{
	var selectList = "";
	for (var i = 0; i < form.length; i++)
	{
		if (form.elements[i].checked && form.elements[i].name != "selectAll")
		{
			if (selectList.length == 0)
			{
				selectList = form.elements[i].value+",";
			}
			else
			{
				selectList = selectList+form.elements[i].value+",";
			}
		}
	}

	return selectList;
}

function containsElement(input, strArray)
{
	for (var i = 1; i < strArray.length; i++)
	{
		if ((!isUndefined(strArray[i]))&&(strArray[i].indexOf(input) != -1))
			return i;
	}

	return -1;
}

function countOccurrence(input, str)
{
	count = 0;
	pos = str.indexOf(input);
	while ( pos != -1 ) 
	{
		count++;
		pos = str.indexOf(input,pos+1);
	}

	return count;
}

// assumption: only one email list check box is checked
function getUidValue(form)
{
	for (var i = 0; i < form.length; i++)
	{
		if (form.elements[i].checked)
		{
			value = form.elements[i].value;
			break;
		}
	}

	return value;
}

// assumption: only one email list check box is checked
function getFolderPathName(form)
{
	for (var i = 0; i < form.length; i++)
	{
		if (form.elements[i].checked)
		{
			name = form.elements[i].name;
			break;
		}
	}

	return name;
}

function addToSelect(input,value,text,blankValue)
{
	var options = input.options;
	for (var i = 0; i < options.length; i ++)
	{
	var option = options[i];
	if (option.value == value)
	{
		return;
	}
	else if (option.value == blankValue)
	{
		option.value = value;
		option.text = text;
		return;
	}
	}
	// create new option
	var option = new Option(text,value);
	options[options.length] = option;
}

function removeSelected(input,minSize,hasTitle,blankVal)
{
	var options = input.options;
	var startIndex = (hasTitle) ? 1 : 0;
	var originalLength = options.length
	for (var i = startIndex; i < options.length; i++)
	{
	var option = options[i];
	if (option.selected)
	{
		options[i] = null;
		i--;
	}
	}

	// ensure options is minSize
	var idealLength = Math.max(options.length,minSize);
	while(options.length < idealLength)
	{
	options[options.length] = new Option("",blankVal,false,false);
	}
	
}

function setHomeFrame(win)
{
		win.psHomeFrame=true;
}

function getHomeFrame(win)
{
	if(!win)
		win = self;
	
	while(win != null)
	{
		// required for Firefox (possibly all Mozilla) when home frame is not present
		if(win.closed)
			break;	
	
		if(win.psHomeFrame && (win == win.top || !win.parent.psHomeFrame))
			return win;
		
		if(win.opener)
			win = win.opener;
		else if(win != win.top)
			win = win.parent;
		else
			win = null;
	}
}

function getContentFrame(win)
{
	var homeFrame = getHomeFrame(win);
	if (homeFrame == null)
	{
	return null;
	}
	return homeFrame.frames[2];
}

function getErrorFrame(win)
{
	var homeFrame = getHomeFrame(win);
	var contentFrame = getContentFrame(win);
	if (win == homeFrame ||
	(win.parent == homeFrame && win != contentFrame))
	{
	if (win == homeFrame.frames[1]
		|| win == homeFrame.frames[0])
	{
		return homeFrame;
	}
	else
	{
		return homeFrame.frames[2];
	}
	}
	else
	{
	return contentFrame;
	}
}

function isTopLevel(win)
{
	return (win.parent == win);
}

function isHomeSubFrame(win,homeFrame)
{
	if (homeFrame == null) homeFrame = getHomeFrame();
	if (win == homeFrame) return false;
	if (win == win.top) return false;
	if (win.parent == homeFrame) return true;
	return isHomeSubFrame(win.parent,homeFrame);
}

function isPopup(win)
{
	var homeFrame = getHomeFrame(win);
	var theTop = (win.top)  ? win.top : win;
	if (!isHomeSubFrame(win,homeFrame) && theTop.opener && (isHomeSubFrame(theTop.opener,homeFrame) || isPopup(theTop.opener)))
	{
	return true;
	}
	else
	{
	return false;
	}
}

function closeAllPopups()
{
	var popups=JSContext.getInstance().getAllPopups();
	for (var x = 0; x<popups.length; x++)
	{
		if (!popups[x].closed)
		popups[x].close();
	}
}

function getPopups(win)
{
	if (!isPopup(win))
	{
	return new Array();
	}
	var popupArray = new Array();
	var homeFrame = getHomeFrame(win);
	var homeFound = false;
	var nextWin = win;
	var i = 0;
	while (!homeFound)
	{
	if (isHomeSubFrame(nextWin))
	{
		homeFound = true;
	}
	else
	{
		popupArray[i] = nextWin.top;
		nextWin = nextWin.top.opener;
		i++;
	}
	}
	return popupArray;
}

function compareHosts(frame1,frame2)
{
	if (isUndefined(frame1.location)
	|| isUndefined(frame1.location.host)
	|| typeof frame1.location.host != "string")
	{
	return false;
	}
	if (isUndefined(frame2.location)
	|| isUndefined(frame2.location.host)
	|| typeof frame2.location.host != "string")
	{
	return false;
	}
	if (frame1.location.host != frame2.location.host)
	{
	return false;
	}
	return true;
}

function closeWin(){
	self.close();
}

function closeWindow(winName)
{
    var winChild = JSContext.getInstance().getFrame(winName);
    if (winChild)
    {
        if (!winChild.closed)
        {
            winChild.close();
            return true;
        }
    }
    return false;
}

function popWin(url,name,width,height,menubar)
{	
	var etc;
	if (menubar == null) menubar="no";

	if (width != null && height != null)
	{
		etc = "menubar="+menubar+",scrollbars=yes,resizable=yes,width="+width+",height="+height;
	}
	var newWin = window.open(url,name,etc);
	newWin.focus();
	JSContext.getInstance().registerPopup(newWin);
	return newWin;
}

function setAllCheckboxes(form,inputName,checked)
{
	for (var i = 0; i < form.elements.length;i++)
	{
	var input = form.elements[i];
	if (input.name==inputName)
	{
		input.checked=checked;
	}
	}
}

function checkboxClicked(form,inputName,selectAll, checked)
{
	if (checked)
	{
		var allSelected = true;
		for (var i = 0; i < form.elements.length;i++)
		{
			var input = form.elements[i];
			if ((input.name==inputName) && (input.checked == false))
			{
				allSelected = false;
				break;
			}
		}	
		if (allSelected)
		{
			selectAll.checked = true;
		}
	}
	else
	{
		selectAll.checked = false;
	}
}

/**
 * Assuming for now that submit to page goes to first form in opener window.
 */
function transferToOpener(prepareParamsFunc)
{
	var hash = readForm(window.document.forms[0]);
	if (prepareParamsFunc != null)
	{
	prepareParamsFunc(hash);
	}
	fillForm(window.opener.document.forms[0],hash);
	window.close();
	return false;
}

function loadPage(url,frameName)
{
	if (frame == null)
	{
	window.location=url;
	}
	else
	{
	var frame = findFrame(frameName);
	if (frame != null) frame.location=url;
	}
}

function setCheckboxList(form,inputName,value)
{
	for(var i =0; i < form.elements.length; i++)
	{
	var element = form.elements[i];
	if (element.name == inputName)
	{
		element.checked = value;
	}
	}
}


function readInput(form,inputName)
{
	return readForm(form).get(inputName);
}

function readForm(form, recordAllCheckBoxStates)
{
var inputNum  = form.elements.length;
var parameters = new Hashtable();
for (var i = 0; i < inputNum; i++)
{
var element = form.elements[i];
readFormElement(element,parameters,recordAllCheckBoxStates);
}
return parameters;
}

var INPUT_NOT_SELECTED="INPUT_NOT_SELECTED";

function readFormElement(element,parameters,recordAllCheckBoxStates)
{
	var type = element.type;
	var name = element.name;
	if (type == null || type == "undefined")
	{
	alert("readFormElement() : Input type not supported ("+type+")");
	return;
	}
	var value = null;
	if (type == "select-one" || type == "select-multiple")
	{
		var options = element.options;
	for (var i = 0; i < options.length; i++)
	{
		var option = options[i];
		if (option.selected)
		{
		value = (value == null) ? option.value : value + "," + option.value;
		}
	}
	}
	else if (type == "radio" || type == "checkbox")
	{
    	if (element.checked)
    	{
    		value = element.value;
    	}
    	else if (recordAllCheckBoxStates)
    	{
    	    value = INPUT_NOT_SELECTED;
    	}
    	
	}
	else
	{
		value = element.value;
	}
	if (value != null)
	{
	
	var prevValue = parameters.get(name);
	var newValue = (prevValue == null) ? value : prevValue + "," + value;
	parameters.put(name,newValue);
	}
	return;
}

  

function fillForm(form,parameters)
{
	var paramNames = parameters.keys();
	for (var i = 0; i < paramNames.length; i++)
	{
	var paramName = paramNames[i];
	var paramValue = parameters.get(paramName);
	fillFormValue(form,paramName,paramValue);
	}
}

function fillFormValue(form,paramName,paramValue)
{  
	var elements = form.elements;
	if (elements[paramName] == null)
	{
		return;
	}
	var tokenizer = new StringTokenizer(paramValue,",");
	var values = new Hashtable();
	do
	{
	var nextValue = tokenizer.nextToken();
	for (var i = 0; i < elements.length; i++)
	{
		var element = elements[i];
		if (element.name != paramName)
		{
		continue;
		}
		var type = element.type;
		if (type == null || type == "undefined")
		{
		alert("fillFormValue() : Input type not supported ("+type+")");
		}
		else if (type == "radio" || type == "checkbox")
		{
    		if (element.value == nextValue)
    		{
    			element.checked = true;
    		}
    		else if (values.get(element.value) == null)
    		{
    		    element.checked = false;
    		}
    		values.put(element.value," ");
		}
		else if (type == "select-one" || type == "select-multiple" )
		{
		var options = element.options;
		for (var j = 0; j < options.length; j++)
		{
			var option = options[j];
			if (option.value == nextValue)
			{
			option.selected = true;
			}
		}
		}
		else if (type != "button" && type != "submit")
		{
		element.value = paramValue;
		}
	}
	}
	while (tokenizer.hasMoreTokens());
}


// inone_obj.js
// Sept. 20, 2001
// Javascript Objects.

//-------------------------------------------------

/**
 * Key - Value pair
 */
function element(key,value)
{
	this.key=key;
	this.value=value;
}

//-------------------------------------------------
//--- Constructor ---
/**
 *  Simple hash table object. Key and values can be simple types or objects.
 */
function Hashtable()
{
	/**
	 * Array containing for key - value pairs
	 */
	this.array = new Array();

	/**
	 * Array containing keys
	 */
	this.keyArray = new Array();
}

//--- methods ---
/**
 * Put key value pair into hash table.  Equality operation used compare keys ( == ).
 * If key exists, old value will be overwritten.
 * @param key
 * @param value
 */
Hashtable.prototype.put = function(key, value)
{
	for (var i=0; i < this.keyArray.length; i++)
	{
	if (this.keyArray[i] == key)
	{
		this.array[i] = new element(key,value);
		this.keyArray[i] = key;
		return;
	}
	}
	this.array[this.array.length] = new element(key, value);
	this.keyArray[this.keyArray.length] = key;
	return;
}

/**
 */
Hashtable.prototype.remove = function(key)
{
	var newArray = new Array();
	for (var i = 0; i < this.array.length; i++)
	{
		var element = this.array[i];
		if (element.key != key)
		{
			newArray[newArray.length]=element;
		}
	} 
	this.array = newArray;
	var newKeyArray = new Array();
	for (var i = 0; i < this.keyArray.length; i++)
	{
		var element = this.keyArray[i];
		if (element != key)
		{
			newKeyArray[newKeyArray.length]=element;
		}
	} 
	this.keyArray = newKeyArray;
	return;
}

/**
 * Get value for specified key.
 * @param key
 * @return value corresponding to key.
 */
Hashtable.prototype.get = function(key)
{
	for (var i = 0; i < this.array.length; i++)
	{
		var element = this.array[i];
		if (element.key == key)
		{
			return element.value;
		}
	}
	return null;
}

/**
 * @return Array of key objects
 */
Hashtable.prototype.keys = function()
{
	return this.keyArray;
}

Hashtable.prototype.toString = function()
{
	var result = "[";
	for (var i=0; i < this.keyArray.length; i++)
	{
	result+="("+this.keyArray[i]+":"+this.get(this.keyArray[i])+")";
	}
	result+="]";
	return result;
}

// ------------------------------------------

function StringTokenizer(theString, token)
{
	this.theString = theString;
	this.token = token;
	this.currentIndex =0;
	
	this.nextToken = function()
	{
	var index = this.theString.indexOf(this.token,this.currentIndex);
	var next;
	if (index < 0)
	{
		next = this.theString.substring(this.currentIndex,this.theString.length);
		this.currentIndex = this.theString.length;
	}
	else
	{
		next = this.theString.substring(this.currentIndex,index)
		this.currentIndex = index+this.token.length;
	}
	return next;
	}

	this.hasMoreTokens = function()
	{
	return (this.currentIndex < this.theString.length);
	}
}

//-------------------------------------------------

function JSContext()
{
	this.hash = new Hashtable();
	this.frameStore = new Hashtable();
	this.formStore = new Hashtable();
	this.popupStore = new Hashtable();
}

JSContext.prototype.remove = function(key)
{
	return this.hash.remove(key);
}

JSContext.prototype.put = function(key,value)
{
	return this.hash.put(key,value);
}

JSContext.prototype.get = function(key)
{
	return this.hash.get(key);
}

JSContext.prototype.getFrame = function(frameName)
{
	return this.frameStore.get(frameName);
}

JSContext.prototype.getForm = function(formName)
{
	return this.formStore.get(formName);
}

JSContext.prototype.getAllPopups = function()
{
	var keys = this.popupStore.keys();
	var frames = new Array();
	if (keys && keys.length > 0)
	{
		var frameArray = new Array();
		for (var x = 0; x < keys.length;x++)
		{
			var frame = this.popupStore.get(keys[x]);
			frames[frames.length]=frame;
		}
	}
	return frames;
}

JSContext.prototype.registerPopup = function(frame,name)
{
	this.registerFrame(frame,name);
        var frameName = (name && name != null && name.length > 0) ? name :frame.name;
        var existingFrame = this.popupStore.get(frameName);
        if (existingFrame == null)
        {       
                this.popupStore.put(frameName,frame);
        }
        else if (frame != existingFrame)   
        {       
                this.popupStore.remove(frameName);
		this.registerPopup(existingFrame,new String((new Date()).getTime()));
		this.popupStore.put(frameName,frame);
        }
}

JSContext.prototype.registerFrame = function(frame,name)
{   
   
	var frameName = (name && name != null && name.length > 0) ? name :frame.name;
	var existingFrame = this.frameStore.get(frameName);
	if (existingFrame == null)
	{
		this.frameStore.put(frameName,frame);
	}
	else
	{
		this.frameStore.put(frameName,frame);
	}
}

JSContext.prototype.removeFrame = function(frameName)
{   

	var existingFrame = this.frameStore.remove(frameName);
	this.popupStore.remove(frameName);
}

JSContext.prototype.removeForm = function(formName)
{
	var existingForm = this.formStore.remove(formName);
}

JSContext.prototype.registerForm = function(form)
{
	var formName = form.name;
	var existingForm = this.formStore.get(formName);
	if (existingForm == null)
	{
	this.formStore.put(formName,form);
	}
	else
	{
	// need to override incase previous form is not unregistered;
	this.formStore.put(formName,form);
	}
}

//--- static methods ---

function getJSContextInstance()
{
	var container = JSContext.getContainer();
	if (!container || typeof container.jsContext == "undefined" || container.jsContext == null)
	{
		if (window == container)
		{
			window.jsContext = new JSContext();
		}
		else if (!container || container.closed || !container.JSContext)
		{
			container=window;
			window.jsContext = new JSContext();
		}
		else
		{
			return container.JSContext.getInstance();
		}
	}
	return container.jsContext;
}

function getJSContextInstanceSafari()
{
	var homeFrame = getHomeFrame(self);
	if(!homeFrame.theJSContext)
		homeFrame.theJSContext = new JSContext();
	
	return homeFrame.theJSContext;
}

if(navigator.userAgent.indexOf("Safari") == -1)
	JSContext.getInstance = getJSContextInstance;
else
	JSContext.getInstance = getJSContextInstanceSafari;


JSContext.getContainer = getHomeFrame;




//-------------------------------------------------
// Status Message object. 
// Special object representing the applications status message.
//-------------------------------------------------

function StatusMessage(locationFrame)
{
	this.locationFrame=locationFrame;
}

StatusMessage.registerLocation = function(locationFrame)
{
	JSContext.getInstance().put("StatusMessageObject",new StatusMessage(locationFrame));
}

StatusMessage.setStatusMessage = function (newMessage)
{
	var smObj=JSContext.getInstance().get("StatusMessageObject");
	if (isUndefined(smObj))
	{
	return;
	}
	var smf=smObj.locationFrame;
	if(isUndefined(smf))
	{
	// status message is not being shown anywhere.
	return;
	}
	if(isUndefined(smf.document.getElementById))
	{
	
	// not dom compliant browser
	// refresh frame
	var url=smf.document.location.href;
	if (url.indexOf("?") < 0)
	{
		smf.document.location=url+"?shm="+encodeParameter(newMessage);
	}
	else
	{
		smf.document.location=url+"&shm="+encodeParameter(newMessage);
	}
	}
	else
	{
	// dom compliant
	// set value directory
	var messageDiv = smf.document.getElementById("statusMessage");
	
	messageDiv.innerHTML=newMessage;
	}
}

//-------------------------------------------------

// determines what fields have not been filled out
function verifyForm(form)
{
	var emptyFields = new Array();
	var j =0; // counter for emptyFields array

	for (var i = 0; i < form.length; i++)
	{
		var element = form.elements[i];

		if ((element.type == "text") || (element.type == "textarea") || (element.type == "hidden"))
		{
			// check if the field is empty
			if ((element.value == null) || (element.value == "") || (isBlank(element.value)))
			{
				
				emptyFields[j] = element.name;
				j++;
				
			}
		}
	
	}

	return emptyFields;
}

//determines if string parameter contains only space, carage return or tab characters
function isBlank(stringParam)
{
	
	for (var i = 0; i < stringParam.length; i++)
	{
		var c = stringParam.charAt(i);

		if ((c !=' ') || (c !='\n') || (c !='\t')) 
		{
			return false;
		}
	}
	
	return true;
	
}

//---------Returns a select list as an comma delimited string.---------------------
function listToString(formName,listName,beginLocation) {
	 
	 var list = eval("document."+formName+"."+listName) ; //create the base string for the source list
	 var tempList = new Array();
	 var listLength = list.length - 1;
	 var j = beginLocation;

	 for(var i = 0; i < listLength; i++) {
	
	if (list.options[j].value != 0)
	{
			tempList[i] = list.options[j].text;		
	}
	
	j++;
	 }
	 return tempList.toString();
  }

// --------- Writes a hidden input field to a form---------------------
// form - the form the hidden input element is to be written to.
// name - the name of the hidden input element.
// value - the value of the hidden input element.

function writeHiddenElement(form, name, value) {
	  // Obtain a reference to the hidden form element with name "name".
	  var hidden = form.elements[name];
	  // Set the value of the hidden form element to "value".
	  hidden.value = value;
   }

// --------- Writes values to a list field ---------------------
// element - array of elements.

function writeToList(formName, fieldName, listName)
{
	var list = eval("document."+formName+"."+listName) ; //create the base string for the destination list
	var source = eval("document."+formName); //create the base string for the source field
	var listElements = new Array();

	listElements = parseInputString(source.elements[fieldName].value);

	for (var i = 0; i < listElements.length; i++)
	{
		//alert ("list.options[i+1]" + list.options[i+1]);
		//alert ("list.options[i+1].text" + list.options[i+1].text);
		//alert ("listElements [i]" + listElements [i]);

		var txt = listElements [i];
		var val = 1;

		// Using i+1 since 0 is the instructional text. 
		if ( list.options[i+1] == null )
		{
			var option = new Option(txt, val);
			list.options[i+1] = option; 
		}
		else
		{
			list.options[i+1].text = listElements [i];
			list.options[i+1].value = 1;
		}
	}
}

// --------- Writes specified values and text to a list field (general) ---------------------
// formName - the name of the form.
// fieldTextName - the name of the form field containing text parameter for the list.
// fieldValueName - the name of the form field containing value parameter for the list. 
// listName - the name of the form list that needs to be filled out.
// delimeter - string parameter in fieldTextName and fieldValueName parametes that separates 
//			 needed values 

function writeToListGeneral(formName, fieldValueName, fieldKeyName, listName,delimeter)
{
	var list = eval("document."+formName+"."+listName) ; //create the base string for the destination list
	var source = eval("document."+formName); //create the base string for the source field

	var tokenizer1 = new StringTokenizer(source.elements[fieldValueName].value,delimeter);
	var tokenizer2 = new StringTokenizer(source.elements[fieldKeyName].value,delimeter);
	var i = 1; // required to skip the first element in the list
	
	//reset list
	for (var j = 1; j < list.options.length; j++)
	{	  
		list.options[j].text = "";
		list.options[j].value = 0;	
	}
	
	if (source.elements[fieldValueName].value != 'null')
	{		
		// parse input string to retrieve values and store them in the list	 
		do
		{
			var nextValue1 = tokenizer1.nextToken();
			var nextValue2 = tokenizer2.nextToken();
			
			if (i > list.options.length - 1)
			{
				list.options[i] = new Option(nextValue1,nextValue2);
			}
			else
			{
				list.options[i].text = nextValue1;
				list.options[i].value = nextValue2;
			}
			i++;
		}
		while (tokenizer1.hasMoreTokens());
		
		
		// set possible remaining list fields 
		while (i < list.options.length - 1)
		{
			list.options[i].text = "";
			list.options[i].value = 0;
			i++;
		}
		
	}	 
}

// --------- Parses input string and stores elements in an array---------------------
// inputString - string to be parsed.

function parseInputString(inputString) {
	
	var DELIMETER = ",";
	var elements = new Array();
	var toParse = inputString;
	var begin = 0;
	var i = 0;

	// if input stirng is non empty parse it and store elements in an elements array
	if (inputString.length != 0)
	{
		while (toParse.indexOf(DELIMETER) != -1)
		{
			end = toParse.indexOf(DELIMETER);
			elements[i] = toParse.slice(begin,end);
			toParse = toParse.substr(toParse.indexOf(DELIMETER)+1);
			i++;
		}

		elements[i] = toParse;
		
	 
	}
	
	return elements;
   }

// --------- Submits a form---------------------
// form - form to be submited.


//submits the form
function submitForm(form,message,notesMaxLength,notesLengthExceededMessage)
{
		
	var emptyStatus = false; // boolean variable that determines if there are important fields that are missing 

	

	var emptyFields = new Array(); // array to store missing form fields
		var missingCount = 0;
		
		if (checkLengthExceeded(notesMaxLength, form.elements['notes'].value.length))
		{
			alert(notesLengthExceededMessage);
			return;
		}

	// check what fields were not filled out by the user
	emptyFields = verifyForm(form);
	

	
	if (emptyFields.length != 0)
	{
		for (var i = 0; i < emptyFields.length; i++)
		{
			
			if ((emptyFields[i] == "firstName") || (emptyFields[i] == "lastName") || (emptyFields[i] == "company") || (emptyFields[i] == "email1"))
			{
				missingCount++;
				//emptyStatus = true;
			}			
		}
		
				if (missingCount == 4)
		//if (emptyStatus == true)
		{
					alert(message);
					return;
			
						//var conf = confirm(message);
						//if (conf == true)
			//{
			//	//submit data
			//	form.submit();
			//}
						
		}
				else
				//else if (emptyStatus == false)
		{
					//submit form
					form.submit();
					return;
		}
	}
	else
	{
		//submit form
		form.submit();
				return;
	}

}

// --------- Displays an error message inside an alert box ---------------------
// message - string to be displayed.

function displayErrorMessage(message)
{
	var errorMessage = message;
	
	//if and error message exists display it in a popup alert box
	if (errorMessage != null)
	{
		alert(errorMessage);
	}
}

// ----------- checks the field for the predefined length limit and displays adn error message if needed----------------------
function checkLength(inputFieldLengthLimit, currentFieldLength,message)
{
	if(currentFieldLength > inputFieldLengthLimit)
	{
		alert(message);
	}
}
// ----------- checks the field for the predefined length limit returns a boolean value----------------------
function checkLengthExceeded(inputFieldLengthLimit, currentFieldLength)
{
	if(currentFieldLength > inputFieldLengthLimit)
	{
		return true;
	}
	else
	{
		return false;
	}
}

			
// ----------- checks if the supplied email address is valid ----------------------
function isAddressValid(address) {
    return /^([!#\$%&'\*\+\-\.\/0-9=\?A-Z\^\x5E_`a-z\{\|\}~]+)@(([0-9a-zA-Z]([0-9a-zA-Z-]*[0-9a-zA-Z])?)(\.[0-9a-zA-Z]([0-9a-zA-Z-]*[0-9a-zA-Z])?)+)$/.test(address);
	} // end isAddressValid

	function isSpecialChar(ch) {
		if (ch == ("!") || ch == ("@") || ch == ("#") || ch == ("$") || ch == ("%")
		|| ch == ("^") || ch == ("&") || ch == ("*") || ch == (",") || ch == (" ")
		|| ch == ("\"") || ch == ("+") || ch == ("/") || ch == (";") || ch == ("(")
		|| ch == (")") || ch == (":") || ch == ("=") || ch == ("?") || ch == (">")
		|| ch == ("<")) {
			return true;
		}
		return false;
	} // end isSpecialChar


function isArray(obj)
{
	return (typeof obj == "object") && (obj.constructor == Array);
}

function isString(obj)
{
	return (typeof obj == "string");
}

function isInteger(num)
{
	return !isNaN(num) && num == Math.round(num);
}

function removeElement(index,array) {
	for (;index<array.length;index++) {
		array[index] = array[index + 1];
	}
	array.length=array.length-1;
}

function goTo(dest){
	self.location = dest ;
}

function getCachableArray()
{
    return new Array();
}

function getCachableObject()
{
    return new Object();
}

var logOn = false;

function log(message)
{
	if (logOn)
	{
		var etc = "menubar=no,scrollbars=yes,resizable=yes,width=800,height=300";
		var logWin = window.open("","PSLogWindow",etc);
	
		var divEl = logWin.document.getElementById("log");
		
		if (!divEl || divEl == null)
		{
			function clearLog()
			{
				window.document.getElementById("log").innerHTML="";
			}
			logWin.document.open();
			logWin.document.writeln("<html><head><title>Presentation Server Javascript Log</title><script language='Javascript'>"+clearLog.toString()+"</script></head><body><form><font size='2'><div id='log'></div></font><input name='clear' type='button' value='Clear Messages' onclick='clearLog()'></form></body></html>");
			logWin.document.close();
			divEl = logWin.document.getElementById("log");
		}

		var theDate = new Date();
		divEl.innerHTML=divEl.innerHTML+"<br>"+theDate+" : "+message;
	}
}

function debugstuff()
{
        alert("util.js - debugstuff. Deprecated");
        /* <%-- 
        alert("JSJSContext.prototype.registerFrame");
        alert("frame is...");
        alert(frame);
        alert("name is...");
        alert(name);   
        --%>  */
        
      /*  <%-- 
        alert("JSJSContext.prototype.removeFrame");
        alert("frame is...");
        alert(frameName); 
       --%> */
}

function trim(value) 
{
    var temp = value;
    var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
    if (obj.test(temp)) 
    {
        temp = temp.replace(obj, '$2');
    }
    var obj = / +/g;
    temp = temp.replace(obj, " ");
    if (temp == " ") 
    { 
        temp = ""; 
    }
    return temp;
}

// Removes white space from a string.
function removeWhiteSpaceFromString(url) 
{
    var leadingWhitespace = /^\s*/g;
    var trailingWhitespace = /\s*$/g;
    var result = url.replace(leadingWhitespace, "");
    result = result.replace(trailingWhitespace, "");
    return result;
}

// Removes multi-whitespace chars to one whitespace char
function removeMultiWhiteSpaceFromString(ins)
{
    var patern = /\s{2,}/g;
    return ins.replace(patern, " ");
}



/*
 *	The following fixes the escape() and unescape() methods
 *	if they are broken.
 */
if(escape(String.fromCharCode(233)).toLowerCase() != "%c3%a9")
{
	escape = jspatch_urlEncode;
	unescape = jspatch_urlDecode;
}

var jspatch_hex = new Array
(
	"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
	"%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
	"%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
	"%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
	"%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
	"%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
	"%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
	"%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
	"%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
	"%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
	"%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
	"%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
	"%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
	"%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
	"%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
	"%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
	"%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
	"%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
	"%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
	"%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
	"%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
	"%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
	"%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
	"%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
	"%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
	"%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
	"%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
	"%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
	"%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
	"%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
	"%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
	"%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
 );		

function jspatch_urlEncode(s)
{
	if(!s)
		return s;

	var enc = "";
	var len = s.length;
	for (var i = 0; i < len; i++)
	{
		var ch = s.charCodeAt(i);
		if ("A".charCodeAt(0) <= ch && ch <= "Z".charCodeAt(0))
		{
			enc += String.fromCharCode(ch);
		}
		else if ("a".charCodeAt(0) <= ch && ch <= "z".charCodeAt(0))
		{
			enc += String.fromCharCode(ch);
		}
		else if ("0".charCodeAt(0) <= ch && ch <= "9".charCodeAt(0))
		{
			enc += String.fromCharCode(ch);
		}
		else if (ch == "-".charCodeAt(0) || ch == "_".charCodeAt(0) || ch == ".".charCodeAt(0) || ch == "!".charCodeAt(0) || ch == "~".charCodeAt(0) || ch == '*' || ch == "\'".charCodeAt(0) || ch == "(".charCodeAt(0) || ch == ")".charCodeAt(0))
		{
			enc += String.fromCharCode(ch);
		}
		else if (ch <= 0x007f)
		{
			enc += jspatch_hex[ch];
		}
		else if (ch <= 0x07FF)
		{
			enc += jspatch_hex[0xc0 | (ch >> 6)];
			enc += jspatch_hex[0x80 | (ch & 0x3F)];
		}
		else
		{
			enc += jspatch_hex[0xe0 | (ch >> 12)];
			enc += jspatch_hex[0x80 | ((ch >> 6) & 0x3F)];
			enc += jspatch_hex[0x80 | (ch & 0x3F)];
		}
	}
	return enc;
}

function jspatch_urlDecode(s)
{
	if(!s)
		return s;

	var sbuf = "";
	var l  = s.length;
	var ch = -1 ;
	var b, sumb = 0;
	for (var i = 0, more = -1 ; i < l ; i++)
	{
		ch = s.charAt(i);
		
		if(ch == '%')
		{
			var code;
			
			ch = s.charAt (++i) ;
			if(isNaN(ch))
			{
				code = 10 + ch.toLowerCase().charCodeAt(0) - "a".charCodeAt(0);
			}
			else
			{
				code = ch.charCodeAt(0) - "0".charCodeAt(0)
			}
			var hb = code & 0xF;
			
			ch = s.charAt (++i) ;
			if(isNaN(ch))
			{
				code = 10 + ch.toLowerCase().charCodeAt(0) - "a".charCodeAt(0);
			}
			else
			{
				code = ch.charCodeAt(0) - "0".charCodeAt(0)
			}
			var lb = code & 0xF;
			
			b = (hb << 4) | lb ;
		}
		else
		{	
			b = ch.charCodeAt(0) ;
		}
		
		/* Decode byte b as UTF-8, sumb collects incomplete chars */
		if ((b & 0xc0) == 0x80)	// 10xxxxxx (continuation byte)
		{
			sumb = (sumb << 6) | (b & 0x3f) ;	// Add 6 bits to sumb
			if (--more == 0)
				sbuf += String.fromCharCode(sumb) ; // Add char to sbuf
		}
		else if ((b & 0x80) == 0x00) // 0xxxxxxx (yields 7 bits)
		{
			sbuf += isNaN(b) ? b : String.fromCharCode(b);			// Store in sbuf
		}
		else if ((b & 0xe0) == 0xc0) // 110xxxxx (yields 5 bits)
		{
			sumb = b & 0x1f;
			more = 1;				// Expect 1 more byte
		}
		else if ((b & 0xf0) == 0xe0)	// 1110xxxx (yields 4 bits)
		{
			sumb = b & 0x0f;
			more = 2;				// Expect 2 more bytes
		}
		else if ((b & 0xf8) == 0xf0)	// 11110xxx (yields 3 bits)
		{
			sumb = b & 0x07;
			more = 3;				// Expect 3 more bytes
		}
		else if ((b & 0xfc) == 0xf8)	// 111110xx (yields 2 bits)
		{
			sumb = b & 0x03;
			more = 4;				// Expect 4 more bytes
		}
		else 	// 1111110x (yields 1 bit)
		{
			sumb = b & 0x01;
			more = 5;				// Expect 5 more bytes
		}
		/* We don't test if the UTF-8 encoding is well-formed */
	}
	return sbuf;
}
