function apri_popup(url,lar,alt,scroll,nome) {
    if(scroll=="") scroll='auto';
    if(nome=="") nome='';
    var w = screen.width;
    var h = screen.height;
    var x = Math.round(w / 2) - Math.round(lar / 2);
    var y = Math.round(h / 2) - Math.round(alt / 2);
    window.open(url, nome, 'menubar=no, height=' + alt + ', width=' + lar + ', resizable=yes, toolbar=no, location=no, status=no, left=' + x + ', top=' + y +', scrollbars=' + scroll);
    return true;
}

function apri_popup_da_mod(url,lar,alt) {
    var w = screen.width;
    var h = screen.height;
    var x = Math.round(w / 2) - Math.round(lar / 2);
    var y = Math.round(h / 2) - Math.round(alt / 2);
    window.open(url, "", 'menubar=no, height=' + alt + ', width=' + lar + ', resizable=yes, toolbar=no, location=no, status=no, left=' + x + ', top=' + y +', scrollbars=yes');
    return true;
}

// Funzione per ottenere il valore del radio button
function readvalue(check) {
    var indice = 0;
    for (var i = 0; i < check.length; i++) {
        if (check[i].checked) indice = i;
    }
    return check[indice].value;
}

// Funzioni di apertura e chiusura
function apri(m) {
	document.getElementById(m).style.visibility = "visible";
	document.getElementById(m).style.display = "block";            
	return true;
}
function chiudi(m) {
	document.getElementById(m).style.visibility = "hidden";
	document.getElementById(m).style.display = "none";            
	return true;
}

// Funzione di controllo campi checkbox multipli
function campocontrollocheckboxmultiplo(campo,errore) {

	for (var i=0; i<campo.length; i++) {
    
    	// Campo selezionato --> ritorno
    	if (campo[i].checked) {
        	return true;
    	}
	}
	alert(errore);
	return false;
}

// Funzione di controllo campo checkbox singolo
function campocontrollocheckbox(campo,errore) {
	if (campo.checked) return true;
	else {
		alert(errore);
		return false;
	}
}


// Funzione di controllo campi select multipli
function campocontrollaselectmultiplo(campo,errore) {

	var controllo;
	for(var i=0; i<campo.length; i++) {
		select=campo[i];
		if(select.options[select.selectedIndex].value!='') controllo=1;
			else if(controllo!=1) controllo=0;
	}
	if(controllo==0) {
		alert(errore);
		return false;
	} else return true;

}

// Funzione di controllo campo select singolo
function campocontrollaselect(campo,errore) {
	if(campo.options[campo.selectedIndex].value=='') {
		alert(errore);
		campo.focus();
		return false;
	} else return true;
}

// Funzione di controllo campo testo
function campocontrollatesto(campo,errore) {
	if(campo.value=='') {
		alert(errore);
		campo.focus();
		return false;	
	}	else return true;
}

// Funzione per il controllo del login
function logincontrolla(form) {
	if(!(campocontrollatesto(form.email,'E\' necessario compilare il campo e-mail per effettuare il log-in'))) return false;
	if(!(campocontrollatesto(form.pass,'E\' necessario compilare il campo password per effettuare il log-in'))) return false;
	form.submit();
	return true;
}
function recuperopasscontrollo(form) {
	if(!(campocontrollatesto(form.email,'E\' necessario compilare il campo e-mail per effettuare il log-in'))) return false;
	form.submit();
	return true;	
}


// INIZIO AJAX
var campo;
var tipo;
var loading;
var risultato;

// Richiesta ajax
function makeRequest(id_div_output,url) {

	// Aggiusto il paramentro campo tenendo conto della presenza o meno di un iframe
	if(!(id_div_output)) id_div_output=campo;
	if(parent.document.getElementById(id_div_output)) id_div_output=parent.document.getElementById(id_div_output);
		else id_div_output=document.getElementById(id_div_output)
		
    var http_request = false;
    risultato = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
            // See note below about this line
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Giving up  Cannot create an XMLHTTP instance');
        return false;
    }
    
	if(loading!=0) id_div_output.innerHTML='<div class="loading"><img src="http://www.emis-studios.eu/webi/_layout/loading.gif" /> Attendere..</div>';
    http_request.onreadystatechange = function() { alertContents(http_request,id_div_output); };

    if(tipo==null) {
        http_request.open('GET', url, true);
        http_request.send(null);
    } else if(tipo=='POST') {
   		tipo=null;
		var parti = url.split("?");
        var parameters=parti[1];
        url=parti[0];
        http_request.open('POST', url, true);
        http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");
        http_request.setRequestHeader("Content-length", parameters.length);
        http_request.setRequestHeader("Connection", "close");
        http_request.send(parameters);
    }              
}

function alertContents(http_request,output) {

    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
			risposta=http_request.responseText;
			output.innerHTML=risposta;
	   		risposta=execJS(risposta);
		   	risultato=true;
        } else {
            output.innerHTML= http_request.status + ' There was a problem with the request.';
        }
    }

}

// Ajax color box
function ajax_box(div,indirizzo,larghezza,altezza) {
	$("#" + div).colorbox({width: larghezza, height: altezza, scrolling: true, iframe: true, href: indirizzo});
	return true;
}

// Ajax slide
function ajax_slide(div,url,img) {
	
	if ($("#" + div).is(":hidden")) {
		$("#" + div).slideDown(100);
        if(img!='') document.getElementById(div + '_img').src=img + '_0.jpg';
		makeRequest(div,url);
	} else {
		$("#" + div).slideUp(100);
        if(img!='') document.getElementById(div + '_img').src=img + '_1.jpg';
	}
	return true;
}

 
// Esegue il javascript nella chiamata ajax
function execJS(t)
{
    var p1 = 0, p2 = 0, p3 = 0, p4 = 0;
    p1 = t.indexOf("<" + "script", 0);
    if(p1 == -1) return t;
    
    p2 = t.indexOf(">", p1 + 7) + 1;
    p3 = t.indexOf("<" + "/script>", p2);
    p4 = p3 + 9;
    
    var c = t.substring(p2, p3);
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.text = c;
    document.getElementsByTagName("head")[0].appendChild(s);
    
    t = t.substring(0, p1) + t.substr(p4);
    return execJS(t);

}
