var myReq = null;
var url=null;
function AJAXReq(methodtype,url,asynch,resp){
 if(window.XMLHttpRequest){
  myReq = new XMLHttpRequest();
 }else if(window.ActiveXObject){
   myReq = new ActiveXObject("Msxml2.XMLHTTP");
    if(!myReq){
      myReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
 }
 
 if(myReq){
  if(methodtype.toLowerCase() != "post"){
    execfunc(methodtype,url,asynch,resp);
  }else{
    var args = arguments[4];
    execfunc(methodtype,url,asynch,resp,args);
  }
 }else{
   alert("Your browser doesn't support AJAX utilities");
 }
}

function execfunc(methodtype,url,asynch,resp){
  try{
    myReq.onreadystatechange = resp;
    myReq.open(methodtype,url,asynch);
    
    if(methodtype.toLowerCase() == "post"){
      myReq.setRequestHeader("Content-Type",
                             "application/x-www-form-urlencoded; charset=UTF-8");
      myReq.send(arguments[4]);
    }else{
      myReq.send(null);
    }
  }catch(errv){
    alert("Enable to contact the server\nError: "+errv.message);
  }
}

function PreparaDati(form_specifico){
  stringa = "";
  var form = document.forms[form_specifico];
  var numeroElementi = form.elements.length;
 
  for(var i = 0; i < numeroElementi; i++){
    if(i < numeroElementi-1){
      stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
    }else{
      stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value);
    } 
  }
}
/*handleResponse GENERICO
function handleResponse(id){
 if(myReq.readyState == 4 && myReq.status == 200){
  document.getElementById(id).innerHTML = encodeURIComponent(myReq.responseText);
 }else{
  alert("Errore durante l'invio dei dati!");
 }
}
*/
									//****************** 									 CHIAMATE AJAX

//Carica in archivio i nuovi video del produttore passato tramite chiamata GET
function GetAjaxVideo(id_div,playlist){
  loaderDettagli(id_div);
  url='carica_video.php?playlist=' + playlist;
  url += (url.indexOf("?") == -1) ? "?" : "&";
  url += "rand="+escape(Math.random());
  setTimeout("AJAXReq('GET',url,true,function (){handleResponse1('"+id_div+"')});", 1000);
}


// INVIA LA MAIL PER LA CONDIVISIONE DEL VIDEO AD UN AMICO
function suggerisciAJAX(id_div, form){
  document.getElementById(id_div).style.display = 'block';
  document.getElementById('segnala_video').style.display = 'none';
  PreparaDati(form);
  setTimeout("AJAXReq('POST','serverAJAX.php?action=suggerisci&videoID=' + top.videoID,true,function (){handleResponseSuggerimento('"+id_div+"')},stringa);", 1000);
}
function handleResponseSuggerimento(id){
 if(myReq.readyState == 4 && myReq.status == 200){
	 //alert(parseInt(myReq.responseText,10));
	if(parseInt(myReq.responseText,10) == 1){
		document.getElementById(id).style.display = 'none';
		document.getElementById('conferma_strumenti').style.display = 'block';
		ripulisciCampi();
		setTimeout("document.getElementById('conferma_strumenti').style.display = 'none';", 3000);
	}else{
		document.getElementById(id).style.display = 'none';
		document.getElementById('errore_strumenti').style.display = 'block';
		setTimeout("document.getElementById('errore_strumenti').style.display = 'none';", 3000);
	}
 }
}// fine

// COMMENTO VIDEO
function commentaAJAX(id_div, form){
  document.getElementById(id_div).style.display = 'block';
  document.getElementById('submit_commenti').style.display = 'none';
  PreparaDati(form);
  setTimeout("AJAXReq('POST','serverAJAX.php?action=commenta&videoID=' + top.videoID + '&utente=' + login.utente ,true,function (){handleResponseCommenta('"+id_div+"')},stringa);", 1000);
}
function handleResponseCommenta(id){
 if(myReq.readyState == 4 && myReq.status == 200){
	 	 //alert(parseInt(myReq.responseText,10));
	if(parseInt(myReq.responseText,10) == 1){
		document.getElementById(id).style.display = 'none';
		//document.getElementById('conferma_strumenti').style.display = 'block';
		ripulisciCampi();
		//setTimeout("document.getElementById('conferma_strumenti').style.display = 'none';", 3000);
		listaCommentiAJAX('wait_update_commenti', 'commento_video_form');
	}else{
		document.getElementById(id).style.display = 'none';
		document.getElementById('errore_strumenti').style.display = 'block';
		setTimeout("document.getElementById('errore_strumenti').style.display = 'none';", 3000);
	}
 }
}// fine

// SEGNALAZIONE DEL VIDEO ALL'AMMINISTRATORE
function segnalaAJAX(id_div, form){
  document.getElementById(id_div).style.display = 'block';
  document.getElementById('elimina_video').style.display = 'none';
  PreparaDati(form);
  setTimeout("AJAXReq('POST','serverAJAX.php?action=elimina&videoID=' + top.videoID ,true,function (){handleResponseEliminazione('"+id_div+"')},stringa);", 1000);
}
function handleResponseEliminazione(id){
 if(myReq.readyState == 4 && myReq.status == 200){
	 	 //alert(parseInt(myReq.responseText,10));
	if(parseInt(myReq.responseText,10) == 1){
		document.getElementById(id).style.display = 'none';
		document.getElementById('conferma_strumenti').style.display = 'block';
		ripulisciCampi();
		setTimeout("document.getElementById('conferma_strumenti').style.display = 'none';", 3000);
	}else{
		document.getElementById(id).style.display = 'none';
		document.getElementById('errore_strumenti').style.display = 'block';
		setTimeout("document.getElementById('errore_strumenti').style.display = 'none';", 3000);
	}
 }
}// fine

// LISTA COMMENTI
function listaCommentiAJAX(id_div, form){

  document.getElementById(id_div).style.display = 'block';
  PreparaDati(form);
  setTimeout("AJAXReq('POST','serverAJAX.php?action=lista_commenti&videoID=' + top.videoID ,true,function (){handleResponseListaCommenti('"+id_div+"')},stringa);", 500);
}
function handleResponseListaCommenti(id){
 if(myReq.readyState == 4 && myReq.status == 200){
		document.getElementById('lista_commenti').innerHTML = myReq.responseText;
		document.getElementById(id).style.display = 'none';
		if(login.autenticazione == 1){
			document.getElementById('submit_commenti').style.display="block";
		}else{
			document.getElementById('submit_commenti').style.display="none";
		}
 }
}// fine

