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

function SetXmlHttpRequest(vars,targetDiv){
  var ObjetoXmlHttp=GetXmlHttpObject();
  if (ObjetoXmlHttp==null) {
    alert ("Browser does not support HTTP Request")
    return
  }
  
  var url="http://"+location.host+"/includes/funciones.php";
  
  ObjetoXmlHttp.open("POST", url, true);
  ObjetoXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ObjetoXmlHttp.setRequestHeader("Content-length", vars.length);
  ObjetoXmlHttp.setRequestHeader("Connection", "close");
  ObjetoXmlHttp.onreadystatechange=function() {
    if (ObjetoXmlHttp.readyState==4)  { 
      if (targetDiv!=null) {
	      document.getElementById(targetDiv).innerHTML=ObjetoXmlHttp.responseText; 
	  }
      else {
	      newLoc=ObjetoXmlHttp.responseText;
          if (newLoc=='') location.reload(true)
          else document.location.href=newLoc;
	  }
    } 
  }
  ObjetoXmlHttp.send(vars);	
}

function jgotoComic(jid_comic) {
  SetXmlHttpRequest('funcName=gotoComic&id_comic='+jid_comic,'comic');
}

function jgotoPortada(jid_item) {
	SetXmlHttpRequest('funcName=gotoPortada&id_item='+jid_item,null);
}
function jgotoCircus(jid_comic) {
    SetXmlHttpRequest('funcName=gotoCircus&id_comic='+jid_comic,null);
}

function poblar_previo(jid_entrada,usuario) {
  var com_target=document.getElementById('com_target');
  com_target.value=jid_entrada;
  SetXmlHttpRequest('funcName=poblar_previo&id_entrada='+jid_entrada+'&usuario='+usuario,'previo_item');	
}

function previewLink() {
  var parent=document.getElementById('parent_enlace').value;
  if (parent=='codigo_comment') {
    preview();
  }
  else {
    previewB();
  }
}

function preview() {
  var txt=document.getElementById('codigo_comment').value;
  var usuario=document.getElementById('usuario').value;
  SetXmlHttpRequest('funcName=preview&txt='+txt+'&usuario='+usuario,'previo_comment');
}

function previewB() {  
  var titulo=document.getElementById('titulo').value;
  var cuerpo=document.getElementById('codigo_item').value;
  var autor=document.getElementById('usuario').value;
  
  titulo=titulo.replace(/\+/g,'%26#43;');
  cuerpo=cuerpo.replace(/\+/g,'%26#43;');
  titulo=titulo.replace(/&/g,'%26');
  cuerpo=cuerpo.replace(/&/g,'%26');
  SetXmlHttpRequest('funcName=previewB&cabecera='+titulo+'&autor='+autor+'&cuerpo='+cuerpo,'previo_item');
}

function saveNew(login) {
  var titulo=document.getElementById('titulo').value;
  var cuerpo=document.getElementById('codigo_item').value;
  var categoria=document.getElementById('categoria').value;
  titulo=titulo.replace(/&/g,'%26');
  cuerpo=cuerpo.replace(/&/g,'%26');
  //titulo=titulo.replace(/+/g,'%2B');
  //cuerpo=cuerpo.replace(/+/g,'%2B');
  var auxlist=document.getElementById('ftaglist');
  var taglist='';
  for (i=0;i<auxlist.length;i++) {
    if (auxlist[i].checked) {
      taglist=taglist+auxlist[i].value+',';
    }
  }
  SetXmlHttpRequest('funcName=guardarEntrada&login='+login+'&titulo='+titulo+'&cuerpo='+cuerpo+'&categoria='+categoria+'&taglist='+taglist,null);
}

function saveToDB(){
  var relItem=document.getElementById('com_target').value;
  var usuario=document.getElementById('usuario').value;
  var comentario=document.getElementById('codigo_comment').value;
  SetXmlHttpRequest('funcName=saveComment&relItem='+relItem+'&usuario='+usuario+'&comentario='+comentario,null);	
}

function jaddTag(){
  var tag=document.getElementById('new_tag').value;
  var usuario=document.getElementById('usuario').value;
  SetXmlHttpRequest('funcName=addTag&newTag='+tag+'&usuario='+usuario,'taglist');
}

function jkillSession(){
  SetXmlHttpRequest('funcName=killSession',null);
}