// JavaScript Document

// --------------------------------------------------------------------------------
// cookies

	function setFormValues(form){
		updateForm(form, true)
		}

	function getFormValues(form){
		updateForm(form, false)
		}

	function flagFormElement(obj){
		var bgColor = obj.style.backgroundColor
		obj.style.backgroundColor = '#ff0000'
		alert('This form element does have an id!')
		obj.style.backgroundColor = bgColor
		}

	function updateForm(form, state, warn){


		// state==true : write form values
		// state==false : read form values

		formIdErrors = 0
		var elements = form.elements

		for(var i = 0; i< elements.length; i++){
			// variables
				var obj		= elements[i];
				//var cookieName	= form.name +'|'+ obj.id
				var cookieName	= obj.id;
				var cookieValue	= "";
				var cookieTime	= 24*7;

			// get values
				//alert([cookieName,readCookie(cookieName)])
				//alert([obj.id,obj.value])

				switch(obj.type){
					case 'checkbox':
					case 'radio':
						if(obj.id != ''){
							cookieValue = obj.checked ? true : ''
							state ? writeCookie(cookieName, cookieValue, cookieTime) : obj.checked = readCookie(cookieName)
							}
						else{
							if(warn)flagFormElement(obj)
							formIdErrors ++
							}
						break
					case 'select-one':
						if(obj.id != ''){
							cookieValue = obj.selectedIndex
							state ? writeCookie(cookieName, cookieValue, cookieTime) : obj.selectedIndex = parseInt(readCookie(cookieName))
							}
						else{
							if(warn)flagFormElement(obj)
							formIdErrors ++
							}
						break
					default:
					}
				}
			// warn
				if(formIdErrors>0)alert(formIdErrors +' form elements did not have id attributes. These elements will NOT be automatically updated when the user returns to the page.')
				// alert(document.cookie.split('; ').join('\n'))
		}


// --------------------------------------------------------------------------------
// get / set form values

	function getSelectedIndex(obj){
		alert(obj)
		return obj.selectedIndex
		}

	function setSelectedIndex(obj, index){
		obj.selectedIndex = parseInt(index)
		}

	function getCheckedState(obj){
		return obj.checked
		}

	function setCheckedState(obj,state){
		state = state == 'true' ? 'checked' : ''
		obj.checked = state
		}

// --------------------------------------------------------------------------------
// utilities

	function checkSelection(obj){
		var val = obj.options[obj.selectedIndex].value
		if(val == "-")obj.selectedIndex -= 1
		}



// --------------------------------------------------------------------------------
// cookies

	// Example:
	// alert( readCookie("myCookie") );
	function readCookie(name)
		{
		var cookieValue = "";
		var search = name + "=";
		if(document.cookie.length > 0){ 
			offset = document.cookie.indexOf(search);
			if (offset != -1)
				{ 
				offset += search.length;
				end = document.cookie.indexOf(";", offset);
				if (end == -1) end = document.cookie.length;
				cookieValue = unescape(document.cookie.substring(offset, end))
				}
			}
		return cookieValue;
		}

	// Example:
	// writeCookie("myCookie", "my name", 24);
	// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
	function writeCookie(name, value, hours)
		{
		var expire = "";
		if(hours != null)
			{
			expire = new Date((new Date()).getTime() + hours * 3600000);
			expire = "; expires=" + expire.toGMTString();
			}
		document.cookie = name + "=" + escape(value) + expire;
		}
	

