/*

 Questo file contiene tutti gli script per il funzionamento lato client
 dell'applicazione web. 

 Questo file č tutelato dalle leggi sul copyright, dalle leggi sui diritti
 d'autore e dalle disposizioni dei trattati internazionali.
 La riproduzione e/o la distribuzione parziale e/o totale di esso
 sarą perseguibile civilmente e penalmente nella misura massima consentita
 dalla legge in vigore.
 
 Copyright:		©2005 - 2007 Emme 4 S.r.l.
 Autore script:	Omar Destefani
 Nome file:		poderespedalone.js
 Versione:		1.1
 
*/

var winPhoto		= null;

// Restituisce l'oggetto con l'id passato
function getElement(id)
{
	var obj = null;

	if (document.getElementById) obj = document.getElementById(id);
	else if (document.all) obj = document.all[id];
	else if (document.layers) obj = document.layers[id];
	
	return obj;
}

function showImage(image,w,h)
{
	var env;
	var html;

	// if w or h equals -1 then the window is auto resized after the load of the image ends
	if ((w==-1)||(h==-1))
	{
		env =	"resizable=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes";
		html =	"<html>\n<head><title>Foto gallery</title></head>\n<body>\n<img src=\""+image+"\" id=\"image\" name=\"image\">\n</body>\n</html>";
	}
	else
	{
		env =	"width="+w+",height="+h+",resizable=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes";	
		html =	"<html>\n<head><title>Foto gallery</title></head>\n<body>\n<img src=\""+image+"\" id=\"image\" name=\"image\">\n</body>\n</html>";
	}
	
	if (winPhoto) winPhoto.close();
	
	winPhoto=window.open("","FotoGallery",env);

	winPhoto.document.write(html);
	winPhoto.focus();
}

function showCitazione(index,w,h)
{
	var env="width="+w+",height="+h+",resizable=1,scrollbars=1";
	var win=window.open("citazioni.php?i="+index,"Citazione",env);
	win.focus();
}

function showPayWindow()
{
	var env="width=760,height=570,resizable=1,scrollbars=1,status=1,menubar=1,location=1";
	var win=window.open("merchant_pagamento.php", "Pagamento", env);
	win.focus();
}

function emailCheck(address) {
	var emailPath=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPath=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPath=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPath=new RegExp("^" + atom + "(\\." + atom +")*$");

	var matchArray=address.match(emailPath);
	
	if (matchArray==null) {
		//alert("Email address seems incorrect (check @ and .'s)")
		return false;
	}
	
	var user=matchArray[1];
	var domain=matchArray[2];

	if (user.match(userPat)==null) {
	    //alert("The username doesn't seem to be valid.")
	    return false;
	}

	var IPArray=domain.match(ipDomainPath)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
		        //alert("Destination IP address is invalid!")
				return false;
		    }
	    }
	    return true;
	}

	var domainArray=domain.match(domainPath)
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
	    return false;
	}

	var atomPath=new RegExp(atom,"g");
	var domArr=domain.match(atomPath);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   //alert("The address must end in a three-letter domain, or two letter country.")
	   return false;
	}

	if (len<2) {
	   //var errStr="This address is missing a hostname!"
	   //alert(errStr)
	   return false;
	}

	return true;
}

var _timeout = 100;
var _timer = null;

function autoResize(elId)
{
	if (!elId) return;
	
	var w = elId.offsetWidth;
	var h = elId.offsetHeight;
	
	if ((!w)||(!h))
	{
		_timer = setTimeout('autoResize(elId)', _timeout);
	}
	else
	{
		_timer = clearTimeout();
		_timer = null;
		
		var sw = screen.width;
		var sh = screen.height;
		
		if (w>sw)w=sw;
		if (h>sh)h=sh;
		
		this.resizeTo(w,h);
	}
}

function showContactDialog()
{
	var _container=document.getElementById('DialogContainer');
	
	if (_container.childNodes.length==0)
	{
		var _iframe=document.createElement('iframe');
		_iframe.src='form_contatto.php';
		_iframe.style.width='100%';
		_iframe.style.height='100%';
		_iframe.style.border='none';
		_iframe.style.overflow='hidden';
		_iframe.scrolling='no';
		_iframe.frameborder='no';
		
		_container.appendChild(_iframe);
	}

	showDialog();
}

function showDialog()
{
	var _dialog=document.getElementById('PageLocker');
	if(_dialog)_dialog.style.display='block';
}

function closeDialog()
{
	var _dialog=document.getElementById('PageLocker');
	if(_dialog)_dialog.style.display='none';
}

$(document).ready(
	function()
	{
		$('div.DialogBox').wrap('<div class="dialog"><div class="dialog-body"><div class="l"></div><div id="DialogContainer" class="c"></div><div class="r"></div></div>');
		$('div.dialog').prepend('<div class="dialog-head"><div class="l"></div><div class="c"><div class="dialog-close"><a href="javascript:void(0);" onclick="closeDialog();" title="Chiudi"><img alt="Chiudi" src="images/Void.png" /></a></div></div><div class="r"></div></div>').append('<div class="dialog-foot"><div class="l"></div><div class="c"></div><div class="r"></div></div>')
	;}
);

document.onclick=closeDialog;
