$(document).ready(function(){
	error_status = false;
	sent = false;
	$(".email").click(function(){
		if(sent){
			sent = false;
			$("#email_form").toggle();
			$("#i1").val("");
			$("#i2").val("");
			$("#email_send").html("");
		}
		$("#email").toggle();
		return false;
	});
	$("#close").click(function(){
		$("#email").toggle();
	});
	$(".close").click(function(){
		$("#email").toggle();
	});
	$("#email_friend_submit").click(function(){
		error = formCheckGJ();
//		$("#page").val(loc);
		if(error == ''){
			sent = true;
			if(error_status == true){
				$("#email_error").toggle();
				error_status = false;
			}
			$("#email_form").toggle();
			$("#email_send").toggle('<strong>Sending Email</strong><br /><img src="../load.gif" alt="Sending Mail" />');
			loc = window.document.location.toString();
			$.post("q.php", { i: $("#i").val(),
							  i1: $("#i1").val(),
							  i2: $("#i2").val(),
							  i3: $("#i3").val(),
							  page: loc },
			  function(data){
				//alert("Data Loaded: " + data);
				insitePost();
				$("#email_send").html('<h2>Thank You!</h2><p><strong>Your Message Has Been Sent</strong></p>');
			  }
			);
		}else{
			if(error_status == false){
				$("#email_error").html(error);
				error_status = true;
			}else{
				$("#email_error").html(error);
			}
		}
	});
});

function formCheckGJ(){
	error = "";
	error += $("#i").val() == '' ? 'Your Name is a required field<br />' : '';
	error += checkemail($("#i1").val()) ? 'Your Email Address is invalid or missing<br />' : '';
	error += checkemail($("#i2").val()) ? 'Your Friend\'s Email Address is invalid or missing' : '';
	return error;
}

function checkemail(str){
//	var str=document.validation.emailcheck.value
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	return !filter.test(str);
}

function insitePost() {
	//save the action to insite
	loc = window.document.location.toString();
	url = "http://www.topfloortech.com/insitemetrics/uRMJ/uniformv2.php";
	url += "?actk=j7ktki-2nf0hd2p77"; //unique action key
	url += "&imReferrerField=" + escape(document.referrer);
	url += "&Sender_Name=" + escape($("#i").val());
	url += "&Sender_Email=" + escape($("#i1").val());
	url += "&imEmailField=" + escape($("#i1").val());
	url += "&Friend_Email=" + escape($("#i2").val());
	url += "&Message=" + escape($("#i3").val());
	url += "&Page=" + escape(loc);
	img = new Image(0,0);
 	img.src = url;	
}