function Form(form) {
	this.form = null; //form id
	this.action = null;
	
	this.init = function(form) {
		this.form = "#" + form;
		this.action = $(this.form).attr("action");

		var magicForm = this;

		$(this.form).bind("submit", function() {
			return magicForm.submit()
		});	


		$(this.form+" textarea").each(function() {
			if (this.value = " ") {
				this.value = "";
			}
		});
	}

	this.submit = function() {
		var errorString = "";

		$(this.form+" .required").each(function() {
			if (this.value == "" || this.value == $("label[for='"+this.id+"']").text() ) {
				errorString += " - " + $("label[for='"+this.id+"']").text() + "\n";
				$("label[for='"+this.id+"']").addClass("error");
			}
			else {
				$("label[for='"+this.id+"']").removeClass("error");
			}
		});


		if (errorString != "") {
			alert("Please ensure the following fields are filled in:\n\n" + errorString);
			return false;
		}		

		var request = "";

		$(this.form+" input").each(function() {
			request += $(this).attr("name") + "=" + this.value + "&";
		});

		$(this.form+" textarea").each(function() {
			request += $(this).attr("name") + "=" + this.value + "&";
		});

		$(this.form+" select").each(function() {
			request += $(this).attr("name") + "=" + this.options[this.selectedIndex].value + "&";
		});

		var magicForm = this;
		
		$.ajax({
			url: this.action + "?" + request,
			success: function(data) {
				magicForm.response(data);
			}
		});

		return false;
	}

	this.response = function(responseText) {
		if (responseText == "OK") {
			$(this.form).slideUp(function() {
				$(this).after("<h1>Thank you</h1><p>Thank you for taking the time to fill out our contact form. If required we will contact you as soon as possible</p>");
			});
			return;
		}
		
		var errors = eval(responseText);
		var message = "I'm sorry, the folloing error occured: \n\n - " + error;

		alert(message);
	}

	this.init(form);
}

