function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function page_Over(idImage,message) {	
	if (document.getElementById(idImage)) {
		myImage = document.getElementById(idImage).src;
		document.getElementById(idImage).src = myImage.replace('.gif','Over.gif');
		document.getElementById('pageMessage').innerHTML = message;
	}
}

function page_Out(idImage,message) {
	if (document.getElementById(idImage)) {
		myImage = document.getElementById(idImage).src;
		document.getElementById(idImage).src = myImage.replace('Over.gif','.gif');
		document.getElementById('pageMessage').innerHTML = message;
	}
}

function List_Over(me) {
		document.getElementById('List_'+me).style.backgroundImage="url('template/standard/img_struct/ArticleBGOver.gif')";
}

function List_Out(me) {
		document.getElementById('List_'+me).style.backgroundImage="url('template/standard/img_struct/ArticleBG.gif')";
}

function ControlFormPayPack(idForm) {
	
	errorObl = false;

	if (document.getElementById('name_'+idForm).value=="") {
		errorObl = true;
	}
	if (document.getElementById('email_'+idForm).value=="") {
		errorObl = true;
	}
	if (document.getElementById('confemail_'+idForm).value=="") {
		errorObl = true;
	}
	if (document.getElementById('piva_'+idForm).value=="") {
		errorObl = true;
	}
	if (document.getElementById('tel_'+idForm).value=="") {
		errorObl = true;
	}
	if (document.getElementById('address_'+idForm).value=="") {
		errorObl = true;
	}
	if (document.getElementById('location_'+idForm).value=="") {
		errorObl = true;
	}

	if (document.getElementById('confemail_'+idForm).value!=document.getElementById('email_'+idForm).value) {
		alert("Attenzione! Verificare che il campo Email e Conferma Email combacino!");
		errorObl = true;
	}


	if (document.getElementById('departures_'+idForm)) {
		departure = document.getElementById('departures_'+idForm).options[document.getElementById('departures_'+idForm).selectedIndex].value;
		if (departure=="") {
			errorObl = true;
		}
	}
	
	nAdult = document.getElementById('nadult_'+idForm).options[document.getElementById('nadult_'+idForm).selectedIndex].value * 1;
	nBaby = document.getElementById('nbaby_'+idForm).options[document.getElementById('nbaby_'+idForm).selectedIndex].value * 1;
	nNeo = document.getElementById('nneo_'+idForm).options[document.getElementById('nneo_'+idForm).selectedIndex].value * 1;
	//nAdult = 0;
	//nBaby = 0;

	if (nAdult==0) errorObl = true;

	if (errorObl==true) {
		//Controllo obbligatorietà
		alert("Compilare Nome, Email, Cod. Fiscale, Telefono, Indirizzo, Citta', il Nr. partecipanti e se presente la Partenza");
	} else {
		//alert("Tot post: "+document.getElementById('totElement_'+idForm).value+" ("+(nAdult+nBaby)+")");
		//Controllo disponibilità
		if ((nAdult+nBaby+nNeo)>document.getElementById('totElement_'+idForm).value) {
			alert("Disponibilta' posti richiesta non presente!");
		} else {
			totPrice = (document.getElementById('PriceA_'+idForm).value*1)*nAdult + (document.getElementById('PriceB_'+idForm).value*1)*nBaby + (document.getElementById('PriceC_'+idForm).value*1)*nNeo;
			
			UpdateTotal(idForm,scontoSaved);
			//document.getElementById('Price_'+idForm).value = totPrice;
			//document.getElementById('PriceClient_'+idForm).value = totPrice;
			//alert(document.getElementById('Price_'+idForm).value);
			document.getElementById('form_'+idForm).submit();
		}
	}

}

var idFormSaved = 0;
var scontoSaved = 0;

function CalcTotal(idForm) {
		
	codSconto = document.getElementById('codsconto_'+idForm).value;
	if ((codSconto!="")&&(codSconto!="Cod. di Sconto")) {
		idFormSaved = idForm;		
		requestContent('actionphp/request.code.php?cod='+codSconto);
	} else {
		UpdateTotal(idForm,0);
	}
}

function UpdateTotal(idForm,sconto) {

	var nAdult = document.getElementById('nadult_'+idForm).options[document.getElementById('nadult_'+idForm).selectedIndex].value * 1;
	var nBaby = document.getElementById('nbaby_'+idForm).options[document.getElementById('nbaby_'+idForm).selectedIndex].value * 1;
	var nNeo = document.getElementById('nneo_'+idForm).options[document.getElementById('nneo_'+idForm).selectedIndex].value * 1;	
	
	totPrice = euro((document.getElementById('PriceA_'+idForm).value*1)*nAdult + (document.getElementById('PriceB_'+idForm).value*1)*nBaby + (document.getElementById('PriceC_'+idForm).value*1)*nNeo);
	
	if (sconto>0) {
		var totNewPrice = euro(totPrice-totPrice*sconto/100);
		document.getElementById('total_'+idForm).innerHTML = "<div><h2>Totale cliente: "+totPrice+" &euro;</h2></div><div><h2>Totale netto: "+totNewPrice+" &euro;</h2></div>";
		document.getElementById('Price_'+idForm).value = totNewPrice;
		document.getElementById('PriceClient_'+idForm).value = totPrice;
	} else {
		document.getElementById('total_'+idForm).innerHTML = "<h2>Totale: "+totPrice+" &euro;</h2>";
		document.getElementById('Price_'+idForm).value = totPrice;
		document.getElementById('PriceClient_'+idForm).value = totPrice;
	}
	

}

function euro(number) {
	
	num=Math.round(parseFloat(number)*100)/100; 
    num=""+num; 
    if(num.indexOf(".") == -1) 
    { 
        num=num+".00"; 
         
    } 
    else 
    { 
        var splitted=num.split("."); 
        if(splitted[1].length < 2)splitted[1]=splitted[1]+"0"; 
        num=splitted[0]+"."+splitted[1]; 
    } 
    return num;
    
}

function requestContent(myLink) { 

    var httpAJAXList = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        httpAJAXList = new XMLHttpRequest();
        if (httpAJAXList.overrideMimeType) {
            httpAJAXList.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            httpAJAXList = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                httpAJAXList = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!httpAJAXList) {
		//Nel caso il browser non supporti l'oggetto g eseguo un semplice redirect
		return false;
	}

    httpAJAXList.open('GET', myLink, true); 
    httpAJAXList.onreadystatechange = function() { responseContentMessage(httpAJAXList); };
    httpAJAXList.send(null);  
    
    if (method!="automatic") {
    	xhrTimeout=setTimeout("timeoutContentMessage();",120000);
	}

}  
  
function responseContentMessage(httpAJAXList) {  
    if (httpAJAXList.readyState == 4) {    
    	if (httpAJAXList.status == 200) {
    		//clearTimeout(xhrTimeout);
    	    var response = httpAJAXList.responseText;  
       	 	var update = new Array();  
        	if (response == "LOGOUT") {  
  		  		//window.location="";
  		  	} else {
	        	if (response.indexOf('|' != -1)) {
	        		//alert("Risposta: "+response);
	        		scontoSaved = response;
	        		if (scontoSaved==0) {
						alert("Cod. Sconto non valido!");	        		
	        		} else {
		        		UpdateTotal(idFormSaved,response);
	        		}
   		        }
    	
    		}
    	
        }  
        
    }  
    
} 
