function swapImage(theimage) {
 var target=null;
 if (document.images.myimage)
     target=document.images.myimage;
 if (document.all && document.getElementById("myimage"))
     target=document.getElementById("myimage");
	 
 if(target!=null)
    target.src = theimage;
}

function trim(s){
 s += '|';
 var length = s.indexOf('|');
 var r = "";
 
 var i = 0;
 while(i<length && s.charAt(i)==' ') i++;

 var u = length-1;
 while(u>0 && s.charAt(u)==' ') u--;
 
 for(var k=i; k<=u; k++) r+=s.charAt(k);
 return r;
}

function mailThisPage(title,togo){
  mail_str = "mailto:?subject=" + title;
  mail_str += "&body=Enlace a \"" + title+"\"";
  mail_str += ": " + togo; 
  location.href = mail_str;
}

function goPage(thepage){
  document.location = thepage;
}

function goiPreview(image,iwidth,iheight){
 var rwidth=iwidth+40;
 var rheight=iheight+60;
 if(rheight>600) rheight=800;
 window.open("weboc_ipreview.php?w="+iwidth+"&h="+iheight+"&i="+image,"","scrollbars=1, resizable=0, width="+rwidth+", height="+rheight+", status=0, location=0, menubar=0");
}

function goCloseSession(){
 var agree=confirm("Te dispones a cerrar la sesión. ¿Desea continuar?");
 if (agree)
    document.location = "weboc_closesession.php";
}

function goPrintText(id,pa,textid){
 window.open("textpreview.php?id="+id+"&pa="+pa+"&textid="+textid,"","scrollbars=1, resizable=0, width=400, height=600, status=0, location=0, menubar=0");
}

function ValidateSearch( theform ) {
   theform.q.value = trim(theform.q.value);
   if (theform.q.value == '') {
      alert('Debes introducir un término a buscar. Imposible continuar.');
      return false;
   }
   return true;
}

function ValidateForumSearch( theform ) {
   theform.qf.value = trim(theform.qf.value);
   if (theform.qf.value == '') {
      alert('Debes introducir un término a buscar. Imposible continuar.');
      return false;
   }
   return true;
}

function ValidateEmail( theform ) {
   theform.email.value = trim(theform.email.value);
   theform.pword.value = trim(theform.pword.value);

   var s = theform.email.value;
   if (s == '') {
      alert('Debes introducir una dirección de correo. Imposible continuar.');
      return false;
   }

   var posAT = s.indexOf('@');
   if (posAT == -1) {
      alert('Debes introducir una dirección de correo correcta. Imposible continuar.');
      return false;
   }

   var s = theform.pword.value;
   if (s == '') {
      alert('Debes introducir la palabra de control de presencia. Imposible continuar.');
      return false;
   }

   return true;
}

function goUnsubscribe(){
  document.forms['bulletin_uform'].email.value = document.forms['bulletin_aform'].email.value;
  document.forms['bulletin_uform'].pword.value = document.forms['bulletin_aform'].pword.value;
  if(ValidateEmail( document.forms['bulletin_uform'] )){
     var agree=confirm("Te dispones a dar de baja a '"+document.forms['bulletin_uform'].email.value+"' de nuestra lista de correo. ¿Deseas continuar?");
     if (agree)
         document.forms['bulletin_uform'].submit(); 
  }
}

function ValidateLogin( theform ) {
   theform.login.value = trim(theform.login.value);
   theform.psw.value = trim(theform.psw.value);
   theform.email.value = trim(theform.email.value);
   theform.pword.value = trim(theform.pword.value);
   
   var s = theform.login.value;
   if (s == '') {
      alert('Debes introducir tu nombre de usuario. Imposible continuar.');
      return false;
   }
   if(s.length<4){
      alert("La longitud del nombre de usuario debe ser mayor o igual a 4 caracteres");
      return false;
   }
   
   var s = theform.psw.value;
   if (s == '') {
      alert('Debes introducir tu contraseña. Imposible continuar.');
      return false;
   }
   if(s.length<4){
      alert("La longitud de la contraseña debe ser mayor o igual a 4 caracteres");
      return false;
   }   
   
   var s = theform.email.value;
   if (s == '') {
      alert('Debes introducir tu dirección de correo. Imposible continuar.');
      return false;
   }

   var posAT = s.indexOf('@');
   if (posAT == -1) {
      alert('Debes introducir una dirección de correo correcta. Imposible continuar.');
      return false;
   }

   var s = theform.pword.value;
   if (s == '') {
      alert('Debes introducir la palabra de control de presencia. Imposible continuar.');
      return false;
   }

   return true;
}

 function mostrar(id) {
        if(document.getElementById){  //Soporta DOM
           var obj = document.getElementById(id);
           var image = document.getElementById('im'+id);
           if(obj.style.display == ""){
                obj.style.display = "none";
                if(image!=null)
                   image.src = "res/plus.gif";
           }
           else{
                obj.style.display = "";
                if(image!=null)
                   image.src = "res/minus.gif";
           }
        }
        else if (document.layers){
           if(document[id].display == "")
                document[id].display = "none";
           else
                document[id].display = "block";
        }
        else if (document.all){
           var image = document.all['im'+id];
           if(document.all[id].style.display == ""){
                document.all[id].style.display = "none";
                if(image!=null)
                   image.src = "res/plus.gif";
           }else{
                document.all[id].style.display = "";
                if(image!=null)
                   image.src = "res/minus.gif";
           }
        }
        window.status=' ';
 }

function actualizarContenido(idcapa,contenido){
     if (document.getElementById)
		 document.getElementById(idcapa).innerHTML=contenido;
	 else if (document.layers)
		 document[idcapa].innerHTML=contenido;
	 else if (document.all)
		 document.all[idcapa].innerHTML=contenido;
}

function llamarServidor(url, vars){
     var xml = null;
     try{
         xml = new ActiveXObject("Microsoft.XMLHTTP");
     }catch(expeption){
         xml = new XMLHttpRequest();
     }
     xml.open("GET",url + vars, false);
     xml.send(null);
     if(xml.status == 404) alert("Error 404: Url incorrecta.");
     return xml.responseText;
}

function procesar(id){
    var response = llamarServidor("weboc_preview.php","?ajax=setheader&content_id="+id);
    var did = 'html'+id; 
    actualizarContenido(did,response);
}

function procesarConLoading(id,ptime){
 //document.getElementById('html'+id).innerHTML = '<center><br>Cargando contenido en portada...</center>';
 document.getElementById('html'+id).innerHTML = '<center><br><img border="0" src="res/ico_loading.gif" width="32" height="32"><br>Cargando contenido...<br><br>&nbsp;</center>';
 setTimeout("procesar("+id+");",ptime);
}
