// JavaScript Document
/**
 * @file global.js
 * @package MB4.1
 * @package MBLibs
 * @author Maurice Bischoff
 * @version 2.1    
 */
// Global Variables
var mouseX;
var mouseY;
var screenX;
var screenY;
var activeForm;
var activeElement;
var activeElementID;

//document.all.tags("button")[0].blur();
function setMousePos(mouseEvent)
{
	if(navigator.appName == 'Netscape') 
	{
        mouseX = mouseEvent.pageX;
        mouseY = mouseEvent.pageY;
	}else{
		mouseX = window.event.clientX;
		mouseY = window.event.clientY;
	}
	//showStatus();
}

function setWindowSize()
{
    if (self.innerHeight) // all except Explorer
    {
    	screenX = self.innerWidth;
    	screenY = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    	// Explorer 6 Strict Mode
    {
    	screenX = document.documentElement.clientWidth;
    	screenY = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
    	screenX = document.body.clientWidth;
    	screenY = document.body.clientHeight;
    }
}

function showStatus()
{
    window.status = screenX + 'x' + screenY + ' | mouseX: ' + mouseX + ' mouseY: ' + mouseY; 
    return true;
}

setWindowSize();
window.status = screenX + 'x' + screenY;
document.onmousedown = setMousePos;

function showOptions(myID, Xpos, Ypos)
{
    if (Xpos == 0) {Xpos = mouseX;}
    if (Ypos == 0) {Ypos = mouseY;}
	if(document.getElementById(myID))
	{
		document.getElementById(myID).style.display = 'none';
		document.getElementById(myID).style.top = Ypos + 'px';
		document.getElementById(myID).style.left = Xpos + 'px';
		document.getElementById(myID).style.display = 'block';
	}
}

function closeOption(myID)
{
	document.getElementById(myID).style.display = 'none';
}

function toggle(toggleid)
{
	 if(document.getElementById(toggleid))
	 {
		if(document.getElementById(toggleid).style.display == 'block')
		{
			document.getElementById(toggleid).style.display = 'none';
			//setCookie(toggleid, "none");
		} else {
			document.getElementById(toggleid).style.display = 'block';
			//setCookie(toggleid, "block");
		}
	}
}

/*function treeToggle(toggleID, buttonID)
{
	 if(document.getElementById(toggleID))
	 {
		if(document.getElementById(toggleID).style.display == 'block')
		{
			document.getElementById(toggleID).style.display = 'none';
			document.getElementById(buttonID).style.background = 'url(../../images/close_package.png)';
			//setCookie(toggleid, "none");
		} else {
			document.getElementById(toggleID).style.display = 'block';
			document.getElementById(buttonID).style.background = 'url(../../images/open_package.png)';
			//setCookie(toggleid, "block");
		}
	}
}*/

function autoSubmit(submitFormular)
{
    document.getElementsByName(submitFormular)[0].submit();
}

function autoHeight(cellName, startID1, startID2)
{
	var ID1 = startID1;
	var ID2 = startID2;
	minHeigh = new Array();
	minHeigh[ID1] = 0;
	divID = cellName + ID1 + "-" + ID2;
	while(document.getElementById(divID))
	{
		if(!document.getElementById(divID)){alert("ID '" + divID + "' does not exist!");}
		while(document.getElementById(divID))
		{
			if(minHeigh[ID1] < document.getElementById(divID).offsetHeight)
			{
				minHeigh[ID1] = document.getElementById(divID).offsetHeight;
			}
			ID2++;
			divID = cellName + ID1 + "-" + ID2; 
		}
		ID1++;
		ID2 = startID2; 
		minHeigh[ID1] = 0;
		divID = cellName + ID1 + "-" + ID2;
	}
	ID1 = startID1;
	ID1 = startID2;
	divID = cellName + ID1 + "-" + ID2; 
	while(document.getElementById(divID))
	{
		while(document.getElementById(divID))
		{
			document.getElementById(divID).style.height = minHeigh[ID1]+"px";
			ID2++;
			divID = cellName + ID1 + "-" + ID2; 
		}
		ID1++;
		ID2 = startID2; 
		divID = cellName + ID1 + "-" + ID2;
	}
}

function getMaxHeight(cellName, startID1, startID2)
{
	var ID1 = startID1;
	var ID2 = startID2;
	var maxHeigh = 0;
	divID = cellName + ID1 + "-" + ID2;
	while(document.getElementById(divID))
	{
		if(maxHeigh < document.getElementById(divID).offsetHeight)
		{
			maxHeigh = document.getElementById(divID).offsetHeight;
		}
		ID2++;
		divID = cellName + ID1 + "-" + ID2; 
	}
	return maxHeigh;
}

function centrix(IDName, curID1, curID2, opWidth)
{
	var maxHeight = 0;
	var maxWidth  = 0;
	var ID1 = curID1;
	var ID2 = curID2;
	ID2++;
	divID = IDName + ID1 + '-' + ID2;
	while(document.getElementById(divID))
	{
		if(maxHeight < document.getElementById(divID).offsetHeight){
			maxHeight = document.getElementById(divID).offsetHeight;
		}
		ID2++;
		divID = IDName + ID1 + '-' + ID2;
	}
	divID = IDName + curID1 + '-' + curID2;
    if(opWidth > 0)
    {
        maxWidth = opWidth;
    }else{
        maxWidth = document.getElementById(IDName + curID1).offsetWidth;
    }
	document.getElementById(IDName + ID1 + '-1').style.width = ((maxWidth - document.getElementById(IDName + ID1 + '-2').offsetWidth) / 2) + 'px';
	document.getElementById(IDName + ID1 + '-1').style.height = maxHeight + 'px';
}

function changeInput(ciForm, ciName, ciValue)
{
    document.forms[ciForm].elements[ciName].value = ciValue;
}

function changeAndSubmit(ciForm, ciName, ciValue)
{
    changeInput(ciForm, ciName, ciValue);
    document.forms[ciForm].submit();
}

function checkSubmit(message, formularName, cmdValue){
	Check = confirm(message);
	if(Check == true){
		document.getElementsByName(formularName)[0].cmd.value = cmdValue;
		document.getElementsByName(formularName)[0].submit();
	}
}

function setLoginData(newCmd)
{
    //alert(document.getElementsByName('loginForm')[0].name);
    document.getElementsByName('loginForm')[0].cmd.value = newCmd;
    document.getElementsByName('loginForm')[0].submit();
}

function containerFloatHeight(tagID, i, endVal, speed)
{
    document.getElementById(tagID).style.height = i * speed + "px";
    if(i < endVal)
    {
        i++;
        window.setTimeout("containerFloatHeight('" + tagID + "'," + i + "," + endVal + "," + speed + ")", 0);
    }else{
        return i;
    }
}    

/**
 * @brief openMenu()
 * @author Maurice Bischoff
 * @version 1.1 
 */
function openMenu(tagID, endY, closeThenOpen, speed)
{
    closeMenuAlso(tagID);
    if(document.getElementById(tagID).style.display != "none")
    {
        if(closeThenOpen == 0)
        {
            document.getElementById(tagID).style.display = "none";
        }
    }else{
        document.getElementById(tagID).style.display = "block";
        var endVal = 0;
        endVal = endY / speed;
        containerFloatHeight(tagID, 0, endVal, speed);
        //document.getElementById(tagID + "Content").style.display = "block";
    }
}

/**
 * @brief openTag()
 * @author Maurice Bischoff
 * @version 1.1 
 */
function openTag(tagID)
{
    closeMenuAlso(tagID);
    document.getElementById(tagID).style.display = "block";
    /*if(document.getElementById(tagID).style.display != "none")
    {
        document.getElementById(tagID).style.display = "none";
    }else{
        document.getElementById(tagID).style.display = "block";
    }*/
}

/**
 * @brief getIDName()
 * @author Maurice Bischoff
 * @version 1.0 
 */
function getIDName(getIDName_tagID)
{
    getIDName_i = 0;
    getIDName_tagName = "";
    
    for(getIDName_i = 0; getIDName_i < getIDName_tagID.length; getIDName_i++)
    {
        if(navigator.appName == "Netscape")
        {
            if(isNaN(getIDName_tagID[getIDName_i]))
            {
                getIDName_tagName += getIDName_tagID[getIDName_i];
            }
        }else{
            if(isNaN(getIDName_tagID.substr(i, 1)))
            {
                getIDName_tagName += getIDName_tagID.substr(getIDName_i, 1);
            }
        }
    }
    return getIDName_tagName;
}

/**
 * @brief getIDNumber()
 * @author Maurice Bischoff
 * @version 1.0 
 */
function getIDNumber(getIDNumber_tagID)
{
    getIDNumber_i = 0;
    getIDNumber_tagNumber = "";
    
    for(getIDNumber_i = 0; getIDNumber_i < getIDNumber_tagID.length; getIDNumber_i++)
    {
        if(navigator.appName == "Netscape")
        {
            if(!isNaN(getIDNumber_tagID[getIDNumber_i]))
            {
                getIDNumber_tagNumber += getIDNumber_tagID[getIDNumber_i];
            }
        }else{
            if(!isNaN(getIDNumber_tagID.substr(i, 1)))
            {
                getIDNumber_tagNumber += getIDNumber_tagID.substr(getIDNumber_i, 1);
            }
        }
    }
    return parseInt(getIDNumber_tagNumber);
}

/**
 * @brief closeMenuAlso()
 * @author Maurice Bischoff
 * @version 1.1 
 */
function closeMenuAlso(tagID)
{
    var i = 1;
    var curID = 0;
    var tagName = "";
    
    for(i = 0; i < tagID.length; i++)
    {
        if(navigator.appName == "Netscape")
        {
            if(isNaN(tagID[i]))
            {
                    tagName += tagID[i];
            }
        }else{
            if(isNaN(tagID.substr(i, 1)))
            {
                    tagName += tagID.substr(i, 1);
            }
        }
    }
    
    i = 1;
    curID = tagName + i;
            //alert(tagID + " : " + curID);
    while(document.getElementById(curID))
    {
        if(tagID != curID)
        {
            document.getElementById(curID).style.height = "0px";
            document.getElementById(curID).style.display = "none";
        }
        i++;
        curID = tagName + i;
    }
}

/**
 * @brief checkMessageHeadline()
 * @author Maurice Bischoff
 * @version 1.0 
 */
function checkMessageHeadline(curString)
{
    curString = curString.replace(/re: /g,"");
    curString = curString.replace(/RE: /g,"");
    return curString.replace(/Re: /g,"");
}

/**
 * @brief setCurrentInput()
 * @author Maurice Bischoff
 * @version 1.0 
 */
function setCurrentInput(formName, elementName)
{
    activeForm = formName;
    activeElement = elementName;
}

/**
 * @brief setCurrentInputByID()
 * @author Maurice Bischoff
 * @version 1.0 
 */
function setCurrentInputByID(elementID)
{
    activeElementID = elementID;
}

/**
 * @brief setInputByID()
 * @author Maurice Bischoff
 * @version 1.0 
 */
function setInputByID(curValue)
{
    document.getElementById(activeElementID).value = curValue;
}

/**
 * @brief sortTopPos()
 * @author Maurice Bischoff
 * @version 1.1 
 */
function getTopNOStr(getTopNOStr_tagName)
{
    getTopNOStr_topCnt = 1;
    getTopNOStr_curTopID = getTopNOStr_tagName + getTopNOStr_topCnt;
    getTopNOStr_topNOs = new Array();
    // save IDs in Array
    while(document.getElementById(getTopNOStr_curTopID))
    {
        getTopNOStr_topNOs.push(document.getElementById(getTopNOStr_curTopID).value);
        getTopNOStr_topCnt++;
        getTopNOStr_curTopID = getTopNOStr_tagName + getTopNOStr_topCnt;
    }
    //alert(getTopNOStr_topNOs.join(" "));
    return getTopNOStr_topNOs.join(" ");                // convert array in a string sequence
}

/** 
 * @brief getMissingTop5()
 * @author Maurice Bischoff
 * @version 1.0 
 */
function getMissingTop5(topNOstr)
{
    missingNO = 1;
    while(topNOstr.search(missingNO) != -1)
    {
        missingNO++;
    }
    return missingNO;
}

/**
 * @brief sortTopPos()
 * @author Maurice Bischoff
 * @version 1.3 
 */
function sortTopPos(topID)
{
    topCnt = 1;
    tagName = getIDName(topID);
    curTopID = tagName + topCnt;
    topNO = document.getElementById(topID).value;
    NObefore = topNO;
    topNOs = new Array();
    checkStr = "";

    topNOstr = getTopNOStr(tagName);                // convert array in a string sequence
    missingNO = getMissingTop5(topNOstr);           // search missing ID
    checkStr = "b | m x c -> to;\n" + NObefore + " | " + missingNO + " 0 " + topNO;

    topCnt = 1;
    curTopID = tagName + topCnt;
    topNOstr = "";
    curTopNOstr = "";

    while(document.getElementById(curTopID))        // sort IDs
    {
        curTopNOstr = getTopNOStr(tagName);
        curTopNo = document.getElementById(curTopID).value;
        checkStr += "\n" + NObefore + " | " + missingNO + " " + curTopNo + " " + topNO;
        if(curTopID != topID)
        {
            if(curTopNo <= topNO && curTopNo > missingNO)
            {
                document.getElementById(curTopID).value = missingNO;
                missingNO = getMissingTop5(getTopNOStr(tagName));           // search missing ID
            }else if(curTopNo >= topNO && curTopNo < missingNO){
                curTopNo = parseInt(document.getElementById(curTopID).value) + 1;
                while(curTopNOstr.search(curTopNo) != -1)
                {
                    curTopNo++;
                }
                document.getElementById(curTopID).value = curTopNo;
                missingNO = getMissingTop5(getTopNOStr(tagName));           // search missing ID
            }else if(curTopNo == NObefore && curTopNo < topNO && curTopNo < missingNO){
                document.getElementById(curTopID).value = missingNO;
                missingNO = getMissingTop5(getTopNOStr(tagName));           // search missing ID
            }else if(curTopNo > topNO && curTopNo > missingNO){
                curTopNo = parseInt(document.getElementById(curTopID).value) + 1;
                while(curTopNOstr.search(curTopNo) != -1)
                {
                    curTopNo++;
                }
                document.getElementById(curTopID).value = curTopNo;
                missingNO = getMissingTop5(getTopNOStr(tagName));           // search missing ID
            }
            if(NObefore > 1)
            {
                NObefore = document.getElementById(curTopID).value;
            }
            checkStr += " -> " + document.getElementById(curTopID).value;
            topNOs.push(document.getElementById(curTopID).value);
            topNOstr = topNOs.join(" ");
        }
        topCnt++;
        curTopID = tagName + topCnt;
    }
    //alert(checkStr);
}

/**
 * @brief setBBCode()
 * @author Maurice Bischoff
 * @version 1.1 
 */
function setBBCode(startTag, endTag)
{
    if(arguments[2]) 
    {
        spaceBefore = arguments[2];
    }else if(setBBCode.arguments[2]){
        spaceBefore = setBBCode.arguments[2];
    }else{
        spaceBefore = '0';
    }
    switch(spaceBefore)
    {
        case '1':
            startTag = " " + startTag;
        break;
        case '2':
            endTag = " " + endTag;
        break;
        case '3':
            startTag = " " + startTag;
            endTag = " " + endTag;
        break;
        default:
    }
    formName = activeForm;
    elementName = activeElement;
    curText = document.forms[formName].elements[elementName].value;
    if (window.getSelection || document.getSelection) 
    {
        startPos = document.forms[formName].elements[elementName].selectionStart;
        endPos   = document.forms[formName].elements[elementName].selectionEnd;
        prevString = curText.substr(0,startPos);
        oldString = curText.substr(startPos, endPos - startPos);
        nextString = curText.substr(endPos);
        document.forms[formName].elements[elementName].value = prevString + startTag + oldString + endTag + nextString;
    } else if (document.selection) 
    {
    	oldString = document.selection.createRange().text;
    	if(oldString == "")
    	{
    	   document.forms[formName].elements[elementName].value += startTag + oldString + endTag;
        }else{
    	   document.selection.createRange().text = startTag + oldString + endTag;
    	}
    }
}
