// Script per mostrate/nascondere il box del Login in alto
function espandiLogin(scelta) {
	var itemLoginEsteso = document.getElementById('login_esteso');
	if (scelta=="LoginDefault") {
		itemLoginEsteso.className="invisibile";
	}
	else if (scelta=="LoginEsteso"){
		itemLoginEsteso.className="visibile";
	}
}

var myRequest = null;
var ajaxserver = "http://"+document.domain+"/ajaxserver.php";
var home = "http://"+document.domain+"/";

function setCookie(sNome, sValore, iGiorni) {
	  var dtOggi = new Date()
	  var dtExpires = new Date()
	  dtExpires.setTime
	    (dtOggi.getTime() + 24 * iGiorni * 3600000)
	  document.cookie = sNome + "=" + escape(sValore) +
	    "; expires=" + dtExpires.toGMTString();
	}

function CreateXmlHttpReq(handler){
    var xmlhttp = null;
    try {
        xmlhttp = new XMLHttpRequest();
    } 
    catch (e) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    xmlhttp.onreadystatechange = handler;
    return xmlhttp;
}

function ajaxHandler(id){
    if (myRequest.readyState == 4 && myRequest.status == 200) {
        e = document.getElementById("box-centro");
        e.innerHTML = myRequest.responseText;
    }
}

function alertHandler(){
	 if (myRequest.readyState == 4 && myRequest.status == 200) {
	        e = document.getElementById("alerts");
	        e.innerHTML = myRequest.responseText;        
	    }
}

function deleteAlert(id){
	myRequest = CreateXmlHttpReq(alertHandler);	
    myRequest.open("GET", "ajaxserver.php?action=deleteAlert&id="+id);
    myRequest.send(null);
}

function overlay()
{
   	var newdiv = document.createElement('div');
   	newdiv.setAttribute('id', 'overlay');   
	   
   	newdiv.style.width = document.body.clientWidth+"px";
	newdiv.style.height = document.body.clientHeight+"px";
	
  	newdiv.style.border = '1px';
	newdiv.style.borderColor = "red";
	//newdiv.style.opacity = 0.6;      
	newdiv.style.zIndex = 1000;
	newdiv.style.top = 0; 
	
	//newdiv.style.width = '1440px';
	
   	document.body.appendChild(newdiv);
   	
   	
   	
}

function box_promo (id, shop)
{
	overlay();	
	newbox();
	
	myRequest = CreateXmlHttpReq(ajaxHandler);	
    myRequest.open("GET", ajaxserver+"?action=promozione&id="+id);
    myRequest.send(null);
	
	//window.open(home+"promoserver.php?id="+id);
    window.open(home+"redirect.php?type=promo&shop="+shop+"&id="+id);
}

function newbox ()
{
	var newbox = document.createElement('div');	
	newbox.setAttribute('id', 'box-centro');
	larghezza = document.body.clientWidth;
	left = larghezza/2-185;
	newbox.style.left = left+"px";
	newbox.style.top = "200px";
	document.body.appendChild(newbox);
}

function box_close (id, url)
{
	
	box = document.getElementById(id);
	document.body.removeChild(box);
	
	overlay_div = document.getElementById('overlay');
	document.body.removeChild(overlay_div);
	
	if (url){
		window.location.href= url;
	}
}

function newAccount (zona)
{	
	//alert(document.forms['account'].promo[0].checked);
	check = document.forms['account'].condizioni.checked;
	if (check==true){	
		overlay();	
		newbox();		
		var link = ajaxserver+"?timestamp=" + new Date().getTime();
		var query = "&action=newAccount";
		query += "&nome="+encodeURIComponent(document.forms['account'].nome.value);
		query += "&cognome="+encodeURIComponent(document.forms['account'].cognome.value);
		query += "&sesso="+encodeURIComponent(document.forms['account'].sesso.value);
		query += "&day="+encodeURIComponent(document.forms['account'].day.value);
		query += "&month="+encodeURIComponent(document.forms['account'].month.value);
		query += "&year="+encodeURIComponent(document.forms['account'].year.value);
		query += "&email="+encodeURIComponent(document.forms['account'].email.value);
		query += "&user="+encodeURIComponent(document.forms['account'].user.value);
		query += "&password="+encodeURIComponent(document.forms['account'].password.value);
		query += "&address="+encodeURIComponent(document.forms['account'].address.value);
		query += "&cap="+encodeURIComponent(document.forms['account'].cap.value);
		query += "&citta="+encodeURIComponent(document.forms['account'].citta.value);
		query += "&nazione="+document.forms['account'].nazione.value;
		//query += "&prodotti="+document.forms['account'].prodotti.value;
		query += "&promo="+document.forms['account'].promo[0].checked;
		//query += "&viaggi="+document.forms['account'].viaggi.value;
		query += "&referer="+document.forms['account'].referer.value;
		query += "&kingfortune="+document.forms['account'].kingfortune[0].checked;
		
		myRequest = CreateXmlHttpReq(ajaxHandler);
	    myRequest.open("POST", link)
	    myRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	    myRequest.send(query);
	    return testCheckPI();
	} else {
		if (zona=="it"){
			alert ("Devi accettare le condizioni di utilizzo per poterti iscrivere");
		}
		if (zona=="fr"){
			alert ("Vous devez accepter les conditions générales d'utilisation");
		}
		return false;
	}
}

function modificaAccount(id)
{
	overlay();	
	newbox();
	
	var link = ajaxserver+"?timestamp=" + new Date().getTime();
	var query = "&action=modificaAccount";
	query += "&nome="+encodeURIComponent(document.forms['account'].nome.value);
	query += "&cognome="+encodeURIComponent(document.forms['account'].cognome.value);
	query += "&sesso="+encodeURIComponent(document.forms['account'].sesso.value);
	query += "&day="+encodeURIComponent(document.forms['account'].day.value);
	query += "&month="+encodeURIComponent(document.forms['account'].month.value);
	query += "&year="+encodeURIComponent(document.forms['account'].year.value);
	query += "&email="+encodeURIComponent(document.forms['account'].email.value);	
	query += "&address="+encodeURIComponent(document.forms['account'].address.value);
	query += "&cap="+encodeURIComponent(document.forms['account'].cap.value);
	query += "&citta="+encodeURIComponent(document.forms['account'].citta.value);
	query += "&nazione="+document.forms['account'].nazione.value;
	query += "&prodotti="+document.forms['account'].prodotti.value;
	query += "&promo="+document.forms['account'].promo.value;
	query += "&viaggi="+document.forms['account'].viaggi.value;
	query += "&id="+id;
	
	myRequest = CreateXmlHttpReq(ajaxHandler);
    myRequest.open("POST", link)
    myRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
    myRequest.send(query);
}

function setCookie(sNome, sValore, iGiorni) {
	  var dtOggi = new Date()
	  var dtExpires = new Date()
	  dtExpires.setTime
	    (dtOggi.getTime() + 24 * iGiorni * 3600000)
	  document.cookie = sNome + "=" + escape(sValore) +
	    "; expires=" + dtExpires.toGMTString();
	}

function logout(url) {
	setCookie("session", "");
	location.href = url;
}

function doLogin(){
	
	//alert (document.domain);
	overlay_div = document.getElementById('overlay');
	if (overlay_div != null){
		document.body.removeChild(overlay_div);
	}
	
	overlay();	
	newbox();
	
	myRequest = CreateXmlHttpReq(ajaxHandler);	
    myRequest.open("GET", ajaxserver+"?action=box_login");
    myRequest.send(null);
    	
}

function box_segnala (id)
{
	overlay();	
	newbox();
	
	myRequest = CreateXmlHttpReq(ajaxHandler);	
    myRequest.open("GET", ajaxserver+"?action=box_segnala&id="+id);
    myRequest.send(null);
}

function box_alert (id)
{
	overlay();	
	newbox();
	
	myRequest = CreateXmlHttpReq(ajaxHandler);	
    myRequest.open("GET", ajaxserver+"?action=box_alert&id="+id);
    myRequest.send(null);
}

function alert_promo (id, action){	
	myRequest = CreateXmlHttpReq(ajaxHandler);	
    myRequest.open("GET", ajaxserver+"?action=alert_promo&shop="+id+"&choice="+action);
    myRequest.send(null);
    
}

function alert_promo_out (id, action){	
	overlay();	
	newbox();
	
	myRequest = CreateXmlHttpReq(ajaxHandler);	
    myRequest.open("GET", ajaxserver+"?action=alert_promo&shop="+id+"&choice="+action);
    myRequest.send(null);
}

function delete_promoAlert (id){
	myRequest = CreateXmlHttpReq(ajaxHandler);	
    myRequest.open("GET", ajaxserver+"?action=alert_promo&shop="+id);
    myRequest.send(null);
}

function box_comment (id)
{
	overlay();	
	newbox();
	
	myRequest = CreateXmlHttpReq(ajaxHandler);	
    myRequest.open("GET", ajaxserver+"?action=box_comment&id="+id);
    myRequest.send(null);
}

function box_amico (id)
{
	overlay();	
	newbox();
	
	myRequest = CreateXmlHttpReq(ajaxHandler);	
    myRequest.open("GET", ajaxserver+"?action=box_amico&id="+id);
    myRequest.send(null);
}

function invia_segnalazione ()
{
	var link = ajaxserver+"?timestamp=" + new Date().getTime();
	var query = "&action=inviaSegnalazione";
	query += "&testo="+encodeURIComponent(document.forms['errore'].segnalazione.value);
	query += "&email="+encodeURIComponent(document.forms['errore'].email.value);
	query += "&id="+document.forms['errore'].id.value;
	
	myRequest = CreateXmlHttpReq(ajaxHandler);
    myRequest.open("POST", link)
    myRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
    myRequest.send(query);
}

function newAlert()
{
	overlay_div = document.getElementById('overlay');
	document.body.removeChild(overlay_div);
	var link = ajaxserver+"?timestamp=" + new Date().getTime();
	var query = "&action=newAlert";
	query += "&prezzo="+encodeURIComponent(document.forms['alert'].prezzo.value);	
	query += "&id="+document.forms['alert'].id.value;
	
	myRequest = CreateXmlHttpReq(ajaxHandler);
    myRequest.open("POST", link)
    myRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
    myRequest.send(query);     
    
    box_close('box-centro');
}

function newComment(zona)
{
	errore = 0;
	if (zona=="it") {
		errore_titolo = "Devi specificare un titolo per il tuo commento";
		errore_commento = "Il commento non può essere vuoto";
	}
	if (zona=="fr"){
		errore_titolo = "Vous devez intituler votre commentaire";
		errore_commento = "Le commentaire doit être renseigné";
	}
	
	for (i = 0; i < 5; i++) {
	        if (document.commento.voto[i].checked) {
	            voto = document.commento.voto[i].value;				
	        }
	}
	titolo = document.forms['commento'].titolo.value;
	testo = document.forms['commento'].testo.value;
	
	if (titolo.length==0){
		alert (errore_titolo);	
		errore = 1;
	}
	if (testo.length==0){
		alert (errore_commento);
		errore = 1;
	}
	if (errore != 1){
		/*
		overlay_div = document.getElementById('overlay');
		document.body.removeChild(overlay_div);
		*/
		newbox();
		var link = ajaxserver+"?timestamp=" + new Date().getTime();
		var query = "&action=newComment";
		query += "&titolo="+encodeURIComponent(document.forms['commento'].titolo.value);
		query += "&testo="+encodeURIComponent(document.forms['commento'].testo.value);
		query += "&voto="+voto;
		query += "&id="+document.forms['commento'].id.value;
		
		
		myRequest = CreateXmlHttpReq(ajaxHandler);
	    myRequest.open("POST", link)
	    myRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	    myRequest.send(query);
	}
        
}

function inviaAmico (zona)
{
	if (zona == "it"){
		errore_email = "Devi indicare un indirizzo email valido <br/> (del tipo nickname@server.est)"
	}
	if (zona == "fr"){
		errore_email = "Merci d'indiquer une adresse email valide <br/> (du type nickname@server.est)"
	}
	to = document.forms['amico'].to.value;
	var r = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if( r.test( to ) ){
		overlay_div = document.getElementById('overlay');
		document.body.removeChild(overlay_div);
		var link = ajaxserver+"?timestamp=" + new Date().getTime();
		var query = "&action=emailAmico";	
		query += "&to="+encodeURIComponent(document.forms['amico'].to.value);
		query += "&messaggio="+encodeURIComponent(document.forms['amico'].messaggio.value);		
		query += "&id="+encodeURIComponent(document.forms['amico'].id.value);
		query += "&from="+encodeURIComponent(document.forms['amico'].from.value);
		query += "&nome="+encodeURIComponent(document.forms['amico'].nome.value);
			
		myRequest = CreateXmlHttpReq(ajaxHandler);
	    myRequest.open("POST", link)
	    myRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	    myRequest.send(query);
	}
	else {
		alert(errore_email);
	}
}

function password (){
	overlay();	
	newbox();
	
	myRequest = CreateXmlHttpReq(ajaxHandler);	
    myRequest.open("GET", ajaxserver+"?action=box_password");
    myRequest.send(null);
}

function deleteAccount (){
	overlay();	
	newbox();
	
	myRequest = CreateXmlHttpReq(ajaxHandler);	
    myRequest.open("GET", ajaxserver+"?action=deleteAccount&password="+document.forms['delete'].password.value);
    myRequest.send(null);
}

function sendPassword (zona){
	if (zona == "it"){
		errore_email = "Devi indicare un indirizzo email valido (del tipo nickname@server.est)";
		nuova_email = "Una nuova password è stata spedita all'indirizzo indicato";
	}
	if (zona == "fr"){
		errore_email = "Merci d'indiquer une adresse email valide (du type nickname@server.est)"
	}
	email = document.forms['password'].email.value;
	var r = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if( r.test( email ) ){
		overlay_div = document.getElementById('overlay');
		document.body.removeChild(overlay_div);
		var link = ajaxserver+"?timestamp=" + new Date().getTime();
		var query = "&action=sendPassword";	
		query += "&email="+encodeURIComponent(document.forms['password'].email.value);
					
		myRequest = CreateXmlHttpReq(ajaxHandler);
	    myRequest.open("POST", link)
	    myRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	    myRequest.send(query);
	}
	else {
		alert(errore_email);
	}

}

function contattaci (zona)
{
	if (zona == "it"){
		errore_email = "Devi indicare un indirizzo email valido (del tipo nickname@server.est)";
	}
	if (zona == "fr"){
		errore_email = "Merci d'indiquer une adresse email valide (du type nickname@server.est)";
	}
	var from = document.forms['contactus'].from.value;
	var r = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if( r.test( from ) ){
		document.contactus.submit();
	} else {
		alert (errore_email);
	}
}

function contact2(zona)
{
	if (zona == "it"){
		errore_email = "Devi indicare un indirizzo email valido (del tipo nickname@server.est)";
	}
	if (zona == "fr"){
		errore_email = "Merci d'indiquer une adresse email valide (du type nickname@server.est)";
	}
	var from = document.forms['business'].from.value;
	if( checkmail( from ) ){
		document.business.submit();
	} else {
		alert (errore_email);
	}
}

function checkmail (email)
{
	var r = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if( r.test( email ) ){
		return true;
	} else {
		return false;
	}
}

function searchHandler(){
	 if (myRequest.readyState == 4 && myRequest.status == 200) {
	        e = document.getElementById("ricerca_default");
	        e.innerHTML = myRequest.responseText;        
	    }
}

function extraSearch(){	
	e = document.getElementById("ricerca_default");
	e.style.height = "175px";
	myRequest = CreateXmlHttpReq(searchHandler);	
    myRequest.open("GET", "box_cerca.php?type=advanced&timestamp=" + new Date().getTime());
    myRequest.send(null);
}

function defaultSearch (){
	e = document.getElementById("ricerca_default");
	e.style.height = "100px";
	myRequest = CreateXmlHttpReq(searchHandler);	
	
    myRequest.open("GET", "box_cerca.php?type=default&timestamp=" + new Date().getTime());
    myRequest.send(null);
}

function alertCashback (url){
	overlay();	
	newbox();
	
	myRequest = CreateXmlHttpReq(ajaxHandler);	
	myRequest.open("GET", ajaxserver+"?action=verifyCash");
    myRequest.send(null);
}

function popupOne (){
	overlay();	
	newbox();
	
	setCookie("inscriptionPopup", 1, 100);
	myRequest = CreateXmlHttpReq(ajaxHandler);	
    myRequest.open("GET", ajaxserver+"?action=popupOne");
    myRequest.send(null);
}

function goUp ()
{
	box_close('box-centro');
	setCookie("cashback", "no", "1");
	window.location.reload();
}

function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

// submitPublisherForm() is called by the LSF scripts after processing the coreg offers sending data to the LSF server.
// This function must be the only way to submit the form.
function submitPublisherForm() { 
	document.forms['account'].submit();
	} 

function checkNewsletter(){
	$("input[type='radio'][value=1]").attr("checked", "checked");
	$("input[type='radio'][value='y']").attr("checked", "checked");
	}

function checkCoreg(){
	var error = 0;
	for (i=1; i<document.getElementsByTagName('input').length; i=i+2) {
		var coreg = document.getElementsByTagName('input')[i];
		if (coreg.type == 'radio'){
			if (coreg.checked == false) {
				alert ("Essendo il sito completamente gratuito e' necessario scegliere di iscriversi anche alle newsletter proposte");
				error = 1;
				return false;
			}
		} 
	}
	if(error == 1)	{
		alert ("error : " + error);
		return false;
	}
	else {
		//alert ("true!");
		document.forms['account'].submit();
		return true;
	}
}