function getXMLHttp() {
var xmlhttp = null;
if (window.ActiveXObject) {
if (navigator.userAgent.toLowerCase().indexOf('msie 5') != -1) {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
} else {
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
}
}
if (!xmlhttp && typeof(XMLHttpRequest) != 'undefined') {
xmlhttp = new XMLHttpRequest()
}
return xmlhttp
}
objHTTP = getXMLHttp();
objHTTP.open('GET', 'indirizzo.php', true);
objHTTP.onreadystatechange = function() {elaboraRisposta()}
objHTTP.send(null);
function elaboraRisposta() {
if (objHTTP.readyState == 4) {
	var risposta=objHTTP.responseTEXT;
	if(risposta!=''){
	document.getElementById('risultato').innerHTML=risposta;
	}
}
}

function rank(media, id){
	var i=media;
	while(i<=id) {
		//document.getElementById("rank_"+i).style.opacity=1;
		$("#rank_"+i).css({ opacity: 1 });
		i++;
	}
}

function rank_out(media, id){
	var ia=5;
	while(ia>media) {
		//document.getElementById("rank_"+ia).style.opacity=0.2;
		$("#rank_"+ia).css({ opacity: 0.2 });
		ia--;
	}
}

function addrank(aid, voto)
{
var xmlHttp = getXMLHttp();
//document.write('ciao');
xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
	  addrank_in(xmlHttp.responseText);
    }
  }

xmlHttp.open("POST", "func/functions.php?a=addrank", true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send("aid="+aid+"&voto="+voto);

}

function addrank_in(response){
	if(response==1){
		location.reload(true);
	}
}


function atcoup(poid){
$.ajax({
type: "GET",
url: "func/functions.php?a=atcoup",
data: "poid="+poid
});
}

function disatcoup(poid){
$.ajax({
type: "GET",
url: "func/functions.php?a=disatcoup",
data: "poid="+poid
});
}

function checkreg(input, num_min) {
	
	e = document.getElementById(input);
	d = document.getElementById(input+'_1');
	s = document.getElementById('send');
	err = document.getElementById('error');
	
	rso=document.getElementById('rsociale');
	piv=document.getElementById('piva');
	loc=document.getElementById('localita');
	tel=document.getElementById('telefono');
	mai=document.getElementById('email');
	use=document.getElementById('username');
	pas=document.getElementById('password');
	pas1=document.getElementById('password_1');
	rep=document.getElementById('repassword');
	rep1=document.getElementById('repassword_1');
	ris=document.getElementById('risultato');
	

if(input=='send'){
	if ( (rso.value=='') || (piv.value=='') || (loc.value=='') || (tel.value=='') || (mai.value=='') || (use.value=='') || (pas.value=='') || (rep.value=='')){
		err.innerHTML = 'Mi dispiace ma hai lasciato dei campi incompleti.'; 
		err.style.color='red';
		checkreg('rsociale');
		checkreg('piva');
		checkreg('localita');
		checkreg('telefono');
		checkreg('email');
		checkreg('username');
		checkreg('password');
		checkreg('repassword');
		return false;
	}
	
else if(input=='send' && document.getElementById('privacy').checked==false){
		err.innerHTML = 'Spiacente, devi accettare le condizioni.'; 
		err.style.color='red';
		return false;
	}
else if(input=='send' && document.getElementById('password').value!=document.getElementById('repassword').value){
		err.innerHTML = 'Spiacente, le password non coincidono.'; 
		err.style.color='red';
		rep.style.border='1px solid red';
		
		pas.style.border='1px solid red';
		return false;
	}
	else{
		return true;
	}
}
else{	
	if(input=='password' || input=='repassword' && document.getElementById('password').value!='' && document.getElementById('repassword').value!=''  && document.getElementById('password').value!=document.getElementById('repassword').value){
		rep.style.border='1px solid red';
		
		pas.style.border='1px solid red';
	}
	else if(input=='password' || input=='repassword' && document.getElementById('password').value!='' && document.getElementById('repassword').value!='' && document.getElementById('password').value==document.getElementById('repassword').value){
		rep.style.border='1px solid green';
		
		pas.style.border='1px solid green';
	}
	
	if(input!='password' && input!='repassword' && e.value==''){
		e.style.border='1px solid red';
		}
	else{
		e.style.border='1px solid green';
		}
}
}

function srclist(idinput)
{	
  ricerca=document.getElementById(idinput).value;
  $("#"+idinput).autocomplete({
		source: "func/autocomplete.php?d="+idinput+"&ric="+ricerca,
		//select: function(event, ui){
			//$("#"+idinput+"_v").val(ui.item.id);
		//},
		minLength:3
      });

}

function rendivis(id){
	document.getElementById("dettaz").style.display='none';
	document.getElementById("gall").style.display='none';
	document.getElementById("contact").style.display='none';
	
	$("#gall_b").removeClass("selected_Sc");
	$("#dettaz_b").removeClass("selected_Sc");
	$("#contact_b").removeClass("selected_Sc");
	
	function vis(id){
		$("#"+id+"_b").addClass("selected_Sc");
		$("#"+id).fadeIn("slow");
	}
	
	setTimeout(vis(id),6000);	
}

function verifyform(inputf) {
		
	if(document.getElementById('nome').value==""){
		jAlert("Attenzione, il campo Nome non pu&ograve; restare vuoto");
		return false;
	}
	else if(document.getElementById('telefono').value==""){
		jAlert("Attenzione, il campo Telefono non pu&ograve; restare vuoto");
		return false;
	}
	else if(document.getElementById('email').value==""){
		jAlert("Attenzione, il campo E-mail non pu&ograve; restare vuoto");
		return false;
	}
	else if(document.getElementById('testo').value==""){
		jAlert("Attenzione, il campo Testo non pu&ograve; restare vuoto");
		return false;
	}
	else if(document.getElementById('security_code').value==""){
		jAlert("Attenzione, il campo Codice di verifica non pu&ograve; restare vuoto");
		return false;
	}
	else if(document.getElementById('cond').checked==false){
		jAlert("Attenzione, devi accettare le condizioni");
		return false;
	}
	else{
		return true;
	}
}

function scorricoupon(id, direzione){
		document.getElementById('boxcou').innerHTML=document.getElementById('lista_'+id).innerHTML;
}

function printcoup(poid)
{
var xmlHttp = getXMLHttp();
//document.write('ciao');
xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
	  printcoup_a(xmlHttp.responseText);
    }
  }
  
xmlHttp.open("GET", "func/functions.php?a=printcoupon&poid="+poid, true);
xmlHttp.send(null);

}

function printcoup_a(response)
{
	 var windowFeatures = "width=500,height=300,menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes";
    child1 = window.open("about:blank", "subWind", windowFeatures);
    child1.document.write('<html><head><title></title><link rel="stylesheet" type="text/css" href="../css/style.css" /><link rel="stylesheet" href="css/style.css" type="text/css" /><body style="background:#FFF;">'+response+'</body></head></html>');
  /*var newWin=window.open('','Stampa Coupon','width=400,height=300');
  newWin.document.open();
  newWin.document.write(response);*/
  child1.document.close();
  child1.print();
  setTimeout(function(){child1.close();},100);
}

function xmap(aid)
{
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
	  ret_xmap(xmlHttp.responseText, aid);
    }
  }
  
xmlHttp.open("GET", "func/funxmappa.php?a=xmappa&aid="+aid, true);
xmlHttp.send(null);

}

function ret_xmap(response, aid)
{	
	//document.getElementById(hid+'.nome').innerHTML=response;
	//$("#"+hid+".nome").html("ciao");
	var dati=response.split("--t--");
	$('div#desc_'+aid).html(dati[0]+"<br />"+dati[1]+"&nbsp;&nbsp;"+dati[2]+"<br />"+dati[3]);
	$('div#catmap_'+aid).html("<span>"+dati[4]+" - "+dati[5]+"</span>");
}
