/**********************************************************************
 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>
	
 chkMandatory function was revised and rename on 8-1-2005. JT
**********************************************************************/
var aMandatory = new Array();
function chkMandatoryRevise(objForm){

var msgCounter = 0, msgArray = new Array, phonePattern = /([0-9]{10})/, tempPhone;
var indent = "\n     -  ";
var focusField = "", skipCheck = false;
var regEx = new RegExp("[^0-9]", "g");

  for (var iLoop in aMandatory){
    var strVal, objField;
    strVal = "";
	objField = eval("objForm." + iLoop);
    if (aMandatory[iLoop][1] == "Text"){
	      strVal = eval("objForm." + iLoop + ".value");
		  if (alltrim(strVal) != "")  {
		  	if (objField.name.search("Email") != -1 || objField.name.search("email") != -1 ){
		  		if(!isValidEmail(objField.value)){
		  			msgArray [msgCounter++] = indent + "Invalid " + aMandatory[iLoop][0];					
					if(focusField == ""){focusField = iLoop;}
				}}				
			if (objField.name.search("phone") != -1 || objField.name.search("Phone") != -1 ){						  
					tempPhone = objField.value.replace(regEx, "");
					if (tempPhone.search(phonePattern) == -1){
					msgArray [msgCounter++] = indent + "Invalid " + aMandatory[iLoop][0];}} }	
	 }
    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"){
		if(objField.options[0].selected != true){
     	var iIdx = eval("objForm." + iLoop + ".selectedIndex");
        strVal = eval("objForm." + iLoop + "[" + iIdx + "].value");}}
	 else if (aMandatory[iLoop][1] == "checkbox"){
	 	if(objField.checked != false){strVal = eval("objForm." + iLoop + ".value");}}

    strVal = alltrim(strVal);
    if (strVal == ""){
		msgArray [msgCounter++] = indent + aMandatory[iLoop][0];		
		if(focusField == ""){			
			if (aMandatory[iLoop][2] != ""){
				focusField = aMandatory[iLoop][2];
				skipCheck = true;
			} else {focusField = iLoop;}
			
		}
    }
  } //end loop
  
  if(msgArray != ""){
			alert(" Please double-check and complete the field(s) listed below:\n________________________________________________ \n" + msgArray + "\n\n" + "________________________________________________ ");
  	if(focusField != ""){
  		aType = typeof aMandatory[focusField];
		if (aType != "undefined" && aMandatory[focusField][1] == "Radio"){
      		focusField = eval("objForm." + focusField + "[0]");
    	} else{
      		focusField =  eval("objForm." + focusField);
	  	}		
		focusField.focus();
	}
	return false;
  }

  return true;
}

function isValidEmail(email){
	strEmailPattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if (strEmailPattern.test(email)){return true;}
	else{return false;}}

function isValidURL(inp){		
	url = inp.value.toLowerCase();
	p = new RegExp(/^(http:\/\/|https:\/\/)/);
	if(p.test(url)){
		return true;
	}
	return false;
}
 //funtion from qc_process.js. rename to LATrim in case of conflict
function LATrim(str) { return str.replace(/^[ \t]+|[ \t]+$/g, ""); }