﻿var popupStatus = 0;

function loadPopup(linkUrl){
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.6"
		});
		$("#backgroundPopup").fadeIn("slow");
		
		$.ajax({
				  url: linkUrl,
				  cache: false,
				  async: false,
				  success: function(html){
					
					pos1= html.indexOf('<body')+6;
					pos2= html.indexOf('</body>'); 
					corpo3 = html.slice(pos1,pos2);


					$(".popupcont").append(corpo3);
					$("#popupContact").fadeIn("slow");
					 }
				});
		
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow", function() {
  		  $("#backgroundPopup").remove();
 		 });
		$("#popupContact").fadeOut("slow", function() {
			$("#popupContact").remove();
		});
		popupStatus = 0;
	}
}

function centerPopup(){
	if(document.documentElement.clientWidth==0){
		var windowWidth =  document.body.offsetWidth;
		var windowHeight = document.body.offsetHeight;
	}else{
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
	}
	var popupHeight = "450";
	var popupWidth = $("#popupContact").width();
	$("#popupContact").css({
		/*"position": "absolute",*/
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

/* POP UP PER IL FORM */
function apriPop(linkUrl){
	var elemento = "<div id='popupContact'>";
	elemento = elemento +"<div class='popupheader'><a id='popupContactClose' href='#'><img src='/img/pop-form/bott-chiudi.gif' width='97' height='33' /></a></div>";
	elemento = elemento +"<div class='popupcont'>"; 
	elemento = elemento +"</div></div><div id='backgroundPopup'></div>";
	$("body").append(elemento); 

	//CLOSING POPUP
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	
	centerPopup();
	loadPopup(linkUrl);
	}

function controlla(){
	if (document.forms.form_pop.NOME.value.length==0 ||
		document.forms.form_pop.CGN.value.length==0 || 
		document.forms.form_pop.IND.value.length==0 ||
		document.forms.form_pop.NCIV.value.length==0 ||
		document.forms.form_pop.LOC.value.length==0 ||
		document.forms.form_pop.CAP.value.length==0 ||
		document.forms.form_pop.PROV.value.length==0 
		)
		{
			alert("ATTENZIONE:\nAlcuni campi non sono stati compilati!");
		    return;
    	}
		
	document.forms.form_pop.submit();
}
