var richiesta;
var nometag;
// Browser sniffing

/// detect Opera browser
is_opera = /opera/i.test(navigator.userAgent);

/// detect a special case of "web browser"
is_ie = ( /msie/i.test(navigator.userAgent) && !is_opera );

/// detect IE5.0/Win
is_ie5 = ( is_ie && /msie 5\.0/i.test(navigator.userAgent) );

/// detect IE for Macintosh
is_mac_ie = ( /msie.*mac/i.test(navigator.userAgent) && !is_opera );

/// detect KHTML-based browsers
is_khtml = /Konqueror|Safari|KHTML/i.test(navigator.userAgent);

/// detect Konqueror
is_konqueror = /Konqueror/i.test(navigator.userAgent);

/// detect Gecko
is_gecko = /Gecko/i.test(navigator.userAgent);

function ajax(pagina,idElemento) {
			data=new Date();
			pagina=pagina+'&time='+data.getTime();

       nometag=idElemento;
       var browser=navigator.appName;
       if(browser=="Microsoft Internet Explorer"){richiesta= new ActiveXObject("Microsoft.XMLHTTP");}
         else{ 
               richiesta= new XMLHttpRequest();
               richiesta.overrideMimeType('text/xml');
          }
       richiesta.open('get',pagina,true); 
       richiesta.onreadystatechange=aggiornaElemento;  
       richiesta.send(null);
}
    
function aggiornaElemento() {
       if(richiesta.readyState==4){
             var risposta=richiesta.responseText;
             var ob=document.getElementById(nometag);
             ob.innerHTML=risposta; 
       }
}

var pos=15;
function scroll() {
			if(document.all||document.getElementById){
					h=document.getElementById("myDiv1").offsetHeight;
					pos=pos-1;
					if(h==-pos){pos=0}
					document.getElementById("myDiv1").style.top=pos+"px";
					document.getElementById("myDiv2").style.top=pos+"px";
					
			}else{
					h=myDiv.offsetHeight;
					pos=pos-1;
					if (-pos>h)pos=300;
					myDiv.top=pos+"px";
			}
}

function trim(stringa){
    var newstr = stringa + "";
    newstr = lTrim(newstr);
    newstr = rTrim(newstr);
    return newstr;
}

function lTrim(stringa){
    var newstr = stringa + "";
    while(newstr.charAt(0) == " ") 
        newstr = newstr.substring(1, newstr.length);    
    return newstr;
}

function rTrim(stringa){
    var newstr = stringa + "";
    while(newstr.charAt(newstr.length - 1) == " ") 
        newstr = newstr.substring(0, newstr.length - 1);    
    return newstr;
}

function convalida_email(mail){
	if (trim(mail)=='')return false;
		var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;	
		if (espressione.test(mail))return true;
		else return false;
}



var ab_menu=false;
							    
function rimuovi() { 
			if(ab_menu==false){
				e=document.getElementById('menu_elem');
				if(e!=null)document.body.removeChild(e);
				
				f=document.getElementById('WCH1');
				if(f!=null)document.body.removeChild(f);
				
			}
	}

function menu(elemento,indice){
	rimuovi();
	var tab = document.createElement('table');
		tab.onmouseout=function(){ab_menu=false;setTimeout('rimuovi()',500);};
		tab.onmouseover=function(){ab_menu=true;};
		tab.id='menu_elem';
		tab.cellSpacing='1px';
		tab.cellPadding='2px';
		tab.style.position='absolute';
		tab.style.top = eval(curTop(elemento) + elemento.offsetHeight+1) + 'px';
		tab.style.left = curLeft(elemento) + 'px';
    tab.style.width='170px'; 
 		tab.style.zIndex='0'; 		
 
 //on top IE 		
 		if (is_ie && !is_ie5) {
 			var frameFront = null;
 			frameFront=createFrameFront(); 			 			
 		}
		document.body.appendChild(tab);				
		for(var i=0;i<menu_array[indice].length;i=i+2){
				r = tab.insertRow(-1);
				c = r.insertCell(-1);
				c.className='tasto_menu';
				var testo='';
				c.pagina=menu_array[indice][i+1];
				c.onmouseover=function (){this.className='tasto_menu_over';ab_menu=true;}
				c.onmouseout=function (){this.className='tasto_menu';ab_menu=false;}
				if(indice==0)c.onclick=function (){window.open(this.pagina);}
				else c.onclick=function (){document.location.href=this.pagina;}
				c.innerHTML ='&nbsp;&nbsp;'+menu_array[indice][i]+testo;
		}
		if (is_ie && !is_ie5) {
			var pos = getAbsolutePos(tab),
			X1 = pos.x,
			Y1 = pos.y,
			X2 = X1 + tab.offsetWidth,
			Y2 = Y1 + tab.offsetHeight;		
			setFrameFront (frameFront,X1,Y1,X2-X1,Y2-Y1);		
		}
		ab_menu=true;
}
createFrameFront = function() {
	var f = null;		
		var filter = 'filter:progid:DXImageTransform.Microsoft.alpha(style=0,opacity=0);';
		var id = "WCH1";
		window.self.document.body.insertAdjacentHTML
			('beforeEnd', '<iframe id="' + id + '" scroll="no" frameborder="0" ' +
		 	'style="z-index:0;position:absolute;visibility:hidden;' + filter +
		 	'border:0;top:0;left:0;width:0;height:0;" ' +
		 	'src="javascript:false;"></iframe>');		
		f = window.self.document.getElementById(id);		
		return f;
}
setFrameFront = function (el,X,Y,W,H) {
	var s = el.style;
	s.left = X + "px";
	s.top = Y + "px";
	s.width = W + "px";
	s.height = H + "px";
	s.visibility = "visible";				
}

getAbsolutePos = function(el) {
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(el.tagName);
	if (is_div && el.scrollLeft)
		SL = el.scrollLeft;
	if (is_div && el.scrollTop)
		ST = el.scrollTop;
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) {
		var tmp = this.getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
};
function curLeft(obj){
	toreturn = 0;
	while(obj){
		toreturn += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return toreturn;
}

function curTop(obj){
	toreturn = 0;
	while(obj){
		toreturn += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return toreturn;
}

var ab_date=false;
							    
function rimuovi_div_date() { 
			if(ab_date==false){
				e=document.getElementById('divdate');
				if(e!=null)document.body.removeChild(e);
				f=document.getElementById('WCH1');
				if(f!=null)document.body.removeChild(f);
				}
	}

function div_date(elemento,idviaggio){
	rimuovi_div_date();
	
	//on top IE 		
 		if (is_ie && !is_ie5) {
 			var frameFront = null;
 			frameFront=createFrameFront(); 			 			
 		}
	
	var tab = document.createElement('div');
		tab.onmouseout=function(){ab_date=false;setTimeout('rimuovi_div_date()',500);};
		tab.onmouseover=function(){ab_date=true;};
		tab.id='divdate';
		tab.style.position='absolute';
		tab.style.top = curTop(elemento)+ 'px';
		tab.style.left = curLeft(elemento) + 'px';
 		tab.style.zIndex='0'; 		
 		tab.className='divdate';
		document.body.appendChild(tab);
		tab.innerHTML ='';
		if (is_ie && !is_ie5) {
			var pos = getAbsolutePos(tab),
			X1 = pos.x,
			Y1 = pos.y,
			X2 = X1 + tab.offsetWidth,
			Y2 = Y1 + tab.offsetHeight;		
			setFrameFront (frameFront,X1,Y1,X2-X1,Y2-Y1);		
		}
		ajax('viaggi_date.php?id='+idviaggio,'divdate')
		ab_date=true;
}
function ValidateData(DateString) {
		var match;
		var tmpDate;
		var validFormat = false;
		var dataOdierna=new  Date();
									
		try {
				match = DateString.match(/^(\d?\d)[/](\d?\d)[/](\d{4})$/);
				tmpDate = new Date(match[3], match[2] - 1, match[1]);
				validFormat = ((tmpDate.getMonth()==match[2]-1) && (tmpDate.getDate()==match[1]));
				if (validFormat)if(dataOdierna<tmpDate){
													alert('Data '+DateString+' successiva alla data odierna!');
													validFormat=false;
													}
		}catch (e) {}
			finally { return validFormat;}
}
function div_date2(elemento,idviaggio){
	rimuovi_div_date();
	var tab = document.createElement('div');
		tab.onmouseout=function(){ab_date=false;setTimeout('rimuovi_div_date()',500);};
		tab.onmouseover=function(){ab_date=true;};
		tab.id='divdate';
		tab.style.position='absolute';
		tab.style.top = (curTop(elemento)-1) + 'px';
		tab.style.left = (curLeft(elemento)-110) + 'px';
 		tab.style.zIndex='10';
 		tab.className='divdate';
		document.body.appendChild(tab);
		tab.innerHTML ='';
		ajax('viaggi_date2.php?id='+idviaggio,'divdate')
		ab_date=true;
}
