// JavaScript Document: JAVASCRIPT FORMS TOOLKIT v5.0 (March 23, 2004)
/** 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 in the head section exactly as: <script language="JavaScript" src="scripts/tkForms.js"></script>
			 * In the form tag of the form that requires validation, include the onSubmit attribute exactly as: onSubmit="javascript: fnValidateForm(this);"
			 * Now every field that must be validated my have the word 'required' as an attribute in its tag. Add the attribute 'label' as: label="". Enclose in the quotation marks the label of the field as you want appears in the error message to the user.
			 *
			 **************Example:
			 <html><head>
			 <script language="JavaScript" src="scripts/tkForms.js"></script>
			 </head><body>
			 <form name="myform" action="processMyform.php" method="POST" onSubmit="javascript: fnValidateForm(this);">
			 	<input type="text" name="age" id="age" required label="Age">
			 </form>
			 </body></html>
			 *************End Example
	* 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++) {
		if(frmElm[i].getAttribute("required")!=null) {
		  if(frmElm[i].getAttribute("required")!="false") {
			lstReqdElem[lstReqdElem.length] = frmElm[i]; //stringing (the bead) the required object in an array to return.
		  }
		}
	}
	var frmElm = theForm.getElementsByTagName("select");
	for(i=0;i<frmElm.length;i++) {
		if(frmElm[i].getAttribute("required")!=null) {
		  if(frmElm[i].getAttribute("required")!="false") {
			lstReqdElem[lstReqdElem.length] = frmElm[i]; //stringing (the bead) the required object in an array to return.
		  }
		}
	}
	var frmElm = theForm.getElementsByTagName("textarea");
	for(i=0;i<frmElm.length;i++) {
		if(frmElm[i].getAttribute("required")!=null) {
		  if(frmElm[i].getAttribute("required")!="false") {
			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) {
/** 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 input tags must have the attribute required or required=true
	* All reqd field names must have the attribute label="<what ever you want displayed in the error message as field name>"
**/
	var aStr = "Please provide information in the following fields:\n\n"; var aStrFList="";
	var lstReqdElem = fnGetReqdElementsByName(theForm);
	var elementToSetFocusOn = -1;
	for(i=0;i<lstReqdElem.length;i++) {
	  if(lstReqdElem[i].value==null || lstReqdElem[i].value=="") {
		if(lstReqdElem[i].getAttribute("label")!=null) {
		  if(elementToSetFocusOn<0)
		   elementToSetFocusOn = i;	
		  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 = "";
	  }
	}
	aStrFList += fnValidateRadio(theForm);
	if(aStrFList!="") {
		aStr += aStrFList;
		alert(aStr);
		if(elementToSetFocusOn>=0)
		  lstReqdElem[elementToSetFocusOn].focus();
		event.returnValue = false;
	} else {
		return true;
	}
	
}//end of function fnValidateForm

//================================================ START OF RADIOEYE VALIDATION SECTION ==============================================
function fnGetReqdRadiosByName(theForm) {
	var lstReqdElem = new Array();
	var frmElm = theForm.getElementsByTagName("input");
	for(i=0;i<frmElm.length;i++) {
		if((frmElm[i].getAttribute("required")!= null) && (frmElm[i].getAttribute("type")=="radio"))
			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 Radio Elements\n"; //customize this.
		for(i=0;i<lstReqdElem.length;i++)
			aStr += lstReqdElem[i].name + "\n";
		alert(aStr);
		event.returnValue = false;
	*/}
	return lstReqdElem;
}


function checkRadioControl(strFormField) {
	var objFormField = document.forms[0].elements[strFormField];
	intControlLength = objFormField.length;
	for (j=0;j<intControlLength;j++) {
		if(objFormField[j].checked)
			return true;
	}
	return false;
}

function getRadioLabel(strFormField) {
	var objFormField = document.forms[0].elements[strFormField];
	objFormField[0].style.border = "thin solid #990000";
	return objFormField[0].getAttribute("label");
}

function fnValidateRadio(theForm) {
	var aStr = "Please make a selection in the following fields:\n\n"; var aStrFList="";
	var lstReqdElem = fnGetReqdRadiosByName(theForm);
	
	for(i=0;i<lstReqdElem.length;i++)
		if (checkRadioControl(lstReqdElem[i].name)==false)
			aStrFList += getRadioLabel(lstReqdElem[i].name) + "\n";
	
	if(aStrFList!="") {
		return aStrFList;
	} else {
		return "";
	}
	
	/*
	if(aStrFList!="") {
		aStr += aStrFList;
		alert(aStr);
		return false;
	} else {
		return true;
	}
	*/

}//end of function fnValidateRadio
//================================================ END OF RADIOEYE VALIDATION SECTION ==============================================

function ClearOtherDescription(fldOther) {
	fldOther.value="";
}

function FocusOtherDescription(fldOther) {
	fldOther.focus();
}

function CheckOtherDescription(fldOther) {
	fldOther.checked = true;
}

function MandateOtherDescription(fldOther, radOther) {
	if(radOther.checked==true && fldOther.getAttribute("mandate")!= null && (fldOther.value=="" || fldOther.value==null)) {
		alert("Please provide information about your choice Other before proceeding.");
		fldOther.focus();
		fldOther.style.border = "thin solid #990000";
		return false;
	} else {
		fldOther.style.border = "";
		return true;
	}
}

