/*
################################ Check Form #############################
Version    : v1.102
Last Update: 24/09/2003

NEW:
	+ Added support for selectboxes, checkboxes and radio buttons.
	+ Added error handling.

Input  ==> [this.form] (the form Object to be checked)
Output ==> Bool (true/false) + Alert/DialogBox

NOTE: You can override the original behaviour of alert by some other type
  if you supply [ ww_alert(alert_string) ] function.

The form element should have these properties :
{
  validate --> either 1 or 0 (check or not) (0 is default and can be omitted).
  regEx    --> the first regular expression or value to check
    (for selectbox/checkbox/radio)
  regEx2   --> the second regular expression (only for text/textarea fields)
    *note: the check is (regEx or regEx2) but not both.
    *note: both regEx and regEx2 can have either a regular expression or the
      PRESET number
  caption  --> the alert to be displayed if the value is invalid.
}

An example of a regular expression :
{
	(.*@{1}.*\\..*) --> checks the email to be valid.
	(.{5,7})        --> any text at least 5 chars long but less then 7.
	(^\\d+$)        --> just the numeric values, with at least one digit.
	
	*NOTE: You don't have to put regEx, if none is present the field is checked
		to have something.
	*NOTE: You can read all about Regular Expressions in the MSDN Library.
}

selectbox, checkbox, radio :
{
	These elements have can have regEx atribute:
		regEx="abc"  --> the value of the field should be [abc]
		regEx="!abc" --> the value of the field should NOT be [abc]
	(no real regular expressions are parsed here)
	
	in checkbox regEx="1" means it should be selected and regEx="0" means it should
    NOT be selected.
}
#########################################################################
*/


//Regular expressions PRESETs (note that [\] should be escaped)
var g_aRegEx = new Array();
g_aRegEx[0] = ".+";                         //Any Chars
g_aRegEx[1] = "(^(\\d+)(\\.{0,1})(\\d*$))"; //Integer or Float, but at least one digit
g_aRegEx[2] = "(.*@{1}.*\\..*)";            //Email

var g_aRegExLength = g_aRegEx.length;

function checkForms(oF)
{
	var oForm = oF;
	
	if(typeof(oForm) != "object" || oForm == null) //Exit if the form is undefined
		return false;
	
	var iElmLength = oForm.elements.length;
	var i, sGlobalAlertString = "";
	
	for(i = 0; i < iElmLength; i++)
	{
		if(eval(oForm.elements[i].validate))
			sGlobalAlertString += CheckElement(oForm.elements[i], oForm);
	}
	
	if(sGlobalAlertString.length)
	{
		(typeof(ww_alert) == "function")? ww_alert(sGlobalAlertString): alert(sGlobalAlertString);
		return false;
	}
	
	return true;
}


function CheckElement(oE, oF)
{
	var oElm = oE, oRegEx = null, oForm = oF;
	var bResult = true, bResult2 = false;
	
	var sCaption = "";
	var sRegEx = "", sElmType = oElm.type, sV = "";
	var sReg = oElm.regEx, sReg2 = oElm.regEx2;
	
	var oRadio = null;
	var i = 0, iL = 0;
	
	if(typeof(oForm) != "object" || oForm == null)
		return "Undefined Form\t\n";
	
	if(typeof(oElm) != "object" || oElm == null)
		return "Undefined Element\t\n";
	
	if(sElmType == "text" || sElmType == "textarea") //for text and textarea
	{
		if(sReg != null)
		{
			sRegEx  = (isNaN(sReg))? sReg: (sReg <= g_aRegExLength)? g_aRegEx[sReg]: "xxLAME USERxx";
			oRegEx  = new RegExp(sRegEx, "gi");
			bResult = oRegEx.test(oElm.value);
			
			if(sReg2 != null)
			{
				sRegEx   = (isNaN(sReg2))? sReg2: (sReg2 <= g_aRegExLength)? g_aRegEx[sReg2]: "xxLAME USERxx";
				oRegEx   = new RegExp(sRegEx, "gi");
				bResult2 = oRegEx.test(oElm.value);
			}
			
			bResult ^= bResult2;
		}
		else
			bResult = (oElm.value.length > 0);
	}
	else if(sReg != null) //selectbox, radio
	{
		switch(sElmType)
		{
			case "radio":
				oRadio = eval("oForm." + oElm.name);
				iL     = oRadio.length;
				
				for(i = 0; i < iL; i++)
				{
					if(oRadio[i].checked)
						sV = oRadio[i].value;
				}
				
				if(sReg.charAt(0) == "!")
					if(("!" + sV) == sReg)
						bResult = false;
				else
					if(sV != sReg)
						bResult = false;
				
				break;
			
			case "select-one":
				sV = oElm.value;
			
				if(sReg.charAt(0) == "!")
				{
					if(("!" + sV) == sReg)
						bResult = false;
				}
				else
					if(sV != sReg)
						bResult = false;
				
				break;
			
			case "checkbox":
				sV = oElm.checked;
				
				if(sV != sReg)
					bResult = false;
				
				break;
		}
	}
	
	sCaption = (typeof(oElm.caption) == "undefined")? "ERROR in " + oElm.name: oElm.caption;
	
	if(!bResult)
		return sCaption + "\t\n";
	else
		return "";
}









//----------------- End Check form functions
