/**********************************************************************************
*
* $Header: /cvs/chef/src/js/headscripts.js,v 1.16 2004/02/06 03:14:48 zqian Exp $
*
***********************************************************************************
*
* Copyright © 2002, 2003, 2004 The Regents of the University of Michigan
*
* This work, including software, documents, or other related items (the "Software"), is being
* provided by the copyright holder(s) subject to the terms of the UM-CHEF Software License. By
* obtaining, using and/or copying this Software, you agree that you have read, understand, and
* will comply with the following terms and conditions of the UM-CHEF Software License:
*
* Permission to use, copy, modify, and distribute this Software and its documentation, with or
* without modification, for any purpose and without fee or royalty is hereby granted, provided
* that you include the following on ALL copies of the Software or portions thereof, including
* modifications or derivatives, that you make:
*
*    - The full text of the UM-CHEF Software License in a location viewable to users of the
*      redistributed or derivative work.
*
* 	 - Any pre-existing intellectual property disclaimers, notices, or terms and conditions. If
*      none exist, a short notice similar to the following should be used within the body of
*      any redistributed or derivative Software:
* 	   "Copyright © 2002, 2003, 2004 The Regents of the University of Michigan. All Rights Reserved."
*
*    - Notice of any changes or modifications to the UM-CHEF Software, including the date
*      the changes were made. Any modified software must be distributed in such as manner
*      as to avoid any confusion with the original UM-CHEF Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* The name and trademarks of copyright holder(s) and/or The University of Michigan may NOT
* be used in advertising or publicity pertaining to the Software without specific, written prior
* permission. Title to copyright in the Software and any associated documentation will at all
* times remain with the copyright holders.
*
* The export of software employing encryption technology may require a specific license from
* the United States Government. It is the responsibility of any person or organization
* contemplating export to obtain such a license before exporting this Software.
*
**********************************************************************************/

var courierRunning = false;

var focus_path;

function openWindow(url, title, options)
{
	var win = top.window.open(url, title, options);
	win.focus();
	return win;
}

function sitehelp(whereto) 
{
	umcthelp=window.open(whereto,'umcthelpWindow','toolbar=yes,scrollbars=yes,resizable=yes,menubar=no,status=yes,directories=no,location=no,width=600,height=400')
}

function hideElement(hideMe)
{
	if (hideMe != 'none')
	{
		var menuItem = document.getElementById(hideMe);
		if(menuItem != null)
			menuItem.style.display = 'none';
	}
	return true;
}

/* %%% currently disabled, used for paitence display -ggolden
function showSubmitMessage()
{
	var submitDiv = document.getElementById('SubmitMessage');
	var normalDiv = document.getElementById('chefPortletContainer')
	if (submitDiv != null) 
	{
		submitDiv.style.display = '';
		if (normalDiv != null)
			normalDiv.style.display = 'none';
	}
	return true;
}
*/

function URLencode(sStr) {
    return escape(sStr).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
 }

// escape also the plus sign!
function fullEscape(value)
{
	var once = escape(value);
	var twice = "";
	for (var i=0; i < once.length; i++)
	{
		if (once.charAt(i) == '+')
		{
			twice += "%2b";
		}
		else
		{
			twice += once.charAt(i);
		}
	}
	return twice;
}

function buildQueryString(theFormName)
{
	theForm = document.forms[theFormName];
	var qs = '';
	for (var e=0; e<theForm.elements.length; e++)
	{
		var element = theForm.elements[e];
		if ((element.name) && (element.type) && (element.value))
		{
			var checkRadio = ((element.type == "checkbox") || (element.type == "radio"));
			if ((element.name != '') && (element.name.indexOf("eventSubmit_") == -1))
			{
				if ( !checkRadio || element.checked)
				{
					// old
					//qs += '&' + fullEscape(element.name) + '=' + fullEscape(element.value);
					// now test
					qs += '&' + element.name + '=' + element.value;
					//alert(qs);
				}
			}
		}
	}
	return qs;
}

function updCourier()
{
	if ((!top.courierRunning) && (top.courier))
	{
		top.courierRunning = true;
		top.courier.location.replace(top.courier.location);
	}
}

function formSubmitOnEnter(field, event)
{
	var keycode;
	if (window.event)
	{
		keycode = window.event.keyCode;
	}
	else
	{
		keycode = event.which ? event.which : event.keyCode
	}

	if (keycode == 13)
	{
		field.form.submit();
		return false;
	}
	
	return true;
}

// click an element id = element
function clickOnEnter(event, element)
{
	var keycode;
	if (window.event)
	{
		keycode = window.event.keyCode;
	}
	else
	{
		keycode = event.which ? event.which : event.keyCode
	}

	if (keycode == 13)
	{
		if (document.getElementById(element) && document.getElementById(element).click)
		{
			document.getElementById(element).click();
		}
		return false;
	}
	
	return true;
}

function sso_check()
{
	// becom sso check
		
	url = top.location.href;
	if (url.indexOf("/firstlogin") != -1)
	{
		idx = url.indexOf("/portal");
		loginurl = url.substring(0, idx) + "/servlet/login";
		//alert("need reauth : " + loginurl);
		top.location.href = loginurl;
		return;
	}

	// end becom sso check
}

// if the containing frame is small, then offsetHeight is pretty good for all but ie/xp.
// ie/xp reports clientHeight == offsetHeight, but has a good scrollHeight
function setMainFrameHeight(id)
{

// run the script only if this window's name matches the id parameter
// this tells us that the iframe in parent by the name of 'id' is the one who spawned us
	if (typeof window.name != "undefined" && id != window.name) return;

	var obj = parent.document.getElementById(id);
	if (obj)
	{
// reset the scroll
		parent.window.scrollTo(0,0);

// to get a good reading from some browsers (ie?) set the height small
		obj.style.height="50px";

		var height = document.body.offsetHeight;
// here's the detection of ie/xp
		if ((height == document.body.clientHeight) && (document.body.scrollHeight))
		{
			height = document.body.scrollHeight;
		}
// here we fudge to get a little bigger
		height = height + 50;
// no need to be smaller than...
//		if (height < 200) height = 200;
		obj.style.height=height + "px";
	}
}

// find the first form's first text field and place the cursor there
function firstFocus()
{
	if (document.forms.length > 0)
	{
		var f = document.forms[0];
		for (var i=0; i<f.length; i++)
		{
			var e = f.elements[i];
			if ((e.name) && (e.type) && ((e.type=='text') || (e.type=='textarea')) && (!e.disabled))
			{
				e.focus();
				break;
			}
		}
	}
}

// set focus on a particular element in a page.
// the path to that element is given by a series of element id's in an array (param "elements")
// which should be the id's of zero or more frames followed by the id of the element.
function setFocus(elements)
{
	if(typeof elements == "undefined")
	{
		return;
	}

	var focal_point = document;
	for(var i = 0; i < elements.length; i++)
	{
		if(focal_point.getElementById(elements[i]))
		{
			focal_point = focal_point.getElementById(elements[i]);
		}
		
		if(focal_point.contentDocument)
		{
			focal_point = focal_point.contentDocument;
		}
		else if(focal_point.contentWindow)
		{
			focal_point = focal_point.contentWindow;
			if(focal_point.document)
			{
				focal_point = focal_point.document;
			}
		}
		else
		{
			break;
		}
	}
	
	if(focal_point && focal_point.focus)
	{
		focal_point.focus();
	}
}

/**********************************************************************************
*
* $Header: /cvs/chef/src/js/headscripts.js,v 1.16 2004/02/06 03:14:48 zqian Exp $
*
**********************************************************************************/
