/* 		
		requiredFields.js - Notes (v1)

		1. Your submit button needs to be type="button" and not type="submit".
		2. Set an element as "required" by giving it a class name of "required".
		3. The script lists out the fields based on the following triggers (and in this order):
			<input class="required[First name]" ...
			<input alt="First name" ...
			<input name="firstName"
			If you don't supply a plain-text indentifyer to the class value, or alt value, the fields name is used
		4. Checkboxes such as "I agree to terms" are the only ones you should set as required generally
		5. To call the script you set the onclick event for the submit button.
		6. Radio buttons are required to have IDs in order to be processed.
		
		Example use:
		
		<!-- css for .missingFieldClass -->		
		<form>
		<input type="text" name="firstName" class="required[First name]">
		<input type="button" onclick="requiredFields(this,'missingFieldClass')">
		<!--				don't change this value --^            ^--- CSS class to apply when a field has been skipped
																		This is optional, if you leave it out the script 
																		will throw an alert box, listing out the missing 
																		fields. You can change the message of that alert
																		box by editing the first line after this comment
		-->
		</form>
*/
warningText = "The following fields are missing or invalid:";

function requiredFields(which,missingClass) {

	f = getFormsIndex(which);		
	err=Array(); e=-1;
	
	inputs = document.forms[f].elements;
	for(i=0;i<inputs.length;i++) {
		// check to see if its required
		if(inputs[i].className.indexOf("required")>-1) {
			inputs[i].className = inputs[i].className.replace(missingClass,"");
			if(inputs[i].type=="text" && inputs[i].value<="") { e++; err[e] = getFieldsName(inputs[i]); setClass(inputs[i],missingClass); }
			if(inputs[i].type=="password" && inputs[i].value<="") { e++; err[e] = getFieldsName(inputs[i]); setClass(inputs[i],missingClass); }			
			if(inputs[i].type=="textarea" && inputs[i].value<="") { e++; err[e] = getFieldsName(inputs[i]); setClass(inputs[i],missingClass); }				
			if(inputs[i].type=="checkbox" && inputs[i].checked<="") { e++; err[e] = getFieldsName(inputs[i]); setClass(inputs[i],missingClass);}
			if(inputs[i].type=="file" && inputs[i].value<="") { e++; err[e] = getFieldsName(inputs[i]); setClass(inputs[i],missingClass);}				
			if((inputs[i].type.indexOf("select")>-1) && inputs[i].selectedIndex<0) { e++; err[e] = getFieldsName(inputs[i]); setClass(inputs[i],missingClass);}			
			
			if(inputs[i].type=="radio") {
				elem = eval("document.forms[f]." + inputs[i].id);
				set=false;
				for(r=0;r<elem.length;r++) {
					if(elem[r].checked==true) {
						set = true;
					}
				}
				if(set==false) {		
					for(ai=0;ai<err.length;ai++) {
						if (err[ai] == getFieldsName(inputs[i])) {
							err.splice(ai, 1);
							e--;
						} 
					}
					e++;
					err[e] = getFieldsName(inputs[i]);
					setClass(inputs[i],missingClass);
				}
			}
			
			// Validate email address
			
			if(inputs[i].name.toLowerCase().indexOf("email")>-1) {
				if(inputs[i].value<="" || inputs[i].value.indexOf("@")<0 || inputs[i].value.indexOf(".")<0) {
					for(ai=0;ai<err.length;ai++) {
						if (err[ai] == getFieldsName(inputs[i])) {
							err.splice(ai, 1);
							e--;
						} 
					}
					e++;
					err[e]=getFieldsName(inputs[i]);
					setClass(inputs[i],missingClass);							
				}
			}
		}
	}

	if(err.length>0) {
		if(!missingClass) {
			errString = err.join("\n",err);
			alert(warningText + "\n\n" + errString);
		}
	} else {
		document.forms[f].submit();
	}
}
function getFieldsName(f) {
	if(f.className.indexOf("required[")>-1) {
		name = f.className.split("required[")[1];
		name = name.split("]")[0];
		
	} else {
		if(f.alt>"") {
			name = f.alt;
		} else {
			name = f.name;
		}
	}
	return name;
}
function setClass(which,missingClass) {
	which.className = which.className + " " + missingClass;
}
function getFormsIndex(element) {
	forms = document.forms;
	for(f=0;f<forms.length;f++) {
		if(element.type=="button") {
			elements = document.forms[f].elements;
			for(e=0;e<elements.length;e++) {
				if(elements[e]==element) {
					return f;
				}
			}
		} else {
			if(document.forms[f].innerHTML.indexOf(element.innerHTML)>-1) {
				return f;				
			}
		}
	}
}