/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 
	return window.undefined; 
}
function getViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
	return window.undefined; 
}

//02-09-2005, DROtero. Funciones JS Comunes (preferentemente cross-browser)

//Devuelve un array con el ancho (posición 0) y alto (posición 1) disponible en el browser
function GetSize() {
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement &&
        ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    //window.alert( 'Width = ' + myWidth + ' x Height = ' + myHeight );
    var v = new Array(myWidth,myHeight);
    return  v;
}

//Busca la refencia de un objeto dado el nombre o id.
function findObj(theObj, theDoc)
{
    var p, i, foundObj;
    if(!theDoc) theDoc = document;
    if((p = theObj.indexOf("?")) > 0 && parent.frames.length)
    {   theDoc = parent.frames[theObj.substring(p+1)].document;
        theObj = theObj.substring(0,p);
        }
    if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
    for (i=0; !foundObj && i < theDoc.forms.length; i++)
        foundObj = theDoc.forms[i][theObj];
    for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
        foundObj = findObj(theObj,theDoc.layers[i].document);
    if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
    return foundObj;
}

function MostrarDiv(strDIV) 
{
if (findObj(strDIV).style.display=="none") 
    {
        findObj(strDIV).style.display="";
    } 
    else
    {
    findObj(strDIV).style.display="none";    
    }
}

function MostrarBoton(strBoton) 
{
if (findObj(strBoton).style.display=="none") 
    {
        findObj(strBoton).style.display="";
    } 
    else
    {
    findObj(strBoton).style.display="none";    
    }
}

function HabilitarControl(strBoton) 
{
    findObj(strBoton).disabled=false; 
}

function DesHabilitarControl(strBoton) 
{
    findObj(strBoton).disabled=true;
}

function DeseleccionarControl(strBoton) 
{
    findObj(strBoton).checked=false;
}

function SeleccionarControl(strBoton) 
{
    findObj(strBoton).checked=true;
}

function MostrarControl(strCtrl) 
{
    findObj(strCtrl).style.display="";
}

function OcultarControl(strCtrl) 
{
    findObj(strCtrl).style.display="none";
}

function AsignarValor(strControl,strValor)
{
findObj(strControl).value=strValor;
}

function generarFechaDeHoy()
{
    var hoydia = (new Date().getDate());
    var hoymes = (new Date().getMonth());
    var hoyanio = (new Date().getYear());
    var hoy = new Date(hoyanio,hoymes,hoydia);
    return hoy;
}    

function generarFecha(fecha)
{
    var dia = fecha.substring (0, fecha.indexOf ("/"));
    var mes = (fecha.substring (fecha.indexOf ("/")+1, fecha.lastIndexOf ("/")))-1;
    var anio = fecha.substring (fecha.lastIndexOf ("/")+1, fecha.length);
    var fecha = new Date(anio,mes,dia);
    return fecha;
}    
function getRefToDiv(divID,oDoc) {
    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 = getRefToDiv(divID,oDoc.layers[x].document); }
            return y; } }
    if( document.getElementById ) {
        return document.getElementById(divID); }
    if( document.all ) {
        return document.all[divID]; }
    return false;
}
function showDiv(divID_as_a_string) {
    //get a reference as above ...
    myReference = getRefToDiv(divID_as_a_string);
    if( !myReference ) {
        window.alert('Nothing works in this browser');
        return false; //don't go any further
        //return anything would work,
        //but I am using false to show failure
    }
    //now we have a reference to it
    if( myReference.style ) { //DOM & proprietary DOM
        myReference.style.visibility = 'visible';
    } else {
        if( myReference.visibility ) { //Netscape
            myReference.visibility = 'show';
        } else {
            window.alert('Nothing works in this browser');
            return false; //don't go any further
        }
    }
    return true;
}
