// JavaScript Document
/** Conceptualized and Developed by Harish Agarwal: agarwalh1@southernct.edu
	* This is a toolkit containing commonly used javascript funtions
	* Usage: * This javascript file tkForms.js must exist in a folder named scripts which in turn must exist on the same level as the page that will use this script/function.
			 * The webpage using this toolkit must include this toolkit: <script language="JavaScript" src="scripts/tkForms.js"></script>
	* More functions will be added to this toolkit in time. Any contributions are welcome.
	* I hereby grant the rights for free use of this toolkit by anyone anywhere. Courtesy of leaving my name and email up there is expected but optional.
	
**/

function fnGetReqdElementsByName(theForm) {
	var lstReqdElem = new Array();
	var frmElm = theForm.getElementsByTagName("input");
	for(i=0;i<frmElm.length;i++) {
		var theElem = frmElm[i].name;//this needs to be done so javascript can consider the id as a string and the function can use the substring and lastIndexOf functions on that string.
		isReqd = theElem.substring(theElem.lastIndexOf("_")+1)
		if(isReqd=="reqd" || isReqd=="required")
			lstReqdElem[lstReqdElem.length] = frmElm[i]; //stringing (the bead) the required object in an array to return.
	}
	
	/** Uncomment this block to see a list of required elements in an alert dialog. 
		This code can also be used in a form validation function. 
			* Just customize the initial text
			* Add an if construct within the for loop that checks for the object's value.
	**/
	{/*
		var aStr = "Required Elements\n"; //customize this.
		for(i=0;i<lstReqdElem.length;i++)
			aStr += lstReqdElem[i].name + "\n";
		alert(aStr);
	*/}
	
	return lstReqdElem;
}

function fnValidateForm(theForm) {
	var lstReqdElem = fnGetReqdElementsByName(theForm); //List the fields that require user response
	var aStr = "Please provide information in the following fields:\n\n";
	var aStrFList = "";
	for(i=0;i<lstReqdElem.length;i++) {
	  if(lstReqdElem[i].value==null || lstReqdElem[i].value=="") {
		if(lstReqdElem[i].getAttribute("label")!=null)
		  aStrFList += lstReqdElem[i].getAttribute("label") + "\n";
		else
		  aStrFList += "Field at position " + (i+1) + " from top\n";
		lstReqdElem[i].style.border = "thin solid #990000";
	  } else {
		lstReqdElem[i].style.border = "";
	  }
	}
	if(aStrFList!="") {
		aStr += aStrFList;
		alert(aStr);
		event.returnValue = false;
	} else { 
		alert("Your information has reached us successfully. Thank-you."); 
		return;
	}
	
/** fnValidateForm() USAGE
	* The javascript file tkForms.js must exist in a folder named scripts which in turn must exist on the same level as the page that will use this script/function.
	* The page must include the javascript file that contains the fnValidateForm()
	* the submit button must call the validate function fnValidateForm(<formname>) as an onClick event. i.e. onClick="fnValidateForm(form1)"
	* All reqd field names must have the name followed by "_reqd" or "_required"
	* All reqd field names must have the attribute label="<what ever you want displayed in the error message as field name>"
**/
	return;
}//end of function fnValidateForm
