// JavaScript Document
//written by Mark Tank ©2009 All rights reserved

var checkForm = Class.create({
	initialize: function(errorcorlor,goodcolor) {
		this.error = "alert";
		this.errors = null;
		this.email = "";
		this.text = "";
		this.phone = "";
		this.errorcorlor = errorcorlor;
		this.goodcolor = goodcolor;
	},
	checkText: function(minimum,maximum) {
		//checking to see if mini and max was set
		if (typeof minimum == 'undefined' ) minimum = false;
		if (typeof maximum == 'undefined' ) maximum = false;
		//check to see if any input id was passed
		if(this.checkInput(this.text)) {
			//spliting the input names apart
			var allInputs = this.text.split(",");
			//looping through the inputs
			for(i=0; i < allInputs.length; i++) {
				var theInput = $(allInputs[i]);
				if(minimum == true) {
					if(theInput.value == "") {
						this.errors +="- Please enter "+theInput.title+"\n";
						theInput.style.backgroundColor = this.errorcorlor;
					}
					else {
						theInput.style.backgroundColor = this.goodcolor;
					}
				}
				if(typeof minimum == 'number') {
					if(theInput.value.length < minimum) {
						this.errors += "- "+theInput.title+" must be longer than "+minimum+" chracters!\n";
					}
				}
				if(typeof maximum == 'number') {
					if(theInput.value.length > minimum) {
						this.errors += "- "+theInput.title+" must be shorter than "+maximum+" chracters!\n";
					}	
				}
			}
			if(this.errors == null) {
				return true;
			}
			else {
				this.displayError();
				return false;
			}
		}
	},
	checkEmail: function() {
		var allInputs = this.email.split(",");
		//looping through the inputs
		for(i=0; i < allInputs.length; i++) {
			if(!$(allInputs[i]).value.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/))
			{
				this.errors += "- "+$(allInputs[i]).title + " does not appear to be a correct email address\n";
				$(allInputs[i]).style.backgroundColor = this.errorcorlor;
			}
			else {
				$(allInputs[i]).style.backgroundColor = this.goodcolor;
			}
		}
		if(this.errors == null) {
			return true;
		}
		else {
			this.displayError();
			return false;
		}	
	},
	checkPhone: function() {
		if(this.checkInput(this.phone)) {	
			var allInputs = this.phone.split(",");
			//looping through the inputs
			for(i=0; i < allInputs.length; i++) {
				if($(allInputs[i]).value != "") {
					var pattern = new RegExp("[0-9]","g");
					var rawPhone = $(allInputs[i]).value.match(pattern);
					rawPhone = (rawPhone == null) ? rawPhone = 0: rawPhone ;
				}
				else {
					var rawPhone = 0;
				}
				if(rawPhone.length != 7 && rawPhone.length != 10)
				{
					this.errors += "- "+$(allInputs[i]).title + " does not appear to be a correct phone number\n";
					$(allInputs[i]).style.backgroundColor = this.errorcorlor;
				}
				else {
					$(allInputs[i]).style.backgroundColor = this.goodcolor;
				}
			}
			if(this.errors == null) {
				return true;
			}
			else {
				this.displayError();
				return false;
			}	
		}
	},
	checkInput: function(passed) {
		try {
			if(passed.length < 1) {
				throw "The checkForm.text function requires you pass form item IDs."	
			}
			else {
				var allInputs = passed.split(",");
				for(i=0; i< allInputs.length; i++) {
					if(!$(allInputs[i])) {
			   			throw 'No form input with id "' + allInputs[i] + '" was found.';
					}
				}
			}
		}
		catch(e) {
			throw(e);
			return false;
		}
		return true;
	},
	displayError: function() {
		if(this.errors != null) {
			this.errors = this.errors.replace("null","");
			if(this.error == "alert") {
				window.alert(this.errors);	
			}
			else {
				this.errors = this.errors.replace(/\r/g,"</li>\r");
				this.errors = this.errors.replace(/- /g,"<li>");
				$(this.error).update("<ul>"+this.errors+"</ul>");
			}
		}
		else if(this.error != "alert") { $(this.error).update(""); }
	}
});

