var CookieName = 'merlocarta';

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function getWindowWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
	if (document.body) return document.body.clientWidth;
	return window.undefined;
}

function getWindowHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight;
	return window.undefined;
}
	
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
	
	
function getPageSize(){	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getObjectSize(DOMelement){	
	var objectWidth, objectHeight;
	if (DOMelement.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			objectWidth = DOMelement.clientWidth; 
		} else {
			objectWidth = DOMelement.innerWidth;
		}
		objectHeight = DOMelement.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		objectWidth = DOMelement.clientWidth;
		objectHeight = DOMelement.clientHeight;
	} else if (document.body) { // other Explorers
		objectWidth = DOMelement.clientWidth;
		objectHeight = DOMelement.clientHeight;
	}	
	arrayPageSize = new Array(objectWidth,objectHeight) 
	return arrayPageSize;
}


function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

function openPopup(divName) {
	//if(readCookie(CookieName) == null){
		var objBody = document.getElementsByTagName('body').item(0);
		var objForm = document.getElementsByTagName('form').item(0);
		var objOverlay = document.getElementById('overlay');
		var objPopup = document.getElementById(divName);
		if(objOverlay == null) {
			var objOverlay = document.createElement('div');
			objOverlay.setAttribute('id','overlay');
			objBody.appendChild(objOverlay);
		}

		var arrayPageSize = getPageSize();
		//arrayPageSize[1] += 200;
		objOverlay.style.backgroundColor = "#000000"; 
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = "0px";
		objOverlay.style.left = "0px";
		// objOverlay.style.zIndex = 80;
		objOverlay.style.opacity = "0.7";
		objOverlay.style.filter = 'alpha(opacity=80)';
		objOverlay.style.width = arrayPageSize[0] + "px";
		objOverlay.style.height = arrayPageSize[1] + "px";
		objOverlay.onclick = function() { closePopup(divName); }
		createCookie(CookieName,'false',30);
		
		nascondiObjects();
		
		showPopup(divName);
	//}
}

function nascondiObjects() {
	var Obj = document.body.getElementsByTagName("object");
	for(i=0;i<Obj.length;i++){
		Obj[i].style.visibility = 'hidden';
	}
}
function mostraObjects() {
	var Obj = document.body.getElementsByTagName("object");
	for(i=0;i<Obj.length;i++){
		Obj[i].style.visibility = 'visible';
	}
}
		
function closePopup(divName) {
	var objOverlay = document.getElementById('overlay');
	var objPopup = document.getElementById(divName);
	objOverlay.style.visibility = 'hidden';
	objOverlay.style.zIndex = -1;
	objPopup.style.visibility = 'hidden';
	objPopup.style.display = 'none';
	objPopup.style.zIndex = -1;
	// showSelectBoxes();
	mostraObjects();
}

var AcceptMessage=true;

if(document.implementation && document.implementation.createDocument) var isMozilla=true;
	else var isMozilla=false;

function showPopup(divName){	
	var objOverlay = document.getElementById('overlay');
	var popupDiv = document.getElementById(divName);
	objOverlay.onclick = function() { closePopup(divName); }
	objOverlay.style.zIndex = 99998;
	objOverlay.style.visibility = "visible";
	popupDiv.style.visibility = "hidden";
	popupDiv.style.display = "block";
	popupDiv.style.zIndex = 99999;
	var scrollTop = getScrollXY()[1];
	var objSize = getObjectSize(popupDiv);
	var popupPositionTop = Math.round(scrollTop + getWindowHeight()/2 - parseInt(objSize[1])/2);
	var popupPositionLeft = Math.round(getWindowWidth()/2 - parseInt(objSize[0])/2);
	popupDiv.style.top  = (popupPositionTop) + "px";
	popupDiv.style.left = popupPositionLeft + "px";
	popupDiv.style.visibility = "visible";
}

function stripSlashes(content) {
	// Strip Slashes
	re = /\\\"/gi;
	content = content.replace(re, '"');
	re = /\\\'/gi;
	content = content.replace(re, "'");
	return content;
}
function LeggiGET(name){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if(results == null){
		return "";
  } else{
  	return results[1];
  }
}
function GOOO(){
	openPopup('popup');
}
//Solo se è un Browser
if(BrowserDetect.browser=="Chrome" || BrowserDetect.browser=="OmniWeb" || BrowserDetect.browser=="Safari" || BrowserDetect.browser=="Opera" || BrowserDetect.browser=="iCab" || BrowserDetect.browser=="Konqueror" || BrowserDetect.browser=="Firefox" || BrowserDetect.browser=="Camino" || BrowserDetect.browser=="Netscape" || BrowserDetect.browser=="Explorer" || BrowserDetect.browser=="Mozilla"){
	if(LeggiGET('popup') == '1'){
		window.onload = function(){setTimeout("GOOO()", 4000);}
	}
}

function ValidaForm(formName){
var email = document.forms[formName].email.value;
var privacy = document.forms[formName].privacy.checked;
var email_exp = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
var validation = true;
if (email.search(email_exp) == -1) {
validation = false;
alert("Inserire un\'indirizzo E-mail valido!");
return false;
}
if (!privacy) {
validation = false;
alert("E' necessario accettare i Termini sulla Privacy!");
return false;
}
if (validation == true) {
createCookie(CookieName,'true',180);
return true;
} else {
return false;
}
}
