var popupStatus = 0;

function loadPass() {
	if (popupStatus == 0) {
		$("#backgroundPopup").css( {
			"opacity" : "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupPass").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePass() {
	if (popupStatus == 1) {
		$("#backgroundPopup").fadeOut("slow");
		$("#popupPass").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPass() {
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupPass").height();
	var popupWidth = $("#popupPass").width();
	$("#popupPass").css( {
		"position" : "absolute",
		"top" : windowHeight / 2 - popupHeight / 2,
		"left" : windowWidth / 2 - popupWidth / 2
	});
	// only need force for IE6
	$("#backgroundPopup").css( {
		"height" : windowHeight
	});

}

function postPass() {
	document.fpass.furl.value=window.location;
	showWaitPass();
	if (popupStatus == 1) {
		$.post("index.php", $("#fpass").serialize(), function(data) {
			if (data != '') {
				alert(data);
			} else {
				document.fpass.reset();
				disablePass();
			}
			hideWaitPass();
		});
	}
}

function showWaitPass() {
	$("#popupPassSend").hide();
	$("#popupPassWait").show();
}

function hideWaitPass() {
	$("#popupPassWait").hide();
	$("#popupPassSend").show();
}

$(document).ready(function() {
	// LOADING POPUP
		// Click the button event!
		$("#buttonPass").click(function() {
			// centering with css
				centerPass();
				// load popup
				loadPass();
			});
		// CLOSING POPUP
		// Click the x event!
		$("#popupPassClose").click(function() {
			disablePass();
		});
		// Click out event!
		$("#backgroundPopup").click(function() {
			disablePass();
		});
		// Press Escape event!
		$(document).keypress(function(e) {
			if (e.keyCode == 27 && popupStatus == 1) {
				disablePass();
			}
		});
		// POST POPUP
		// Click the button event!
		$("#popupPassSend").click(function() {
			postPass();
		});
		
		$("#fcoment590").validate({ 
				
					 rules: { 
						 fnome: { 
							required: true 
						 }, 
						 femail: { 
						   required: true,
						   email: true  
						 }, 
						 fcomment: { 
						  required: true,
						  minlength: 5, 
						  maxlength:5000 
						}
		  } 
		 });
		
		
		
	});

