/*
 * eBusiness Solution
 *
 * (C) TourOne Systems - www.TourOne.de
 *
 * $Id: toshtml.js,v 1.24 2010/12/01 13:25:02 jott Exp $
 *
 */

function cloneAndRename(id, replace_it, with_this, emptyValues)
{
    var original = document.getElementById(id);
    if (original == null)
        return null;

    var newClone = original.cloneNode(true);
	
	if(typeof(emptyValues) == "undefined")
	{
		emptyValues = true;
	}	
    newClone = renameRecursive(newClone, replace_it, with_this, emptyValues);
  
    return newClone;
}

function renameRecursive(node, replace_it, with_this, emptyValues)
{
    if (typeof(replace_it) == "string")
        replace_it = eval('/' + replace_it + '/g');

    
    var current_node = node;

    while (current_node != null)
    {  		
        if (current_node.visited) 
        {
            current_node.visited = false;
        
            if (current_node == node)
                break;
            
            if (current_node.nextSibling)
                current_node = current_node.nextSibling;
            else
                current_node = current_node.parentNode;
        }
        else 
        {

            if (current_node.nodeType == 3)
            {
                current_node.nodeValue = current_node.nodeValue.replace(replace_it, with_this);
            }
            else
            {
                var tagName = current_node.tagName;

                if (current_node.name != null)
                    current_node.setAttribute("name", current_node.name.replace(replace_it, with_this));
					
                if (current_node.nodeName == "A" && current_node.href != null)
                    current_node.setAttribute("href", current_node.href.replace(replace_it, with_this));
					
//                var id = current_node.id;
                if(current_node && current_node.id && current_node.id.length > 0)
				{
/*					decho(current_node);
					decho(current_node.id);
					decho(current_node.nodeName);
*/					
                    current_node.id = current_node.id.replace(replace_it, with_this);
				}
				
                if(current_node.tagName == "LABEL" && current_node.attributes['for'] && current_node.attributes['for'].nodeValue)
                {
                    current_node.setAttribute("for",current_node.attributes['for'].nodeValue.replace(replace_it, with_this));
                }

                var onclick = current_node.onclick;
                if (onclick != null)
                { 	
                    var new_onclick = renameEventAttribute(onclick, 'onclick', replace_it, with_this);
                    current_node.setAttribute("onclick", new_onclick);
                    current_node.onclick = new Function(new_onclick);
                }

                var onfocus = current_node.onfocus;
                if (onfocus != null)
                { 	
                    var new_onfocus = renameEventAttribute(onfocus, 'onfocus', replace_it, with_this);
                    current_node.setAttribute("onfocus", new_onfocus);
                    current_node.onfocus = new Function(new_onfocus);
                }

                if (tagName == "INPUT" || tagName == "SELECT")
                {

                    var value = current_node.getAttribute("value");
                    if(emptyValues && value != null && current_node.tagName == "INPUT" && current_node.getAttribute("type") == "text")
                        current_node.value = '';

                    var onchange = current_node.onchange;
                    if (onchange != null)
                    { 	
                        var new_onchange = renameEventAttribute(onchange, 'onchange', replace_it, with_this);
                        current_node.setAttribute("onchange", new_onchange);
                        current_node.onchange = new Function(new_onchange);
                    }
                }
            }
            
            if (current_node.firstChild && tagName != "SELECT") 
            {
                current_node.visited = true;
                current_node = current_node.firstChild;
            }
            else if (current_node.nextSibling)
                current_node = current_node.nextSibling;
            else
                current_node = current_node.parentNode;
        }
        

    }

    return node;
}


function renameEventAttribute(attribute, name, replace_it, with_this)
{	                                            
    var str = attribute.toString().replace('function anonymous()',''); //IE6 + IE7
    str = str.replace('function ' + name + '()', ''); //IE8
    str = str.replace('function ' + name + '(event)', ''); //FF

    return str.replace(replace_it, with_this);
}


function showLoadDiv()
{
    var obj = document.getElementById('AJAX_LOADER');
    if (obj == null)
        return;

    var pageX = (document.all)?document.body.offsetWidth:window.innerWidth;
    var pageY = (document.all)?document.body.offsetHeight:window.innerHeight;
 
    obj.style.display = '';

    var objW = obj.offsetWidth;
    var objH = obj.offsetHeight;
    obj.style.position = 'absolute';
    obj.style.left = ((pageX/2)-(objW/2) + Number(document.body.scrollLeft))+"px";
    obj.style.top = ((pageY/2)-(objH/2)  + Number(document.body.scrollTop))+"px";
 
}

function hideLoadDiv()
{
    var obj = document.getElementById('AJAX_LOADER');
    if (obj == null)
        return;

    obj.style.display = 'none';
}

function appendToParent(bruderid, neuer, offset)
{
    var parent_obj = document.getElementById(bruderid).parentNode;
    
    if (typeof(offset) == "undefined" || offset == 0)
    {
        parent_obj.appendChild(neuer);
    }
    else
    {
        var i = parent_obj.childNodes.length  - 1 - offset;
        parent_obj.insertBefore(neuer, parent_obj.childNodes[i]);
    }
}

function removeFromParent(nodeid)
{
    var node = document.getElementById(nodeid);
    if (node == null)
        return;
    node.parentNode.removeChild(node);
}

function removeNodeFromParent(node)
{
    if (node == null)
        return;
    
    node.parentNode.removeChild(node);
}

function removeAllChilds(node)
{
    if (node.hasChildNodes())
    {
        for (var i = node.childNodes.length; i >= 0; i--)
        {
            removeNodeFromParent(node.childNodes[i]);
        }
    }
}

function createInput(type, name, value, size, className, disabled)
{
    var node = document.createElement('input');

    node.setAttribute('name', name);

    if (size != null)
        node.setAttribute('size', size);
    
    if (value != null)
        node.setAttribute('value', value);

    if (className != null)
        node.className = className;

    if (disabled != null)
        node.disabled = true;

    return node;
}

function IncValue(idName)
{
	var zz = document.getElementById(idName).value;
	document.getElementById(idName).value = parseInt(zz)  + 1;
}

function DecValue(idName)
{
	var zz = document.getElementById(idName).value;
	document.getElementById(idName).value = parseInt(zz)  - 1;
}

function createHidden(_name, _val)
{
	objHID = document.createElement('input');
	objHID.setAttribute('type', 'hidden');
	objHID.setAttribute('name', _name);
	objHID.setAttribute('id', _name);
	objHID.value = _val;
	return objHID;
}

function cOverlay(id_overlay, position, offsetX, offsetY)
{
    this.position = position;
    this.overlay = document.getElementById(id_overlay);
    this.iframe = null;

    this.overlay.style.position = 'absolute';
    this.overlay.style.zIndex = 100;

    this.opened = false;
    this.mouseovered = false;

	if (typeof(offsetX) == 'undefined')
		this.offsetX = 0;
	else
		this.offsetX = offsetX;

	if (typeof(offsetY) == 'undefined')
		this.offsetY = 0;
	else
		this.offsetY = offsetY ;

    this.mouseover = function()
    {
        this.mouseovered = true;
    };

    this.mouseout = function()
    {
        this.mouseovered = false;
    };

    this.overlay.onmouseover = this.mouseover.bind(this);
    this.overlay.onmouseout = this.mouseout.bind(this);

    this.open = function(el)
    {
        var el_position = this.getPosition(el);
        switch(this.position)
        {
            case "left_below":
                var x = Number(el_position[0]);
                var y = Number(el_position[1]) + Number(el.offsetHeight);
                break;
            case 'right_top':
            	var x = Number(el_position[0]) + Number(el.offsetWidth);
            	var y = Number(el_position[1]);
            	break;
              
        }
        
        x += Number(this.offsetX);
        y += Number(this.offsetY);
        
        this.overlay.style.left = x + 'px';
        this.overlay.style.top = y + 'px';

        this.overlay.style.display = '';

        if (navigator.appName.indexOf("Explorer") != -1)
        {
            if (this.iframe == null)
            {
                var hidder = document.getElementById('hidder_' + this.overlay.id);
                if (hidder == null)
                {
                    hidder = document.createElement('iframe');
                    hidder.id = "hidder_" + this.overlay.id;

                    this.overlay.parentNode.appendChild(hidder);
                }

                this.iframe = hidder;
            }
            
            this.iframe.style.position = 'absolute';
            this.iframe.style.left = x + 'px';
            this.iframe.style.top = y + 'px';
            this.iframe.style.width = this.overlay.offsetWidth + 'px';
            this.iframe.style.height = this.overlay.offsetHeight + 'px';
            this.iframe.zIndex = 99;
            this.iframe.style.display = '';
        }

        this.opened = true;
    };

    this.close = function() 
    {
        if (this.iframe != null)
            this.iframe.style.display = 'none';

        this.opened = false;

        this.overlay.style.display = 'none';
    };

    this.switchIt = function()
    {
        if(this.opened)
            this.close();
    };

    this.clear = function()
    {
        removeAllChilds(this.overlay);
    };

    this.getPosition = function(el, absoluteBreak)
    {
        if (typeof(el.offsetParent) != 'undefined')
        {
            for (var x = 0, y = 0, ebene = 0; el; el = el.offsetParent, ebene++)
            {
                if (absoluteBreak && ebene > 0)
                    if (this.style_position(el) == 'absolute')
                        return [x, y];

                x += Number(el.offsetLeft);
                y += Number(el.offsetTop);

                for (var el2 = el; el2 != el.offsetParent; el2 = el2.parentNode)
                {
                    var position = this.getScroll(el2, x, y);

                    x = position[0];
                    y = position[1];

                }

            }

            return this.getScroll(false, x, y);
        }
        else
        {
            var position = this.getScroll(el, el.x, el.y);
            return this.getScroll(false, position[0], position[1]);
        }

    };

    this.getScroll = function(el, x, y)
    {
        if (el)
        {
            if (typeof(el.scrollLeft) != "undefined")
                x -= el.scrollLeft;
            
            if (typeof(el.scrollTop) != "undefined")
                y -= el.scrollTop;
        }
        else
        {
            if (document.compatMode && document.compatMode == "CSS1Compat") 
            {
                x += document.documentElement.scrollLeft;
                y += document.documentElement.scrollTop;
            } 
            else 
            {
                x += document.body.scrollLeft;
                y += document.body.scrollTop;
            }


        }

        return [x, y];
    };

    this.style_position = function(el)
    {
        if (window.getComputedStyle)
            return window.getComputedStyle(el, "").getPropertyValue("position");
        else if (el.currentStyle)
            return el.currentStyle.position;
        else
            return null;
    };
    
}

