//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
	$("#backgroundPopup").css({
	"opacity": "0.7"
	});
	$("#backgroundPopup").fadeIn("slow");
	$("#popupContact").fadeIn("slow");
	popupStatus = 1;
}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
	$("#backgroundPopup").fadeOut("slow");
	$("#popupContact").fadeOut("slow");
	popupStatus = 0;
}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var windowScroll = document.documentElement.scrollTop;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": (windowHeight/2) - (popupHeight/2) + windowScroll,
		"left": (windowWidth/2) - (popupWidth/2)
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});

}

$(document).ready(function(){
	$("#popupForm").validate();
	$("#popupEnvoyer").click(function() {
		if ($("#popupForm").valid()==true) {popupSend();}
		return false;
	});
	$("#popupResetbut").click(function() {
		var popfrm = $("#popupForm").validate();
		popfrm.prepareForm();
		popfrm.hideErrors(); 
		document.getElementById("popupForm").reset();
		return true;
	});

	//LOADING POPUP
	//Click the button event!
	$("#popupButton").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
	
	function popupSend(){
		//centering with css
		var $body = prepareInfoPopup();
		if ($body != ""){

			$body += '\n';
			$body += '\n';
			$body += '----------------Informations du client----------------\n';
			$body += 'Nom:                  ' + document.getElementById("nom").value + '\n'; 
			$body += 'Courriel:             ' + document.getElementById("courriel").value + '\n'; 
			$body += 'Nom de l\'entreprise:  ' + document.getElementById("entreprise").value + '\n';
			$body += '\n';
			$body += 'Veux des informations sur:\n'; 
			
			if (document.getElementById("creditsRDcheck").checked == true) {
				$body += '  -Credits de recherche et developpement\n'; 
			}
			
			if (document.getElementById("fiduciecheck").checked == true) {
				$body += '  -Utilisation de fiducie\n'; 
			}
			
			if (document.getElementById("CDAEcheck").checked == true) {
				$body += '  -Credits d\'impot pour affaires electroniques\n'; 
			}
			
			if (document.getElementById("etatscheck").checked == true) {
				$body += '  -Etats financiers\n'; 
			}
			
			if (document.getElementById("evaluationcheck").checked == true) {
				$body += '  -Evaluation d\'entreprise\n'; 
			}
			
			if (document.getElementById("planificationcheck").checked == true) {
				$body += '  -Planification fiscale\n'; 
			}
			
			if (document.getElementById("financementcheck").checked == true) {
				$body += '  -Financement\n'; 
			}
			
			if (document.getElementById("gestioncheck").checked == true) {
				$body += '  -Gestion d\'entreprise\n'; 
			}
		
			var myrequest= ajaxRequest();
			myrequest.onreadystatechange=function(){
				 if (myrequest.readyState==4){
					  if (myrequest.status==200 || window.location.href.indexOf("http")==-1){
						alert(myrequest.responseText);
					  }
					  else{
						alert("Une erreur s'est produite, veuillez réessayer.");
					  }
				 }
			}
			$body = encodeURIComponent($body);
			myrequest.open("POST", "sendmail.php", true);
			myrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			myrequest.send("text=" + $body);
			
			disablePopup();
		}
	};
	
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	
	//Press Escape event!
	/*
	$(document).keypress(function(e){
		if(e.keyCode==27 &amp;amp;amp;amp;&amp;amp;amp;amp; popupStatus==1){
			disablePopup();
		}
	});
	*/

});

function ajaxRequest() {
   var AJAX = null;                                 // Initialize the AJAX variable.
   
   if (window.XMLHttpRequest) {                     // Does this browser have an XMLHttpRequest object?
      AJAX=new XMLHttpRequest();                    // Yes -- initialize it.
   } else {                                         // No, try to initialize it IE style
      AJAX=new ActiveXObject("Microsoft.XMLHTTP");  // Wheee, ActiveX, how do we format c: again?
   }      // End setup Ajax.
   
   if (AJAX==null) {                                // If we couldn't initialize Ajax...
      alert("Your browser doesn't support AJAX.");  // Sorry msg.                                               
      return false;                                 // Return false, couldn't set up ajax
   }
   return AJAX;
}
