

//  $Id: formvalidate.js,v 1.11 2004/11/05 18:16:30 jreed 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;
}

function disableButtons(objForm) {
  var i
  for(i=0; i<objForm.elements.length; i++) {
    if(objForm.elements[i].type == "submit" || objForm.elements[i].type == "button") {
      objForm.elements[i].disabled = true;
    }
  }
  hideshowlayers(objForm);
}

function hideshowlayers(objForm) {
  // This fixes a bug in IE where layers disappear even though they are visible
  if (objForm) {
		for(i=0; i<objForm.all.length; i++) {
			if(objForm.all[i].tagName == "DIV") {
				if(objForm.all[i].style.visibility == "visible") {
					objForm.all[i].style.visibility = "hidden";
					objForm.all[i].style.visibility = "visible";
				}
			}
		}
	}
}

function setFormFocus (objForm)
{
  //Call this function from the body onLoad and it will set focus to first field in objForm
  if(objForm) {
    aForm = objForm;
    if(aForm.elements[0]!=null) {
      var i;
      var max = aForm.length;
      for( i = 0; i < max; i++ ) {
        if( aForm.elements[i].type != "hidden" &&
            aForm.elements[i].type != "button" &&
            !aForm.elements[i].disabled &&
            !aForm.elements[i].readOnly ) {
            aForm.elements[i].focus();
          break;
        }
      }
    }
  }
}

function SetFormStatus(/*DOM node*/ d, /*String*/ s){
	if(arguments.length == 2){
		d.innerHTML = s;
	}
	else{
		d.innerHTML += s;
	}
	if(d.style.display != 'block') d.style.display = 'block';
}

