/**********************************************************************
 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 = "";
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 (LATrim(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 = LATrim(strVal);
    if (strVal == ""){
		msgArray [msgCounter++] = indent + aMandatory[iLoop][0];		
		if(focusField == ""){			
			if (aMandatory[iLoop][2] != ""){
				focusField = aMandatory[iLoop][2];
			} 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, ""); }


/////function allow input number only and one dot if dec equal true
function doDigit(myfield, e, dec){		
	var key, keychar;	
	if (window.event){key = window.event.keyCode;}
	else if (e){key = e.which;}
	else{return true;}	
	//allow input (.)dot (once only) with money fields
	if(dec){if(key==46){
			oneDotOnly = /([.]{1})/;
			if(myfield.value.search(oneDotOnly) == -1){return true; }
			else{ return false;}}}	
	if(arguments[3]){ //assumming fourth param allow negative sign (-)
		if(key == 45){			
			if(myfield.value.indexOf("-") == -1){return true; }
			else{ return false;}
		}
	}
	keychar = String.fromCharCode(key);	
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ){return true;}
	else if ((("1234567890").indexOf(keychar) > -1)){return true;}
	else{return false;}}
	
function autoTab(input,len, e){
  var isNN = (navigator.appName.indexOf("Netscape")!=-1);
  var keyCode = (isNN)?e.which:e.keyCode;
  var filter = (isNN)?[0,8,9]:[0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)){
    input.value = input.value.slice(0,len);	  
    input.form[(getLoanAppIndex(input)+1)%input.form.length].focus();	
  }
  return true;
}

function containsElement(arr, ele){
//alert("in");
  var found = false, index = 0;
  while(!found && index < arr.length)
    if(arr[index]==ele)
      found = true;
    else
      index++;

  return found;
}

function getLoanAppIndex(input){
  var index = -1, i = 0, found = false;
  while (i < input.form.length && index==-1)
    if (input.form[i] == input)index = i;
    else i++;
  return index;
}

function isValidPhone(pNumber){
	var phonePattern = /([0-9]{10})/;
	var regEx = new RegExp("[^0-9]", "g");
	pNumber = pNumber.replace(regEx, "");
	return (pNumber.search(phonePattern) > -1);
}