var oElement = null;
var oOldElement = null;
var iPosX = 0;
var iPosY = 0;

function initEvent() {
	bodyEvent = new cMouseEvent(document.getElementsByTagName("body")[0], getMousePosition, cME_CLICK);
}

function getMousePosition(ereignis) {
	iPosX = ereignis.absCrds.x;
	iPosY = ereignis.absCrds.y;
	
    if(oElement != null) {
		iElementWidth = oElement.offsetWidth;
		iElementHeight = oElement.offsetHeight;
		
        oElement.style.left = iPosX+iElementWidth > document.body.clientWidth ? iPosX-3-iElementWidth + "px" : iPosX+3 + "px";
        oElement.style.top = iPosY+iElementHeight > document.body.clientHeight ? iPosY-3-iElementHeight + "px" : iPosY+3 + "px";
    }
    
    resetPopup();
}

function showPopup(i_sId, i_sPopupRows, i_sPopupTitle) {
    hidePopup();
    oElement = document.getElementById(i_sId);
    oElement.style.display = 'block';

    oPopupRows = document.getElementById('popupRows');
    oPopupRows.innerHTML = i_sPopupRows;

    oPopupTitle = document.getElementById('popupTitle');
    oPopupTitle.innerHTML = i_sPopupTitle;

    oOldElement = oElement;
}

function hidePopup() {
    if(oOldElement != null) oOldElement.style.display = 'none';
}

function resetPopup() {
    oElement = null;
    iPosX = 0;
    iPosY = 0;
}

function deleteConfirm(i_sDescription, i_sLink, i_sComand) {
    Check = confirm(i_sDescription);
    if(Check == false) {
        window.location.href = i_sLink;
    }
    else {
        window.location.href = i_sLink + i_sComand;
    }
}


function deleteConfirmWithOutCommand(i_sDescription, i_sLink) {
    Check = confirm(i_sDescription);
    if(Check == true) {
        window.location.href = i_sLink;
    }
}

var pa_fChecked = new Array();	

function toggleCheckbox(ia_oElements, i_sPrefix, i_oImage, i_sSrc) {
    if (typeof(pa_fChecked[i_sPrefix]) == 'undefined') {
        var fChecked = true;
        for (var i = 0; i < ia_oElements.length; i++) {
            var oElement = ia_oElements[i];
            if (oElement.type == "checkbox" && oElement.name.indexOf(i_sPrefix) == 0) {
                if (!oElement.checked) {
                    fChecked = false;
                }
            }
        }
        pa_fChecked[i_sPrefix] = fChecked;
    }
    
    pa_fChecked[i_sPrefix] = !pa_fChecked[i_sPrefix];
    
    for (var i = 0; i < ia_oElements.length; i++) {
        var oElement = ia_oElements[i];
        if (oElement.type == "checkbox" && oElement.name.indexOf(i_sPrefix) == 0) {
            oElement.checked = pa_fChecked[i_sPrefix];
        }
    }
    
    if (pa_fChecked[i_sPrefix]) {
    	i_oImage.src = i_sSrc+'check_all_off.gif';
    }
    else {
        i_oImage.src = i_sSrc+'check_all_on.gif';
    }
}

function setDocumentValueChecked(sIDElement){
    oElement = document.getElementById(sIDElement);
    if(oElement.checked == true){
        oElement.checked = false;
    }
    else
    {
        oElement.checked = true;
    }
}




function MM_swapImgRestore() {
    var i, x, a = document.MM_sr;

    for(i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) {
        x.src = x.oSrc;
    }
}

function MM_preloadImages() {
    var d = document;

    if(d.images) {
        if(!d.MM_p) {
            d.MM_p = new Array();
        }

        var i, j = d.MM_p.length, a = MM_preloadImages.arguments;

        for(i=0; i<a.length; i++) {
            if (a[i].indexOf("#") != 0) {
                d.MM_p[j] = new Image;
                d.MM_p[j++].src = a[i];
            }
        }
    }
}

function MM_findObj(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 = MM_findObj(n,d.layers[i].document);
    }

    if(!x && d.getElementById) {
        x = d.getElementById(n);
    }

    return x;
}

function MM_swapImage() {
    var i, j = 0, x, a = MM_swapImage.arguments;
    document.MM_sr = new Array;

    for(i = 0; i < (a.length-2); i += 3) {
        if ((x = MM_findObj(a[i])) != null) {
            document.MM_sr[j++] = x;

            if(!x.oSrc) {
                x.oSrc = x.src;
            }

            x.src=a[i+2];
        }
    }
}

function openWindow(url)
{
    var breite=screen.availWidth;
    var hoehe=screen.availHeight;
    var positionX=((screen.availWidth / 2) - breite / 2);
    var positionY=((screen.availHeight / 2) - hoehe / 2);

    pop=window.open('','','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,fullscreen=0,width='+breite+',height='+hoehe+',top=10000,left=10000');
    pop.resizeTo(breite,hoehe);
    pop.moveTo(positionX,positionY);
    pop.location=url;
}

function openWindowWithPosition(url, name, width, height, left, top)
{
    var breite=width;
    var hoehe=height;
    var positionX=left;
    var positionY=top;

    pop = window.open(url,name,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,fullscreen=0,width='+breite+',height='+hoehe+',top='+positionY+',left='+positionX);
    pop.focus();
}

function openWindowWithSize(url,height,width)
{
    var breite=width;
    var hoehe=height;
    var positionX=((screen.availWidth / 2) - breite / 2);
    var positionY=((screen.availHeight / 2) - hoehe / 2);

    pop=window.open('','name','toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,fullscreen=0,width='+breite+',height='+hoehe+',top=10000,left=10000');
    pop.resizeTo(breite,hoehe);
    pop.moveTo(positionX,positionY);
    pop.location=url;
}

function toggle(img,arrayname,path){
    checkboxElement = document.getElementById(arrayname);
    if(checkboxElement.value == 1){
        checkboxElement.value = '0';
        image = document.getElementById(img);
        image.src = path+'check_all.gif';
    }
    else {
        checkboxElement.value = '1';
        image = document.getElementById(img);
        image.src = path+'check_all_on.gif';
    }
}

var sortedOn = 100;
	
function SortTable(sortOn) {

	var table = document.getElementById('results');
	var tbody = table.getElementsByTagName('tbody')[0];
	var rows = tbody.getElementsByTagName('tr');

	var rowArray = new Array();
	for (var i=0, length=rows.length; i<length; i++) {
		rowArray[i] = rows[i].cloneNode(true);
	}
	
	/*
	if (sortOn == sortedOn) { 
		rowArray.reverse(); 
	}
	else {
		sortedOn = sortOn;
		if (sortedOn == 0) {
			rowArray.sort(RowCompareNumbers);
		}
		else if (sortedOn == 3) {
			rowArray.sort(RowCompareDollars);
		}
		else {
			rowArray.sort(RowCompare);
		}
	}
	*/
	
	if (sortOn == sortedOn) { 
		rowArray.reverse(); 
	}
	else {
		sortedOn = sortOn;
		rowArray.sort(RowCompare);
	}
	
	var newTbody = document.createElement('tbody');
	for (var i=0, length=rowArray.length; i<length; i++) {
		newTbody.appendChild(rowArray[i]);
	}
	
	table.replaceChild(newTbody, tbody);
}

function isDate(sDate){
	if (sDate == "") { return false; }
	if (sDate.length > 10) { return false; }
	
	var m_arrDate = sDate.split(".");
	
	if (m_arrDate.length != 3) { return false; }
	
	var m_DAY = m_arrDate[0];
	var m_MONTH = m_arrDate[1];
	var m_YEAR = m_arrDate[2];
	
	if (m_DAY.length != 2) { return false; }
	if (m_MONTH.length != 2) { return false; }
	if (m_YEAR.length != 4) {	return false; }
	if (m_DAY > 31) { return false; }
	if (m_MONTH > 12) { return false; }
	
	return true;
}

function transformDate(sDate){
	var m_arrDate = sDate.split(".");
	var m_DAY = m_arrDate[0];
	var m_MONTH = m_arrDate[1];
	var m_YEAR = m_arrDate[2];

	return m_YEAR+m_MONTH+m_DAY;
}

function RowCompare(a, b) {

	var aVal = a.getElementsByTagName('td')[sortedOn].firstChild.nodeValue;
	var bVal = b.getElementsByTagName('td')[sortedOn].firstChild.nodeValue;
	
	if (isDate(aVal) && isDate(bVal)) {
		aVal = transformDate(aVal);
		bVal = transformDate(bVal);
	}
	
	aVal = aVal.toLowerCase();
	bVal = bVal.toLowerCase();
	
	return (aVal == bVal ? 0 : (aVal > bVal ? 1 : -1));
}

function RowCompareNumbers(a, b) {

	var aVal = parseInt(a.getElementsByTagName('td')[sortedOn].firstChild.nodeValue);
	var bVal = parseInt(b.getElementsByTagName('td')[sortedOn].firstChild.nodeValue);
	return (aVal - bVal);
}

function RowCompareDollars(a, b) {

	var aVal = parseFloat(a.getElementsByTagName('td')[sortedOn].firstChild.nodeValue.substr(1));
	var bVal = parseFloat(b.getElementsByTagName('td')[sortedOn].firstChild.nodeValue.substr(1));
	return (aVal - bVal);
}

var marked_row = new Array;

function markRowsInit() {
    // for every table row ...
	var rows = document.getElementsByTagName('tr');
	for ( var i = 0; i < rows.length; i++ ) {
	    // ... with the class 'odd' or 'even' ...
		if ( 'odd' != rows[i].className && 'even' != rows[i].className ) {
		    continue;
		}
	    // ... add event listeners ...
        // ... to highlight the row on mouseover ...
	    if ( navigator.appName == 'Microsoft Internet Explorer' ) {
	        // but only for IE, other browsers are handled by :hover in css
			rows[i].onmouseover = function() {
			    this.className += ' hover';
			}
			rows[i].onmouseout = function() {
			    this.className = this.className.replace( ' hover', '' );
			}
	    }
        // ... and to mark the row on click ...
		rows[i].onmousedown = function() {
		    var unique_id;
            var checkbox;
			
            checkbox = this.getElementsByTagName( 'input' )[0];
            if ( checkbox && checkbox.type == 'checkbox' ) {
                unique_id = checkbox.name + checkbox.value;
            } else if ( this.id.length > 0 ) {
                unique_id = this.id;
            } else {
		        return;
		    }

            if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                marked_row[unique_id] = true;
            } else {
            	marked_row[unique_id] = false;
            }

            if ( marked_row[unique_id] ) {
			    this.className += ' marked';
            } else {
			    this.className = this.className.replace(' marked', '');
            }
			/*
            if ( checkbox && checkbox.disabled == false ) {
                checkbox.checked = marked_row[unique_id];
            }
            */
		}
		
		// ... and disable label ...
		/*
		var labeltag = rows[i].getElementsByTagName('label')[0];
		if ( labeltag ) {
		    labeltag.onclick = function() {
		        return false;
		    }
	    }
	    */
	    
	    // .. and checkbox clicks
		/*
		var checkbox = rows[i].getElementsByTagName('input')[0];
		if ( checkbox ) {
		    checkbox.onclick = function() {
		        // opera does not recognize return false;
		        this.checked = ! this.checked;
		    }
	    }
	    */
	}
}	






