// @@@@ VALIDATE.JS V3.0
// this script requires the form has:
// 1. a unique id
// 2. onClick"(return validate(this.id))"
// and each field you want to validate has a class of 
// 1.  validate {item}
// @@

// global settings
var message = 'Please provide a valid '; //..... this provides default text for user display
var match_message = 'Your email addresses do not match.\n'; //
var err = 1; // turns change bg color on error off and on (0,1)
var errbg = '#faa'; // sets the background color for the input box when there is an error
var bg = ''; // sets the normal bg color of input tags
// functions

// ## ERROR STYLES
function doErrBg(id){
		input = document.getElementById(id);
		//
		input.style.background  = errbg;
		input.focus();
}

// ## DATA CHECKS
function hasValue(text,id){
	data = document.getElementById(id).value;
   if (data.length == 0){ if(err){doErrBg(id);} alert( message + text +"!"); return false; } else { return true; }
   }

function validEmail(id){
	data = document.getElementById(id).value;
	validRegExp = /^[^@]+@[^@]+\.[a-z]{2,}$/i;
   if (data.search(validRegExp) == -1) { if(err){doErrBg(id);} alert(message+'email address!'); return false; }
	}

function matchEmail(id){
	field1 = document.getElementById(id);
	field2 = document.getElementById('email');
	if(field1 != field2){ if(err){doErrBg(id);} alert(match_message + message + 'email address!'); return false; }
	}
 
function validPhone(id){
	data = document.getElementById(id).value;
	if(data.length == 0){ if(err){doErrBg(id);} alert("Please provide a contact number!"); return false; }
	if(data.length <= 6){ if(err){doErrBg(id);} alert(message + 'contact number!'); return false; }
	}

// ## SUB: TESTS
function hasValidate(data){
	if(data != "validate"){ return false; } else { return true; }
	}
// ## SUB: CHECKS
function checkType(type,myid){
	switch(type){
		case 'email':
			return validEmail(myid); break;
		case 'verifyemail':
			var result = validEmail(myid);
			if(result == true){ return matchEmail(myid); } else { return result; }  break;
		case 'firstname':
			return hasValue("first name", myid); break;
		case 'lastname':
			return hasValue("last name", myid); break;
		case 'phone':
			return validPhone(myid); break;
		case 'mobile':
			return validPhone(myid); break;
			
		default:
			return hasValue(type,myid); break;
   	}
	}

// ## GO-TIME: RUN!!!!!!!!
function validate(formid){
	//
	var check = true; 												// set the return to be true by default unless interupted
	var inputs=document.getElementById(formid).elements;	// get the form data from the form id

	if(err){																// go through and reset all background fields if enabled
		for(i=0; i < inputs.length; i++){
			var myid = inputs[i].id;
			if(myid){
				input = document.getElementById(myid);
				input.style.background=bg;		
			}
		}
	}
	for(i=0; i < inputs.length; i++){ 							// for each input found in this form
		//	
		var cl = inputs[i].className;  							//get classname variable
		var spl = cl.split(' '); 									// split by space
		var v = spl[0];
		if(hasValidate(v)){ 								// check if validation is required
			//
			var mytype = spl[1]; 									// get the validator switch
//			var myname = inputs[i].value;
			var myid = inputs[i].id; 								// get the inputs id
			if((check = checkType(mytype,myid)) == false){ break; }
		}
	}
	return check;
}
