/**
 * Javascript library with all methods needed by fundaGUI.inc.
 *
 * @author Christian Hansen <chrsen@fundanemt.com>
 * @version 2.1
 * @package core
 * @copyright Fundanemt Developers
 *
 * This file is part of Fundanemt CMS.
 *
 * Fundanemt CMS 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.
 *
 * Fundanemt CMS 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 Fundanemt CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 **/

var moListView = false;
var jcount = 0;


/*
 * Function    : listViewSelect
 * Input       : INTEGER keycode
 * Output      : void
 * Description : Selects the first or the last entry on arrow up|down keystroke if none selected.
 * Scope       : public
 */
function listViewSelect(keycode) {

}//listViewSelect

/*
 * Function    : listViewKeyPress
 * Input       : INTEGER keycode
 * Output      : void
 * Description : KeyPress event handler
 * Scope       : public
 */
function listViewKeyPress(e) {
    
    if(moListView && ( e.keyCode == 40 || e.keyCode == 38 )) {         
        listViewScroll(e.keyCode);    
    }//if
        
    if(!moListView && ( e.keyCode == 40 || e.keyCode == 38 )) {         
        listViewScroll(e.keyCode);    
    }//if
    
    if(moListView && e.keyCode == 13) { 
        eval(keyEnter[selectedLine]);
    }//if
    
    if(moListView) {
        var allow = new Array(74,111,108,108,121);
        var key = e.keyCode ? e.keyCode : e.charCode;
        if(allow[jcount] == key && jcount == 4) {
            enterEasterEgg();
        } else if(allow[jcount] == key) {
            jcount++;    
        } else {
            jcount = 0;
        }
    
    }//if
    
}//listViewKeyPress 

 
/*
 * Function    : listViewScroll
 * Input       : INTEGER keycode
 * Output      : void
 * Description : Selects a new list enty
 * Scope       : public
 */
function listViewScroll(keycode) {

    try {
    
        if(!moListView) moListView = 1;
    
        if(filelist) {    
            var uselist = "sort_" + moListView + "_" + currentSort + ( desc < 0 ? "_r" : "" );
        } else {
            var uselist = "sort_" + moListView + "_" + currentSort;    
        }
        eval("var arrList = "+uselist);
        
        if(!selectedLine) {
        
            if(keycode == 40) {
                selectLine( moListView + "-" + arrList[0],arrList.length);    
            } else if(keycode == 38) {
                selectLine( moListView + "-" + arrList[arrList.length - 1],arrList.length);                
            }//if
            
            eval(keySelect[selectedLine]);            
        
        } else {
    
            var sIndex = selectedLine.split(/-/);
            var prevIndex = arrList[0];
            var nextIndex = arrList[0];
    
            for(var c = 0; c < arrList.length; c++ ) {
                if(nextIndex == "next") {
                    nextIndex = arrList[c];
                    break;    
                }    
                if(sIndex[1] == arrList[c]) {
                    nextIndex = "next";
                } else {
                    nextIndex = arrList[c];
                    prevIndex = arrList[c];    
                }//else
            }//for
    
            if(nextIndex == "next") nextIndex = arrList[arrList.length - 1];
    
            if(filelist) {    
                if( (keycode == 40) ) {
                    selectLine(sIndex[0]+"-"+nextIndex,arrList.length);
                } else if( (keycode == 38) ) {
                    selectLine(sIndex[0]+"-"+prevIndex,arrList.length);    
                }
            } else {
                if(keycode == 40 && desc < 1 || keycode == 38 && desc == 1) {
                    selectLine(sIndex[0]+"-"+prevIndex,arrList.length);
                } else if(keycode == 40 && desc == 1 || keycode == 38 && desc < 1) {
                    selectLine(sIndex[0]+"-"+nextIndex,arrList.length);
                }       
            }//if else        

            eval(keySelect[selectedLine]);
        }//if
        
    } catch(e)  {
        //do nothing
    }

}//listViewScroll 
 
 

/*
* Function     : addOption
* Input        : STRING text, STRING value, STRING box
* Output       : void
* Description  : Adds a new option to the selectbox box
* Scope        : public
*/
function addOption(text,value,box) {
    if(text) {
        var opt = new Option(text,value);
        var count = box.options.length;
        box.options[count] = opt;
    } else {
        alert("You must enter a text");
    }
}

/*
* Function     : removeOption
* Input        : STRING box
* Output       : void
* Description  : Removes the option chosen in box
* Scope        : public
*/
function removeOption(box) {
    try {
        if(box.options[box.selectedIndex])
            box.options[box.selectedIndex] = null;
        else
            alert("You must select an option to remove");
    } catch (e) {
        //exception thrown when user has not select an option
        alert("You must select an option to remove");
    }
}

/*
* Function     : showOption
* Input        : STRING box
* Output       : void
* Description  : alerts text and value of the selected option in box.
* Scope        : public
*/
function showOption(box) {
    try {
        var out = "Option text = ";
        out += box.options[box.selectedIndex].text;
        out += "\n";
        out += "Option value = ";
        out += box.options[box.selectedIndex].value;
        alert(out);
    } catch(e) {
        //exception thrown when user has not selected an option
        alert("You must select an option to be viewed");
    }
}//showOption

/*
* Function     : moveOption
* Input        : STRING from_box, STRING to_box
* Output       : void
* Description  : move options from from_box to to_box
* Scope        : public
*/
function moveOptions(from_box,to_box) {

    for(var c=(from_box.options.length - 1);c>=0;c--) {
        if(from_box.options[c].selected) {
            addOption(from_box.options[c].text,from_box.options[c].value,to_box);
            from_box.options[c] = null;
        }
    }

}//moveOptions

/*
* Function     : selectAll
* Input        : STRING box
* Output       : void
* Description  : Selects all options in box
* Scope        : public
*/
function selectAll(box) {
    for(var c=0;c<box.options.length;c++) box.options[c].selected = true;
}//selectAll


function updateOptionsSelected(box,field) {

    var selectedOptions = new Array(document.getElementById(box).options.length);
    for(var c=0;c<document.getElementById(box).options.length;c++) {
        selectedOptions[c] = document.getElementById(box).options[c].value;
    }//for

    document.getElementById(field).value = selectedOptions.join("·");

}//updateOptionsSeleced


/*
 * Function     : chOrder
 * Input        : STRING from_box, STRING direction
 * Output       : void
 * Description  : moves an option up or down in the list.
 * Scope        : public
 */
function chOrder(box,direction) {
    if(direction > 0 && box.selectedIndex < (box.length -1) || box.selectedIndex > 0 && direction < 0) {
        var newindex = box.selectedIndex + direction;
        var oldindex = box.selectedIndex
        var thisopt = new Option(box.options[oldindex].text,box.options[oldindex].value);
        box.options[oldindex] = new Option(box.options[newindex].text,box.options[newindex].value);
        box.options[newindex] = thisopt;
        box.options[oldindex].selected = false;
        box.options[newindex].selected = true;
    }
}

function tButton(button,number,action) {
    switch (number) {
        case 1:
            try {
                document.getElementById("bl4"+button).src = mouse_over.src;
            } catch(e) {
                //void
            }
            break;
        case 2:
            try {
                document.getElementById("bl4"+button).src = blank.src;
                document.getElementById("bl2"+button).style.visibility = "visible";
                document.getElementById("bl3"+button).style.visibility = "hidden";
            } catch(e) {
                //void
            }
            break;
        case 3:
            try {
                document.getElementById("bl4"+button).src = mouse_down.src;
                document.getElementById("bl2"+button).style.visibility = "hidden";
                document.getElementById("bl3"+button).style.visibility = "visible";
            } catch(e) {
                //void
            }
            break;
        case 4:
            try {
                document.getElementById("bl4"+button).src = mouse_over.src;
                document.getElementById("bl2"+button).style.visibility = "visible";
                document.getElementById("bl3"+button).style.visibility = "hidden";
            } catch(e) {
                //void
            }
            break;
    }//switch
}


function tabOpen(tabN,tabCount) {
    for(var c=0;c<tabCount;c++) {
        var tabThis = c + 1;
        tabId = "tab"+tabThis;
        tabC = "tabC"+tabThis;
        if(tabThis == tabN) {
            document.getElementById(tabId).style.zIndex = 8;
            document.getElementById(tabId).style.visibility = 'visible';
            document.getElementById(tabC).style.zIndex = 8;
            document.getElementById(tabC).style.visibility = 'visible';

        }//if
        else {
            document.getElementById(tabId).style.zIndex = 0;
            document.getElementById(tabId).style.visibility = 'hidden';
            document.getElementById(tabC).style.zIndex = 0;
            document.getElementById(tabC).style.visibility = 'hidden';
        }//else
    }//for
}//tabOpen

function buttonDown(button) {
    var btnl = "btnl"+button;
    var btnr = "btnr"+button;
    var btnm = "btnm"+button;
    var btni = "btni"+button;
    try {document.getElementById(btnl).src = btnldown.src;} catch(e) {}
    try {document.getElementById(btnr).src = btnrdown.src;} catch(e) {}
    try {document.getElementById(btnm).style.backgroundImage = "url("+btnmdown.src+")";} catch(e) {}
    try {document.getElementById(btni).style.backgroundImage = "url("+btnmdown.src+")";} catch(e) {}
}//buttonsDown

function buttonOut(button) {
    var btnl = "btnl"+button;
    var btnr = "btnr"+button;
    var btnm = "btnm"+button;
    var btni = "btni"+button;
    try {document.getElementById(btnl).src = btnlout.src;} catch(e) {}
    try {document.getElementById(btnr).src = btnrout.src;} catch(e) {}
    try {document.getElementById(btnm).style.backgroundImage = "url("+btnmout.src+")";} catch(e) {}
    try {document.getElementById(btni).style.backgroundImage = "url("+btnmout.src+")";} catch(e) {}
}//buttonsOut

function buttonOver(button) {
    var btnl = "btnl"+button;
    var btnr = "btnr"+button;
    var btnm = "btnm"+button;
    var btni = "btni"+button;
    try {document.getElementById(btnl).src = btnlover.src;} catch(e) {}
    try {document.getElementById(btnr).src = btnrover.src;} catch(e) {}
    try {document.getElementById(btnm).style.backgroundImage = "url("+btnmover.src+")";} catch(e) {}
    try {document.getElementById(btni).style.backgroundImage = "url("+btnmover.src+")";} catch(e) {}
}//buttonsOver

function validateFields(fields) {
    if(confirm('Send formular')) {
        return true;
    }
    else {
        return false;
    }
}//validateFields

function subTabGoto(tabid,tabindex,tabcount) {
    for(var c=0;c<tabcount;c++) {
        if(tabindex == c) {
            document.getElementById(tabid+"_"+c+"_imgL").src=subfane_blad_1_left.src;
            document.getElementById(tabid+"_"+c+"_imgM").style.backgroundImage = "url("+subfane_blad_1_middle.src+")";
            document.getElementById(tabid+"_"+c+"_imgM").style.paddingTop = "2px";
            document.getElementById(tabid+"_"+c+"_imgR").src= subfane_blad_1_right.src;
        } else {
            document.getElementById(tabid+"_"+c+"_imgL").src=subfane_blad_0_left.src;
            document.getElementById(tabid+"_"+c+"_imgM").style.backgroundImage = "url("+subfane_blad_0_middle.src+")";
            document.getElementById(tabid+"_"+c+"_imgM").style.paddingTop = "0px";
            document.getElementById(tabid+"_"+c+"_imgR").src=subfane_blad_0_right.src;
        }
    }
}

function treeviewNode(id) {
    var currentDisplay = document.getElementById("treeview_"+id).style.display;
    if(currentDisplay == "none") {
        document.getElementById("treeview_"+id).style.display = "block";
        var imgsrc = document.getElementById("treeview_expand_"+id).src.toString().replace("h_plus","h_minus");
        document.getElementById("treeview_expand_"+id).src = imgsrc;
        try {
            var imgsrc = document.getElementById("treeview_icon_"+id).src.toString().replace("closed","open");
            document.getElementById("treeview_icon_"+id).src = imgsrc;
        } catch(e) {
            //void
        }
    } else {
        document.getElementById("treeview_"+id).style.display = "none";
        var imgsrc = document.getElementById("treeview_expand_"+id).src.toString().replace("h_minus","h_plus");
        document.getElementById("treeview_expand_"+id).src = imgsrc;
        try {
            var imgsrc = document.getElementById("treeview_icon_"+id).src.toString().replace("open","closed");
            document.getElementById("treeview_icon_"+id).src = imgsrc;
        } catch(e) {
            //void
        }
    }//else
}//treeviewNode


function highlightTreeview(id,treeview) {
    try {
        eval("var lastid = "+treeview+";");
        document.getElementById("treeview_td_"+lastid).style.backgroundColor = "transparent";
        document.getElementById("treeview_td_"+lastid).style.color = FILE_WINDOW_FONT_COLOR;
    } catch(e) {
        //void
    }
    document.getElementById("treeview_td_"+id).style.backgroundColor = FILE_WINDOW_ITEM_BACKGROUND;
    document.getElementById("treeview_td_"+id).style.color = FILE_WINDOW_ITEM_COLOR;
    eval(treeview+" = "+id+";");

}//highlightTreeview