var ERRORCOLOR="#9ACCCD"; 
var SENTCOLOR="lightblue";

$(function() {

			$('#captcha_form').ajaxForm({						
					target: 		'#sentError',
					beforeSubmit: 	validateForm,  
					success:  checkSent
			});	
			
			hideErrors();
			
			 $("input").click(function () { 
      $(this).css("background-color","white");
	  });
	  
	  

	  
	 			 $("textarea").click(function () { 
      $(this).css("background-color","white");
    });
	
			  $("#changeCaptcha").click(function () { 
 
          $('#captcha').attr('src','/contactForm/captcha.php?'+Math.random());
return false;
     });


		});
		
		function checkSent()
		{
		var retVal =$("#sentError").text();	
		
		 if (retVal.length<=1) 
		 { 
			$("#userpass").animate( {backgroundColor:ERRORCOLOR}, 1000);
			errInForm("Incorrect image text");	
		 }
		 
		 if (retVal.length>1)
		 {
			$("#sentError").empty().append("<i>MESSAGE SENT</i><br/>Thank you for mailing us!<br/>  A representative will be in touch with you shortly.");;
			$("#sentError").css("background-color","#336799");
			$("#sentError").slideDown("slow");
			$("#sentError").animate( {backgroundColor:SENTCOLOR}, 400).animate( {backgroundColor:'#336799'}, 1000);;
			clearForm();
		 }
		
		}
		
		function errInForm(sss){
		 $("#sentError").empty();
		 $("#sentError").css("background-color","#336799");
		 $("#sentError").append(sss).show();
		 $("#sentError").animate( {backgroundColor:ERRORCOLOR}, 600);
		}
		
		function validateForm(){
		
			hideErrors();
			
			var fname = $("#fname").val();
			var msg = $("#message").val();
            var ema = $("#email").val();
			var emaX = isValidEmail(ema);
			
			if (fname == null || fname == '') {
				errInForm("Your name is required");
				$("#fname").animate( {backgroundColor:ERRORCOLOR}, 1000);
				return false;
			}


			
			if (ema == null || ema == '') {
				errInForm("Email is required");
				$("#email").animate( {backgroundColor:ERRORCOLOR}, 1000);
				return false;
			}
			
			if (emaX == false) {
				errInForm("Invalid email address");
				$("#email").animate( {backgroundColor:ERRORCOLOR}, 1000);
				return false;;
			}

			if (msg == null || msg == '') {
				errInForm("Messsage is required");
				$("#message").animate( {backgroundColor:ERRORCOLOR}, 1000);
				return false;
			}
			
			return true;	
		   
		}		
		
		function hideErrors()  
		{
		 //hide error containers
		 $("#sentError").empty().hide();

         //return background colors
         $("#fname").css("background-color","white");
         $("#lname").css("background-color","white");
         $("#email").css("background-color","white");
		 $("#message").css("background-color","white");
		 $("#userpass").css("background-color","white");
		}
		
		function clearForm()
		{
			$("#fname").val("");

			$("#email").val("");
						$("#phone").val("");
									$("#city").val("");
												$("#state").val("");
		    $("#message").val("");
			$("#userpass").val("");
			
		}
		

		function isValidEmail(strEmail){
           validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
           strEmail = document.forms[0].email.value;
           if (strEmail.search(validRegExp) == -1) 
           {return false;} 
           return true; 
       }



