/* 
Generic for validations script v.1.3
This script was written by Greg Stewart as part of research into creating a generic validation framework for JavaScript. Feel free to use it, modify it, improve it, but please let me know of those changes so that I may include them in future releases of the script. Oh and please leave this little segment in the script. Should you want to get hold of me you can reach me at gregs@armagossa.com 

Instructions (make these up as I go along):
To call the script include in your opening form tag this: onSubmit="return verify(this);"
In order for this script to work you have make sure that your form fields follow certain rules:
- For required fields, the field name should be prefixed by r_
- For optional fields, the field name should be prefixed by o_
- To validat an email address please make sure that your field name is prefixed by r_ and contains email (e.g. r_email)
- Checking for matching passwords: Here your form fields will have to be called r_password and r_verify_password
IE4 specific - Furthermore you will need to place in the row above each form field the following HTML code: <span id="elField0"></span>, where 0 is the first field element and you increment that number by 1 for each subsequent form field (i.e. <span class="alert" id="elField1"></span> for the second field, <span class="alert" id="elField2"></span> for the third, etc...)
- To check that numeric values fit between a certain range you need to add the follwoing lines of code to your opening form tag in the onSubmit statement: this.r_fieldname.numeric = true; this.r_fieldname.min = 0; this.r_fieldname.max = 100; return verify(this);
Fixes:
- Fixed the script so that it didn't insert blank lines into the page anymore after validating (well until the first error occurs)
What's coming?
- Select multiple ==> 22/6/2001 Implemented select multiple. 
- group check boxes
- NS dHTML
- checking for valida date formats ==> OK cheated here slightly and used someone else's script, but I just didn't see the point in re-inventing the wheel and since it slotted in so nicely. Found it at this URL http://webdeveloper.earthweb.com/pagedev/webjs/item/0,3602,12744_9061,00.html. Michael you initially wrote this script and I tried to get in touch, but there was no contact information, so I hop you don't mind me bundling it with my script.
- credit cards. maybe?
Any other suggestions? Just mail them to me... Or implement them and let me know of the changes...
*/

<!-- Hide Script from older browser 	
	var ie4 = (document.all)? true:false;
	var ns4 = (document.layers)? true:false;
	function isBlank(s){ // check if form field is blank (i.e. a space was entered or a return or a tab function
		for(var i = 0; 1<=s.length; i++) {
			var c = s.charAt(i);
			if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
		}
	return true;
	}
	function strip(str) { // strips out r_, o_ and replaces _ with a space from the field name function
		var ar = str.match(/([r_|o_])?([^# ]*)/);
		var rem = RegExp.$2.replace(/_/g, ' ');
		return (rem);
	}
	function isOptional(name) { // check if it is an optionla field function
		var ar = name.match(/([r_|o_])/);
        var indx = RegExp.$1;
        //indx = name.indexOf('r_');
		//alert(indx);
		return (indx);	
	}
	function isEmail(t) { // check if form field name is e-mail function
		var reg_email = new RegExp("email","i");
		if(reg_email.test(t)){
			return true;
		} 
	}
	function isItDate(s) { // check if form field name is date function
		var reg_date = new RegExp("date","i");
		if(reg_date.test(s)){
			return true;
		}
	}
	function verify(f) {
		var msg;
		var empty_fields = "";
		var errors = "";
		var errors_2 = "";
		var errors_3 = "";
		var j = 1
		for(var i = 0; i < f.length; i++) {
			var e = f.elements[i];
			//alert(e.type);
			if(((e.type == "text") || (e.type == "textarea")) && (isOptional(e.name)!= "o")) {
				// first check if the field is empty
				if ((e.value == null) || (e.value == "") || (e.value == " ") || isBlank(e.value)) {
					if(ie4){
						whichEl = eval("document.all.elField" + i);
			            whichEl.innerHTML = "Please complete the " + strip(e.name) + " field<br />";
						empty_fields += "1";
					}
					else {
						empty_fields += "\n		" + strip(e.name);
					}
				continue;
				}
				else { // field contains something
					if(ie4){ //if error was generated by a previous attempt blank it out
						if (empty_fields != "") {// This checks first to see if there were any errors, 
							whichEl = eval("document.all.elField" + i);
				            whichEl.innerHTML = "&nbsp;";
						}
					}
					// now check for valid e-mail address
					if (isEmail(e.name)) { // first check to see if it is an e-mail form entry
						var reg_email = /^[\-\.\_\&0-9a-zA-Z]+[@][\-\.\_\&0-9a-zA-Z]+[\.][\.\_0-9a-zA-Z]{1,}$/;
						if (reg_email.exec(e.value) == null ) {
							if(ie4){
								whichEl = eval("document.all.elField" + i);
					            whichEl.innerHTML = "Please enter a valid e-mail address<br />";
								empty_fields += "1";
							}
							else {
								empty_fields += "\n		" + strip(e.name);
							}
						} else {
							if(ie4){ //if error was generated by a previous attempt blank it out
									whichEl = eval("document.all.elField" + i);
				            		whichEl.innerHTML = "&nbsp;";
							}
						}
					}
					// now check to see if it's a valide date entry
					if (isItDate(e.name)) { // Let's see if it's a date field
						// this now goes off to run the function found in c_date.js and if the result is false...
						if ( ! isDateString(e.value)) { 
							if(ie4){
								whichEl = eval("document.all.elField" + i);
					            whichEl.innerHTML = "Please enter a valid date format (e.g.: dd/mm/yyyy).<br />";
								empty_fields += "1";
							}
							else {
								empty_fields += "\n		" + strip(e.name);
							}
						}
					}
				}
				//if (e.numeric != false || (e.min != null) || (e.max != null)) {
				//	alert(e.numeric);
				//	var v = parseFloat(e.value);
				//	if (isNaN(v)) { // if it is not a number
				//		if (ie4) { 
				//			errors += "The field " + strip(e.name) + " must be a number";
				//			whichEl = eval("document.all.elField" + i);
			    //       	whichEl.innerHTML = errors;
				//		} else {
				//			errors += "- The field " + strip(e.name) + " must be a number";
				//			errors += ".\n";
				//		}
				//	}
				//	else { // make sure it fits between the specified values
				//		if (((e.min != null) && (v < e.min)) ||	((e.max != null) && (v > e.max))) {
				//			if(ie4){	// Based on the result do the DHTML bit else the pop up
				//				if (e.min != null)
				//					errors += " that is greater than " + e.min;
				//				if (e.max != null && e.min != null)
				//					errors += " and less than " + e.max;
				//				else if (e.max != null)
				//					errors += " that is less than " + e.max;
				//				whichEl = eval("document.all.elField" + i);
			    //       		whichEl.innerHTML = errors;
				//			}
				//			else {
				//				if (e.min != null)
				//					errors += " that is greater than " + e.min;
				//				if (e.max != null && e.min != null)
				//					errors += " and less than " + e.max;
				//				else if (e.max != null)
				//					errors += " that is less than " + e.max;
				//				errors += ".\n";
				//				}
				//			}
				//		}
				//	}
				}
				else if ((e.type == "select-one") && (isOptional(e.name)!= "o")) {
					//alert (e.options[e.selectedIndex].value);
					if ((e.options[e.selectedIndex].value == null) || (e.options[e.selectedIndex].value == "") || (e.options[e.selectedIndex].value == 0)) {
						if(ie4){
							whichEl = eval("document.all.elField" + i);
				            whichEl.innerHTML = "You need to select a" + strip(e.name) + "<br />";
							empty_fields += "1";
						}
						else {
							empty_fields += "\n		" + strip(e.name);
						}
					continue;
					}
					else {
						if(ie4){
							if (empty_fields != "") {
								whichEl = eval("document.all.elField" + i);
				            	whichEl.innerHTML = "&nbsp;";
							}
						}
					}
				}
				else if ((e.type == "select-multiple") && (isOptional(e.name)!= "o")) {
					if ((e.options.value == null) || (e.options.value == "")) {
						if(ie4){
							whichEl = eval("document.all.elField" + i);
				            whichEl.innerHTML = "At least one of your" + strip(e.name) + " fields is empty";
							empty_fields += "1";
						}
						else {
							empty_fields += "\n		" + strip(e.name);
						}
					continue;
					}
					else {
						if(ie4){
							if (empty_fields != "") {
								whichEl = eval("document.all.elField" + i);
				            	whichEl.innerHTML = "&nbsp;";
							}
						}
					}
				}
				else if ((e.type == "password") && (isOptional(e.name)!= "o")) {
					if ((e.value == null) || (e.value == "") || isBlank(e.value)) {
						if(ie4){
							whichEl = eval("document.all.elField" + i);
			            	whichEl.innerHTML = "You need to complete " + strip(e.name) + "<br />";
							empty_fields += "1";
						}
						else {
							empty_fields += "\n		" + strip(e.name);
						}
					continue;
					}
					else {
						if (f.elements.r_password.value != f.elements.r_verify_password.value) {
							if (j<2) {
								errors_2 += "The field(s) for: " + strip(e.name);
								if(ie4) {
									whichEl = eval("document.all.elField" + i);
					            	whichEl.innerHTML = "&nbsp;";
								}
								j++;
							}
							else {	
								errors_2 += " and " + strip(e.name) + " do not match.<br />";
								if (ie4) {
									whichEl = eval("document.all.elField" + i);
					            	whichEl.innerHTML = errors_2;
								}
								continue;
							}
						}
						else {
							if(ie4){
								if (empty_fields != "") {
									whichEl = eval("document.all.elField" + i);
				            		whichEl.innerHTML = "&nbsp;";
								}
							}
						}
					}
				}
				else if ((e.type == "checkbox") && (isOptional(e.name)!= "o")) {
					if (e.checked == false) {
						if(ie4) {
							whichEl = eval("document.all.elField" + i);
			            	whichEl.innerHTML = "The field " + strip(e.name) + " needs to be checked";
						}
						else {
							errors_3 +=  "- The field" + strip(e.name) + " needs to checked";
						}
						continue;
					}
					else {
						if(ie4){
							if (empty_fields != "") {
								whichEl = eval("document.all.elField" + i);
			            		whichEl.innerHTML = "&nbsp;";
							}
						}
					}
				}
			}
			if (!empty_fields && !errors && !errors_2 && !errors_3) return true;
			else {
				msg = "_________________________________________________\n\n"
				msg += " The form: " + f.name + " was not submitted because of the following \n";
				msg += " error(s). Please correct the following error(s) and re-submit.\n";
				msg += "_________________________________________________\n\n"
				if (empty_fields) {
					msg += "- The following required field(s) are empty or incomplete:" + empty_fields + "\n";
					if (errors || errors_2 || errors_3) msg += "\n";
				}
				if(!ie4){
					msg += errors;
					msg += errors_2;
					msg += errors_3;
					alert(msg);
				}
				return false;
			}
		} 
//-->