////$Id$////

/*
This file contain Javascript functions related to HTML forms
Copyright © 2006 WinterNet Studio, Allan Jensen (www.winternet.no). All rights reserved.
*/

function get_field_value(formfield_name) {
	/*
	DESCRIPTION:
	- get a form field value (whether text, dropdown, radio, etc.)
	INPUT:
	- formfield_name : name of form field
	*/
	var value, fieldtype;
	var formfield_obj = get_obj(formfield_name);
	if (typeof formfield_obj.type == "undefined") {
		if (typeof formfield_obj.length != "undefined") {
			switch (formfield_obj[0].type) {
			//a series of radio buttons
			case "radio": fieldtype = "radio"; break;
			//a series of checkboxes all with the same name
			case "checkbox": fieldtype = "checkbox"; break;
			default: js_error("Unknown field type for getting field value.", formfield_obj[0].type);
			}
		} else {
			js_error("Unknown field object for getting field value.", typeof formfield_obj);
		}
	} else {
		fieldtype = formfield_obj.type;  //alternative is the tagName property 
	}
	switch (fieldtype) {
	case "select-one":
	case "select-multiple":
		value = get_dropdown_value(formfield_obj);
		break;
	case "text":
	case "textarea":
	case "hidden":
	case "submit":
		value = formfield_obj.value;
		break;
	case "checkbox":
		if (formfield_obj.type) {  //= single checkbox
			if (formfield_obj.checked) {
				value = formfield_obj.value;
			} else {
				value = "";
			}
		} else {  //= multiple checkboxes
			value = new Array();
			for (var i=0; i < formfield_obj.length; i++) {
				if (formfield_obj[i].checked) {
					value.push(formfield_obj[i].value);
				}
			}
		}
		break;
	case "radio":
		value = "";
		if (formfield_obj.type) {  //= single radio button
			if (formfield_obj.checked) {
				value = formfield_obj.value;
			}
		} else {  //= multiple radio buttons
			for (var i=0; i < formfield_obj.length; i++) {
				if (formfield_obj[i].checked) {
					value = formfield_obj[i].value;
					break;
				}
			}
		}
		break;
	default:
		js_error("Undefined form field type for getting value.", fieldtype);
	}
	return value;
}

function set_field_value(formfield_name, set_value) {
	/*
	DESCRIPTION:
	- set a form field value (whether text, dropdown, radio, etc.)
	- for dropdown, radio buttons, and checkbox it does not alter the values but only which ones are selected
	INPUT:
	- formfield_name : name of form field
	- set_value : value to be set/selected
		- select-multiple: must be an array in case of a dropdown box with multiple selections unless the value is "#set_default" (provide empty array to deselect all)
			- values already selected but not included in this array will be unmarked
		- radio: set non-existing value to not have any radio button selected
		- checkbox: if multiple checkboxes with the same name exists, the value argument can be an array of values for which the checkbox should be checked
			- boxes already checked but whose values are not included in this array will be unchecked
		- set this to "#set_default" to have the default value for the field shown (no need for an array for select-multiple)
	*/
	var fieldtype, cvalue, i;
	var formfield_obj = get_obj(formfield_name);
	if (typeof formfield_obj.type == "undefined") {
		if (typeof formfield_obj.length != "undefined") {
			switch (formfield_obj[0].type) {
			//a series of radio buttons
			case "radio": fieldtype = "radio"; break;
			//a series of checkboxes all with the same name
			case "checkbox": fieldtype = "checkbox"; break;
			default: js_error("Unknown field type for setting field value.", formfield_obj[0].type);
			}
		} else {
			js_error("Unknown field object for setting field value.", typeof formfield_obj);
		}
	} else {
		fieldtype = formfield_obj.type;  //alternative is the tagName property 
	}
	switch (fieldtype) {
	case "select-one":
	case "select-multiple":
		var coption;
		if (formfield_obj.options.length > 0) {  //check if the dropdown has any values
			if (fieldtype == "select-one") {
				for (i=0; i<formfield_obj.options.length; i++) {
					coption = formfield_obj.options[i];
					if (set_value == "#set_default") {
						coption.selected = coption.defaultSelected;
					} else {
						if (coption.value == set_value) {
							coption.selected = true;
							break;
						}
					}
				}
			} else { //select-multiple
				var is_sel;
				if (typeof set_value != "object" && set_value != "#set_default") {
					js_error("Parameter to set selected values for multiple-selection dropdown is not a list of values.", typeof set_value);
				} else {
					for (i=0; i<formfield_obj.options.length; i++) {
						if (set_value == "#set_default") {
							is_sel = formfield_obj.options[i].defaultSelected;
						} else {
							is_sel = false;
							for (var indx in set_value) {
								cvalue = set_value[indx];
								if (formfield_obj.options[i].value == cvalue) {
									is_sel = true;
								}
							}
						}
						formfield_obj.options[i].selected = is_sel;
					}
				}
			}
		}
		break;
	case "text":
	case "textarea":
	case "hidden":
	case "submit":
		if (set_value == "#set_default") {
			if (fieldtype == "text" || fieldtype == "textarea") {  //"hidden" has no defaultValue property
				formfield_obj.value = formfield_obj.defaultValue;
			}
		} else {
			formfield_obj.value = set_value;
		}
		break;
	case "checkbox":
		if (formfield_obj.type) {  //= single checkbox
			if (set_value == "#set_default") {
				formfield_obj.checked = formfield_obj.defaultChecked;
			} else {
				formfield_obj.checked = (formfield_obj.value == set_value ? true:false);
			}
		} else {  //= multiple checkboxes
			if (typeof set_value != "object") {
				set_value = new Array(set_value);
			}
			var set_check;
			for (var i=0; i < formfield_obj.length; i++) {
				set_check = false;
				for (var j in set_value) {
					if (set_value[j] == formfield_obj[i].value) {
						set_check = true;
						break;
					}
				}
				formfield_obj[i].checked = set_check;
			}
		}
		break;
	case "radio":
		for (var i=0; i < formfield_obj.length; i++) {
			if (set_value == "#set_default") {
				formfield_obj[i].checked = formfield_obj[i].defaultChecked;
			} else {
				formfield_obj[i].checked = (formfield_obj[i].value == set_value ? true:false);
			}
		}
		break;
	default:
		js_error("Undefined form field type for setting value.", fieldtype);
	}
}

function get_dropdown_value(select_obj) {
	if (select_obj.options.length > 0) {  //check if the dropdown has any values
		if (select_obj.type == "select-one") {
			myvalue = select_obj.options[select_obj.selectedIndex].value;
			return myvalue;
		} else if (select_obj.type == "select-multiple") {
			var selected = new Array();
			for (var indx = 0; indx < select_obj.options.length; indx++) {
				if (select_obj.options[indx].selected) {
					selected.push(select_obj.options[indx].value);
				}
			}
			if (selected.length > 0) {
				return selected;
			} else {
				return "";  //return something that will evaluate to false (an empty array won't)
			}
		} else {
			alert("Error occured in getting value from dropdown. Please contact system administrator.");
		}
	} else {
		return "";
	}
}

function get_dropdown_value_label(select_obj) {
	if (select_obj.options.length > 0) {  //check if the dropdown has any values
		if (typeof select_obj == 'object') {
			mylabel = select_obj.options[select_obj.selectedIndex].text;
			return mylabel;
		} else {
			alert("Error occured in getting value label from dropdown. Please contact system administrator.");
		}
	} else {
		return "";
	}
}

function dropdown_add(obj, thevalue, thelabel, is_selected) {
	next_index = obj.options.length;
	obj.options[next_index] = new Option(thelabel, thevalue, is_selected, is_selected); //it will only be selected if both arguments are set to true!! (but IE 5.0 does not even correctly set the default value!)
	if (is_selected) {  //IE does not correctly display the selected value by setting it when the item is added to the dropdown, therefore also set the selected here (this is at least what I experienced)
		obj.options[next_index].selected = true
	}
	return next_index;  //return the index number of this new option
}

function dropdown_clear_options(obj) {
	obj.options.length = 0;
}

function get_checkbox_value(obj) {
	var value = "";
	if (obj.type == "checkbox") {
		if (obj.checked) {
			value = obj.value;
		} else {
			value = "";
		}
	} else {
		js_error("Object is not a checkbox.", obj.type);
	}
	return value;
}

function get_radiobutton_value(obj) {
	var value = "";
	if (obj.type == "radio") {
		for (var i=0; i < obj.length; i++) {
			if (obj[i].checked) {
				value = obj[i].value;
				break;
			}
		}
	} else {
		js_error("Object is not a so-called radiobutton.", obj.type);
	}
	return value;
}

function transfer_standard_selection(transfer_from, transfer_to, form_name) {
	if (transfer_from.selectedIndex != 0) {  //first selection in dropdown is not a value
		transfer_value = transfer_from.options[transfer_from.selectedIndex].text;  //take label instead of value
	} else {
		transfer_value = '';
	}
	document.forms[form_name].elements[transfer_to].value = transfer_value;
	if (transfer_value != '') {
		document.forms[form_name].elements[transfer_to].disabled = true;
	} else {
		document.forms[form_name].elements[transfer_to].disabled = false;
	}
}

function set_opener_field(field_in_parent_window, myvalue, add_seperator) {
	/*
	DESCRIPTION:
	- copy a value to a field in the parent window
	INPUT:
	- add_seperator : whether or not the existing value should be overwritten or the new value should just be added
		- set to true if value should be added without any seperator
		- write a seperator string if value should be added with a seperator
		- omit parameter (or set to false) to have new value overwrite any current value
	*/
	obj = getele(field_in_parent_window, opener.document);  //get the field
	currvalue = obj.value;  //get current value
	if (currvalue.length > 0 && add_seperator) {
		if (add_seperator === true) add_seperator = '';
		newvalue  = currvalue + add_seperator + myvalue;
	} else {
		newvalue  = myvalue;
	}
	obj.value = newvalue;  //set new value
}
