/*******************************************************************************
*
*	Project		:	O3L FNI 
*
*	File		:	O3L_FNIForm.js
*
*	Copyright	:	Data Broadcast Services Pty Ltd
*					All rights reserved.
*
*	Purpose		:	Provide a form processing library that allows 
*					simple page calls to include validation.
*
*	Public Functions: 	O3_SubmitForm(xiPageVersion, xcPageDefn )
*					Validate the FNI Page Def for this page.
*					Called from the <BODY onload=>
*
*				O3_SubmitFormNoVal(xiPageVersion, xiElement,xiForm,
*						xcPageDefn, xiWebPage, xiFunction )
*					Validate an individual PageDefelement on the page.
*
*		
*******************************************************************************/


/*******************************************************************************
Globals
*******************************************************************************/
// NO GLOBALS REQUIRED


/*******************************************************************************
Functions
*******************************************************************************/


/*******************************************************************************
*
*	Scope		:	PUBLIC
*	Function	:	O3_SubmitForm()
*
*	Purpose		:	Sets the required values and then calls the 
*					standardard validation for this page
*
*	Parameters	:	xiForm	 		Form for the data to be processed
*
*					xiButtonNm		Button Name pressed. Passed for cgi.
*
*					xiValueField	Field name of the value to be set 
*									when this button is clicked - Optional
*
*					xiValue			Value to be set when this button is
*									clicked - Optional
*
*
*	Returns		:	boolean:		true	if successfully
*									fail	otherwise
*
*	JavaScript	:	1.3
*
*	Assumptions	:	ValidationObject is defined in the Page
*
*
*******************************************************************************/
 
function O3_SubmitForm(xiForm, xiButtonVal, xiValueField, xiValue )
{
	var pageForm
	// Set the page Values for this button.
	if (!document.getElementById("ButtonNm"))
	{
		alert("Cannot find the required ButtonNm field in the form >"+xiForm+"<");
		return false;
	}
	else
		document.getElementById("ButtonNm").value = xiButtonVal;
	

	if(xiValueField)
	{
		if(ValueElement = document.getElementById(xiValueField))
		{
			ValueElement.value = xiValue;
		}
	}
	
	if (!document.getElementById(xiForm))
	{
		alert("Cannot find the required form >"+xiForm+"<");
		return false;
	}
	else
		pageForm=document.getElementById(xiForm);

	// Call the Generic Page Validation Function.
	if (!ValidatePage(PageVersion, xiForm, ValidationObject, document,"Page_ShowError"))
	{
		return false;	
	}
	else
	{
		pageForm.submit();
	}

	return true;
}

/*******************************************************************************
*
*	Scope		:	PUBLIC
*	Function	:	O3_SubmitFormNoVal()
*
*	Purpose		:	Sets the required values.
*
*	Parameters	:	xiForm	 		Form for the data to be processed
*
*					xiButtonNm		Button Name pressed. Passed for cgi.
*
*					xiValueField	Field name of the value to be set 
*									when this button is clicked - Optional
*
*					xiValue			Value to be set when this button is
*									clicked - Optional
*
*
*	Returns		:	boolean:		true	if successfully
*									fail	otherwise
*
*	JavaScript	:	1.3
*
*	Assumptions	:	ValidationObject is defined in the Page
*
*
*******************************************************************************/
function O3_SubmitFormNoVal(xiForm, xiButtonVal, xiValueField, xiValue )
{
	
	// Set the page Values for this button.
	if (!document.getElementById("ButtonNm"))
	{
		alert("Cannot find the required ButtonNm field in the form >"+xiForm+"<");
		return false;
	}
	else
		document.getElementById("ButtonNm").value = xiButtonVal;
		
	if(xiValueField)
	{
		if(ValueElement = document.getElementById(xiValueField))
		{
			ValueElement.value = xiValue;
		}
	}

	if (!document.getElementById(xiForm))
	{
		alert("Cannot find the required form >"+xiForm+"<");
		return false;
	}
	document.getElementById(xiForm).submit();
	return true;
}


/*******************************************************************************
*
*	Scope		:	PUBLIC
*	Function	:	O3_SubmitFormSort()
*
*	Purpose		:	Sets the required values.
*
*	Parameters	:	xiForm	 		Form for the data to be processed
*
*					xiButtonNm		Button Name pressed. Passed for cgi.
*
*					xiSortSet		Name of the sort set 
*
*					xiOrderField	Name of the order field in the sort set
*
*
*	Returns		:	boolean:		true	if successfully
*									fail	otherwise
*
*	JavaScript	:	1.3
*
*	Assumptions	:	N/A
*
*
*******************************************************************************/
function O3_SubmitFormSort(xiForm, xiButtonVal, xiSortSet, xiOrderField )
{
	var OrderField_elt = document.getElementById(xiSortSet + '_OrderField');
	var OrderDirn_elt = document.getElementById(xiSortSet + '_OrderDirn');
	if((xiOrderField == OrderField_elt.value) && (OrderDirn_elt.value == 'ASC'))
	{
		OrderDirn_elt.value = 'DESC';
	}
	else
	{
		OrderDirn_elt.value = 'ASC';
	}
	OrderField_elt.value = xiOrderField;
	return O3_SubmitFormNoVal(xiForm, xiButtonVal, '', '');
}


/*******************************************************************************
*
*	Scope		:	PUBLIC
*	Function	:	O3_ShowError()
*
*	Purpose		:	Alerts the user to fields that fail validation. If the Object is not passed or not
*						valid a standard "alert" box is used.
*
*	Parameters	:	xiString	 		Error String to display
*
*					      xiObject		Object with validation error.
*
*	Returns		:	None
*
*	JavaScript	:	1.3
*
*	Assumptions	:	
*
*
*******************************************************************************/

function O3_ShowError(xiString,xiObject)
{
			
	if (xiObject)
	{
		document.getElementById(xiObject).style.backgroundColor="#FFCCCC";
		alert(xiString);
	}
	else
		alert(xiString);
	return;
}

/*******************************************************************************
*
*	Scope		:	PUBLIC
*	Function	:	O3_SetFormFromObj()
*
*	Purpose		:	Sets the form values from the object passed
*
*	Parameters	:	xiForm 		Form to use
*
*					      xiObject	Object with required values.
*
*	Returns		:	None
*
*	JavaScript	:	1.3
*
*	Assumptions	:	
*
*
*******************************************************************************/
function O3_SetFormFromObj(xiObject)
{
	var Attrib;
	var Element;
			
	if (!xiObject)
	{
		alert("O3_SetFormFromObj: Required Object not passed!");
		return;
	}
	
	// For each Attribute of the Object set a value. 
	for ( Attrib in xiObject)
	{
		// Find the attribute in the page if it exists.
		// NOTE: Page Validation rules will ensure required objects are 
		// correctly populated.
		if (Element=document.getElementById(Attrib))
		{
			if((Element.type != 'hidden' ) || (Element.type != 'text' )   || (Element.type != 'password' ))
			{
				document.getElementById(Attrib).value=xiObject[Attrib];
			}
		}
	}

	return;
}

function O3_SetFocus( xi_Form_name )
{
	var	Form_obj = document.getElementById(xi_Form_name)
	
	if(Form_obj)
	{
		if( Form_obj.elements[0]!=null)
		{
			var elt;
			var max = Form_obj.elements.length;
			for( elt = 0; elt < max; elt++ )
			{
				if( Form_obj.elements[elt].type != "hidden" &&
					!Form_obj.elements[elt].disabled &&
					!Form_obj.elements[elt].readOnly &&
					Form_obj.elements[elt].style.visibility != 'hidden' &&
					Form_obj.elements[elt].style.display != 'none')
				{
					Form_obj.elements[elt].focus();
					break;
				}
			}
		}
	}
}

	function O3_ComboNewNode()
	{
		var	node = new Object();
		node.options = new Array();
		node.nodes = new Object();
		
		return node;
	}
	function O3_ComboTreeAdd(xc_Tree_obj)
	{
		var	args = arguments;
		var	depth = (arguments.length - 1) / 2;
		var	node = xc_Tree_obj;
		var	value, display;
		
		for(var lvl = 0; lvl < depth; lvl++)
		{
			display = arguments[lvl * 2 + 1];
			value = arguments[lvl * 2 + 2];
			alert("seeking " + value);
			if(!node.nodes[value])
			{
				alert("found")
				node.nodes[value] = O3_ComboNewNode();
				node.options[node.options.length] = new Array(display, value);
			}
			else
			{
				alert("not found")
			}
			node = node.nodes[value];
		}
		return true;
	}

	function O3_ComboClear(xi_Combo, xi_Preserve)
	{
		var	Combo_obj;
		if(typeof(xi_Combo) == 'object')
		{
			Combo_obj = xi_Combo;
		}
		else
		{
			Combo_obj = document.getElementById(xi_Combo)
		}
		if(Combo_obj)
		{
			for(var lgth = Combo_obj.length; lgth > xi_Preserve; lgth--)
			{
				Combo_obj[lgth - 1] = null;
			}
		}

		return true;
	}

	function O3_ComboTreeBuild(xi_Tree_obj, xc_Combo, xi_Preserve)
	{
		var	args = arguments;
		var	depth = arguments.length - 3;
		var	Combo_obj;
		var	node = xi_Tree_obj;
		var	value, display;
		
		if(typeof(xc_Combo) == 'object')
		{
			Combo_obj = xc_Combo;
		}
		else
		{
			Combo_obj = document.getElementById(xc_Combo)
		}
		if(Combo_obj)
		{
			O3_ComboClear(Combo_obj, xi_Preserve);
			
			for(var lvl = 0; lvl < depth; lvl++)
			{
				value = arguments[lvl + 3];
				node = node.nodes[value];
			}
			if(node)
			{
				for(var option_nr = 0; option_nr < node.options.length; option_nr++)
				{
					Combo_obj.options[option_nr] = new Option(node.options[option_nr][0],node.options[option_nr][1]);
				}
			}
			Combo_obj.selectedIndex = 0;
			(Combo_obj.onchange) &&	Combo_obj.onchange();
			
		}
	}
	
	function O3_RadioValue(xi_Form_name, xi_Radio_name)
	{
		// Get the value of a radio or checkbox in a form
		var	Radio_obj =	document.forms[xi_Form_name].elements[xi_Radio_name];
		if(!Radio_obj) return "";
		var radioLength = Radio_obj.length;
		if(radioLength == undefined)
			if(Radio_obj.checked)
				return Radio_obj.value;
			else
				return "";
		for(var i = 0; i < radioLength; i++)
		{
			if(Radio_obj[i].checked)	return Radio_obj[i].value;
		}
		return "";
	}
	function O3_RadioSet(xi_Form_name, xi_Radio_name, xi_Value)
	{
		// Set the value of a radio or checkbox in a form
		var	Radio_obj =	document.forms[xi_Form_name].elements[xi_Radio_name];
		if(!Radio_obj) return;
		var radioLength = Radio_obj.length;
		if(radioLength == undefined)
		{
			Radio_obj.checked = (Radio_obj.value == xi_Value.toString());
			return;
		}
		for(var i = 0; i < radioLength; i++)
		{
			Radio_obj[i].checked = false;
			if(Radio_obj[i].value == xi_Value.toString())
			{
				Radio_obj[i].checked = true;
			}
		}
	}


