var xmlHttp;
var n;
function addFavorite(pid){
/* los estados son:
	-1 no autentificado;//no se dara nunca, se controla antes (como es directo saberlo, se ahorra una carga)
	 0 ok
	 1 error  insertando en db
	 2 ya estaba insertado
*/
n=pid;
document.getElementById("favoritos_loading_"+n).style.display="inline";

xmlHttp=GetXmlHttpObject();


if (xmlHttp==null)
  {
  alert ("¡Tu navegador no soporta AJAX!\nYour browser does not support AJAX!");
  return;
  } 
  
var url="sections/poemas/ajax/addFavorite.php";
url=url+"?pid="+n;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=addStateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
} 

function addStateChanged() { 
if (xmlHttp.readyState==4){ 
	if(xmlHttp.responseText=="0"){
		document.getElementById("favoritos_add_"+n).style.display="none";
		document.getElementById("favoritos_added_"+n).style.display="inline";
		
		document.getElementById("favoritos_added").style.display="inline";
		document.getElementById("favoritos_add").style.display="none";
}
	else if(xmlHttp.responseText=="2");
	else alert(xmlHttp.responseText);

document.getElementById("favoritos_loading_"+n).style.display="none";
document.getElementById("favoritos_ver").style.display="inline";


document.getElementById("favoritos_count").innerHTML=parseInt(document.getElementById("favoritos_count").innerHTML)+1;

//document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}

}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////


function removeFavorite(pid){
/* los estados son:
	-1 no autentificado;//no se dara nunca, se controla antes (como es directo saberlo, se ahorra una carga)
	 0 ok
	 1 error  insertando en db
*/

xmlHttp=GetXmlHttpObject()
n=pid;
document.getElementById("favoritos_loading_"+n).style.display="inline";


if (xmlHttp==null)
  {
  alert ("¡Tu navegador no soporta AJAX!\nYour browser does not support AJAX!");
  return;
  } 
  
var url="sections/poemas/ajax/addFavorite.php";
url=url+"?r_pid="+n;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=nameStateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
} 

function nameStateChanged() { 
if (xmlHttp.readyState==4){ 
	
	if(parseInt(xmlHttp.responseText)>=0){
		document.getElementById("favoritos_add_"+n).style.display="inline";
		document.getElementById("favoritos_added_"+n).style.display="none";
		
		var i=parseInt(document.getElementById("favoritos_count").innerHTML)-1;
		document.getElementById("favoritos_count").innerHTML=i;

		if (i==0){
			document.getElementById("favoritos_add").style.display="inline";
			document.getElementById("favoritos_added").style.display="none";
			document.getElementById("favoritos_ver").style.display="none";
		}
	}
				
	else alert(xmlHttp.responseText);
				
			
document.getElementById("favoritos_loading_"+n).style.display="none";
}
}





/////////////////////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////////////////////


function GetXmlHttpObject(){
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function addFavoriteDesconocido(url){
	alert("Ups, no sabemos quien eres, dinoslo para que podamos añadir el poema a tus favoritos");
	window.location.href=url;
	
}