﻿/* FormValidator Object
 * It comes pretty bare-bones, but is very extendable. 
 * 
 * USAGE:
	// set it up.
	myform = document.getElementById('form');
	myform.validate = new RegExFormValidator(myform);
				
	// add rules.		
	myform.validate.addRule('clinicient_username',/^[\w]+\.[\w]+$/,'Clinicient Username','jdoe.demo')
				
	// add fields				
	myform.validate.addField('email','email',true);
	myform.validate.addField('clinid','clinicient_username',false);
	myform.validate.addField('date','date-iso8601');
	myform.validate.addField('phone','phone-us');
	myform.validate.addField('length','not-blank',true);
 * 
 * 
 */
function RegExFormValidator(FormReference) {
	this.FormRef = FormReference;
	this.fields = [];
	this.validators = {
		"email": {
			"regexp"	: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/,
			"name"		: "Email Address",
			"example"	: "yourname@example.com"
			},
		"phone-us": {
			"regexp"	: /^(([+])?1)?([(\-])?[2-9][0-9]{2}([)\-])?([ ])?[1-9][0-9]{2}([\-])?[0-9]{4}$/,
			"name"		: "US Phone Number",
			"example"	: "503-555-0158\n1(212)555-0182\n+17445550162"
			},
		"phone-us-ex": {
			"regexp"	: /^(([+])?1)?([(\-])?[2-9][0-9]{2}([)\-])?([ ])?[1-9][0-9]{2}([\-])?[0-9]{4}(([ ])?x([ ])?[0-9]{1,4})?$/,
			"name"		: "US Phone Number (with or without extension)",
			"example"	: "503-555-0158\n1(212)555-0182 x133\n+17445550162\n+13795550122x333"
			},
		"date-iso8601": {
			"regexp"	: /^(\d{4}((-)?(0[1-9]|1[0-2])((-)?(0[1-9]|[1-2][0-9]|3[0-1])(T(24:00(:00(\.[0]+)?)?|(([0-1][0-9]|2[0-3])(:)[0-5][0-9])((:)[0-5][0-9](\.[\d]+)?)?)((\+|-)(14:00|(0[0-9]|1[0-3])(:)[0-5][0-9])|Z))?)?)?)$/,
			"name"		: "Date (ISO-8601)",
			"example"	: "1997\n2008-06-23\n2008-04-14T13:15+08:00"
			},
		"num-int": {
			"regexp"	: /^[\d]+$/,
			"name"		: "Interger",
			"example"	: "42"
			},
		"num-float": {
			"regexp"	: /^([+\-]?((([0-9]+(\.)?)|([0-9]*\.[0-9]+))([eE][+\-]?[0-9]+)?))$/,
			"name"		: "Float",
			"example"	: "88.72\n6.023e-23"
			},
		"not-blank": {
			"regexp"	: /.+/,
			"name"		: "value",
			"example"	: "antyhing"
			}
		};
	this.FormRef.onsubmit = function() {return this.validate.go();};
}; 	RegExFormValidator.prototype.go = function(failMessage) {
		with (this)
		for(i in fields) {
			if(!fields[i].object.value.match(validators[fields[i].type].regexp)) {
				if(fields[i].object.value=='') {
					if(!fields[i].required) continue;
					alertTxt = 'A valid '+validators[fields[i].type].name+' is required!';
				} else if(!fields[i].required) {
					alertTxt = 'This field only accepts a valid '+validators[fields[i].type].name+'. This is not a required field (you may leve it blank).';
				} else {
					alertTxt = 'The '+validators[fields[i].type].name+' you have entered is invalid.';
				}
				alert(alertTxt+'\n\nEXAMPLE(S):\n'+validators[fields[i].type].example);
				fields[i].object.focus();
				return false;
			}
		}
		return true;		
	}; RegExFormValidator.prototype.addField = function (FieldName,type,required) {
		if(required!=true) required = false;
		FieldRef = this.FormRef[FieldName];
		this.fields[this.fields.length] = {
			"object"	: FieldRef,
			"type"		: type,
			"required"	: required
			};
	}; RegExFormValidator.prototype.addRule = function (type,regexp,name,example) {
		this.validators[type] = {
			"regexp"	: regexp,
			"name"		: name,
			"example"	: example
			};
	};