/*   _________      ___                          ___     ___    
 *   \_   ___ \     \  |                         \  |   / _ \   
 *   /   /   \/ ____ | |__ _____  _____    ______|  |_ / /_\ \  
 *  /    \     /  _ \| __ \\__  \ \__  \  /  ___\   __/       \ 
 *  \     \___(  |_| | \_\ \/ __ \_/ __ \_\___ \ |  |/    |    \
 *   \______  /\____/|___  (____  (____  /____  ||__|\____|__  /
 *          \/           \/     \/     \/     \/             \/ 
 *              | ' |)    |? /\ ¨|¨ |-| [- |?    |} [-          
 *             _\¨ |< /\ ¨|¨ [- |} () /\ |? |) | |\| (¬         
 */

// Popup //////////////////////////////////////////////////////////////////////////////////////////

/* 
 * function opens the url specified with the parameter url
 * in a new window with, if specified, width x and height y.
 */
function openPopup(url,x,y) {
    if (!x) x = 270;
    if (!y) y = 400;
    var pop = window.open(url,"popup","dependent=yes,resizable=yes,scrollbars=yes,width="+x+",height="+y+",left=50,top=50");
    pop.focus();
}

/* 
 * swap to image _on
 */
function imageOn(imgObj) {
    var index = imgObj.src.indexOf("_off.");
    imgObj.src = imgObj.src.substring(0, index) + "_on." + imgObj.src.substring(index+5);
}

/* 
 * swap to image _off
 */
function imageOff(imgObj) {
    var index = imgObj.src.indexOf("_on.");
    imgObj.src = imgObj.src.substring(0, index) + "_off." + imgObj.src.substring(index+4);
}

/* 
 * function opens mail
 */
function contact() {
    var email = "imwald";
    var host = "gmx.ch";
    location.href = "mailto:" + email + "@" + host;
}

// Sort ///////////////////////////////////////////////////////////////////////////////////////////

/* 
 * function sorts the rows of a table
 */
function sortTable(tableId, col, sortfunc, reverse) {
    if (!sortfunc) sortfunc = sortRowsByStr;
    if (!reverse) reverse = false;
    var tableRows = new Array();
    var table = document.getElementById(tableId);
    for (var i = 1; i < table.rows.length; i++) tableRows[i - 1] = table.rows[i];	// copy table rows to row array
    changeColums(tableRows, col, 0);	// set the column to sort at first position
    handleIndex(tableRows, true);		// append index to keep order of equal items, needed by mozilla
    tableRows.sort(sortfunc);			// call of array sort to sort by items in first table column
    handleIndex(tableRows, false);		// remove index to keep order of equal items, needed by mozilla
    changeColums(tableRows, col, 0);	// set the column sorted by back to original position
    if (reverse) tableRows.reverse();	// reverse the hole table if desired so
    for (var i = 0; i < tableRows.length; i++) table.tBodies[0].appendChild(tableRows[i]);	// copy row array to table rows
}

/* 
 * function changes colums of a table by switching them
 */
function changeColums(rows, cola, colb) {
    if (cola!=colb) {
        for (var i in rows) {
            var tmp = rows[i].cells[cola].innerHTML;
            rows[i].cells[cola].innerHTML = rows[i].cells[colb].innerHTML;
            rows[i].cells[colb].innerHTML = tmp;
        }
    }
}

/* 
 * function appends or removes index column.
 * needed because mozillas array sort function
 * doesnt keep existing order of equal items.
 */
function handleIndex(rows, append) {
    for (var i in rows) {
        if (append) {
            var td = document.createElement("td");
            var text = document.createTextNode(i);
            td.appendChild(text);
            rows[i].appendChild(td);
        }
        else {
            rows[i].deleteCell(rows[i].cells.length-1);
        }
    }
}

/* 
 * function sorts the rows numerically
 * accepts int and float, metric and american
 */
function sortRowsByNum(rowa, rowb) {
    var as = rowa.cells[0].innerHTML.split("'");	// if feets split in feets and inches
    var bs = rowb.cells[0].innerHTML.split("'");	// if feets split in feets and inches
    a = parseFloat(as[0]);							// parse default or feets
    b = parseFloat(bs[0]);							// parse default or feets
    if (as.length>1 && bs.length>1 && a==b) {		// if feets and feets are same
        a = parseFloat(as[1].replace("\"",""));		// parse inches
        b = parseFloat(bs[1].replace("\"",""));		// parse inches
    }
    if (a == b) return sortRowsByIndex(rowa, rowb);	// needed by mozilla
    return a-b;
}

/* 
 * function sorts the rows alphabetically
 */
function sortRowsByStr(rowa, rowb) {
    var a = rowa.cells[0].innerHTML;
    var b = rowb.cells[0].innerHTML;
    if (a == b) return sortRowsByIndex(rowa, rowb);	// needed by mozilla
    return a.localeCompare(b);
}

/* 
 * function sorts the rows alphabetically 
 * ignoring all html tags
 */
function sortRowsByHtm(rowa, rowb) {
    var a = rowa.cells[0].innerHTML.replace(/<(.|\n)+?>/ig, "");
    var b = rowb.cells[0].innerHTML.replace(/<(.|\n)+?>/ig, "");
    if (a == b) return sortRowsByIndex(rowa, rowb);	// needed by mozilla
    return a.localeCompare(b);
}

/* 
 * function sorts the rows by date
 */
function sortRowsByDat(rowa, rowb) {
    var exp = /(\d+)\.(\d+)\.(\d+)/;
    exp.exec(rowa.cells[0].innerHTML);
    var a = new Date(RegExp.$3, RegExp.$2-1, RegExp.$1);
    exp.exec(rowb.cells[0].innerHTML);
    var b = new Date(RegExp.$3, RegExp.$2-1, RegExp.$1);
    if (a == b) return sortRowsByIndex(rowa, rowb);	// needed by mozilla
    return b-a;
}

/* 
 * function sorts by previous ordering
 * needed because mozillas array sort function
 * doesnt keep existing order of equal items.
 */
function sortRowsByIndex(rowa, rowb) {
    var a = rowa.cells[rowa.cells.length-1].innerHTML;
    var b = rowb.cells[rowb.cells.length-1].innerHTML;
    return a-b;
}

// Dhtml //////////////////////////////////////////////////////////////////////////////////////////

function displayFromTo(fromId, toId, show) {
    for (var i=fromId; i<=toId; i++) {
        display(i, show);
    }
}

function display(id, show) {
    if (show) document.getElementById(id).style.display = '';
    else document.getElementById(id).style.display = 'none';
}

