// Javascript functions
// @author: Gustavo Solt
// @date: 05/2006

// Marker and pointer for list view
var marked    = new Array();
var allRows   = new Array();

function hiliOn(tr, i, hiliColor, markColor) {
	if (typeof(marked[i]) == 'undefined' || !marked[i]) {
		tr.style.backgroundColor = hiliColor;
	}
}

function hiliOff(tr, i, hiliColor, markColor) {
	if (typeof(marked[i]) == 'undefined' || !marked[i]) {
		tr.style.backgroundColor = allRows[i][0];
	}
}

function marker(tr, i, hiliColor, markColor) {
	if (typeof(marked[i]) == 'undefined' || !marked[i]) {
		marked[i] = true;
		tr.style.backgroundColor = markColor;
	}
	else {
		marked[i] = false;
		tr.style.backgroundColor = allRows[i][0];
	}
}

function selectAll(markColor) {
	for (var i in allRows) {
        if (parseInt(i) > 0) {
    		marked[i] = true;
            if (document.getElementById('ID_'+i) != undefined) {
    	    	document.getElementById('ID_'+i).style.backgroundColor = markColor;
            }
        }
	}
}

function deselectAll() {
	marked = new Array;
	for (var i in allRows) {
        if (parseInt(i) > 0) {
            if (document.getElementById('ID_'+i) != undefined) {
                document.getElementById('ID_'+i).style.backgroundColor = allRows[i][0];
            }
        }
	}
}

// Context menu
var recID;
var menuName;
var column;
var menuStatus
var menuWidth = new Array();

function startMenu(m,i,c) {

	if (typeof(marked[i]) == 'undefined' || !marked[i]) {
        // exlude menu1(boton area) and menu2(header field) 
        if ((i != '')&&(m == 'menu3')) {
    		deselectAll();
	    	marked[i] = true;
		    document.getElementById(i).style.backgroundColor = markColor;
        }
	}

	ie5=(document.getElementById && document.all && document.styleSheets)?1:0;
	nn6=(document.getElementById && !document.all)?1:0;
	if(menuStatus == 1)doHide();
	menuName = m;
	recID = i;
	column = c;
	var mWid = 200;
	if(m == 'menu2'){
		mWid = 150;
	}
	menuWidth[m] = mWid;
	menuHeight=90;
	menuStatus=0;
	document.oncontextmenu=showMenu;
	document.onmouseup=hideMenu;
}


function showMenu(e) {

	cMenu = document.getElementById(menuName);
	cMenu.style.left = xMousePos + "px";
	cMenu.style.top  = yMousePos + "px";
	menuStatus = 1;
	return false;

}

function hideMenu(e) {
	if (menuStatus==1 && ((ie5 && event.button==1) || (nn6 && e.which==1))) setTimeout("doHide()",250);
}

function doHide() {
	document.getElementById(menuName).style.top = -750+"px";
	menuStatus = 0;
	document.oncontextmenu = nop;
}

function doLink(url, target, msg) {
	if (msg) {
        msg = remplace_simbol(msg,'+', ' ');
		if (!confirm(msg)) return
	}
	url = url + recID + '&' + SID; //Ich habe es wieder rein, weil sonst das sortieren usw. nicht mehr funktioniert
	switch (target) {
		case "_blank":
            new_win(url);
    		break;
		case "_top":
            new_win(url);
	    	break;
		default:
	    	location.href = url;
		    break;
	}
}

function proc_marked(url, target, msg) {
	var list = "";
	for (var i in marked) {
        if (parseInt(i) > 0) {
    		var is = i.split('xxx');
	    	var i1 = i;
		    if (is[1]>0) {
    			i1 = is[1];
	    	}
		    if (marked[i]) {
    			list = list + i1 + "-";
	    	}
        }
	}
	if (list != "") {
		if(msg) {
            msg = remplace_simbol(msg,'+', ' ');
			if (!confirm(msg)) return;
		}
		url = url + list.replace(/-$/,"");
		switch (target) {
			case "_blank":
                new_win(url);
    			break;
			case "_top":
                new_win(url);
	    		break;
			default:
	    		location.href = url;
		    	break;
		}
	}
}

function proc_marked_only_one(url, target, msg) {
	var list = "";
    var first = 0;
	for (var i in marked) {
        if (parseInt(i) > 0) {
    		var is = i.split('xxx');
	    	var i1 = i;
		    if (is[1]>0) {
    			i1 = is[1];
	    	}
		    if (marked[i]) {
                if (first == 0) {
        			list = i1;
                    first = 1;
                } else {
                    msg = remplace_simbol(msg,'+', ' ');
                    alert(msg);
                    return;
                }
	    	}
        }
	}
	if (list != "") {
		url = url + list.replace(/-$/,"");
		switch (target) {
			case "_blank":
                new_win(url);
    			break;
			case "_top":
                new_win(url);
		    	break;
			default:
			    location.href = url;
    			break;
		}
	}
}

function proc_marked_ajax(page, parameters, msg) {
	var list = "";
	for (var i in marked) {
        if (parseInt(i) > 0) {
    		var is = i.split('xxx');
	    	var i1 = i;
		    if (is[1]>0) {
    			i1 = is[1];
	    	}
		    if (marked[i]) {
    			list = list + i1 + "-";
	    	}
        }
	}
	if (list != "") {
		if(msg) {
            msg = remplace_simbol(msg,'+', ' ');
			if (!confirm(msg)) return;
		}
		list = list.replace(/-$/,"");

        var param = new Array();
        var params = parameters.split(',');
        param[0] = params[0];
        param[1] = params[1];
        var http_server = params[2];
        ajax(page, param, http_server, list);
	}
}

function proc_marked_none(url, target, msg) {
	switch (target) {
		case "_blank":
            new_win(url);
		    break;
		case "_top":
            new_win(url);
			break;
		default:
			location.href = url;
			break;
	}
}

function nop() {}

// Set Netscape up to run the "captureMousePosition" function whenever
// the mouse is moved. For Internet Explorer and Netscape 6, you can capture
// the movement a little easier.
if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}

// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page
xTop = 0; // Right Corner
yTop = 0; // Top Corner

function captureMousePosition(e) {
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has 
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth + window.pageXOffset;
        yMousePosMax = window.innerHeight + window.pageYOffset;
        xTop =  xMousePosMax - 125;
        yTop =  window.pageYOffset;
    } else if (document.all) {
        // When the page scrolls in IE, the event's mouse position 
        // reflects the position from the top/left of the screen the 
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no 
        // matter if the user has scrolled or not.
        xMousePos = event.clientX + document.documentElement.scrollLeft;
        yMousePos = event.clientY + document.documentElement.scrollTop;
        xMousePosMax = document.documentElement.clientWidth + document.documentElement.scrollLeft;
        yMousePosMax = document.documentElement.clientHeight + document.documentElement.scrollTop;
        xTop = xMousePosMax - 125;
        yTop = document.documentElement.scrollTop;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard 
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth + window.pageXOffset;
        yMousePosMax = window.innerHeight + window.pageYOffset;
        xTop =  xMousePosMax - 125;
        yTop =  window.pageYOffset;
    }
}

function showAjaxStatus(id,xPos,yPos) {
    if (xPos == undefined) {
        xPos = xTop;
    }
    if (yPos == undefined) {
        yPos = yTop;
    }

    df = document.getElementById(id);
	df.style.left = xPos + "px";
    df.style.top  = yPos + "px";
    Element.show(id);
}

//alternative view
function show_alternative_view(text, width, height, id) {
	df = document.getElementById(id);
	if (typeof width != "undefined") {
        df.style.width = width+"px";
    }
	if (typeof height != "undefined") {
        df.style.height = height+"px";
    }

	df.innerHTML        = text;
	xPos = xMousePos + 10;
	yPos = yMousePos - 20;

	df.style.left       = xPos + "px";
    df.style.top        = yPos + "px";
	df.style.display    = 'block';
	df.style.visibility = 'visible';

	return false;
}

function hide_alternative_view(id) {
	df = document.getElementById(id);
	df.style.visibility = "hidden";
	df.style.display    = "none";
	df.style.left       = "-750px";
	df.style.width      = '';
}

function new_win(link) {
    var dp = window.open(link,"dp","left=200,top=10,width=600,height=600,toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1");
    dp.focus();
    return false;
}

var reload_count  = 0;
var reload_tId    = "";
var reload_stopIt = false
function reloadPage() {
    if ((reload_count++) >= 24 || reload_stopIt) {
        return;
    }
    window.location.reload(true);
    reload_tId = setTimeout('reloadPage()',5000)
}
function stopReload() {
    reload_stopIt = true;
}
function continueReload() {
    reload_stopIt = false;
    reload_tId = setTimeout('reloadPage()',5000)
}


/**
 * Move selected items from source select to target select
 *
 * @param string source - Name of the source select field
 * @param string target - Name of the target select field
 * @return void
 */
function moveOption(source,target) {
    var moved = 'no';
    
    var mytarget = getElement(target);
    
    if (mytarget) {
        var mysource = getElement(source);
        if (mysource) {
            do{
                if (mysource.selectedIndex >= 0 && mysource.options[mysource.selectedIndex].value != '') {
                    newtext = mysource.options[mysource.selectedIndex].text;
                    newvalue = mysource.options[mysource.selectedIndex].value;
                    size = mytarget.options.length;
                    newoption = new Option(newtext,newvalue,false,false);
                    mytarget.options[size] = newoption;
                    mytarget.options[size].selected = true;
                    mysource.options[mysource.selectedIndex] = null;
                    mysource.value = '';
                    mysource.focus();
                    moved = 'yes';
                }
            } while (mysource.type == 'select-multiple' && mysource.selectedIndex >= 0);
        }
    }
}

/**
 * Get document element
 *
 * @param string n - Name of the field
 * @param string d - document element
 * @return form element
 */
function getElement(n, d) {
    var p,i,x;
    if (!d) {
        d = document;
    }

    if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p+1)].document;
        n = n.substring(0,p);
    }
    if (!(x=d[n]) && d.all) {
        x = d.all[n];
    }
    for (i = 0;!x && i < d.forms.length;i++) {
        x = d.forms[i][n];
    }
    for (i = 0;!x && d.layers && i < d.layers.length;i++) {
        x = getElement(n,d.layers[i].document);
    }
    if (!x && d.getElementById) {
        x = d.getElementById(n);
    }
    return x;
}

/**
 * Select all the items
 *
 * @param string name - Name of the field
 * @return void
 */
function selector_selectAll(name) {
    var myfield = getElement(name);
    if (myfield) {
        for(i = 0;i < myfield.options.length;i++) {
            myfield.options[i].selected = true;
        }
    }
}

/**
 * Move selected items to up or down in the select
 *
 * @param string source    - Name of the source select field
 * @param string direction - up/down
 * @return void
 */
function movePosOption(source,direction) {
    var mysource = getElement(source);
    if (mysource) {
        if (mysource.selectedIndex >= 0 && mysource.options[mysource.selectedIndex].value != '' && mysource.length > 1) {
            actualPos = mysource.selectedIndex;
            if (direction == 'up') {
                nextPos = actualPos - 1;
            } else { 
                nextPos = actualPos + 1;
            }
                
            if (nextPos > -1 && nextPos < mysource.length) {
                actualText = mysource.options[actualPos].text;
                actualValue = mysource.options[actualPos].value;

                nextText = mysource.options[nextPos].text;
                nextValue = mysource.options[nextPos].value;
                
                mysource.options[nextPos].text  = actualText;
                mysource.options[nextPos].value = actualValue;
                
                mysource.options[actualPos].text  = nextText;
                mysource.options[actualPos].value = nextValue;
                
                mysource.value = actualValue;
                mysource.selectedIndex = nextPos;
                mysource.focus();
            }
        }
    }
}

/**
 * Show/Hide a div block
 *
 * @param string field    - Field id to display/hide
 * @return void
 */
function display_block(field) {
    e = document.getElementById(field);
    if (e.style.display == 'inline') {
        e.style.display = 'none';
    } else {
        e.style.display = 'inline';
    }
}

/**
 * Delete + for contextmenu options
 *
 * @param string  - String to convert
 * @param symbol1  - Symbol to remplace
 * @param symbol2  - Symbol for remplace
 * @return string - Converted String
 */
function remplace_simbol(text, symbol1, symbol2) {
    var tstring = "";
    text = symbol1+text;
    splittext = text.split(symbol1);
    for(i=0; i< splittext.length;i++) {
        tstring += symbol2+splittext[i];
    }
    return tstring;
}
