function getCookieVal (offset) 
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name) 
{  
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
	  var j = i + alen;
	  if (document.cookie.substring(i, j) == arg)
	    return getCookieVal (j);
	  i = document.cookie.indexOf(" ", i) + 1;
	  if (i == 0) break; 
	}
	return "";
}

function setCookie (name,value,expires,path,domain,secure) 
{
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}


//	writeCookies - this function is used to save form fields in browser cookies
function writeCookies()
{
	var expdate = new Date ();
	expdate.setTime (expdate.getTime() + (365 * 24 * 60 * 60 * 1000)); // 365 days from now 
	with(document.theForm)
	{	
//
//If you are making a customized form, you can add or remove fields from this function as you require.
// Corresponding changes should be made to the getCookies function.
//
		//save contact info fields in cookies 
		//edit field values can saved by saving the value member in a cookie
		setCookie("Firstname",fnam.value,expdate);
		setCookie("Lastname",lnam.value,expdate);
		setCookie("Company",cmpy.value,expdate);
		setCookie("Email",emal.value,expdate);
		setCookie("Phone",phon.value,expdate);
		//setCookie("ComputerModel",modl.value,expdate);
		//setCookie("ComputerOS",otyp.value,expdate);
		
		//dropdown list selections can saved by saving the selectedIndex in a cookie
		setCookie("ProductIndex",prod.selectedIndex,expdate);
		//setCookie("SeverityIndex",sevr.selectedIndex,expdate);
	}	
}


//	getCookies - this function pre-sets form fields with values saved in browser cookies
function getCookies()
{
	with(document.theForm)
	{
		//restore contact info fields from stored cookies
		// (need to check if form value not yet set because in NS 4.7, in order to get the relational dropdown
		//   controls to repaint themselves properly, we sometimes force a repaint/reload with history.go(0),
		//   which would cause this func to be re-invoked, potentially overwriting user entered data)
//
//If you are making a customized form, you can add or remove fields from this function as you require
//
		//edit field values can restored by setting the value member
		if (fnam.value.length == 0)
			fnam.value = getCookie("Firstname");
		if (lnam.value.length == 0)
			lnam.value = getCookie("Lastname");
		if (cmpy.value.length == 0)
			cmpy.value = getCookie("Company");
		if (emal.value.length == 0)
			emal.value = getCookie("Email");
		if (phon.value.length == 0)
			phon.value = getCookie("Phone");
		//if (modl.value.length == 0)
		//	modl.value = getCookie("ComputerModel");
		//if (otyp.value.length == 0)
		//	otyp.value = getCookie("ComputerOS");
			
		//dropdown list selections can restored by setting the selectedIndex
		if (!prod.selectedIndex)
		{
			prod.selectedIndex = parseInt(getCookie("ProductIndex"),10);
			updateRelationalLists(prod); //lists that are configured to be parents of other lists need to call
										// this func to keep things in sync after restoring a saved value
		}
			 
		//if (!sevr.selectedIndex)
		//{
		//	sevr.selectedIndex = parseInt(getCookie("SeverityIndex"),10);
			//updateRelationalLists(prod); //lists that are configured to be parents of other lists need to call
										// this func to keep things in sync after restoring a saved value
		//}

	}		
}

function validateForm()
{

	if( emptyField( document.theForm.emal ) )
	{
		alert( "Please enter your email address." );
		document.theForm.emal.focus();
		return false;
	}

	if( emptyField( document.theForm.fnam ) )
	{
		alert( "Please enter your first name." );
		document.theForm.fnam.focus();
		return false;
	}

	if( emptyField( document.theForm.lnam ) )
	{
		alert( "Please enter your last name." );
		document.theForm.lnam.focus();
		return false;
	}

	if( emptyField( document.theForm.desc ) )
	{
		alert( "Please enter a short description." );
		document.theForm.desc.focus();
		return false;
	}
	writeCookies(); //save off some form values into cookies
	return true;
}

// Check to see if field is empty
function emptyField( textObj )
{
	if( textObj.value.length == 0 )
		return true;
	for( var i = 0; i < textObj.value.length; ++i )
	{
		var ch = textObj.value.charAt(i);
		if( ch != ' ' && ch != '\t' )
			return false;
	}
	
	return true;
}

function updateRelationalLists( listObj )
{
var relationOption = "0";
relationData = [{name:"prod",value:"<not set>",child:"comp",values:["<not set>",""]}
,{name:"prod",value:"Capital Projects",child:"comp",values:["<not set>","Database","Documentation","Other","Reports","Setup","User Interface"]}
,{name:"prod",value:"Event Manager",child:"comp",values:["<not set>","Database","Documentation","ESR","ESR Palm","ESR Web","Other","Palm Pilot","Print Server","Reports","Setup","User Interface"]}
,{name:"prod",value:"Kaiser MP2 Upload",child:"comp",values:["<not set>","Database","Documentation","Other","Setup","User Interface"]}
,{name:"prod",value:"Thyssen MME",child:"comp",values:["<not set>","Database","Documentation","Other","Palm Pilot","Reports","Scout","Setup","User Interface"]}
,{name:"prod",value:"Total Elevator",child:"comp",values:["<not set>","Database","Documentation","Other","Reports","Setup","User Interface"]}
,{name:"prod",value:"<not set>",child:"fndv",values:["","Other","MP 1.8","MP 1.7","MP 1.6"]}
,{name:"prod",value:"Capital Projects",child:"fndv",values:["","Other","CP 1.4","CP 1.3","CP 1.2","CP 1.1","MP 1.8"]}
,{name:"prod",value:"Event Manager",child:"fndv",values:["","Other","EM 3.3.3","EM 3.3.2","EM 3.3.1","EM 3.3.0","EM 3.2.6","EM 3.2.5","EM 3.2.4","EM 3.2.2"]}
,{name:"prod",value:"Kaiser MP2 Upload",child:"fndv",values:["","Other","MP 1.8","MP 1.7","MP 1.6"]}
,{name:"prod",value:"Thyssen MME",child:"fndv",values:["","Other","MME 3.0.2","MME 3.0.1","MME 2.0.1","MP 1.8"]}
,{name:"prod",value:"Total Elevator",child:"fndv",values:["","Other"]}]

	//we need to run thru our array of configuration data to...
	var iMadeChanges = 0;
	for( var i = 0; i < relationData.length ; ++i )
	{
		if ((relationData[i].name == listObj.name)
				&& (relationData[i].value == listObj.options[listObj.selectedIndex].text)
				&& (relationData[i].name != relationData[i].child))
				{
					for (var e = 0; e < document.theForm.elements.length; e++)
					{
						if (document.theForm.elements[e].name == relationData[i].child)
						{ //found the child listbox element... clear values and reload it.
							//need to check to make sure the element that we found is in fact a 'select control'
							// (ie. the special case of the version edit/dropdown combo thingy)
							if ( document.theForm.elements[e].type == "select-one")
							{
								//lets save off info about current selection
								var prevSelectedIndex = document.theForm.elements[e].options.selectedIndex;
								var prevSelectedValue = document.theForm.elements[e].options[prevSelectedIndex].value;
								var prevSelectedText = document.theForm.elements[e].options[prevSelectedIndex].text;
								var prevSelectedIsValid = 0;
						
								var numOptions = document.theForm.elements[e].options.length;
								//clear current selection info
								document.theForm.elements[e].options.selectedIndex = 0;

								if (numOptions <= 1 && relationData[i].values.length > 1) iMadeChanges = 1;	//remember if we made 1 element array bigger

								var textlookupIdx = 0; //get index needed to do text->valu lookup for custom dropdown fields 
								for (textlookupIdx = 0; textlookupIdx < relationData.length; textlookupIdx++)
								{
									if ((relationData[textlookupIdx].name == relationData[textlookupIdx].child) && (relationData[textlookupIdx].name == relationData[i].child))
									{ //this is row in relationData that has all possible values/texts for this dropdown... 
										break;
									}
								}

								//replace rows in select with new values from our data table
								document.theForm.elements[e].options.length = relationData[i].values.length;
								for (var j = 0; j < relationData[i].values.length; ++j)
								{
									document.theForm.elements[e].options[j].text = relationData[i].values[j];
									document.theForm.elements[e].options[j].selected = 0;
									if (document.theForm.elements[e].options[j].text == prevSelectedText)
									{
										document.theForm.elements[e].options[j].selected = 1;
										prevSelectedIsValid = 1;
									}
									//need to find internal value for this text in the row in the relationData that had parent=child=this (custom fields only)
									if (textlookupIdx < relationData.length) //was a text->valu lookup row found in the relational data?
									{
										for (var findTextIdx = 0; findTextIdx < relationData[textlookupIdx].values.length; findTextIdx++)
										{
											if (document.theForm.elements[e].options[j].text == relationData[textlookupIdx].texts[findTextIdx])
											{
												document.theForm.elements[e].options[j].value = relationData[textlookupIdx].values[findTextIdx];
												break;
											}
										}
									}
									
								}
								//if original selection is not in the new list...
								if (prevSelectedIsValid == 0)
								{	//need to check option to know if we are to select not set, or re-add original selected value
									if (relationOption == "1")
									{	//force to not set
										document.theForm.elements[e].options[0].selected = 1; //assume 1st row is notset/default valu
										//we have changed the selection of a child list... it might be a parent to someone else 
										// so... go recursive.
										updateRelationalLists( document.theForm.elements[e]);
									}
									else //keep original value
									{
										var lastEntry = document.theForm.elements[e].options.length;
										document.theForm.elements[e].options.length = (lastEntry + 1);
										document.theForm.elements[e].options[lastEntry].text = prevSelectedText;									
										document.theForm.elements[e].options[lastEntry].selected = 1;									
									}
							
								}
							}
							else //we found the control, but it was not a select.. check for special case of our found in version combo thingy
							{
								/*if (document.theForm.elements[e].name == "fndv")
								{	//need to update the 'vselect' select object instead.
									//we need not worry at all about old or current selection ('blank' should always be selected)
									// just update values and text in the control
									document.theForm.vselect.options.length = relationData[i].values.length;
									for (var j = 0; j < relationData[i].values.length; ++j)
									{
										document.theForm.vselect.options[j].text = relationData[i].values[j];
									}
								}*/
							}						
						}
					}
					
				}
	}

	if (iMadeChanges == 1 )
	{// in NS 4.7, the dropdown portion of the select is messed up if it originally had only 1 option, but now we added more
	 //  this history call seems to help... though it messes stuff up in IE,  linux, etc.
	 var bwr = navigator.appName;
	 var ver = parseInt(navigator.appVersion);
	 var plt = navigator.platform;
	 if (bwr == "Netscape" && ver == 4 && plt == "Win32")
			history.go(0);
	}
	return true;	
}
