var Library = new Object();
Library.date = /^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])\D([1-9]|0[1-9]|1[0-2])\D\d{2}$/;
	// matches 12/12/2000 or 12\12\2000 or 12:12:2000
Library.time = /^([0-9]|0[0-9]|1[0-9]|2[0-4])\D(0[0-5]|[0-5][0-9])$/;
	// matches 5:04 or 12:34 or 17:59 but not 75:83 - 24 hour time
Library.currency = /^\$\d{1,3}(,\d{3})*\.\d{2}$/;
	// matches $17.23 or $14,281,545.45 
Library.postcode = /^\d{4}$/;
	// matches 4 digit post codes
Library.phonenumber = /^(\d{10})|(\d{8})|(\d{4}\s?\d{4})|(\d{4}\s?\d{6})$/;
	// matches 10 digit phone number
Library.isBlank = /^\s?$/;
	// null value or whitespace

var ErrorMsg = new Object();
ErrorMsg.date = "You must enter a valid date - dd/mm/yy";
ErrorMsg.time = "You must enter a valid time - hh:mm";
ErrorMsg.currency = "Currency must be entered as $DD.CC";
ErrorMsg.postcode = "You must enter a valid Post Code";
ErrorMsg.choice = "You must select at least one option";
ErrorMsg.select = "You must choose from this drop down menu";
ErrorMsg.text = "You must enter a value";
ErrorMsg.number = "You must enter a numeric value";

function isBlank(value){
	var LibCheck = Library.isBlank.exec(value); valueCheck = (LibCheck)? true:false;
	return valueCheck
}

function trimValue(value){
	var trimValue = (value.replace(/^\W+/,'')).replace(/\W+$/,'');
	return trimvalue
}

function isChecked(form,choiceName){
	var count = 0;
	var isCheck = false
	for(j=0; j < form.length; j++){
		if(form.elements[j].name == choiceName)	{
			if (!isCheck){
				isCheck = eval(form.elements[j].checked);
			}
		count++;
		}
	}
	return (isCheck+','+count)
}

function validate(form)	{
	var normalBGcolor = "#FFFFFF"										// declaring the normal background color
	var normalBGimage = ""												// declaring the normal background image
	var errorBGcolor = "#6699CC"										// declaring the error background color
	var errorBGimage = ""												// declaring the error background image
	var msg;															// declaring the message variable
	var errors = "";													// declaring the error variable
	var formOK = true; 													// formOK starts true
	{
	for(i = 0; i < form.length; i++) {								// loop through form elements
		var el = form.elements[i];              					// get form element for this instance of loop					
		var error = el.error;										// get element error, if any		
		var name = el.name;											// get element name

		var valTemp = el.validator; 								// get element validator, if any		
		if (!valTemp) continue;										// if no validator restart loop
		var val = valTemp.toLowerCase();							// if validator exists convert to lowercase
	
		switch(val) {												// start switch statement on validator
			case 'choice':
				if ((el.type == 'radio') || (el.type == 'checkbox')) {
					var choiceName = name;
					var isCheck = isChecked(form,choiceName);
					var arrCheck = isCheck.split(",")
					if (arrCheck[0] == "false")	{
						errors += (!error) ? (name+" - "+ ErrorMsg[val] + "\n") : (error + "\n")
						formOK = false;
						var count = arrCheck[1];
						i+=(count-1);
					}
				}	
			 continue;
			case 'text':	
				el.style.backgroundColor=normalBGcolor;
				el.style.backgroundImage=normalBGimage;
				blankValue = isBlank(el.value);
				if (blankValue)	{
					el.style.backgroundColor=errorBGcolor;
					el.style.backgroundImage=errorBGimage;
					errors += (!error) ? (name+" - "+ ErrorMsg[val] + "\n") : (error + "\n")
					formOK = false;
				}
			 continue;
			case 'select':
				el.style.backgroundColor=normalBGcolor;
			 	el.style.backgroundImage=normalBGimage;
			 	blankValue = isBlank(el.options.value);
			 	if ((el.options[0].selected) || (blankValue)) {
					el.style.backgroundColor=errorBGcolor;
					el.style.backgroundImage=errorBGimage;
					errors += (!error) ? (name+" - "+ ErrorMsg[val] + "\n") : (error + "\n")
					formOK = false;
				}
			 continue;
			case 'number':
			 	el.style.backgroundColor=normalBGcolor;
			 	el.style.backgroundImage=normalBGimage;
			  	blankValue = isBlank(el.value);
			 	if ((isNaN(el.value))||(blankValue)) {
					el.style.backgroundColor=errorBGcolor;
					el.style.backgroundImage=errorBGimage;
					errors += (!error) ? (name+" - "+ ErrorMsg[val] + "\n") : (error + "\n")
					formOK = false;
					}
    		 continue;
			default :
			 	el.style.backgroundColor=normalBGcolor;
			 	el.style.backgroundImage=normalBGimage;
			 	var libCheck = Library[val].exec(el.value); valueCheck = (libCheck) ? true:false;
			 	if (!valueCheck) {
					el.style.backgroundColor=errorBGcolor;
					el.style.backgroundImage=errorBGimage;
					errors += (!error) ? (name+" - "+ ErrorMsg[val] + "\n") : (error + "\n")
					formOK = false;
				}
  		 	 continue; 
			}
		}
		if (formOK) {
			return true;
		}else {
			msg = "The following error(s) occured.\n\n";
		}
		if (errors) {
			msg += errors;
			alert(msg);
			return false;
		}
	}
}

