/*************************************
* RapidFramework 1.0                 *
**************************************
* Copyright (c) 2005 rapidsoft GmbH  *
* http://www.rapidsoft.de            *
*                                    *
* Author: Moritz Mertinkat           *
* E-Mail: mmertinkat@rapidsoft.de    *
*                                    *
* Last update: 2005-10-05            *
**************************************
* The rapidsoft Javascript Framework *
* "RapidFramework" can be downloaded *
* for free from our labs page at     *
* http://labs.rapidsoft.de           *
**************************************/


function RapidClass() {

    var classname = null;
    var thisprototype = null;
    var parentprototype = null;

    if (arguments.length >= 1) {

        if (typeof(arguments[0]) == 'string') {

            classname = arguments[0];

            if (arguments.length >= 2) {

                if (typeof(arguments[1]) == 'function') {

                    if (arguments[1].prototype) {

                        parentprototype = arguments[1].prototype;

                        if (arguments.length == 3) {

                            if (typeof(arguments[2]) == 'object') {
                                thisprototype = arguments[2];
                            } else {
                                return null;
                            }

                        }

                    } else {
                        return null;
                    }

                } else if (typeof(arguments[1]) == 'object') {
                    thisprototype = arguments[1];
                } else {
                    return null;
                }

            }

        } else if (typeof(arguments[0]) == 'function') {

            if (arguments[0].prototype) {

                parentprototype = arguments[0].prototype;

                if (arguments.length == 2) {

                    if (typeof(arguments[1]) == 'object') {
                        thisprototype = arguments[1];
                    } else {
                        return null;
                    }

                }

            } else {
                return null;
            }

        } else {
            return null;
        }

    } else {
        return null;
    }


    if (thisprototype == null) {
        thisprototype = new Object();
    }


    if (parentprototype != null) {

        for (property in parentprototype) {

            if (!thisprototype[property]) {
                thisprototype[property] = parentprototype[property];
            }

        }

    }

    if (classname != null) {
        thisprototype.classname = classname;
    }

    if (thisprototype.toString.toString().match(/\[native code\]/)) {
        thisprototype.toString = function() { return '[object ' + thisprototype.classname + ']'; }
    }

    var classfunction = function() {

        if (this.create) {
            var res = this.create.apply(this, arguments);
            if (res != true) {
                return res;
            }
        }

    }

    classfunction.prototype = thisprototype;

    return classfunction;

}


var RapidError = RapidClass('RapidError', {

    _message: null,

    create: function(message) {
        this._message = message;
    },

    getMessage: function() {
        return this._message;
    },

    toString: function() {
        return this.getMessage();
    }

});


function isRapidError(obj) {
    return (typeof(obj) == 'object' && obj.classname == 'RapidError');
}


String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };


Function.prototype.bindAsEventListener = function(obj) {

    var __method = this;
    return function(event) {

         var e = event || window.event;
         e.targetElement = e.target || e.srcElement;
         return __method.call(obj, e);

    }

}


Function.prototype.bind = function(object) {

    var __method = this;
    return function() {
        __method.apply(object, arguments);
    }

}


function getElement(id) {

	if (typeof(id) != 'string') return id;

	if (document.getElementById) {
		return document.getElementById(id);
	} else if (document.all) {
		return document.all[id];
	} else {
		return null;
	}

}


function getObjectX(e) {

	if (e != getElement(e)) return 0;
	var x = 0;
	while (e) {
		if (e.offsetLeft != 'undefined') x += e.offsetLeft;
		e = (e.offsetParent != 'undefined') ? e.offsetParent : null;
	}

	return x;

}


function getObjectY(e) {

	if (e != getElement(e)) return 0;
	var y = 0;
	while (e) {
		if (e.offsetTop != 'undefined') y += e.offsetTop;
		e = (e.offsetParent != 'undefined') ? e.offsetParent : null;
	}

	return y;

}


function switchStyle(title) {

    if (!document.getElementsByTagName) return;

    var link_list = document.getElementsByTagName('link');
    for (var i = 0; i < link_list.length; i++ ) {

        if (link_list[i].getAttribute('rel').indexOf('style') != -1 && link_list[i].getAttribute('title')) {

            link_list[i].disabled = true;

            if (link_list[i].getAttribute('title') == title) {
                link_list[i].disabled = false;
            }

        }

    }

}

function fieldCounter(field_id, max) {

    var counter = getElement(field_id + "_counter").innerHTML;
    var current_len = getElement(field_id).value.length;

    counter = max - current_len;

    if (counter < 0) {

        getElement(field_id).value = getElement(field_id).value.substring(0, max);
        counter = 0;

    }

    getElement(field_id + "_counter").innerHTML = counter;

}

function fieldStatusDisable() {

    for (var i = 0; i < arguments.length; ++i) {
        getElement(arguments[i]).disabled = true;
    }

}

function fieldStatusEnable() {

    for (var i = 0; i < arguments.length; ++i) {
        getElement(arguments[i]).disabled = false;
    }

}

function fieldStatusSwitch() {

    for (var i = 0; i < arguments.length; ++i) {

        if (getElement(arguments[i]).disabled != false) {
            fieldStatusEnable(arguments[i]);
        } else {
            fieldStatusDisable(arguments[i]);
        }

    }

}

function switchLayers(name) {
    var elem = document.getElementById(name);
	//alert(name + ": " + elem);

    if( elem.style.display == "none" ) {
        elem.style.display = "block";
    } else {
        elem.style.display = "none";
    }
}
	
function changeLayers(name,name2) {
    var elem = document.getElementById(name);
	var elem2 = document.getElementById(name2);

    if( elem.style.display == "none" ) {
        elem.style.display = "block";
    } else {
        elem.style.display = "none";
    }
		
	if( elem2.style.display == "none" ) {
        elem2.style.display = "block";
    } else {
        elem2.style.display = "none";
    }
}

