/*
Copyright 2002 Critical Path, Inc. All Rights Reserved.
Build : $Name:  $
*/

// Returns true if the object is an array, false otherwise.
function isArray(obj) 
{
  if (obj.constructor.toString().indexOf("Array") == -1)
  {
    return false;
  }
  else
  {
    return true;
  }
}

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 code = "";
	code += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">";
	code += "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n";
	code += "<head><title>post</title></head>\n";
	code += "<body>\n";
	code += "<form action=\""+formAction+"\" target=\""+target+"\" method=\"POST\">\n";
	for(var i=0; i<paramNames.length; i++){
		if(navigator.userAgent.toLowerCase().indexOf('macintosh') != -1 ) {										
			if (typeof params[i] == 'object' && params[i].constructor == Array)	{				
				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";
			}
		}else {
	  	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";

	postWindow.document.open();			
	postWindow.document.write(code);		
	
	postWindow.document.close();
	postWindow.document.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(navigator.userAgent.toLowerCase().indexOf('macintosh') != -1 ) {		
		if (typeof names == 'object' && names.constructor == Array)	{
			namesArray = names;
		}	else {
			namesArray = [names];
		}	
		if(typeof values == 'object' && values.constructor == Array) {
			valuesArray = values;	
		} else {
			valuesArray = [values];	
		}
	} else {	
		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)
{
	if (isUndefined(param))
		return param;

	var s = '';

	for (i=0; i<param.length; i++)
	{
		unicodeEncoding = param.charCodeAt(i);

		if ( unicodeEncoding < 0x80 )
		{
			byte1 = unicodeEncoding; byte2 = 0; byte3 = 0;
		}
		else if (unicodeEncoding < 0x0800)
		{
			byte1 = 192 + ( ( unicodeEncoding >> 6 ) & 31 ) ;
			byte2 = 128 + ( unicodeEncoding & 63 );
			byte3 = 0;
		}
		else if (unicodeEncoding <= 0xffff)
		{
			byte1 = 224 + ( ( unicodeEncoding >> 12 ) & 15 );
			byte2 = 128 + ( ( unicodeEncoding >> 6 ) & 63 );
			byte3 = 128 + ( unicodeEncoding & 63 );
		}

		if ( byte1 != 0 )
		{
			if (isEncodingNeeded(byte1))
			{
				byte1 = tohex(byte1);
				s = s + "%" + ((byte1 < 0x10) ? "0": "") + byte1;
			}
			else if (byte1 == 32) // a space
			{
				s = s + '+';
			}
			else
			{
				s = s + String.fromCharCode(byte1);
			}
		}
		if ( byte2 != 0 )
		{
			byte2 = tohex(byte2);
			s = s + "%" + ((byte2 < 0x10) ? "0": "") + byte2;
		}
		if ( byte3 != 0 )
		{
			byte3 = tohex(byte3);
			s = s + "%" + ((byte3 < 0x10) ? "0": "") + byte3;
		}
	}

	return s;
}

function isEncodingNeeded(B)
{
	if (!(B >= 97 && B <= 122) && // a-z
		!(B >= 65 && B <= 90) &&  // A-Z
		!(B >= 48 && B <= 57) &&  // 0-9
		!(B == 46) && !(B == 45) &&
		!(B == 95) && !(B == 42) && !(B == 32))
	{
		return true;
	}
	return false;
}

function tohex(i)
{
	a2 = '';
	ihex = hexQuot(i);
	idiff = eval(i + '-(' + ihex + '*16)');
	a2 = itohex(idiff) + a2;
	while( ihex >= 16)
	{
		itmp = hexQuot(ihex);
		idiff = eval(ihex + '-(' + itmp + '*16)');
		a2 = itohex(idiff) + a2;
		ihex = itmp;
	}
	a1 = itohex(ihex);
	return a1 + a2;
}

function hexQuot(i)
{
	return Math.floor(eval(i +'/16'));
}

function itohex(i) 
{
  if( i == 0) {
	 aa = '0' }
  else { if( i== 1) {
			aa = '1'}
	 else {if( i== 2) {
			  aa = '2'}
		else {if( i == 3) {
				 aa = '3' }
		   else {if( i== 4) {
					aa = '4'}
			  else {if( i == 5) {
					   aa = '5' }
				 else {if( i== 6) {
						  aa = '6'}
					else {if( i == 7) {
							 aa = '7' }
					   else {if( i== 8) {
								aa = '8'}
						  else {if( i == 9) {
									aa = '9'}
							 else {if( i==10) {
									  aa = 'A'}
								else {if( i==11) {
										 aa = 'B'}
								   else {if( i==12) {
											aa = 'C'}
									  else {if( i==13) {
											   aa = 'D'}
										 else {if( i==14) {
												  aa = 'E'}
											else {if( i==15) {
													 aa = 'F'}

											}
										 }
									  }
								   }
								}
							 }
						  }
					   }
					}
				 }
			  }
		   }
		}
	 }
  }
  return aa
}

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 getEmailSelectVector(form)
{
	var selectList = new Array(); 
	var j=0;
	for (var i = 0; i < form.length; i++)
	{
		if (form.elements[i].checked && form.elements[i].name != "selectAll")
		{
			selectList[j] = form.elements[i].value;
			j++;
		}
	}
	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)
{
	// make sure we have something to work with
	if (win == null) win = window;

		if (!isUndefined(win.psHomeFrame))
		{
				return win;
		}

	//check if top and no opener exists
	if (win == win.top && isUndefined(win.opener))
	{
		return win;
	}

	// check if opener exists and is from same host
	var opener = win.opener;
	if (!isUndefined(win.opener)
		/*&& compareHosts(win,opener)*/)
	{
		return getHomeFrame(opener);
	}

	// top not same host so check parent
	var winParent = win.parent;
	if (!isUndefined(winParent)
		/*&& compareHosts(win,winParent)*/)
	{
		return getHomeFrame(winParent);
	}

	// not top frame but must be home
	return win;
}

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 popWin(url,name,width,height,menubar)
{
	var etc;
	if (menubar == null) menubar="no";

	if (width != null && height != null)
	{
		etc = "menubar="+menubar+",scrollbars=0,resizable=0,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 ---

JSContext.getInstance = function()
{
	var container = JSContext.getContainer();
	if (typeof container.jsContext == "undefined" || container.jsContext == null)
	{
	//alert("creating new context");
	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;
}

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

// Return a boolean value telling whether
// the first argument is an Array object.
function isArray() {

if (typeof arguments[0] == 'object') {
  var criterion =
	arguments[0].constructor.toString().match(/array/i);
   return (criterion != null);
  }
return false;
} 

// Return a boolean value telling whether
// the first argument is a string.
function isString() {

if (typeof arguments[0] == 'string') return true;
if (typeof arguments[0] == 'object') {
  var criterion =
	arguments[0].constructor.toString().match(/string/i);
   return (criterion != null);
  }
return false;
}

// Return a boolean value telling whether
// the first argument is an integer.
function isInteger()
{
	var invalidCharactersRegExp = /[^\d-]/;
	return !(invalidCharactersRegExp.test(arguments[0]));	
}

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, " ");
}

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 setCheckboxListSel(form,inputName,value)
{
	for(var i =0; i < form.elements.length; i++)
	{
		var element = form.elements[i];
		if (element.name == inputName)
		{
			element.checked = value;
			selecta(element);
		}
	}
}