var escapeCHR = "_"; /* your escape charactor (at possition 0) see manual below */
var form;
var submittable;
var focuselement;
var needed;

function checkFORM(fo){
	form = fo;
	submittable = true;
	focuselement = false;
	/* start of error message */
	needed = "";
	needed+= "______________________________________________________    \n\n"; 
	needed+= "The form could not be submited beause of the following error(s)\n";
	needed+= "______________________________________________________    \n\n        "; 
	var checker = checkELEMENTS(fo)
	if (checker){
		/* if checker is true all is well */
		/* if running from ' onsubmit="return checkFORM(this);"' then return true; */
 		/* if running from ' onclick="checkFORM(document.yourformname);"' or  */
 		/* ' href="javascript:checkFORM(document.yourformname);"' then..  */
		/* try fo.submit(); */
		/* or document.yourformname.submit(); */
		 return true; 	
	 } else {
	 	/* if there is a problem finnish the error message and focus on the frist error */
	 	needed+= "\n______________________________________________________\n\n"; 
		needed+= "Please correct the error(s) and resubmit the form\n";
		needed+= "______________________________________________________\n\n"; 
		alert(needed);
		if (focuselement){focuselement.focus();}
		/* return false; only needed if running from ' onsubmit="return checkFORM(this);"' */
		return false; 
	 }
}
/* end daddy function */



/* this will check all the form elements */
/* if you don't want to check them all */
/* use the checkELEMENT() function instead */
function checkELEMENTS(fo) {
	for (i = 0; i <  fo.elements.length; ++i){
			checkELEMENT(fo.elements[i])
			}
	return submittable;
}

function checkELEMENT(element){
			elementtype = element.type;
			if (element.name.indexOf(escapeCHR) != 0){
				if (elementtype == "text"){checkTEXT(element)}
				else if (elementtype == "textarea"){checkTEXT(element)}
				else if (elementtype == "file"){checkTEXT(element)}
				else if (elementtype == "password"){checkPASSWORD(element)}
				else if (elementtype == "select-one"){checkSELECT(element,0)}
				else if (elementtype != "select-one" && elementtype.indexOf('select') != -1){checkSELECT(element,-1)}
				else if (elementtype == "radio"){checkRADIO(element)}
				else if (elementtype == "checkbox"){checkCHECKBOX(element)}
			}
	return submittable;
}

function checkTEXT(element){
	elementname = elementNAME(element);
	if (element.value==""){
	needed+= elementname + " empty \n        ";
	submittable = false;
	if (!focuselement){focuselement = element;}
	}
	else {
		if (elementname.indexOf("EMAIL") != -1){
			if (!isEMAIL(element.value)){
			needed+= elementname + " invalid \n        ";
			submittable = false;
			if (!focuselement){focuselement = element;}
			}
		}
			
		else if (elementname.indexOf("NUMBER") != -1){
			if (!isNUMERIC(element)){
				needed+= elementname + " not numeric \n        ";
				submittable = false;
				if (!focuselement){focuselement = element;}
			}
			else if (elementname.indexOf("CARDNUMBER") != -1){
				elementvalue = element.value.replace(/ /gi,"");/* remove spaces */
				if (elementvalue.length != 16){ /* count digits */
					needed+= elementname + " should be 16 digits \n        ";
					submittable = false;
					if (!focuselement){focuselement = element;}
				}
			}
		}
	}
}

function checkPASSWORD(element){
	elementname = elementNAME(element);
	if (element.value==""){
		needed+= elementname + " empty \n        ";
		submittable = false;
		if (!focuselement){focuselement = element;} 
	}
var b = 0;
for (a = 0;a < form.length; a++){ if (form[a] == element){ b = a; break; }}
	if (form[(b-1)].type == "password" && (form[(b-1)].value != element.value)) {
		needed+= "PASSWORD missmatch \n        ";
		submittable = false;
		if (!focuselement){
			focuselement = element;
			element.value = "";
		}	
	}	
}

function checkSELECT(element,num){
var num = parseInt(num);
	if (element.selectedIndex==num){
	needed+=  elementNAME(element) + " not selected \n        ";
	submittable = false;
	if (!focuselement){focuselement = element;}
	}
}

function isNUMERIC(element) 
{
var TheNumber = element.value;
var valid = 1
var GoodChars = "0123456789 " /* allow spaces */
var i = 0
	for (i =0; i <= TheNumber.length -1; i++) 
	{
		if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) 
		{
		valid = false
		} 
	} // End for loop

return valid
}

function isEMAIL(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function checkRADIO(element) {
var b = 0;
for (a = 0;a < form.length; a++){ if (form[a] == element) b = a;}
	if (form[(b-1)].name != element.name) {
		var group = form[element.name];
		var checked = false;
		if (!group.length)
			checked = element.checked;
		else
		for (var r = 0; r < group.length; r++)
		if ((checked = group[r].checked))
			break;
		if (!checked) {
			needed+=  elementNAME(element) + " radio button not checked \n        ";
			submittable = false;	  
		}
	}
}   

function checkCHECKBOX(element){
var b = 0;
for (a = 0;a < form.length; a++){ if (form[a] == element){b = a; break;}}
	if (form[(b-1)].name != element.name) {
		var group = form[element.name];
		var checked = false;
		if (!group.length)
			checked = element.checked;
		else
		for (var r = 0; r < group.length; r++)
		if ((checked = group[r].checked))
			break;
		if (!checked) {
			needed+=  elementNAME(element) + " checkbox not checked \n        ";
			submittable = false;	  
		}
	}
}

function elementNAME(element){
	elementname = element.name.toUpperCase();
	elementname = elementname.replace(/_/gi, " ");
	return elementname;
}

/* checkFORM */
/* put a javascript link to this file on your form page */
/*  */
/*  */
/* checkFORM will ignor type="hidden" fields */
/* if you don't want an element to be checked then */
/* include your escape charactor e.g. '_' at possition 0 */
/* e.g. name="postcode" will be checked, */
/* name="_postcode" will be skipped */
/*  */
/*  */
/* to submit */
/*  */
/* submit the form use onsubmit="return checkFORM(this);" in the form tag */
/* or call the script from a button or link e.g. */
/* <input type="button" value="submit" onclick="checkFORM(document.yourformname);"> */
/* or */
/* <a href="javascript:checkFORM(document.yourformname);">your image</a> */
/* remember to return false if running from 'onsubmit' */
/*  */
/* error alert message */
/*  */
/* the error message is generated by the element names */
/* so if your required element name is name="first_name" */
/* the error message will look like this... */
/* 
		Please provide..
		 
		FIRST NAME missing, 
		LAST NAME missing, 

*/
/* so don't call your form elements things like name="username1" :) */
/*  */
/*  */
/* email */
/*  */
/* if the form contains email field(s) then */
/* include the word 'email' in the element name e.g. name="from_email" */
/* and it will be checked for correct email syntax. */
/*  */
/*  */
/* numbers */
/*  */
/* if there are any number fields, such as telephone or fax */
/* include the word 'number' in the element name e.g. name="fax_number" */
/* and it will be checked for a numeric value (will allow spaces). */
/*  */
/*  */
/* credit card numbers */
/*  */
/* if you have a credit card number include the word 'cardnumber' */
/* e.g. name="credit_cardnumber" and it will be checked for numeric value and */
/* for number of digits (should have 16) */
/* (it will remove spaces from the number first so your form field can */
/* contain spaces */
/* e.g. value="1234 1234 1234 1234" and value="1234123412341234" ) */
/* will both return 16 i.e. true (tip: maxlength="19" :) */
/*  */
/*  */
/* passwords */
/*  */
/* if you have a form element type="password" it will be checked for content */
/* if you have two password elements next to each other they will be checked */
/* against each other */
/* and an error message will be returned if they differ */
/* i.e. password and password confirm  */
/*  */
/*  */
/* customising checkFORM */
/*  */
/* checkFORM will loop though all the form elements */
/* if you need to customise, you can, by ... */
/* in checkFORM() */
/* replace the check elements funtion

  checkELEMENTS(fo)

*/
/* with somthing that will check each element separatly, like this...

  checkELEMENT(fo.thiselement);
  checkELEMENT(fo.thatelement);
  checkELEMENT(fo.email);
  yourCUSTOMCHECKER(fo.yourelement)
  checkELEMENT(fo.verify_password);

	if (submittable){	
	 fo.submit();  // submits the form
 }
 else {
		alert(needed); // displays error message
		focuselement.focus(); // sets focus() on the first failed element
 }

 */
/* then you can put your custom checking in wherever you need it */
/* the syntax to follow in your custom checking */
/* could look somthing like this... 

	function yourCUSTOMCHECKER(element){
1		if (check for somthing here){
2			needed+= elementNAME(element) + " not filled in \n   ";
3			submittable = false;
4			if (!focuselement){focuselement = element;}
		}
}

*/
/* breakdown */
/* line 1; your if statement e.g. if (element.value == "your name goes here")  */
/* line 2; 'needed' is the error message,  */
/* add the element name and the error to it */
/* line 3; marks the form as not submittable */
/* line 4; checks if this is the first failed element */
/* and if so, it gives the element focus() */
/*  */
/*  */
/* www.patrick.clancey.btinternet.co.uk */
/* hehe :) */
/* ! */
