// JavaScript Document
// Requires Prototype
Event.observe(window, "load", function() {
	getFormElements().each(function(i){
		addValidation(i);
	});
});

function addValidation(field){
	if(field.className.match("validate")){
		if(field.className.match("calender")){
			Event.observe(field, "change", validateThis);
		} else {
			Event.observe(field, "blur", validateThis);
		}
	}
}

function validateThis(){
	validate(this);	
}

function validate(field){
	var value = trim(field.value);
	var errors = "";
	if((value === undefined || trim(value) == "" || trim(value) == "NULL") && field.className.match("validate")) {
		errors += "This is a required field. ";
	} else {
		var type = field.className.replace("validate", "");
		if(type.match("simple_text")){
			if(/[^a-zA-Z0-9-_\s]/.test(value)) {
				errors += "Only letters, numbers, spaces, hypens and underscores are allowed. ";
			}
		}
		if(type.match("date")){
			var splitter;
			if(value.match("/")) {
				splitter = "/"; 
			} else {
				if(value.match(".")){
					splitter = ".";			   
				} else {
					errors += "Please seperate your dates with / or . symbols.";	
				}
			}
			if(errors == ""){
				var temp = value.split(splitter);
				var m = temp[0];
				var d = temp[1];
				var y = temp[3];
				if (m<1 || m>12) errors += "Invalid month. ";
				if (d<1 || d>31) errors += "Invalid day. ";
				if (m==4 || m==6 || m==9 || m==11)
					if (d==31) errors += "Invalid day. ";
				if (m==2) {
					var b=parseInt(y/4);
					if (isNaN(b)) errors += "Invalid Year. ";
					if (d>29) errors += "Invalid day. ";
					if (d==29 && ((y/4)!=parseInt(y/4))) errors += "Invalid day. ";
				}
				if(errors != "") errors += "<br />Please enter the date in mm/dd/yyyy format. ";
			}
		}
		if(type.match("integer")){
			if(parseInt(value)!=Number(value-0)) errors += "This must be an whole number. ";	
		}
		if(type.match("non-negative")){
			if(value < 0) errors += "This must be a non-negative number. ";
		}
		if(type.match("email")){
			if(!(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/.test(value)))
				errors += "This email address is not valid. ";
		}
		if(type.match("phone")){
			var temp = value.replace(/[^0-9]/g, "");
			if(temp.length != 10) errors += "This must be a 10 digit phone number. ";
			field.value = value.replace(/[^0-9()\s-.]/g, "")
		}
		if(type.match("image")){
			if(!(/(\.bmp|\.gif|\.jpg|\.jpeg)$/i.test(this.value))) errors += "This is not a valid image format. ";
		}
	}
	if(errors != "") {
		validationOutput(field, errors);
		return false;
	} else {
		validationOutput(field, errors);
		return true;
	}
}

function validationOutput(field, errors) {
	var error_field = field.getAttribute('name') + "_error";
	if(errors != "") {
		if(!$(error_field)) {
			var element = document.createElement("span");
			element.id = error_field;
			element.className = "error";
			Element.insert(field, {'after':element});
		}
		field.style.background = '#FFFFAA';
		$(error_field).innerHTML = errors;
		$(error_field).show();
	} else {
		if($(error_field)) {
			$(error_field).innerHTML = "";
			$(error_field).hide();
			field.style.background = 'white';
		}
	}
}

function validateForm(form){
	var valid = true;
	getFormsElements(form).each(function(i){
		if(!validate(i)) valid = false;
	});
	if(valid) {
		return true;
	} else {
		return false;	
	}
}

function getFormElements(){
	return $$("input, textarea, select");	
}

function getFormsElements(form){
	return form.select("input, textarea, select");	
}

function trim(str) {
	return str.replace(/^\s+|\s+$/g, '');	
}