Ext.Event = Ext.lib.Event;
Ext.apply(Date, {
    getLastDayOfMonth      : function(Year, Month){
            return(new Date((new Date(Year, Month+1,1))-1)).getDate();
    }
});
Ext.apply(Ext, {
	disableContextMenu	: function(el, event){
		if(typeof(el)=="object"){
			el.oncontextmenu = function(e){
				if(typeof(event)=="function"){
				    if(typeof(e)=="object")
				        event(e);
				    else
					    event(window.event);
				}
				return false;
			};
		}
	},
	getMaxZindex        : function(){
        this.zIndex = (typeof(this.zIndex)=="undefined"?10000:this.zIndex);
        this.zIndex++;
        return this.zIndex;
    },
    stopPropagation     : function(e){
        if(e!=null && typeof(e.stopPropagation)!="undefined")
            e.stopPropagation();
        else
            window.event.cancelBubble = true;
    },
    
	validation  : {
        textStringValidation  : function(text, minLenght, maxLenght){
            if(minLenght==null && maxLenght==null){
                return true;
            }
            else if(minLenght<=text.length && maxLenght==null){
                return true;
            }
            else if(minLenght==null && text.length<=maxLenght){
                return true;
            }
            else if(minLenght<=text.length && text.length<=maxLenght){
                return true;
            }
            else{
                return false;
            }
        },
        textIntValidation  : function(text, minLenght, maxLenght){
            if(!this.isNumeric(text))
                return false;
            text = parseInt(text);
            if(!this.isNumeric(minLenght) && !this.isNumeric(maxLenght))
                return true;
            else if(this.isNumeric(minLenght) && !this.isNumeric(maxLenght)){
                if(minLenght<=text)
                    return true;
                else
                    return false;
            }
            else if(!this.isNumeric(minLenght) && this.isNumeric(maxLenght)){
                if(text<=maxLenght)
                    return true;
                else
                    return false;
            }
            else{
                if(minLenght<=text && text<=maxLenght){
                    return true;
                }
                else{
                    return false;
                }
            }
        },
        textEmailValidation  : function(text){
            var regex = /^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/;
            return this.regexValidator(text, regex);
        },
        textDecimalValidation  : function(text){
            var regex = /^\d*[0-9](\.\d*[0-9])?$/;
            return this.regexValidator(text, regex);
        },
        textDocFilenameValidation  : function(text){
            var regex = /^[a-zA-Z0-9-_\.]+\.(pdf|txt|doc|csv)$/;
            return this.regexValidator(text, regex);
        },
        textImageFilenameValidation  : function(text){
            var regex = /^[a-zA-Z0-9-_\.]+\.(jpg|gif|png)$/;
            return this.regexValidator(text, regex);
        },
        textMultimediaFilenameValidation  : function(text){
            var regex = /^[a-zA-Z0-9-_\.]+\.(swf|mov|wma|mpg|mp3|wav)$/;
            return this.regexValidator(text, regex);
        },
        textHtmlColorCodesValidation  : function(text){
            var regex = /^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$/;
            return this.regexValidator(text, regex);
        },
        textIpAddressValidation  : function(text){
            var regex = /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/;
            return this.regexValidator(text, regex);
        },
        textUrlValidation   : function(text){
            var regex = new RegExp("^(http[s]?://|ftp://)?(www\\.)?[a-zA-Z0-9-\\.]+\\.(com|org|net|mil|edu|ca|co.uk|com.au|gov)$");
            return this.regexValidator(text, regex);
        },
        textCnpValidation       : function(text){
            if(!this.isNumeric(text))
                return false;
            var regex = /\d{13}/;
            if(!this.regexValidator(text, regex))
                return false;
            
            var suma = parseInt(text.substring(0,1)) * 2 +
            parseInt(text.substring(1,2)) * 7 +
            parseInt(text.substring(2,3)) * 9 +
            parseInt(text.substring(3,4)) * 1 +
            parseInt(text.substring(4,5)) * 4 +
            parseInt(text.substring(5,6)) * 6 +
            parseInt(text.substring(6,7)) * 3 +
            parseInt(text.substring(7,8)) * 5 +
            parseInt(text.substring(8,9)) * 8 +
            parseInt(text.substring(9,10)) * 2 +
            parseInt(text.substring(10,11)) * 7 +
            parseInt(text.substring(11,12)) * 9;
            var rest = suma % 11;

            var valid = false;
            if((rest<10) && (rest==text.substring(12,13)) || (rest == 10) && (text.substring(12,13)=='1')){
                valid = true;
            }

            return valid;
        },
        textNotFakeInput       : function(text){
            var regex = /(.)\1{2,}/;
            return !this.regexValidator(text, regex);
        },
        regexValidator      : function(value, regex){
            if(typeof(regex.test)=="function"){
                return regex.test(value);
            }
            return false;
        },
        isNumeric       : function(inputVal) {
             if (isNaN(parseFloat(inputVal))) {
                  return false;
             }
             return true;
        },
        handleValidationErrors  : function(errorList, messages){
            var alertText = "";
            for(index in errorList){
                if(typeof(errorList[index])=="object"){
                    if(typeof(messages)=="object"){
                    	alertText += messages.field + ": [" + errorList[index].field + "] ";
                        if(typeof(errorList[index].type)!="undefined")
                            alertText += "\r\n   " + messages.type + ": [" + errorList[index].type + "] ";
                        if(typeof(messages.minLength)!="undefined" && typeof(errorList[index].min)!="undefined")
                            alertText += "\r\n   " + messages.minLength + ": [" + errorList[index].min + "] ";
                        if(typeof(messages.maxLength)!="undefined" && typeof(errorList[index].max)!="undefined")
                            alertText += "\r\n   " + messages.maxLength + ": [" + errorList[index].max + "]";
                        alertText += "\r\n";
                    }
                    else{
                        alertText += "Field: [" + errorList[index].field + "] ";
                        alertText += "\r\n   Type: [" + errorList[index].type + "] ";
                        alertText += "\r\n   MinLenght: [" + errorList[index].min + "] ";
                        alertText += "\r\n   MaxLenght: [" + errorList[index].max + "]\r\n";
                    }
                }
            }
            alert(alertText);
        }
    },
	
	maxZIndex   : 100,
	
    setTransparence : function(object, percent){
        var element = Ext.getDom(object);
        if(percent!=null && element!=null){
            if (typeof(element.filters)!="undefined")
                element.style.filter = 'alpha(opacity=' + percent + ')';
	        else
	            element.style.opacity = percent/100;
        }
    },
    
    disableSelection    : function(element){
        var el = Ext.get(element);
        /*
		 * if(el){ if(typeof(element.disabledEventList)!="object"){
		 * element.disabledEventList = { selection : { disabled : true, elem :
		 * element, moz : element.style.MozUserSelect, fct : function(){
		 * if(this.disabled==true){ var scope = this; return function(e){
		 * if(scope.disabled==false) Ext.stopPropagation(e); return
		 * !scope.disabled; }; } else{ return null; } } } } }
		 * //element.onselectstart = element.disabledEventList.selection.fct();
		 * el.addListener("selectionstart", function(){}, this,
		 * {stopPropagation: true, preventDefault: true, stopEvent: true});
		 * el.dom.unselectable = "on"; el.dom.style.MozUserSelect = "-moz-none";
		 * el.dom.style.KhtmlUserSelect = "none";
		 */
           // el.setStyle("cursor", "default");
           // el.unselectable();
        // }
    },
    enableSelection    : function(element){
        /*
		 * if(element!=null){ if(typeof(element.disabledEventList)=="object"){
		 * if(typeof(element.disabledEventList.selection)=="object"){
		 * element.disabledEventList.selection.disabled = false; } }
		 * 
		 * 
		 * element.unselectable = "off"; element.style.MozUserSelect = "text";
		 * element.style.KhtmlUserSelect = "auto"; element.style.cursor =
		 * "default"; }
		 */
    },
    getIconPath     : function(){
        return "/libs/images/" + this.theme;
    },
    addslashes: function(str) {
        return (str+'').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0').replace(/\(/g, '\\(').replace(/\)/g, '\\)');
    },
	theme : (typeof(theme)=="undefined"?"default":theme)
});


Iterator = Ext.extend(Object, {
    array   : null,
    currentIndex    : null,
    constructor     : function(array){
        this.array = array;
        this.currentIndex = 0;
        return this;
    },
    
    hasNext     : function(){
        if(this.currentIndex < this.array.length) return true;
        return false;
    },
    
    hasPrevious : function(){
        if(this.currentIndex > 0) return true;
        return false;
    },
    
    next        : function(){
        if(!this.hasNext()) return null;
        this.currentIndex++;
        return this.array[this.currentIndex-1];
    },
    
    previous    : function(){
        if(!this.hasPrevious()) return null;
        this.currentIndex--;
        return this.array[this.currentIndex+1];
    },
    
    getIndex    : function(){
        return this.currentIndex;
    },
    
    getArray    : function(){
        return this.Array();
    }
});

Ext.applyIf(Array.prototype, {
    iterator    : function(){
        return new Iterator(this);
    }
});

Base = Ext.extend(Object, {
    constructor     : function(config){
        Ext.apply(this, config);
        this.events = {};
        if(typeof(this.listeners)=="object" && this.listeners!=null){
            for(var index in this.listeners){
                if(index!="scope"){
                    this.addListener(index, this.listeners[index]);
                }
            }
        }
        return this;
    },
    
    addListener         : function(evt, fct, scope){
        if(typeof(fct)=="function"){
            if(typeof(this.events[evt])=="undefined"){
                this.events[evt] = {
                    fctList : {},
                    event   : evt,
                    scopeList   : {},
                    fire    : function(sc, args){
                        for(var index in this.fctList){
                            if(typeof(this.fctList[index].fct)=="function"){
                                if(!args) args={sc: sc};
                                if(this.fctList[index].scope==null)
                                    if(sc==null) 
                                        this.fctList[index].fct();
                                    else
                                        this.fctList[index].fct.call(sc, args);
                                else{
                                    if(sc!=null)
                                        this.fctList[index].fct.call(this.fctList[index].scope, sc, args);
                                    else
                                        this.fctList[index].fct.call(this.fctList[index].scope, args);
                                }
                            }
                        }
                    },
                    addFunction     : function(fct, scope){
                        this.fctList[this.event+fct]={
                            fct: fct,
                            scope: scope
                        };
                    },
                    removeFunction  : function(fct){
                        this.fctList.remove(fct);
                    }
                };
            }
            this.events[evt].addFunction(fct, scope);
        }
    },
    removeListener      : function(evt, fct){
        if(typeof(this.events[evt])!="undefined"){
            this.events[evt].removeFunction(fct);
        }
    },
    applyEvents         : function(element, fctList, propagate){
        element = Ext.get(element);
        for(var i=0; i<fctList.length; i++){
            var evtObject = {
                evt     : fctList[i],
                element : element,
                scope   : this,
                fct     : function(e){
                    if(typeof(this.scope.events[this.evt])!="undefined"){
                        this.scope.events[this.evt].fire(this.scope, e);
                    }
                },
                addEvent    : function(){
                    if(propagate){
                        this.element.addListener(this.evt, function(e){this.fct(e);}, this);
                    }
                    else{
                        this.element.addListener(this.evt, function(e){this.fct(e);}, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
                    }
                }
            };
            evtObject.addEvent();
        }
    }
});

Timer = Ext.extend(Base, {
    time: 300,
    callback: null,
    timerObj: null,
    started: false,
    scope: null,
    count: 0,
    maxCount: -1,
    
    start: function(){
        this.started = true;
        this.count = 0;
        var me= this;
        this.timerObj = setInterval(function(){
            if(me.maxCount>-1 && me.count>=me.maxCount){
                me.stop();
                return;
            }
            if(!me.started)
                return;
            if(me.scope)
                me.callback.call(me.scope);
            else
                me.callback();
            me.count++;
        }
        , this.time);
    },
    
    stop: function(){
        clearInterval(this.timerObj);
        this.started=false;
    }
});

/* Ajax class that can made a request to the server */
var Ajax = Ext.extend(Object, {
    xmlHttp     : null,
    object      : null,
	url			: null,
	parent		: null,
	objName		: null,
	method		: "GET",
	parameterList	: null,
	asyncronous		: true,
	parameterString	: null,
	callBackTemp	: null,
	ready           : true,
    
    constructor     : function(config){
        config.object = (config.object ? Ext.get(config.object) : (config.objName ? Ext.get(config.objName) : null));
        Ajax.superclass.constructor.call(this, config);
        return this;
    },
    
    /* functie cu care primesc obiectul tinta daca ma aflu in alt frame */
    getObjectFromParent : function(){
		if(this.objName!=null || this.objName!=""){
			if(this.parent!=null){
				if(typeof(this.parent.document)!="undefined"){
					this.object = this.parent.document.getElementById(this.objName);
					return true;
				}
				else
					alert("Parent has no document");
			}
			else
				alert("Parent was not set");
		}
		else
			alert("Object name was not set");
		return false;
    },
    
    /* functia cu care obtin obiectul xmlHttp */
    getXmlHttp  : function(){
        var xmlHttp=null;
        try{
            // Firefox, Opera 8.0+, Safari
            xmlHttp=new XMLHttpRequest();
        }
        catch (e){
            // Internet Explorer
            try{
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e){
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    },
    
    /* init the object */
    constructor        : function(config){
        Ext.apply(this, config);
        this.xmlHttp=this.getXmlHttp();
        if (this.xmlHttp==null)
        {
            alert ("Browser does not support HTTP Request");
            return;
        }
        return this;
    },
  
	requestCallBack	: function(callBack){
	    this.ready = true;
		this.callBackTemp = callBack;
		if(this.callBackTemp!=null)
			this.callBackTemp(this.xmlHttp);
	},
	
	getParameters	:	function(){
		if(this.parameterList!=null && this.parameterList.length>0){
			for(var index=0; index<this.parameterList.length; index++){
				if(this.parameterString!=null && this.parameterString.length>0)
					this.parameterString += "&";
				else
				    this.parameterString = "";
				this.parameterString += this.parameterList[index];
			}
		}
		return this.parameterString;
	},
    
    /* function that makes the request */
    doRequest   : function(callBack){
        if(this.ready==true){
		    if(this.url!=null || this.url!=""){
		        this.xmlHttp.open(this.method, this.url, this.asyncronous);
			    var scope = this;
	            this.xmlHttp.onreadystatechange=function(){
			        if(this.readyState!=null){
	                    if ((this.readyState==4 || this.readyState=="complete")){ 
	                        scope.requestCallBack(callBack);
	                    }
	                } 
	            };
    	        
    	        var params = this.getParameters();
			    this.ready = false;
			    if(this.method=="POST"){
			        this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    this.xmlHttp.setRequestHeader("Content-length", params.length);
                    this.xmlHttp.setRequestHeader("Connection", "close");
			    }
	            this.xmlHttp.send(params);
		    }
		    else{
			    alert("URL was not set");
		    }
		}
    },
	
	/* assign the request text to objects innerHTML */
	assignToInnerHTML	: function(callBack, xmlHttp){
		if(typeof(this.object)!="undefined" && this.object!=null){
		    if(typeof(this.object.innerHTML) == "undefined")
			    this.object.update(xmlHttp.responseText);
			else
			    this.object.innerHTML = xmlHttp.responseText;
			if(callBack!=null)
					callBack(this, xmlHttp);
		}
	},
	
	/* assign the request text to objects value */
	assignToValue	: function(callBack, xmlHttp){
		if(typeof(this.object)!="undefined" && this.object!=null){
		    if(typeof(this.object.value)=="undefined")
			    this.object.setValue(xmlHttp.responseText);
			else
			    this.object.value = xmlHttp.responseText;
			if(callBack!=null)
					callBack(this, xmlHttp);
		}
	},
    
    /* function that make a request without callback */
    flatRequest : function(callback){
        this.doRequest(function(xmlHttp){
            if(typeof(callback)=="function"){
                callback(xmlHttp);
            }
        });
    },
    
    /* function that insert into objects innerHTML the response text */
    getObjMsgToInnerHTML  : function(callBack){
    	this.doRequest(function(xmlHttp){
            this.assignToInnerHTML(callBack, xmlHttp);
        });
    },
    
    /* function that insert into objects value the response text */
    getObjMsgToValue  : function(callBack){
		this.doRequest(function(xmlHttp){
			this.assignToValue(callBack, xmlHttp);
		});
    },
    
    /*
	 * function that insert the response text into innerHTML of an element from
	 * actual document
	 */
    getObjNameFromMsgToInnerHTML    : function(callBack){
        if(this.getObject()){
			this.doRequest(function(xmlHttp){
                this.assignToInnerHTML(callBack, xmlHttp);
            });
        }
    },
    
    /*
	 * function that insert the response text into value of an element from
	 * actual document
	 */
    getObjNameFromMsgToInnerHTML    : function(callBack){
        if(this.getObject()){
			this.doRequest(function(xmlHttp){
                this.assignToValue(callBack, xmlHttp);
            });
        }
    },
    
    /*
	 * function that insert the response text into innerHTML of an element that
	 * is get from a parent
	 */
    getObjNameFromParentMsgToInnerHTML  : function(callBack){
		if(this.getObjectFromParent()){
            this.doRequest(function(xmlHttp){
                this.assignToInnerHTML(callBack, xmlHttp);
            });
        }
    },
    
    /*
	 * function that insert the response text into value of an element that is
	 * get from a parent
	 */
    getObjNameFromParentMsgTovalue  : function(url, objName, parameterList, parent, callBack){
        if(this.getObjectFromParent()){
            this.doRequest(function(xmlHttp){
                this.assignToValue(callBack, xmlHttp);
            });
        }
    }
});

Visual = Ext.extend(Base, {
    left        : null,
    top         : null,
    width       : null,
    height      : null,
    child       : null,
    value       : null,
    margin      : '',
    padding     : '',
    rendered    : false,
    client      : { getPage: function() { var pageWidth = 720; var pageHeight = 576; var scrollArr = this.getScroll(); var winArr = this.getWindow(); pageWidth = winArr.width + scrollArr.left; pageHeight = winArr.height + scrollArr.top; return { scrollX: scrollArr.left, scrollY: scrollArr.top, winW: winArr.width, winH: winArr.height, pageW: pageWidth, pageY: pageHeight }; }, getScroll: function() { return { left: this.scrollLeft(), top: this.scrollTop() }; }, getWindow: function() { return { width: this.windowWidth(), height: this.windowHeight() }; }, scrollLeft: function() { var xScroll = 0; if (self.pageXOffset) xScroll = self.pageXOffset; else if (document.documentElement && document.documentElement.scrollLeft) xScroll = document.documentElement.scrollLeft; else if (document.body) xScroll = document.body.scrollLeft; return xScroll; }, scrollTop: function() { var yScroll = 0; if (self.pageYOffset) yScroll = self.pageYOffset; else if (document.documentElement && document.documentElement.scrollTop) yScroll = document.documentElement.scrollTop; else if (document.body) yScroll = document.body.scrollTop; return yScroll; }, windowWidth: function() { var xWin = 720; if (self.innerHeight) xWin = self.innerWidth; else if (document.documentElement && document.documentElement.clientWidth) xWin = document.documentElement.clientWidth; else if (document.body) xWin = document.body.clientWidth; return xWin; }, windowHeight: function() { var yWin = 576; if (self.innerHeight) yWin = self.innerHeight; else if (document.documentElement && document.documentElement.clientHeight) yWin = document.documentElement.clientHeight; else if (document.body) yWin = document.body.clientHeight; return yWin; }},
    renderTo    : null,
    xType       : 'visual',
    shown       : null,
    selectable  : false,
    cls         : null,
    hidden      : false,
    
    dom     : null,
    
    constructor     : function(config){
        Visual.superclass.constructor.call(this, config);
        this.rendered = false;
        var scope = this;
        this.createElements();
        this.render(this.renderTo);
        
        return this;
    },
    
    createElements  : function(){
       if(typeof(this.dom)=="undefined" || this.dom==null){
            var obj = document.createElement('div');
            obj.setAttribute("id", Ext.id());
            this.dom = Ext.get(obj);
        }
    },
    
    getSize     : function(element) {
        var el = Ext.get(element);
        return {
            width: el.getWidth(),
            height: el.getHeight()
        };
    },
    
    render  : function(renderTo){
        if(this.rendered==false){
            if(renderTo) this.renderTo = renderTo;
            if(this.renderTo!=null){ 
                Ext.get(this.dom).appendTo(this.renderTo);
                if(!this.selectable)
                    Ext.disableSelection(this.dom.dom);
                Ext.disableContextMenu(this.dom.dom, function(){});
                this.rendered = true;
                this.doLayout();
            }   
        }
    },
    
    doLayout    : function(){
        if(this.dom!=null){
            this.setPosition();
            this.setMargin();
            this.setPadding();
            this.setSize();
            this.setCls();
            if(this.hidden) this.hide(); else this.show();
        }
    },
    
    setWidth    : function(width){
        this.width = (width != null && /(d)*/.test(width) ? width : this.width);
        if(this.width)
            this.dom.setWidth(this.width);
    },
    
    setHeight    : function(height){
        this.height = (height != null && /(d)*/.test(height) ? height : this.height);
        if(this.height)
            this.dom.setHeight(this.height);
    },
    
    setSize     : function(width, height){
        this.setWidth(width);
        this.setHeight(height);
    },
    
    setLeft    : function(left){
        this.left = (left != null && /(d)*/.test(left) ? left : this.left);
        if(this.left && /(d)*/.test(this.left))
            this.dom.setHeight(this.left);
    },
    
    setTop    : function(top){
        this.top = (top != null && /(d)*/.test(top) ? top : this.top);
        if(this.top && /(d)*/.test(this.top))
            this.dom.setHeight(this.top);
    },
    
    setPosition     : function(left, top, animate){
        animate = (animate==null ? false : animate);
        this.left = (left != null && /(d)*/.test(left) ? left : this.left);
        this.top = (top != null && /(d)*/.test(top) ? top : this.top);
        if(this.top && /(d)*/.test(this.top) && this.left && /(d)*/.test(this.left))
            this.dom.setLocation(this.left, this.top, animate);
    },
    
    show            : function(cfg){
        if(!this.shown || this.shown==null){
            if(cfg){
                this.dom.fadeIn(cfg);
            }
            else{
                this.dom.setDisplayed(true);
            }
            this.shown=true;
            if(typeof(this.events.show)!="undefined"){
                this.events.show.fire(this);
            }
        }
    },
    
    hide            : function(cfg){
        if(this.shown || this.shown==null){
            if(cfg){
                cfg.useDisplay = "none";
                this.dom.fadeOut(cfg);
            }
            else{
                this.dom.setDisplayed(false);
            }
            this.shown=false;
            if(typeof(this.events.hide)!="undefined"){
                this.events.hide.fire(this);
            }
        }
    },
    
    setPadding  : function(padding){
        this.padding = (padding!=null ? padding : this.padding);
        this.dom.setStyle("bottom", this.padding);
    },
    
    setMargin  : function(margin){
        this.margin = (margin!=null ? margin : this.margin);
        this.dom.setStyle("margin", this.margin);
    },
    
    getNextChild   : function(parent, child){
        var internalParent = this.dom;
        if(child!=null){
            internalParent = parent;
        }
        else{
            child = parent;
        }
        if(typeof(internalParent.childNodes)!="undefined"){
            for(var i=0; i<internalParent.childNodes.length; i++){
                if(child==internalParent.childNodes[i] && typeof(internalParent.childNodes[i+1])!="undefined"){
                    return internalParent.childNodes[i+1];
                }
            }
        }
        return null;
    },
    
    getPosition : function(el){
        var elem = Ext.get(el);
        var x=0, y=0;
        for (var p = elem.dom; p; p = p.offsetParent)
			if (p.style.position != 'absolute'){
				x += p.offsetLeft;
				y += p.offsetTop;
			}
		return {
		    x: x,
		    y: y
		};
    },
    
    setCls: function(cls){
        this.cls = (cls ? cls : this.cls);
        if(this.cls){
            this.dom.addClass(this.cls);
        }
    }
});

Calendar = Ext.extend(Visual, {
    headerVisible   : false,
    headerText	    : '',
    width           : 160,
	top			    : 0,
	showPosition        : "right-top",
	fixed           : false,
	autoHeight      : false,
	monthsName      : 'January;February;March;April;May;June;July;August;September;October;Novenmber;December',
	daysName        : 'M;T;W;T;F;S;S',
	year            : null,
	month           : null,
	day             : null,
	actualDate      : null,
	dateFormat      : "d-m-y",
	maxTst			: null,
	minTst			: null,
	noYearWrite		: false,
	
	constructor     : function(config){
        Ext.apply(this, config);
        this.referenceObj = Ext.get(this.referenceObj);
        this.days = this.daysName.split(";");
		this.months = this.monthsName.split(";");
		Calendar.superclass.constructor.call(this, config);
		this.initEvents();
        return this;
    },
    
    createElements  : function(){
        Calendar.superclass.createElements.call(this, arguments);
        this.dom.hide();
        if(typeof(this.masterTable)=="undefined" || this.masterTable==null){
            this.cellList = new Array();
            
            this.masterDiv = Ext.get(this.dom.insertHtml("beforeEnd", "<div></div>"));
            this.masterTable = Ext.get(this.dom.insertHtml("beforeEnd", "<table></table>"));
            this.masterTable.shown = true;
            
            Ext.apply(this.masterTable.dom,{
                cellSpacing :"1px",
                cellPadding :"0px",
                align   : "center",
                className   :"xt-calendar-table"
            });
            
            for(i=0; i<7; i++){
                var row = Ext.get(this.masterTable.insertHtml("beforeEnd", "<tbody><tr></tr></tbody>").firstChild);
                
                for(j=0; j<7; j++){
                    var cell = Ext.get(row.insertHtml("beforeEnd", "<td></td>"));
                    Ext.apply(cell.dom,{
                        align   :"center",
                        hasEvent   :false,
                        className   : (i>0 ? "xt-calendar-cell" : "xt-calendar-cell-heather")
                    });
                    cell.dom.style.width = 100/7 + "%";
                    this.cellList["cell_" + i + "_" + j] = cell;
                    
                    
                    cell.setAktiv = function(e, el){
                        if(el.innerHTML!=this.day)
                            el.className="xt-calendar-cell-activ";
                    };
                    
                    cell.setInactiv = function(e, el){
                        if(el.innerHTML!=this.day)
                            el.className="xt-calendar-cell";
                    };
                    
                    cell.setValue = function(e, el){
                        if(this.referenceObj!=null){
                            this.animation.restoreHeaderElements.call(this);
                            var value = this.dateFormat;
                            
                            this.day = parseInt(el.innerHTML);
                            value = value.replace(/d/g, (this.day<10 ? "0" + this.day : this.day));
                            value = value.replace(/y/g, this.year);
                            value = value.replace(/m/g, (this.month+1<10 ? "0" + (this.month+1) : (this.month+1)));
                            this.referenceObj.dom.value = value;
                            this.setDay(el.innerHTML);
                            this.setDays();
                            if(this.events.dateselected)
                            	this.events.dateselected.fire(this, {year: this.year, month: this.month+1, day: this.day});
                        }
                        this.hide();
                    };
                }
            }
            
            this.monthTable = Ext.get(this.masterDiv.insertHtml("beforeEnd", "<table></table>"));
            
            Ext.apply(this.monthTable.dom, {
                border      : "0px",
                cellSpacing : "0px",
                cellPadding : "0px",
                align       : "center",
                className   : "xt-calendar-table"
            });
            var row = Ext.get(this.monthTable.insertHtml("beforeEnd", "<tbody><tr></tr></tbody>").firstChild);
            row.setHeight(19);
            
            this.leftYearCell = Ext.get(row.insertHtml("beforeEnd", "<td></td>"));
            Ext.apply(this.leftYearCell.dom, {
                align           : "center"
            });
            this.leftYearCell.setWidth((this.width-110)/2);
            this.leftYearCell.insertHtml("beforeEnd", "<img src='" + Ext.getIconPath() + "/db-left.png'>");
            
            this.centerYearCell = Ext.get(row.insertHtml("beforeEnd", "<td></td>"));
            Ext.apply(this.centerYearCell.dom, {
                align           : "center",
                className       : "xt-calendar-head-td"
            });
            this.centerYearCell.setWidth(100);
            this.centerYearCell.dom.style.padding = "0px";
            
            this.rightYearCell = Ext.get(row.insertHtml("beforeEnd", "<td></td>"));
            Ext.apply(this.rightYearCell.dom, {
                align           : "center"
            });
            this.rightYearCell.setWidth((this.width-110)/2);
            this.rightYearCell.insertHtml("beforeEnd", "<img src='" + Ext.getIconPath() + "/db-right.png'>");
            
            
	        this.yearDiv = Ext.get(this.centerYearCell.insertHtml("beforeEnd", "<div></div>"));
	        if(this.noYearWrite==false){
	        	this.yearDiv.addListener("click", function(){
	                this.animation.restoreHeaderElements.call(this);
	                this.yearDiv.setDisplayed(false);
	                this.yearHiddenInput.setDisplayed(true);
	                this.yearHiddenInput.dom.value = '';
	                this.yearHiddenInput.focus();
	            }, this);
            }
            
            this.yearHiddenInput = Ext.get(this.centerYearCell.insertHtml("beforeEnd", "<input type='text'/>"));
            this.yearHiddenInput.addClass("xt-calendar-input");
            this.yearHiddenInput.setDisplayed(false);
            
            var row = Ext.get(this.monthTable.insertHtml("beforeEnd", "<tbody><tr></tr></tbody>").firstChild);
            this.leftMonthCell = Ext.get(row.insertHtml("beforeEnd", "<td></td>"));
            Ext.apply(this.leftMonthCell.dom, {
                align           : "center",
                className       : "xt-calendar-button"
            });
            this.leftMonthCell.insertHtml("beforeEnd", "<img src='" + Ext.getIconPath() + "/db-left.png'>");
            this.leftMonthCell.setWidth((this.width-110)/2);
            
            this.centerMonthCell = Ext.get(row.insertHtml("beforeEnd", "<td></td>"));
            Ext.apply(this.centerMonthCell.dom, {
                align           : "center",
                className       : "xt-calendar-head-td"
            });
            this.centerMonthCell.update("");
            this.centerMonthCell.setWidth(110);
                
            this.rightMonthCell = Ext.get(row.insertHtml("beforeEnd", "<td></td>"));
            Ext.apply(this.rightMonthCell.dom, {
                align           : "center",
                className       : "xt-calendar-button"
            });
            this.rightMonthCell.insertHtml("beforeEnd", "<img src='" + Ext.getIconPath() + "/db-right.png'>");
            this.rightMonthCell.setWidth((this.width-110)/2);
            
            this.hiddenMonthTable = Ext.get(this.masterDiv.insertHtml("beforeEnd", "<table></table>"));
            this.hiddenMonthTable.shown = false;
            Ext.apply(this.hiddenMonthTable.dom, {
                border      : "0px",
                cellSpacing : "1px",
                cellPadding : "0px",
                align       : "center",
                className   : "xt-calendar-table"
            });
            this.hiddenMonthTable.setDisplayed(false);
            
            for(i=0; i<6; i++){
                var row = Ext.get(this.hiddenMonthTable.insertHtml("beforeEnd", "<tbody><tr></tr></tbody>").firstChild);
                for(j=0; j<2; j++){
                    var cell = Ext.get(row.insertHtml("beforeEnd", "<td></td>"));
                    Ext.apply(cell.dom, {
                        align           : "center",
                        className       : "xt-calendar-cell"
                    });
                    cell.update(this.months[j*6+i]);
                    cell.setWidth(this.width/2);
                    
                    cell.addListener('mouseover', function(e, el){el.className="xt-calendar-cell-activ";}, this);
                    cell.addListener('mouseout', function(e, el){el.className="xt-calendar-cell";}, this);
                    cell.addListener('click', function(e, el){
                        for(i=0; i<this.months.length; i++){
                            if(this.months[i] == el.innerHTML){
                                this.setMonth(i);
                                this.setDays();
                                this.animation.showMasterTable.call(this);
                                this.animation.hideMonthTable.call(this);
                                break;
                            }
                        }
                    }, this);
                }
            }
        }
    },
    
    initEvents      : function(){
        this.leftYearCell.addListener("click", function(){
            this.setYear(this.year-1);
            this.animation.changeToLeft.call(this);
        }, this);
        this.leftYearCell.addListener("mouseover", function(){this.leftYearCell.dom.className = "xt-calendar-button-activ";}, this);
        this.leftYearCell.addListener("mouseout", function(){this.leftYearCell.dom.className = "xt-calendar-button";}, this);
        this.rightYearCell.addListener("click", function(){
            this.setYear(this.year+1);
            this.animation.changeToRight.call(this);
        }, this);

        this.rightYearCell.addListener("mouseover", function(){this.rightYearCell.dom.className = "xt-calendar-button-activ";}, this);
        this.rightYearCell.addListener("mouseout", function(){this.rightYearCell.dom.className = "xt-calendar-button";}, this);
        this.centerYearCell.addListener("mouseover", function(){this.centerYearCell.dom.className = "xt-calendar-button-activ";}, this);
        this.centerYearCell.addListener("mouseout", function(){this.centerYearCell.dom.className = "xt-calendar-button";}, this);
        
        this.rightMonthCell.addListener("click", function(){
            this.setMonth(this.month+1);
            this.animation.changeToRight.call(this);
        }, this);
        this.rightMonthCell.addListener("mouseover", function(){this.rightMonthCell.dom.className = "xt-calendar-button-activ";}, this);
        this.rightMonthCell.addListener("mouseout", function(){this.rightMonthCell.dom.className = "xt-calendar-button";}, this);
        
        this.leftMonthCell.addListener("click", function(){
            this.setMonth(this.month-1);
            this.animation.changeToLeft.call(this);
        }, this);
        this.leftMonthCell.addListener("mouseover", function(){this.leftMonthCell.dom.className = "xt-calendar-button-activ";}, this);
        this.leftMonthCell.addListener("mouseout", function(){this.leftMonthCell.dom.className = "xt-calendar-button";}, this);
        this.centerMonthCell.addListener("click", function(){
            this.animation.restoreHeaderElements.call(this);
            this.animation.hideMasterTable.call(this);
            this.animation.showMonthTable.call(this);
        }, this);
        this.centerMonthCell.addListener("mouseover", function(){this.centerMonthCell.dom.className = "xt-calendar-button-activ";}, this);
        this.centerMonthCell.addListener("mouseout", function(){this.centerMonthCell.dom.className = "xt-calendar-button";}, this);
        
        this.yearHiddenInput.addListener("blur", function(e){
            this.yearHiddenInput.dom.value = this.yearHiddenInput.dom.value.replace(/\D/g, "");
            if(this.yearHiddenInput.dom.value!="" && this.yearHiddenInput.dom.value.length==4){
                this.setYear(this.yearHiddenInput.dom.value);
                this.setDays();
            }
        }, this);
        
        this.yearHiddenInput.addListener("keypress", function(e){
            if(e.getKey()==13){
                this.yearHiddenInput.setDisplayed(false);
                this.yearDiv.setDisplayed(true);
                this.yearDiv.focus();
                this.animation.restoreHeaderElements.call(this);
            }
        }, this);
		this.dom.addListener("click", function(){}, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
    },
    
    animation: {
        showMasterTable   : function(){
            if(this.masterTable.shown==false){
                this.masterTable.slideIn("t", {
                    easing: 'easeOut',
                    duration: .3,
                    useDisplay: "block"
                });
                this.masterTable.shown=true;
            }
        },
        
        hideMasterTable   : function(){
            if(this.masterTable.shown==true){
                this.masterTable.slideOut("t", {
                    easing: 'easeOut',
                    duration: .3,
                    useDisplay: "none"
                });
                this.masterTable.shown=false;
            }
        },
        
        showMonthTable  : function(){
            if(this.hiddenMonthTable.shown==false){
                this.hiddenMonthTable.slideIn("t", {
                    easing: 'easeOut',
                    duration: .3,
                    useDisplay: "block"
                });
                this.hiddenMonthTable.shown=true;
            }
        },
        
        hideMonthTable  : function(){
            if(this.hiddenMonthTable.shown==true){
                this.hiddenMonthTable.slideOut("t", {
                    easing: 'easeOut',
                    duration: .3,
                    useDisplay: "none"
                });
                this.hiddenMonthTable.shown=false;
            }
        },
        
        restoreHeaderElements   : function (){
            this.yearHiddenInput.setDisplayed(false);
            this.yearDiv.setDisplayed(true);
        },
		
		changeToLeft: function(){
			this.masterTable.slideOut("r", {
				easing: 'easeOut',
				duration: .3,
				useDisplay: "none",
				scope: this,
				callback: function(){
					this.renderTexts();
					this.setDays();
				}
			});
			this.masterTable.fadeIn({
				easing: 'easeOut',
				duration: .3,
				useDisplay: "none"
			});
		},
		
		changeToRight: function(){
			this.masterTable.slideOut("l", {
				easing: 'easeOut',
				duration: .3,
				useDisplay: "none",
				scope: this,
				callback: function(){
					this.renderTexts();
					this.setDays();
				}
			});
			this.masterTable.fadeIn({
			    easing: 'linear',
				duration: .3
			});
		}
    },
    
    adjustDate      : function(){
        if(this.month<0){
            this.month=11;
            this.year--;
        }
        else if(this.month>11){
            this.month=0;
            this.year++;
        }
    },
    
    doLayout    : function(){
        Calendar.superclass.doLayout.call(this);
        this.renderTexts();
        this.setYear();
        this.setMonth();
        this.setDay();
        this.setDays();
        this.dom.setWidth(this.width);
        
        if(!this.cls)
        	this.dom.addClass("xt-calendar");
        if(this.hidden){
            Calendar.superclass.hide.call(this);
        }
    },
    
    renderTexts     : function(){
        if(typeof(this.cellList)!="undefined" && this.cellList!=null){
            for(i=0; i<7; i++){
                this.cellList["cell_0_" + i].update("<b>" + this.days[i] + "</b>");
            }
        }
    },
    
    setYear     : function(year){
        var today = new Date();
        this.year = (year ? parseInt(year) : parseInt(this.year ? this.year : today.getFullYear()));
        if(this.year<1900 && this.year!=null) this.year += 1900;
    },
    
    setMonth   : function(month){
        var today = new Date();
    	this.month = (this.month ? this.month : "") + "";
    	this.month = this.month.replace(/^0/, "");
        this.month = (month || month==0 ? parseInt(month) : parseInt(this.month ? this.month : today.getMonth()));
    },
    
    setDay      : function(day){
        var today = new Date();
    	this.day = (this.day ? this.day : "") + "";
    	this.day = this.day.replace(/^0/, "");
    	this.day = (day ? parseInt(day) : parseInt(this.day ? this.day : today.getDate()));
        
        this.selectDay();
    },
    
    selectDay   : function(){
        for(i=1; i<7; i++){
            for(j=0; j<7; j++){
                var cellId = "cell_" + i + "_" + j ;
                
                if(this.cellList[cellId].dom.innerHTML!=this.day){
                    this.cellList[cellId].dom.className = (this.cellList[cellId].dom.oldClass ? this.cellList[cellId].dom.oldClass :"xt-calendar-cell");
                }
                else{
                    this.cellList[cellId].dom.oldClass = this.cellList[cellId].dom.className;
                    this.cellList[cellId].dom.className = "xt-calendar-cell-selected";
                }
            }
        }
    },
    
    setDays         : function(){
        this.adjustDate();
        this.yearDiv.update(this.year);
        this.centerMonthCell.update(this.months[this.month]);
        
        this.actualDate = new Date();
        if(this.year==null)
            this.setYear(this.actualDate.getYear());
        if(this.month==null)
            this.setMonth(this.actualDate.getMonth());
        if(this.day==null)
            this.setDay(this.actualDate.getDate());
            
        var lastDay = Date.getLastDayOfMonth(this.year, this.month);
        var firsDayInWeek = new Date(this.year, this.month, 0).getDay();
        var prevMonthLastDay = Date.getLastDayOfMonth(this.year, this.month-1);
        
        for(i=1; i<7; i++){
            for(j=0; j<7; j++){
                
                var date = (i-1)*7 + (j + 1) - firsDayInWeek;
                var cellId = "cell_" + i + "_" + j ;
                this.cellList[cellId].dom.day = date;
                var tst = new Date(this.year, this.month, date,0 ,0,0).getTime();
                if((i==1 && j<firsDayInWeek) || (i!=1 && date>lastDay)){
                    if(this.cellList[cellId].dom.hasEvent==true){
                        this.cellList[cellId].removeListener('click', this.cellList[cellId].ll, this);
                        this.cellList[cellId].removeListener('mouseover', this.cellList[cellId].setAktiv, this);
                        this.cellList[cellId].removeListener('mouseout', this.cellList[cellId].setInactiv, this);
                    }
                    if(i==1)
                        this.cellList[cellId].update(prevMonthLastDay-(firsDayInWeek-j-1));
                    else
                        this.cellList[cellId].update(date-lastDay);
                    
                    this.cellList[cellId].dom.className = "xt-calendar-cell-outer";
                    this.cellList[cellId].dom.hasEvent=false;
                }
                else{
                	if((this.minTst && this.minTst>tst) || (this.maxTst && this.maxTst<tst)){
                		if(this.cellList[cellId].dom.hasEvent==true){
                            this.cellList[cellId].removeListener('click', this.cellList[cellId].ll, this);
                            this.cellList[cellId].removeListener('mouseover', this.cellList[cellId].setAktiv, this);
                            this.cellList[cellId].removeListener('mouseout', this.cellList[cellId].setInactiv, this);
                        }
                        this.cellList[cellId].update(date);
                        
                        this.cellList[cellId].dom.className = "xt-calendar-cell-outer";
                        this.cellList[cellId].dom.hasEvent=false;
	                }
	                else{
	                    this.cellList[cellId].update(date);
	                    if(date!=this.day){
	                        this.cellList[cellId].dom.className = "xt-calendar-cell";
	                    }
	                    else{
	                        this.cellList[cellId].dom.className = "xt-calendar-cell-selected";
	                    }
	                    if(this.cellList[cellId].dom.hasEvent==false){
	                    this.cellList[cellId].ll = this.cellList[cellId].setValue;
	                        this.cellList[cellId].addListener('click', this.cellList[cellId].setValue, this);
	                        this.cellList[cellId].addListener('mouseover', this.cellList[cellId].setAktiv, this);
	                        this.cellList[cellId].addListener('mouseout', this.cellList[cellId].setInactiv, this);
	                    }
	                    this.cellList[cellId].dom.hasEvent=true;
	                }
                }
            }
        }
    },
    show            : function(){
        Calendar.superclass.show.call(this, {
            duration    : .4
        });
       // this.doLayout();
        this.position();
    },
    
    hide            : function(){
        Calendar.superclass.hide.call(this, {
            duration    : .4
        });
    },
    
    position: function(){
        var pos = this.getPosition(this.referenceObj.dom);
        
        if(this.showPosition == "right-top"){
	        this.dom.setTop(pos.y);
	        this.dom.setTop(this.referenceObj.getTop(false));
	        this.dom.setLeft(this.referenceObj.getLeft(false)+this.referenceObj.getWidth());
	    }
    	else if(this.showPosition == "left-bottom"){
    		this.dom.setTop(this.referenceObj.getTop(false) + this.referenceObj.getHeight());
	        this.dom.setLeft(this.referenceObj.getLeft(false));
    	}
    },
    
    setValue: function(){
    	var value = this.dateFormat;
    	value = value.replace(/d/g, (parseInt(this.day)<10 ? "0" + parseInt(this.day) : this.day));
        value = value.replace(/y/g, this.year);
        value = value.replace(/m/g, (this.month+1<10 ? "0" + (this.month+1) : (this.month+1)));
        this.referenceObj.dom.value = value;
    }
});


TreeViewElement = Ext.extend(Visual, {
    level   : 0,
    collapsed   : true,
    xType       : 'treeNode',
    parentNode  : null,
    parent      : null,
    items       : null,
    selected    : null,
    
    
    constructor     : function(config){
        this.items=[];
        Ext.apply(this, config);
        TreeViewElement.superclass.constructor.call(this, config);
        this.initEvents();
        return this;
    },
    
    render  : function(renderTo){
        TreeViewElement.superclass.render.call(this, renderTo);
        if(this.rendered==true){
            if(this.items!=null){
                var it = this.items.iterator();
                while(it.hasNext()){
                    this.addItem(it.next());
                }
            }
        }
    },
    
    createElements  : function(){
        this.childList = new Array();
        TreeViewElement.superclass.createElements.call(this, arguments);
        if(this.dom!=null){

            this.treeNode = new Array();
            
            this.treeNode.node = this.dom.insertHtml("beforeEnd", "<div></div>", true);
            this.treeNode.childContainer = this.dom.insertHtml("beforeEnd", "<div></div>", true);
            this.treeNode.nodeContainerTable = this.treeNode.node.insertHtml("beforeEnd", "<table cellspacing='0px'><tr><td></td><td align='center' align='middle'><img></td><td></td><td>"+this.text+"</td></tr></table>", true);
            
            this.treeNode.nodeContainer = Ext.get(this.treeNode.node.dom.firstChild.firstChild.firstChild);
            this.treeNode.nodeDistance = Ext.get(this.treeNode.nodeContainer.dom.firstChild);
            this.treeNode.nodeControlContainer = this.treeNode.nodeDistance.next("", false);
            this.treeNode.nodeControl = Ext.get(this.treeNode.nodeControlContainer.dom.firstChild);
            this.treeNode.imgContainer = this.treeNode.nodeControlContainer.next("", false);
            this.treeNode.nodeText = this.treeNode.imgContainer.next("", false);
            
            if(this.imgSrc!=null){
                this.treeNode.img = this.treeNode.imgContainer.dom.insertHtml("beforeEnd", "<img src='" + this.imgSrc + "'/>", true);
                this.treeNode.img.addClass("xt-" + this.xType + "-img");
                this.treeNode.imgContainer.addClass("xt-" + this.xType + "-imgContainer");
            }
        }
    },
    
    initEvents      : function(){
        this.treeNode.nodeContainerTable.addListener("contextmenu", function(e){
            if(typeof(this.events.rightclick)!="undefined"){
                this.events.rightclick.fire(e, scope);
            }
            return false;
        }, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
    
        this.treeNode.nodeControl.addListener("click", function(e){
            this.select();
            if(this.items.length>0){
                if(this.collapsed==false){ this.collapse(); }
                else{ this.expand(); }
            }
        }, this, {stopPropagation: true});
        
        this.treeNode.nodeText.addListener("click", function(){
            this.select();
        }, this, {stopPropagation: true});
        
        this.applyEvents(this.treeNode.nodeText.dom, ["dblclick"]);
        this.applyEvents(this.treeNode.nodeContainer.dom, ["keypress"]);
        
        var scope = this;
        this.addListener("dblclick", function(){
            if(scope.collapsed){ scope.expand(); }
            else{ scope.collapse(); }
        });
    },
    
    select          : function(obj){
        if(this!=obj){
            this.treeNode.nodeText.addClass("xt-" + this.xType + "-selected");
            if(this.parent!=null && typeof(this.parent.selectNode)=="function"){ this.parent.selectNode(this); }
            if(typeof(this.events.select)!="undefined"){ this.events.select.fire(this); }
            if(typeof(this.events.childSelect)!="undefined"){ this.events.childSelect.fire(this); };
            var it = this.items.iterator();
            while(it.hasNext()){
                var ob = it.next();
                if(ob.deselect)
                    ob.deselect(obj);
            }
            this.selected=true;
        }
    },
    
    deselect          : function(obj){
        if(this!=obj){
            this.treeNode.nodeText.removeClass("xt-" + this.xType + "-selected");
            if(typeof(this.events.deselect)!="undefined"){ this.events.deselect.fire(this); }
            this.selected=false;
            
            var it = this.items.iterator();
            while(it.hasNext()){
                var ob = it.next();
                if(ob!=obj && ob.deselect)
                    ob.deselect(obj);
            }
        }
    },
    
    addItem    : function(child){
        if(typeof(child)=="object" && typeof(child.dom)=="object"){
            child.level = this.level + 1;
            child.render(this.treeNode.childContainer);
            child.parentNode = this;
            child.parent = this.parent;
            child.addListener("childSelect", function(el){
                if(typeof(this.events.childSelect)!="undefined"){
                    this.deselect(el);
                    this.events.childSelect.fire(el);
                }
            }, this);
            
            if(this.items.indexOf(child)<0)
                this.items[this.items.length] = child;
        }
    },
    
    removeChilds    : function(){
        while(this.items.length>0){
            this.items[0].remove();
            this.items.remove(this.items[0]);
        }
    },
    
    expand      : function(fast){
            this.treeNode.nodeControl.dom.src = Ext.getIconPath() + "/minus.png";
        if(!fast)
            this.treeNode.childContainer.slideIn("t", {
                easing: 'easeOut',
                duration: .3,
                useDisplay: "block"
            });
        else
            this.treeNode.childContainer.setDisplayed(true);
        this.collapsed=false;
        
        if(typeof(this.events.expand)!="undefined"){
            this.events.expand.fire(this);
        }
    },
    
    collapse    : function(fast){
        this.treeNode.nodeControl.dom.src = Ext.getIconPath() + "/plus.png";
        if(!fast)
            this.treeNode.childContainer.slideOut("t", {
                easing: 'easeOut',
                duration: .3,
                useDisplay: "none"
            });
        else    
            this.treeNode.childContainer.setDisplayed(false);
        this.collapsed=true;
        
        if(typeof(this.events.collapse)!="undefined"){
            this.events.collapse.fire(this);
        }
    },
    
    doLayout    : function(){
        TreeViewElement.superclass.doLayout.call(this, arguments);
        this.treeNode.nodeText.dom.style.padding="2px";
        
        this.treeNode.nodeDistance.update("");
        for(var i=0; i<this.level; i++){
            this.treeNode.nodeDistance.dom.innerHTML += "&nbsp;&nbsp;&nbsp;";
        }
        if(this.collapsed==true)
            this.collapse(true);
        else
            this.expand(true);
        
        this.dom.addClass("xt-" + this.xType);
        this.treeNode.nodeText.addClass("xt-" + this.xType);
    },
    
    setImg      : function(imgSrc){
        if(typeof(this.treeNode.img)=="object"){
            this.treeNode.img.dom.src = imgSrc;
        }
    }
});


TreeView = Ext.extend(Visual, {
    xType       : 'treeView',
    selectedNode    : null,
    rendered        : null,
    items           : null,
    
    constructor     : function(config){
        Ext.apply(this, config);
        TreeView.superclass.constructor.call(this, config);
        return this;
    },
    
     createElements  : function(){
        TreeView.superclass.createElements.call(this);
     },
    
    render  : function(){
        TreeView.superclass.render.call(this);
        if(this.items!=null){
            for(i=0; i<this.items.length; i++){
                this.addItem(this.items[i]);
            }
        }
    },
    
    addItem    : function(child){
        if(typeof(child)=="object" && typeof(child.dom)=="object"){
            child.render(this.dom);
            child.level = 0;
            child.parent = this;
            child.addListener("childSelect", function(el){
                var it = this.items.iterator();
                while(it.hasNext()){
                    var ob = it.next();
                    if(ob!=el && ob.deselect)
                        ob.deselect(el);
                }
                this.selectedNode = el;
            }, this);
            if(this.items.indexOf(child)<0)
                this.items[this.items.length] = child;
        }
    },
    
    doLayout    : function(){
        TreeView.superclass.doLayout.call(this, arguments);
        this.dom.addClass("xt-" + this.xType);
        this.dom.dom.style.width = "100%";
    },
    
    selectNode  : function(node){
        if(this.childNode!=node){
            if(this.childNode!=null && typeof(this.childNode.deselect)=="function"){
                this.childNode.deselect();
            }
            this.childNode = node;
        }
    }
});


ContextMenu = Ext.extend(Visual, {
    items       : null,
    xType       : 'contextMenu',
    triggerObj  : null,
    mouseIn     : null,
    
    constructor     : function(config){
        this.items = [];
        Ext.apply(this, config);
        ContextMenu.superclass.constructor.call(this, config);
        this.initEvents();
        return this;
    },
    
    render      : function(renderTo){
        ContextMenu.superclass.render.call(this, renderTo);
        var it = this.items.iterator();
        while(it.hasNext()){
            this.addItem(it.next());
        }
        this.initEvents();
    },
    
    doLayout   : function(){
        ContextMenu.superclass.doLayout.call(this, arguments);
        this.dom.addClass("xt-" + this.xType);
        ContextMenu.superclass.hide.call(this);
    },
    
    show            : function(){
        ContextMenu.superclass.show.call(this, {duration: .4});
    },
    
    hide            : function(){
        ContextMenu.superclass.hide.call(this, {duration: .4});
    },
    
    addItem  : function(item){
        if(typeof(item)=="object" && item.xType=="contextMenuElement"){
            item.render(this.dom);
            item.parent = this;
            item.addListener("mouseover", function(el){
                var it = this.items.iterator();
                while(it.hasNext()){
                    var element = it.next();
                    if(element!=el)
                        element.hideSubMenu();
                }
            }, this);
            
            if(this.items.indexOf(item)<0)
                this.items[this.items.length] = item;
        }
    },
    
    applyToElement   : function(element){
        if(typeof(element)=="object" && typeof(element.dom)=="object"){
            var scope = this;
            element.addListener("rightclick", function(e, triggerObj){
               this.show();
               this.dom.setXY(e.getXY());
                this.show();
            }, this);
        }
    },
    
    initEvents      : function(){
        this.dom.addListener("mouseover", function(){}, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
        this.dom.addListener("mouseout", function(){}, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
        this.dom.addListener("click", function(){}, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
        Ext.getBody().addListener('click', function(){this.hide();}, this);
    }
});

ContextMenuElement = Ext.extend(Visual, {
    xType       : 'contextMenuElement',
    parent      : null,
    text        : '',
    imgSrc      : null,
    subMenu     : null,
    
    constructor     : function(config){
        Ext.apply(this, config);
        ContextMenuElement.superclass.constructor.call(this, config);
        this.initEvents();
        return this;
    },
    
    render          : function(renderTo){
        ContextMenuElement.superclass.render.call(this, renderTo);
        this.addSubMenu();
    },
    
    createElements  : function(){
        ContextMenuElement.superclass.createElements.call(this);
        this.cme = new Array();
        this.cme.container = this.dom.insertHtml("beforeEnd", "<table><tr><td></td><td></td><td><td/></tr></table>", true);
        
        this.cme.imgContainer = Ext.get(this.cme.container.dom.firstChild.firstChild.firstChild);
        this.cme.textContainer = this.cme.imgContainer.next("", false);
        this.cme.subContainer = this.cme.textContainer.next("", false);
        
        if(this.imgSrc!=null){
            this.cme.img = this.cme.imgContainer.insertHtml("beforeEnd", "<img src='" + this.imgSrc + "'/>", true);
        }
        
   },
    
    initEvents  : function(){
        this.applyEvents(this.dom, ["click", "mouseover", "mouseout"]);
        this.dom.addListener('mouseover', function(e){
            this.dom.addClass("xt-" + this.xType + "-selected");
        }, this);
        
        this.dom.addListener('mouseout', function(){
            this.dom.removeClass("xt-" + this.xType + "-selected");
        }, this);
    },
    
    doLayout   : function(){
        ContextMenuElement.superclass.doLayout.call(this, arguments);
        this.setText();
        this.dom.addClass("xt-" + this.xtype);
        this.cme.container.addClass("xt-" + this.xType + "-container");
        this.cme.imgContainer.addClass("xt-" + this.xType + "-imgContainer");
        this.cme.textContainer.addClass("xt-" + this.xType + "-textContainer");
        this.cme.subContainer.addClass("xt-" + this.xType + "-submenu");
        if(this.subMenu) this.cme.subContainer.addClass("xt-" + this.xType + "-submenu-displayed");
        
        if(this.imgSrc!=null){
            this.cme.img.addClass("xt-" + this.xType + "-img");
        }
    },
    
    setText : function(text){
        this.text = (text ? text : this.text);
        this.cme.textContainer.update(this.text);
    },
    
    show    : function(){
        ContextMenuElement.superclass.show.call(this, {duration: .4});
    },
    
    hide    : function(){
        ContextMenuElement.superclass.hide.call(this, {duration: .4});
    },
    
    hideSubMenu : function(){
        if(this.subMenu) this.subMenu.hide();
        if(this.subMenu){
            var it = this.subMenu.items.iterator();
            while(it.hasNext()){
                var element = it.next();
                element.hideSubMenu();
            }
        }
    },
    
    addSubMenu : function(subMenu){
        if(this.subMenu != null && this.subMenu.xType == "contextMenu" && subMenu!=null && subMenu.xType == "contextMenu"){
            this.subMenu.dom.remove();
        }
        if(subMenu!=null && subMenu.xType == "contextMenu"){
            this.subMenu = subMenu;
        }
        if(this.subMenu!=null && this.subMenu.xType == "contextMenu"){
            this.subMenu.render(this.dom);
            this.addListener("mouseover", function(e, el){
                this.subMenu.show();
                this.subMenu.dom.setXY([this.cme.subContainer.getXY()[0]+this.cme.subContainer.getWidth(),this.cme.subContainer.getXY()[1]]);
            }, this);
            this.cme.subContainer.addClass("xt-" + this.xType + "-submenu-displayed");
        }
    }
});

Mask = Ext.extend(Visual, {
    xType       : 'mask',
    targetObj   : null,
    hidden      : true,
    backgrondColor  : null,
    taransparency   : null,
    
    constructor     : function(config){
        if(!config.backgrondColor) config.backgrondColor = "gray";
        if(!config.taransparency) config.taransparency = 0.4;
        config.renderTo = (!config.renderTo && config.targetObj ? config.targetObj : config.renderTo);
        
        Ext.apply(this, config);
        Mask.superclass.constructor.call(this, config);
        
        if(!this.shown){
            this.shown = true;
            this.hide();
        }
        else{
            this.shown = false;
            this.show();
        }
        
        return this;
    },
    
    doLayout   : function(){
        Mask.superclass.doLayout.call(this, arguments);
        this.dom.setStyle("backgroundColor", this.backgrondColor);
        this.setTargetObj(this.targetObj);
    },
    
    setTargetObj    : function(target){
        this.targetObj = (typeof(target)=="object" && target!=null ? target : this.targetObj);
        this.targetObj = Ext.get(this.targetObj);
        if(this.targetObj){
            if(this.targetObj==Ext.getBody()){
                var me = this;
                Ext.Event.addListener(window, "resize", function(){
                    me.resize();
                });
                this.dom.setStyle("position", "fixed");
            }
            else{
                this.targetObj.addListener("resize", function(){
                    this.resize();
                }, this);
                this.dom.setStyle("position", "absolute");
            }
        }
    },
    
    show    : function(){
        if(!this.shown){
            this.dom.setStyle("zIndex", Ext.getMaxZindex());
            this.targetObj.oldScroll = this.targetObj.getScroll();
            this.targetObj.scrollTo("top", this.targetObj.oldScroll.top);
            this.targetObj.oldClassName = this.targetObj.dom.className;
            this.targetObj.dom.className = "xt-mask-body-hide";
            this.resize();
            this.dom.setDisplayed(true);
            this.dom.setOpacity(this.taransparency);
            this.shown = true;
        }
    },
    hide    : function(){
        if(this.shown){
            if(typeof(this.targetObj)=="object" && this.targetObj.oldClassName){
                this.targetObj.dom.className = this.targetObj.oldClassName;
                
                if(this.targetObj.oldScroll)
                    this.targetObj.scrollTo("bottom", this.targetObj.oldScroll.top);
            }
            this.dom.setOpacity(0);
            this.dom.setDisplayed(false);
            this.shown = false;
        }
    },
    resize  : function(){
        if(this.targetObj && this.rendered){
        	if(this.targetObj==Ext.getBody()){
        		this.dom.setStyle("top", "0px");
        		this.dom.setStyle("left", "0px");
	            this.dom.setStyle("width", "100%");
	            this.dom.setStyle("height", "100%");
        	}
        	else{
	        	this.dom.setWidth(this.targetObj.getWidth()-1);
	            this.dom.setHeight(this.targetObj.getHeight()-1);
	            this.dom.setLeft(this.targetObj.getXY()[0]);
	            this.dom.setTop(this.targetObj.getXY()[1]);
        	}
        }
    }
});

WorkLoad = Ext.extend(Visual, {
    xType       : 'workLoad',
    text        : '',
    mask        : null,
    
    constructor     : function(config){
        config.renderTo = (!config.renderTo && config.targetObj ? config.targetObj : config.renderTo);
        Ext.apply(this, config);
        WorkLoad.superclass.constructor.call(this, config);
        
        if(!this.shown){
            this.shown = true;
            this.hide();
        }
        else{
            this.shown = false;
            this.show();
        }
        
        return this;
    },
    
    createElements  : function(){
        WorkLoad.superclass.createElements.call(this);
        this.workLoad = new Array();
        
        this.workLoad.containerDiv = this.dom.insertHtml("beforeEnd", "<div><table><tr><td align='center' valign='middle'><img src='"+Ext.getIconPath() + "/loader.gif'/></td><td align='center' valign='middle'><div></div></td></tr></table></div>", true);
        
        this.workLoad.containerTable = Ext.get(this.workLoad.containerDiv.dom.firstChild.firstChild.firstChild);
        this.workLoad.loaderImg = Ext.get(this.workLoad.containerTable.dom.firstChild.firstChild);
        this.workLoad.loaderText = Ext.get(this.workLoad.containerTable.dom.firstChild).next("", false);
        
        this.mask = new Mask({
            renderTo: this.renderTo,
            targetObj: this.targetObj,
            shown: this.shown
        });
        this.setTargetObj(this.targetObj);
   },
   
   setTargetObj    : function(target){
        this.targetObj = (typeof(target)=="object" && target!=null ? target : this.targetObj);
        this.targetObj = Ext.get(this.targetObj);
        if(this.targetObj == Ext.getBody()){
            this.dom.setStyle("position", "fixed");
        }
        if(this.targetObj){
            this.targetObj.addListener("resize", function(){
                this.dom.setWidth(this.targetObj.getWidth());
                this.dom.setHeight(this.targetObj.getHeight());
                this.dom.setXY(this.targetObj.getXY());
                this.resize();
            }, this);
            this.mask.setTargetObj(this.targetObj);
        }
    },
    
    show    : function(){
        this.mask.show();
        this.resize();
        WorkLoad.superclass.show.call(this);
    },
    
    doLayout    : function(){
        this.workLoad.containerDiv.addClass("xt-workLoad-visible");
        this.workLoad.loaderImg.addClass("xt-workLoad-img");
        this.workLoad.loaderText.addClass("xt-workLoad-text");
        this.dom.setStyle("position", "absolute");
        this.setText();
    },
    
    hide    : function(){
        this.mask.hide();
        WorkLoad.superclass.hide.call(this);
    },
    
    resize  : function(){
        if(typeof(this.targetObj)=="object"){
            var mw = this.workLoad.containerDiv.getWidth()/2;
            var mh = this.workLoad.containerDiv.getHeight()/2;
            this.dom.setLeft(this.targetObj.getWidth()/2-mw+this.targetObj.getXY()[0]);
            this.dom.setTop(this.targetObj.getHeight()/2-mh+this.targetObj.getXY()[1]);
        }
        this.dom.setStyle("zIndex", Ext.getMaxZindex());
    },
    
    setText     : function(text){
        this.text = (text ? text : this.text);
        this.workLoad.loaderText.dom.innerHTML = this.text;
    }
});

ComboItem = Ext.extend(Object, {
    text    : null,
    value   : null,
    el      : null,
    xType   :'combo-item',
    constructor     : function(config){    
        Ext.apply(this, config);
        ComboItem.superclass.constructor.call(this, config);
        return this;
    }, 
    
    setEl   : function(el){
        this.el = (typeof(el)!="undefined" && el ? el : this.el);
    }
});

Label = Ext.extend(Visual, {
    xType       : 'label',
    text        : '',
    align       : 'left',
    constructor     : function(config){
        Ext.apply(this, config);
        Label.superclass.constructor.call(this, config);
        return this;
    },
    
    initEvents  : function(){
        this.applyEvents(this.dom.dom, ["click", "dbclick", "mouseover", "mouseout"]);
    },
    
    doLayout   : function(){
        this.dom.addClass("xt-" + this.xType);
        this.setText();
        this.setSize();
        this.setAlign();
    },
    
    setText    : function(text){
        this.text = (text != null ? text : this.text);
        this.dom.update(this.text);
    },
    
    setAlign    : function(align){
        this.text = (align && /^left|right|center&/.test(align) ? align : this.align);
        this.dom.setStyle("textAlign", this.align);
    },
    
    gettext    : function(){
        return this.dom.dom.innerHTML;
    }
});


Combo = Ext.extend(Visual, {
    xType       : 'combo',
    labelText   : '',
    noLabel     : false,
    name        : null,
    autoWidth   : true,
    align       : null,
    items       : null,
    disabled    : false,
    selectedIndex: null,
    labelWidth  : null,
    label       : null,
    value       : null,
    
    constructor     : function(config){
        Ext.apply(this, config);
        this.label = null;
        this.labelWidth = (this.labelWidth ? this.labelWidth : 100);
        Combo.superclass.constructor.call(this, config);
        this.initEvents();
        this.setValue();
        return this;
    },
    
    render  : function(renderTo){
        Combo.superclass.render.call(this, renderTo);
        if(this.items!=null && typeof(this.items.iterator)=="function"){
            var it = this.items.iterator();
            while(it.hasNext()){
                var item = it.next();
                this.addItem(item);
            }
        }
    },
    
    createElements  : function(){
        Combo.superclass.createElements.call(this);
        this.combo = new Array();
        
        if(this.noLabel==false){
            this.combo.containerTable = this.dom.insertHtml("beforeEnd", "<table cellspacing='0px' cellpadding='0px' style='width:100%;'><tr><td align='left' valign='middle' width='100px'><div></div></td><td align='left' valign='middle'><select></select></td></tr></table>", true);
            this.combo.containerTr = Ext.get(this.combo.containerTable.dom.firstChild.firstChild);
            
            this.combo.textContainer = Ext.get(this.combo.containerTr.dom.firstChild.firstChild);
            this.combo.combo = Ext.get(Ext.get(this.combo.containerTr.dom.firstChild).next("", true).firstChild);
            this.label=new Label({
                text: this.labelText,
                renderTo: this.combo.textContainer
            });
        }
        else{
            this.combo.combo = this.dom.insertHtml("beforeEnd", "<select></select>", true);
        }
   },
    
    initEvents  : function(){
        this.applyEvents(this.combo.combo.dom, ["dbclick", "mouseover", "mouseout"]);
        this.applyEvents(this.combo.combo.dom, ["click", "keyup", "keypress", "change"], true);
    },
    
    doLayout   : function(){
        Combo.superclass.doLayout.call(this, arguments);
        if(this.noLabel==false){
            this.combo.containerTable.addClass("xt-combo-table");
        }
        this.combo.combo.addClass("xt-combo-input");
        this.dom.addClass("xt-" + this.xType);
            
        this.setLabelText();
        this.setName();
        this.setSize();
        this.setDisabled();
        this.setSelectedIndex();
        this.setLabelWidth();
        this.setValue();
        
        if(this.align){
            this.dom.dom.style.textAlign = this.align;
        }
    },
    
    setSelectedIndex    : function(index){
        if(index && index<this.items.length)
            this.index = index;
        if(index)
            this.setItem(this.items[index]);
        
    },
    
    setLabelWidth: function(width){
        this.labelWidth = (width && /[\d]*/.test(width) ? width : this.labelWidth);
        
        if(/[\d]*/.test(this.labelWidth) && !this.noLabel && this.label)
            this.label.setWidth(this.labelWidth);
    },
    
    setLabelText    : function(labelText){
        if(this.noLabel==false){
            this.labelText = (labelText != null ? labelText : this.labelText) + ":";
            this.label.setText(this.labelText);
        }
    },
    setDisabled     : function(disabled){
        this.disabled = (disabled!=null ? disabled: this.disabled);
        if(this.disabled)
            this.combo.combo.dom.disabled = true;
        else
            this.combo.combo.dom.disabled = "";
    },
    
    addItem     : function(item){
        if(item==null || item.xType!='combo-item') return;
        var elOptNew = document.createElement('option');
        elOptNew.text = item.text;
        elOptNew.value = item.value;
        var elOptOld = null;
        
        if(this.items==null) this.items = new Array();
        if(this.items.indexOf(item)<0){
            this.items[this.items.length] = item;
        }
        
        try {
            this.combo.combo.dom.add(elOptNew, null);
            
        }
        catch(ex) {
          this.combo.combo.dom.add(elOptNew);
        }
        item.setEl(elOptNew);
    },
    
    removeItem  : function(item){
        if(item==null) return;
        if(this.items.indexOf(item)>=0){
            this.items.remove(item);
            for(var i=0; i<this.combo.combo.dom.options.length; i++){
                if(this.combo.combo.dom.options[i].value==item.value){
                    if(typeof(this.combo.combo.dom.options.remove)!="undefined")
                        this.combo.combo.dom.options.remove(i);
                    else if(typeof(this.combo.combo.dom.remove)!="undefined")
                        this.combo.combo.dom.remove(i);
                }
            }
        }
    },
    
    clearItems  : function(){
        while(this.items!=null && this.items.length>0){
            this.removeItem(this.items[0]);
        }
    },
    
    focus       : function(){
        this.combo.combo.focus();
    },
    
    setValue    : function(value){
        this.value = (value ? value : this.value);
        if(!this.value) return;
        var idx = -1;
        var i=0;
        for(i=0; i<this.items.length; i++){
            if(this.items[i].value==this.value){
                idx=i; break;
            }
        }
        if(idx>=0){
            this.setItem(this.items[idx]);
        }
    },
    getValue    : function(){
        return this.combo.combo.getValue();
    },
    setName     : function(name){
        this.name = (name!=null ? name : this.name);
        if(this.name!=null) this.combo.combo.set({name: this.name});
    },
    
    getName     : function(){
        if(typeof(this.combo.combo.dom.name)!="undefined")
            return this.combo.combo.getName();
        else
            return null;
    },
    
    setWidth    : function(width){
        if(this.autoWidth==false){
            this.width = (width && /(d)*/.test(width) ? width : this.width);
            this.combo.combo.setWidth(this.width);
        }
    },
    
    setHeight    : function(height){
        if(this.autoHeight==false){
            this.height = (height && /(d)*/.test(height) ? height : this.height);
            this.combo.combo.setHeight(this.height);
            
        }
    },
    
    setSize     : function(width, height){
        this.setWidth(width);
        this.setHeight(height);
    },
    
    setItem     : function(item){
        if(typeof(item)!="object" || item.xType!="combo-item" || this.items.indexOf(item)==-1) return;
		for(var i=0; i<this.combo.combo.dom.options.length; i++){
			if(this.combo.combo.dom.options[i].value == item.value){
				this.combo.combo.dom.selectedIndex = i;
				return;
			}
		}
	}
});

Field = Ext.extend(Visual, {
    xType       : 'field',
    labelText   : '',
    labelWidth  : '',
    value       : '',
    validator   : null,
    valid       : true,
    noLabel     : false,
    name        : null,
    type        : 'text',
    autoWidth   : true,
    autoHeight  : true,
    align       : null,
    textarea    : false,
    id          : null,
    noValidate  : false,
    checked       : false,
    disabled	: false,
    
    constructor     : function(config){
        config.selectable = true;
        Ext.apply(this, config);
        this.label = null;
        this.labelWidth = (this.labelWidth ? this.labelWidth : 100);
        this.id = (this.id ? this.id : Ext.id());
        this.autoWidth = (this.width ? false : this.autoWidth);
        this.autoHeight = (this.height ? false : this.autoHeight);
        Field.superclass.constructor.call(this, config);
        return this;
    },
    
    createElements  : function(){
        Field.superclass.createElements.call(this);
        this.field = new Array();
        
        if(!/text|password|file|hidden|checkbox/.test(this.type))
            this.type="text";
        if(this.noLabel==false){
            if(this.textarea==false)
                this.field.containerTable = Ext.get(this.dom.insertHtml("beforeEnd", "<table cellspacing='0px' cellpadding='0px'><tr><td align='left' valign='middle'><div></div></td><td align='left' valign='middle'><input type='"+this.type+"'></td></tr></table>"));
            else
                this.field.containerTable = Ext.get(this.dom.insertHtml("beforeEnd", "<table cellspacing='0px' cellpadding='0px'><tr><td align='left' valign='middle'><div></div></td><td align='left' valign='middle'><textarea></textarea></td></tr></table>"));
            
            this.field.containerTr = Ext.get(this.field.containerTable.dom.firstChild.firstChild);
            this.field.textContainer = Ext.get(this.field.containerTr.dom.firstChild.firstChild);
            this.field.input = Ext.get(this.getNextChild(this.field.containerTr.dom, this.field.containerTr.dom.firstChild).firstChild);
            
            this.label = new Label({
                text: this.labelText,
                width: this.labelWidth,
                renderTo: this.field.textContainer
            });
        }
        else{
            if(this.textarea==false)
                this.field.input = Ext.get(this.dom.insertHtml("beforeEnd", "<input type='"+this.type+"'>"));
            else
                this.field.input = Ext.get(this.dom.insertHtml("beforeEnd", "<textarea></textarea>"));
        }
        
        this.applyEvents(this.field.input.dom, ["dbclick", "mouseover", "mouseout"]);
        this.applyEvents(this.field.input.dom, ["click", "keyup", "keypress", "change", "keydown"], true);
        
        var scope = this;
        this.addListener('keyup', function(){
            scope.validate();
        });
   },
    
    doLayout   : function(){
        Field.superclass.doLayout.call(this, arguments);
        this.dom.addClass("xt-" + this.xType);
        if(!this.noLabel)
            this.field.containerTable.addClass("xt-" + this.xType + "-table");
        this.field.input.addClass("xt-" + this.xType + "-input");
        
        this.setValue();
        this.setName();
        this.setSize();
        this.setLabelText();
        this.setLabelWidth();
        this.setId();
        this.setCheck();
        this.setDisabled();
        if(this.align){
            this.dom.dom.style.textAlign = this.align;
        }
        
        if(/checkbox/.test(this.type)){
            this.field.input.setStyle("padding", "0px");
            this.field.input.setStyle("height", "auto");
            this.field.input.setStyle("border", "0px");
        }
    },
    
    setCheck    : function(value){
        if(/checkbox/.test(this.type)){
        	this.checked = (value==true || value==false ? value : this.checked);
            this.checked = (value==0 ? false : this.checked);
            this.checked = (value==1 ? true : this.checked);
            
            this.checked = (this.checked==0 ? false : this.checked);
            this.checked = (this.checked==1 ? true : this.checked);
            
            if(this.checked==true || this.checked==false)
                this.field.input.dom.checked = this.checked;
        }
    },
    
    setValue    : function(value){
            this.value = (value && this.validate(value) ? value : this.value);
			if(this.textarea==false){
	            if(this.value && this.validate(this.value))
	                this.field.input.set({value: this.value});
			}
			else{
					this.field.input.update(this.value);
			}
    },
    validate    : function(value){
        value = (value ? value : this.field.input.dom.value);
        if(!this.validator || this.noValidate)
            return true;
        if(this.validator.test(value)){
            this.valid = true;
            this.setValidate(true);
        }
        else {
            this.valid = false;
            this.setValidate(false);
        }
        return this.valid;
    },
    
    setValidate: function(value){
        if(value){
            this.field.input.addClass("xt-" + this.xType + "-input");
            this.field.input.removeClass("xt-" + this.xType + "-input-invalid");
        }
        else{
            this.field.input.addClass("xt-" + this.xType + "-input-invalid");
            this.field.input.removeClass("xt-" + this.xType + "-input");
        }
    },
    
    focus       : function(){
        this.field.input.focus();
    },
    
    getValue    : function(){
        return this.field.input.dom.value;
    },
    setName     : function(name){
        this.name = (name!=null ? name : this.name);
        if(this.name!=null) this.field.input.set({name: this.name});
    },
    
    getName     : function(){
        if(typeof(this.field.input.dom.name)!="undefined")
            return this.field.input.dom.name;
        else
            return null;
    },
    
    setDisabled     : function(disabled){
        this.disabled = (disabled!=null ? disabled: this.disabled);
        if(this.disabled){
        	this.field.input.dom.disabled = true;
        } else {
        	this.field.input.dom.disabled = "";
        }
    },
    
    enable		: function(){
    	this.setDisabled(false);
    },
    
    disable		: function(){
    	this.setDisabled(true);
    },
    
    setWidth    : function(width){
        if(this.autoWidth==false){
            this.width = (width != null && /(d)*/.test(width) ? width : this.width);
            this.field.input.setWidth(this.width);
        }
    },
    
    setHeight    : function(height){
        if(this.autoHeight==false){
            this.height = (height != null && /(d)*/.test(height) ? height : this.height);
            this.field.input.setHeight(this.height);
        }
    },
    
    setSize     : function(width, height){
        this.setWidth(width);
        this.setHeight(height);
    },
    
    setLabelWidth: function(width){
        this.labelWidth = (width && /[\d]*/.test(width) ? width : this.labelWidth);
        
        if(/[\d]*/.test(this.labelWidth) && !this.noLabel && this.label)
            this.label.setWidth(this.labelWidth);
    },
    
    setLabelText    : function(labelText){
        if(this.noLabel==false){
            this.labelText = (labelText != null ? labelText : this.labelText) + ":";
            this.label.setText(this.labelText);
        }
    },
    
    setId: function(id){
        this.id = (id ? id : this.id);
        this.field.input.set({id: this.id});
    }
});

Panel = Ext.extend(Visual, {
    xType       : 'panel',
    text        : '',
    autoWidth   : false,
    autoHeight  : false,
    items       : null,
    buttons     : null,
    form        : false,
    action      : null,
    target      : null,
    method      : 'POST',
    enctype     : 'multipart/form-data',
    html        : null,
    color       : null,
    
    constructor     : function(config){
        Ext.apply(this, config);
        Panel.superclass.constructor.call(this, config);
        return this;
    },
    
    createElements  : function(){
        Panel.superclass.createElements.call(this);
        this.panel = new Array();
        if(!this.form){
            this.panel.baseContainer = this.dom.insertHtml("beforeEnd", "<div></div><div><div style='width:auto;' align='right'><table><tr><td></td></tr></table></div></div>", true);
            this.panel.itemContainer = Ext.get(this.dom.dom.firstChild);
        }
        else{
        	this.panel.baseContainer = this.dom.insertHtml("beforeEnd", "<div><form></form></div><div><div style='width:auto;' align='right'><table><tr><td></td></tr></table></div></div>", true);
            this.panel.itemContainer = Ext.get(this.dom.dom.firstChild.firstChild);
            
            if(this.target!=null) this.panel.itemContainer.dom.target = this.target;
            if(this.action!=null) this.panel.itemContainer.dom.action = this.action;
            this.panel.itemContainer.dom.method = this.method;
            this.panel.itemContainer.dom.encoding = this.enctype;
        }
        
        this.panel.buttonParent = Ext.get(Ext.get(this.dom.dom.firstChild).next("", true).firstChild);
        this.panel.buttonContainer = Ext.get(Ext.get(this.dom.dom.firstChild).next("", true).firstChild.firstChild.firstChild.firstChild);
    },
    
    render     : function(renderTo){
    	Panel.superclass.render.call(this, renderTo);
        
        if(this.items!=null && typeof(this.items.iterator)=="function"){
            var it = this.items.iterator();
            while(it.hasNext()) this.add(it.next());
        }
        if(this.buttons!=null && typeof(this.buttons.iterator)!="function"){
            var it = this.buttons.iterator();
            while(it.hasNext()) this.addButton(it.next());
        }
    },
    
    doLayout   : function(){
    	Panel.superclass.doLayout.call(this, arguments);
        this.setSize();
        this.setBorder();
        //this.setAction();
        //this.setMethod();
        //this.setEnctype();
        //this.setTarget();
        this.setHtml();
        this.setColor();
    },
    
    setBorder   : function(value){
        this.border = (value ? value : this.border);
        if(this.border){
            this.panel.itemContainer.addClass("xt-" + this.xType + "-border");
        }
        else{
            this.panel.itemContainer.removeClass("xt-" + this.xType + "-border");
        }
    },
    
    setWidth    : function(width){
        if(this.autoWidth==false){
            this.width = (width != null && /(d)*/.test(width) ? width : this.width);
            if(this.width) Panel.superclass.setWidth.call(this, this.width);
        }
        else this.dom.setStyle("width", "auto");
    },
    
    setHeight    : function(height){
        if(this.autoHeight==false){
            this.height = (height != null && /(d)*/.test(height) ? height : this.height);
            if(this.height) Panel.superclass.setHeight.call(this, this.height);
        }
        if(this.buttons==null){
            this.panel.buttonParent.setDisplayed(false);
            if(this.height)
                this.panel.itemContainer.setHeight(this.height);
        }
        else{
            this.panel.buttonParent.setDisplayed(true);
            if(this.height && this.height-25>0)
                this.panel.itemContainer.setHeight(this.height-25);
        }
    },
    
    setSize     : function(width, height){
        this.setWidth(width);
        this.setHeight(height);
    },
    
    add         : function(item){
        if(typeof(item)=="object" && typeof(item.xType)=="string"){
            item.render(this.panel.itemContainer);
            if(this.items==null) this.items = [item];
            else{
                if(this.items.indexOf(item)<0)
                    this.items[this.items.lenght] = item;
            }
        }
    },
    addButton         : function(item){
        if(typeof(item)=="object" && typeof(item.dom)=="object"){
            item.render(this.panel.buttonContainer.insertHtml("beforeEnd", "<td></td>", true));
            if(this.buttons==null) this.buttons = [item];
            else{
                if(this.buttons.indexOf(item)<0)
                    this.buttons[this.buttons.lenght] = item;
            }
            this.setHeight();
        }
    },
    
    setTarget       : function(target){
    	if(this.form==true){
    		this.target = (target!=null ? target : this.target);
            if(this.target) this.panel.itemContainer.set({target: this.target});
        }
    },
    
    setAction       : function(action){
        if(this.form==true){
            this.action = (action!=null ? action : this.action);
            if(this.action) this.panel.itemContainer.set({action: this.action});
        }
    },
    
    getAction       : function(){
        if(this.form==true)
            return this.panel.itemContainer.dom.action;
        else
            return null;
    },
    
    setMethod       : function(method){
        if(this.form==true){
            this.method = (method!=null ? method : this.method);
            if(this.method) this.panel.itemContainer.set({method: this.method});
        }
    },
    
    setEnctype       : function(enctype){
        if(this.form==true){
        	this.enctype = (enctype!=null ? enctype : this.enctype);
            if(this.enctype) this.panel.itemContainer.set({encoding: this.enctype});
        }
    },
    
    submit          : function(){
    	if(this.form==true){
            this.panel.itemContainer.dom.submit();
        }
    },
    
    setHtml         : function(html){
        if(this.items==null){
            this.html = (html ? html : this.html);
            if(this.html) this.panel.itemContainer.update(this.html);
        }
    },
    
    setColor        : function(color){
        this.color = (color && /#[a-zA-Z0-9]{6,6}/.test(color) ? color : this.color);
        if(this.color && /#[a-zA-Z0-9]{6,6}/.test(this.color)){
            this.dom.setStyle("backgoundColor", this.color);
        }
    }
});

ButtonXP = Ext.extend(Visual, {
    xType       : 'button',
    text        : '',
    renderTo    : null,
    rendered    : null,
    disabled    : false,
    
    constructor     : function(config){
        Ext.apply(this, config);
        ButtonXP.superclass.constructor.call(this, config);
        this.setDisabled();
        return this;
    },
    
    createElements  : function(){
        ButtonXP.superclass.createElements.call(this);
        this.button = new Array();
        
        this.button.baseTable =Ext.get(this.dom.insertHtml("beforeEnd", "<table cellborder='0px' cellspacing='0px'><tr><td>&nbsp;&nbsp;</td><td align='center' valign='middle'><div></div></td><td>&nbsp;&nbsp;</td></tr></table>"));
        this.button.baseTr = Ext.get(this.button.baseTable.dom.firstChild.firstChild);
        this.button.left = Ext.get(this.button.baseTable.dom.firstChild.firstChild.firstChild);
        this.button.center = Ext.get(this.getNextChild(this.button.baseTr.dom, this.button.left.dom));
        this.button.right = Ext.get(this.getNextChild(this.button.baseTr.dom, this.button.center.dom));
        this.button.button = Ext.get(this.button.center.dom.firstChild);
    },
    
    initEvents  : function(){
        // this.applyEvents(this.button.button.dom, ["keyup", "keypress",
		// "click", "dbclick", "mouseover", "mouseout", "mousedown",
		// "mouseup"]);
        // var scope=this;
        this.button.baseTable.addListener("mouseover", function(){
            this.button.left.dom.className = "xt-button-left-over";
            this.button.right.dom.className = "xt-button-right-over";
            this.button.center.dom.className = "xt-button-center-over";
            if(this.events.mouseover)
                this.events.mouseover.fire(this);
        }, this);
        
        this.button.baseTable.addListener("mouseout", function(){
            this.button.left.dom.className = "xt-button-left-inactiv";
            this.button.right.dom.className = "xt-button-right-inactiv";
            this.button.center.dom.className = "xt-button-center-inactiv";
            if(this.events.mouseout)
                this.events.mouseout.fire(this);
        }, this);
        
        this.button.baseTable.addListener("mousedown", function(){
            this.button.left.dom.className = "xt-button-left-down";
            this.button.right.dom.className = "xt-button-right-down";
            this.button.center.dom.className = "xt-button-center-down";
            if(this.events.mousedown)
                this.events.mousedown.fire(this);
        }, this);
        
        this.button.baseTable.addListener("mouseup", function(){
            this.button.left.dom.className = "xt-button-left-inactiv";
            this.button.right.dom.className = "xt-button-right-inactiv";
            this.button.center.dom.className = "xt-button-center-inactiv";
            if(this.events.mouseup)
                this.events.mouseup.fire(this);
        }, this);
        
        this.button.button.addListener("click", function(){
            if(this.events.click)
                this.events.click.fire(this);
        }, this);
    },
    
    removeEvents: function(){
        this.dom.removeAllListeners();
    },
    
    setDisabled: function(value){
        this.disabled = (value || value==false ? value : this.disabled);
        if(this.disabled==true){
            this.removeEvents();
            this.dom.addClass("xt-" + this.xType + "-disabled");
        }
        else{
            this.initEvents();
            this.dom.removeClass("xt-" + this.xType + "-disabled");
        }
    },
    
    doLayout   : function(){
        ButtonXP.superclass.doLayout.call(this, arguments);
        this.setText();
        this.dom.addClass("xt-" + this.xType);
        this.button.button.addClass("xt-" + this.xType + "-button");
        this.button.left.addClass("xt-button-left-inactiv");
        this.button.right.addClass("xt-button-right-inactiv");
        this.button.center.addClass("xt-button-center-inactiv");
        // this.dom.dom.style.width="auto";
    },
    
    setText    : function(text){
        this.text = (text!=null?text:this.text);
        this.button.button.dom.value = this.text;
        this.button.button.dom.innerHTML = this.text;
        
        this.button.left.addClass("xt-button-left-inactiv");
        this.button.right.addClass("xt-button-right-inactiv");
    },
    
    setWidth: function(width){
        this.width = (width && /(\d)*/.test(width) ? width : this.width);
        if(this.width)
            this.button.center.setWidth(this.width);
    },
    
    setheight: function(height){
        this.height = (height && /(\d)*/.test(height) ? height : this.height);
        if(this.height)
            this.button.center.setWidth(this.height);
    }
});

Window = Ext.extend(Visual, {
    xType       : 'button',
    text        : '',
    width       : 100,
    height      : 30,
    title       : '',
    items       : null,
    buttons     : null,
    lastMousePosition   : null,
    lastPosition        : null,
    innerBorder         : true,
    dragable            : true,
    html        : null,
    masked      : false,
    center      : false,
    targetObj   : null,
    hidden      : true,
    whitePanel  : false,
    
    constructor     : function(config){
	    Ext.apply(this, config);
	    if(this.masked){
            this.center = true;
        }
        if(this.center){
            this.dragable = false;
            this.targetObj = (this.targetObj ? this.targetObj : Ext.getBody());
        }
        
        this.renderTo =(this.renderTo ? this.renderTo : Ext.getBody());
        
        Window.superclass.constructor.call(this, config);
        
        if(this.rendered==true){
        	if(this.items!=null && typeof(this.items.length)!="undefined"){
                var it = this.items.iterator();
                while(it.hasNext()){
                	this.add(it.next());
                }
            }
            if(this.buttons!=null && typeof(this.buttons.length)!="undefined"){
                var it = this.buttons.iterator();
                while(it.hasNext()){
                    this.addButton(it.next());
                }
            }
        }
        
        this.initEvents();
        return this;
    },
    
    createElements  : function(){
        Window.superclass.createElements.call(this);
        this.window = new Array();
        
        this.window.baseTable =this.dom.insertHtml("beforeEnd", "<table cellborder='0px' cellspacing='0px' style=''>" + 
            "<tr><td></td><td><div></div><div></div></td><td></td></tr>" + 
            "<tr><td>&nbsp;&nbsp;</td><td valign='top'></td><td>&nbsp;&nbsp;</td></tr>" + 
            "<tr><td></td><td></td><td></td></tr>" + "</table>", true);
            
        this.window.hiddenTable =this.dom.insertHtml("beforeEnd", "<table cellborder='0px' cellspacing='0px' style=''>" + 
            "<tr><td class='xt-window-hidden-head'>&nbsp;</div></td></tr>" + 
            "<tr><td class='xt-window-hidden-body'>&nbsp;</td></tr>" + "</table>", true);
        this.window.hiddenTitle = Ext.get(this.window.hiddenTable.dom.firstChild.firstChild.firstChild);
        this.window.hiddenContainer = Ext.get(Ext.get(this.window.hiddenTable.dom.firstChild.firstChild).next("", true).firstChild);
        
        this.window.baseTTr = Ext.get(this.window.baseTable.dom.firstChild.firstChild);
        this.window.baseCTr = this.window.baseTTr.next("");
        this.window.baseBTr = this.window.baseCTr.next("");
        
        this.window.tl = Ext.get(this.window.baseTTr.dom.firstChild);
        this.window.tc = this.window.tl.next("");
        this.window.tr = this.window.tc.next("");
        
        this.window.cl = Ext.get(this.window.baseCTr.dom.firstChild);
        this.window.cc = this.window.cl.next("");
        this.window.cr = this.window.cc.next("");
        
        this.window.bl = Ext.get(this.window.baseBTr.dom.firstChild);
        this.window.bc = this.window.bl.next("");
        this.window.br = this.window.bc.next("");
        
        this.window.title = Ext.get(this.window.tc.dom.firstChild);
        this.window.close = this.window.title.next("");
        
        this.window.containerPanel = new Panel({
            border: this.innerBorder,
			autoWidth: true,
			autoHeight: false,
            renderTo    : this.window.cc,
            color: (this.whitePanel ? "#ffffff" : null)
        });
        if(this.masked){
            this.window.mask = new Mask({
                renderTo: this.renderTo,
                targetObj: this.targetObj,
                hidden: this.hidden
            });
        }
    },
    
    initEvents  : function(){
        var scope = this;
        this.window.close.addListener("mouseover", function(){
            this.window.close.addClass("xt-window-close-over");
            this.window.close.removeClass("xt-window-close-out");
        }, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
        
        this.window.close.addListener("mouseout", function(){
            this.window.close.addClass("xt-window-close-out");
            this.window.close.removeClass("xt-window-close-over");
        }, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
        this.window.close.addListener("mousedown", function(e){
        }, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
        this.window.close.addListener("click", function(e){
            this.hide();
            if(typeof(this.events.close)!="undefined"){
                this.events.close.fire(e, this);
            }
        }, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
        
        
        if(this.dragable){
            this.window.tc.addListener("mousedown", function(e){
                this.lastMousePosition = {x: e.xy[0], y: e.xy[1]};
                
                var lastPosition = scope.getPosition(scope.dom);
                this.left = lastPosition.x;
                this.top = lastPosition.y;
                
                this.window.hiddenTable.setWidth(this.width);
                this.window.hiddenTable.setHeight(this.height);
                this.window.hiddenTitle.setWidth(this.width);
                this.window.hiddenContainer.setWidth(this.width);
                this.window.hiddenContainer.setHeight(this.height-20);
                
                this.window.hiddenTable.setDisplayed(true);
                this.window.baseTable.setDisplayed(false);
                
                this.dom.dom.style.zIndex = Ext.getMaxZindex();
                
                Ext.getBody().addListener("mousemove", folowMouse, this);
            }, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
            
            this.window.hiddenTable.addListener("mouseup", function(){
                Ext.getBody().removeListener("mousemove", folowMouse, this);
                
                this.window.hiddenTable.setDisplayed(false);
                this.window.baseTable.setDisplayed(true);
            }, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
            
            function folowMouse(e){
                mousePosition = {x: e.xy[0], y: e.xy[1]};
                this.left = this.left + mousePosition.x - this.lastMousePosition.x;
                this.top = this.top + mousePosition.y - this.lastMousePosition.y;
                this.lastMousePosition = mousePosition;
                this.setPosition();
            }
        }
        else{
            this.window.tc.addListener("mousedown", function(e){
                this.dom.dom.style.zIndex = Ext.getMaxZindex();
            }, this, {stopPropagation: true, preventDefault: true, stopEvent: true});
        }
        if(this.center){
            if(this.targetObj==Ext.getBody()){
                var me = this;
                Ext.Event.addListener(window, "resize", function(){
                    me.setToCenter();
                });
            }
            else{
                this.targetObj.addListener("resize", function(){
                    this.setToCenter();
                }, this);
            }
        }
    },
    
    render     : function(renderTo){
    	Window.superclass.render.call(this, renderTo);
    },
    
    doLayout   : function(){
        Window.superclass.doLayout.call(this);
        this.window.hiddenTable.setDisplayed(false);
        
        this.setSize();
        this.setTitle();
        this.dom.dom.style.zIndex = Ext.getMaxZindex();
        
        this.window.tl.addClass("xt-window-tl");
        this.window.tc.addClass("xt-window-tc");
        this.window.tr.addClass("xt-window-tr");
        
        this.window.cl.addClass("xt-window-cl");
        this.window.cc.addClass("xt-window-cc");
        this.window.cr.addClass("xt-window-cr");
        
        this.window.bl.addClass("xt-window-bl");
        this.window.bc.addClass("xt-window-bc");
        this.window.br.addClass("xt-window-br");
        
        this.dom.addClass("xt-window");
        this.window.title.addClass("xt-window-title");
        this.window.close.addClass("xt-window-close-out");
        
        this.setHtml();
        this.setTitle();
        if(!this.center) this.setPosition();
        if(this.targetObj==Ext.getBody()){
        	this.dom.setStyle("position", "fixed");
        }
    },
    
    setWidth    : function(width){
        this.width = (width != null && /(d)*/.test(width) ? width : this.width);
        this.window.cc.setWidth(this.width-15);
    },
    
    setHeight    : function(height){
        this.height = (height != null && /(d)*/.test(height) ? height : this.height);
        this.window.containerPanel.setHeight(this.height-30);
    },
    
    setSize     : function(width, height){
        this.setWidth(width);
        this.setHeight(height);
    },
    
    setLeft    : function(left){
        this.left = (left != null && /(d)/.test(left) ? width : this.left);
        this.dom.setLeft((this.left ? this.left : 0));
        
    },
    
    setTop    : function(top){
        this.top = (top != null && /(d)/.test(top) ? top : this.top);
        this.dom.setTop((this.top ? this.top : 0));
    },
    
    setPosition     : function(left, top){
        this.setLeft(left);
        this.setTop(top);
    },
    
    add         : function(item){
    	if(typeof(item)=="object" && typeof(item.dom)=="object"){
            this.window.containerPanel.add(item);
            if(this.items.indexOf(item)<0) this.items.push(item);
        }
    },
    
    addButton  : function(item){
        if(typeof(item)=="object" && typeof(item.dom)=="object"){
            this.window.containerPanel.addButton(item);
            if(this.buttons.indexOf(item)<0) this.buttons.push(item);
        }
    },
    
    setTitle    : function(title){
        this.title = (title != null && title ? title : this.title);
        this.window.title.update(this.title);
    },
    
    show        : function(){
        if(this.masked) this.window.mask.show();
        this.dom.dom.style.zIndex = Ext.getMaxZindex();
        Window.superclass.show.call(this);
        if(this.center) this.setToCenter();
    },
    
    hide        : function(){
        if(this.masked) this.window.mask.hide();
        Window.superclass.hide.call(this);
    },
    
    setHtml     : function(html){
        this.html = (html!=null?html:this.html);
        this.window.containerPanel.setHtml(this.html);
    },
    
    setToCenter : function(){
        if(this.center && this.targetObj){
            if(this.targetObj!=Ext.getBody()){
                var targetXY = this.targetObj.getXY();
                this.dom.setLeft(Math.round(this.targetObj.getWidth()/2 - this.dom.getWidth()/2 + targetXY[0], 0));
                this.dom.setTop(Math.round(this.targetObj.getHeight()/2 - this.dom.getHeight()/2 + targetXY[1], 0));
            }
            else{
                var targetXY = [0,0];
                var w, h;
                 
                 if (typeof window.innerWidth != 'undefined'){
                    w = window.innerWidth;
                    h = window.innerHeight
                 }
                 
                // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

                 else if (typeof document.documentElement != 'undefined'
                     && typeof document.documentElement.clientWidth !=
                     'undefined' && document.documentElement.clientWidth != 0){
                    w = document.documentElement.clientWidth;
                    h = document.documentElement.clientHeight
                 }
                 
                 // older versions of IE
                 
                 else{
                    w = document.getElementsByTagName('body')[0].clientWidth,
                    h = document.getElementsByTagName('body')[0].clientHeight
                 }
                this.dom.setLeft(Math.round(w/2 - this.dom.getWidth()/2 + targetXY[0], 0));
                this.dom.setTop(Math.round(h/2 - this.dom.getHeight()/2 + targetXY[1], 0));
            }
        }
    }
});


AccordionField = Ext.extend(Visual, {
    xType       : 'accordionField',
    parent      : null,
    img         : null,
    selected    : false,
    text        : '',
    width       : '',
    
    constructor     : function(config){
        Ext.apply(this, config);
        AccordionField.superclass.constructor.call(this, config);
        this.initEvents();
        return this;
    },
    
    createElements  : function(){
        this.childList = new Array();
        AccordionField.superclass.createElements.call(this, arguments);
        if(this.dom!=null){
            
            this.accordionFiled = new Array();
            this.accordionFiled.fieldParent = Ext.get(this.dom.insertHtml("beforeEnd", "<div></div>"));
            this.accordionFiled.imgContainer = Ext.get(this.accordionFiled.fieldParent.insertHtml("beforeEnd", "<div><img src='#'></div>"));
            this.accordionFiled.textContainer = Ext.get(this.accordionFiled.fieldParent.insertHtml("beforeEnd", "<div align='center'><span></span></div>"));
            this.accordionFiled.img = Ext.get(this.accordionFiled.imgContainer.dom.firstChild);
            this.accordionFiled.text = Ext.get(this.accordionFiled.textContainer.dom.firstChild);
        }
    },
    
    initEvents      : function(){
        var scope = this;
        this.accordionFiled.fieldParent.addListener("click", function(e){
            this.select(false);
        }, this, {stopPropagation: true});
        
        this.accordionFiled.fieldParent.addListener("mouseover", function(e){
            if(!this.selected){
                this.accordionFiled.fieldParent.addClass("xt-" + this.xType + "-over");
            }
        }, this, {stopPropagation: true});
        
        this.accordionFiled.fieldParent.addListener("mouseout", function(e){
            if(!this.selected){
                this.accordionFiled.fieldParent.removeClass("xt-" + this.xType + "-over");
            }
        }, this, {stopPropagation: true});
        
        this.applyEvents(this.accordionFiled.fieldParent.dom, ["dblclick", "mouseover", "mouseout"]);
    },
    
    select          : function(disableClickEvent){
        this.accordionFiled.fieldParent.addClass("xt-" + this.xType + "-selected");
        this.accordionFiled.fieldParent.removeClass("xt-" + this.xType + "-deselected");
        this.accordionFiled.fieldParent.removeClass("xt-" + this.xType + "-over");
        this.selected = true;
        if(typeof(this.events.select)!="undefined"){
            this.events.select.fire(this);
        }
        if(!disableClickEvent){
            if(typeof(this.events.click)!="undefined"){
                this.events.click.fire(this);
            }
        }
    },
    
    deselect          : function(){
        this.accordionFiled.fieldParent.removeClass("xt-" + this.xType + "-selected");
        this.accordionFiled.fieldParent.addClass("xt-" + this.xType + "-deselected");
        if(typeof(this.events.deselect)!="undefined"){
            this.events.deselect.fire(this);
        }
        this.selected = false;
    },
    
    doLayout    : function(){
        AccordionField.superclass.doLayout.call(this, arguments);
        this.accordionFiled.text.dom.innerHTML = "";
        Ext.disableSelection(this.dom.dom);
        Ext.disableContextMenu(this.dom.dom, function(e){});
        this.dom.addClass("xt-" + this.xType);
        
        this.accordionFiled.img.addClass("xt-" + this.xType + "-img");
        this.accordionFiled.text.addClass("xt-" + this.xType + "-text");
        this.accordionFiled.imgContainer.addClass("xt-" + this.xType + "-img-container");
        this.accordionFiled.textContainer.addClass("xt-" + this.xType + "-text-container");
        
        if(this.selected){
            this.select();
        }
        else{
            this.deselect();
        }
        this.setImg();
        this.setText();
        
    },
    
    setImg      : function(imgSrc){
        this.img = (imgSrc!=null ? imgSrc : this.img);
        if(typeof(this.accordionFiled.img)=="object"){
            this.accordionFiled.img.dom.src = this.img;
        }
    },
    
    setText      : function(text){
        this.text = (text!=null ? text : this.text);
        this.accordionFiled.text.dom.innerHTML = this.text;
    }
});


AccordionBar = Ext.extend(Visual, {
    xType       : 'accordionBar',
    parent      : null,
    items       : null,
    selected    : false,
    selectedItem    : null,
    text        : '',
    width       : '',
    item        : null,
    
    constructor     : function(config){
        this.items = [];
        Ext.apply(this, config);
        AccordionBar.superclass.constructor.call(this, config);
        this.initEvents();
        return this;
    },
    
    createElements  : function(){
        AccordionBar.superclass.createElements.call(this, arguments);
        if(this.dom!=null){
            
            this.accordionBar = new Array();
            this.accordionBar.fieldParent = Ext.get(this.dom.insertHtml("beforeEnd", "<div></div>"));
            this.accordionBar.textContainer = Ext.get(this.accordionBar.fieldParent.insertHtml("beforeEnd", "<div></div>"));
            this.accordionBar.itemContainer = Ext.get(this.accordionBar.fieldParent.insertHtml("beforeEnd", "<div></div>"));
        }
    },
    
    render      : function(renderTo){
        AccordionBar.superclass.render.call(this, renderTo);
        if(this.items!=null && typeof(this.items.length)!="undefined"){
            var it = this.items.iterator();
            while(it.hasNext()){
                this.addItem(it.next());
            }
        }
        this.selectItem(this.item);
    },
    
    initEvents      : function(){
        this.accordionBar.fieldParent.addListener("click", function(e){
            if(!this.selected)
                this.select();
            else
                this.deselect();
        }, this, {stopPropagation: true});
        
        this.accordionBar.fieldParent.addListener("mouseover", function(){
            if(!this.selected){
                this.accordionBar.textContainer.addClass("xt-" + this.xType + "-over");
                this.accordionBar.textContainer.removeClass("xt-" + this.xType + "-deselected");
            }
        }, this);
        
        this.accordionBar.fieldParent.addListener("mouseout", function(){
            if(!this.selected){
                this.accordionBar.textContainer.addClass("xt-" + this.xType + "-deselected");
                this.accordionBar.textContainer.removeClass("xt-" + this.xType + "-over");
            }
        }, this);
        
        this.applyEvents(this.accordionBar.fieldParent.dom, ["click", "dblclick", "mouseover", "mouseout"]);
    },
    
    selectItem      : function(idx){
        if(idx!=null && this.items.length>idx){
            this.items[idx].select();
        }
    },
    
    select          : function(fast){
        this.accordionBar.textContainer.addClass("xt-" + this.xType + "-selected");
        this.accordionBar.textContainer.removeClass("xt-" + this.xType + "-deselected");
        this.accordionBar.textContainer.removeClass("xt-" + this.xType + "-over");
        if(this.accordionBar.itemContainer.shown==false || !this.accordionBar.itemContainer.shown){
            if(!fast){
                this.accordionBar.itemContainer.slideIn("t", {
                    easing: 'easeOut',
                    duration: .3,
                    useDisplay: "block"
                });
            }
            else
                this.accordionBar.itemContainer.setDisabled(true);
            this.accordionBar.itemContainer.shown=true;
        }
                
        this.selected = true;
        if(typeof(this.events.select)!="undefined"){
            this.events.select.fire(this);
        }
    },
    
    deselect          : function(fast){
        this.accordionBar.textContainer.addClass("xt-" + this.xType + "-deselected");
        this.accordionBar.textContainer.removeClass("xt-" + this.xType + "-selected");
        this.accordionBar.textContainer.removeClass("xt-" + this.xType + "-over");
        
        if(this.accordionBar.itemContainer.shown==true || !this.accordionBar.itemContainer.shown){
            if(!fast){
                this.accordionBar.itemContainer.slideOut("t", {
                    easing: 'easeOut',
                    duration: .3,
                    useDisplay: "none"
                });
            }
            else
                this.accordionBar.itemContainer.setDisplayed(false);
            this.accordionBar.itemContainer.shown=false;
        }
        this.selected = false;
		var it = this.items.iterator();
		while(it.hasNext()){
			it.next().deselect();
		}
        if(typeof(this.events.deselect)!="undefined"){
            this.events.deselect.fire(this);
        }
    },
    
    deselectItems       : function(){
        var it=this.items.iterator();
        while(it.hasNext()){
            it.next().deselect();
        }
    },
    
    doLayout    : function(){
        AccordionBar.superclass.doLayout.call(this, arguments);
            
        this.accordionBar.textContainer.dom.innerHTML = "";
        Ext.disableSelection(this.dom.dom);
        Ext.disableContextMenu(this.dom.dom, function(e){});
        this.dom.addClass("xt-" + this.xType);
        this.accordionBar.fieldParent.addClass("xt-" + this.xType);
        
        if(this.selected){
            this.select(true);
        }
        else{
            this.deselect(true);
        }
        this.setText();
    },
    
    setText      : function(text){
        this.text = (text!=null ? text : this.text);
        this.accordionBar.textContainer.dom.innerHTML = this.text;
    },
    
    addItem    : function(item){
        if(typeof(item)=="object" && typeof(item.dom)=="object" && item.xType=="accordionField"){
            item.render(this.accordionBar.itemContainer.dom);
            item.parent = this;
            if(item.selected == true && this.selected==false){
                this.selectedItem = item;
                this.select();
            }
            item.addListener("select", function(el){
                this.selectedItem = el;
                var it = this.items.iterator();
                while(it.hasNext()){
                    var ch = it.next();
                    if(ch!=el){
                        ch.deselect();
                    }
                }
                if(typeof(this.events.selectItem)!="undefined"){
                    this.events.selectItem.fire(this);
                }
            }, this);
			
			if(this.items.indexOf(item)<0)
				this.items[this.items.length] = item;
        }
    },
    
    removeItems    : function(){
        while(this.items.length>0){
            this.items[0].dom.remove();
            this.items.remove(this.items[0]);
        }
    },
    
    removeItem    : function(item){
        if(isNaN(item/1) && typeof(item)=="object" && typeof(item.dom)=="object"){
            var it=this.items.iterator();
			for(i=0; i<this.items.length; i++){
                if(this.items[i]==item){
                    this.items[i].dom.remove();
                    this.items.remove(this.items[i]);
                }
            }
        }
        else{
            if(item<this.items.length){
                this.items[item].dom.remove();
                this.items.remove(this.items[item]);
            }
        }
    }
});

Accordion = Ext.extend(Visual, {
    xType       : 'accordion',
    items       : null,
    rendered    : null,
    selectedItem    : null,
    selectedBar     : null,
    width       : '',
    selectBar   : 0,
    
    
    constructor     : function(config){
        this.items = [];
        Ext.apply(this, config);
        Accordion.superclass.constructor.call(this, config);
        return this;
    },
    
    createElements  : function(){
        Accordion.superclass.createElements.call(this, arguments);
        if(this.dom!=null){
            Ext.disableContextMenu(this.dom, function(e){});
        }      
    },
    
    render      : function(renderTo){
        Accordion.superclass.render.call(this, renderTo);
        if(this.items!=null && typeof(this.items.length)!="undefined"){
            var it = this.items.iterator();
            while(it.hasNext()){
                this.addItem(it.next());
            }
        }
        
        if(!this.selectedBar)
            this.select();
    },
    
    doLayout    : function(){
        Accordion.superclass.doLayout.call(this, arguments);
        Ext.disableSelection(this.dom.dom);
        this.dom.addClass("xt-" + this.xType);
        this.dom.dom.style.height = "100%";
    },
    
    select      : function(idx){
        this.selectBar = (idx!=null ? idx : this.selectBar);
        if(this.items.length>this.selectBar){
            this.items[this.selectBar].select();
        }
    },
    
    addItem    : function(item){
        if(typeof(item)=="object" && typeof(item.dom)=="object" && item.xType=="accordionBar"){
            item.render(this.dom);
            item.parent = this;
            
            var it = this.items.iterator();
            while(it.hasNext()){
                var ch = it.next();
                if(ch.selected){
                    this.selectedItem = ch;
                }
            }
            
            if(item.selected)
                this.selectedBar = item;
            item.addListener("select", function(el){
                this.selectedBar = item;
                
                var it = this.items.iterator();
                while(it.hasNext()){
                    var ch = it.next();
                    if(ch!=el){
                        ch.deselect();
                    }
                }
                
                if(this.selectedItem==el.selectedItem && el.selectedItem!=null){
                    el.selectedItem.select(true);
                }
            }, this);
            
            item.addListener("selectItem", function(el){
                this.selectedItem = el.selectedItem;
                var it = this.items.iterator();
                while(it.hasNext()){
                    var curEl = it.next();
                    if(curEl!=el){
                        curEl.deselectItems();
                    }
                }
            }, this);
			
			if(this.items.indexOf(item)<0)
				this.items[this.items.length] = item;
        }
    },
    
    removeItems    : function(){
        while(this.items.length>0){
            this.items[0].dom.remove();
            this.items.remove(this.items[0]);
        }
    },
    
    removeItem    : function(item){
        if(isNaN(item/1) && typeof(item)=="object" && typeof(item.dom)=="object"){
            for(i=0; i<this.items.length; i++){
                if(this.items[i]==item){
                    this.items[i].dom.remove();
                    this.items.remove(this.items[i]);
                }
            }
        }
        else{
            if(item<this.items.length){
                this.items[item].dom.remove();
                this.items.remove(this.items[item]);
            }
        }
    }
});

/** *************************************************************** */
/** *************************************************************** */
/** *************************************************************** */
/** *************************************************************** */
/** *************************************************************** */
/** *************************************************************** */
/** *************************************************************** */

Button = Ext.extend(Visual, {
    objectLeft      : null,
    objectCenter    : null, 
    objectRight     : null, 
    color           : null,
    activColor      : null,
    passivColor     : null,
    request         : false,
    requestCallBack : null,
    renderTo    : null,
    src         : null,
    
    manageColor     : function(color){
        if(color!=null)
        this.color=color;
        
        if(this.color==null || this.color=='green'){
            this.activColor = 'blue';
            this.passivColor = this.color;
        }
        else{
            this.activColor = 'green';
            this.passivColor = this.color;
        }
    },
    
    initEventes     : function(){
        var scope = this;
        this.objectLeft.onmouseover =  function() {scope.setActiv();};
        this.objectLeft.onmouseout =  function() {scope.setInactiv();};
        if(typeof(this.click)!="undefined" && this.click!=null)
            this.objectLeft.onclick= function(){scope.click();};
    },
    
    setInactiv      : function(){
        this.objectLeft.className = this.passivColor + "-button-left-activ";
        this.objectCenter.className = this.passivColor + "-button-center-activ bButton";
        this.objectRight.className = this.passivColor + "-button-right-activ";
    },
    
    setActiv        : function(){
        this.objectLeft.className = this.activColor + "-button-left-activ";
        this.objectCenter.className = this.activColor + "-button-center-activ bButton";
        this.objectRight.className = this.activColor + "-button-right-activ";
    },
    show        : function(){
        this.dom.setDisplayed(true);
    },
    
    constructor     : function(config){
        Ext.apply(this, config);
        Button.superclass.constructor.call(this, config);
        
        if(this.color==null || this.color=="")
            this.color="green";
            
        this.render();
        this.manageColor();
        this.initEventes();
        //this.eventHandler(this.events, this.dom);
        return this;
    },
    render      : function(renderTo){
    	Button.superclass.render.call(this, renderTo);
        //Button.superclass.render.call(this, arguments);
        
        if(this.dom!=null){
            if(this.src==null){
                this.objectLeft = document.createElement("div");
                this.objectLeft.setAttribute("id", Ext.id());
                this.dom.appendChild(this.objectLeft);
            }
            else{
                this.link = document.createElement("a");
                this.link.setAttribute("id", Ext.id());
                this.dom.appendChild(this.link);
                this.link.href = this.src;
                
                this.objectLeft = document.createElement("div");
                this.objectLeft.setAttribute("id", Ext.id());
                this.link.appendChild(this.objectLeft);
            }
            
            this.objectRight = document.createElement("div");
            this.objectRight.setAttribute("id", Ext.id());
            this.objectLeft.appendChild(this.objectRight);
            
            this.objectCenter = document.createElement("span");
            this.objectCenter.setAttribute("id", Ext.id());
            this.objectRight.appendChild(this.objectCenter);
            
            this.objectText = document.createElement("div");
            this.objectText.setAttribute("id", Ext.id());
            this.objectCenter.appendChild(this.objectText);
            
            this.doLayout();
        }
    },
    
    doLayout    : function(){
        Button.superclass.doLayout.call(this.arguments);
        this.objectText.style.paddingTop = "3px";
        this.objectText.innerHTML = this.htmlContent;
        
        this.objectLeft.className = this.color + "-button-left-activ";
        this.objectRight.className = this.color + "-button-right-activ";
        this.objectCenter.className = this.color + "-button-center-activ " + this.baseClass;
        if(this.src!=null){
            this.link.className = this.color + "-button-center-link";
        }
        
        Ext.disableSelection(this.dom);
        this.show();
    }
});

AutoloadItem = Ext.extend(Visual, {
    text: "",
    html: "",
    value: "",
    xType: "autoload-item",
    
    constructor: function(config){
        Ext.apply(this, config);
        AutoloadItem.superclass.constructor.call(this, config);
        this.initEvents();
        return this;
    },
    
    doLayout: function(){
        AutoloadItem.superclass.doLayout.call(this);
        this.setText();
    },
    
    initEvents: function(){
        this.applyEvents(this.dom, ["mouseover", "mouseout", "click"]);
        this.dom.addListener("mouseenter", function(){
            this.select();
        }, this);
        this.dom.addListener("mouseout", function(){
            this.deselect();
        }, this);
    },
    
    setText: function(text){
        this.text = (text ? text : this.text);
        this.dom.update(this.html + this.text);
    },
    
    getText: function(){return this.text;},
    
    setValue: function(value){
    	this.value = (value ? value : this.value);
    },
    
    remove: function(){
        this.dom.remove();
        this.dom.removeAllListeners();
    },
    
    select: function(){
        this.dom.addClass("xt-autoload-item-over");
        this.dom.scrollIntoView(this.dom.dom.parentElement, false);
    },
    
    deselect: function(){
        this.dom.removeClass("xt-autoload-item-over");
    }
});

AutoloadCombo = Ext.extend(Field, {
    xType: "autoload-combo",
    value: "",
    jsonPath: null,
    items: null,
    popupHeight: 50,
    itemCls: null,
    maxNrItems: 20,
    searchType: null,
    
    selectedItem: null,
    selectedIdx: null,
    isPopupHidden: null,
    oldValue: "",
    ajaxLoad: true,
    visibleItems: 0,
    visibleMap: null,
    
    constructor     : function(config){
        Ext.apply(this, config);
        AutoloadCombo.superclass.constructor.call(this, config);
        this.items = [];
        this.selectedItem = null;
        this.selectedIdx = -1;
        this.initEvents();
        return this;
    },
    
    createElements  : function(){
        AutoloadCombo.superclass.createElements.call(this);
        this.autoloadCombo = [];
        
        this.autoloadCombo.popup = this.dom.insertHtml("beforeEnd", "<div></div>", true);
        this.autoloadCombo.popup.setStyle("position", "absolute");
        this.autoloadCombo.ajax = new Ajax({asyncronous: true, method: "POST"});
        
        this.autoloadCombo.value = this.dom.insertHtml("beforeEnd", "<input type='hidden' name='" + this.name + "VALUE'/>", true);
        this.field.input.set({"autocomplete": "off"});
    },
    
    doLayout: function(){
        AutoloadCombo.superclass.doLayout.call(this);
        
        this.setJsonPath();
        this.setItemCls();
    },
    
    initEvents: function(){
        this.addListener("keypress", function(e){
            if(e.keyCode!=e.ENTER && e.keyCode!=e.ESCAPE){
                var value = this.getValue() + String.fromCharCode(e.keyCode);
                this.ajaxLoad = (this.ajaxLoad || this.oldValue.length==0 || this.oldValue.length>value.length ? true : false);
                this.oldValue = value;
                
            }
        });
        
        this.field.input.addListener("keyup", function(e){
            if(e.keyCode == e.BACKSPACE && this.field.input.getValue().length==0){
                this.hidePopup();
                this.ajaxLoad = true;
                return;
            }
            else{
                if(e.keyCode != e.UP && e.keyCode != e.DOWN && e.keyCode!=e.ENTER && e.keyCode!=e.ESC){
                    this.resetContent(this.field.input.getValue());
                    return;
                }
            }
        }, this);
        
        this.field.input.addListener("keydown", function(e){
            if(e.keyCode==e.ENTER && (!this.isPopupHidden || !this.selectedItem)){
                this.setItem(this.selectedItem);
                e.preventDefault();
                e.stopEvent();
                e.stopPropagation();
                return;
            }
            
            if(e.keyCode == e.ESC){
                this.hidePopup();
                e.preventDefault();
                e.stopEvent();
                e.stopPropagation();
                this.ajaxLoad = true;
                this.field.input.set({value:""});
                this.resetContent("");
                return;
            }
            
            if(e.keyCode == e.UP || e.keyCode == e.DOWN){
                if(this.isPopupHidden==null || this.isPopupHidden==true)
                    this.showPopup();
                if(e.keyCode == e.DOWN){
                    this.selectNext();
                }
                else if(e.keyCode == e.UP){
                    this.selectPrevious();
                }
            }
        }, this);
        
        this.field.input.addListener("change", function(e){
        	if(!this.selectedIdx || this.selectedIdx==-1){
        		if(this.items.length>0){
        			this.setItem(this.items[0]);
        		}
        	}
        }, this);
        
        this.dom.addListener("resize", function(e){
            this.setPopupLayout();
        }, this);
        
        this.dom.addListener("scroll", function(e){
            this.setPopupLayout();
        }, this);
        
        Ext.getBody().addListener("click", function(){
            this.hidePopup();
        }, this);
    },
    
    addItem: function(item){
        if(item && item.xType=="autoload-item" && this.items.length<this.maxNrItems){
            this.items.push(item);
            item.render(this.autoloadCombo.popup);
            
            item.addListener("click", function(el){
                this.setItem(el);
            }, this);
            
            item.addListener("mouseover", function(el){
                this.selectItem(el);
            }, this);
        }
    },
    
    setItem: function(item){
        if(this.items.indexOf(item)>-1){
            this.field.input.dom.value = item.text;
            this.autoloadCombo.value.dom.value = item.value;
            this.hidePopup();
        }
    },
    
    selectNext: function(){
        var idx = this.items.indexOf(this.selectedItem);
        idx++;
        while(idx<this.items.length){
            if(this.items[idx].shown==true){
                break;
            }
            idx++;
        }
        this.selectItem(this.items[idx]);
    },
    
    selectPrevious: function(){
        var idx = this.items.indexOf(this.selectedItem);
        idx--;
        while(idx>=0){
            if(this.items[idx].shown==true){
                break;
            }
            idx++;
        }
        this.selectItem(this.items[idx]);
    },
    
    selectItem: function(item){
        if(this.items.indexOf(item)>-1){
            if(this.selectedItem)
                this.selectedItem.deselect();
            
            this.selectedItem = item;
            this.selectedIdx = this.items.indexOf(item);
            item.select();
        }
    },
    
    clearPopup: function(){
        while(this.items.length>0){
            this.items[0].remove();
            this.items.remove(this.items[0]);
        }
    },
    
    resetContent: function(value){
        if(/((ftp|http|https):\/\/)?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(this.url)){
        	this.filtrateItems(value);
        	this.autoloadCombo.ajax.url = this.jsonPath;
            this.autoloadCombo.method = "POST";
            this.autoloadCombo.ajax.parameterList = [];
            
            if(this.searchType){
                this.autoloadCombo.ajax.parameterList.push("type=" + this.searchType);
            }
            this.autoloadCombo.ajax.parameterList.push("par1=" + value);
            if((this.ajaxLoad || this.visibleItems<5 || this.itemMatch(value)<3) && value.length>=3){
                this.clearPopup();
                var me = this;
                this.autoloadCombo.ajax.doRequest(function(xmlHttp){
                    var itemsList = [];
                    try{
                        itemsList = eval(xmlHttp.responseText);
                    }catch(e){
                        alert(xmlHttp.responseText);
                    }

                    if(!itemsList || itemsList.length==0){
                        me.hidePopup();
                        return;
                    }
                    var it = itemsList.iterator();
                    while(it.hasNext()){
                        tmp = it.next();
                        me.addItem(new AutoloadItem({
                            text: tmp.text,
                            html: tmp.html,
                            value: tmp.value,
                            cls: me.itemCls,
                            hidden: true
                        }));
                    }
                    if(me.isPopupHidden==null || me.isPopupHidden==true)
                        me.showPopup();
                    me.ajaxLoad = false;
                    me.filtrateItems(me.getValue());
                });
            }
            else{
                this.filtrateItems(value);
            }
        }
    },
    
    filtrateItems: function(value){
        value = (value ? value : "");
        var it = this.items.iterator();
        value = value.toLowerCase();
        this.visibleItems = 0;
        this.visibleMap = {};
        var count = 0;
        var regEx = new RegExp("^(" + Ext.addslashes(value) + ")", "i");
        while(it.hasNext()){
            var item = it.next();
            if(item.getText().toLowerCase().indexOf(value)==0){
                item.dom.update(item.html + item.getText().replace(regEx, "<b>$1</b>"));
                item.show();
                this.visibleMap[this.visibleItems] = count;
                this.visibleItems++;
            }
            else
                item.hide();
            count++;
        }
    },
    
    itemMatch: function(value){
        value = (value ? value : "");
        var it = this.items.iterator();
        value = value.toLowerCase();
        this.visibleItems = 0;
        this.visibleMap = {};
        var count = 0;
        var regEx = new RegExp("^(" + Ext.addslashes(value) + ")", "i");
        while(it.hasNext()){
            var item = it.next();
            if(item.getText().toLowerCase().indexOf(value)==0){
            	count++;
            }
        }
        return count;
    },
    
    setJsonPath: function(jsonPath){
        this.jsonPath = (jsonPath && /((ftp|http|https):\/\/)?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(jsonPath) ? jsonPath : this.jsonPath);
        this.autoloadCombo.ajax.url = this.jsonPath;
    },
    
    setPopupLayout: function(){
        this.autoloadCombo.popup.addClass("xt-autoload-combo-popup");
        this.autoloadCombo.popup.setWidth(this.field.input.getWidth());
        this.autoloadCombo.popup.dom.style.maxHeight = this.popupHeight + "px";
        this.autoloadCombo.popup.setLeft(this.field.input.getXY()[0]);
        this.autoloadCombo.popup.setTop(this.dom.getXY()[1] + this.field.input.getHeight());
    },
    
    showPopup: function(){
        this.setPopupLayout();
        this.autoloadCombo.popup.setDisplayed(true);
        this.isPopupHidden = false;
    },
    
    hidePopup: function(){
        this.autoloadCombo.popup.setDisplayed(false);
        this.isPopupHidden = true;
    },
    
    setItemCls: function(cls){
        this.itemCls = (cls ? cls : this.itemCls);
    }
});

SimplePopup = Ext.extend(Visual, {
    html: null,
    xType: "simple-popup",
    doLayout   : function(){
        SimplePopup.superclass.doLayout.call(this);
        this.dom.setStyle("position", "absolute");
        this.dom.addClass("xt-" + this.xType);
        this.setHtml();
        this.dom.setDisplayed(!this.hidden);
            
    },
    setHtml: function(html){
        this.html = (html ?html : this.html);
        if(this.html) this.dom.update(this.html);
    },
    show: function(from){
        if(!this.hidden) return;
        this.dom.setStyle("zIndex", Ext.getMaxZindex());
        this.dom.slideIn((from ? from : 't'), {
        	easing: 'easeOut',
            duration: .3,
            useDisplay: "block"
        });
        this.hidden=false;
    },
    hide: function(){
        if(this.hidden) return;
        this.dom.setStyle("position", "absolute");
        this.dom.fadeOut({
        	easing: 'easeOut',
			duration: .5,
			afterStyle: {
			    position: "absolute"
			}
        });
        this.hidden=true;
    }
});

ExtPoint = Ext.extend(Object, {
    x: 0,
    y: 0,
    xType: "xt-point",
    
    constructor     : function(config){
        Ext.apply(this, config);
        ExtPoint.superclass.constructor.call(this, config);
        return this;
    }
});

ExtImage = Ext.extend(Object, {
    path: null,
    width: 0,
    height: 0,
    anchorPoint: null,
    xType: "xt-image",
    gImage: null,
    
    constructor     : function(config){
        Ext.apply(this, config);
        ExtImage.superclass.constructor.call(this, config);
        
        if(!this.path) return;
        
        this.gImage = new google.maps.MarkerImage(
            this.path,
            new google.maps.Size(this.width, this.height),
            (this.anchorPoint && this.anchorPoint.xType=="xt-point" ? new google.maps.Point(this.anchorPoint.x,this.anchorPoint.y) : new google.maps.Point(0,0)),
            new google.maps.Point(0, this.height)
        );
        return this;
    },
    
    getImage: function(){
        return this.gImage;
    }
});

ExtMarker = Ext.extend(Base, {
    lat: 0,
    long: 0,
    image: null,
    shadow: null,
    title: null,
    created: false,
    gMarker: null,
    map: null,
    xType: "xt-marker",
    zIndex: null,
    shape: null,
    infoContent: null,
    infoWindow: null,
    selected: false,
    
    constructor     : function(config){
        Ext.apply(this, config);
        this.shape = (this.shape ? this.shape : []);
        ExtMarker.superclass.constructor.call(this, config);
        return this;
    },
    
    createMarker: function(map){
        if(!map || this.created || this.gMarker) return
        
        if(!this.lat || !this.long) return;
        
        this.map = map;
        
        var shape = [];
        var it = this.shape.iterator();
        while(it.hasNext()){
            var sh = it.next();
            if(sh && sh.xType && sh.xType=="xt-point"){
                shape.push(sh.x);
                shape.push(sh.y);
            }
        }
        
        var image = (this.image && this.image.xType && this.image.xType=="xt-image" ? this.image.getImage() : null);
        var shadow = (this.shadow && this.shadow.xType && this.shadow.xType=="xt-image" ? this.shadow.getImage() : null);
        
        var myLatLng = new google.maps.LatLng(this.lat, this.long);
        this.gMarker = new google.maps.Marker({
            position: myLatLng,
            icon: image,
            shadow: shadow,
            title: this.title,
            map: this.map
        });
        
        var me = this;
        google.maps.event.addListener(this.gMarker, 'click', function() {
        	me.select();
    		
    		if(me.events.click)
    			me.events.click.fire(me);
	    });
        
        if(this.infoWindow==null && this.infoContent){
        	this.infoWindow = new google.maps.InfoWindow({
	            content: this.infoContent
	        });
        }
        
        if(this.selected)
        	this.select();
    },
    
    select: function(){
    	if(this.infoWindow!=null){
    		this.infoWindow.open(this.map, this.gMarker);
    		if(this.events.showinfo)
    			this.events.showinfo.fire(this);
    	}
    	this.selected = true;
    },
    
    closeInfo: function(){
    	if(this.infoWindow==null) return;
    	this.infoWindow.close();
    	this.selected = false;
    }
});

ExtMap = Ext.extend(Visual, {
    map: null,
    centerLat: 0,
    centerLong: 0,
    zoom: 10,
    markers: null,
    latlng: null,
    
    constructor     : function(config){
        Ext.apply(this, config);
        this.markers = [];
        ExtMap.superclass.constructor.call(this, config);
        this.createMap.defer(100, this);
        return this;
    },
    
    render: function(renderTo){
        ExtMap.superclass.render.call(this, renderTo);
    },
    
    createMap: function(){
    	this.latlng = new google.maps.LatLng(this.centerLat, this.centerLong);
        
        var options = {
            zoom: this.zoom,
            center: this.latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            scrollwheel: false
        };
        this.map = new google.maps.Map(this.dom.dom, options);
        var it = this.markers.iterator();
        while(it.hasNext()){
            this.addMarker(it.next());
        }
    },
    
    doLayout: function(){
        ExtMap.superclass.doLayout.call(this);
    },
    
    addMarker: function(marker){
        if(!marker || !marker.xType || marker.xType!="xt-marker") return;
        if(this.markers.indexOf(marker)<0)
            this.markers.push(marker);
        marker.createMarker(this.map);
        marker.addListener("showinfo", function(obj){
        	var it = this.markers.iterator();
        	while(it.hasNext()){
        		var m = it.next();
        		if(m==obj) continue;
        		
        		m.closeInfo();
        	}
        }, this);
    },
    
    resetCenter: function(){
    	if(!this.latlng) return;
    	this.map.setCenter(this.latlng);
    }
});

ImageViewerElement = Ext.extend(Object, {
    buttonImage :null,
    targetImage: null,
    baseId: null,
    idx: null,
    buttonParent: null,
    
    constructor     : function(config){
        Ext.apply(this, config);
        ImageViewer.superclass.constructor.call(this, config);
        return this;
    }
});

ImageViewer = Ext.extend(Object, {
    elements: null,
    elementsList: null,
    currentElement: null,
    selectedCls: 'xt-selected',
    deselectedCls: 'xt-deselected',
    canChange: true,
    canSlide: true,
    sliding: false,
    timerObj: false,
    timerValue: 3000,
    
    constructor     : function(config){
        Ext.apply(this, config);
        ImageViewer.superclass.constructor.call(this, config);
        this.elements = {};
        this.elementsList = [];
        return this;
    },
    
    addElement: function(baseId, type){
    	if(!this.elements[baseId]){
    		var initObject={};
    		initObject[type] = Ext.get(baseId + type);
    		initObject.baseId = baseId;
    		this.elements[baseId] = new ImageViewerElement(initObject);
    		this.elementsList.push(this.elements[baseId]);
    		this.elements[baseId].idx = this.elementsList.indexOf(this.elements[baseId]);
    	}
    	else{
    		this.elements[baseId][type] = Ext.get(baseId + type);
    	}
    	
    	var me = this;
    	if(type=="buttonImage"){
    		this.elements[baseId][type].addListener("click", function(){
    			me.select(this.baseId);
    		}, this.elements[baseId]);
    	}
    },
    
    selectNext: function(){
    	var idx = 0;
    	if(this.currentElement)
    		if(this.elementsList[this.currentElement.idx+1])
    			idx = this.currentElement.idx+1;
    		this.select(this.elementsList[idx].baseId);
    },
    
    selectPrevious: function(){
    	var idx = this.elementsList.length-1;
    	if(this.currentElement)
    		if(this.elementsList[this.currentElement.idx-1])
    			idx = this.currentElement.idx-1;
    		this.select(this.elementsList[idx].baseId);
    },
    
    setButtonParent: function(buttonParent){
    	this.buttonParent = Ext.get(buttonParent);
    },
    
    slide: function(){
    	if(!this.sliding){
    		var me = this;
    		this.canSlide = true;
    		this.timerObj = setInterval(function(){
    	    	if(me.canSlide){
    	    		me.sliding = true;
    	    		me.selectNext();
    	    	}
    	    	else{
    	    		me.sliding = false;
    	    		clearInterval(me.timerObj);
    	    	}
    	    }, this.timerValue);
    		return true;
    	}
    	else{
    		this.canSlide = false;
    		return false;
    	}
    },
    
    isSelected: function(baseId){
    	if(this.cucurrentElement == this.elements[baseId]) return true;
    	return false;
    },
    
    select: function(baseId){
    	if(this.canChange){
    		var changeImg = this.currentElement;
    		this.currentElement = this.elements[baseId];
	    	this.canChange = false;
	    	if(changeImg){
	    		changeImg.buttonImage.removeClass(this.selectedCls);
	    		changeImg.buttonImage.addClass(this.deselectedCls);
	    		changeImg.targetImage.fadeOut({
	    			duration: 0.1,
	    			afterStyle: "display: none",
	    			scope: this,
	    			callback: function(){
	    				this.elements[baseId].targetImage.fadeIn({
	    					duration: 0.3,
	    					scope: this,
	    					callback: function(){
	    						this.canChange = true;
	    					}
	    				});
	    			}
	    		});
	    	}
	    	else{
	    		this.elements[baseId].targetImage.fadeIn({
					duration: 0.3,
					scope: this,
					callback: function(){
						this.canChange = true;
					}
				});
	    	}
	    	
	    	this.elements[baseId].buttonImage.addClass(this.selectedCls);
			this.elements[baseId].buttonImage.removeClass(this.deselectedCls);
			
			if(this.buttonParent){
				this.elements[baseId].buttonImage.scrollIntoView(this.buttonParent, false);
			}
    	}
    }
});

BlockSlider = Ext.extend(Base, {
	btnIn: null,
	btnOut: null,
	target: null,
	expanded: null,
	
	constructor     : function(config){
		
		BlockSlider.superclass.constructor.call(this, config);
		this.btnIn = Ext.get(config.btnIn);
		this.btnOut = Ext.get(config.btnOut);
		this.target = Ext.get(config.target);
		
		
		this.btnOut.setDisplayed("inline");
		
		this.btnOut.addListener("click", function(){
			this.collapse();
		}, this);
		
		this.btnIn.addListener("click", function(){
			this.expand();
		}, this);
	    return this;
	},
	
	setHeight: function(value){
		this.target.setHeight(value, true);
	},
	
	collapse: function(){
		if(this.expanded==false) return;
		this.target.slideOut("t", {
			easing: 'easeOut',
            duration: .3,
            useDisplay: "none"
       });
       this.btnOut.fadeOut({
			easing: 'easeOut',
			duration: .2,
			useDisplay: "none",
			scope: this,
			callback: function(){
				this.btnIn.fadeIn({
					easing: 'easeOut',
					duration: .2
				});
				this.expanded = false;
				if(this.events.collapse)
					this.events.collapse.fire(this);
			}
		});
	},
	
	expand: function(){
		if(this.expanded==true) return;
		this.target.slideIn("t", {
			easing: 'easeOut',
            duration: .3,
            useDisplay: "none"
		});
		this.btnIn.fadeOut({
			easing: 'easeOut',
			duration: .2,
			scope: this,
			useDisplay: "none",
			callback: function(){
    	   		this.btnOut.fadeIn({
					easing: 'easeOut',
					duration: .2
				});
				this.expanded = true;
				if(this.events.expand)
					this.events.expand.fire(this);
       		}
       });
	}
});



// TODO: meg kell csinalni
ProgressBar = Ext.extend(Visual, {
    xType       : 'progress',
    value       : null,
    
    constructor     : function(config){
        Ext.apply(this, config);
        ProgressBar.superclass.constructor.call(this, config);
        return this;
    },
    
    createElements  : function(){
        ProgressBar.superclass.createElements.call(this);
        this.progress = new Array();
        
        this.progress.container = this.dom.insertHtml("beforeEnd", "<div><div></div></div>", true);
        this.progress.percent = Ext.get(this.progress.container.dom.firstChild);
   },
    
   doLayout   : function(){
        ProgressBar.superclass.doLayout.call(this, arguments);
        this.dom.addClass("xt-" + this.xType);
        this.progress.container.addClass("xt-" + this.xType + "-container");
        this.progress.percent.addClass("xt-" + this.xType + "-percent");
        this.setSize();
        this.setValue();
        if(this.align){
            this.dom.dom.style.textAlign = this.align;
        }
    },
    
    setValue    : function(value){
        this.value = (value != null && /(d)*/.test(value) ? value : this.value);
        this.progress.percent.setWidth(Math.round(this.value * this.width / 100, 0));
    },
    
    getValue    : function(){
        return this.value;
    },
    
    setWidth    : function(width){
        this.width = (width != null && /(d)*/.test(width) ? width : this.width);
        this.progress.container.setWidth(this.width);
    },
    
    setHeight    : function(height){
        this.height = (height != null && /(d)*/.test(height) ? height : this.height);
        this.progress.container.setHeight(this.height);
    },
    
    setSize     : function(width, height){
        this.setWidth(width);
        this.setHeight(height);
        this.setValue();
    }
});
