/**
 * @author		Library Automation Services Team
 * @version		1.0
 * @copyright	Sarawak Information Systems Sdn. Bhd.
 *
 * Development Environment			:	EditPlus
 * Name of the Application			:	forms.js
 * Overview of Application			:	Contains javascript functions for forms
 * Creation/Modification History	:
 *		12-Apr-2001		Created
 *		27-Jun-2001		Updated
 *		29-Jun-2001		Updated - Include calling of a function valForm() in submitForm()
 *		17-FEB-3004		Updated - Include tickRadioValue
 * 
 * Function Declaration:
 *		function submitForm(frm)
 *		function identifyListValue(obj, p_value)
 *		function uniqueD(f, obj, num, all)
 *		function tickRadioValue(obj, p_value)
 *
 */

/**
 * Automatically submit a form when user press enter key
 * Pre-requisite: Must implement a function called valForm(formObj) 
 * 		  in the caller of submitForm() JavaScript
 */
function submitForm(frm) {
	if (document.layers)
		document.captureEvents(Event.KEYDOWN);
		document.onkeydown =
		function (evt) {
			var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
			var eventTarget = evt ? evt.target : event.srcElement;
			var textField = eventTarget.type == 'text' || eventTarget.type == 'password' || eventTarget.type == 'textarea';
			if (keyCode == 13 && textField) {
				if (valForm(frm)) {
					frm.submit();
					return false;
				}
			} else
				return true;
			};
}

/**
 * Select the appropriate value from a list box
 */
function identifyListValue(obj, p_value) {
	for (var i=0; i< obj.options.length; i++) {
		if (obj.options[i].value == p_value)
			obj.options[i].selected = true;
	}
return; }

/**
 * Select the appropriate value from a list box
 */
function tickRadioValue(obj, p_value) {
	for (var i=0; i< obj.length; i++) {
		if (obj[i].value == p_value)
			obj[i].checked = true;
	}
return; }


/**
 * Get the ticked radio button value
 */
function getRadioValue(obj) {
	for (var i=0; i<obj.length; i++) {
		if (obj[i].checked) {
			return obj[i].value
		}
	}
	return "";
}

/**
 * This function is used to ensure that a value selected in current drop down select box is unique and others do not have the same value.
 * Example <select name="dummy" onchange="uniqueD(document.frmMain, this, 0, 0);"><option...
 * First parameter is the name of the form
 * Second parameter is the object of focus
 * Second last parameter is the numbering of the objects start with 0
 * Last parameter used to determine the scope of the uniqueness of values in a form
 */
function uniqueD(f, obj, num, all)
{	counter = 0;
	for (i=0;i<f.elements.length;i++)
	{	if (all==1)
		{	if (f.elements[i].type=="select-one")
			{	if ((obj[obj.selectedIndex].value==f.elements[i][f.elements[i].selectedIndex].value) && (counter!=num))
				{	f.elements[i].selectedIndex=0;
				}
				counter = counter + 1;
			}
		}
		else
		{	if ((f.elements[i].type=="select-one") && (obj.name==f.elements[i].name))
			{	if ((obj[obj.selectedIndex].value==f.elements[i][f.elements[i].selectedIndex].value) && (counter!=num))
				{	f.elements[i].selectedIndex=0;
				}
				counter = counter + 1;
			}
		}
	}
}
// End of forms.js