/**
 *   utils.js: funzioni in JavaScript di utilita` 
 *   Copyright (C) 2004 Roberto Posenato
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *   Roberto Posenato <posenato@sci.univr.it>
 *   Dipartimento di Informatica
 *   Università degli Studi di Verona
 *   Strada le Grazie 15
 *   37134 Verona (Italy)
 *
 */

var iFrames = new Array();
var tO = new Array(0,0,0,0,0);


function adjustAllIFrames(){
    for (i=0 ; i<iFrames.length; i++) {
        adjustIFrameSize(iFrames[i]);
    }
}

function addIFrame(id) {
    iFrames.push(id);
}


/*
 * adjustIFrameSize(id) aggiusta la dimensione di un iframe 'id' in base
 * al suo contenuto
 */
function adjustIFrameSize(id) {
    var myIframe = document.getElementById(id);
    var morePixels = 35; //Mozilla sometimes doesn't resize correctly
    if (myIframe) {
        if (myIframe.contentDocument && myIframe.contentDocument.body.offsetHeight) {
            // W3c DOM syntax for NN 6
            oH = myIframe.contentDocument.body.offsetHeight;
            myIframe.height = oH+morePixels;
            if (myIframe.contentDocument.body.scrollHeight) {
                sH = myIframe.contentDocument.body.scrollHeight; 
                if (sH>oH) myIframe.height = sH+morePixels;
            }
        } else {
            if (myIframe.Document && myIframe.Document.body.scrollHeight) {
                //IE syntax
                myIframe.height = myIframe.Document.body.scrollHeight;
            }
        }
        return false;
    } else
        return true;
}

/*
 * switchIFrame(id, url) carica nell'iframe id l'url se iframe e` vuoto e ridimensione l'altezza, altrimenti 
 * azzera l'altezza per simulare una cancellazione del iframe
 */
function switchIFrame(id, url) {
    var myIframe = document.getElementById(id);
    if (myIframe) {
        if ( myIframe.src != url ) {
            myIframe.src = url;
        }
        if (myIframe.height == 0) {
            tO[0] = setTimeout("adjustIFrameSize('"+id+"')",  500);
            tO[1] = setTimeout("adjustIFrameSize('"+id+"')", 1000);
            tO[2] = setTimeout("adjustIFrameSize('"+id+"')", 1500);
            tO[3] = setTimeout("adjustIFrameSize('"+id+"')", 2000);
            tO[4] = setTimeout("adjustIFrameSize('"+id+"')", 3000);
            myIframe.height = 600;
        } else {
			for (var i=0; i<5; i++) {
				if (tO[i] != 0) {
					clearTimeout(tO[i]);
					tO[i] = 0;
				}
			}
            myIframe.height = 0;
        }
        return false;
    } else
        return true;
}

function show_props(obj, objName) {
  msgWindow=window.open(objName, objName,
                        'scrollbars=yes,width=300,height=600,resizable');
  msgWindow.document.open('text/plain');
  for (var i in obj)
    msgWindow.document.writeln(objName + "." + i + " = " + obj[i] + '<br>');
  msgWindow.document.close()
}


/*
 * highlightElement(id) attiva un backgroud giallo sull'elemento id
 */
function highlightElement(id) {
    var e = document.getElementById(id);
    if (e) { 
        var bg;
        if (e.currentStyle) {
            bg = e.currentStyle.backgroundColor;
        } else {
            var compStyle = getComputedStyle(e,"");
            //show_props(compStyle);
            bg = compStyle.backgroundColor;
        }
        e.style.backgroundColor = "#ffff80";
        return bg;
    } else {
        return null;
    }
}

/*
 * dehighlightElement(id, bg) setta il backgroud dell'elemento id a bg se non nullo,
 * altrimenti lo setta a trasparente;
 */
function dehighlightElement(id, bg) {
    var e = document.getElementById(id);
    if (e) {
        if (bg) {
            e.style.backgroundColor = bg;
        } else {
            e.style.backgroundColor = "transparent";
        }
    }
    return true;
}


/*
 *disableIfNotEqual(id, idValue, value) disabilita l'elemento select 'id' se il valore 'value' non è uguale
 *a quello presente nel select idValue
 */
function disableIfNotEqual(id, idValue, value, idTd, idTd1) {
    var selValue = document.getElementById(idValue);
    var sel = document.getElementById(id);
    var td = document.getElementById(idTd);
    var td1 = document.getElementById(idTd1);

    if (selValue.value != value) {
        sel.disabled = true;
        //td.style.visibility = td1.style.visibility = "hidden";
        td.style.display = td1.style.display = "none";
    } else {
        sel.disabled = false;
        //td.style.visibility = td1.style.visibility = "visible";
        td.style.display = td1.style.display = "inline";
    }
    return true;
}

/**
 * Rende invisibile la scritta di attesa del caricamento dei dati
 * nella pagina della ricerca nel catalogo delle pubblicazioni
 */
function cacheOff() {
        var nava = (document.layers);
        var dom = (document.getElementById);
        var iex = (document.all);
        if (nava) { cach = document.cache}
        else if (dom) {cach = document.getElementById("cache").style}
        else if (iex) {cach = cache.style}
        cach.visibility = "hidden";
}

/**
 * Rende visibile la scritta di attesa del caricamento dei dati
 * nella pagina della ricerca nel catalogo delle pubblicazioni
 */
function cacheOn() {
        var nava = (document.layers);
        var dom = (document.getElementById);
        var iex = (document.all);
        if (nava) { cach = document.cache}
        else if (dom) {cach = document.getElementById("cache").style}
        else if (iex) {cach = cache.style}
        //var width = document.documentElement.clientWidth;
        //var height = document.documentElement.clientHeight;
        //cach.left = Math.round(width/2) - 200;
        //cach.top = Math.round(height);
        cach.visibility = "visible";
}

/**
 * Cancella i dati della form del catalogo della ricerca.
 */
function cancelCatalogoForm() {
    document.catalogoForm.id_catisicrui[0].selected=true;
    document.catalogoForm.titolo.value="";
    document.catalogoForm.autore.value="";
    document.catalogoForm.anno[0].selected=true;
    document.catalogoForm.id_tipopubb[0].selected=true;
    document.catalogoForm.cod_sett[0].selected=true;
    document.catalogoForm.id_dipartimento[0].selected=true;
    document.catalogoForm.id_arearic[0].selected=true;
}

/**
 * Cancella i dati della form del catalogo dell'offerta formativa di ateneo.
 */
function cancelOffertaForm() {
    document.offertaForm.insegn.value="";
    document.offertaForm.idFacolta[0].selected=true;
    document.offertaForm.idTipocs[0].selected=true;
    document.offertaForm.idSettore[0].selected=true;
    document.offertaForm.stato[0].selected=true;
    document.offertaForm.linguaErogazione[0].selected=true;
}
