var clValidator = {
	init: function (settings) {
		this.settings = settings;
		this.form = document.getElementById(this.settings["formId"]);
		formInputs = this.form.getElementsByTagName("input");
		formSelects = this.form.getElementsByTagName("option");
		// change color of inputs on focus
		for(i=0;i<formInputs.length;i++)
		{
			if(formInputs[i].getAttribute("type") != "submit") {
				input = formInputs[i];
				input.style.background = settings["inputColors"][0];
				input.onblur = function () {
					this.style.background = settings["inputColors"][0];
				}
				input.onfocus = function () {
					this.style.background = settings["inputColors"][1];
				}
			}
		};
		for(i=0;i<formSelects.length;i++)
		{
			if(formSelects[i].getAttribute("option") != "submit") {
				option = formSelects[i];
				option.style.background = settings["selectColors"][1];
				option.onblur = function () {
					this.style.background = settings["selectColors"][0];
				}
				option.onfocus = function () {
					this.style.background = settings["selectColors"][1];
				}
			}
		};
		this.form.onsubmit = function () {
			error = clValidator.validate();
			if(error.length < 1) {
				return true;
			} else {
				clValidator.printError(error);
				return false;
			}
		};
	},
	validate: function () {
		error = '';
		validationTypes = new Array("isSample", "isTitle", "isLastName", "isFirstName", "isBusiness", "isAddress", "isEmail", "isPassword", "isPassCopy", "isCity", "isCountry", "isCounty", "isPostCode", "isBusinessNumber");
		for(n=0; n<validationTypes.length; n++) {
			var x = this.settings[validationTypes[n]];
			if(x != null) {
				for(i=0; i<x.length; i++) 
				{
					inputField = document.getElementById(x[i]);
					selectField = document.getElementById(x[i]);
					if(inputField)
					{
						switch (validationTypes[n]) {
							case "isSample" :
							valid = !isTitle(inputField.value);
							errorMsg = "Please select at least one Sample";
							break;
							case "isTitle" :
							valid = !isTitle(inputField.value);
							errorMsg = "Please enter your Title";
							break;
							case "isLastName" :
							valid = !isLastName(inputField.value);
							errorMsg = "Please enter a Last Name.";
							break;
							case "isFirstName" :
							valid = !isFirstName(inputField.value);
							errorMsg = "Please enter a First Name.";
							break;
							case "isBusiness" :
							valid = !isBusiness(selectField.value);
							errorMsg = "Please select your Business Type";
							break;
							case "isEmail" :
							valid = isEmail(inputField.value);
							errorMsg = "Please enter valid Email Address.";
							break;
							case "isPassword" :
							valid = !isPassword(inputField.value);
							errorMsg = "Please enter Password.";
							break;
							case "isPassCopy" :
							valid = !isPassCopy(inputField.value);
							if (valid)
							{
								valid = (inputField.value == document.getElementById("samplepassword").value);
							}
							errorMsg = "Please check your passwords match.";
							break;
							case "isAddress" :
							valid = !isAddress(inputField.value);
							errorMsg = "Please enter your Address.";
							break;
							case "isCity" :
							valid = !isCity(inputField.value);
							errorMsg = "Please enter City/Town.";
							break;
							case "isCountry" :
							valid = !isCountry(inputField.value);
							errorMsg = "Please enter Country.";
							break;
							case "isCounty" :
							valid = !isCounty(inputField.value);
							errorMsg = "Please enter County.";
							break;
							case "isPostcode" :
							valid = !isPostcode(inputField.value);
							errorMsg = "Please enter Postcode.";
							break;
							case "isBusinessNumber" :
							valid = isBusinessNumber(inputField.value);
							errorMsg = "Please enter valid Phone Number.";
							break;
						}
						if(!valid) {
							error += " "+errorMsg+"\n";
							inputField.style.background = this.settings["errorColors"][0];
							selectField.style.color = this.settings["selectErrorColors"][0];
						} else {
							inputField.style.background = this.settings["inputColors"][0];
							inputField.style.background = this.settings["correctColors"][0];
							selectField.style.background = this.settings["selectColors"][0];
							selectField.style.background = this.settings["correctColors"][0];
						}
					}
				}
			}
		}
		return error;
	},
	printError: function (error) {
		alert(error);
	}
};

// returns true if the string is not empty
function isSample(str){
	return (str == null) || (str.length == 0) || (str == "null");
}
function isTitle(str){
	return (str == null) || (str.length == 0) || (str == "Title");
}
function isLastName(str){
	return (str == null) || (str.length == 0) || (str == "Surname");
}
function isFirstName(str){
	return (str == null) || (str.length == 0 ) || (str == "First Name");
}
function isBusiness(str){
	return (str == null) || (str.length == 0) || (str == "Business Type");
}
function isPassword(str){
	return (str == null) || (str.length == 0) || (str == "Password");
}
function isPassCopy(str){
	return (str == null) || (str.length == 0) || (str == "Confirm Password");
}
function isAddress(str){
	return (str == null) || (str.length == 0) || (str == "Address 1");
}
function isCity(str){
	return (str == null) || (str.length == 0) || (str == "Town/City");
}
function isCountry(str){
	return (str == null) || (str.length == 0) || (str == "Country");
}
function isCounty(str){
	return (str == null) || (str.length == 0) || (str == "County");
}
function isPostCode(str){
	return (str == null) || (str.length == 0) || (str == "Postcode");
}
// returns true if the string is a valid email
function isEmail(str){
	if(isLastName(str)) return false;
	var re = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/;
	return re.test(str);
}
// returns true if the string only contains characters 0-9 and is not null
function isBusinessNumber(str){
	if(isLastName(str)) return false;
	var re = /(\+)?([-\._\(\) ]?[\d]{3,20}[-\._\(\) ]?){2,10}$/;
	return re.test(str);
//	if (re.test(str)) return false;
//	return true;
}