﻿// Helper function used by the javaScript functions below. Cross browser function for returning the object of an id.
function getItem(id) {
    var itm = false;
    if(document.getElementById)
        itm = document.getElementById(id);
    else if(document.all)
        itm = document.all[id];
    else if(document.layers)
        itm = document.layers[id];

    return itm;
}


// Expands or collapses all items. When a item is collapsed the original background color is restored.
// Also sets/resets the "details" span tag´s className.
function toggleAll(dowhat) {
    var tags = document.getElementsByTagName('tr');
    var counter = 0;
    var detailsObj;
    if(!tags)
        return false;

    for(var i = 0; i < tags.length; i++) {
        if(tags[i].className == 'collapsetr') {
            detailsObj = getItem('details'+counter);
            if(dowhat == 'collapse') {
                tags[i].style.display = 'none';
                tags[i].previousSibling.className = tags[i].originalBgClass;
                detailsObj.className = 'details'; // The "details" <span> tag´s className.
            } else {
                tags[i].style.display = '';
                tags[i].previousSibling.className = 'active';
                detailsObj.className = 'detailsOpen'; // The "details" <span> tag´s className.
            }
            counter++;
        }
    }
    return true;
}


// Expands a collapsed extra info row or collapses an expanded extra info row.
// Sets the main row (the document title row) class to "active" when extra info is shown.
// Resets the main row´s origigal background class when the extra info is hidden.
// Also sets/resets the "details" span tag´s className.
// "id" is the id of the extra info row.
function toggleItem(id) {
    itm = getItem(id);

    if(!itm)
        return false;

    if(itm.style.display == 'none') {
        itm.style.display = '';
        itm.previousSibling.className = 'active';
        itm.previousSibling.all.item(1).all.item(3).className = 'detailsOpen'; // The "details" <span> tag´s className.
    } else {
        itm.style.display = 'none';
        itm.previousSibling.className = itm.originalBgClass;
        itm.previousSibling.all.item(1).all.item(3).className = 'details'; // The "details" <span> tag´s className.
    }
    return true;
}



// Set the main row´s (trObj) class to "className" if the extra info is hidden.
// If the extra info is shown the main row class stays as it is (i.e. "active").
function setMainRowClass(trObj, className) {
    if(trObj.nextSibling.style.display == 'none')
        trObj.className = className;
}


// Called after the HTML page has loaded. 
// Sets the originalBgClass attribute of the row with extra information to the same as the className on the row above (the main row with the document title), i.e. to "normal" or "alternate".
// This attribute is used to restore the main row´s class when changing back the main row from "active" to it´s original class it had before the extra info was expanded.
function addDefaultAttribute() {
    var tags = document.getElementsByTagName('tr');
    if(!tags)
        return false;

    for(var i = 0; i < tags.length; i++) {
        if(tags[i].className == 'collapsetr') {
            tags[i].originalBgClass = tags[i].previousSibling.className;
        }
    }
}


// For debug purpose only. Outputs the text and value to a tag with id = "debug".
function debug(txt, value) {
    document.all('debug').innerHTML = document.all('debug').innerHTML + txt + value + '<br>';
}


// Displays a animated gif as a background image.
function loadSubmit() {
	document.getElementById('pleasewait').style.display = '';
	document.getElementById('pleasewait').style.backgroundImage = 'url(../images/search/progress.gif)';
	return true;
}



