/**************************************************************************
 *  Fersoft.js
 *
 *  Implementacion de procedimientos.
 *
 *  Autor: Manuel Palma Martín.
 *
 *  Copyright (c) 2009 FerSoft Informática S.L.
 *  Todos los derechos reservados.
 */
 
function Trim(cValor)
{
   var x = 0;
   var y = cValor.length;
   if(y == 0 || cValor == "null") return "";
   for(x = 0; x < y; x++) if(cValor.substring(x, x + 1) != " ") break;
   for(y = y; y > 0; y--) if(cValor.substring(y - 1, y) != " ") break;
   if(x > y) cValor = "";
   else {
      if(x == y) {
         if(cValor.substring(x, x + 1) == " " ) cValor = "";
         else cValor = cValor.substring(x, x + 1);
      }  else cValor = cValor.substring(x, y);
   }
   
   return cValor;
}

var HttpProcLock = false;
var DoHttpProcLock = null;
var HttpPrimeraLlamada = true;
var HttpRefresSesion = false;
var HttpRSesion = null;
function _(sURL, sVal, sFunc, lInput)
{
   if(HttpRSesion != null) window.clearInterval(HttpRSesion);
   
   sVal = "_P_L_=" + (HttpPrimeraLlamada ? "S" : "N") + "&" + sVal
   HttpPrimeraLlamada = false;
      
   if(sFunc == undefined) sFunc = null;
   if(!HttpProcLock) {
      var oReq = null;
      if(window.XMLHttpRequest) {
         oReq = new XMLHttpRequest();
      } else if(window.ActiveXObject) {
         try {
            oReq = new ActiveXObject("Microsoft.XMLHTTP");
         } catch(e) {
            try {
               oReq = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(ex) {
               oReq = false;
            }
         }
      }
      if(typeof oReq == 'object') {
         oReq.onreadystatechange = function() {
            try {
               if(oReq.readyState == 4) {
                  if(oReq.status == 200) {
                     if(HttpRefresSesion) HttpRSesion = window.setInterval('RefrescarSesion()', 120000);
                     HttpProcLock = false;
                     if(DoHttpProcLock != null) DoHttpProcLock();
                     var oRes = JSON(oReq.responseText);
                     if(oRes.Error) {
                        if(oRes.Eval != undefined) eval(oRes.Eval);
                        if(oRes.MsjError) Alert(oRes.MsjError, oRes.Focus);
                     } else if(sFunc != null) sFunc(oRes);
                  } else {
                     if(oReq.status == 404) {
                        HttpProcLock = false;
                        if(DoHttpProcLock != null) DoHttpProcLock();
                        alert("Aplicacion no disponible.");
                     } else if(oReq.status == 500) {
                        HttpProcLock = false;
                        if(DoHttpProcLock != null) DoHttpProcLock();
                        var win = window.open("", "Error", "width=600,height=450,toolbar=no,status=no,menubar=no");
                        win.document.open();
                        win.document.clear();
                        win.document.write(oReq.responseText);
                        win.document.close();
                     }
                  }
               }
            } catch(e) {
               Alert("Error en conexion con el servidor: " + e.message);
            }
         }
         oReq.open("POST", sURL, true);
         oReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
         oReq.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
         HttpProcLock = true;
         if(DoHttpProcLock != null) DoHttpProcLock();
         oReq.send(sVal);
      } else {
         alert("Su navegador no esta soportado por esta aplicacion.");
      }
   }
}

function JSON(cVal)
{
   var funcion = new Function("return " + cVal);
   var objeto = funcion();
   return objeto;
}

function Opacidad(sId, nOpacidad)
{
   $(sId).style.opacity = "" + (nOpacidad / 100);
   $(sId).style.filter = "alpha(opacity=" + nOpacidad + ")";
}

function Fundido(sId, nValIni, nValFin, nDelta, lPrimero)
{
   if(nDelta == undefined) nDelta = 1;
   if(lPrimero == undefined) lPrimero = true;
   
   if(lPrimero) Opacidad(sId, nValIni);
   
   nValIni += (nValIni < nValFin ? nDelta : ((-1) * nDelta));
   nValIni = (nValIni > nValFin ? nValFin : nValIni);
   
   Opacidad(sId, nValIni);
   
   if(nValIni < nValFin) setTimeout("Fundido('" + sId + "', " + nValIni + ", " + nValFin + ", " + nDelta + ", false)", 1);
}

function Imprimir(sHtm)
{
   sHtm = sHtm.replace("<!--&&", "");
   sHtm = sHtm.replace("&&-->", "");
   var win = window.open("", "Imprimir", "width=" + screen.width / 1.5 + ",height=" + screen.height / 1.5 + ",toolbar=no,statusbar=no,menubar=no,resizable=yes");
   win.document.open("text/html", "replace");
   win.document.write(sHtm);
   win.document.close();
   win.print();
   win.setTimeout("close()", 2000);
}

function ExisteId(sId)
{
   return !($(sId) == null);
}

function RefrescarSesion()
{
    if(!HttpProcLock) {
       _DoHttpProcLock_ = DoHttpProcLock;
       DoHttpProcLock = null;
       _("app.aspx", "acc=" + "__REFRESCA_SESION__", function() {
          DoHttpProcLock  = _DoHttpProcLock_;
       });
   }   
}
