
var d = document;
var successOpt, ID;
var loopSwitch = 'off';


var CustomForm = {

    ini: function (form) {
        this.el = ['input', 'textarea', 'select'];
        this.elements = [];
        this.queryString = [];
        for (var i=0; i < this.el.length; i+=1) {
            this.elements.push($(form).getElementsByTagName(this.el[i]));
        }
        this._input();
        return(this.queryString.join('&'));
    },

    _input: function () {
        if (0 < this.elements[0].length) {
            for (var i=0; i < this.elements[0].length; i+=1) {
                if (this.elements[0][i].name.length) {
                    if ('hidden' == this.elements[0][i].type || 'text' == this.elements[0][i].type
                     || 'password' == this.elements[0][i].type)
                        this.queryString.push(this.elements[0][i].name+'='+this.elements[0][i].value);
                    if ('checkbox' == this.elements[0][i].type && true == this.elements[0][i].checked)
                        this.queryString.push(this.elements[0][i].name+'='+this.elements[0][i].value);
                    if ('radio' == this.elements[0][i].type && true == this.elements[0][i].checked)
                        this.queryString.push(this.elements[0][i].name+'='+this.elements[0][i].value);
                }
            }
        }
        this._select();
    },

    _select: function() {
        if (0 < this.elements[2].length) {
            for (var i=0; i < this.elements[2].length; i+=1) {
                if (this.elements[2][i].name.length) {
                    this.queryString.push(this.elements[2][i].name+'='+this.elements[2][i].options[this.elements[2][i].selectedIndex].value);
                }
            }
        }
        this._textarea();
    },

    _textarea: function() {
        if (0 < this.elements[1].length) {
            for (var i=0; i < this.elements[1].length; i+=1) {
                if (this.elements[1][i].name.length) {
                    this.queryString.push(this.elements[1][i].name+'='+this.elements[1][i].value);
                }
            }
        }
    }
}

handleResponse = function () {};
handleResponse = {

    ini: function (response) {

        switch (successOpt) {
            case 'reload':
                window.location.reload();
                break;
            case 'xml':
                handleResponse._xmlResponse(response.responseXML.getElementsByTagName('root'));
                break;
            default:
                handleResponse._textResponse(response.responseText);
        }
    },

    _textResponse: function (para) {
        document.getElementById(ID).innerHTML = para;
    },

    _xmlResponse: function (para) {
        if (typeof(para[0].childNodes) == "object") {
            this.xmldoc = para[0].childNodes;
        }
        else {
            this.xmldoc = para.childNodes;
        }
        alert(responseElement.getAttribute("func"));

        for (var i=0; i < this.xmldoc.length; i++) {
            var responseElement = this.xmldoc[i];
            if (responseElement.nodeType != 1) {

                /** TODO: loopfunction */

                continue;
            }

            var responseType = responseElement.getAttribute("type");
            var responseId   = responseElement.getAttribute("id");

            if (null == responseId) responseId = ID;

            if (responseType == "element") {

                if (responseElement.getAttribute("classname")) {
                    $(responseId).className = responseElement.getAttribute("classname");
                }
                if (responseElement.getAttribute("func")) {
                    eval(responseElement.getAttribute("func"));
                }

                handleResponse._processElementUpdate(responseId, responseElement);
            }
            else if (responseType == "object") {

                /** TODO: handle objects */

                handleResponse._processObjectUpdate(responseId, responseElement);
            }
            else {
                alert('unrecognized AjaxResponse type : ' + responseType);
            }
        }
    },

    _processElementUpdate: function (responseId, responseElement) {
        $(responseId).innerHTML = responseElement.firstChild.nodeValue;
    },

    _processObjectUpdate: function (responseId, responseElement) {
        /** TODO: handle objects */
    }
}

var Calendar = function () {};
Calendar = {

    ini: function (id, evt) {

        this.evt = this.Evt(evt);
        this.Id = id;

        this.myDate = new Date();
        this.acM = this.myDate.getMonth();
        this.acY = this.myDate.getFullYear();

        this.oD = this.myDate.getDate();
        this.oM = this.myDate.getMonth();
        this.oY = this.myDate.getFullYear();

        this.shortDayOfWeek = new Array("Mo","Di","Mi","Do","Fr","Sa","So");
        this.dayOfWeek = new Array("Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag","Sonntag");
        this.monthOfYear = new Array("Januar","Februar","M&auml;rz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember");
        this.countDay = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

        this._schaltjahr();

        this.hl = this.monthOfYear[this.acM]+' '+this.myDate.getFullYear();
        this.cnt = this._container();

        this._giveback();
    },

    Evt: function(evt) {

        evt = evt ? evt : window.event;
        evt.pos = Calendar.fnMousePosition(evt);
        
        return evt;
    },

    fnMousePosition: function(evt) {

        if(typeof(evt.pageX) == 'number') {
            mouseX = evt.pageX;
            mouseY = evt.pageY;
        } else {
            if((!window.document.compatMode) ||(window.document.compatMode == 'BackCompat')) {
                mouseX = evt.clientX+document.body.scrollLeft-1;
                mouseY = evt.clientY+document.body.scrollTop-1;
            } else {
                IE = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ?
                        window.document.documentElement : window.document.body || null;

                if (IE) {
                    mouseX = window.event.x;
                    mouseY =  window.event.y;
                }
            }
        }
        return {x:mouseX,y:mouseY}
    },

    _giveback: function () {
        $('layer').innerHTML = this.cnt;
        $('layer').style.position = 'absolute';
        $('layer').style.left = this.evt.pos.x + 10 + "px";
        $('layer').style.top = this.evt.pos.y + 10 + "px";
        $('layer').style.display = '';
    },

    _back: function () {

        this._schaltjahr();

        if (0 < this.acM) {
            this.acM -= 1;
            this.myDate = new Date(this.acY, this.acM, 1);
        }
        else {
            this.acM = 11;
            this.acY -= 1;
            this.myDate = new Date(this.acY, this.acM, 1);
        }

        this.hl = this.monthOfYear[this.acM]+' '+this.acY;
        this.cnt = this._container();
        this._giveback();
    },

    _next: function () {

        this._schaltjahr();

        this.acM = (11 == this.acM) ? 12 : this.acM;
        if (12 > this.acM) {
            this.acM += 1;
            if (12 == this.acM) this.acM = 11;
            this.myDate = new Date(this.acY, this.acM, 1);
        }
        else {
            this.acM = 0;
            this.acY += 1;
            this.myDate = new Date(this.acY, this.acM, 1);
        }

        this.hl = this.monthOfYear[this.acM]+' '+this.acY;
        this.cnt = this._container();
        this._giveback();
    },

    _container: function () {

        if (this.acY == this.oY && this.acM == this.oM) {
            this.myDate = new Date(this.oY, this.oM, this.oD);
            this.acM = this.myDate.getMonth();
            this.acY = this.myDate.getFullYear();
        }


        var tpl = '';
        tpl += '<input id="dY" type="hidden" value="'+this.acY+'" />';
        tpl += '<input id="dM" type="hidden" value="'+(this.acM+1)+'" />';
        tpl += '<table style="background-color:#8B8D9F;color:#fff;width:100px;">';

        tpl += '<tr><td style="cursor:pointer;"><span onclick="Calendar._back()"><<</span></td><td colspan="5" align="center">';
        tpl += this.hl;
        tpl += '</td><td style="cursor:pointer;"><span onclick="Calendar._next()">>></span></td></tr><tr>';

        for (var wd=0; wd < this.shortDayOfWeek.length; wd +=1) {
            tpl += '<td style="color:red;border:1px solid #ddd;background-color:#fff">'+this.shortDayOfWeek[wd]+'</td>';
        }
        tpl += '</tr><tr>';

        tDate = new Date(this.acY, this.myDate.getMonth(), 1);
        for (var mi=1; mi <= 7; mi +=1) {
            if (mi < tDate.getDay() || (mi < 7 && 0 == tDate.getDay())) tpl += '<td></td>';
            if (mi == tDate.getDay() || (mi == 7 && 0 == tDate.getDay())) {

                for (var i=1; i <= this.countDay[this.acM]; i +=1) {
                    var c = (i == this.myDate.getDate() && this.acM == this.oM && this.acY == this.oY) ? 'red;' : '#ddd;';
                    var c2 = (i < this.myDate.getDate() || this.acY < this.oY || (this.acM < this.oM && this.acY <= this.oY)) ? '#ccc;' : '#000;';
                    var x = (i <= 9) ? '0'+i : i;
                    tpl += '<td id="t'+i+'" style="cursor:pointer;border:1px solid '+c+';background-color:#fff;color:'+c2+'" onclick="Calendar.setTimestamp(\'t'+i+'\')">'+x+'</td>';
                    if ((((mi-1)+i)%7) == 0) tpl += '</tr><tr>';
                }
            }
        }

        tpl += '</tr></table>';
        return (tpl);
    },

    _schaltjahr: function () {
        if (this.acY % 4 == 0) {
            this.countDay[1] = 29;
            if (this.acY % 100 == 0 && this.acY % 400 != 0) this.countDay[1] = 28;
        }
    },

    setTimestamp: function (mID) {
        var sDay = $(mID).innerHTML;
        var aM = (10 > (this.acM+1)) ? '0'+(this.acM+1) : (this.acM+1);
        var string = this.acY+'-'+aM+'-'+sDay+' 00:00:00';
        document.getElementById(this.Id).value = string;
        $('layer').style.display = 'none';
    }
}

function setOpt () {

    ID = arguments[0];
    successOpt = arguments[1].length ? arguments[1] : '';
    param = arguments[0].length ? CustomForm.ini(arguments[0]) : '';
    if (undefined != arguments[2] && arguments[2].length > -1) {
        param = 'cmd=' + arguments[2].join(':');
    }

    var opt = {
        method: 'post',
        postBody: param,
        onSuccess: handleResponse.ini
    }
    return (opt);
}

var Custom = function () {};
Custom = {
    treetoggle: function (parent_id) {
        var pID_object = $(parent_id);
        var objects = pID_object.getElementsByClassName('ce_btn');

        var regex = /(t.*\;)/;

        if (objects.length > 0) {
            for (var i=0; i < objects.length; i +=1) {
                var func = objects[i].onclick;
                regex.exec(func);
                if (RegExp.$1.length > 0)
                    eval(RegExp.$1);
            }
        }
        if ($('treetoggle').innerHTML.indexOf('open')>-1)
            $('treetoggle').innerHTML = 'close all';
        else if ($('treetoggle').innerHTML.indexOf('close')>-1)
            $('treetoggle').innerHTML = 'open all';
    }
}

function selectInnerAll (ID) {
    var obj = document.getElementById(ID);    
    var childs = obj.getElementsByTagName('input');

    for (i=0; i < childs.length; i +=1) {
        if ('checkbox' == childs[i].type) {
            childs[i].checked = (childs[i].checked == false) ? true : false;
        }
    }
}


function setTabslider (tsID, rootUrl, initparam, isin, ptype, label_all, label_count, height) {


var tsHtm  = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="' + height + '" id="tableslider" align="top">';
    tsHtm += '<param name="wmode" value="opaque">';
    tsHtm += '<param name="allowScriptAccess" value="sameDomain" />';
    tsHtm += '<param name="movie" value="/applets/tableslider.swf" />';
    tsHtm += '<param name="quality" value="high" />';
    tsHtm += '<param name="bgcolor" value="#eeeeee" />';
    tsHtm += '<param name="FlashVars" value="rootUrl=' + rootUrl + '&path=tools/tableslider/xml/product_type,' + ptype + '/isin,' + isin + '/filters,&initialParameters=' + initparam +'&label_show_all_products=' + label_all + '&label_product_count=' + label_count + '" />';
    tsHtm += '<embed wmode="opaque" src="/applets/tableslider.swf?rootUrl=' + rootUrl + '&path=tools/tableslider/xml/product_type,' + ptype + '/isin,' + isin + '/filters,&initialParameters=' + initparam +'&label_show_all_products=' + label_all + '&label_product_count=' + label_count + '" quality="high" bgcolor="#eeeeee" width="100%" height="' + height + '" name="tableslider" align="top" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
    tsHtm += '</object>';

    $(tsID).innerHTML = tsHtm;
}

function setOpenEndMap (id) {
var tpl  = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="100%" height="445" id="equity" align="middle">';
    tpl += '<param name="allowScriptAccess" value="sameDomain" />';
    tpl += '<param name="movie" value="/_files/flash/equity_map/equity.swf" />';
    tpl += '<param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />';
    tpl += '<embed src="/_files/flash/equity_map/equity.swf" quality="high" bgcolor="#ffffff" width="100%" height="445" name="equity" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></embed></object>';

    $(id).innerHTML = tpl;
}




