var popupStatus = 0;

//load the popup
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#background_popup").css({
			"opacity": "0.7"
		});
		$("#background_popup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#popupContact").slideUp(300,function() {
			$("#background_popup").fadeOut("slow");
		});
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#background_popup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#contact").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#background_popup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

})

function contact() {
	
	oForm = document.contact_form;
	oFname = oForm.first_name.value;
	oLname = oForm.last_name.value;
	oStreet1 = oForm.street1.value;
	oStreet2 = oForm.street2.value;
	oCity = oForm.city.value;
	oState = oForm.state.value;
	oZip = oForm.zip.value;
	oDayphone = oForm.dayphone.value;
	oNightphone = oForm.nightphone.value;
	oEmail = oForm.email.value;
	oComment = oForm.comments.value;

	//ajax
	 var xmlhttp;

 if (window.XMLHttpRequest)
   {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
   }
 else
   {// code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
 xmlhttp.open("POST","ajax/contact",false);
 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlhttp.send("first_name="+oFname+"&last_name="+oLname+"&street1="+oStreet1+"&street2="+oStreet2+"&city="+oCity+"&state="+oState+"&zip="+oZip+"&dayphone="+oDayphone+"&nightphone="+oNightphone+"&email="+oEmail+"&comments="+oComment);
 response=xmlhttp.responseText;
 if (response=='true') {
	 success();
 }
 else{
	 fail();
 }
}

function fail() {
	popup = document.getElementById("popupContact");
	a = document.contact_form;
	popup.removeChild(a);
	b=document.createElement('div');
	b.className='error';
	b.innerHTML='There was an error processing your message.<br />Please try again later.';
	popup.appendChild(b);
	setTimeout(disablePopup, 5000);
}

function success() {
	popup = document.getElementById("popupContact");
	a = document.contact_form;
	popup.removeChild(a);
	b=document.createElement('div');
	b.className='success';
	b.innerHTML='Your message was sent successfully.<br />Thank you.';
	popup.appendChild(b);
	setTimeout(disablePopup, 5000);
}
