//--------------------------------------------------------
// JavaScript "retain_tray.js"
// Created by: Doug Woolley
// Date Created: 12/20/2009
// Purpose: This program includes functions to retain the tray value in a cookie and to recall
//          that value when needed.
//*************  The following functions were created by Doug Woolley - 12/19/2009  ********************

function ReadCookie(CookieName) {
	var CookieString = document.cookie;
	var CookieSet = CookieString.split (';');
	var SetSize = CookieSet.length;
	var CookiePieces;
	var ReturnValue = "";
	var x = 0;
	for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) {
		CookiePieces = CookieSet[x].split ('=');
		if (CookiePieces[0].substring(0,1) == ' ') {
			CookiePieces[0] = CookiePieces[0].substring(1, CookiePieces[0].length);
		}
		if (CookiePieces[0] == CookieName) {
			ReturnValue = CookiePieces[1];
		}
	}
	retval = ReturnValue;
	return ReturnValue;
}

function getTray() {
	//Read Tray in Cookies; if Tray not defined, return a single space, which selects first option [0]
	var Tray = ReadCookie('Tray');
	if (Tray == null || Tray.length == 0 || Tray == 'undefined') {
		Tray = ' ';
	}
	return Tray;
}

function assignElementToValue(elemName, value) {
	//Note: elemName must be valid on the form otherwise a JavaScript error occurs here
	//If the elemName is a "select" name with an option list, then
	//	After assigning value to the selectName, if the value is really among the list of options then
	//		the selectName will retain that value;
	//	otherwise
	//		the value will equal some null value indicating it is nnot on the list.
	//	Thereforme, compare the two values and if it is different, assign the first option.
	
	var elem = document.forms[0].elements[elemName];
	elem.value = value;
	
	if (elem.type == "select-one" || elem.type == "select-multiple") {
		if (elem.value != value || elem.value == "") {
			elem.options[0].selected = true;
		}
	}
}

//Write specific tray selected
function WriteCookie(cookieName, cookieValue) {
	document.cookie = cookieName + "=" + cookieValue + "; path=/; expires=Fri, 31-Dec-2011 23:12:40 GMT "

	//Store cookieValue in hidden form field of tray to be passed to child window frame
	document.forms[0].tray.value = cookieValue;  //Use alternative method in case Cookies do not get written
			
	//Test if cookie was stored properly, or if Security setting is too high
	var storedCookie = ReadCookie(cookieName);
	if (storedCookie != cookieValue) {
		//alert('A cookie value for the ' + cookieName + ' could not be written to the computer ' +
		//	    'due to the extra high level of your security setting. To reduce it to Medium High, ' +
		//	    'go to Tools - Internet Options - Privacy - Settings');
		return false;
	}
}
