erroricon = new Image();
erroricon.src = "formvalidate/iqsystem-arrowleftanim.gif";
blankicon = new Image();
blankicon.src = "formvalidate/iqsystem-pad.gif";

function fValidate()
{
	errorcount=0;
	errortext="____________________________\nThe following errors were detected:\n\n";
	form=window.document.forms[0];

	for ( x = 0; x < form.elements.length; x++)
	{
		// validatename=document.forms[0].elements[x].name+"validate";
		tmp=x+1;

		if ( tmp != form.elements.length ) { // Don't go beyond bounds of the elements[] array
		
			validateField=document.forms[0].elements[x].name+"validate";
			// tmpb=document.forms[0].elements[tmp].name;
			// if ( tmpb.toUpperCase() == validatename.toUpperCase() )

			if ( document.forms[0].elements[tmp].name == validateField ) {
				// fieldName="document.forms[0].elements["+x+"].name" + "error";
				fieldName=document.forms[0].elements[x].name;
				fieldName="document.forms[0]."+fieldName+"error";
				eval(fieldName).src=blankicon.src;
				checkField(tmp);
			}
		}
	}
	
	if ( errorcount == 0 ) return true; else { errortext += "\n________________________________        \nPlease correct the form and submit again\n"; alert(errortext); return false };
}

function checkField(pos)
{
	tmp=document.forms[0].elements[pos].value;
	els=tmp.split(",");
	numels=els.length;
	
	nam=document.forms[0].elements[pos-1].name;
	val=document.forms[0].elements[pos-1].value;
	typ=document.forms[0].elements[pos-1].type;

	for( y = 0; y < numels; y++ )
	{
		eqloc=0;
		eqloc=els[y].indexOf("=");
		
		if ( eqloc > 0 )
		{
			buf=els[y];
			eqbreak=buf.split("=");
			evalnam = eqbreak[0];
			evalval = eqbreak[1];

			if ( val == "" || val == " " )
			{
				errorcount++;
				outcheck = nam + " required  \n";
				rept=errortext.indexOf(outcheck);
				if ( rept < 0 ) errortext += "-" + nam + " required  \n";
				eval("document."+nam+"error").src=erroricon.src;
			}
			else if ( evalnam == "minlength" )
			{
				if ( val.length < evalval ) 
				{
					errorcount++;
					errortext += "-" + nam + " can't be less than " + evalval + " chars \n";
					eval("document."+nam+"error").src=erroricon.src;
				}
			}
			else if ( evalnam == "maxlength" )
			{
				if ( val.length > evalval ) 
				{
					errorcount++;
					errortext += "-" + nam + " can't be more than " + evalval + " chars \n";
					eval("document."+nam+"error").src=erroricon.src;
				}
			}
			else if ( evalnam == "reqdlength" )
			{
				if ( val.length != evalval ) 
				{
					errorcount++;
					errortext += "-" + nam + " must be " + evalval + " chars \n";
					eval("document."+nam+"error").src=erroricon.src;
				}
			}			
			else if ( evalnam == "mustnotbeval" )
			{
				if ( val == evalval )
				{
					errorcount++;
					errortext += "-" + nam + " can't be " + evalval + "\n";
					eval("document."+nam+"error").src=erroricon.src;
				}
				
				if ( typ == "select-one" && document.forms[0].elements[pos-1].selectedIndex == 0 )
				{
					errorcount++;
					errortext += "-" + nam + " can't be " + evalval + "\n";
					eval("document."+nam+"error").src=erroricon.src;
				}
			}
			else if ( evalnam == "mustnotcontain" )
			{
				inpbuf=val.toUpperCase();
				evbuf=evalval.toUpperCase();
				
				if ( inpbuf.indexOf(evbuf) != -1 ) 
				{
					errorcount++;
					errortext += "-" + nam + " can't contain " + evalval + "\n";
					eval("document."+nam+"error").src=erroricon.src;
				}
			}
		}
		else
		{
			if ( els[y] == "noempty" )
			{
					if (val == "" || val == " ")
					{
						errorcount++;
						outcheck = nam + " required  \n";
						rept=errortext.indexOf(outcheck);
						if ( rept < 0 ) errortext += "-" + nam + " required  \n";
						eval("document."+nam+"error").src=erroricon.src;
					}
			} 
			else if ( els[y] == "emailfield" )
			{
				rx = /^[a-z0-9]{1,64}@[a-z0-9]{2,256}\.[a-z]{2,4}$/i;
				
				if(!rx.test(val)) {
					errorcount++;
					errortext += "-" + nam + " must be a valid e-mail address\n";
					eval("document."+nam+"error").src=erroricon.src;
					
				}
			}			
		}
	}
}