/**
* @author rpalacios
* @contact rodrigo.palacios.cb@gmail.com
* Some right reserved (cc) 2006 
**/

/**
* Permite obtener la version del navegador utilizado
**/
var user_agent = navigator.userAgent.toLowerCase();
function getAgent() 
{
	  if (user_agent.indexOf('opera')!=-1) { // Opera (check first in case of spoof)
		 return 'opera';
	  } else if (user_agent.indexOf('msie 7')!=-1) { // IE7
		 return 'ie7';
	  } else if (user_agent.indexOf('msie') !=-1) { // IE
		 return 'ie';
	  } else if (user_agent.indexOf('safari')!=-1) { // Safari (check before Gecko because it includes "like Gecko")
		 return 'safari';
	  } else if (user_agent.indexOf('gecko') != -1) { // Gecko
		 return 'gecko';
	  } else {
		 return false;
	  }
}
var agent = getAgent();
var cursorHand = (agent=='gecko')?'pointer':'hand';	

/**
* Permite pausar la ejecucion de un script los milisegundos especificados
* ejemplo de uso
* function cargaslider(){return false;}//primero definir la funcion a invocar
* Luego se debe hacer un ciclo que su condición de ejecución sea hasta que la 
* funcion exista y/o la cantidad de iteraciones sea menor que 3 (en este caso la funcion retorna true)
* 		for (i=0;!cargaslider() && i<3;i++ )
*			pauseScript(3000);			
* @param millis, milisegundos a pausar
*/
function pauseScript(millis)
{
	var date = new Date();
	var curDate = null;
	
	do
	{ 
		curDate = new Date(); 
	}while(curDate-date < millis);
} 
/**
* Retorna una referencia a un objeto iframe
* @param formID , corresponde al identificador del formulario
* @param objID , corresponde al identificador del objeto 
* @param esDelPadre, si viene quiere decir que el la referencia al objeto 
*        que se quiere obtener esta en el documento padre (parent.document.),
*        si no viene se toma como referencia el documento actual (document.)	
**/

function getObjIframe(objID,esDelPadre)
{
	if(esDelPadre)
	{
		return parent.window.frames[objID];
	}else
		return window.frames[objID];
}

/**
* Retorna una referencia a un objeto 
* @param formID , corresponde al identificador del formulario
* @param objID , corresponde al identificador del objeto 
* @param esDelPadre, si viene quiere decir que el la referencia al objeto 
*        que se quiere obtener esta en el documento padre (parent.document.),
*        si no viene se toma como referencia el documento actual (document.)	
**/

function getObjFromForm(formID, objID,esDelPadre)
{
	if(esDelPadre)
	{
		return parent.document.forms[formID][objID];
	}else
		return document.forms[formID][objID];
}
/**
* Retorna una referencia a un objeto perteneciente al formulario que pertenece al iframe o frame seleccionado 
* @param formID , corresponde al identificador del formulario
* @param objID , corresponde al identificador del objeto 
* @param frameID ,corresponde al identificador del frame que contiene al objeto solicitado
* @param esDelPadre, si viene quiere decir que el la referencia al objeto 
*        que se quiere obtener esta en el documento padre (parent.document.),
*        si no viene se toma como referencia el documento actual (document.)	
**/	
function getObjFromFormFromFrame(formID, objID,frameID,esDelPadre)
{
	if(esDelPadre)
		return parent.window.frames[frameID].document.forms[formID][objID];
	else
		return window.frames[frameID].document.forms[formID][objID];
}

/**
* Retorna una referencia a un objeto que pertenece al iframe o frame indicado en @param frameID
* @param objID ,corresponde al identificador del objeto    
* @param frameID ,corresponde al identificador del frame que contiene al objeto solicitado
* @param esDelPadre, si viene quiere decir que el la referencia al objeto 
*        que se quiere obtener esta en el documento padre (parent.document.),
*        si no viene se toma como referencia el documento actual (document.)	
**/
function getObjFromFrame(objID,frameID,esDelPadre)
{
	if(esDelPadre)
	{
		if(document.getElementById)
			return parent.window.frames[frameID].document.getElementById(objID);
		else if( document.all )
			return parent.window.frames[frameID].document.all[objID];
		else if( document.layers )
			 {
				if( parent.window.frames[frameID].document.layers[objID] )
					return parent.window.frames[frameID].document.layers[objID];
			 }
	}else{
		if(document.getElementById)		
			return window.frames[frameID].document.getElementById(objID);
		else if( document.all )
			return window.frames[frameID].document.all[objID];
		else if( document.layers )
			 {
				if( window.frames[frameID].document.layers[objID] )
					return window.frames[frameID].document.layers[objID];
			 }
	}
}

/**
* Retorna una referencia a un objeto de la página a través del atributo NAME ó ID
* @param objID ,corresponde al identificador del objeto    
* @param esDelPadre, si viene quiere decir que el la referencia al objeto 
*        que se quiere obtener esta en el documento padre (parent.document.),
*        si no viene se toma como referencia el documento actual (document.)	
**/
function getObj(objID,esDelPadre)
{
	if(esDelPadre)
	{
		if(document.getElementById){		 	
			//alert("objID="+objID+" src="+ parent.document.getElementById(objID).src);
			return parent.document.getElementById(objID);
		}	
		else if( document.all ) 
			return parent.document.all[objID];
		else if( document.layers )
				if( parent.document.layers[objID] )
				   parent.document.layers[objID]; 									     
	}else{
	  if( document.getElementById ) 
		return document.getElementById(objID); 
	  if( document.all ) 
		return document.all[objID]; 
	  if( !oDoc ) 
		oDoc = document;     
	  if( document.layers ){
		if( oDoc.layers[objID] ) 
			return oDoc.layers[objID]; 
	  } 			  
	}
	return false;
}

function doNuhn() {}
/**
* Retorna la coordenada x e y donde esta posicionado el mouse
* @return array posicion 0=coordenada X, posicion 1=coordenada Y
*/
//now create the event handler function to process the event
function getCoords(e) {
  if( !e ) {
    if( window.event ) {
      //Internet Explorer
      e = window.event;
    } else {
      //total failure, we have no way of referencing the event
      return;
    }
  }
  if( typeof( e.pageX ) == 'number' ) {
    //most browsers
    var xcoord = e.pageX;
    var ycoord = e.pageY;
  } else if( typeof( e.clientX ) == 'number' ) {
    //Internet Explorer and older browsers
    //other browsers provide this, but follow the pageX/Y branch
    var xcoord = e.clientX;
    var ycoord = e.clientY;
    var badOldBrowser = ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) ||
     ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) ||
     ( navigator.vendor == 'KDE' )
    if( !badOldBrowser ) {
      if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //IE 4, 5 & 6 (in non-standards compliant mode)
        xcoord += document.body.scrollLeft;
        ycoord += document.body.scrollTop;
      } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE 6 (in standards compliant mode)
        xcoord += document.documentElement.scrollLeft;
        ycoord += document.documentElement.scrollTop;
      }
    }
  } else {
    //total failure, we have no way of obtaining the mouse coordinates
    return;
  }
  return new Array (xcoord,ycoord);  
}

/**
* Imprime las coordenadas x e y del mouse
*/
function alertCoords(e) {
	var coordenadas = getCoords(e);
	if(coordenadas)
		window.alert('Mouse coordinates are x=('+coordenadas(0)+',Y='+coordenadas(1)+')');
	else
		window.alert('total failure, we have no way of obtaining the mouse coordinates');
}
	
/**
* Retorna la coordenada x e y donde esta posicionado el mouse
* @return array posicion 0=coordenada X, posicion 1=coordenada Y 
*/	
//now create the event handler function to process the event
function getkey(e) {
  if( !e ) {
    //if the browser did not pass the event information to the
    //function, we will have to obtain it from the event register
    if( window.event ) {
      //Internet Explorer
      e = window.event;
    } else {
      //total failure, we have no way of referencing the event
      return;
    }
  }
  if( typeof( e.keyCode ) == 'number'  ) {
    //DOM
    e = e.keyCode;
  } else if( typeof( e.which ) == 'number' ) {
    //NS 4 compatible
    e = e.which;
  } else if( typeof( e.charCode ) == 'number'  ) {
    //also NS 6+, Mozilla 0.9+
    e = e.charCode;
  } else {
    //total failure, we have no way of obtaining the key code
    return;
  }
  return e;  
}
/**
* Imprime la tecla del teclado
*/
function alertkey(e) {
	var key = getkey(e);
	if(key)
		window.alert('The key pressed has keycode ' + key +' and is key ' + String.fromCharCode( key ) );
	else
		window.alert('total failure, we have no way of obtaining the key code');
}
function getBut( e, evElement ) {
  if( !e ) {
    if( window.event ) {
      //Internet Explorer
      e = window.event;
    } else {
      //total failure, we have no way of referencing the event
      return;
    }
  }
  if( typeof( e.which ) == 'number' ) {
    //Netscape compatible
    e = e.which;
  } else if( typeof( e.button ) == 'number' ) {
    //DOM
    e = e.button;
  } else {
    //total failure, we have no way of obtaining the button
    return;
  }
  if( !evElement ) { evElement = this; }
  return e;
}
/**
* Imprime la tecla del teclado
*/
function alertBut(e,evElement) {
	var but = getBut(e,evElement);
	if(but)
	  /* 'this' will exist if I have used object.onEventName = alertBut;
	  If I have passed evElement from the onmouseup attribute,
	  'this' will refer to window */
	  window.alert( evElement + ' was clicked with button ' + but );
	else
		window.alert('total failure, we have no way of referencing the event');
}
/**
* Retorna un objeto puntero a un objeto del documento
* @param divID identificador (dom) del objeto 
* @param oDoc  referencia a documento 
**/
function getElemento(divID,oDoc)
{
  	
  if( document.getElementById ) 
  {
    return document.getElementById(divID); 
   }  
  if( document.all ) 
    return document.all[divID]; 
  if( !oDoc ) 
  	oDoc = document;     
  if( document.layers ){
    if( oDoc.layers[divID] ){ 
	    return oDoc.layers[divID]; 
	}else{
      //repeatedly run through all child layers
      for( var x = 0, y; !y && x < oDoc.layers.length; x++ ){
        //on success, return that layer, else return nothing
        y = getElemento(divID,oDoc.layers[x].document); 
      }
      return y; 
   } 
  }
  return false;
}
/**
* Retorna un objeto puntero a un objeto del documento
* @param divID identificador (dom) del objeto 
* @param oDoc  referencia a documento 
**/
function getElementoFromForm(formID, divID){  	  
  return document.forms[formID][divID]; 
}
/**
* Muestra una capa 
* Example of use : onclick="showDiv('myDiv');"
*/
function showDiv(divID_as_a_string){
  //get a reference as above ...
  myReference = getElemento(divID_as_a_string);
  if( !myReference ){
    window.alert('Nothing works in this browser');
    return; //don't go any further
  }
  //now we have a reference to it
  if( myReference.style ){
    //DOM & proprietary DOM
    myReference.style.visibility = 'visible';
  } else {
    //layers syntax
    myReference.visibility = 'show';
  }
}
/**
* Oculta una capa 
* Example of use : onclick="hideDiv('myDiv');"
*/
function hideDiv(divID_as_a_string) {
	//get a reference as above ...
	var myReference = getElemento(divID_as_a_string);
	if( !myReference ) { 
		window.alert('Nothing works in this browser'); 
		return; 
	}
	if( myReference.style ) 
	{
		myReference.style.visibility = 'hidden'; 
	} else {
		if( myReference.visibility ) { 
			myReference.visibility = 'hide'; 
		} else {
			window.alert('Nothing works in this browser'); 
			return; 
		} 
	}
}
/**
* Muestra una capa 
* Example of use : onclick="showTr('myDiv');"
*/
function showTr(divID_as_a_string){
  //get a reference as above ...
  myReference = getElemento(divID_as_a_string);
  if( !myReference ){
    window.alert('Nothing works in this browser');
    return; //don't go any further
  }
  //now we have a reference to it
  if( myReference.style ){
    //DOM & proprietary DOM
    myReference.style.display = (navigator.userAgent.indexOf("MSIE")>0)?'block':'table-row';
  } else {
    //layers syntax
    myReference.visibility = 'show';
  }
}

/**
* Oculta una Tr de una tabla
* Example of use : onclick="hideTr('myDiv');"
*/
function hideTr(divID_as_a_string) {
	//get a reference as above ...
	var myReference = getElemento(divID_as_a_string);
	if( !myReference ) { 
		window.alert('Nothing works in this browser'); 
		return; 
	}
	
	if( myReference.style ) 
	{		
		myReference.style.display = 'none'; 
	} else {
		if( myReference.visibility ) { 
			myReference.visibility = 'hide'; 
		} else {
			window.alert('Nothing works in this browser'); 
			return; 
		} 
	}
}

function changeDisplay( elementId, setTo ) {
   var theElement;
  if( document.getElementById ) {
    //DOM
    theElement = document.getElementById( elementId );
  } else if( document.all ) {
    //Proprietary DOM
    theElement = document.all[ elementId ];
  }
  if( !theElement ) {
    /* The page has not loaded, or the browser claims to
    support document.getElementById or document.all but
    cannot actually use either */
    return;
  }
  //Reference the style ...
  if( theElement.style ) { theElement = theElement.style; }
  if( typeof( theElement.display ) == 'undefined' ) {
    //The browser does not allow us to change the display style
    //Alert something sensible (not what I have here ...)
    window.alert( 'Your browser does not support this' );
    return;
  }
  //Change the display style
  theElement.display = setTo;
}	
/**
* Autoajusta en tamaño del iframe al contenido de este al momento de cargar el body del doc hijo(iframe)
* Uso: en body del doc html del iframe poner autofitIframe('Noticias'); 
* donde 'Noticias' es el id del tag iframe en documento html padre
* @param id, identificador del iframe
**/
function autofitIframe(id)
{	
	var the_height;	
	if (!window.opera && !document.mimeType && document.all && document.getElementById)
	{	
		the_height = parseInt(this.document.body.offsetHeight);
		parent.document.getElementById(id).style.height=the_height+"px";
	}
	else if(document.getElementById) 
	{
		the_height = parseInt(this.document.body.scrollHeight);
		parent.document.getElementById(id).style.height=the_height+"px"
	}
}

