/* <![CDATA[ */

/*------------------------------------------------------------------------------------*/  
/*------------------------------------------------------------------------------------*/  
/*------------------------------------------------------------------------------------*/  
/*------------------------------------------------------------------------------------*/  
/*------------------------------------------------------------------------------------*/  
/*------------------------------------------------------------------------------------*/  
/*
// Creamos el objeto dhtmlHistory
window.dhtmlHistory.create();
 
// Esta función es llamada cada vez que el usuario navegue con los botones atras / adelante, recibe como parametro la entrada al historial asi como la informacion asociada
function funcionQueEscuchaLosCambios(newLocation, historyData) 
{
              
              alert(newLocation);           
}
window.onload = function() 
{
	 // Inicializamos y agregamos nuestro Listener
//	 dhtmlHistory.initialize();
	 //dhtmlHistory.addListener(funcionQueEscuchaLosCambios);
}
*/


var ajax = null;
var idWaiting = -1;
var nAction = 0;
var lastAction = new Array();
var lastBytes = new Array();
var desconectar = false;
var eventIsFiredFromElement;
var bWaiting = false;
var nHistory = 1;
var bDefineHttpRequest = false;
var bUseXMLHttpRequest = bDefineHttpRequest;
//var bOneTime = false;
var bUpdate =  true;

document.onkeypress = processKey;
document.onclick = listenerClick;

/*------------------------------------------------------------------------------------*/
function listenerClick(e) 
{
  if (e == null) 
    eventIsFiredFromElement = event.srcElement;
  else 
    eventIsFiredFromElement = e.target;
}

/*------------------------------------------------------------------------------------*/
function detectBrowser() 
{
  var ie = document.all != undefined;
  var opera = window.opera != undefined;

  if (opera) return "opera";
  if (ie) return "ie";
  if ((window) && (window.netscape) && (window.netscape.security)) {
    if (window.XML) {
      return "firefox15";
    }
    else return "firefox10";
  }
  return "ie"; /* Si no sabemos que navegador es, devolvemos ie.*/
}

/*------------------------------------------------------------------------------------*/
function scrollText() 
{
  if (document.getElementById('idBodyText'))
    document.getElementById('idBodyText').scrollTop += document.getElementById('idBodyText').scrollHeight;
}
  
/*------------------------------------------------------------------------------------*/
function processKey(e)
{
  if (e == null)
    e = window.event;

  if (e != null && e.keyCode == 13) {
    var srcEl = e.srcElement ? e.srcElement : e.target;
    srcEl.blur();
  }
}

/*------------------------------------------------------------------------------------*/
function waitingAjax() 
{
  var obj;
  if (window.event)
    obj = window.event.srcElement;
  else
    obj = eventIsFiredFromElement;
  
  if (obj != null && obj != undefined) 
  {
    obj.style.opacity = 0.25;
    obj.style.filter = 'alpha(opacity=' + 25 + ')';
    /*var object = document.getElementById("idBodyCapa");
    object.style.background = 'transparent url("../images/loader.gif") no-repeat fixed top center';*/
  }
}

/*------------------------------------------------------------------------------------*/
function showLastAction()
{
  var buffer = "DEBUG:<br />";
  var objSpan = document.createElement("span");
  objSpan.id = "spanDebug";
  objSpan.className ="dynamicSpan";
  
  for (var x = nAction; x > nAction - 5 && x > 0; x--)
  {
    if (lastAction[x])
    {
      buffer += "" + x + ".- Function: [" + lastAction[x] + "] <font color=\"Blue\">" + lastBytes[x] + "</font> bytes";
      buffer += "<br />";
    }
  }
  if (objSpan != null)
    objSpan.innerHTML = buffer;
  
  document.body.appendChild(objSpan);
}

/*------------------------------------------------------------------------------------*/
function wait(msecs)
{
  var start = new Date().getTime();
  var cur = start;
  while(cur - start < msecs)
  {
    cur = new Date().getTime();
  }
} 

/*------------------------------------------------------------------------------------*/
function opacity(id, opacStart, opacEnd, millisec)
{ 
    var speed = Math.round(millisec / 100); 
    var timer = 1; 

    if(opacStart > opacEnd) 
    { 
        for(i = opacStart; i >= opacEnd; i--) 
        { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    }
     else if(opacStart < opacEnd) 
     {
      
        for(i = opacStart; i <= opacEnd; i++) 
        { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
     } 
} 

/*------------------------------------------------------------------------------------*/
function changeOpac(opac, id) 
{ 
    var object = document.getElementById(id).style; 
    object.opacity = (opac / 100); 
    object.MozOpacity = (opac / 100); 
    object.KhtmlOpacity = (opac / 100); 
    object.filter = "alpha(opacity=" + opac + ")"; 
}

/*------------------------------------------------------------------------------------*/
function AjaxCreate()
{
  var objetoAjax = null;
  try 
  {
    objetoAjax = new XMLHttpRequest();
  }
  catch (trymicrosoft) 
  {
    try 
    {
      objetoAjax = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (othermicrosoft) 
    {
      try 
      {
        objetoAjax = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (failed) 
      {
        objetoAjax = false;
      }
    }
  }

  if (!objetoAjax)
    alert("Error initializing XMLHttpRequest!");

  return objetoAjax;
}

/*------------------------------------------------------------------------------------*/
function updatePage() 
{
  if (ajax.readyState == 1)
  {
    Waiting();
    }
  else if (ajax.readyState == 4)
  {
  //alert(ajax.status);
    if (ajax.status == 200) 
    {
      UnWaiting();
//      dhtmlHistory.add("a"+(nHistory++), document.getElementById("idBodyCapa").innerHTML);
      
      if (bUpdate == true)
      {
        var divBodyContent = ajax.responseText.split("<div id=\"idBodyCapa\">");
        var content = divBodyContent[1];
        
        if (content != undefined)
        {
          if (document.getElementById("idBodyCapa") != null)
            document.getElementById("idBodyCapa").innerHTML = content;

          SetTitle();
          lastBytes[nAction] = ajax.responseText.length;
          /*showLastAction();*/
          scrollText();
        }
      }
    }
    else
    {
    //alert("reload");
      window.location.reload();
      }
  }
  else
    UnWaiting();
//    bOneTime = false;
}

/*------------------------------------------------------------------------------------*/
function FormatLocation(sLocation)
{
  if (g_PathSession != "")
    sLocation += '&pathsession=' + g_PathSession;
  
  if (bUseXMLHttpRequest)
    sLocation += "&rand=" + Math.random();

  return sLocation;
}

/*------------------------------------------------------------------------------------*/
function SendCommandNoUpdate(href, async, history)
{
  var ret = 1;
  //if (bOneTime == false) 
  {
    //bOneTime = true;  

    var sLocation = href;
    sLocation = FormatLocation(sLocation);

    lastAction[nAction++] = sLocation;

    if (bUseXMLHttpRequest) 
    {
      if (ajax == null)
        ajax = AjaxCreate();
      
      //-- Por defecto es mejor seguir utilizando el false. Mas seguro
      ajax.open('GET', sLocation, /*async*/true);
      ajax.onreadystatechange = updatePage;
      ajax.send(null);
    }
    else
    {
      Waiting();
      location.href = sLocation;
    }
  }
  //else
  //  alert("Se nota tol click!!!!");
   
  return ret;
}

/*------------------------------------------------------------------------------------*/
function FunctionDetalle(ev, strCod)
{
    StopPropagation(ev);
    var sLocation = './' + g_NameCGI + '?FunctionDetalle&id=' + g_SessionID + '&cod=' + strCod;

    SendCommandNoUpdate(sLocation, true, false);
}

/*------------------------------------------------------------------------------------*/                               
function ModalDialog(ev, href, nombre) 
{
  var nSizeX = 400;
  var nSizeY = 400;
  var posx = (screen.width/2)-(nSizeX/2);
  var posy = (screen.height/2)-(nSizeY/2);

  if (!ev) 
    ev = window.event;
  if (ev.screenX|| ev.screenY) 	
	{
    posx = ev.screenX;
    posy = ev.screenY;
	}
	else 
	{
    if (ev.clientX || ev.clientY) 	
    {
      posx = ev.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      posy = ev.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
    }
  }

  window.showModalDialog(href, nombre, "dialogWidth:" + nSizeX + "px;dialogHeight:" + nSizeY + "px;dialogTop:" +
                        posy + ";dialogLeft:" + posx + ";toolbar:0; status:0;");
}

/*------------------------------------------------------------------------------------*/
function ChangeLocation(href)
{
  var sLocation = href;
  if (g_PathSession != "")
    sLocation += '&pathsession=' + g_PathSession;

  location.replace(sLocation);
}
    
/*------------------------------------------------------------------------------------*/
function SetOpacity(value) 
{
  var bodycontent = document.getElementById('divBodyContent');
  if (bodycontent != null)
	{
	  bodycontent.style.opacity = value/10;
	  bodycontent.style.filter = 'alpha(opacity=' + value*10 + ')';
  }
}

/*------------------------------------------------------------------------------------*/
function DoUnWaiting() 
{
  var waiting = document.getElementById('idWaiting');
  if (waiting != null)
  {
    bWaiting = false;
    waiting.style.visibility = "hidden";
    waiting.style.display = 'none';
    waiting.style.cursor = 'auto';
  }
}

/*------------------------------------------------------------------------------------*/
function UnWaiting() 
{
  var object = document.getElementById("idBodyCapa")
  if (object != null)
  {
    object.style.background = "";
  }
  clearTimeout(idWaiting);
  DoUnWaiting();
  idWaiting = -1;
}

/*------------------------------------------------------------------------------------*/
function GetScrollTop()
{
	if(window.pageYOffset)
		return window.pageYOffset;
  else 
	  return  Math.max(document.body.scrollTop,document.documentElement.scrollTop);
}

/*------------------------------------------------------------------------------------*/
function GetScrollLeft()
{
	if(window.pageXOffset)
		return window.pageXOffset;
 else 
		return  Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
}

/*------------------------------------------------------------------------------------*/
function DoWaiting() 
{ 
  var waiting = document.getElementById('idWaiting');
  
  if (waiting != null && idWaiting > 0)
  {
  waiting.style.visibility = "visible";
    waiting.style.display = "block";
    waiting.style.opacity = 0.4;
	  waiting.style.filter = "alpha(opacity=" + 40 + ")";
	  waiting.style.cursor = "wait";
    waiting.style.top = GetScrollTop() + 2 + "px";
    waiting.style.left = GetScrollLeft() + 2 + "px";
    waiting.style.width = "99%";
    waiting.style.height = "99%";
    //waiting.style.background = "#dddddd url('../images/wait.gif') no-repeat scroll center center";
    waiting.style.background = "#dddddd no-repeat scroll center center";
    bWaiting = true;
  }
}

/*------------------------------------------------------------------------------------*/
function Waiting() 
{
  //waitingAjax();
  idWaiting = setTimeout("javascript:DoWaiting();", 1);
}

/*------------------------------------------------------------------------------------*/
function PopupCenter(pageURL, title, w, h) 
{
  var left = (screen.width/2)-(w/2);
  var top = (screen.height/2)-(h/2);
  var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 

/*------------------------------------------------------------------------------------*/
function FunctionImage(obj)
{
  /*var divBodyContent = document.body.innerHTML.split("<div id=\"idBodyCapa\">");
  var content = divBodyContent[1];
//  alert(content);
  dhtmlHistory.add("a"+(nHistory++), content);*/
//  dhtmlHistory.add("a"+(nHistory++), document.getElementById("idBodyCapa").innerHTML);
}

/*-- Mostrar una imagen en un punto determinado.*/
/*function FunctionImage(nId , sType)
{
  var sLocation = './' + g_NameCGI + '?FunctionImage&id=' + g_SessionID + '&idimage=' + nId + '&typeimage=' + sType;
  sLocation = FormatLocation(sLocation);
  
  PopupCenter(sLocation, "Abrir", 600, 600);
}
*/

/*------------------------------------------------------------------------------------*/  
function FunctionCss(sPath)
{
  var sLocation = './' + g_NameCGI + '?FunctionCss&id=' + g_SessionID + /*'&op=' + g_Operation +*/ '&path=' + sPath;
  sLocation = FormatLocation(sLocation);
  
  ChangeLocation(sLocation);
}

/*------------------------------------------------------------------------------------*/  
function StopPropagation(e) 
{ 
  e.cancelBubble = true; 
  if (e.stopPropagation) 
    e.stopPropagation();
}

/*------------------------------------------------------------------------------------*/  
function FunctionSetField(ev, nField, strValue, strCod) 
{
    StopPropagation(ev);
    var sLocation = './' + g_NameCGI + '?FunctionSetField&id=' + g_SessionID + '&field=' + nField + '&value=' + strValue + '&cod=' + strCod;
    bUseXMLHttpRequest = true;
    SendCommandNoUpdate(sLocation, true, false);
    bUseXMLHttpRequest = bDefineHttpRequest;
    SetTitle();
}

/*------------------------------------------------------------------------------------*/  
function FunctionUpload()
{
  Waiting();
  document.getElementById('formUpload').submit();
}

/*------------------------------------------------------------------------------------*/  
function FunctionDelete(ev, obj, strCod)
{
  StopPropagation(ev);
  var sLocation = './' + g_NameCGI + '?FunctionDelete&id=' + g_SessionID + '&cod=' + strCod;

  if (confirm('¿Desea borrar este registro?'))
  {
    SendCommandNoUpdate(sLocation, true, false);
  }
}

/*------------------------------------------------------------------------------------*/  
function FunctionChangeSchema(nSchema)
{
    var sLocation = './' + g_NameCGI + '?FunctionChangeSchema&id=' + g_SessionID + '&schema=' + nSchema;

    bUseXMLHttpRequest = false;
    SendCommandNoUpdate(sLocation, true, true);
    bUseXMLHttpRequest = bDefineHttpRequest;
}

/*------------------------------------------------------------------------------------*/  
function FunctionIncidencia(ev, strCod)
{
    StopPropagation(ev);
    
    var sLocation = './' + g_NameCGI + '?FunctionIncidencia&id=' + g_SessionID + '&cod=' + strCod;
    bUpdate = false;
    SendCommandNoUpdate(sLocation, false, false);
    bUpdate = true;
    FunctionChangeSchema(12855);
}

/*------------------------------------------------------------------------------------*/  
function FunctionCheck(ev, nCol, nRow)
{
    StopPropagation(ev);
    var sLocation = './' + g_NameCGI + '?FunctionCheck&id=' + g_SessionID + '&col=' + nCol + '&row=' + nRow;
    //bUseXMLHttpRequest = true;
    SendCommandNoUpdate(sLocation, true, false);
    //bUseXMLHttpRequest = bDefineHttpRequest;
    SetTitle();
}

/*------------------------------------------------------------------------------------*/  
function FunctionOrder(nCol)
{
    var sLocation = './' + g_NameCGI + '?FunctionOrder&id=' + g_SessionID + '&col=' + nCol;
    bUseXMLHttpRequest = true;
    SendCommandNoUpdate(sLocation, true, false);
    bUseXMLHttpRequest = bDefineHttpRequest;
    SetTitle();
}

/*------------------------------------------------------------------------------------*/  
function FunctionFind()
{
  var element = document.getElementsByName('FindName')[0];
  if (element != null)
  {
    var sLocation = './' + g_NameCGI + '?FunctionFind&id=' + g_SessionID + /*'&op=' + g_Operation +*/ '&cod=' + element.value;
    SendCommandNoUpdate(sLocation, true, false);
  }
}

/*------------------------------------------------------------------------------------*/  
function FunctionAut(sPath)
{
  var elementUser = document.getElementsByName('UserName')[0];
  var elementPass = document.getElementsByName('PasswordName')[0];
  
  if (elementUser != null && elementPass != null)
  {
    var sLocation = './' + g_NameCGI + '?FunctionLogin&path=' + sPath + '&user=' + elementUser.value + '&password=' 
                      + elementPass.value + '&cginame=' + g_NameCGI;
    SendCommandNoUpdate(sLocation, true, false);
  }
}

/*------------------------------------------------------------------------------------*/  
function FunctionGoTree(sCod)
{
  var sLocation = './' + g_NameCGI + '?FunctionGo&id=' + g_SessionID + /*'&op=' + g_Operation +*/ '&cod=' + sCod;
  sLocation = FormatLocation(sLocation);
  parent.frameView.location.replace(sLocation);
}

/*------------------------------------------------------------------------------------*/  
function FunctionEdit()
{
  alert("Convertir esquema en edición...")
}

/*------------------------------------------------------------------------------------*/  
function FunctionGo(sCod)
{
  var sLocation = './' + g_NameCGI + '?FunctionGo&id=' + g_SessionID + /*'&op=' + g_Operation +*/ '&cod=' + sCod;
  bUseXMLHttpRequest = false;
  SendCommandNoUpdate(sLocation, true, true);
  bUseXMLHttpRequest = bDefineHttpRequest;
}

/*------------------------------------------------------------------------------------*/  
function FunctionInfo()
{
  var sLocation = './' + g_NameCGI + '?FunctionInfo&id=' + g_SessionID;/*+ '&op=' + g_Operation;*/
  SendCommandNoUpdate(sLocation, true, false);
}

/*------------------------------------------------------------------------------------*/  
function FunctionSugerir(ev)
{
  var sLocation = './' + g_NameCGI + '?FunctionSugerir&id=' + g_SessionID;/*+ '&op=' + g_Operation;*/
  sLocation = FormatLocation(sLocation);
  ModalDialog(ev, sLocation, 'Sugerir...');
}

/*------------------------------------------------------------------------------------*/  
/*-- Funciones de control de la datatable*/
function FunctionDtRows(rows)
{
  var sLocation = './' + g_NameCGI + '?FunctionDtRows&id=' + g_SessionID + "&row=" + rows;
  SendCommandNoUpdate(sLocation, true, false);
}

/*------------------------------------------------------------------------------------*/  
function FunctionDtFirst()
{
  var sLocation = './' + g_NameCGI + '?FunctionDtFirst&id=' + g_SessionID;
  SendCommandNoUpdate(sLocation, true, false);
}

/*------------------------------------------------------------------------------------*/  
function FunctionDtLast()
{
  var sLocation = './' + g_NameCGI + '?FunctionDtLast&id=' + g_SessionID;
  SendCommandNoUpdate(sLocation, true, false);
}

/*------------------------------------------------------------------------------------*/  
function FunctionDtNext()
{
  var sLocation = './' + g_NameCGI + '?FunctionDtNext&id=' + g_SessionID;
  SendCommandNoUpdate(sLocation, true, false);
}

/*------------------------------------------------------------------------------------*/  
function FunctionDtPrev()
{
  var sLocation = './' + g_NameCGI + '?FunctionDtPrev&id=' + g_SessionID;
  SendCommandNoUpdate(sLocation, true, false);
}

/*------------------------------------------------------------------------------------*/  
function FunctionDtPage(page)
{
  var sLocation = './' + g_NameCGI + '?FunctionDtPage&id=' + g_SessionID + '&col=' + page;
  SendCommandNoUpdate(sLocation, true, false);
}

/*------------------------------------------------------------------------------------*/  
function FunctionLogout()
{
  if (confirm('¿Desea cerrar la sesión actual?'))
  {
    var sLocation = './' + g_NameCGI + '?FunctionLogout&id=' + g_SessionID;
    bUseXMLHttpRequest = false;
    SendCommandNoUpdate(sLocation, false, false);
    bUseXMLHttpRequest = bDefineHttpRequest;
    ChangeLocation("../");
  }
}

/*------------------------------------------------------------------------------------*/  
function getStyle(el, styleProp) 
{
  var x = document.getElementById(el);
  if (x.currentStyle)
    var y = x.currentStyle[styleProp];
  else if (window.getComputedStyle)
    var y = document.defaultView.getComputedStyle(x, null).getPropertyValue(styleProp);
  return y;
}

/*------------------------------------------------------------------------------------*/  
function FunctionChangeScrollText() 
{
  var obj = document.getElementById("idBodyText");

  if (detectBrowser() == "ie")
  {
    var a = getStyle("idBodyText", "maxHeight");
    a = getStyle("idBodyText", "maxHeight");
    obj.style.maxHeight = (a == "none") ? "200px" : "none";
  }
  else
    obj.style.maxHeight = (obj.style.maxHeight == "none") ? "200px" : "none";
  scrollText();
}

/*------------------------------------------------------------------------------------*/  
function SetTitle()
{
  var object = document.getElementById('idTitleUsuario');
  if (object != undefined)
  {
    var objectSpan = object.getElementsByTagName('span');
    if (objectSpan != undefined && objectSpan[0] != null)
      document.title = objectSpan[0].innerHTML;
  }
}

/*
/*------------------------------------------------------------------------------------*
function SetTitle(title)
{
  document.title = title;
}
*/
/*------------------------------------------------------------------------------------*/  
/*function SetTitle()
{
  var objTitle = document.getElementById("idTitleUsuario");
  if (objTitle)
  {
    var strTitle = objTitle.getElementsByTagName("span")[0].innerHTML;
    document.title = strTitle;
  }
}*/

/* ]]> */



