// work out which browser type is needed
var ns4 = false;
var ns5plus = false;
var ie = false;
if (document.layers)
	ns4 = true;
else if ((parseInt(navigator.appVersion) >= 5) && (navigator.appName=="Netscape"))
	ns5plus = true;
else if (document.all)
	ie = true;


// make the user confirm their action before proceeding (used when deleting things,  etc)
function confirm_action (message, urllocation) {
	if (!confirm (message)) {
		return (false);
	}
	else {
		document.location.href = urllocation;
	}
}

// record in a hidden field that a form on the page has been updated
function form_changed () {
	document.formChanges.changesMade.value = 'Y';
}

// record in a hidden field that a form on the page has been updated
function go_to (urllocation) {
	if (document.formChanges.changesMade.value == 'Y')
		confirm_action ("You have made changes to a form on this page.\nDo you wish to continue without saving?", urllocation);
//		alert ("You have made changes to the form on this page.\nPlease save this information before continuing.");
	else
		document.location.href = urllocation;
}

// toggle a checkbox's value
function toggle_checkbox (fieldName, arrayPosition) {
	temp = get_object (fieldName);

	// check to see if javascript thinks this is an object (array) or not
	if (typeof (temp[arrayPosition]) != 'undefined') {
		if (temp[arrayPosition].checked == false)
			temp[arrayPosition].checked = true;
		else
			temp[arrayPosition].checked = false;
	}
	else {
		if (temp.checked == false)
			temp.checked = true;
		else
			temp.checked = false;
	}
	return true;
}

// toggle a checkbox's value
function set_radio_button (fieldName, arrayPosition) {
	temp = get_object (fieldName);

	// check to see if javascript thinks this is an object (array) or not
	if (typeof (temp[arrayPosition]) != 'undefined')
		temp[arrayPosition].checked = true;
	else
		temp.checked = true;

	return true;
}

// display or hide a div
function flip_div (divName) {
	temp = get_object (divName);

	current=(temp.style.display == 'none') ? 'inline' : 'none';
	temp.style.display = current;
}

// display a div
function display_div (divName) {
	temp = get_object (divName);

	temp.style.display = 'inline';
}

// hide a div
function hide_div (divName) {
	temp = get_object (divName);

	temp.style.display = 'none';
}














// 
function get_object (fieldName) {
	if (ie)
		temp = document.all[fieldName];
	else if (ns5plus)
		temp = document.getElementById(fieldName);
	else if (ns4)
		temp = document.layers[fieldName];

	return temp;
}

// 
function select_all_checkboxes (fieldName) {
	temp = get_object (fieldName);

	if (temp.length) {
		for (count = 0; count < temp.length; count++)
			temp[count].checked = true;
	}
	else
		temp.checked = true;
}

// 
function unselect_all_checkboxes (fieldName) {
	temp = get_object (fieldName);

	if (temp.length) {
		for (count = 0; count < temp.length; count++)
			temp[count].checked = false;
	}
	else
		temp.checked = false;
}

function are_all_checkboxes_selected (fieldName) {
	temp = get_object (fieldName);

	allSelected = true;
	if (temp.length) {
		for (count = 0; count < temp.length; count++) {
			if (temp[count].checked == false)
				allSelected = false;
		}
	}
	else {
		if (temp.checked == false)
			allSelected = false;
	}
	return allSelected;
}

// 
function toggle_all_checkboxes (fieldName) {
	if (are_all_checkboxes_selected (fieldName))
		return unselect_all_checkboxes (fieldName);
	return select_all_checkboxes (fieldName);
}

// 
function update_button_text_based_on_all_checkboxes (fieldName, buttonName, allSelectedText, notAllSelectedText) {
	buttonObject = get_object (buttonName);

	if (are_all_checkboxes_selected (fieldName))
		buttonObject.value = allSelectedText;
	else
		buttonObject.value = notAllSelectedText;
}



//***********************************************************************/
//*	DATA STRUCTURES									*/
//***********************************************************************/

	function month(name, numdays, abbr) 
	{
		this.name = name;
		this.numdays = numdays;
		this.abbr = abbr;
	}

//*********************************************************************/

	function ans(daySave,value)
	{
		this.daySave = daySave;
		this.value = value;
	}

//*********************************************************************/

	function city(name, lat, lng, zoneHr) 
	{
		this.name = name;
		this.lat = lat;
		this.lng = lng;
		this.zoneHr = zoneHr;
	}





























	var jsFormManager = Class.create ();
	jsFormManager.prototype = {
		iframeForms: new Object (),
		identifier: "jsFormManager",
		randomIdentifier: undefined,

		// constructor
		initialize: function () {
			// unique identifier
			this.randomIdentifier = Math.round ((Math.random () * 10000000) + 1);
		},

		// generate the domId of a message block holder div
		get_iframe_domId: function () {
			return this.identifier + "_" + this.randomIdentifier + "_iframes";
		},

		// 
		submit_form: function (formDomId, targetUrl, httpMethod) {
			if ($(formDomId) != null) {

				// make sure the main tools div exists
				if ($(this.get_iframe_domId ()) == undefined) {
					// set up the main div that will hold the message divs
					var element_div = document.createElement ("div");
					element_div.setAttribute ("id", this.get_iframe_domId ());
					$("toolsDiv").appendChild (element_div);
				}




				var id = this.get_iframe_domId () + "_" + formDomId + "_iframe";

				// remove the iframe if it exists
				if ($(id) != null)
					$(id).remove ();

				var div = document.createElement ("div");
				div.innerHTML = '<iframe src="about:blank" style="display: none;" id="' + id + '" name="' + id + '" onLoad="jsFormManager.load_finished (this.id);"></iframe>';
//				div.innerHTML = '<iframe src="about:blank" style="display: inline;" id="' + id + '" name="' + id + '" onLoad="jsFormManager.load_finished (this.id);"></iframe>';
				$(this.get_iframe_domId ()).appendChild (div);

				httpMethod = String (httpMethod);
				httpMethod = httpMethod.toUpperCase ();
				if ((httpMethod != 'GET') && (httpMethod != 'POST'))
					httpMethod = 'POST';

				$(formDomId).setAttribute ("action", targetUrl);
				$(formDomId).setAttribute ("target", id);
				$(formDomId).setAttribute ("method", httpMethod);
				$(formDomId).setAttribute ("enctype", "multipart/form-data");	// firefox
				$(formDomId).setAttribute ("encoding", "multipart/form-data");	// internet explorer
				$(formDomId).submit ();
			}
		},

		load_finished: function (iframeDomId) {
//			var i = $(iframeDomId);
			var i = document.getElementById (iframeDomId);
			if (i.contentDocument)
				var d = i.contentDocument;
			else if (i.contentWindow)
				var d = i.contentWindow.document;
			else
				var d = window.frames[id].document;

			if (d.location.href == "about:blank")
				return;
//			if (typeof(i.onComplete) == 'function')
//				i.onComplete(d.body.innerHTML);

			var xmlDoc = null;
			try {	//Internet Explorer
				xmlDoc = new ActiveXObject ("Microsoft.XMLDOM");
				xmlDoc.async = "false";
//				xmlDoc.loadXML (d.body.innerHTML);
				xmlDoc.loadXML (d.getElementById ('response').value);
			}
			catch (e) {
				var xmlDoc = null;
				try {	// Firefox, Mozilla, Opera, etc.

//					xmlDoc = document.implementation.createDocument ("", "", null);
//					xmlDoc.async = "false";

					parser = new DOMParser ();
//					xmlDoc = parser.parseFromString (d.body.innerHTML, "text/xml");
					xmlDoc = parser.parseFromString (d.getElementById ('response').value, "text/xml");
				}
				catch (e) {
					var xmlDoc = null;
				}
			}

			if (xmlDoc != null) {
//				alert (d.body.innerHTML);
//				alert (d.getElementById ('response'));
				return gc_ajaxManager.process_xml (xmlDoc);
			}
		}
	}

	jsFormManager = new jsFormManager ();
