

//  $Id: formvalidate.js,v 1.6 2004/01/20 16:55:22 jseverson Exp $
//

/**********************************************************************
 chkMandatory: check mandatory fields on the form objForm
 parameters: objForm - form reference

 pre-requisites: 
    aMandatory - array of fields for which mandatory evaluation should be done
    Depends on:   alltrim() which is a function of string.js library
        
		aMandatory should be defined on each HTML page format of aMandatory:
    aMandatory[<field-name-string>] = new Array(<message-string>, <input-type-string>, <focus-on-string>);

 <message-string>: message to be displayed on mandatory check failure 
		(NOTE: system dispalyes "<message-string> is mandatory")
 <input-type-string>: "TEXT", "RADIO"
 <focus-on-string>: name of the field where focus should be placed on mandatory check failure.
		    if this is "" then focus is place on <field-name-string>
**********************************************************************/
var aMandatory = new Array();
function chkMandatory(objForm)
{
	for (var iLoop in aMandatory)
	{
		var strVal;
		strVal = "";
		if (aMandatory[iLoop][1] == "Text")
			strVal = eval("objForm." + iLoop + ".value");
		else if (aMandatory[iLoop][1] == "Radio")
		{
			var oEle = eval("objForm." + iLoop);
			var iLen = oEle.length;
			for (var iEle = 0; iEle < iLen; iEle++)
				if (oEle[iEle].checked)
				{
					strVal = oEle[iEle].value;						
					break;					
				}
		}
		else if (aMandatory[iLoop][1] == "Select")
		{
			var iIdx = eval("objForm." + iLoop + ".selectedIndex");
			if (iIdx > -1)
				strVal = eval("objForm." + iLoop + "[" + iIdx + "].value");
			else
				strVal = "";
		}
    strVal = alltrim(strVal);
    
		if (strVal == "")
		{
			alert("Please enter a value for " + aMandatory[iLoop][0] + ".");
			if (aMandatory[iLoop][2] != "")
				eval("objForm." + aMandatory[iLoop][2] + ".focus()");
			else
			{
				if (aMandatory[iLoop][1] == "Radio")
					eval("objForm." + iLoop + "[0].focus()");
				else
					eval("objForm." + iLoop + ".focus()");
			}
			return false;
		}
	}
	return true;
}

