// JavaScript Document
$(document).ready(function() { 
	$("input#mailto").select().focus();
});


$(function() {
  $('.error').hide();

  $("#submitcontact").click(function() {
									 
		// validate and process form
		// first hide any error messages
    //$('.error').hide();
	$("#captcha_error").hide();
	// email REGEX
	var validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	var stripHttpRegEx = /http:\/\//;
	
	// check is a vaild mailto email --//
	var mailto = $("input#mailto").val();
	if (mailto == "") {
      $("#captcha_error").html("<p>Invaild 'Mail to' field</p>");
	  $("#captcha_error").show();
      $("input#mailto").focus();
      return false;
    }else{
   	// search email text for regular exp matches
   			if (mailto.search(validRegExp) == -1) 
			{
			  $("#captcha_error").html("<p>Invaild 'Mail to' field</p>");
			  $("#captcha_error").show();
			  $("input#email").focus();
			  return false;
			} 
	}
	
	// check sender is valid strip http:// --//
	var sender = $("input#sender").val();
	sender = sender.replace(stripHttpRegEx, "");
	$("input#sender").val(sender);
		if (sender == "") {
      $("#captcha_error").html("<p>Sender error</p>");
	  $("#captcha_error").show();
      $("input#sender").focus();
      return false;
    }
	
	// check is a vaild from email --//
	var from = $("input#from").val();
	if (from == "") {
		$("#captcha_error").html("<p>Invaild 'Your Email' field</p>");
      $("#captcha_error").show();
      $("input#from").focus();
      return false;
    }else{
   	// search email text for regular exp matches
   			if (from.search(validRegExp) == -1) 
			{
			  $("#captcha_error").html("<p>Invaild 'Your Email' field</p>");
			    $("#captcha_error").show();
			  $("input#email").focus();
			  return false;
			} 
	}
	
	
	// check subject is valid strip http:// --//
	var subject = $("input#subject").val();
	subject = subject.replace(stripHttpRegEx, "");
	$("input#subject").val(subject);
		if (subject == "") {
      $("#captcha_error").html("<p>Enter Subject</p>");
	   $("#captcha_error").show();
      $("input#subject").focus();
      return false;
    }
	
	// check subject is valid strip http:// --//
	var thelink = $("input#thelink").val();
	//alert(document.referrer);
	if (thelink == "") {
      $("#captcha_error").html("<p>Link did not carry</p>");
	   $("#captcha_error").show();
      return false;
    }
	
	
	var c = $("input#captcha").val();
	if (c == "") {
      $("#captcha_error").html("<p>Enter CAPTCHA</p>");
	   $("#captcha_error").show();
      $("input#captcha").focus();
      return false;
    } else {
		// prepare info for check
		var dataString = "&captcha="+$("input#captcha").val();
		// make ajax check
		var capchaTest = $.ajax({
		  type: "POST",
		  url: "captcha.php",
		  data: dataString,
		  beforeSend: function(){$("#loading").show("fast");},
		  success: function(msg) {
			  // need captcha test here...
			 $("#loading").hide("fast");
			 $('#captcha_error').html(msg)
			 .hide()
			 .fadeIn(1500);
		  },
   			complete: function(){
			if($('#captcha_error').html() != ""){
				////////-----------------------------------alert("captcha!");	
			} else{
				$("#captcha_error").hide("fast");     
				//var dataString = '?mailto='+mailto+'&sender='+sender+'&from='+from+'&subject='+subject+'&link='+(document.referrer);
				var dataString = $("#formDiv form").serialize();
//alert(dataString);
				$.ajax({
					  type: "POST",
					  url: "process.php",
					  data: dataString,
					  success: function() {
						$('#formDiv').html("<h2 style='text-align:center'>Mailed successfully.</h2>")
						.hide()
						.fadeIn(1000, function() {
								window.close();
								return false;
								});/**/
					  }
				});
				/**/
				//$("#formDiv form").submit();
				//alert("Send Me!");
				//submitbutton('send');	
			}
     	// Handle the complete event
   		}
  });
		return false;
	}
    return false;
});
});
