/*
 * Bugs:
 * 	-Un div absolute amb una imatge dins, i un element amb opacity a sobre, dintra del element amb opacity es veu lo que hi ha derrere del div. La solució es posar opacity:1 als elements que transparenten malament
 * Notes:
 *  -Anar amb compte perque a Explorer no es pot fer var s = undefined; i pot portar problemes
 *  -Si afegeixes un element a $(document.body) abans de que es carregui tot el document, a explorer donará un error
 * Require:
 * 	-Strict DocType
 * 	-Correct HTML struct
 * 	-In JavaScript not exist associative arrays!
 * 
 */
(function(){
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	
	function getIndexEasyJSVars(element){
		for(var c=0;c<window.easyJSVarsCollection.length;c++){
			if(window.easyJSVarsCollection[c][0] === element)
				return c;
		}
		
		return -1;
	}
	/*#########################################
	  ############# EasyJS OBJECT #############
	  #########################################*/	
	var EasyJS = function(ele, attr) {
		this.element = null;
		this.easyJSVars = null;
		
		// primer instanciem l'objecte, i quan torna aquí al instanciar, iniciem l'inicialitzador
		if ( !this.init )
			return new EasyJS(ele, attr);
		else
			return this.init(ele, attr);
	};
	window.EasyJS = window.$ = EasyJS; //poso tambe EasyJS per a que si faig '[var] instanceof EasyJS' doni true
	
	EasyJS.prototype = {
		init: function( ele, attr ) {
			var element = null;
			this.easyJSVars = [];
			
			if( typeof ele == "string" ){
				element = document.getElementById(ele);
				if(!element)
					throw new Error( "ID not encountred('"+ele+"')" );
			}
			else if ( typeof ele == "object" ){
				element = ele;
			}
			
			if(element && attr){
				attr.each(function(value){
					var aux = value.split(":");
					element[aux[0]] = aux[1];
				});
			}
			
			if(!window.easyJSVarsCollection) //con esto mantenemos la instancia de la variable easyJSVars si se captura un elemento que había sido ya capturado
				window.easyJSVarsCollection = [];
			
			var indexOfEasyJSVar = getIndexEasyJSVars(element);
			if(indexOfEasyJSVar != -1){
				this.easyJSVars = window.easyJSVarsCollection[indexOfEasyJSVar][1];
			}
			else{
				window.easyJSVarsCollection.push([element,this.easyJSVars]);
			}

			if( element && element.tagName ){
				switch(element.tagName.toUpperCase()){
				case "TABLE":
					this.extend( Table );
					if((!element.childNodes[0].tagName || element.childNodes[0].tagName.toUpperCase() != "TBODY") && (!element.childNodes[1].tagName || element.childNodes[1].tagName.toUpperCase() != "TBODY")){
						var childs = [];
						while(element.firstChild){
							childs.push(element.firstChild);
							element.removeChild(element.firstChild);
						}
						var tb = document.createElement("tbody");
						for(var c=0;c<childs.length;c++)
							tb.appendChild(childs[c]);
						element.appendChild(tb);
						
						this.easyJSVars["tableTBody"] = tb;
					}
					else{
						if(element.childNodes[0].tagName && element.childNodes[0].tagName.toUpperCase() == "TBODY")
							this.easyJSVars["tableTBody"] = $(element.childNodes[0]);
						else
							this.easyJSVars["tableTBody"] = $(element.childNodes[1]);
					}
					break;
				case "A":
					this.extend( A );
					break;
				case "TEXTAREA":
					this.extend( TextArea );
					break;
				case "INPUT":
					this.extend( Input );
					if(element.type == "text" || element.type == "password")
						this.extend( InputText );
					else if(element.type == "radio" || element.type == "checkbox")
						this.extend( InputRadio );
					break;
				case "SELECT":
					this.extend( Select );
					break;
				case "LABEL":
					this.extend( Label );
					break;
				case "IMG":
					this.extend( Img );
					break;
				case "FORM":
					this.extend( Form );
					break;
				case "IFRAME":
					this.extend( Iframe );
					if(!this.easyJSVars["iframeSRC"])
						this.easyJSVars["iframeSRC"] = element.src;
					if(!this.easyJSVars["iframeGETVars"])
						this.easyJSVars["iframeGETVars"] = [];
					if(!this.easyJSVars["iframeCache"])
						this.easyJSVars["iframeCache"] = false;
					break;
				}
			}
			else if(element && element.navigator){ //.navigator per saber que es tracta de un objecte tipus Window
				this.extend( EasyJSWindow );
				if(!this.easyJSVars["coverWindow"])
					this.easyJSVars["coverWindow"] = [];
				if(IE6 && !this.easyJSVars["coverResizeFunct"])
					this.easyJSVars["coverResizeFunct"] = [];
			}
			else if(element && element.nodeType && element.nodeType == 9)
				this.extend( EasyJSDocument );

			this.element = element;
			return this;
		},
		
		extend: function(object) {
		    for (var property in object)
		        this[property] = object[property];
		},
		
		addEvent: function( type, fn ) {
			var ele = this.element;
			var easyJSObject = this;
			var easyJSVars = this.easyJSVars;

			if(!easyJSVars[type]) {
				easyJSVars[type] = [];
				easyJSVars[type].push(fn);

				if(type=="load" && ele.onreadystatechange !== undefined){ /*IE*/
					ele["onreadystatechange"] = function(e){
						if(ele.readyState == "complete"){
							$event = new Event(e || window.event);
							$this = easyJSObject;

							var returnValue;
							easyJSVars[type].each(function(funct){
								returnValue = funct();
							});								
							
							return returnValue;
						}
					};
				}
				else if(ele.tagName && ele.tagName.toUpperCase() == "FORM" && type.toLowerCase()=="submit"){
					ele["onsubmit"] = function(e){
						$event = new Event(e || window.event);
						$this = easyJSObject;

						var returnValue;
						easyJSVars[type].each(function(funct){
							returnValue = funct();
						});
						//això es fa per evitar doble click al submit i enviar per tant dos cops el formulari
						if(returnValue == true){
							ele["onsubmit"] = function(){
								return false;
							};
							return true;
						}
						else
							return false;
					};
				}
				else if(type.toLowerCase()=="keypress"){ //en cas de ser keypress, si alguna funció retorna false el retorn serà false
					ele["on"+type] = function(e){
						$event = new Event(e || window.event);
						$this = easyJSObject;

						var returnValue=true;
						easyJSVars[type].each(function(funct){
							if(returnValue==true)
								returnValue = funct();
							else
								funct();
						});

						return returnValue;
					};
				}
				else{
					ele["on"+type] = function(e){
						$event = new Event(e || window.event);
						$this = easyJSObject;

						var returnValue;
						easyJSVars[type].each(function(funct){
							returnValue = funct();
						});

						return returnValue;
					};
				}
			}
			else
				easyJSVars[type].push(fn);
		},
		
		removeEvent: function( type, fn ) {
			if(!fn){
				if(!type)
					throw new Error("not defined type of event");
				if(type=="load" && this.element.onreadystatechange !== undefined) /*IE*/
					this.element["onreadystatechange"] = null;
				else
					this.element["on"+type] = null;
				this.easyJSVars[type] = null;
			}
			else {
				var easyJSVars = this.easyJSVars;
				
				easyJSVars[type].each(function(funct, index){
					if(funct == fn){
						easyJSVars[type].remove(index);
						return true;
					}
				});
				
				if(easyJSVars[type].length == 0){
					if(type=="load" && this.element.onreadystatechange !== undefined) /*IE*/
						this.element["onreadystatechange"] = null;
					else 
						this.element["on"+type] = null;
					easyJSVars[type] = null;
				}
			}
		},
		
		simulateEvent: function(event){
			if(this.element[event])
				this.element[event]();
		},
		
		isChildOf: function(parent){
			try{
				var nextParent = this.element.parentNode;
				do{
					if(nextParent==parent.element)
						return true;
					nextParent = nextParent.parentNode;
				}while(nextParent);
			}
			catch(e){
				return true;
			}
			
			return false;
		},
		
		addText: function(txt){
			this.element.appendChild(document.createTextNode(txt));
		},
		
		setText: function(txt){
			while(this.getFirstChild())
				this.getFirstChild().remove();
			this.element.appendChild(document.createTextNode(txt));
		},
		
		getStyleSheet: function(property){
			var element = this.element;
			var strValue = "";
			
			if(document.defaultView && document.defaultView.getComputedStyle){
				strValue = document.defaultView.getComputedStyle(element, "").getPropertyValue(property);
			}
			else if(element.currentStyle){
				property = property.replace(/\-(\w)/g, function (strMatch, p1){
					return p1.toUpperCase();
				});
				strValue = element.currentStyle[property];
			}
			return strValue;
		},
		
		getStyle: function(property){
			property = property.split("-");
			if(property.length>0){
				for(var c=1;c<property.length;c++)
					property[c] = property[c].capitalize();
			}
			property = property.join("");
			
			switch(property){
			case "minHeight":
				if(!IE6)
					return this.element.style.minHeight;
				else
					return this.element.style.height;
				break;
			case "minWidth":
				if(!IE6)
					return this.element.style.minWidth;
				else
					return this.element.style.width;
				break;
			case "float":
				if(this.element.style.cssFloat != undefined)
					return this.element.style.cssFloat;
				else
					return this.element.style.styleFloat;
				break;
			case "opacity":
				if(this.element.style.opacity != undefined)
					return this.element.style.opacity;
				else
					return new Number(new String(this.element.style.filter).toInt())/100;
				break;
			default:
				return this.element.style[property];
			}
		},
		
		setStyle: function(property, value){
			property = property.split("-");
			if(property.length>0){
				for(var c=1;c<property.length;c++)
					property[c] = property[c].capitalize();
			}
			property = property.join("");
			
			switch(property){
			case "minHeight":
				if(!IE6)
					this.element.style.minHeight = value;
				else
					this.element.style.height = value;
				break;
			case "minWidth":
				if(!IE6)
					this.element.style.minWidth = value;
				else
					this.element.style.width = value;
				break;
			case "float":
				if(this.element.style.cssFloat != undefined)
					this.element.style.cssFloat = value;
				else
					this.element.style.styleFloat = value;
				break;
			case "opacity":
				if(this.element.style.opacity != undefined)
					this.element.style.opacity = value;
				else {
					this.element.style.zoom = 1; //to enable filter
					this.element.style.filter = "alpha(opacity="+value*100+")";
				}
				break;
			default:
				this.element.style[property] = value;
			}
		},
		
		fadeIn: function(velocity, tope, funct) {
			this.easyJSVars["timerFadeOut"] = false;
			this.easyJSVars["timerFadeIn"] = true;
	
			tope = tope || 1;
			var $this = this;
			var opacity = this.getStyle("opacity") || this.getStyleSheet("opacity");
			if(!opacity)
				opacity = 0;
			else if(opacity==1)
				opacity = 0;
			opacity = parseFloat(opacity);
			velocity = parseFloat(velocity);
			
			$this.setStyle("opacity",opacity);
			var timer = setInterval(function() {
				if($this.easyJSVars["timerFadeIn"]){
					if(opacity+velocity < tope){
						opacity = opacity+velocity;
						$this.setStyle("opacity",opacity);
					}
					else {
						$this.setStyle("opacity",tope); 
						if(funct) funct();
						$this.easyJSVars["timerFadeIn"] = false;
						clearInterval(timer);
					}
				}
				else{
					if(funct) funct();
					clearInterval(timer);
				}
			}, 1);
		},
		
		fadeOut: function(velocity, funct) {
			this.easyJSVars["timerFadeIn"] = false;
			this.easyJSVars["timerFadeOut"] = true;
			
			var $this = this;
			var opacity = this.getStyle("opacity") || this.getStyleSheet("opacity");
			if(!opacity)
				opacity = 1;
			opacity = parseFloat(opacity);
			velocity = parseFloat(velocity);
			
			$this.setStyle("opacity",opacity);
			var timer = setInterval(function() {
				if($this.easyJSVars["timerFadeOut"]){
					if(opacity-velocity > 0){
						opacity = opacity-velocity;
						$this.setStyle("opacity",opacity);
					}
					else {
						$this.setStyle("opacity",0);
						if(funct) funct();
						$this.easyJSVars["timerFadeOut"] = false;
						clearInterval(timer);
					}
				}
				else{
					if(funct) funct();
					clearInterval(timer);
				}
			}, 1);
		},
		
		show: function(){
			if(!this.easyJSVars["showOrHide"])
				this.element.style.display = "";
			else
				this.element.style.display = this.easyJSVars["showOrHide"]; 
		},
		
		hide: function(){
			var style = this.getStyle("display") || this.getStyleSheet("display");
			
			if(style != "none"){
				this.easyJSVars["showOrHide"] = style;
				this.element.style.display = "none";
			}
		},
		
		append: function(child){
			this.element.appendChild(child.element);
		},
		
		appendBelow: function(node){
			this.element.parentNode.insertBefore(node.element, this.element.nextSibling);
		},
		
		clone: function(cloneVars){
			var clone = $(this.element.cloneNode(true));
			clone.setID("");
			if(cloneVars)
				clone.easyJSVars = this.easyJSVars.clone();
			return clone;
		},
		
		setName: function(name){
			this.element.name = name;
		},
		
		setID: function(newID){
			this.element.id = newID;
		},
		
		getID: function(){
			return this.element.id;
		},
		
		setClass: function(className){
			this.element.className = className;
		},
		
		getClass: function(){
			return this.element.className;
		},
		
		getByClass: function(className){
			var elements = [];
			var childs = this.element.getElementsByTagName("*");
			var pattern = new RegExp("(^| )" + className + "( |$)");
			
			for(var i=0; i<childs.length; i++){
				if( pattern.test( childs[i].className ) )
					elements.push( $(childs[i]) );
			}
			
			return elements;
        },
		
        getByTag: function(tag){
			var elements = [];
			var childs = this.element.getElementsByTagName(tag);
			
			for(var i=0; i<childs.length; i++){
				elements.push( $(childs[i]) );
			}
			
			return elements;
        },
        
        getFirstChild: function(){
        	if(this.element.firstChild)
        		return $(this.element.firstChild);
        	else
        		return null;
        },
        
        getParent: function(){
        	return this.element.parentNode;
        },
        
        isAppend: function(){
        	if(this.element.parentNode)
        		return true;
        	else
        		return false;
        },

        remove: function(){
        	this.element.parentNode.removeChild(this.element);
        },
        
        removeAllChilds: function(){
        	while(this.getFirstChild())
        		this.getFirstChild().remove();
        },

        getHTMLTag: function(){
        	var tag = this.element.tagName;
        	if(tag)
        		return tag.toUpperCase();
        	else
        		return false;
        },

        getValue: function(){
        	if(this.element.value != undefined)
        		return this.element.value;
        },
        
        setValue: function(value){
        	if(this.element.value != undefined)
        		this.element.value = value;
        },
        
        focus: function(){
        	this.element.focus();
        },
        
        getLengthFromLeft: function(){
        	var px = 0;
        	var ele = this.element;
        	
            while(ele){
            	px += ele.offsetLeft;
            	ele = ele.offsetParent;
            }
            
        	return px;
        },
        
        getLengthFromTop: function(){
        	var px = 0;
        	var ele = this.element;
        	
            while(ele){
            	px += ele.offsetTop;
            	ele = ele.offsetParent;
            }
            
        	return px;
        },
        
        getWidth: function(){
        	return this.element.offsetWidth;
        },
        
        getHeight: function(){
        	return this.element.offsetHeight;
        },
        
        equal: function(easyJSObject){
        	return this.element == easyJSObject.element;
        },
        
        getText: function(){
        	return this.element.textContent || this.element.innerText;
        },
        
        setHTML: function(html){
        	this.element.innerHTML = html;
        },
        
        getHTML: function(){
        	return this.element.innerHTML;
        },
        
        truncateText: function(width, height, setLabel){
        	var position = this.getStyleSheet("position");
        	var overflow = this.getStyleSheet("overflow");
        	var visibility = this.getStyleSheet("visibility");
        	var html = this.getHTML();
        	var truncated = false;
        	
        	//si no fiques postion absolute, no agafa bé el getWidth() i getHeight()
        	this.setStyle("position", "absolute");
    		this.setStyle("visibility","hidden");
    		this.setStyle("overflow","visible");
    		
    		if(height!=null){
    			this.setStyle("width",width+"px");
    			this.setStyle("height","auto");
    			
    			if(this.getHeight() > height){
    				var cont = 20;
    				this.setHTML(html.substr(0,cont));
    				this.setHTML(this.getHTML()+"&hellip;");
    				
        			while(this.getHeight() < height){
        				cont+=20;
        				this.setHTML(html.substr(0,cont));
        				this.setHTML(this.getHTML()+"&hellip;");
        			}
        			
        			while(this.getHeight() > height){
        				this.setHTML(html.substr(0,cont));
        				this.setHTML(this.getHTML()+"&hellip;");
        				cont--;
        			} 
        		
        			truncated = true;
    			}
    		}
    		else{
    			this.setStyle("width","auto");
    			height = this.getStyleSheet("height").toInt();
    			this.setStyle("height","auto");

    			if(this.getWidth() > width){
    				var cont = 1;
        			var nextChar;
        			while(this.getWidth() > width){
        				this.setHTML(html.substr(0,html.length-cont));
        				this.setHTML(this.getHTML()+"&hellip;");
        				cont++;
        			}
        			
        			truncated = true;
    			}
    		}
    		
    		this.setStyle("position",position);
    		this.setStyle("overflow", overflow);
        	this.setStyle("width", width+"px");
        	this.setStyle("height", height+"px");
        	this.setStyle("visibility",visibility);
    		
        	if(truncated && setLabel) {
        		var label = new Element("p");
        		label.setHTML(html);
        		label.setStyle("font-size","10px");
        		label.setStyle("position","absolute");
        		label.setStyle("overflow","visible");
        		label.setStyle("background-color","white");
        		label.setStyle("color","black");
        		label.setStyle("padding","5px");
        		label.setStyle("border","1px solid black");
        		label.setStyle("width",width+"px");
        		
        		this.addLabel(label, "bottom-right", 1000);
        	}
        },
        
        addLabel: function(label, position, miliseconds, width){
        	if(typeof label=="string") {
        		var txt = label;
	        	label = new Element("p");
	        	label.addText(txt);
	    		label.setStyle("font-size","10px");
	    		label.setStyle("position","absolute");
	    		label.setStyle("overflow","visible");
	    		label.setStyle("background-color","white");
	    		label.setStyle("color","black");
	    		label.setStyle("padding","5px");
	    		label.setStyle("border","1px solid black");
	    		if(width)
	    			label.setStyle("width",width+"px");
        	}
    		
        	var top;
        	var left;
    		var out = true;
    		var element = this;
    		var clientX, clientY;
    		this.addEvent("mouseover", function(){
    			//agafo els valors abans perque sino a explorer peta
    			clientX = $event.getClientX();
    			clientY = $event.getClientY();
    			var el = $event.getTarget();
    			setTimeout(function(){
    				if(out == false){
    					label.setStyle("left",clientX+$(window).getScrollLeft()+left+"px");
        				label.setStyle("top",clientY+$(window).getScrollTop()+top+"px");
    					label.setStyle("display","block");
    				}
    			},miliseconds);
    		});
    		this.addEvent("mouseout", function(){
    			if(!$event.getRelatedTarget().isChildOf(element) && !$event.getRelatedTarget().equal(element)){
    				label.setStyle("display","none");
    			}
    			out = true;
    		});
    		this.addEvent("mousemove", function(){
    			clientX = $event.getClientX();
    			clientY = $event.getClientY();
    			out = false;
				if(label.getStyle("display") == "block"){
					label.setStyle("left",$event.getClientX()+$(window).getScrollLeft()+left+"px");
    				label.setStyle("top",$event.getClientY()+$(window).getScrollTop()+top+"px");
				}
    		});
    		
    		//per a que getHeight() funcioni ha d'estar amb display a block y dntre del DOM
    		if($(window).isPageLoaded()){
    			if(label.getParent())
    				label.remove();
    			$(document.body).append(label);
    			
    			if(txt){
    				//-10 pel padding top + el padding bottom
    				label.setStyle("height",label.getHeight()-10+"px");
    			}
    			else
    				label.setStyle("display", "block");
    			
    			var labelHeight = label.getStyleSheet("height").toInt()+label.getStyleSheet("padding-top").toInt()+label.getStyleSheet("padding-bottom").toInt();
    			var labelWidth = label.getStyleSheet("width").toInt()+label.getStyleSheet("padding-left").toInt()+label.getStyleSheet("padding-right").toInt();
    			
    			
    			if(position=="top-left"){
    				top = -10-labelHeight;
            		left = -10-labelWidth;
            	}
    			else if(position=="top-right"){
    				top = -10-labelHeight;
            		left = +10;
            	}
    			else if(position=="bottom-left"){
    				top = +10;
            		left = -10-labelWidth;
            	}
    			else if(position=="bottom-right"){
    				top = +10;
            		left = +10;
            	}
    			
        		label.setStyle("display","none");
    		}
    		else {
        		$(window).addEvent("load", function(){
        			if(label.getParent())
        				label.remove();
        			$(document.body).append(label);        				
        			
        			if(txt){
        				//-10 pel padding top + el padding bottom
        				label.setStyle("height",label.getHeight()-10+"px");
        			}
        			else
        				label.setStyle("display", "block");
        			
        			var labelHeight = label.getStyleSheet("height").toInt()+label.getStyleSheet("padding-top").toInt()+label.getStyleSheet("padding-bottom").toInt();
        			var labelWidth = label.getStyleSheet("width").toInt()+label.getStyleSheet("padding-left").toInt()+label.getStyleSheet("padding-right").toInt();
        			
        			if(position=="top-left"){
        				top = -10-labelHeight;
                		left = -10-labelWidth;
                	}
        			else if(position=="top-right"){
        				top = -10-labelHeight;
                		left = +10;
                	}
        			else if(position=="bottom-left"){
        				top = +10;
                		left = -10-labelWidth;
                	}
        			else if(position=="bottom-right"){
        				top = +10;
                		left = +10;
                	}
        			
            		label.setStyle("display","none");
        		});
    		}
        }
	};
	
	var Table = {
		addRow: function(){
			var tr = new Element("tr");
			for(var i=0; i<arguments.length; i++){
				var td = new Element("td");
				
				if(typeof arguments[i] == "string")	td.addText(arguments[i]);
				else td.append(arguments[i]);
				
				tr.append(td);
			}
			this.easyJSVars["tableTBody"].append(tr);
		},
		
		removeRow: function(index){
			this.element.deleteRow(index);
		}
	};
	
	var Form = {
		submit: function(noEvent){
			var ret = true;
			if(!noEvent && this.element.onsubmit)
				ret = this.element.onsubmit();
			if(ret)
				this.element.submit();
		}
	};
	
	var A = {
		setHref: function(href){
			this.element.href = href;
		},
		
		getHref: function(href){
			return this.element.href;
		},
		
		setTarget: function(target){
			this.element.target = target;
		},
		
		setTitle: function(title){
			this.element.title = title;
		}
	};
	
	var EasyJSDocument = {
		getByID: function(id){
			return this.element.getElementById(id);
		}
	};
	
	var Iframe = {
		setCache: function(value){
			this.easyJSVars["iframeCache"] = value;
		},
		
		getDoc: function(){
			if(!this.element.name)
				throw new Error("attribute name is not defined on iframe");
			return $(window.frames[this.element.name].document);
		},
		
		setSrc: function(src){
			this.easyJSVars["iframeSRC"] = src;
			this.element.src = src;
		},
		
		rebuildSrc: function(){
			var src = this.easyJSVars["iframeSRC"];
			var vars = this.easyJSVars["iframeGETVars"];
			var cache = this.easyJSVars["iframeCache"];
			
			for(var i=0;i<vars.length;i++){
				if(i==0){
					src = src + "?" + vars[i][0] + "=" + vars[i][1];
				}
				else
					src = src + "&" + vars[i][0] + "=" + vars[i][1];
			}
			
			if(vars.length==0 && !cache)
				src = src + "?nochache=" + Math.random();
			else if(!cache)
				src = src + "&nochache=" + Math.random();
			
			this.element.src = src;
		},
		
		setGETVars: function(arrayGETVars){
			this.easyJSVars["iframeGETVars"] = arrayGETVars;
		}
	};
	
	var Img = {
		
		setSrc: function(src,cache){
			this.element.src = ""; //si no fiquem el src en buit primer, en webkit no funcionarà bé
			if(src){ //per si volen buidar el src
				if(!cache)
					this.element.src = src+"?nochache="+Math.random();
				else
					this.element.src = src;
			}
		},
		
		getSrc: function(){
			return this.element.src;
		},
		
		setAlt: function(alt){
			this.element.alt = alt;
		},
		
		getAlt: function(){
			return this.element.alt;
		}
	};
	
	var EasyJSWindow = {
		isPageLoaded: function(){
			return pageLoaded;
		},
		
		getScrollLeft: function(){
			return document.documentElement.scrollLeft || document.body.scrollLeft;
		},
		
		getScrollTop: function(){
			return document.documentElement.scrollTop || document.body.scrollTop;
		},
			
		getWidth: function(){
			if(document.documentElement.clientWidth > document.documentElement.scrollWidth)
				return document.documentElement.clientWidth;
			else
				return document.documentElement.scrollWidth;			
		},
		
		getScrollWidth: function(){
			if(document.body.scrollLeft)
				return document.body.scrollLeft;
			else
				return document.documentElement.scrollLeft;
		},
		
		getVisibleWidth: function(){
			return document.documentElement.clientWidth;
		},
		
		getHeight: function(){
			if(document.documentElement.clientHeight > document.documentElement.scrollHeight)
				return document.documentElement.clientHeight;
			else
				return document.documentElement.scrollHeight;
		},
		
		getScrollHeight: function(){
			if(document.body.scrollTop)
				return document.body.scrollTop;
			else
				return document.documentElement.scrollTop;
		},
		
		getVisibleHeight: function(){
			return document.documentElement.clientHeight;
		},
		
		cover: function(color,velocity,tope){
			var ID;
			do{
				ID = Misc.getUniqID();
			}while(this.easyJSVars["coverWindow"][ID]);
			
			var div =  this.easyJSVars["coverWindow"][ID] = new Element("div");
			div.setStyle("background-color",color);
			div.setStyle("position","fixed");
			div.setStyle("z-index","2");
			div.setStyle("opacity","0");
			if(IE6){
				div.setStyle("position","absolute");
				div.setStyle("width",$(window).getWidth()+"px");
				div.setStyle("height",$(window).getHeight()+"px");
				this.easyJSVars["coverResizeFunct"][ID] = function(){
					div.setStyle("width",$(window).getWidth()+"px");
					div.setStyle("height",$(window).getHeight()+"px");
				};
				$(window).addEvent("resize",this.easyJSVars["coverResizeFunct"][ID]);
			}
			document.body.appendChild(div.element);
			div.fadeIn(velocity,tope);
			div.setStyle("bottom","0px");
			div.setStyle("right","0px");
			div.setStyle("left","0px");
			div.setStyle("top","0px");
			
			return ID;
		},
		
		uncover: function(ID, velocity){
			if(!ID)
				throw new Error("missing argument ID");
			var div = this.easyJSVars["coverWindow"][ID];
			var $this = this;
			div.fadeOut(velocity,function(){
				div.remove();
				if(IE6)
					$(window).removeEvent("resize",$this.easyJSVars["coverResizeFunct"][ID]);
				$this.easyJSVars["coverWindow"][ID] = null;
			});
		},
		
		startLoading: function(){
			this.stopLoading();
			var loadingImg = this.easyJSVars["loadingAnimation"] = new Element("img");
			loadingImg.addEvent("load",function(){
				loadingImg.setStyle("left",$(window).getVisibleWidth()/2+$(window).getScrollWidth()-loadingImg.element.width/2+"px");
				loadingImg.setStyle("top",$(window).getVisibleHeight()/2+$(window).getScrollHeight()-loadingImg.element.height/2+"px");
				loadingImg.fadeIn(0.02);
				loadingImg.setStyle("visibility","visible");
			});
			$(window).addEvent("resize",function(){
				if(loadingImg){
					loadingImg.setStyle("left",$(window).getVisibleWidth()/2+$(window).getScrollWidth()-loadingImg.element.width/2+"px");
					loadingImg.setStyle("top",$(window).getVisibleHeight()/2+$(window).getScrollHeight()-loadingImg.element.height/2+"px");
				}
			});
			loadingImg.setStyle("position","absolute");
			loadingImg.setStyle("display","block");
			loadingImg.setStyle("visibility","hidden");
			loadingImg.setStyle("z-index","999");
			$(document.body).append(loadingImg);
			this.easyJSVars["coverLoadAnimID"] = $(window).cover("black",0.009,0.07);
			loadingImg.setSrc("/files/js/loading.gif", true);
		},
		
		stopLoading: function(){
			var loadingImg = this.easyJSVars["loadingAnimation"];
			var $this = this;
			if(loadingImg){
				loadingImg.fadeOut(0.03,function(){
					loadingImg.remove();
					$this.easyJSVars["loadingAnimation"] = null;
				});
				$(window).uncover($this.easyJSVars["coverLoadAnimID"], 0.009);
			}
		}
	};
	
	var TextArea = {
			
		getSelectedText: function(){
			if(document.selection){ //IE
				this.element.focus();
				sel = document.selection.createRange();
				return sel.text;
			}
			else{
				var startPos = this.element.selectionStart;
				var endPos = this.element.selectionEnd;
				return this.element.value.substr(startPos, endPos - startPos);
			}
		},
			
		getPointerPosition: function(){
			if(this.element.selectionStart != undefined)
				return this.element.selectionStart;
			else{ /*IE*/
				var range = document.selection.createRange();
				var stored_range = range.duplicate();
				stored_range.moveToElementText( this.element );
				stored_range.setEndPoint( 'EndToEnd', range );
				return  stored_range.text.length - range.text.length;
			}
		},
		
		maxLength: function(max){
			this.addEvent("keypress",function(){
				if($this.element.value.length >= max && $event.getCharCode()){
					$this.element.value = $this.element.value.substring(0, max);
					return false;
				}
			});
			this.addEvent("blur", function(){
				if($this.element.value.length>max){ //keyCode te valor si es tracta d'una tecla que no sigui alfanumerica, si es alfanumerica el valor l'agafa la propietat e.which
					$this.element.value = $this.element.value.substring(0,max);
					alert("Límite excedido ("+max+" carácteres). Se ha reducido el texto.");
				}
			});
		}
	};
	
	var Input = {
		getType: function(){
			return this.element.type.toLowerCase();
		},
		
		isDisabled: function(){
			if(this.element.disabled==true)
				return true;
			else
				return false;
		}
	};
	
	var InputRadio = {
		isChecked: function(){
			return this.element.checked;
		},
		
		setChecked: function(bool){
			this.element.checked = bool;
		}
	};
	
	var Label = {
		setFor: function(id){
			this.element.htmlFor = id; 
		}
	};
	
	var InputText = {
		getSelectedText: function(){
			if(document.selection){ //IE
				this.element.focus();
				sel = document.selection.createRange();
				return sel.text;
			}
			else{
				var startPos = this.element.selectionStart;
				var endPos = this.element.selectionEnd;
				return this.element.value.substr(startPos, endPos - startPos);
			}
		},
			
		getPointerPosition: function(){
			if(this.element.selectionStart != undefined)
				return this.element.selectionStart;
			else{ /*IE*/
				var oSel = document.selection.createRange(); 
				oSel.moveStart('character', -this.element.value.length); 
				return oSel.text.length;
			}
		},
		
		restrictNum: function(maxNum, numType){
			this.restrict(null, numType);
			this.addEvent("keypress",function(){
				var charCode = $event.getCharCode();
				
				var pointerPosition = $this.getPointerPosition();
				var value = $this.getValue();
				
				if($this.getSelectedText()==$this.getValue())
					var newValue = new String(String.fromCharCode(charCode));
				else if(pointerPosition != value.length){
					var newValue = "";
					for(var c=0;c<value.length;c++){
						if(c==pointerPosition){
							newValue += String.fromCharCode(charCode)+value.substr(c,1);
						}
						else
							newValue += value.substr(c,1);
					}
				}
				else
					var newValue = value+String.fromCharCode(charCode);
				
				if(newValue > maxNum && charCode)
					return false;
				else
					return true;
			});
		},
		
		restrict: function(maxLength, varType, mixedExprReg){
			var element = this.element;
			var exprReg;
			
			if(maxLength && (varType!="decimal" || varType!="unsigned-decimal"))
				element.maxLength = maxLength;
	
			if(varType=="int")
				exprReg = /^[0-9\-]*$/;
			else if(varType=="unsigned-int")
				exprReg = /^[0-9]*$/;
			else if(varType=="decimal")
				exprReg = /^[0-9,.\-]*$/;
			else if(varType=="unsigned-decimal")
				exprReg = /^[0-9,.]*$/;
			else if(varType=="varchar")
				exprReg = false;
			else if(varType=="mixed")
				exprReg = mixedExprReg;
			else
				exprReg = false;

			if(exprReg){
				this.addEvent("keypress",function(){
					var charCode = $event.getCharCode();
					
					if(varType=="decimal" || varType=="unsigned-decimal"){ 
						if(element.value.search(/[,.]/)>=0){
							if(varType=="unsigned-decimal")
								exprReg = /^[0-9]*$/;
							else
								exprReg = /^[0-9\-]*$/;
						}
						else{
							if(varType=="unsigned-decimal")
								exprReg = /^[0-9,.]*$/;
							else
								exprReg = /^[0-9,.\-]*$/;
						}

						var decimalNum = element.value.split(/[,.]/);
						if($this.getPointerPosition() != 0 && String.fromCharCode(charCode)=="-" && varType=="decimal")
							return false;
						else if($this.getPointerPosition() <= decimalNum[0].length && decimalNum[0].length>=maxLength && (decimalNum[1]==undefined && (String.fromCharCode(charCode)!="." && String.fromCharCode(charCode)!=",")) && charCode)
							return false;
						else if($this.getPointerPosition() <= decimalNum[0].length && decimalNum[0].length>=maxLength && decimalNum[1]!=undefined && charCode)
							return false;
						else if(decimalNum[1]!=undefined && $this.getPointerPosition() >= decimalNum[0].length+1 && decimalNum[1].length>=2 && charCode)
							return false;
					}
					else if($this.getPointerPosition() != 0 && String.fromCharCode(charCode)=="-" && varType=="int")
						return false;

					if(!exprReg.test(String.fromCharCode(charCode)) && charCode){
						return false;
					}else{
						return true;
					}
				});
			}
		}
	};
	
	var Select = {
		selectAll: function(){ /*select multiple type*/
			for(var i=0;i<this.element.options.length;i++){
				this.element.options[i].selected = true;
			}
		},
		
		sort: function(selectionValue){
			var element = this.element;
			var data = [];
			
			for(var i=0;i<element.options.length;i++){
				data.push([element.options[i].text, element.options[i].value]);
			}
			
			data.sort();
			
			for(var i=0;i<element.options.length;i++){
				element.options[i].text = data[i][0];
				element.options[i].value = data[i][1];
			}
			if(selectionValue)
				element.value = selectionValue;
			else
				element.selectedIndex = 0;
		},
		
		toBegin: function(item){
			var text = item.text;
			var value = item.value;
			
			this.remove(item);
			this.add(text, value, 0);
		},
		
		getItemFromValue: function(value){
			for(var i=0;i<this.element.options.length;i++){
				if(this.element.options[i].value==value){
					return this.element.options[i];
				}
			}
			
			return false;
		},
		
		selectFromValue: function(value){
			this.element.selectedIndex = this.getItemFromValue(value).index;
		},
		
		selectFromText: function(text){
			for(var i=0;i<this.element.options.length;i++){
				if(this.element.options[i].text==text){
					this.element.selectedIndex = this.element.options[i].index;
					break;
				}
			}
		},
		
		existItemWithText: function(text, caseSensitive, optionException){
			if(!caseSensitive)
				text = text.toString().toLowerCase();
			for(var i=0;i<this.element.options.length;i++){
				if(!caseSensitive)
					var optionText = this.element.options[i].text.toString().toLowerCase();
				else
					var optionText = this.element.options[i].text;
				if(typeof this.element.options[i]!=undefined && typeof this.element.options[i].text!=undefined && optionException!=this.element.options[i] && optionText==text)
					return true;
			}
			
			return false;
		},
		
		add: function(text, value, pos){
			try{
				if(pos === undefined) var item = null;
				else var item = this.element.options[pos];
				this.element.add(new Option(text, value), item);
			}
			catch(e){
				if(pos === undefined) 
					this.element.add(new Option(text, value));
				else
					this.element.add(new Option(text, value), pos);
			}
		},
		
		remove: function(item){
			if(typeof item == "object"){
				for(var i=0;i<this.element.options.length;i++){
					if(this.element.options[i] === item){
						this.element.remove(i);
						break;
					}
				}
			}
			else
				this.element.remove(item);
		},
		
		setMultiple: function(bool){
			this.element.multiple = bool;
		},
		
		fill: function(list){
			var element = this.element;
			
			list.each(function(el){
				try{
					element.add(new Option(el[0], el[1]), null);
				}
				catch(e){
					element.add(new Option(el[0], el[1]));
				}
			});
		},
		
		clear: function(){
			this.element.length = 0;
		},
		
		isSelected: function(){
			if(this.element.selectedIndex != -1)
				return true;
			else
				return false;
		},
		
		isEmpty: function(){
			if(this.element.length == 0)
				return true;
			else
				return false;
		},
		
		upPosition: function(){
			if(this.element.selectedIndex > 0){
				var value1 = this.element.options[this.element.selectedIndex].value;
				var value2 = this.element.options[this.element.selectedIndex-1].value;
				var text1 = this.element.options[this.element.selectedIndex].text;
				var text2 = this.element.options[this.element.selectedIndex-1].text;
				this.element.options[this.element.selectedIndex].value=value2;
				this.element.options[this.element.selectedIndex-1].value=value1;
				this.element.options[this.element.selectedIndex].text=text2;
				this.element.options[this.element.selectedIndex-1].text=text1;
				this.element.selectedIndex = this.element.selectedIndex-1;
			}
		},
		
		downPosition: function(){
			if(this.element.selectedIndex >=0 && this.element.selectedIndex<this.element.length-1){
				var value1 = this.element.options[this.element.selectedIndex].value;
				var value2 = this.element.options[this.element.selectedIndex+1].value;
				var text1 = this.element.options[this.element.selectedIndex].text;
				var text2 = this.element.options[this.element.selectedIndex+1].text;
				this.element.options[this.element.selectedIndex].value=value2;
				this.element.options[this.element.selectedIndex+1].value=value1;
				this.element.options[this.element.selectedIndex].text=text2;
				this.element.options[this.element.selectedIndex+1].text=text1;
				this.element.selectedIndex = this.element.selectedIndex+1;
			}
		},
		
		getSelected: function(){
			return this.element.options[this.element.selectedIndex];
		},
		
		setSelected: function(index){
			return this.element.selectedIndex = index;
		},
		
		getValue: function(){
			return this.element.value;
		}
	};
	
	
	/*#########################################
	############ PRIMARY OBJECTS ############
	#########################################*/
	
	Array.prototype.clone = function(){
		var clon = [];
		this.each(function(element){
			if(element instanceof Array == true)
				clon.push(element.clone());
			else
				clon.push(element);
		});
		
		return clon;
	};
	
	Array.prototype.each = function(funct){
		var length = this.length;
		for(var c=0;c<length;c++){
			if(funct(this[c], c) === false)
				break;
		}
	};
	
	/*reverse each*/
	Array.prototype.revEach = function(funct){
		var length = this.length;
		for(var c=length;c>=0;c--){
			if(funct(this[c], c) === false)
				break;
		}
	};

	Array.prototype.isWithin = function(element){
		for(var c=0; c < this.length; c++){
			if(this[c] == element) 
				return true;
		}
		return false;
	};

	Array.prototype.remove = function(index){
		this.splice(index, 1);
	};
	
	Array.prototype.indexOf = function(element){
		for(var c=0; c < this.length; c++){
			if(this[c] == element)
				return c;
		}
		
		return false;
	};

	String.prototype.urlEncode = function() {
		return escape(this).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
	};

	String.prototype.trim = function(){
		return this.replace(/^\s+|\s+$/g,"");
	};

	String.prototype.capitalize = function(){
		return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
	};
	
	String.prototype.toInt = function(){
		var integer = this.replace(",", ".");
		integer = integer.replace(/[^[0-9.\-]/g, "");
		return Math.ceil(new Number(integer));
	};
	
	String.prototype.toFloat = function(){
		var integer = this.replace(",", ".");
		integer = integer.replace(/[^[0-9.\-]/g, "");
		return new Number(integer);
	};
	
	String.prototype.isInt = function(){
		var aux = this.toInt();
		if(this==aux)
			return true;
		else 
			return false;
	};
	
	String.prototype.count = function(regExp){
	    return (this.length - this.replace(new RegExp(regExp,"g"), '').length) / regExp.length;
	};
	
	String.prototype.replaceLoop = function(regexp, replace){
		var text = this;
		do{
			text = text.replace(regexp,replace);
		}while(text.match(regexp));
		
		return text;
	};
	
	/*#########################################
	############ OBJECTS ############
	#########################################*/
	
	var Event = function(e){
		this.getCharCode = function(){
			if(e.type == "keypress"){ //e.which en firefox retorna 0 cuan presiones retrocès o intro, no tindria que ser així, no hauria de retornar res.
				if(e.which != undefined){
					if(e.which==8 || e.which==13)
						return 0;
					else
						return e.which;
				}
				else if(e.charCode != undefined)
					return e.charCode;
				else if(e.keyCode == 13 || e.keyCode == 8)
					return 0;
				else
					return e.keyCode;					
			}
			else
				throw new Error("charCode only for keypress event. For other keyboard events use the keyCode");
		};
		
		this.getKeyCode = function(){
			return e.keyCode;
		};
		
		this.getTarget = function(){
			var target = e.target || e.srcElement;
			
			return $(target);
		};
		
		this.getRelatedTarget = function(){
			var target = e.relatedTarget || e.toElement;
			
			return $(target);
		};
		
		this.getLayerX = function(){
			return e.layerX || e.offsetX;
		};
		
		this.getLayerY = function(){
			return e.layerY || e.offsetY;
		};
		
		this.getClientX = function(){
			return e.clientX;
		};
		
		this.getClientY = function(){
			return e.clientY;
		};
	};
	
	//això ho faig pq a Explorer si no fiques res a txtDefault, apareix "undefined" al prompt
	var windowPrompt = window.prompt;
	window.prompt = function(txt, txtDefault){
		if(!txtDefault) txtDefault = "";
		return windowPrompt(txt, txtDefault);
	};
	
	var pageLoaded = false;
	$(window).addEvent("load", function(){
		pageLoaded = true;
	});
})();

/*###################################
############# GLOBALS #############
###################################*/

function isEmpty(txt){
	if(typeof txt == "object"){
		if(txt==null)
			return true;
		else if(txt.element.value.trim().length == 0 || txt.element.value==undefined)
			return true;
		else
			return false;
	}
	else if(txt.trim().length == 0 || txt==undefined){
		return true;
	}else{
		return false;
	}
}

function isIE(){
	return /*@cc_on!@*/false;
}

function hasElementID(id){
	if(document.getElementById(id))
		return true;
	else
		return false;
}

var Element = function(tag, attr){
	return $(document.createElement(tag), attr);
};

var Misc = {
	getUniqID: function(){
		return Math.ceil(Math.random()*10000);
	},
	
	isEmail: function(string){
		return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(string);
	}
};

var RPC = function(url, loadingAnimation){
	var vars = [];
	var onLoad;
	var onLoading;
	var sc;
	
	this.setOnLoad=function(onld){
		onLoad = onld;
	};
	this.setOnLoading=function(onlding){
		onLoading = onlding;
	};
	this.addVar = function(key, value){
		vars.push([key, escape(value)]);
	};
	
	this.start = function(){
		if(onLoading)
			onLoading();
		if(loadingAnimation)
			$(window).startLoading();

		if(sc) {document.getElementsByTagName('head')[0].removeChild(sc);}
		sc= document.createElement('script');
		sc.type="text/javascript";
		sc.src=url+'?'+getVars()+'nocache='+Math.random();

		if(sc.onreadystatechange === null){ /*IE*/
			sc.onreadystatechange = function(){
			 	if(sc.readyState=="loaded"){
			 		if(onLoad)
			 			onLoad();
  					if(loadingAnimation)
						$(window).stopLoading();
			 	}
			};
		}
		else{
			sc.onload=function(){
				if(onLoad)
					onLoad();
				if(loadingAnimation)
					$(window).stopLoading();
			};
		}
		document.getElementsByTagName('head')[0].appendChild(sc);
	};
	
	function getVars(){
		var string = "";
		
		vars.each(function(element){
			string += element[0]+"="+element[1]+"&";
		});

		return string;
	}
};

var Message = function(boxID){
	if(typeof boxID != "object")
		var box = $(boxID);
	else
		var box = boxID;
	var open = false;
	var cover;
	
	/*parametros de herencia*/
	this.element = box.element;
	this.easyJSVars = [];
	
	function construct(){
		if(box.isAppend())
			box.remove();
		$(document.body).append(box);
		box.setStyle("display","none");
	}construct();
	
	/*PRIVATE METHODS*/
	
	function relocate(){
		var w = box.getWidth();
		var h = box.getHeight();
		var left = $(window).getVisibleWidth()/2+$(window).getScrollWidth()-w/2;
		var top = $(window).getVisibleHeight()/2+$(window).getScrollHeight()-h/2;
		if(left< 0) left = 0;
		if(top < 0) top = 0;
		box.setStyle("left",left+"px");
		box.setStyle("top",top+"px");
	}
	
	/*PUBLIC METHODS*/
	
	this.relocate = function(){
		relocate();
	};
	
	this.show = function(boxVel, coverVel, covTop){
		open = true;
		var coverVelocity = coverVel || 0.009;
		var boxVelocity = boxVel || 0.06;
		var coverTop = covTop || 0.07;
		box.setStyle("position","absolute");
		box.setStyle("z-index",3);
		
		$(window).addEvent("resize",function(){
			if(open){
				if(box.getStyle("opacity") == 1){
					box.fadeOut(0.06,function(){
						relocate();
						box.fadeIn(0.06);
					});
				}
				else {
					relocate();
				}
			}
		});
		box.setStyle("opacity",0);
		box.setStyle("display","block");
		relocate();
		
		cover = $(window).cover("black", coverVelocity,coverTop);
		box.fadeIn(boxVelocity);		
	};
	
	this.close = function(vel){
		open = false;
		var velocity = vel || 0.06;
		$(window).uncover(cover, velocity);
		box.fadeOut(velocity, function(){
			box.setStyle("display","none");
		});
	};
	
	this.destroy = function(vel, funct){
		open = false;
		var velocity = vel || 0.06;
		$(window).uncover(cover, velocity);
		box.fadeOut(velocity, function(){
			box.remove();
			if(funct)
				funct();
		});
	};
};
Message.prototype = new EasyJS;

var Alert = function(message){
	var WIDTH = 380;
	var HEIGHT = 150;
	function construct(){
		var box = new Element("div");
		box.setStyle("border","6px solid #6c6c6c");
		box.setStyle("background-color", "#e8e8e8");
		box.setStyle("width",WIDTH+"px");
		box.setStyle("min-height", HEIGHT+"px");
		box.setStyle("border-radius", "10px");
		box.setStyle("Moz-border-radius", "10px");
		box.setStyle("Webkit-border-radius", "10px");
		box.setStyle("margin","0px");
		box.setStyle("padding","0px");
		var header = new Element("h1");
		header.setStyle("margin","0px");
		header.setStyle("padding","0px");
		header.setStyle("background-color","#9c9c9c");
		header.setStyle("padding-top", "10px");
		header.setStyle("height", "30px");
		header.setStyle("font-size", "20px");
		header.setStyle("font-weight", "bold");
		header.setStyle("text-align","center");
		header.addText("ALERTA");
		box.append(header);
		var text = new Element("p");
		text.addText(message);
		text.setStyle("margin","0px");
		text.setStyle("padding","0px");
		text.setStyle("padding", "20px 15px 70px");
		text.setStyle("color", "black");
		text.setStyle("text-align","center");
		text.setStyle("font-size","13px");
		box.append(text);
		var accept = new Element("a");
		accept.setStyle("margin","0px");
		accept.setStyle("padding","0px");
		accept.setStyle("display","block");
		accept.setStyle("width","80px");
		accept.setStyle("height","20px");
		accept.setStyle("position","absolute");
		accept.setStyle("bottom","10px");
		accept.setStyle("left",WIDTH/2-40+"px");
		accept.setStyle("background-color","#9c9c9c");
		accept.setStyle("text-align","center");
		accept.setStyle("padding-top","5px");
		accept.setStyle("font-size","15px");
		accept.setStyle("font-weight","bold");
		accept.setStyle("color", "black");
		accept.setStyle("text-decoration", "none");
		accept.setStyle("border","3px solid #9c9c9c");
		accept.setStyle("Moz-border-radius", "5px");
		accept.setStyle("Webkit-border-radius", "5px");
		accept.setStyle("border-radius", "5px");
		accept.addText("Aceptar");
		accept.setHref("#");
		accept.addEvent("mouseover", function(){
			accept.setStyle("background-color", "#b9b9b9");
			accept.setStyle("border","3px solid #b9b9b9");
		});
		accept.addEvent("mouseout", function(){
			accept.setStyle("background-color", "#9c9c9c");
			accept.setStyle("border","3px solid #9c9c9c");
		});
		accept.addEvent("click", function(){
			msg.destroy(0.1);
			return false;
		});
		box.append(accept);
		
		var msg = new Message(box);
		msg.show(0.07,0.01, 0.06);
	}construct();
};

var Confirm = function(message, title, acceptFunction, cancelFunction){
	var WIDTH = 380;
	var HEIGHT = 150;
	function construct(){
		var box = new Element("div");
		box.setStyle("border","6px solid #6c6c6c");
		box.setStyle("background-color", "#e8e8e8");
		box.setStyle("width",WIDTH+"px");
		box.setStyle("min-height", HEIGHT+"px");
		box.setStyle("border-radius", "10px");
		box.setStyle("Moz-border-radius", "10px");
		box.setStyle("Webkit-border-radius", "10px");
		box.setStyle("margin","0px");
		box.setStyle("padding","0px");
		var header = new Element("h1");
		header.setStyle("margin","0px");
		header.setStyle("padding","0px");
		header.setStyle("background-color","#9c9c9c");
		header.setStyle("padding-top", "10px");
		header.setStyle("height", "30px");
		header.setStyle("font-size", "20px");
		header.setStyle("font-weight", "bold");
		header.setStyle("text-align","center");
		header.addText(title);
		box.append(header);
		var text = new Element("p");
		text.addText(message);
		text.setStyle("margin","0px");
		text.setStyle("padding","0px");
		text.setStyle("padding", "20px 15px 70px");
		text.setStyle("color", "black");
		text.setStyle("text-align","center");
		text.setStyle("font-size","13px");
		box.append(text);
		var accept = new Element("a");
		accept.setStyle("margin","0px");
		accept.setStyle("padding","0px");
		accept.setStyle("display","block");
		accept.setStyle("width","80px");
		accept.setStyle("height","20px");
		accept.setStyle("position","absolute");
		accept.setStyle("bottom","10px");
		accept.setStyle("left",WIDTH/2+5+"px");
		accept.setStyle("background-color","#9c9c9c");
		accept.setStyle("text-align","center");
		accept.setStyle("padding-top","5px");
		accept.setStyle("font-size","15px");
		accept.setStyle("font-weight","bold");
		accept.setStyle("color", "black");
		accept.setStyle("text-decoration", "none");
		accept.setStyle("border","3px solid #9c9c9c");
		accept.setStyle("Moz-border-radius", "5px");
		accept.setStyle("Webkit-border-radius", "5px");
		accept.setStyle("border-radius", "5px");
		accept.addText("Aceptar");
		accept.setHref("#");
		accept.addEvent("mouseover", function(){
			accept.setStyle("background-color", "#b9b9b9");
			accept.setStyle("border","3px solid #b9b9b9");
		});
		accept.addEvent("mouseout", function(){
			accept.setStyle("background-color", "#9c9c9c");
			accept.setStyle("border","3px solid #9c9c9c");
		});
		accept.addEvent("click", function(){
			msg.destroy(0.1, acceptFunction);
			return false;
		});
		box.append(accept);
		var cancel = new Element("a");
		cancel.setStyle("margin","0px");
		cancel.setStyle("padding","0px");
		cancel.setStyle("display","block");
		cancel.setStyle("width","80px");
		cancel.setStyle("height","20px");
		cancel.setStyle("position","absolute");
		cancel.setStyle("bottom","10px");
		cancel.setStyle("left",WIDTH/2-85+"px");
		cancel.setStyle("background-color","#9c9c9c");
		cancel.setStyle("text-align","center");
		cancel.setStyle("padding-top","5px");
		cancel.setStyle("font-size","15px");
		cancel.setStyle("font-weight","bold");
		cancel.setStyle("color", "black");
		cancel.setStyle("text-decoration", "none");
		cancel.setStyle("border","3px solid #9c9c9c");
		cancel.setStyle("Moz-border-radius", "5px");
		cancel.setStyle("Webkit-border-radius", "5px");
		cancel.setStyle("border-radius", "5px");
		cancel.addText("Cancelar");
		cancel.setHref("#");
		cancel.addEvent("mouseover", function(){
			cancel.setStyle("background-color", "#b9b9b9");
			cancel.setStyle("border","3px solid #b9b9b9");
		});
		cancel.addEvent("mouseout", function(){
			cancel.setStyle("background-color", "#9c9c9c");
			cancel.setStyle("border","3px solid #9c9c9c");
		});
		cancel.addEvent("click", function(){
			msg.destroy(0.1, cancelFunction);
			return false;
		});
		box.append(cancel);
		
		var msg = new Message(box);
		msg.show(0.07,0.01, 0.06);
	}construct();
};

var ProgressMessage = function(message, title, total){
	var WIDTH = 380;
	var HEIGHT = 150;
	var progressText;
	var progress;
	var msg;
	var completed = false;
	
	function construct(){
		var box = new Element("div");
		box.setStyle("border","6px solid #6c6c6c");
		box.setStyle("background-color", "#e8e8e8");
		box.setStyle("width",WIDTH+"px");
		box.setStyle("min-height", HEIGHT+"px");
		box.setStyle("border-radius", "10px");
		box.setStyle("Moz-border-radius", "10px");
		box.setStyle("Webkit-border-radius", "10px");
		box.setStyle("margin","0px");
		box.setStyle("padding","0px");
		var header = new Element("h1");
		header.setStyle("margin","0px");
		header.setStyle("padding","0px");
		header.setStyle("background-color","#9c9c9c");
		header.setStyle("padding-top", "10px");
		header.setStyle("height", "30px");
		header.setStyle("font-size", "20px");
		header.setStyle("font-weight", "bold");
		header.setStyle("text-align","center");
		header.addText(title);
		box.append(header);
		var text = new Element("p");
		text.addText(message);
		text.setStyle("margin","0px");
		text.setStyle("padding","0px");
		text.setStyle("padding", "20px 15px 15px");
		text.setStyle("color", "black");
		text.setStyle("text-align","center");
		text.setStyle("font-size","13px");
		box.append(text);
		var progressBar = new Element("div");
		progressBar.setStyle("width", "250px");
		progressBar.setStyle("height", "15px");
		progressBar.setStyle("background-color", "gray");
		progressBar.setStyle("border", "1px solid black");
		progressBar.setStyle("position", "relative");
		progressBar.setStyle("left", WIDTH/2-125+"px");
		progressBar.setStyle("overflow", "hidden");
		progress = new Element("div");
		progress.setStyle("position", "absolute");
		progress.setStyle("left", "0px");
		progress.setStyle("top", "0px");
		progress.setStyle("background-color", "black");
		progress.setStyle("position", "relative");
		progress.setStyle("height", "15px");
		progress.setStyle("width", "0px");
		progressBar.append(progress);
		box.append(progressBar);
		progressText = new Element("p");
		progressText.setStyle("text-align","center");
		progressText.setStyle("margin","5px 0px 15px");
		progressText.addText("0 de "+total);
		box.append(progressText);
		
		msg = new Message(box);
		msg.show(0.07,0.01, 0.06);
	}construct();
	
	this.setValue = function(value){
		progress.setStyle("width", Math.ceil(250*value/total)+"px");
		progressText.setText(value+" de "+total);
		if(value>=total || total==0){
			completed = true;
			msg.destroy(0.1);
		}
	};
	
	this.isCompleted = function(){
		return completed;
	};
};
