
$(function() {
	$("#theform input,textarea").blur(function(){
		if(jQuery.trim($(this).val()) == '')
			$(this).css("border","2px solid red");
		else
			$(this).css("border","");
	});
	$("#submitform").click(function(){
		var i=0;
		$("#error").fadeOut('slow');
		$("#theform input,textarea").each(function(){
			if(jQuery.trim($(this).val()) == ""){
				$(this).css("border","2px solid red");
				$("#error").fadeIn('slow');
				i++;
			}else{
				if($(this).attr('name') == 'email'){
					var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
					if(reg.test($(this).val()) == false) {
						$(this).css("border","2px solid red");
						$("#error").fadeIn('slow');
						i++;
					}else{
						$(this).css("border","");
					}
				}else {
					$(this).css("border","");
				}
				/*if($(this).attr('name') == 'phone'){
					var reg = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
					if(reg.test($(this).val()) == false) {
						$(this).css("border","2px solid red");
						$("#error").fadeIn('slow');
						i++;
					}else{
						$(this).css("border","");
					}
				}else{
					$(this).css("border","");
				}}*/
			}
		});
		if(i>0) return false;
		else $(this).val("Submitting...");
		$.ajax({
		   type: "post",
		   url: "mail.php",
		   data: $("#theform").serialize(),
		   success: function(msg){
			   $("#theform input,textarea").each(function(){ $(this).val(''); });
		       $("#submitform").val("Submitted");
		       setTimeout('$("#submitform").val("Submit");',3000);
		   }
		 });
	});
});
/*
$(document).ready(function(){
	$("#theform input,textbox").focus(function(){
		$(this).val('');
		$(this).css('border','initial');
	});
	$("#theform input,textbox").blur(function(){
		if(jQuery.trim($(this).val()) == '')
			$(this).val($(this).attr("title"));
	});
	$("#submitform").click(function(){
		$("#theform input,textbox").each(function(){
			if($(this).val() == $(this).attr("title"))
				$(this).css('border','1px yellow solid');
		});
	});
	/* Place ID's of all required fields here.
	required = ["name", "email", "message","address","phone"];
	var defaultText = new Array();
	for (i=0;i<required.length;i++) {
		var input = $('#'+required[i]);
		defaultText.push(input.val());
	}
	// If using an ID other than #email or #error then replace it here
	email = $("#email");
	errornotice = $("#error");
	// The text to show up within a field when it is incorrect
	emptyerror = "Please fill out this field.";
	emailerror = "Please enter valid e-mail.";

	$("#theform").submit(function(){	
		//Validate required fields
		for (i=0;i<required.length;i++) {
			var input = $('#'+required[i]);
			if ((input.val() == "") || (input.val() == emptyerror)||(input.val() == defaultText[i])) {
				
				input.addClass("needsfilled");
				input.val(emptyerror);
				errornotice.fadeIn(750);
			} else {
				input.removeClass("needsfilled");
			}
		}
		// Validate the e-mail.
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("needsfilled");
			email.val(emailerror);
		}

		//if any inputs on the page have the class 'needsfilled' the form will not submit
		if ($(":input").hasClass("needsfilled")) {
			return false;
		} else {
			errornotice.hide();
			return true;
		}
	});
	
	// Clears any fields in the form when the user clicks on them
	$(":input").focus(function(){		
	   if ($(this).hasClass("needsfilled") ) {
			$(this).val("");
			$(this).removeClass("needsfilled");
	   }
	});
});	*/
