/*
gestione AJAX della votazione
*/
var myRequest = null;

function gestload(idbz,stato)
{  //gestire i vari browser (da fare)
  if (stato==1)
  {
   document.getElementById('load'+idbz).style.visibility='visible';
  }
  else
  {
   document.getElementById('load'+idbz).style.visibility='hidden';
  } 
}

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 myHandler() {
    var rt=new String;
	var dati=new Array();
		
	if (myRequest.readyState == 4 && myRequest.status == 200) 
	{
        var response = myRequest.responseText;
        var update = new Array();

        if(response.indexOf('|') != -1) {
		   update = response.split('|');
		   ck=update[2];
           if (getCookie(ck)!=null)
    	   { 
		      delCookie(ck);
		      setCookie(ck,1,1);
		   }
		   //devo rendere "hidden" l'ID load+idbz, idbz lo estraggo da ck (dopo la stringa "voto")
		   idbz=ck.substr(4,ck.length-4);
		   gestload(idbz,0);
		   update = response.split('|');
           changeText(update[0], update[1]);
		   changeText(update[3], update[4]);
		   //se il voto è accettato modificare il rating nel DB e visualizzarlo
		   //alert(ck+" "+getCookie(ck));		  
		}
	}
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}

function vota(idbz,vt) {
    if (getCookie("voto"+idbz)==null)
	{ setCookie("voto"+idbz,0,1);}
	gestload(idbz,1);
	myRequest = CreateXmlHttpReq(myHandler);
    myRequest.open("GET","votobz.php?idbarz="+idbz+"&voto="+vt);
    myRequest.send(null);
}

