// AJAXForms
// Copyright (C) 2006 AJAXForms S.L.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
// Contact the author at: <info@ajaxforms.com>.
var gusztustalansag=false;
function Binding(xpath, model, bind) {
	this.bind = bind? $(bind).xfElement : null;
	this.xpath = xpath? XPath.get(xpath) : null;
	this.model = model? (typeof model == "string"? $(model) : model).xfElement : null;
};

Binding.prototype.evaluate = function(ctx, depsNodes, depsElements) {
	var result = null;

	if (this.bind) {
		result = this.bind.nodes;
		copyArray(this.bind.depsNodes, depsNodes);
		copyArray(this.bind.depsElements, depsElements);
	} else {
		var exprCtx = new ExprContext(this.model? this.model.getInstanceDocument() : ctx,
			null, null, null, null, ctx);
		exprCtx.initDeps(depsNodes, depsElements);
		result = this.xpath.evaluate(exprCtx);
	}

	return result;
};
var IdManager = {
    cloneId : function(element) {
        assert(element && element.id);
        var id = element.getAttribute("oldid") || element.id;
        var list = this.data[id];

        if (!list) {
            list = [];
            this.data[id] = list;
        }
        
        var newId = "clonedId" + this.index++;
        list.push(newId);
        element.setAttribute("oldid", id);
        element.id = newId;
    },
    find : function(id) {
        var ids = this.data[id];
        
        if (ids) {
	        for (var i = 0; i < ids.length; i++) {
	            var element = $(ids[i]);
	
	            if (element) {
	                var parent = element.parentNode;
	        
	                while (parent.nodeType == NodeType.ELEMENT) {
	                	if (Core.hasClass(parent, "xforms-repeat-item")) {
	                		if (Core.hasClass(parent, "xforms-repeat-item-selected")) {
	                            return element;
	                        } else {
	                            break;
	                        }
	                     }
	                
	                     parent = parent.parentNode;
	                }
	            }
	        }
	    }
        
        var res = $(id);
        
        if (!res) {
        	alert("element " + id + " not found");
        }

        return res;
    },
    clear : function() {
        for (var i in this.data) {
            this.data[i] = null;
        }
        
        this.data = null;
    },
    data : new Array(),
    index : 0
};var Writer = {
    toString : function(node, map, init) {
	    var st = "";
	    
	    if (!map) {
	        map = {length : 0};
	        this.loadNS(node, map);
	    }
	
	    switch(node.nodeType) {
	    case NodeType.ELEMENT :
	        var name = this.name(node, map);
	        st += "<" + name;
	
	        if (!init) {
	            for (var i in map) {
	                if (i == "length") continue;
	                
	                var prefix = map[i];
	                st += " xmlns";

	                if (prefix) {
	                     st += ":" + prefix;
	                }
	                
	                st += "=\"" + i + "\"";
	            }

	            init = true;
	        }

	        var atts = node.attributes;
	
	        for (var i = 0; i < atts.length; i++) {
	            st += this.toString(atts[i], map, init);
	        }
	
	        st += ">";
	
	        var childs = node.childNodes;
	
	        for (var i = 0; i < childs.length; i++) {
	            st += this.toString(childs[i], map, init);
	        }
	
	        st += "</" + name + ">";
	
	        break;
	    case NodeType.ATTRIBUTE :
	        return " " + this.name(node, map) + "=\"" + this.encoding(node.nodeValue) + "\"";
	    case NodeType.TEXT :
	        return this.encoding(node.nodeValue);
	    case NodeType.DOCUMENT :
	        return this.toString(node.documentElement, map, init);
	    }
	    
	    return st;
    },
    loadNS : function(node, map) {
	    var ns = node.namespaceURI;
	
	    if (ns != null && typeof map[ns] == "undefined") {
	        if (map.length == 0) {
	            map[ns] = null;
	        } else {
	            map[ns] = "pre" + map.length;            
	        }
	        
	        map.length++;
	    }
	    
	    var atts = node.attributes;
	    var childs = node.childNodes;
	
	    for (var i = 0; i < atts.length; i++) {
	        this.loadNS(atts[i], map);
	    }
	
	    for (var i = 0; i < childs.length; i++) {
	        this.loadNS(childs[i], map);
	    }
    },
    name : function(node, map) {
	    var ns = node.namespaceURI;
	    var name = node.nodeName;
	    
	    if (ns) {
	        var prefix = map[ns];
	        
	        if (prefix) {
	            name = prefix + ":" + name;
	        }
	    }
	    
	    return name;
    },
    encoding : function(value) {
        var res = "";

        for (var i = 0; i < value.length; i++) {
        	var ch = value[i];

            if (ch == '>') {
            	res += "&gt;";
            } else if (ch == '<') {
            	res += "&lt;";
            } else {
	            var c = value.charCodeAt(i);
	            res +=  c >= 160? "&#" + c + ";" : value.charAt(i);
	        }
        }
        
        return res;
    }
};
function AJXConfirm(id, binding, ifexpr, whileexpr) {
	this.id = id;
	this.binding = binding;
	this.init(ifexpr, whileexpr);
};

AJXConfirm.prototype = new XFAbstractAction;

AJXConfirm.prototype.run = function(element, ctx, evt) {
	var text;

	if (this.binding) {
		var node = this.binding.evaluate(ctx)[0];

		if (node) {
			text = getValue(node);
		}
	} else {
		var e = IdManager.find(this.id);
		xforms.build(e, ctx);
		text = e.textContent || e.innerText;
	}

	if (text) {
		var res = confirm(text.trim());
        
		if (!res) {
			evt.stopPropagation();
			evt.stopped = true;
		}
	}
};
function AJXSetproperty(name, value, literal, ifexpr, whileexpr) {
	this.name = name;
	this.value = value;
	this.literal = literal;
	this.init(ifexpr, whileexpr);
};

AJXSetproperty.prototype = new XFAbstractAction;

AJXSetproperty.prototype.run = function(element, ctx) {
	var value = this.literal;

	if (this.value) {
		value = this.value.evaluate(node);
	    
		if (typeof(value) != "string" && typeof(value.length) != "undefined") {
			value = value.length > 0? getValue(value[0]) : "";
		}
	}
	
	if (value) {
		I8N.lang = value;
		DebugConsole.write("setproperty " + name + " = " + value);
	}
};
function XFAbstractAction() {
};

XFAbstractAction.prototype.init = function(ifexpr, whileexpr) {
	this.ifexpr = XPath.get(ifexpr);
	this.whileexpr = XPath.get(whileexpr);
	//XMLEvents.dispatch(this, "xforms-refresh");
};

XFAbstractAction.prototype.execute = function(element, ctx, evt) {
	if (evt.stopped) return;
	
	if (!ctx) {
		ctx = element.node || xforms.defaultModel.getInstanceDocument();
	}

	if (this.whileexpr) {
		while(booleanValue(this.whileexpr.evaluate(ctx))) {
			this.exec_(element, ctx, evt);
		}
	} else {
		this.exec_(element, ctx, evt);
	}
};

XFAbstractAction.prototype.exec_ = function(element, ctx, evt) {
	if (this.ifexpr) {
		if (booleanValue(this.ifexpr.evaluate(ctx))) {
			this.run(element, ctx, evt);
		}
	} else {
		this.run(element, ctx, evt);
	}
};

XFAbstractAction.prototype.run = function(element, ctx, evt) { };
function XFAction(ifexpr, whileexpr) {
	this.init(ifexpr, whileexpr);
	this.childs = [];
};

XFAction.prototype = new XFAbstractAction;

XFAction.prototype.add = function(action) {
	this.childs.push(action);
	return this;
};

XFAction.prototype.run = function(element, ctx, evt) {
	forEach(this.childs, "execute", element, ctx, evt);
};function XFDelete(nodeset, model, bind, at, context, ifexpr, whileexpr) {
	this.binding = new Binding(nodeset, model, bind);
	this.at = XPath.get(at);
	this.context = XPath.get(context);
	this.init(ifexpr, whileexpr);
};

XFDelete.prototype = new XFAbstractAction;

XFDelete.prototype.run = function(element, ctx) {
	if (this.context) {
		ctx = this.context.evaluate(ctx)[0];
	}
    
	if (!ctx) return;

	var nodes = this.binding.evaluate(ctx);

	if (nodes.length > 0) {
		var index = numberValue(this.at.evaluate(new ExprContext(ctx, 1, nodes)));
		var node = nodes[index - 1];
		
		if (!node) return;

		node.parentNode.removeChild(node);
		var repeat = node.repeat;

		if (node.repeat) {
			node.repeat.deleteNode(node);
		}

		var model = node.ownerDocument.model;
		xforms.addChange(model);
		model.setRebuilded(true);
	    XMLEvents.dispatch(model.findInstance(node), "xforms-delete");
		XNode.recycle(node);
	}
};
function XFDispatch(name, target, ifexpr, whileexpr) {
	this.name = name;
	this.target = target;
	this.init(ifexpr, whileexpr);
};

XFDispatch.prototype = new XFAbstractAction;

XFDispatch.prototype.run = function() {
	var target = typeof this.target == "string"? $(this.target) : this.target;
	XMLEvents.dispatch(target, this.name);
};
function XFInsert(nodeset, model, bind, at, position, origin, context, ifexpr, whileexpr) {
	this.binding = new Binding(nodeset, model, bind);
	this.origin = XPath.get(origin);
	this.context = XPath.get(context);
	this.at = XPath.get(at);
	this.position = XPath.get(position);
	this.init(ifexpr, whileexpr);
};

XFInsert.prototype = new XFAbstractAction;

XFInsert.prototype.run = function(element, ctx) {
	if (this.context) {
		ctx = this.context.evaluate(ctx)[0];
	}
    
	if (!ctx) return;

	var nodes = this.binding.evaluate(ctx);
	var index = 0;
	var node = null;
	var parent = null;

	if (this.origin) {
		node = this.origin.evaluate(ctx)[0];
	}

	if (!node) {
		if (nodes.length == 0) {
			return;
		}
    
		node = nodes[nodes.length - 1];
	}

	if (node.nodeType == NodeType.ATTRIBUTE) {
		alert("TODO clone Attribute");
	}

	if (nodes.length == 0) {
		parent = ctx;
	} else {
		parent = nodes[0].nodeType == NodeType.DOCUMENT? nodes[0] : nodes[0].parentNode;
        
		if (parent.nodeType != NodeType.DOCUMENT) {
			var res = this.at? numberValue(this.at.evaluate(new ExprContext(ctx, 1, nodes))) - 1: nodes.length - 1;
			var pos = this.position == "after"? 1 : 0;
			index = isNaN(res)? nodes.length : res + pos;
		}
	}

	var clone = node.cloneNode(true, parent.ownerDocument, true);

	DebugConsole.write("insert " + clone.nodeName + " in " + parent.nodeName
		+ " at " + index + " - " + ctx.nodeName);

	if (parent.nodeType == NodeType.DOCUMENT) {
		var first = parent.firstChild;
		parent.removeChild(first);
		XNode.recycle(first);
		parent.appendChild(clone);
	} else {
		var nodeAfter;

		if (index + 1 >= nodes.length) {
			parent.appendChild(clone);
		} else {
			nodeAfter = nodes[index];
			parent.insertBefore(clone, nodeAfter);
		}

		var repeat = nodes.length > 0? nodes[0].repeat : null;

		if (repeat) {
			repeat.insertNode(clone, nodeAfter,true);
		}
	}

	var model = clone.ownerDocument.model;
	xforms.addChange(model);
	model.setRebuilded(true);
	XMLEvents.dispatch(model.findInstance(clone), "xforms-insert");
};
function XFLoad(binding, resource, show, ifexpr, whileexpr) {
	this.binding = binding;
	this.resource = resource;
	this.show = show;
	this.init(ifexpr, whileexpr);
	
};

XFLoad.prototype = new XFAbstractAction;

XFLoad.prototype.run = function(element, ctx) {
	var href = this.resource;
  //document.write(href);
  if (this.binding) {
		var node = this.binding.evaluate(ctx)[0];
		if (node) {
			//document.write(getValue(node));
//			href = getValue(node);
		}
	}
  if (href) {
		if (this.show == "new") {
		var parent = element.parentNode;
		var ids="";
		var label;
		var test;
		var first=true;
		var nodes=Array();
		var teaor=false;
		var cim=false;
		var melleklet=false;
		var illetek=false;
		if(href.match("teaor")!=null){
			teaor=true;
			nodes[0]=element.previousSibling.previousSibling;
			nodes[1]=element.previousSibling;
			max=2;
		}
		if(href.match("irsz")!=null){
			cim=true;
			max=8;
			nodes[0]=element.nextSibling;
			for(i=1;i<max;i++){
				nodes[i]=nodes[i-1].nextSibling;
			}
		}
		if(href.match("melleklet")!=null){
			melleklet=true;
			nodes[0]=element.previousSibling;
			max=1;
		}
		if(href.match("illetek")!=null){
			illetek=true;
			nodes[0]=element.previousSibling;
			max=1;
		}
		for(i=0;i<max;i++){
			if(nodes[i].childNodes[0].childNodes[0].childNodes[2].childNodes[0].nodeName=="INPUT"){
						label=getValue(nodes[i].childNodes[0].childNodes[0].childNodes[1].childNodes[0]);
						if(label){
							if(label.match("TE")!=null||label.match("Megnevez")!=null||label.match("Irsz")!=null||label.match("Hely")!=null||label.match("Ker")!=null||label.match("ter")!=null||label.match("jelleg")!=null||label.match("Ter")!=null||label.match("Jelleg")!=null||label.match("illet")!=null||label.match("befizetett")!=null){
								if(first){
									ids+="fieldId[]="+nodes[i].id+"&fieldVal[]="+nodes[i].childNodes[0].childNodes[0].childNodes[2].childNodes[0].value;
			            first=false;
			          }
			          else{
			            ids+="&fieldId[]="+nodes[i].id+"&fieldVal[]="+nodes[i].childNodes[0].childNodes[0].childNodes[2].childNodes[0].value;
			          }
			          if(label.match("Megnevez")!=null){
			          	break;
			          }
			          //alert(ids);
			      	}
		      	}
	      	}
		}
		/*var irsz=
		var hely=irsz.nextSibling;
		var ker=hely.nextSibling;
		var ter=ker.nextSibling;
		var jel=ter.nextSibling;
		*/
		
		//alert(element);
		/*alert(parent.childNodes.length);
		for(i=0;i<parent.childNodes.length;i++){
			if(parent.childNodes[i]&&parent.childNodes[i].childNodes[0]&&parent.childNodes[i].childNodes[0].childNodes[0]){
				if(parent.childNodes[i]&&parent.childNodes[i].childNodes[0]&&parent.childNodes[i].childNodes[0].childNodes[0]&&parent.childNodes[i].childNodes[0].childNodes[0].childNodes[2]){
					if(parent.childNodes[i].childNodes[0].childNodes[0].childNodes[2].childNodes[0].nodeName=="INPUT"){
						label=getValue(parent.childNodes[i].childNodes[0].childNodes[0].childNodes[1].childNodes[0]);
						if(label){
							if(label.match("TE")!=null||label.match("Megnevez")!=null||label.match("Irsz")!=null||label.match("Hely")!=null||label.match("Ker")!=null||label.match("ter")!=null||label.match("jelleg")!=null){
								if(first){
									ids+="fieldId[]="+parent.childNodes[i].id+"&fieldVal[]="+parent.childNodes[i].childNodes[0].childNodes[0].childNodes[2].childNodes[0].value;
			            first=false;
			          }
			          else{
			            ids+="&fieldId[]="+parent.childNodes[i].id+"&fieldVal[]="+parent.childNodes[i].childNodes[0].childNodes[0].childNodes[2].childNodes[0].value;
			          }
			          //alert(ids);
			      	}
		      	}
	      	}
	    	}
    	}
    }*/
		  /*for(j=0;j<parent.childNodes[i].childNodes.length;j++){
		    for(k=0;k<parent.childNodes[i].childNodes[j].childNodes.length;k++){
          for(l=0;l<parent.childNodes[i].childNodes[j].childNodes[k].childNodes.length;l++){
            for(m=0;m<parent.childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes.length;m++){
              if(parent.childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].nodeName=="INPUT"){
                if(first){
                  ids+="fieldId[]="+parent.childNodes[i].id+"&fieldVal[]="+parent.childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].value;
                  first=false;
                }
                else{
                  ids+="&fieldId[]="+parent.childNodes[i].id+"&fieldVal[]="+parent.childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].value;
                }
              }
            }
          }
        }
      }*/
      /*window.open('adatlap_teaor.php?
      fieldId[]=id_779_0_id_781&fieldVal[]=' + document.getElementById('id_779_0_id_781').value + '&fieldId[]=id_779_0_id_780&fieldVal[]=' + document.getElementById('id_779_0_id_780').value + '', 
      '', 
      'location=0,status=1,scrollbars=1,width=640,height=480')
      document.writeln(parent.childNodes[i].childNodes[0].childNodes[0].nodeName);
    }*/
		window.open(href+ids,'', 'location=0,status=1,scrollbars=1,width=640,height=480');
		} else {
			location.href = href;
		}
	}
};
function XFMessage(id, binding, level, ifexpr, whileexpr) {
	this.binding = binding;
	this.id = id;
	this.level = level;
	this.init(ifexpr, whileexpr);
};

XFMessage.prototype = new XFAbstractAction;

XFMessage.prototype.run = function(element, ctx) {
	var text;

	if (this.binding) {
		var node = this.binding.evaluate(ctx)[0];

		if (node) {
			text = getValue(node);
		}
	} else {
		var e = IdManager.find(this.id);
		xforms.build(e, ctx);
		text = e.textContent || e.innerText;
	}

	if (text) {
		alert(text.trim());
	}
};
function XFSetindex(repeat, index, ifexpr, whileexpr) {
	this.repeat = repeat;
	this.index = XPath.get(index);
	this.init(ifexpr, whileexpr);
};

XFSetindex.prototype = new XFAbstractAction;

XFSetindex.prototype.run = function(element, ctx) {
	var repeat = IdManager.find(this.repeat);
	var index = numberValue(this.index.evaluate(ctx));
    DebugConsole.write("setIndex " + index);

	if (!isNaN(index)) {
		repeat.xfElement.setIndex(index);
	}
};
function XFSetvalue(binding, value, literal, ifexpr, whileexpr, conv) {
	this.binding = binding;
	this.value = value? XPath.get(value) : null;
	if(conv){
	 this.conv=conv;
  }
	this.literal = literal;
	this.init(ifexpr, whileexpr);
};

XFSetvalue.prototype = new XFAbstractAction;

XFSetvalue.prototype.run = function(element, ctx) {
	var node = this.binding.evaluate(ctx)[0];

	if (node) {
		var value = this.value? stringValue(this.value.evaluate(node))
			: this.literal;
		xforms.openAction();
		if(this.conv){
		  value=num2text(value);
    }
    if(value=="null"){
      value="";
    }
		setValue(node, value || "");
		node.ownerDocument.model.addChange(node);
		DebugConsole.write("Setvalue " + node.nodeName + " = " + value); 
		xforms.closeAction();
	}
};
function XFToggle(caseId, ifexpr, whileexpr) {
	this.caseId = caseId;
	this.ifexpr=ifexpr;
	this.init(ifexpr, whileexpr);
};

XFToggle.prototype = new XFAbstractAction;

XFToggle.prototype.run = function() {
	XFToggle.toggle(this.caseId, this.ifexpr);
};

XFToggle.toggle = function(caseId, ifexpr) {
	xforms.openAction();
	//document.write(ifexpr);
	var element = IdManager.find(caseId);
	var childs = element.parentNode.childNodes;
	var ul;
	var index = -1;

	if (childs.length > 0 && childs[0].nodeName.toLowerCase() == "ul") {
		ul = childs[0];
	}

	for (var i = ul != null? 1 : 0; i < childs.length; i++) {
		var child = childs[i];

		if (child == element) {
			index = i - 1;
		} else if (child.style.display != "none") {
			XMLEvents.dispatch(child, "xforms-deselect");
			child.style.display = "none";
                 
			if (ul != null) {
				Core.setClass(ul.childNodes[i - 1], "ajx-tab-selected", false);
			}
		}
	}

	if (element.style.display == "none") {
		element.style.display = "block";
		XMLEvents.dispatch(element, "xforms-select");
	    
		if (ul != null) {
			Core.setClass(ul.childNodes[index], "ajx-tab-selected", true);
		}
	}
	xforms.closeAction();
};
function AJXTree(id, binding) {
	this.init(id);
	this.binding = binding;
	this.hasBinding = true;
	this.root = this.element.getElementsByTagName("ul")[0];
	this.label = this.root.firstChild.cloneNode(true);
};

AJXTree.prototype = new XFElement;

AJXTree.prototype.dispose = function() {
	this.root = null;
	this.selected = null;
	XFElement.prototype.dispose.call(this);
};

AJXTree.prototype.build_ = function(ctx) {
	var node = this.binding.evaluate(ctx, this.depsNodesBuild, this.depsElements)[0];

	if (node) {
		var nodes = [];
		this.buildTree(this.root, 0, node, nodes);

		if (!this.selected || !inArray(this.selected, nodes)) {
			this.select(this.root.firstChild);
		}
	}
};

AJXTree.prototype.select = function(item) {
	var changed = true;
	var init = !!this.selected;

	if (init) {
		if (this.selected == item) {
			changed = false;
		} else {
			Core.setClass(this.selected, "xforms-tree-item-selected", false);
			Core.setClass(this.selected.childNodes[1], "xforms-tree-item-label-selected", false);
		}
	}
    
	if (changed) {
		this.element.node = item.node;
		this.selected = item;
		Core.setClass(item, "xforms-tree-item-selected", true);
		Core.setClass(item.childNodes[1], "xforms-tree-item-label-selected", true);
		xforms.openAction();
		xforms.addChange(this);
		xforms.addChange(item.node.ownerDocument.model);
		xforms.closeAction();
	}
};

AJXTree.prototype.click = function(target) {
	if (target.className == "xforms-tree-item-button") {
		var ul = target.nextSibling.nextSibling;

		if (ul != null) {
			ul.style.display = ul.style.display != "none"? "none" : "block";
		}
	} else if (Core.hasClass(target, "xforms-tree-item-label")) {
		this.select(target.parentNode);
	}
};

AJXTree.prototype.buildTree = function(parent, index, node, nodes) {
	var li = null;
	var ul = null;
	var label = null;
	var childs = node.childNodes;
	var nochild = childs.length == 0;
	nodes.push(node);

	if (parent.childNodes.length < index + 1) {
		li = this.label.cloneNode(true);
		parent.appendChild(li);
		XFRepeat.initClone(li);
	} else {
		li = parent.childNodes[index];
		var last = li.lastChild;
		
		if (last.nodeName.toLowerCase() == "ul") {
			ul = last;

			if (nochild) {
				xforms.dispose(ul);
				li.removeChild(ul);
			}
		}
	}

	li.node = node;
	Core.setClass(li, "xforms-tree-item-fork", !nochild);
	Core.setClass(li, "xforms-tree-item-leaf", nochild);

	for (var i = 0; i < childs.length; i++) {
		var child = childs[i];

		if (child.nodeType == NodeType.ELEMENT) {
			if (!ul) {
				ul = Core.createElement("ul", li);
			}

			this.buildTree(ul, i, child, nodes);
		}
	}

	if (ul) {		
		for (var i = ul.childNodes.length; i > childs.length; i--) {
			xforms.dispose(ul.lastChild);
			ul.removeChild(ul.lastChild);
		}
	}
};

AJXTree.prototype.refresh = function() {
};function XFControl() {
	this.isControl = true;
};

XFControl.prototype = new XFElement;

XFControl.prototype.initFocus = function(element, principal) {
	if (principal) {
		this.focusControl = element;
	}

	Event.attach(element, "focus", XFControl.focusHandler);
	Event.attach(element, "blur", XFControl.blurHandler);
};

XFControl.prototype.dispose = function() {
	this.focusControl = null;
	XFElement.prototype.dispose.call(this);
};

XFControl.prototype.focus = function(focusEvent) {
	if (this.isOutput) {
		return;
	}

	if (xforms.focus != this) {
		xforms.openAction();
		xforms.blur(true);
		xforms.focus = this;
		Core.setClass(this.element, "xforms-focus", true);
		var parent = this.element.parentNode;
	
		while (parent.nodeType == NodeType.ELEMENT) {
			if (typeof parent.node != "undefinned"
				&& Core.hasClass(parent, "xforms-repeat-item")) {
				XFRepeat.selectItem(parent);
			}
	
			parent = parent.parentNode;
		}
		
		XMLEvents.dispatch(xforms.focus, "DOMFocusIn");
		xforms.closeAction();
		
		if (this.full && !focusEvent) { // select full
			this.focusFirst();
		}
	}

	var fcontrol = this.focusControl;
	xforms.posibleBlur = false;
	
	if (fcontrol && !focusEvent) {
		var control = this.focusControl;
		var name = control.nodeName.toLowerCase();

		if (control != document.activeElement) {
			control.focus();
			
			if (Core.isOpera) {
				control.focus();
			}
		}

		if (name == "input" || name == "textarea") {
			control.select();
		}
	}
};

XFControl.prototype.build_ = function(ctx) {
	var result = this.binding.evaluate(ctx, this.depsNodesBuild, this.depsElements);

	if (typeof result == "object") {
		var node = result[0];
		var element = this.element;
		var old = element.node;

		if (old != node || !xforms.ready) {
			element.node = node;
			this.nodeChanged = true;
		}
		
		if (node) {
			this.depsNodesRefresh.push(node);
		}
	} else {
		this.outputValue = result;
	}
};

XFControl.prototype.refresh = function() {
	var element = this.element;
	var node = element.node;

    if (node) {
		var value = getValue(node, true);
		xforms.openAction();
		var changed = value != this.currentValue;
		
		if (this.relevant) {
			Core.setClass(element, "xforms-disabled", false);
		}

		this.changeProp(node, "required", "xforms-required", "xforms-optional", changed);
		this.changeProp(node, "relevant", "xforms-enabled", "xforms-disabled", changed);
		this.changeProp(node, "readonly", "xforms-readonly", "xforms-readwrite", changed);
		this.changeProp(node, "valid", "xforms-valid", "xforms-invalid", changed);

		if (changed) {
			this.currentValue = value;
			this.setValue(value);

			if (!this.nodeChanged && !this.isTrigger) {
				XMLEvents.dispatch(element, "xforms-value-changed");
			}
		}

		xforms.closeAction();
	} else if (this.outputValue != null) {
		this.setValue(this.outputValue);
    } else {
		Core.setClass(element, "xforms-disabled", !this.hasValue);
    }
    
    this.nodeChanged = false;
};

XFControl.prototype.changeProp = function(node, prop, onTrue, onFalse, changed) {
	var value = node[prop];

	if (changed || value != this[prop]) {
		if (!this.nodeChanged && !this.isTrigger) {
			XMLEvents.dispatch(this.element, value? onTrue : onFalse);
		}

		Core.setClass(this.element, onTrue, value);
		Core.setClass(this.element, onFalse, !value);
		this[prop] = value;
		
		if(prop == "readonly" && this.changeReadonly) {
			this.changeReadonly();
		}
	}	
};

XFControl.prototype.valueChanged = function(value) {
	var node = this.element.node;
	var model = node.ownerDocument.model;

	if (value != null && value.length > 0 && node.type.parse) {
		try { value = node.type.parse(value); } catch(e) { }
	}

	if (value != getValue(node)) {
		xforms.openAction();
		setValue(node, value);
		model.addChange(node);
		XMLEvents.dispatch(model, "xforms-recalculate");
		xforms.refresh();
		xforms.closeAction();
	}
};

XFControl.getXFElement = function(element) {
	var xf = null;

	while (!xf && element) {
		xf = element.xfElement;

		if (xf && !xf.isControl) {
			xf = null;
		}

		element = element.parentNode;
	}

	return xf;
};

XFControl.focusHandler = function() {
	var xf = XFControl.getXFElement(this);

	if (xforms.focus != xf) {
		xf.focus(true);
	} else {
		xforms.posibleBlur = false;
	}
};

XFControl.blurHandler = function() {
	if (XFControl.getXFElement(this) == xforms.focus) {
		xforms.posibleBlur = true;
		setTimeout("xforms.blur()", 200);
	}
};
function XFElement() {
};

XFElement.prototype.initSelect = function(id) {
	this.element = $(id);
	this.element.xfElement = this;
	this.depsElements = [];
	this.depsNodesBuild = [];
	this.depsNodesRefresh = [];
	XMLEvents.dispatch(this, "xforms-refresh");
  //XFDispatch(xforms-refresh, this.element, null, null);
};

XFElement.prototype.init = function(id) {
	this.element = $(id);
	this.element.xfElement = this;
	this.depsElements = [];
	this.depsNodesBuild = [];
	this.depsNodesRefresh = [];
	/*if(){
    XMLEvents.dispatch(this, "xforms-refresh");
  }*/
  //XFDispatch(xforms-refresh, this.element, null, null);
};

XFElement.prototype.dispose = function() {
	if(this.element!=null)
	this.element.xfElement = null;
	this.element = null;
	this.depsElements = null;
	this.depsNodesBuild = null;
	this.depsNodesRefresh = null;
};

XFElement.prototype.build = function(ctx) {
	if (this.hasBinding) {
		var deps = this.depsElements;
		var depsN = this.depsNodesBuild;
		var depsR = this.depsNodesRefresh;
		var build = !xforms.ready || (deps.length == 0) || ctx != this.ctx;
		var refresh = false;
		var changes = xforms.changes;

		for (var i = 0; !build && i < deps.length; i++) {
			var el = deps[i];

			for (var j = 0; !build && j < changes.length; j++) {
				if (el == changes[j]) {
					if (el.instances) { //model
						if (el.rebuilded) {
							build = true;
						} else {
							for (var k = 0; !build && k < depsN.length; k++) {
								build = inArray(depsN[k], el.nodesChanged);
							}

							if (!build) {
								for (var k = 0; k < depsR.length; k++) {
									refresh = inArray(depsR[k], el.nodesChanged);
								}
							}
						}
					} else {
						build = true;
					}
				}
			}
		}

		this.changed = build || refresh;

		if (build) {
			depsN.length = 0;
			depsR.length = 0;
			deps.length = 0;
			this.ctx = ctx;
			this.build_(ctx);
		}
	} else {
		this.element.node = ctx;
	}
};
function XFGroup(id, binding) {
	this.init(id);

	if (binding) {
		this.hasBinding = true;
		this.binding = binding;
	}
};

XFGroup.prototype = new XFElement;

XFGroup.prototype.clone = function(id) { 
	return new XFGroup(id, this.binding);
};

XFGroup.prototype.build_ = function(ctx) {
	var nodes = this.binding.evaluate(ctx, this.depsNodesBuild, this.depsElements);
	this.element.node = nodes[0];
	this.depsNodesRefresh.push(nodes[0]);
};

XFGroup.prototype.refresh = function() {
	var element = this.element;
	var disabled = !element.node || !element.node.relevant;
	Core.setClass(element, "xforms-disabled", disabled);

	/** Tabs */
	var ul = element.parentNode.firstChild;
	
	if (ul.nodeName.toLowerCase() == "ul") {
		var childs = element.parentNode.childNodes;
		var tab;

		for (var i = 1; i < childs.length; i++) {
			if (childs[i] == element) {
				tab = ul.childNodes[i - 1];
			}
		}

		Core.setClass(tab, "xforms-disabled", disabled);
	}
};
function XFInput(id, binding, inputmode, incremental, aidButton, clone) {
	this.init(id);
	this.binding = binding;
	this.inputmode = typeof inputmode == "string"? XFInput.InputMode[inputmode] : inputmode;
	this.incremental = incremental;
	var cells = this.element.rows[0].cells;
	this.cell = cells[cells.length - 2];
	this.isClone = clone;
	this.hasBinding = true;
	this.type;
	this.bolAidButton = aidButton;

	if (aidButton) {
		this.aidButton = cells[cells.length - 1].firstChild;
		this.initFocus(this.aidButton);
	}
};

XFInput.prototype = new XFControl;

XFInput.prototype.clone = function(id) { 
	return new XFInput(id, this.binding, this.inputmode, this.incremental, this.bolAidButton, true);
};

XFInput.prototype.dispose = function() {
	this.cell = null;
	this.calendarButton = null;
	XFControl.prototype.dispose.call(this);
};

XFInput.prototype.initInput = function(type) {
	var cell = this.cell;
	var input = cell.firstChild;
	var clase = type["class"];

	if (input.type == "password" || input.nodeName.toLowerCase() == "textarea") {
		this.type = Schema.getType("xsd_:string");
		this.initEvents(input);
	} else if (type != this.type) {
		this.type = type;

		for (; cell.firstChild; cell.removeChild(cell.firstChild));

		if (clase == "boolean") {
			input = Core.createElement("input");
			input.type = "checkbox";
			cell.appendChild(input);
		} else {
			input = Core.createElement("input", cell, null, "xforms-value");
			this.initEvents(input);

			if (clase == "date" || clase == "datetime") {
				this.calendarButton = Core.createElement("button", cell, "", "aid-button");
				this.initFocus(this.calendarButton);
			} else if (clase == "number") {
				input.style.textAlign = "right";
			}

			var max = type.getMaxLength();
			var length = type.getDisplayLength();

			if (max) {
				input.maxLength = max;
			}
			
			if (length) {
				input.size = length;
			}
		}
	}

	this.initFocus(input, true);
	this.input = input;
};

XFInput.prototype.setValue = function(value) {
	var node = this.element.node;
	var type = node.type;
	
	if (!this.input || type != this.type) {
		this.initInput(type);
		this.changeReadonly();
	}

	if (type["class"] == "boolean") {
		this.input.checked = value == "true";
	} else {
		this.input.value = value || "";
	}
};

XFInput.prototype.changeReadonly = function() {
	if (this.input) {
		this.input.disabled = this.readonly;

		if (this.calendarButton) {
			this.calendarButton.style.display = this.readonly? "none" : "";
		}
	}
};

XFInput.prototype.initEvents = function(input) {
	if (this.incremental) {
		Event.attach(input, "keyup", XFInput.keyUpIncremental);
	} else if (this.inputmode) {
		Event.attach(input, "keyup", XFInput.keyUpNormal);
	}
};

XFInput.prototype.blur = function(target) {
	if (!this.incremental) {
		var input = this.input;
		assert(input, this.element.id);
		var value = input.type == "checkbox"? (input.checked? "true" : "false") : input.value;
		this.valueChanged(value)
	}
};

XFInput.prototype.click = function(target) {
	if (target == this.aidButton) {
		xforms.openAction();
		XMLEvents.dispatch(this, "ajx-aid");
		xforms.closeAction();
	} else if (target == this.input && this.type["class"] == "boolean" && this.incremental) {
		this.valueChanged(target.checked? "true" : "false");
	} else if (target == this.calendarButton) {
		Calendar.show(target.previousSibling, this.type["class"] == "datetime"? Calendar.SECONDS : Calendar.ONLY_DATE);
	}
};

XFInput.keyUpNormal = function() {
	var xf = XFControl.getXFElement(this);
	this.value = xf.inputmode(this.value);
};

XFInput.keyUpIncremental = function() {
	var xf = XFControl.getXFElement(this);

	if (xf.inputmode != null) {
		this.value = xf.inputmode(this.value);
	}

	xf.valueChanged(this.value || "");
};

XFInput.InputMode = {
	lowerCase : function(value) { return value.toLowerCase(); },
	upperCase : function(value) { return value.toUpperCase(); },
	titleCase : function(value) {
		return value.charAt(0).toUpperCase() + value.substring(1).toLowerCase();
	},
	digits : function(value) {
		if (/^[0-9]*$/.exec(value) != null) {
			return value;
		} else {
			alert("Ide csak számot írhat!");
			var digits = "1234567890";
			var newValue = "";

			for (var i = 0; i < value.length; i++) {
				if (digits.indexOf(value.charAt(i)) != -1) {
					newValue += value.charAt(i);
				}
			}

			return newValue;
		}
	}
};
function XFItem(id, bindingL, bindingV) {
	this.init(id);

	if (bindingL || bindingV) {
		this.hasBinding = true;
		this.bindingL = bindingL;
		this.bindingV = bindingV;
	}

	var element = this.element;

	if (element.nodeName.toLowerCase() != "option") {
		this.input = element.getElementsByTagName("input")[0];
		this.input.name = XFControl.getXFElement(this.element).element.id;
		Event.attach(this.input, "focus", XFControl.focusHandler);
		Event.attach(this.input, "blur", XFControl.blurHandler);
		this.label = element.getElementsByTagName("span")[0];
	}
};

XFItem.prototype = new XFElement;

XFItem.prototype.clone = function(id) { 
	return new XFItem(id, this.bindingL, this.bindingV);
};

XFItem.prototype.dispose = function() {
	this.input = null;
	this.label = null;
	XFElement.prototype.dispose.call(this);
};

XFItem.prototype.build_ = function(ctx) {
	var element = this.element;
	var xf = element.parentNode.xfElement;

	if (xf && xf.isRepeat) {
		ctx = element.node;
	} else {
		element.node = ctx;
	}

	if (this.bindingL) {
		element.nodeL = this.bindingL.evaluate(ctx, this.depsNodesBuild, this.depsElements)[0];
		this.depsNodesRefresh.push(element.nodeL);
	}

	if (this.bindingV) {
		element.nodeV = this.bindingV.evaluate(ctx, this.depsNodesBuild, this.depsElements)[0];
		this.depsNodesRefresh.push(element.nodeV);
	}
};

XFItem.prototype.refresh = function() {
	var element = this.element;

	if (element.nodeName.toLowerCase() == "option") {
		if (element.nodeL) {
			try { element.text = getValue(element.nodeL, true) } catch(e) { }
		}

		if (element.nodeV) {
			try { element.value = getValue(element.nodeV) } catch(e) { }
		}
	} else {
		if (element.nodeL) {
			setValue(this.label, getValue(element.nodeL, true));
		}

		if (element.nodeV) {
			this.input.value = getValue(element.nodeV);
		}
	}
};

XFItem.prototype.click = function (target) {
	var input = this.input;
	//if(Core.isIE || Core.isChrome){
		XMLEvents.dispatch(this, "DOMActivate");
	//}
	if (input) {
		var xf = XFControl.getXFElement(this.element);
		
		if (!xf.element.node.readonly) {
			if (target != input) {
				if (input.type != "radio" || !input.checked) {
					input.checked = !input.checked;
					input.focus();
				}
			}

			xf.itemClick(input.value);
		}
	}
};
function XFItemset(id, nodesetBinding, labelBinding, valueBinding) {
	this.init(id);
	this.nodesetBinding = nodesetBinding;
	this.labelBinding = labelBinding;
	this.valueBinding = valueBinding;
	this.hasBinding = true;
};

XFItemset.prototype = new XFElement;

XFItemset.prototype.build_ = function(ctx) {
	if (this.element.getAttribute("cloned")) return;

	this.nodes = this.nodesetBinding.evaluate(ctx, this.depsNodesBuild, this.depsElements);	
	var next = this.element;
	var parentNode = next.parentNode;
	var length = this.nodes.length;
	var oldNode = next;
	
	for (var cont = 1; true;) {
		next = next.nextSibling;
	
		if (next && next.getAttribute("cloned")) {
			if (cont >= length) {
				parentNode.removeChild(next);
				next = oldNode;
			} else {
				this.refresh_(next, cont++);
				oldNode = next;
			}
		} else {
			for (var i = cont; i < length; i++) {
				var node = this.element.cloneNode(true);
				node.setAttribute("cloned", "true");
				node.id = null;
				parentNode.appendChild(node);
				this.refresh_(node, i);
			}
	
			break;
		}
	}
	
	if (length > 0) {
		this.refresh_(this.element, 0);
	}
};

XFItemset.prototype.refresh = function() {
	Core.setClass(this.element, "xforms-disabled", this.nodes.length == 0);	
};

XFItemset.prototype.clone = function(id) {
	return new XFItemset(id, this.nodesetBinding, this.labelBinding, this.valueBinding);
};

XFItemset.prototype.refresh_ = function(element, cont) {
	var ctx = this.nodes[cont];
	var nodeLabel = this.labelBinding.evaluate(ctx, this.depsNodesBuild, this.depsElements)[0];
	var nodeValue = this.valueBinding.evaluate(ctx, this.depsNodesBuild, this.depsElements)[0];

	if (nodeLabel) {
		this.depsNodesRefresh.push(nodeLabel);

	    try { element.text = getValue(nodeLabel, true); } catch(e) { }
	}

	if (nodeValue) {
		this.depsNodesRefresh.push(nodeValue);
	    try { element.value = getValue(nodeValue); } catch(e) { }
	}
};

function XFLabel(id, binding) {
	this.init(id);

	if (binding) {
		this.hasBinding = true;
		this.binding = binding;
	}
};

XFLabel.prototype = new XFElement;

XFLabel.prototype.clone = function(id) { 
	return new XFLabel(id, this.binding);
};

XFLabel.prototype.build_ = function(ctx) {
	var nodes = this.binding.evaluate(ctx, this.depsNodesBuild, this.depsElements);
	this.element.node = nodes[0];
	this.depsNodesRefresh.push(nodes[0]);
};

XFLabel.prototype.refresh = function() {
	var node = this.element.node;
	var value = node? getValue(node, true) : "";
    setValue(this.element, value);
};
function XFOutput(id, binding) {
	this.init(id);

	if (this.element.rows) {
		var cells = this.element.rows[0].cells;
		this.valueElement = cells[cells.length - 2].firstChild;
	} else {
		this.valueElement = this.element.firstChild;
	}
	
	this.hasBinding = true;
	this.binding = binding;
	this.isOutput = true;
};

XFOutput.prototype = new XFControl;

XFOutput.prototype.clone = function(id) { 
	return new XFOutput(id, this.binding);
};

XFOutput.prototype.dispose = function() {
	this.valueElement = null;
	XFControl.prototype.dispose.call(this);
};

XFOutput.prototype.setValue = function(value) {
	var node = this.element.node;
	var element = this.valueElement;
    
    if (element.nodeName.toLowerCase() == "span") {
        setValue(element, value);
    } else {
        element.src = value;
    }
};
function XFRepeat(id, binding, clone) {
	this.init(id);
	this.binding = binding;
	this.index = 1;
	var el = this.element;
	this.isRepeat = true;
	this.hasBinding = true;
	this.root = el.nodeName.toLowerCase() == "table"? el.lastChild : el;
	this.isItemset = Core.hasClass(el, "xforms-itemset");
};

XFRepeat.prototype = new XFElement;

XFRepeat.prototype.dispose = function() {
	this.root = null;
	XFElement.prototype.dispose.call(this);
};

XFRepeat.prototype.setIndex = function(index) {
	if (this.index != index) {
		var node = this.nodes[index - 1];
        
        if (node) {    
			xforms.openAction();
			this.index = index;
			this.element.node = node;
			xforms.addChange(this);
			xforms.addChange(node.ownerDocument.model);
			xforms.closeAction();
		}
	}
};

XFRepeat.prototype.deleteNode = function(node) {
	var newNodes = [];
	var nodes = this.nodes;
	
	for (var i = 0; i < nodes.length; i++) {
		if (node != nodes[i]) {
			newNodes.push(nodes[i]);
		}
	}

	this.nodes = newNodes;
	this.setIndex(this.index == nodes.length? this.index - 1 : this.index);
};

XFRepeat.prototype.insertNode = function(node, nodeAfter) {
	var nodes = this.nodes;

	if (nodeAfter) {
		var newNodes = [];
		var index = 1;

		for (var i = 0; i < nodes.length; i++) {
			if (nodeAfter == nodes[i]) {
				newNodes.push(node);
				index = i + 1;
			}
			
			newNodes.push(nodes[i]);
		}

		this.nodes = newNodes;
		this.setIndex(index);
	} else {
		nodes.push(node);
		this.setIndex(nodes.length);
	}
};

XFRepeat.prototype.build_ = function(ctx) {
	var nodes = this.binding.evaluate(ctx, this.depsNodesBuild, this.depsElements);
	var r = this.root;
	var l = r.childNodes.length;
	this.nodes = nodes;
	var n = nodes.length;

	for (var i = l; i < n; i++) {
		var child = r.firstChild.cloneNode(true);
		r.appendChild(child);
		XFRepeat.initClone(child);
	}

	for (var i = n; i < l && r.childNodes.length > 1; i++) {
		xforms.dispose(r.lastChild);
		r.removeChild(r.lastChild);
	}

	for (var i = 0; i < n; i++) {
		nodes[i].repeat = this;
		r.childNodes[i].node = nodes[i];
	}

	if (this.index > n) {
		this.index = 1;
	}
    
	this.element.node = nodes[this.index - 1];
};

XFRepeat.prototype.refresh = function(selected) {
	var empty = this.nodes.length == 0;
	Core.setClass(this.element, "xforms-disabled", empty);

	if (!empty && !this.isItemset) {
		var childs = this.root.childNodes;

		for (var i = 0; i < childs.length; i++) {
			var sel = selected && (this.index == i + 1);
			childs[i].selected = sel;
			Core.setClass(childs[i], "xforms-repeat-item-selected", sel);
		}
	}
};

XFRepeat.prototype.clone = function(id) { 
	return new XFRepeat(id, this.binding, true);
};

XFRepeat.initClone = function(element) {
	var id = element.id;
  
	if (id) {
		IdManager.cloneId(element);
		var oldid = element.getAttribute("oldid");
		var original = $(oldid);
		var xf = original.xfElement;

		if (xf) {
			xf.clone(element.id);
		}
		
		var listeners = original.listeners;
	
		if (listeners && !Core.isIE) {
			for (var i = 0; i < listeners.length; i++) {
				listeners[i].clone(element);
			}
		}
	}

	var next = element.firstChild;
	
	while (next) {
		var child = next;
		next = next.nextSibling;

		if (child.id && child.getAttribute("cloned")) {
			element.removeChild(child);
		} else {
			XFRepeat.initClone(child);
		}
	}
};

XFRepeat.selectItem = function(element) {
	var par = element.parentNode;

	if (par) {
		var repeat = par.xfElement? par : par.parentNode;
		var childs = par.childNodes;
		assert(repeat.xfElement, element.nodeName +  " - " + repeat.nodeName);
		for (var i = 0; i < childs.length; i++) {
			if (childs[i] == element) {
				repeat.xfElement.setIndex(i + 1);
				break;
			}
		}
	}
};
function XFSelect(id, multiple, full, binding, incremental, clone) {
	this.initSelect(id);
	this.binding = binding;
	this.multiple = multiple;
	this.full = full;
	this.incremental = incremental;
	this.isClone = clone;
	this.hasBinding = true;
    
	if (!this.full) {
		this.select = this.element.getElementsByTagName("select")[0];
		this.initFocus(this.select);

		Event.attach(this.select, "change",
			incremental? XFSelect.incrementalChange : XFSelect.normalChange);
	}
	//XMLEvents.dispatch(this, "xforms-refresh");
};

XFSelect.prototype = new XFControl;

XFSelect.prototype.clone = function(id) { 
	return new XFSelect(id, this.multiple, this.full, this.binding, this.incremental, true);
};

XFSelect.prototype.dispose = function() {
	this.select = null;
	this.selectedOptions = null;
	XFControl.prototype.dispose.call(this);
};

XFSelect.prototype.focusFirst = function() {
	var input = this.element.getElementsByTagName("input")[0];
	input.focus();

	if (Core.isOpera) {
		input.focus();
	}
};

XFSelect.prototype.setValue = function(value) {
	var vals = value? value.split(" ") : [""];
	var list = this.full? this.element.getElementsByTagName("input") : this.select.options;
	var well = true;
    
	for (var i = 0; well && i < vals.length; i++) {
		var val = vals[i];
		var finded = false;
            
		for (var j = 0; !finded && j < list.length; j++) {
			if (list[j].value == val) {
				finded = true;
			}
		}

		well = finded;
	}

	if (well || (this.multiple && !value)) {
		if (this.outRange) {
			this.outRange = false;
			XMLEvents.dispatch(this, "xforms-in-range");
		}
	} else if ((!this.multiple || value) && !this.outRange) {
		this.outRange = true;
		XMLEvents.dispatch(this, "xforms-out-of-range");
	}

	vals = this.multiple? vals : [vals[0]];
	var readonly = this.element.node.readonly;

	if (this.full) {
		for (var i = 0; i < list.length; i++) {
			var item = list[i];
			item.checked = inArray(item.value, vals);
		}
	} else {
		for (var i = 0; i < list.length; i++) {
			var item = list[i];
			try { item.selected = inArray(item.value, vals); } catch(e) { };
		}
	}
};

XFSelect.prototype.changeReadonly = function() {
	if (this.full) {
		var list = this.element.getElementsByTagName("input");

		for (var i = 0; i < list.length; i++) {
			list[i].disabled = this.readonly;
		}
	} else {
		this.select.disabled = this.readonly;
	}
};

XFSelect.prototype.itemClick = function(value) {
	var inputs = this.element.getElementsByTagName("input");
	xforms.openAction();

	if (this.multiple) {
		var newValue = null;
		
		for (var i = 0; i < inputs.length; i++) {
			var input = inputs[i];

			if (input.value == value) {
				XMLEvents.dispatch(input.parentNode, input.checked? "xforms-select" : "xforms-deselect");
			}
			
            if (input.checked) {
                newValue = (newValue? newValue + " " : "") + input.value;
            }
		}

		value = newValue;
	} else {
		var old = this.value || getValue(this.element.node);
		var inputSelected = null;

		if (old == value) return;

		for (var i = 0; i < inputs.length; i++) {
			var input = inputs[i];
			input.checked = input.value == value;
			
			if (input.value == old) {
				XMLEvents.dispatch(input.parentNode, "xforms-deselect");
			} else if (input.value == value) {
				var inputSelected = input;
			}
		}
		
		XMLEvents.dispatch(inputSelected.parentNode, "xforms-select");
	}

	if (this.incremental) {
		this.valueChanged(value || "");
	} else {
		this.value = value || "";
	}
	
	xforms.closeAction();
};

XFSelect.prototype.blur = function(evt) {
	if (this.value != null) {
		xforms.openAction();
		this.valueChanged(this.value);
		xforms.closeAction();
		this.value = null;
	}
};

XFSelect.normalChange = function(evt) {
	var xf = XFControl.getXFElement(this);
	var news = [];
	var value = "";
	var old = xf.getSelected();
	var opts = this.options;
	xforms.openAction();
	
	for (var i = 0; i < old.length; i++) {
		if (old[i].selected) {
			news.push(old[i]);
		} else {
			XMLEvents.dispatch(old[i], "xforms-deselect");
		}
	}
	
	for (var i = 0; i < opts.length; i++) {
		var opt = opts[i];
	    
		if (opt.selected) {
			value = value? value + " " + opt.value : opt.value;
	
			if (!inArray(opt, news)) {
				news.push(opt);
				XMLEvents.dispatch(opt, "xforms-select");
			}
		}
	}

	xf.value = value;	
	xf.selectedOptions = news;
	xforms.closeAction();
};

XFSelect.incrementalChange = function(evt) {
	var xf = XFControl.getXFElement(this);
	xforms.openAction();
	XFSelect.normalChange.call(this, evt);
	xf.valueChanged(xf.value);
	xforms.closeAction();
};

XFSelect.prototype.getSelected = function() {
    var s = this.selectedOptions;

    if (!s) {
        s = [];
        var opts = this.select.options;

        for (var i = 0; i < opts.length; i++) {
            if (opts[i].selected) s.push(opts[i]);
        }
    }
    
    return s;
};
function XFTrigger(id, binding, clone) {
	this.init(id);
	this.binding = binding;
	this.hasBinding = !!binding;
	this.isTrigger = true;
	var button = this.element.getElementsByTagName("a")[0]
		|| this.element.getElementsByTagName("button")[0];
	this.initFocus(button);
};

XFTrigger.prototype = new XFControl;

XFTrigger.prototype.setValue = function () { };

XFTrigger.prototype.clone = function (id) {
	return new XFTrigger(id, this.binding, true);
};

XFTrigger.prototype.click = function () {
	xforms.openAction();
	if(Core.isIE){
		XMLEvents.dispatch(this, "DOMActivate");
	}
	xforms.closeAction();
};

XFTrigger.prototype.blur = function () { };
function AJXTimer(id, time) {
	this.init(id, null, "xforms-timer");
	this.running = false;
	this.time = time;
};

AJXTimer.prototype = new XFCoreElement;

AJXTimer.prototype.start = function() {
	this.running = true;
	var timer = this;
	setTimeout(function() { timer.run() }, this.time);
};

AJXTimer.prototype.stop = function() {
	this.running = false;
};

AJXTimer.prototype.run = function() {
	if (this.running) {
		var timer = this;
		xforms.openAction();
		XMLEvents.dispatch(timer.element, "ajx-time");
		xforms.closeAction();
		setTimeout(function() { timer.run() }, this.time);
	}
};
function XFBind(id, parent, nodeset, type, readonly, required, relevant, calculate, constraint) {
	this.init(id, parent, "xforms-bind");
	this.model = parent.model || parent;
	this.type = type? Schema.getType(type) : null;
	this.nodeset = nodeset;
	this.readonly = XPath.get(readonly);
	this.required = XPath.get(required);
	this.relevant = XPath.get(relevant);
	this.calculate = XPath.get(calculate);
	this.constraint = XPath.get(constraint);
	this.depsNodes = [];
	this.depsElements = [];
	this.nodes = [];
	this.binding = new Binding(this.nodeset);
	parent.addBind(this);
};

XFBind.prototype = new XFCoreElement;

XFBind.prototype.addBind = function() {};

XFBind.prototype.refresh = function(ctx, index) {
	if (!index) {
		this.depsNodes.length = 0;
		this.depsElements.length = 0;
		this.nodes.length = 0;
	}

	ctx = ctx || this.model.getInstanceDocument();
	copyArray(this.binding.evaluate(ctx, this.depsNodes, this.depsElements), this.nodes);
	var el = this.element;

	for (var i = 0; i < this.nodes.length; i++) {
		var node = this.nodes[i];

		if (node.bind && node.bind != this) {
			XFProcessor.error(el, "xforms-binding-exception", "Two binds affect one node");
		} else {
			node.bind = this;

			if (this.type) {
				if (node.schemaType) {
					XFProcessor.error(el, "xforms-binding-exception", "Type especified in xsi:type attribute");
				} else {
					node.type = this.type;
				}
			}
		}

		for (var j = 0; j < el.childNodes.length; j++) {
			el.childNodes[j].xfElement.refresh(node, i);
		}
	}
};

XFBind.prototype.recalculate = function() {
	var el = this.element;

	if (this.calculate) {
		for (var i = 0; i < this.nodes.length; i++) {
			var node = this.nodes[i];
			var ctx = new ExprContext(node, i + 1, this.nodes);
			var value = stringValue(this.calculate.evaluate(ctx));
			value = node.type.normalize(value);
			setValue(node, value);
			this.model.addChange(node);
			DebugConsole.write("Calculate " + node.nodeName + " " + value);
		}
	}

	for (var j = 0; j < el.childNodes.length; j++) {
		el.childNodes[j].xfElement.recalculate();
	}
};
function XFCoreElement() {
};

XFCoreElement.prototype.init = function(id, parent, className) {
	parent = parent? parent.element : document.getElementsByTagName("head")[0];
	this.element = Core.createElement("span", parent, null, className);
	this.element.id = id;
	this.element.xfElement = this;
};

XFCoreElement.prototype.dispose = function() {
	this.element.xfElement = null;
	this.element = null;
	this.model = null;
};
function XFInstance(id, model, src, srcXML) {
	this.init(id, model, "xforms-instance");
	this.src = src;
	this.srcXML = srcXML;
	this.model = model;
	model.addInstance(this);
};

XFInstance.prototype = new XFCoreElement;
 
XFInstance.prototype.dispose = function() {
	XFCoreElement.prototype.dispose.call(this);
	XNode.recycle(this.old);
	XNode.recycle(this.doc);
};

XFInstance.prototype.construct = function() {
    if (!xforms.ready) {
	    if (this.src) {
	        try {
	            var req = Core.openRequest("get", this.src, false);
	            DebugConsole.write("Loading " + this.src);
	            req.send(null);
	
	            if (req.status != 200 && req.status != 0) {
	            	throw "Request error: " + req.status;
	        	}
	        	
				if(req.responseText=="auth_error"){
					alert("Lejárt a biztonsági időkorlát (session)! Kérjük lépjen be újra!");
					window.location="../../belepes.php";
				}
				this.setDoc(req.responseText);
	        } catch(e) {
                xforms.error(this.element, "xforms-link-exception",
                    "Fatal error loading " + this.src, e.toString());
	        }
	    } else {
		   	this.setDoc(xmlResolveEntities(this.srcXML));
		}
	}
};

XFInstance.prototype.reset = function() {
	this.setDoc(this.old, true);
};
        
XFInstance.prototype.store = function(isReset) {
	if (this.old && !isReset) {
		XNode.recycle(this.old);
		this.old.model = null;
	}

    this.old = this.doc.cloneNode(true);
};

XFInstance.prototype.setDoc = function(doc, isReset) {
	this.doc = typeof doc == "string"? XDocument.parse(doc) : doc;
	this.doc.model = this.model;
    this.store(isReset);
};
        
XFInstance.prototype.revalidate = function() {
    this.validation_(this.doc.documentElement);
};

XFInstance.prototype.validation_ = function(node, readonly, relevant) {
    if (readonly == null) readonly = false;
    if (relevant == null) relevant = true;

    this.validate_(node, readonly, relevant);
    readonly = node.readonly;
    relevant = node.relevant;
    var atts = node.attributes;

	if (atts) {
	    for (var i = 0; i < atts.length; i++) {
	        this.validation_(atts[i], readonly, relevant);
	    }
	}
   
    for (var i = 0; i < node.childNodes.length; i++) {
        var child = node.childNodes[i];

        if (child.nodeType == NodeType.ELEMENT) {
            this.validation_(child, readonly, relevant);
        }
    }
};

XFInstance.prototype.validate_ = function(node, readonly, relevant) {
    var bind = node.bind;
    var value = xmlValue(node);

    if (bind) {
        var nodes = bind.nodes;
        var i = 0;
        
        for (; i < nodes.length; i++) {
            if (nodes[i] == node) {
                break;
            }
        }

        var ctx = new ExprContext(node, i, nodes);

        if (bind.required) {
            this.setProperty_(node, "required", booleanValue(bind.required.evaluate(ctx)));
        }

        this.setProperty_(node, "relevant", relevant && (bind.relevant? booleanValue(bind.relevant.evaluate(ctx)) : true));
        this.setProperty_(node, "readonly", readonly || (bind.readonly? booleanValue(bind.readonly.evaluate(ctx)) : false));

        this.setProperty_(node, "valid",
            !node.relevant || (!node.required && (!value || value == ""))
            || (value && node.type.validate(value) 
                 && (!bind.constraint || booleanValue(bind.constraint.evaluate(ctx)))));
    } else {
        this.setProperty_(node, "relevant", relevant);
        this.setProperty_(node, "readonly", readonly);
        this.setProperty_(node, "valid", !value || node.type.validate(value));
    }
};

XFInstance.prototype.setProperty_ = function (node, property, value) {
    if (node[property] != value) {
        node[property] = value;
        this.model.addChange(node);   
    }
};function XFModel(id, schemas) {
	this.init(id, null, "xforms-model");
	this.instances = {};
	this.binds = [];
	this.nodesChanged = [];
	this.newNodesChanged = [];
	this.schemas = [];
	this.defaultInstance = null;
	xforms.models.push(this);
	xforms.defaultModel = xforms.defaultModel || this;
  //document.write(schemas);
	if (schemas) {
		schemas = schemas.split(" ");

		for (var i = 0; i < schemas.length; i++) {
			var founded = false;
			for (var id in Schema.all) {
				var schema = Schema.all[id];
				if (schema.name == schemas[i]) {
					this.schemas.push(schema);
					founded = true;
					break;
				}
			}
			
			if (!founded) {
				xforms.error(this, "xforms-link-exception", "Schema " + schemas[i] + " not found");
			}
		}
	}
};

XFModel.prototype = new XFCoreElement;

XFModel.prototype.addInstance = function(instance) {
	this.instances[instance.element.id] = instance;
	this.defaultInstance = this.defaultInstance || instance;
};

XFModel.prototype.addBind = function(bind) {
	this.binds.push(bind);
};

XFModel.prototype.dispose = function() {
	this.instances = null;
	this.binds = null;
	this.defaultInstance = null;
	XFCoreElement.prototype.dispose.call(this);
};

XFModel.prototype.getInstance = function(id) {
	return id? this.instances[id] : this.defaultInstance;
};

XFModel.prototype.getInstanceDocument = function(id) {
	var instance = this.getInstance(id);
	return instance? instance.doc : null;
};

XFModel.prototype.findInstance = function(node) {
	var doc = node.ownerDocument;

	for (var id in this.instances) {
		var inst = this.instances[id];

		if (doc == inst.doc) {
			return inst;
		}
	}

	return null;
};

XFModel.prototype.construct = function() {
	if (!xforms.ready) {
		forEach(this.instances, "construct");
	}

	XMLEvents.dispatch(this, "xforms-rebuild");
	XMLEvents.dispatch(this, "xforms-model-construct-done");
};

XFModel.prototype.reset = function() {
	forEach(this.instances, "reset");
	this.setRebuilded(true);
	xforms.addChange(this);
};

XFModel.prototype.rebuild = function() {
	if (xforms.ready) {
		this.setRebuilded(true);
	}

	forEach(this.binds, "refresh");
	XMLEvents.dispatch(this, "xforms-recalculate");
};

XFModel.prototype.recalculate = function() { 
	forEach(this.binds, "recalculate");
	XMLEvents.dispatch(this, "xforms-revalidate");
};
        
XFModel.prototype.revalidate = function() {
	forEach(this.instances, "revalidate");
	
	if (xforms.ready) {
		XMLEvents.dispatch(this, "xforms-refresh");
	}
};

XFModel.prototype.refresh = function() {
	// Nada?
};

XFModel.prototype.addChange = function(node) {
	var list = xforms.building? this.newNodesChanged : this.nodesChanged;

	if (!inArray(node, list)) {
		list.push(node);
		xforms.addChange(this);
	}
};

XFModel.prototype.setRebuilded = function(value) {
	if (xforms.building) {
		this.newRebuilded = value;
	} else {
		this.rebuilded = value;		
	}
};function XFSubmission(id, model, ref, bind, action, method, version, indent,
			mediatype, encoding, omitXmlDeclaration, cdataSectionElements,
			replace, instance, separator, includenamespaceprefixes, validate,
			synchr) {
	this.init(id, model, "xforms-submission");
	this.model = model;
	this.action = action;
	this.method = method;
	this.replace = replace;
	this.version = version;
	this.indent = indent;
	this.validate = validate;
	this.synchr = synchr;

	if (mediatype != null) {
		var lines = mediatype.split(";");
		this.mediatype = lines[0];
         
		for (var i = 1; i < lines.length; i++) {
			var vals = lines[i].split("=");

			switch (vals[0]) {
				case "action" : this.soapAction = vals[1]; break;
				case "charset" : this.charset = vals[1]; break;
			}
		}
	}
    
	this.encoding = encoding;
	this.omitXmlDeclaration = omitXmlDeclaration;
	this.cdataSectionElements = cdataSectionElements;
	this.replace = replace;
	this.instance = instance;
	this.separator = separator == "&amp;"? "&" : separator;
	this.includenamespaceprefixes = includenamespaceprefixes;

	if (ref || bind) {
		this.binding = new Binding(ref, model.element, bind);
        
		this.eval_ = function() {
			return this.binding.evaluate()[0];
		}
	} else {
		this.eval_ = function() {
			return this.model.getInstanceDocument();
		}
	}
};

XFSubmission.prototype = new XFCoreElement;

XFSubmission.prototype.submit = function() {
	xforms.openAction();
	var node = this.eval_();
	var action = this.action;
	if (node) {
		if (this.validate && !validate_(node)) {
			XMLEvents.dispatch(this, "xforms-submit-error");
			xforms.closeAction();
			return;
		}
 	if (this.method == "get") {
			action += (action.indexOf('?') == -1? '?' : this.separator)
				+ XFSubmission.toUrl_(node, this.separator);
		}
	}

	var synchr = this.synchr;
	var instance = this.instance;
	
	try {
		var req = Core.openRequest(this.method, action, !synchr);
		var subm = this;
	
		var func = function() {
			if (!synchr && req.readyState != 4) return;
	
			try {
				if (req.status != 200 && req.status != 0) {
					throw "Request error: " + req.status;
				}
	
				if (subm.replace == "instance") {
					var inst = instance == null? subm.model.getInstance() : $(instance).xfElement;
					inst.setDoc(req.responseText);
					XMLEvents.dispatch(subm.model, "xforms-rebuild");
					xforms.refresh();
				}
	
				XMLEvents.dispatch(subm, "xforms-submit-done");
				xforms.closeAction();
				
				/*if (subm.replace == "all") {
					xforms.close();
					document.write(req.responseText);
					document.close();
				}*/
					xforms.close();
					
						if(!gusztustalansag){
							gusztustalansag=false;
				  		window.location=req.responseText;
						}
					
				  //document.write(req.responseText);
			} catch(e) {
				DebugConsole.write(e.message || e);
				XMLEvents.dispatch(subm, "xforms-submit-error");
				xforms.closeAction();
			}
		};
	
		if (!synchr) {
			req.onreadystatechange = func;
		}
	
		var media = this.mediatype;
		var mt = (media || "application/xml")
			+ (this.charset? ";charset" + this.charset : "");
	
		DebugConsole.write("Submit " + this.method + " - " + media + " - "
			+ action + " - " + synchr);

		if (this.method == "get") {
			if (media == XFSubmission.SOAP_) {
				req.setRequestHeader("Accept", mt);
			}
	
			req.send(null);
		} else {
			req.setRequestHeader("Content-Type", mt);
	
			if (media == XFSubmission.SOAP_) {
				req.setRequestHeader("SOAPAction", this.soapAction);
			}
	
			req.send(Writer.toString(node));
		}
	
		if (synchr) {
			func();
		}
	} catch(e) {
		DebugConsole.write(e.message || e);
		XMLEvents.dispatch(this, "xforms-submit-error");
		xforms.closeAction();
	}
};


XFSubmission.SOAP_ = "application/soap+xml";

XFSubmission.toUrl_ = function(node, separator) {
	var url = "";
	var val = "";
	var hasChilds = false;

	for(var i = 0; i < node.childNodes.length; i++) {
		var child = node.childNodes[i];

		switch (child.nodeType) {
		case NodeType.ELEMENT :
			hasChilds = true;
			url += this.toUrl_(child, separator);
			break;
		case NodeType.TEXT :
			val += child.nodeValue;
			break;
		}
	}
    
	if (!hasChilds && val.length > 0) {
		url += node.nodeName + '=' + escape(val) + separator;
	}
    
	return url;
};// JsCore
// Copyright (C) 2006 AJAXForms S.L.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
// Contact the author at: <contact@ajaxforms.com>.

function Calendar() {
    var calendar = this;
    var body = document.getElementsByTagName("BODY")[0];
    this.element = Core.createElement("table", body, null, "calendar");

    var tHead = Core.createElement("thead", this.element);
    var trTitle = Core.createElement("tr", tHead);
    var title = Core.createElement("td", trTitle, null, "title");
    title.colSpan = 7;

    this.selectMonth = Core.createElement("select", title);
    Event.attach(this.selectMonth, "change", function() {
        Calendar.INSTANCE.refresh();
    } );

    for (var i = 0; i < 12; i++) {
        this.selectMonth.options[i] = new Option(I8N.get("calendar.month" + i), i);
    }

    this.inputYear = Core.createElement("input", title);
    this.inputYear.readOnly = true;
    Event.attach(this.inputYear, "mouseup", function() {
        var cal = Calendar.INSTANCE;
        cal.yearList.show();
    } );
    Event.attach(this.inputYear, "change", function() {
        Calendar.INSTANCE.refresh();
    } );

    var close = Core.createElement("button", title, "X");
    close.setAttribute("title", "Close");

    Event.attach(close, "click", function() {
        Calendar.close();
    } );

    var trDays = Core.createElement("tr", tHead, null, "names");
    var ini = parseInt(I8N.get("calendar.initDay"));

    for (var j = 0; j < 7; j++) {
    	var ind = j + ini;
        this.createElement(trDays, "name", I8N.get("calendar.day" + (ind >= 7? ind - 7 : ind)))
    }

    this.tBody = Core.createElement("tbody", this.element);

    var handler = function(event) {
        var value = Event.getTarget(event).childNodes[0].nodeValue;
        var cal = Calendar.INSTANCE;

        if (value != "") {
	        cal.day = value;
    	    var date = new Date();
        	date.setYear(cal.inputYear.value);
	        date.setMonth(cal.selectMonth.value);
    	    date.setDate(cal.day);
    	    
            if (cal.isTimestamp) {
    	    	date.setMinutes(cal.inputMin.value);
    	    	date.setHours(cal.inputHour.value);
    	    	cal.input.value = I8N.format(date);
            } else {
                cal.input.value = I8N.formatDate(date);
            }

	        Calendar.close();
    	    Event.dispatch(cal.input, "change");
    	    cal.input.focus();
    	}
    };

    for (var i = 0; i < 6; i++) {
        var trLine = Core.createElement("tr", this.tBody);

        for (var j = 0; j < 7; j++) {
            this.createElement(trLine, "day", " ", 1, handler)
        }
    }
    
    var tFoot = Core.createElement("tFoot", this.element);
    var trFoot = Core.createElement("tr", tFoot);
    var tdFoot = Core.createElement("td", trFoot);
   	tdFoot.colSpan = 7;
   	
    this.inputHour = Core.createElement("input", tdFoot);
    this.inputHour.readOnly = true;
    Event.attach(this.inputHour, "mouseup", function() {
        Calendar.INSTANCE.hourList.show();
    } );
    
    tdFoot.appendChild(document.createTextNode(":"));
    this.inputMin = Core.createElement("input", tdFoot);
    this.inputMin.readOnly = true;
    Event.attach(this.inputMin, "mouseup", function() {
        Calendar.INSTANCE.minList.show();
    } );

    tdFoot.appendChild(document.createTextNode(":"));
    this.inputSec = Core.createElement("input", tdFoot);
    this.inputSec.readOnly = true;
    Event.attach(this.inputSec, "mouseup", function() {
    	if (Calendar.INSTANCE.type >= Calendar.SECONDS) {
	        Calendar.INSTANCE.secList.show();
	    }
    } );

    this.yearList = new NumberList(title, "calendarList", this.inputYear, 1900, 2050);
    this.hourList = new NumberList(tdFoot, "calendarList", this.inputHour, 0, 23, 2);
    this.minList = new NumberList(tdFoot, "calendarList", this.inputMin, 0, 59, 2);
    this.secList = new NumberList(tdFoot, "calendarList", this.inputSec, 0, 59, 2);
};

Calendar.prototype.today = function() {
	this.refreshControls(new Date());
};

Calendar.prototype.refreshControls = function(date) {
    this.day = date.getDate();
    this.selectMonth.value = date.getMonth();
    this.inputYear.value = date.getYear() < 1000? 1900 + date.getYear() : date.getYear();

    if (this.isTimestamp) {
	    this.inputHour.value = zeros(date.getHours(), 2);
    	this.inputMin.value = this.type >= Calendar.MINUTES? zeros(date.getMinutes(), 2) : "00";
    	this.inputSec.value = this.type >= Calendar.SECONDS? zeros(date.getSeconds(), 2) : "00";
   	}
   	
   	this.refresh();
};

Calendar.prototype.refresh = function() {
    var firstDay = this.getFirstDay();
    var daysOfMonth = this.getDaysOfMonth();
    var cont = 0;
    var day = 1;
    var currentMonthYear = this.selectMonth.value == this.currentMonth
        && this.inputYear.value == this.currentYear;

    for (var i = 0; i < 6; i++) {
        var trLine = this.tBody.childNodes[i];

        for (var j = 0; j < 7; j++, cont++) {
            var cell = trLine.childNodes[j];
            Core.setClass(cell, "hover", false);
            Core.setClass(cell, "today", currentMonthYear && day == this.currentDay);
            Core.setClass(cell, "selected", day == this.day);
            Core.setClass(cell, "weekend", j > 4);

            cell.firstChild.nodeValue
                = (cont >= firstDay && cont < firstDay + daysOfMonth)? day++ : "";
        }
    }
};

Calendar.prototype.getFirstDay = function() {
   var date = new Date();
   date.setDate(1);
   date.setMonth(this.selectMonth.value);
   date.setYear(this.inputYear.value);

   return date.getDay() == 0? 6 : date.getDay() - 1;
};

Calendar.prototype.getDaysOfMonth = function() {
	var year = this.inputYear.value;
	var month = this.selectMonth.value;

	if (month == 1 && ((0 == (year % 4)) && (   (0 != (year % 100))
	                                         || (0 == (year % 400))))) {
		return 29;
	}

    return Calendar.daysOfMoth[this.selectMonth.value];
};

Calendar.prototype.createElement = function(parent, className, text, colspan, handler) {
    var element = Core.createElement("td", parent, text, className);

    if (colspan > 1) {
        element.colSpan = colspan;
    }
    
    if (handler) {
        Event.attach(element, "click", handler);
        Core.initHover(element);
    }

    return element;
};

Calendar.daysOfMoth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

Calendar.ONLY_DATE = 0;
Calendar.HOURS = 1;
Calendar.MINUTES = 2;
Calendar.SECONDS = 3;

Calendar.show = function(input, type) {
    var cal = Calendar.INSTANCE;

    if (!cal) {
        cal = new Calendar();
        Calendar.INSTANCE = cal;
    }

	if (!type) {
		type = Calendar.ONLY_DATE;
	}

    cal.input = input;
    cal.type = type;
    cal.isTimestamp = type != Calendar.ONLY_DATE;
    Core.setClass(cal.element, "date", !cal.isTimestamp);
    var date;
    
    try {
        date = cal.isTimestamp? I8N.parse(input.value) : I8N.parseDate(input.value);
    } catch (e) { date = new Date(); }

    if (date != null) {
	    cal.refreshControls(date);
    } else {
        cal.today();
    }
    
    Dialog.show(cal.element, input);
};

Calendar.close = function() {
    var cal = Calendar.INSTANCE;
    cal.yearList.close();
    Dialog.hide(cal.element);
};
var Core = {
    fileName : "jsCore.js",
    isOpera : navigator.userAgent.match(/\bOpera\b/) != null,
    isIE : navigator.userAgent.match(/\bMSIE\b/) != null
           && navigator.userAgent.match(/\bOpera\b/) == null,
    isMozilla : navigator.userAgent.match(/\bGecko\b/) != null,
    isChrome : navigator.userAgent.match("Chrome") != null && navigator.userAgent.match("Safari") != null,
    setClass : function(element, className, value) {
        assert(element && className);

        if (value) {
            if (!this.hasClass(element, className)) {
                element.className += " " + className;
            }
        } else if (element.className) {
            element.className = element.className.replace(className, "");
        }
    },
    hasClass : function(element, className) {
    	var cn = element.className;

    	return inArray(className, (cn && cn.split(" ")) || []);
    },
    initHover : function(element) {
        Event.attach(element, "mouseover", function(event) {
            Core.setClass(Event.getTarget(event), "hover", true);
        } );

        Event.attach(element, "mouseout", function(event) {
            Core.setClass(Event.getTarget(event), "hover", false);
        } );
    },
    getEventPos : function(ev) {
        ev = ev || window.event;
		return { x : ev.pageX || ev.clientX + window.document.body.scrollLeft || 0,
		         y : ev.pageY || ev.clientY + window.document.body.scrollTop || 0 };
    },
    getAbsolutePos : function(e) {
        var r = Core.getPos(e);

        if (e.offsetParent) {
            var tmp = Core.getAbsolutePos(e.offsetParent);
            r.x += tmp.x;
            r.y += tmp.y;
        }

        return r;
    },
    getPos : function(e) {
        var is_div = /^div$/i.test(e.tagName);

        var r = {
            x: e.offsetLeft - (is_div && e.scrollLeft? e.scrollLeft : 0),
            y: e.offsetTop - (is_div && e.scrollTop? e.scrollTop : 0)
        };

        return r;
    },
    setPos : function(element, left, top) {
        if (element.offsetParent) {
            var tmp = Core.getAbsolutePos(element.offsetParent);
            left -= tmp.x;
            top -= tmp.y;
        }

        element.style.top = top + "px";
        element.style.left = left + "px";
    },
    loadProperties : function(name) {
        if (!this.ROOT) {
            var scripts = document.getElementsByTagName("SCRIPT");

            for (var i = 0; i < scripts.length; i++) {
                var src = scripts[i].src;

                if (src.indexOf(Core.fileName) != -1) {
                    this.ROOT = src.replace(Core.fileName, "");
                    break;
                }
            }
        }

        var uri = this.ROOT + name + ".properties";
        var req = Core.openRequest("GET", uri, false);

        if (req.overrideMimeType) {
	        req.overrideMimeType("text/plain");
	    }

        try {        
            req.send(null);
        } catch(e) {
            alert("File not found: " + uri);
        }

        var text = req.responseText;
        var lines = text.split(/\s*\n\s*/);
        var properties = [];
        
        for (var i = 0; i < lines.length; i++) {
        	var line = lines[i].replace(/^\s+|\s+$/, ''); //trim

        	if (line[0] != '#' && line[0] != '!') {
	            var spl = lines[i].split(/\s*=\s*/);
	            properties[spl[0]] = spl[1];
	        }
        }
        
        return properties;
    },
    constructURI : function(uri) {
        if (!uri.match(/\/\//)) {
            uri = document.location.href.replace(/[^\/]+$/,"") + uri;
        }
    
        return uri;
    },
    createElement : function(type, parent, content, className) {
        var el = document.createElement(type);
        if (className) el.className = className;
        if (parent) parent.appendChild(el);
        if (content) el.appendChild(document.createTextNode(content));
        return el;
    },
    getWindowSize : function() {
        return {
            height : document.body.clientHeight || document.documentElement.clientHeight || window.innerHeight || 0,
            width : document.body.clientWidth || document.documentElement.clientWidth || window.innerWidth || 0
        };
    }
};

if (Core.isIE) {
    Core.openRequest = function(method, uri, async) {
        var req = new ActiveXObject("Msxml2.XMLHTTP");
        req.open(method, Core.constructURI(uri), async);

        return req;
    };
} else {
    Core.openRequest = function(method, uri, async) {
    //  netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
        var req = new XMLHttpRequest();
        req.open(method, Core.constructURI(uri), async);
        if (Core.isMozilla) req.overrideMimeType("text/xml");

        return req;
    };
}
var DebugConsole = {
    element_ : null,
    isInit_ : false,
    time_ : 0,
    init_ : function() {
        this.element_ = $("console");
        this.isInit_ = true;
        this.time_ = new Date().getTime();
    },
    write : function(text) {
        if (this.isOpen()) {
            var time = new Date().getTime();
            this.element_.appendChild(document.createTextNode(time - this.time_ + " -> " + text));
            Core.createElement("br", this.element_);
            this.time_ = time;
        }
    },
    clear : function() {
        if (this.isOpen()) {
            while (this.element_.firstChild) {
                this.element_.removeChild(this.element_.firstChild);
            }

            this.time_ = new Date().getTime();
        }
    },
    isOpen : function() {
        if (!this.isInit_) {
            this.init_();
        }
        
        return this.element_ != null;
    }
};var Dialog = {
    dialogs : [],
    init : false,
    show : function(div, parent) {
        if (!this.init) {
            this.init = true;

            Event.attach(document, "mousedown", function(event) {
                var target = Event.getTarget(event);
                var ds = Dialog.dialogs;
                
                for (var i = 0; i < ds.length; i++) {
                    var d = ds[i];

                    if (d.style.display != "none") {
	                    var t = target;
	                    
	                    for (; t && t != d; t = t.parentNode);
	                    
	                    if (!t) Dialog.hide(d);
	                }
                }
            } );
        }

        div = typeof div == "string"? $(div) : div;
        div.style.display = "block";

        if (parent) {
            var absPos = Core.getAbsolutePos(parent);
            Core.setPos(div, absPos.x, (absPos.y + parent.offsetHeight));
        } else {        
     		var size = Core.getWindowSize();
     		var h = (size.height - div.offsetHeight) / 2;
     		
            Core.setPos(div, (size.width - div.offsetWidth) / 2,
                h > 0? h : 100);
        }

        this.showSelects(div, false);
        
        if (!inArray(div, this.dialogs)) {
            this.dialogs.push(div);
        }
    },
    hide : function(div) {
        this.showSelects(div, true);
        div = typeof div == "string"? $(div) : div;

        if (div) {
            div.style.display = "none";
        }
    },
    showSelects : function(div, value) {
        if (Core.isIE) {
		    var selects = document.getElementsByTagName("select");
		    var pos = Core.getAbsolutePos(div);
		    var w = div.offsetWidth;
		    var h = div.offsetHeight;
		    
		    for (var i = 0; i < selects.length; i++) {
		    	var s = selects[i];
		    	var p = s.parentNode;
		    	
		    	while (p && p != div) {
		    	    p = p.parentNode;
		    	}

                if (p != div) {
                    var ps = Core.getAbsolutePos(s);
          	        var ws = s.offsetWidth;
                    var hs = s.offsetHeight;

                    if (ps.x + ws > pos.x && ps.x < pos.x + w
                        && ps.y + hs > pos.y && ps.y < pos.y + h) {
    	                s.style.visibility = value? "" : "hidden";
    	            }
			    }
		    }
        }
    }
};var Event = {
    cache :null,
    add_ : function() {
        if (!Event.cache) {
            Event.cache = [];
            Event.attach(window, "unload", Event.flush_);
        }

        Event.cache.push(arguments);
    },
    flush_ : function() {
        for (var i = Event.cache.length - 1; i >= 0; i--) {
            var item = Event.cache[i];
            Event.detach(item[0], item[1], item[2], item[3]);
        }
        
        if (Event.onunload) {
            Event.onunload();
        }
        
        Event.onunload = null;
    },
    onunload : null
};

if (Core.isIE) {
    Event.attach = function(target, name, handler, phase) {
    	var func = function(evt) { handler.call(window.event.srcElement, evt); };
        target.attachEvent("on" + name, func);
        this.add_(target, name, func, phase);
    };

    Event.detach = function(target, name, handler, phase) {
        target.detachEvent("on" + name, handler);
    };

    Event.getTarget = function() {
        return window.event.srcElement;
    };
    
    Event.dispatch = function(target, name) {
        target.fireEvent("on" + name, document.createEventObject())
    };
} else {
    Event.attach = function(target, name, handler, phase) {
        if (target == window && !window.addEventListener) {
            target = document;
        }

        target.addEventListener(name, handler, phase);
        this.add_(target, name, handler, phase);
    };
    
    Event.detach = function(target, name, handler, phase) {
        if (target == window && !window.addEventListener) {
            target = document;
        }

        target.removeEventListener(name, handler, phase);
    };

    Event.getTarget = function(ev) {
        return ev.target;
    };
    
    Event.dispatch = function(target, name) {
        var event = document.createEvent("Events");
        event.initEvent(name, true, true);
        target.dispatchEvent(event);
    };
}var I8N = {
    messages : null,
    lang : null,
    langs : [ "es", "gl", "nn-NO", "nb-NO"],
    get : function(key) {
        if (I8N.messages == null) {
            var lan = navigator.language || navigator.userLanguage;
            var finded = inArray(lan, I8N.langs);

            if (!finded) {
	            var ind = lan.indexOf("-");
	            
	            if (ind != -1) {
	                lan = lan.substring(0, ind);
	            }
	            
	            finded = inArray(lan, I8N.langs);
            }

            var filename = "messages" + (finded? "_" + lan.replace("-", "_") : "");
            I8N.messages = Core.loadProperties(filename);
        }

        return I8N.messages[key];
    },
    parse : function(str, pattern) {
        if (str == null || str.match("^\\s*$")) {
            return null;
        }

        if (!pattern) pattern = I8N.get("format.datetime");
        var d = new Date();
        I8N._parse(d, "Year", str, pattern, "yyyy");
        I8N._parse(d, "Month", str, pattern, "MM");
        I8N._parse(d, "Date", str, pattern, "dd");
        I8N._parse(d, "Hours", str, pattern, "hh");
        I8N._parse(d, "Minutes", str, pattern, "mm");
        I8N._parse(d, "Seconds", str, pattern, "ss");

        return d;
    },
    format : function(date, pattern) {
        if (!date) {
            return "";
        }

        if (!pattern) pattern = I8N.get("format.datetime");

        var str = I8N._format(pattern, date.getDate(), "dd");
        str = I8N._format(str, date.getMonth() + 1, "MM");
        str = I8N._format(str, date.getYear() < 1000? 1900 + date.getYear() : date.getYear(), "yyyy");
        str = I8N._format(str, date.getSeconds(), "ss");
        str = I8N._format(str, date.getMinutes(), "mm");
        str = I8N._format(str, date.getHours(), "hh");

        return str;
    },
    parseDate : function(str) {
        return I8N.parse(str, I8N.get("format.date"));
    },
    formatDate : function(str) {
        return I8N.format(str, I8N.get("format.date"));
    },
    formatNumber : function(number, decimals) {
    	if (isNaN(number)) return number;

    	var value = "" + number;
		var index = value.indexOf(".");
		var integer = parseInt(index != -1? value.substring(0, index) : value);
		var decimal = index != -1? value.substring(index + 1) : "";
		var signo = I8N.get("format.decimal");

    	return integer
    		+ (decimals > 0? signo + zeros(decimal, decimals, true) 
    		: (decimal? signo + decimal : ""));
    },	
    
    parseNumber : function(value) {
		var signo = I8N.get("format.decimal");

		if(!value.match("^[-+]?[0-9]*[\\" + signo + "[0-9]*]?$")) {
			throw "Invalid number " + value;
		}

		var index = value.indexOf(signo);
		var integer = parseInt(index != -1? value.substring(0, index) : value);
		var decimal = index != -1? value.substring(index + 1) : null;
		
		return integer + (decimal? "." + decimal : "");
    },
    _format : function(returnValue, value, el) {
        return returnValue.replace(el, zeros(value, el.length));
    },
    _parse : function(date, prop, str, format, el) {
        var index = format.indexOf(el);
        
        if (index != -1) {
            var val = str.substr(index, el.length);
            
            if (val.charAt(0) == '0') val = val.substring(1);
            
            val = parseInt(val);
        
            if (isNaN(val)) {
                throw "Error parsing date " + str + " with pattern " + format;
            }

            date["set" + prop](prop == "Month"? val - 1 : val);
        }
    }
};
var NumberList = function(parent, className, input, min, max, minlengh) {
    this.element = Core.createElement("ul", parent, null, className);
    this.move = 0;
    this.input = input;
    this.min = min;
    this.max = max;
    this.minlength = minlengh || 1;
    var list = this;

    this.createChild("+", function() { list.start(1); }, function() { list.stop(); } );

    for (var i = 0; i < 7; i++) {
        this.createChild(" ", function(event) {
            list.input.value = Event.getTarget(event).childNodes[0].nodeValue;
            list.close();
            Event.dispatch(list.input, "change");
        } );
    }
    
    this.createChild("-", function() { list.start(-1); }, function() { list.stop(); } );
};

NumberList.prototype.show = function() {
	var input = this.input;
    this.current = parseInt(input.value);
    this.refresh();
    Dialog.show(this.element, input);
};

NumberList.prototype.close = function() {
    Dialog.hide(this.element);
}; 

NumberList.prototype.createChild = function(content, handler, handler2) {
    var child = Core.createElement("li", this.element, content);
    Core.initHover(child);

    if (handler2) {
        Event.attach(child, "mousedown", handler);
        Event.attach(child, "mouseup", handler2);
    } else {
        Event.attach(child, "click", handler);
    }
};
    
NumberList.prototype.refresh = function()  {
    var childs = this.element.childNodes;
    var cur = this.current;
    
    if (cur >= this.max - 3) {
        cur = this.max - 3;
    } else if (cur <= this.min + 3) {
    	cur = this.min + 3;
    }
    
    var top = cur + 4;

    for (var i = 1; i < 8; i++) {
        Core.setClass(childs[i], "hover", false);
        var str = (top - i) + "";
        
        for (; str.length < this.minlength; str = '0' + str);
        
        childs[i].firstChild.nodeValue = str;
    }
};

NumberList.prototype.start = function(value) {
    this.move = value;
    NumberList.current = this;
    this.run();
};
    
NumberList.prototype.stop = function() {
    this.move = 0;
};

NumberList.prototype.run = function() {
    if (   (this.move > 0 && this.current + 3 < this.max)
        || (this.move < 0 && this.current - 3> this.min)) {
        this.current += this.move;
        this.refresh();
        var list = this;
        setTimeout("NumberList.current.run()", 60);
    }
};

NumberList.current = null;
function $(id) {
    return document.getElementById(id);
};

function forEach(object, block) {
    var args = [];
   
    for (var i = 0; i < arguments.length - 2; i++) {
        args[i] = arguments[i + 2];
    }

    if (object) {
        if (typeof object.length == "number") {
            for (var i = 0; i < object.length; i++) {
            	var obj = object[i];
            	var func = typeof block == "string"? obj[block] : block;
                func.apply(obj, args);
            }
        } else {
            for (var key in object) {
            	var obj = object[key];
            	var func = typeof block == "string"? obj[block] : block;
                func.apply(obj, args);
            }   
        }
    }
};

function assert(condition, message) {
    if (!condition && DebugConsole.isOpen()) {
        DebugConsole.write("Assertion failed: " + message);
        var callstack = null;

        if (arguments.caller) { // Internet Explorer
            this.callstack = [];
    
            for (var caller = arguments.caller; caller != null; caller = caller.caller) {
                this.callstack.push(caller.name ? caller.name : "<anonymous>");
            }
        } else {
            try {
                var x; x.y;
            } catch (exception) {
                this.callstack = exception.stack.split("\n");
            }
        }

        if (this.callstack) {
            for (var i in this.callstack) {
                DebugConsole.write("> " + this.callstack[i]);
            }
        }

        throw message;
    }
};

function inArray(value, array) {
    for (var i = 0; i < array.length; i++) {
        if (value == array[i]) {
            return true;
        }
    }
    
    return false;
};

function zeros(value, length, right) {
	var res = "" + value;

	for (; res.length < length; res = right? res + '0' : '0' + res);

	return res;
};function Schema(ns, name, prefixes) {
	assert(ns && !Schema.all[ns], "Needed schema name or exists one schema with that namespace");
	this.name = name;
	this.ns = ns;
	this.types = {};
	this.prefixes = prefixes || {};
	Schema.all[ns] = this;
};

Schema.all = {};

Schema.prototype.getType = function(name) {
	if (name.indexOf(":") != -1) {
		var res = name.split(":");
		var prefix = res[0];
		var ns = this.prefixes[prefix];
		//document.write(ns);
		if (ns) {
			return Schema.getTypeNS(ns, res[1]);
		}

		return Schema.getType(name);
	}

	var type = this.types[name];

	if (!type) {
		alert("Type " + name + " not defined");
		throw "Error";
	}

	return type;
};

Schema.getType = function(name) {
	var res = name.split(":");
	//document.write("name:"+Schema.prefixes[res[0]]+"----"+res[1]+"<br/>");
	return Schema.getTypeNS(Schema.prefixes[res[0]], res[1]);
};

Schema.getTypeNS = function(ns, name) {
	var schema = Schema.all[ns];
	//document.write(ns+"::::"+name+"<br/>");
  if (!schema) {
	 	alert("Schema " + ns + " not defined");
		throw "Error";
	}
	
	var type = schema.types[name];	
	/*for(var sd in schema.types){
  	document.write(sd+"<br>");	
  }*/
	if (!type) {
		//alert("Type " + name + " not defined");
		//throw "Error";
	}
  return type;
};

Schema.get = function(ns) {
	var schema = Schema.all[ns];

	if (!schema) {
		schema = new Schema(ns);
	}
	
	return schema;
};

Schema.prefixes = {
	"xsd_" : "http://www.w3.org/2001/XMLSchema",
	"xsd" : "http://www.w3.org/2001/XMLSchema",
	"types" : "http://www.netsolicitor.hu/types",
	"types_" : "http://www.netsolicitor.hu/types"
};

Schema.registerPrefix = function(prefix, namespace) {
	this.prefixes[prefix] = namespace;
};
function Type() {
};

Type.prototype.setSchema = function(schema) {
	this.schema = schema;
	return this;
};

Type.prototype.setName = function(name) {
	this.name = name;
	this.schema.types[name] = this;
	return this;
};

Type.prototype.canonicalValue = function(value) {
	value = value.toString();

	switch (this.whiteSpace) {
		case "replace": value = value.replace(/[\t\r\n]/g, " "); break;
		case "collapse": value = value.replace(/[\t\r\n ]+/g, " ").replace(/^\s+|\s+$/g, ""); break;
	}

	return value;
};


Type.prototype.getMaxLength = function() {
	return this.maxLength != null? this.maxLength 
		: (this.length != null? this.length
			: (this.totalDigits != null? this.totalDigits + 1 : null));
};

Type.prototype.getDisplayLength = function() {
	return this.displayLength != null? this.displayLength 
		: this.getMaxLength();
};function AtomicType() {
	this.patterns = [];
};

AtomicType.prototype = new Type;

AtomicType.prototype.setBase = function(base) {
	var baseType = typeof base == "string"? this.schema.getType(base) : base;

	for (var id in baseType)  {
		var value = baseType[id];

		if (id == "patterns") {
			copyArray(value, this.patterns);
		} else if (id != "name") {
			this[id] = value;
		}
	}
	
	return this;
};

AtomicType.prototype.put = function(name, value) {
	if (name == "base") {
		this.setBase(value);
	} else if (name == "pattern") {
		copyArray([value], this.patterns);
	} else {
		this[name] = value;
	}
	
	return this;
};

/** If valid return canonicalValue else null*/
AtomicType.prototype.validate = function (value) {
	value = this.canonicalValue(value);

	for (var i = 0; i < this.patterns.length; i++) {
		if (!value.match(this.patterns[i])) {
			return false;
		}
	}

	if (this.enumeration != null) {
		var matched = false;

		for (var i = 0; i < this.enumeration.length; i++) {
			if (value == this.canonicalValue(this.enumeration[i])) {
				matched = true;
				break;
			}
		}

		if (!matched) {
			return false;
		}
	}

	var l = value.length;

	if (   (this.length != null && this.length != l)
		|| (this.minLength != null && l < this.minLength)
		|| (this.maxLength != null && l > this.maxLength)
		|| (this.maxInclusive != null && value > this.maxInclusive)
		|| (this.maxExclusive != null && value >= this.maxExclusive)
		|| (this.minInclusive != null && value < this.minInclusive)
		|| (this.minExclusive != null && value <= this.minExclusive) ) {
		return false;
	}
	
	if (this.totalDigits != null || this.fractionDigits != null) {
		var index = value.indexOf(".");
		var integer = parseInt(index != -1? value.substring(0, index) : value);
		var decimal = index != -1? value.substring(index + 1) : "";
		
		if (index != -1) {
			var i = decimal.length - 1;
			for (; i >= 0 && decimal.charAt(i) == 0; i--);
			decimal = decimal.substring(0, i + 1);
		}

		if (   (this.totalDigits != null && integer.length + decimal.length > this.totalDigits)
			|| (this.fractionDigits != null && decimal.length > this.fractionDigits)) {
			return false;
		}
	}
	
	return true;
};

AtomicType.prototype.normalize = function (value) {
	if (this.fractionDigits != null) {
		var number = parseFloat(value);
		var num;

		if (isNaN(number)) {
			return "NaN";
		}

		if (number == 0) {
			num = zeros(0, this.fractionDigits + 1, true);
		}  else {
			var mult = zeros(1, this.fractionDigits + 1, true);
			num = "" + Math.round(number * mult);
		}

		if (this.fractionDigits != 0) {
			var index = num.length - this.fractionDigits;
			return (index == 0? "0" : num.substring(0, index)) + "." + num.substring(index);
		}
		
		return num;
	}
	
	return value;
};
var TypeDefs = {
	initAll : function() {
		this.init("http://www.w3.org/2001/XMLSchema", this.Default);
		this.init("http://www.w3.org/2002/xforms", this.XForms);
	},
	init : function(ns, list) {
		var schema = Schema.get(ns);
	
		for (var id in list) {
			var type = new AtomicType();
			type.setSchema(schema);
			type.setName(id);
			var props = list[id];
			var base = props["base"];

			if (base) {
				type.setBase(base);
			}
		
			for (var prop in props) {
				if (prop != "base") {				
					type[prop] = props[prop];
				}
			}
		}
	},
	ctes : {
		i : "A-Za-z_\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\xFF",
		c : "A-Za-z_\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\xFF\\-\\.0-9\\xB7"
	}
};

TypeDefs.Default = {
	"string" : {
		"whiteSpace" : "preserve"
	},
	"boolean" : {
		"patterns" : [ "^true|false|0|1$" ],
		"class" : "boolean"
	},
	"decimal" : {
		"patterns" : [ "^[-+]?[0-9]*[\\.[0-9]*]?$" ],
		"class" : "number",
		"displayLength" : 8,
		"format" : function(value) {
			return I8N.formatNumber(value, this.fractionDigits);
		},
		"parse" : function(value) {
			return I8N.parseNumber(value);
		}
	},
	"float" : {
		"patterns" : [ "^(([-+]?([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))([eE][-+]?[0-9]+)?|-?INF|NaN)$" ],
		"class" : "number"
	},
	"double" : {
		"patterns" : [ "^(([-+]?([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))([eE][-+]?[0-9]+)?|-?INF|NaN)$" ],
		"class" : "number"
	},
	"datetime" : {
		"patterns" : [ "^([12][0-9]{3})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?(Z|[+-]([01][0-9]|2[0-3]):[0-5][0-9])?$" ],
		"class" : "datetime",
		"displayLength" : 20,
		"format" : function(value) {
			return I8N.format(I8N.parse(value, "yyyy-MM-ddThh:mm:ss"));
		},
		"parse" : function(value) {
			return I8N.format(I8N.parse(value), "yyyy-MM-ddThh:mm:ss");
		}
	},
	"date" : {
		"patterns" : [ "^([12][0-9]{3})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])(Z|[+-]([01][0-9]|2[0-3]):[0-5][0-9])?$" ],
		"class" : "date",
		"displayLength" : 10,
		"format" : function(value) {
			return I8N.formatDate(I8N.parse(value, "yyyy-MM-dd"));
		},
		"parse" : function(value) {
			return I8N.format(I8N.parseDate(value), "yyyy-MM-dd");
		}
	},
	"time" : {
		"patterns" : [ "^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?(Z|[+-]([01][0-9]|2[0-3]):[0-5][0-9])?$" ],
		"displayLength" : 8
	},
	"duration" : {
		"patterns" : [ "^P([0-9]+Y)?([0-9]+M)?([0-9]+D)?(T([0-9]+H)?([0-9]+M)?([0-9]+S)?)?$" ]
	},
	"gDay" : {
		"patterns" : [ "^(0[1-9]|[12][0-9]|3[01])$" ]
	},
	"gMonth" : {
		"patterns" : [ "^(0[1-9]|1[012])$" ]
	},
	"gMonthDay" : {
		"patterns" : [ "^(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$" ]
	},
	"gYear" : {
		"patterns" : [ "^([12][0-9]{3})$" ]
	},
	"gYearMonth" : {
		"patterns" : [ "^([12][0-9]{3})-(0[1-9]|1[012])$" ]
	},
	"integer" : {
		"base" : "xsd_:decimal",
		"fractionDigits" : "0"
	},
	"nonPositiveInteger" : {
		"base" : "xsd_:integer",
		"patterns" : [ "^[-][0-9]*$" ]
	},
	"nonNegativeInteger" : {
		"base" : "xsd_:integer",
		"patterns" : [ "^[+][0-9]*$" ]
	},
	"negativeInteger" : {
		"base" : "xsd_:integer",
		"patterns" : [ "^[-][0-9]*$" ]
	},
	"positiveInteger" : {
		"base" : "xsd_:integer",
		"patterns" : [ "^[+][0-9]*$" ]
	},
	"normalizedString" : {
		"whiteSpace" : "replace"
	},
	"token" : {
		"whiteSpace" : "collapse"
	},
	"language" : {
		"base" : "xsd_:token",
		"patterns" : [ "^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$" ]
	},
	"name" : {
		"base" : "xsd_:token",
		"patterns" : [ "^[" + TypeDefs.ctes.i + ":][" + TypeDefs.ctes.c + ":]*$" ]
	},
	"NCName" : {
		"base" : "xsd_:token",
		"patterns" : [ "^[" + TypeDefs.ctes.i + "][" + TypeDefs.ctes.c + "]*$" ]
	},
	"ID" : {
		"base" : "xsd_:NCName"
	},
	"IDREF" : {
		"base" : "xsd_:NCName"
	}
};

TypeDefs.XForms = {
	"email" : {
		"base" : "xsd_:string",
		"whiteSpace" : "collapse",
		"patterns" : [ "^[A-Za-z0-9\*\+\-]+(\.[A-Za-z0-9\*\+\-]+)*@[A-Za-z0-9\*\+\-]+(\.[A-Za-z0-9\*\+\-]+)*$" ]
	},
	"ID-card-number" : {
		"base" : "xsd_:string",
		"minLength" : 12,
		"maxLength" : 19,
		"patterns" : [ "^[0-9]+$" ]
	},
	"url" : {
		"base" : "xsd_:string",
		"whiteSpace" : "collapse",
		"pattern" : [ "^(ht|ft)tp(s?)://([a-z0-9]*:[a-z0-9]*@)?([a-z0-9.]*\.[a-z]{2,7})$" ]
	},
	"amount" : {
		"base" : "xsd_:decimal",
		"format" : function(value) {
			return I8N.formatNumber(value, 2);
		}
	}
};

TypeDefs.initAll();function ListType() {
	this.whiteSpace = "collapse";
};

ListType.prototype = new Type;

ListType.prototype.setItemType = function(itemType) {
	this.itemType = typeof itemType == "string"? this.schema.getType(itemType) : itemType;
	return this;
};

ListType.prototype.validate = function (value) {
	var items = this.baseType.canonicalValue.call(this, value).split(" ");
	value = "";

	if (items.length == 1 && items[0] == "") {
		items = [];
    }

	for (var i = 0; i < items.length; i++) {
		var item = itemType.validate(items[i]);

		if (!item) {
			return null;
		}
		
		value += value.length == 0? item : " " + item;
	}

	if (   (this.length != null > 0 && this.length != l)
		|| (this.minLength != null && l < this.minLength)
		|| (this.maxLength != null && l > this.maxLength)) {
		return null;
	}

    return null;
};

ListType.prototype.canonicalValue = function(value) {
	var items = this.baseType.canonicalValue(value).split(" ");
	var value = "";

	for (var i = 0; i < items.length; i++) {
		var item = this.itemType.canonicalValue(items[i]);
		value += (value.length == 0? "" : " ") + item;
    }

    return value;
};



function UnionType() {
	this.baseTypes = [];
};

UnionType.prototype = new Type;

UnionType.prototype.addType = function(type) {
	this.baseTypes.push(typeof type == "string"? this.schema.getType(type) : type);
	return this;
};

UnionType.prototype.validate = function (value) {
	for (var i = 0; i < this.baseTypes.length; ++i) {
		value = this.baseTypes[i].validate(value);
		
		if (value) {
			return null;
		}
	}

	return value;
};function getValue(node, format) {
	assert(node);
	var value = node.nodeType == NodeType.ATTRIBUTE? node.nodeValue :
		(node.firstChild != null? node.firstChild.nodeValue : "");

	if (value && format && node.type.format) {
		try { value = node.type.format(value); } catch(e) { }
	}

	return value;
};

function setValue(node, value) {
	assert(node);

	if (node.nodeType == NodeType.ATTRIBUTE) {
		node.nodeValue = value;
	} else if (node.firstChild) {
		node.firstChild.nodeValue = value;
	} else {
		node.appendChild(node.ownerDocument.createTextNode(value));
	}
};

function run(action, element, evt, synch) {
	xforms.openAction();
    
	if (synch) {
		Dialog.show("statusPanel");

		setTimeout(function() { 
			action.execute(IdManager.find(element), null, evt);
			Dialog.hide("statusPanel");
			xforms.closeAction();
		}, 1 );
	} else {
		action.execute(IdManager.find(element), null, evt);
		xforms.closeAction();
	}
};

function show(el, type, value) {
	el.parentNode.lastChild.style.display = value? 'inline' : 'none';
};

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

function copyArray(source, dest) {
	for (var i = 0; i < source.length; i++) {
		dest[i] = source[i];
	}
};
var xforms = {
	cont : 0,
	ready : false,
	body : null,
	models : [],
	defaultModel : null,
	changes : [],
	newChanges : [],
	building : false,
	posibleBlur : false,
	init : function() {
		var b = document.getElementsByTagName("body")[0];
		this.body = b;

		Event.attach(b, "click", function(evt) {
			var target = Event.getTarget(evt);
			var parent = target;
			var xfClick = null;
			
			while (parent.nodeType == NodeType.ELEMENT) {
				if (typeof parent.node != "undefined") {
					if (Core.hasClass(parent, "xforms-repeat-item")) {
						XFRepeat.selectItem(parent);
					} else {
						var xf = parent.xfElement;

						if (xf) {
							if(xf.focus && !parent.node.readonly) {
								var name = target.nodeName.toLowerCase();
								xf.focus(name == "input" || name == "textarea");
							}

							if(xf.click && !xfClick) {
								xfClick = xf;
							}
						}
					}
				}

				parent = parent.parentNode;
			}
			
			if (xfClick) {
				xfClick.click(target);
			}
		} );

		Event.onunload = function() {
			xforms.close();
		};

		this.openAction();
		XMLEvents.dispatchList(this.models, "xforms-model-construct");
		XMLEvents.dispatchList(this.models, "xforms-ready");
		this.refresh();
		this.closeAction();
		this.ready = true;
		XMLEvents.dispatchList(this.models, "xforms-ready");
		Dialog.hide("statusPanel");
		autoSave(0);
	},
	close : function() {
		if (xforms.body) {
			xforms.openAction();
			XMLEvents.dispatchList(xforms.models, "xforms-model-destruct");
			xforms.closeAction();
			IdManager.clear();
			xforms.defaultModel = null;
			xforms.changes = null;
			xforms.models = null;
			xforms.body = null;
			xforms.cont = 0;
			xforms.dispose(document.documentElement);
			Event.flush_();
		}
	},
	openAction : function() {
		if (this.cont++ == 0) {
			DebugConsole.clear();
		}
	},
	closeAction : function() {
		if (this.cont == 1) {
			this.closeChanges();
		}
		
		this.cont--;
	},
	closeChanges : function() {
		var changes = this.changes;

		for (var i = 0; i < changes.length; i++) {
			var change = changes[i];

			if (change.instances) {//Model
				if (change.rebuilded) {
					XMLEvents.dispatch(change, "xforms-rebuild");
				} else {
					XMLEvents.dispatch(change, "xforms-recalculate");
				}
			} else { // Repeat or tree
			}
		}

		if (changes.length > 0) {
			this.refresh();
			
			if (this.changes.length > 0) {
				this.closeChanges();
			}
		}
	},
	error : function(element, event, message, causeMessage) {
		Dialog.hide("statusPanel");

		if (element != null) {
			XMLEvents.dispatch(element, event);
		}
		
		setValue($("statusPanel"), message);
		Dialog.show("statusPanel");
		
		if (causeMessage) {
			message += " : " + causeMessage;
		}

		DebugConsole.write("Error: " + message);
		throw event;        
	},
	refresh : function() {
		this.building = true;
		this.build(this.body, this.defaultModel.getInstanceDocument(), true);
		
		if (this.newChanges.length > 0) {
			this.changes = this.newChanges;
			this.newChanges = [];
		} else {
			this.changes.length = 0;
		}
		
		for (var i = 0; i < this.models.length; i++) {
			var model = this.models[i];

			if (model.newNodesChanged.length > 0 || model.newRebuilded) {
				model.nodesChanged = model.newNodesChanged;
				model.newNodesChanged = [];
				model.rebuilded = model.newRebuilded;
				model.newRebuilded = false;
			} else {
				model.nodesChanged.length = 0;
				model.rebuilded = false;
			}
		}

		this.building = false;
	},
	build : function(element, ctx, selected) {
		if (   element.nodeType != NodeType.ELEMENT
			|| element.id == "console" || element.hasXFElement == false) { return; }

		var xf = element.xfElement;
		var hasXFElement = !!xf;

		if (!ctx) { alert("xforms.build " + element.id + " no ctx"); };
		
		if (xf) {
			xf.build(ctx);

			if (xf.isRepeat) {
				xf.refresh(selected);
			}
		}

   		ctx = element.node || ctx;
		var childs = element.childNodes;
		var sel = element.selected;

		if (typeof sel != "undefined") {
			selected = sel;
		}

		if (!xf || !xf.isRepeat || xf.nodes.length > 0) {
			for (var i = 0; i < childs.length; i++) {
				hasXFElement = this.build(childs[i], ctx, selected) || hasXFElement;
			}
		}

		if (xf && xf.changed) {
			xf.refresh(selected);
			xf.changed = false;
		}
		
		if (element.hasXFElement == null) {
			element.hasXFElement = hasXFElement;
		}

		return hasXFElement;
	},
	addChange : function(element) {
		var list = this.building? this.newChanges : this.changes;

		if (!inArray(element, list)) {
			list.push(element);
		}
	},
	dispose : function(element) {
		if (element.nodeType != NodeType.ELEMENT || element.id == "console") return;

		element.listeners = null;
		element.node = null;
		var xf = element.xfElement;
        
		if (xf) {
			xf.dispose();
		}

		var childs = element.childNodes;

		for (var i = 0; i < childs.length; i++) {
			this.dispose(childs[i]);
		}
	},
	blur : function(direct) {
		if ((direct || this.posibleBlur) && this.focus) {
			if(this.focus.element) {
				this.openAction();
				XMLEvents.dispatch(this.focus, "DOMFocusOut");
				Core.setClass(this.focus.element, "xforms-focus", false);
				this.focus.blur();
				this.closeAction();
			}

			this.posibleBlur = false;
			this.focus = null;
		}
	}
};
function Listener(observer, name, phase, handler) {
    phase = phase || "default";
    this.observer = observer;
    this.name = name;
    this.evtName = document.addEventListener? name : "errorupdate";
    this.phase = phase;
    this.handler = handler;
    assert(observer);
    
    if (!observer.listeners) {
        observer.listeners = [];
    }
    
    observer.listeners.push(this);
    
    this.callback = function(event) {
	    if (!document.addEventListener) {
	        event = event || window.event;
	        event.target = event.srcElement;

	        if (event.trueName && event.trueName != name) {
	            return;
	        }
	  
	        if (!event.phase) {
	            if (phase == "capture") {
	                return;
	            }
	        } else if (event.phase != phase) {
	            return;
	        }
	  
	        if (phase == "capture") {
	            event.cancelBubble = true;
	        }
	
	        event.preventDefault = function() {
	            this.returnValue = false;
	        };
	  
	        event.stopPropagation = function() {
	            this.cancelBubble = true;
	            this.stopped      = true;
	        };
	    }
	
	    if (event.target != null && event.target.nodeType == 3) {
	        event.target = event.target.parentNode;
	    }
	    
	    handler.call(event.target, event);
	
	    if (!document.addEventListener) {
	        event.preventDefault  = null;
	        event.stopPropagation = null;
	    }
	};

    this.attach();
};

Listener.prototype.attach = function() {
    Event.attach(this.observer, this.evtName, this.callback, this.phase == "capture");
};

Listener.prototype.detach = function() {
    Event.detach(this.observer, this.evtName, this.callback, this.phase == "capture");
};

Listener.prototype.clone = function(element) {
    new Listener(element, this.name, this.phase, this.handler);
};
XMLEvents = {
    REGISTRY : new Array(),
    define : function(name, bubbles, cancelable, defaultAction) {
        XMLEvents.REGISTRY[name] = {
            bubbles:       bubbles,
            cancelable:    cancelable,
            defaultAction: defaultAction? defaultAction : function() { }
        };
    }
};

XMLEvents.dispatchList = function(list, name) {
    for (var id in list) {
        XMLEvents.dispatch(list[id], name);
    }
};

XMLEvents.dispatch = function(target, name, type, bubbles, cancelable, defaultAction) {
	target = target.element || target;
	  assert(target != null && typeof(target.nodeName) != "undefined");
    DebugConsole.write("Dispatching event " + name + " on <" + target.nodeName
      + (target.className? " class=\"" + target.className + "\"" : "")
      + (target.id? " id=\"" + target.id + "\"" : "") + "/>");
    var reg = XMLEvents.REGISTRY[name];
    /*if(name='xforms-refresh'){
      document.write(target);
    }*/
    if (reg != null) {
        bubbles = reg.bubbles;
        cancelable = reg.cancelable;
        defaultAction = reg.defaultAction;
    }

    if (!defaultAction) {
        defaultAction = function() { 
        	//document.write("mivan?");
        	};
    }

    if (target.dispatchEvent) {
        var event = document.createEvent("Events");
        event.initEvent(name, bubbles, cancelable);
        var res = target.dispatchEvent(event);

        if ((res && !event.stopped) || !cancelable) {
            defaultAction.call(target.xfElement, event);
        }
    } else {
        var fauxName = "errorupdate";
        var canceler = null;
        // Capture phase.
        var ancestors = [];
    
        for (var a = target.parentNode; a != null; a = a.parentNode) {
            ancestors.unshift(a);
        }

        for (var i in ancestors) {
            var event = document.createEventObject();
            event.trueName = name;
            event.phase = "capture";
            ancestors[i].fireEvent("onerrorupdate", event);

            if (event.stopped) {
                return;
            }
        }

        var event = document.createEventObject();
        event.trueName = name;
        event.phase = "capture";
        event.target = target;
        target.fireEvent("onerrorupdate" , event);

        // Bubble phase.
        if (!bubbles) {
            canceler = new Listener(target, name, "default",
                function(event) { event.cancelBubble = true; });
        }

        var event = document.createEventObject();
        event.trueName = name;
        event.phase = "default";
        event.target = target;
        
        var res = target.fireEvent("onerrorupdate", event);

        if ((res && !event.stopped) || !cancelable) {
            defaultAction.call(target.xfElement, event);
        }
    
        if (!bubbles) {
            canceler.detach();
        }
    }
};

XMLEvents.define("xforms-model-construct",      true, false, function(event) { this.construct(); });
XMLEvents.define("xforms-model-construct-done", true, false);
XMLEvents.define("xforms-ready",                true, false);
XMLEvents.define("xforms-model-destruct",       true, false);
XMLEvents.define("xforms-rebuild",              true, true, function(event) { this.rebuild(); });
XMLEvents.define("xforms-recalculate",          true, true, function(event) { this.recalculate(); });
XMLEvents.define("xforms-revalidate",           true, true, function(event) { this.revalidate(); });
XMLEvents.define("xforms-reset",                true, true, function(event) { this.reset(); });
XMLEvents.define("xforms-submit",               true, true, function(event) { this.submit(); });
XMLEvents.define("xforms-refresh",              true, true, function(event) { this.refresh(); });

XMLEvents.define("xforms-focus",                true, true, function(event) { this.focus(); } );

XMLEvents.define("DOMActivate",          true,  true);
XMLEvents.define("DOMFocusIn",           true,  false);
XMLEvents.define("DOMFocusOut",          true,  false);
XMLEvents.define("xforms-select",        true,  false);
XMLEvents.define("xforms-deselect",      true,  false);
XMLEvents.define("xforms-value-changed", true,  false);

XMLEvents.define("xforms-insert",        true,  false);
XMLEvents.define("xforms-delete",        true,  false);
XMLEvents.define("xforms-valid",         true,  false);
XMLEvents.define("xforms-invalid",       true,  false);
XMLEvents.define("xforms-enabled",       true,  false);
XMLEvents.define("xforms-disabled",      true,  false);
XMLEvents.define("xforms-optional",      true,  false);
XMLEvents.define("xforms-required",      true,  false);
XMLEvents.define("xforms-readonly",      true,  false);
XMLEvents.define("xforms-readwrite",     true,  false);
XMLEvents.define("xforms-in-range",      true,  false);
XMLEvents.define("xforms-out-of-range",  true,  false);
XMLEvents.define("xforms-submit-done",   true,  false);
XMLEvents.define("xforms-submit-error",  true,  false);

XMLEvents.define("xforms-compute-exception",     true, false);
XMLEvents.define("xforms-binding-exception",     true, false);

XMLEvents.define("ajx-start", true,  true, function(evt) { evt.target.xfElement.start(); });
XMLEvents.define("ajx-stop",  true,  true, function(evt) { evt.target.xfElement.stop(); });
XMLEvents.define("ajx-time",  true,  true);
XMLEvents.define("ajx-show",  true,  true, function(evt) { Dialog.show(evt.target); });
XMLEvents.define("ajx-hide",  true,  true, function(evt) { Dialog.hide(evt.target); });function BinaryExpr(expr1, op, expr2) {
    this.expr1 = expr1;
    this.expr2 = expr2;
    this.op = op.replace("&gt;", ">").replace("&lt;", "<");
};

BinaryExpr.prototype.evaluate = function(ctx) {
    var v1 = this.expr1.evaluate(ctx);
    var v2 = this.expr2.evaluate(ctx);
    var n1 = numberValue(v1);
    var n2 = numberValue(v2);
    
    if (isNaN(n1) || isNaN(n2)) {
        n1 = stringValue(v1);
        n2 = stringValue(v2);
    }

    switch (this.op) {
        case 'or'  : return booleanValue(v1) || booleanValue(v2);
        case 'and' : return booleanValue(v1) && booleanValue(v2);
        case '+'   : return n1 + n2;
        case '-'   : return n1 - n2;
        case '*'   : return n1 * n2;
        case 'mod' : return n1 % n2;
        case 'div' : return n1 / n2;
        case '='   : return n1 == n2;
        case '!='  : return n1 != n2;
        case '<'   : return n1 < n2;
        case '<='  : return n1 <= n2;
        case '>'   : return n1 > n2;
        case '>='  : return n1 >= n2;
    }
};

function ExprContext(node, position, nodelist, parent, nsresolver, current,
		depsNodes, depsElements) {
    assert(node && node.nodeType && node.ownerDocument);
    this.node = node;
    this.current = current || node;
    this.position = position || 1;
    this.nodelist = nodelist || [ node ];
    this.parent = parent;
    this.root = parent? parent.root : node.ownerDocument;
    this.nsresolver = nsresolver;
    this.initDeps(depsNodes, depsElements);
};

ExprContext.prototype.clone = function(node, position, nodelist) {
    return new ExprContext(node || this.node, 
           typeof position == "undefined"? this.position : position,
           nodelist || this.nodelist, this, this.nsresolver, this.current,
           this.depsNodes, this.depsElements);
};

ExprContext.prototype.setNode = function(node, position) {
    this.node = node;
    this.position = position;
};

ExprContext.prototype.initDeps = function(depsNodes, depsElements) {
	this.depsNodes = depsNodes;
	this.depsElements = depsElements;
};

ExprContext.prototype.addDepNode = function(node) {
	var deps = this.depsNodes;

	if (deps && !inArray(node, deps)) {
		deps.push(node);
	}
};

ExprContext.prototype.addDepElement = function(element) {
	var deps = this.depsElements;

	if (deps && !inArray(element, deps)) {
		deps.push(element);
	}
};function TokenExpr(m) {
    this.value = m;
};

TokenExpr.prototype.evaluate = function() {
    return stringValue(this.value);
};

function UnaryMinusExpr(expr) {
    this.expr = expr;
};

UnaryMinusExpr.prototype.evaluate = function(ctx) {
    return -numberValue(this.expr.evaluate(ctx));
};

function CteExpr(value) {
    this.value = value;
};

CteExpr.prototype.evaluate = function() {
    return this.value;
};
function FilterExpr(expr, predicate) {
    this.expr = expr;
    this.predicate = predicate;
};

FilterExpr.prototype.evaluate = function(ctx) {
    var nodes = nodeSetValue(this.expr.evaluate(ctx));

    for (var i = 0; i < this.predicate.length; ++i) {
        var nodes0 = nodes;
        nodes = [];

        for (var j = 0; j < nodes0.length; ++j) {
            var n = nodes0[j];
            var newCtx = ctx.clone(n, j, nodes0);

            if (booleanValue(this.predicate[i].evaluate(newCtx))) {
                nodes.push(n);
            }
        }
    }

    return nodes;
};function FunctionCallExpr(name) {
	this.name = name;
	this.func = XPathCoreFunctions[name];
	this.args = [];
    
	if (!this.func) {
		alert("Function " + name + " not found");
		throw "Function " + name + " not found";
	}

	for (var i = 1; i < arguments.length; i++) {
		this.args.push(arguments[i]);
	}
};

FunctionCallExpr.prototype.evaluate = function(ctx) {
	var arguments = [];

	for (var i = 0; i < this.args.length; i++) {
		arguments[i] = this.args[i].evaluate(ctx);
	}

	return this.func.call(ctx, arguments);
};
function LocationExpr(absolute) {
    this.absolute = absolute;
    this.steps = [];

    for (var i = 1; i < arguments.length; i++) {
        this.steps.push(arguments[i]);
    }
};

LocationExpr.prototype.evaluate = function(ctx) {
	var start = this.absolute? ctx.root : ctx.node;
	ctx.addDepElement(start.ownerDocument.model);

	var nodes = [];
	this.xPathStep(nodes, this.steps, 0, start, ctx);
	return nodes;
};

LocationExpr.prototype.xPathStep = function(nodes, steps, step, input, ctx) {
    var s = steps[step];
    var nodelist = s.evaluate(ctx.clone(input));

    for (var i = 0; i < nodelist.length; ++i) {
        var node = nodelist[i];

        if (step == steps.length - 1) {
            nodes.push(node);
            if (!this.isRoot) ctx.addDepNode(node);
        } else {
            this.xPathStep(nodes, steps, step + 1, node, ctx);
        }
    }
};function NSResolver() {
    this.map = {};
};

NSResolver.prototype.registerAll = function(resolver) {
    for (var prefix in resolver.map) {
        this.map[prefix] = resolver.map[prefix];
    }
};

NSResolver.prototype.register = function(prefix, uri) {
    this.map[prefix] = uri;
};

NSResolver.prototype.lookupNamespaceURI = function(prefix) {
    return this.map[prefix];
};function NodeTestAny() { };

NodeTestAny.prototype.evaluate = function(node) {
    return true;
};

function NodeTestType(type) {
    this.type = type;
};

NodeTestType.prototype.evaluate = function(node) {
    return node.nodeType == this.type;
};

function NodeTestPI(target) {
    this.target = target;
};

NodeTestPI.prototype.evaluate = function(node) {
    return node.nodeType == NodeType.PROCESSING_INSTRUCTION &&
         (!this.target || node.nodeName == this.target);
};

function NodeTestName(prefix, name) {
    this.prefix = prefix;
    this.name = name;
};

NodeTestName.prototype.evaluate = function(node, nsresolver) {
    var pre = this.prefix;

    if (this.name == "*") {
        return pre? node.namespaceURI == nsresolver.lookupNamespaceURI(pre) : true;
    }
    
    var ns = node.namespaceURI;

    return node.nodeName == this.name
       && (pre? ns == nsresolver.lookupNamespaceURI(pre)
               : ns == null || ns == "" || ns == nsresolver.lookupNamespaceURI(""));
};

function PredicateExpr(expr) {
    this.expr = expr;
};

PredicateExpr.prototype.evaluate = function(ctx) {
    var v = this.expr.evaluate(ctx);
    var number;

    try { number = numberValue(v); } catch (e) { }
    
    return number? ctx.position == number : booleanValue(v);
};function PathExpr(filter, rel) {
    this.filter = filter;
    this.rel = rel;
};

PathExpr.prototype.evaluate = function(ctx) {
    var nodes = nodeSetValue(this.filter.evaluate(ctx));
    var nodes1 = [];

    for (var i = 0; i < nodes.length; i++) {
        var newCtx = ctx.clone(nodes[i], i, nodes);
        var nodes0 = nodeSetValue(this.rel.evaluate(newCtx));

        for (var j = 0; j < nodes0.length; j++) {
            nodes1.push(nodes0[j]);
        }
    }

    return nodes1;
};function StepExpr(axis, nodetest) {
	this.axis = axis;
	this.nodetest = nodetest;
	this.predicates = [];

	for (var i = 2; i < arguments.length; i++) {
		this.predicates.push(arguments[i]);
	}
};

StepExpr.prototype.evaluate = function(ctx) {
	var input = ctx.node;
	var list = [];

	switch(this.axis) {
	case XPathAxis.ANCESTOR_OR_SELF :
		_push(ctx, list, input, this.nodetest);
	case XPathAxis.ANCESTOR :
		for (var n = input.parentNode; n.parentNode; n = input.parentNode) {
			_push(ctx, list, n, this.nodetest);
            nodelist.push(n);
        }

        break;
    case XPathAxis.ATTRIBUTE :
        _pushList(ctx, list, input.attributes, this.nodetest);
        break;
    case XPathAxis.CHILD :
        _pushList(ctx, list, input.childNodes, this.nodetest);
        break;
    case XPathAxis.DESCENDANT_OR_SELF :
        _push(ctx, list, input, this.nodetest);
    case XPathAxis.DESCENDANT :
        _pushDescendants(ctx, list, input, this.nodetest);
        break;
    case XPathAxis.FOLLOWING :
        for (var n = input.parentNode; n; n = n.parentNode) {
            for (var nn = n.nextSibling; nn; nn = nn.nextSibling) {
                _push(ctx, list, nn, this.nodetest);
                _pushDescendants(ctx, list, nn, this.nodetest);
            }
        }
        break;
    case XPathAxis.FOLLOWING_SIBLING :
        for (var n = input.nextSibling; n; n = n.nextSibling) {
            _push(ctx, list, n, this.nodetest);
        }
        break;
    case XPathAxis.NAMESPACE : 
        alert('not implemented: axis namespace');
        break;
    case XPathAxis.PARENT :
		if (input.parentNode) {
			_push(ctx, list, input.parentNode, this.nodetest);
		}

        break;
    case XPathAxis.PRECEDING :
        for (var n = input.parentNode; n; n = n.parentNode) {
            for (var nn = n.previousSibling; nn; nn = nn.previousSibling) {
                _push(ctx, list, nn, this.nodetest);
                _pushDescendantsRev(ctx, list, nn, this.nodetest);
            }
        }
        break;
    case XPathAxis.PRECEDING_SIBLING :
        for (var n = input.previousSibling; n; n = n.previousSibling) {
            _push(ctx, list, n, this.nodetest);
        }
        break;
    case XPathAxis.SELF :
        _push(ctx, list, input, this.nodetest);
        break;
    default :
        throw 'ERROR -- NO SUCH AXIS: ' + this.axis;
    }   

    for (var i = 0; i < this.predicates.length; i++) {
        var pred = this.predicates[i];
        var newList = [];

        for (var j = 0; j < list.length; j++) {
            var n = list[j];
            var newCtx = ctx.clone(n, j + 1, list);

            if (booleanValue(pred.evaluate(newCtx))) {
                newList.push(n);
            }
        }
    
        list = newList;
    }

    return list;
};

function _push(ctx, list, node, test) {
    if (test.evaluate(node, ctx.nsresolver)) {
        list.push(node);
    }
};

function _pushList(ctx, list, l, test) {
    for (var i = 0; i < l.length; i++) {
        _push(ctx, list, l[i], test);
    }
};

function _pushDescendants(ctx, list, node, test) {
    for (var n = node.firstChild; n; n = n.nextSibling) {
        _push(ctx, list, n, test);
        arguments.callee(ctx, list, n, test);
    }
};

function _pushDescendantsRev(ctx, list, node, test) {
    for (var n = node.lastChildd; n; n = n.previousSibling) {
        _push(ctx, list, n, test);
        arguments.callee(ctx, list, n, test);
    }
};

function UnionExpr(expr1, expr2) {
    this.expr1 = expr1;
    this.expr2 = expr2;
};

UnionExpr.prototype.evaluate = function(ctx) {
    var nodes1 = nodeSetValue(this.expr1.evaluate(ctx));
    var nodes2 = nodeSetValue(this.expr2.evaluate(ctx));

    var I1 = nodes1.length;

    for (var i2 = 0; i2 < nodes2.length; ++i2) {
        for (var i1 = 0; i1 < I1; ++i1) {
            if (nodes1[i1] == nodes2[i2]) {
                i1 = I1;
            }
        }
    
        nodes1.push(nodes2[i2]);
    }
    
    return nodes1;
};function XPath(expression, compiled) {
	this.expression = expression;
	this.compiled = compiled;
	this.compiled.isRoot = true;
	this.nsresolver = new NSResolver();
	XPath.expressions[expression] = this;

	if (arguments.length > 2)  {
		for (var i = 2; i < arguments.length; i += 2) {
			this.nsresolver.register(arguments[i], arguments[i + 1]);
		}
	} else {
		this.nsresolver.register("", "http://www.w3.org/1999/xhtml");
	}
};

XPath.prototype.evaluate = function(ctx) {
	assert(ctx);

	if (!ctx.node) {
		ctx = new ExprContext(ctx, 1, null, null, this.nsresolver);
	} else if (!ctx.nsresolver) {
		ctx.nsresolver = this.nsresolver;
	}

	return this.compiled.evaluate(ctx);
};

XPath.expressions = {};

XPath.get = function(str) {
	return XPath.expressions[str];
};
function XPathFunction(acceptContext, defaultTo, returnNodes, body) {
	this.evaluate = body;
	this.defaultTo = defaultTo;
	this.acceptContext = acceptContext;
	this.returnNodes = returnNodes;
};

XPathFunction.DEFAULT_NONE = null;
XPathFunction.DEFAULT_NODE = 0;
XPathFunction.DEFAULT_NODESET = 1;
XPathFunction.DEFAULT_STRING = 2;

XPathFunction.prototype.call = function(context, arguments) {
	if (arguments.length == 0) {
		switch (this.defaultTo) {
		case XPathFunction.DEFAULT_NODE:
			if (context.node != null) {
				arguments = [context.node];
			}

			break;
		case XPathFunction.DEFAULT_NODESET:
			if (context.node != null) {
				arguments = [[context.node]];
			}

			break;
		case XPathFunction.DEFAULT_STRING:
			arguments = [XPathCoreFunctions["string"].evaluate([context.node])];
			break;
		}
	}
  
	if (this.acceptContext) {
		arguments.unshift(context);
	}

	return this.evaluate.apply(null, arguments);
};

XPathCoreFunctions = {
	"last" : new XPathFunction(true, XPathFunction.DEFAULT_NONE, false,
		function(ctx) { return ctx.nodelist.length; } ),
	"position" : new XPathFunction(true, XPathFunction.DEFAULT_NONE, false,
		function(ctx) { return ctx.position; } ),
	"count" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(nodeSet) { return nodeSet.length; } ),
	"id" : new XPathFunction(true, XPathFunction.DEFAULT_NODE, false,
		function(context, object) {
			var result = new Array();

			if (typeof(object.length) != "undefined") {
				for (var i = 0; i < object.length; ++i) {
					var res = XPathCoreFunctions.id.evaluate(context, object[i]);

					for (i = 0; i < res.length; i++) {
						result.push(res[i]);
					}
				}
			} else if (context.node != null) {
				var ids = stringValue(object).split(/\s+/);
      
				for (var i in ids) {
					result.add(context.node.ownerDocument.getElementById(ids[i]));
				}
			}
    
			return result;
		} ),
	"local-name" : new XPathFunction(false, XPathFunction.DEFAULT_NODESET, false,
		function(nodeSet) {
			return nodeSet.length == 0? "" : nodeSet[0].nodeName.replace(/^.*:/, "");
		} ),
	"namespace-uri" : new XPathFunction(false, XPathFunction.DEFAULT_NODESET, false,
		function(nodeSet) {
			return nodeSet.length == 0? "" : xmlNamespaceURI(nodeSet[0]);
		} ),
	"name" : new XPathFunction(false, XPathFunction.DEFAULT_NODESET, false,
		function(nodeSet) {
			return nodeSet.length == 0? "" : nodeSet[0].nodeName;
		} ),
	"string" : new XPathFunction(false, XPathFunction.DEFAULT_NODESET, false,
		function(object) { return stringValue(object); } ),
	"concat" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function() {
			var string = "";

			for (var i = 0; i < arguments.length; ++i) {
				string += stringValue(arguments[i]);
			}
    
			return string;
		} ),
	"starts-with" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(string, prefix) {   
			return stringValue(string).indexOf(stringValue(prefix)) == 0;
		} ),
	"contains" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(string, substring) {
			return stringValue(string).indexOf(stringValue(substring)) != -1;
		} ),
	"substring-before" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(string, substring) {
			string = stringValue(string);
			return string.substring(0, string.indexOf(stringValue(substring)));
		} ),
	"substring-after" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(string, substring) {
			string = stringValue(string);
			substring = stringValue(substring);
			var index = string.substring(substring);
			return index == -1? "" : string.substring(index + substring.length);
		} ),
	"substring" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(string, index, length) {
			string = stringValue(string);
			index  = Math.round(numberValue(index));
			
			if (length != null) {
				length = Math.round(numberValue(length));
				return string.substr(index - 1, length);
			}
			
			return string.substr(index - 1);
		} ),
	"string-length" : new XPathFunction(false, XPathFunction.DEFAULT_STRING, false,
		function(string) { return stringValue(string).length; } ),
	"normalize-space" : new XPathFunction(false, XPathFunction.DEFAULT_STRING, false,
		function(string) {
			return stringValue(string).replace(/^\s+|\s+$/g, "")
				.replace(/\s+/, " ");
		} ),
	"translate" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(string, from, to) {
			string =  stringValue(string);
			from = stringValue(from);
			to = stringValue(to);
			
			var result = "";
			
			for (var i = 0; i < string.length; ++i) {
			    var index = from.indexOf(string.charAt(i));
			    result += index == -1? string.charAt(i) : to.charAt(index);
			}
			
			return result;
		} ),
	"boolean" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(object) { return booleanValue(object); } ),
	"not" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(condition) { return !booleanValue(condition); } ),
	"true" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(condition) { return true; } ),
	"false" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(condition) { return false; } ),
	"lang" : new XPathFunction(true, XPathFunction.DEFAULT_NONE, false,
		function(context, language) {
			language = stringValue(language);

			for (var node = context.node; node != null; node = node.parentNode) {
				if (typeof(node.attributes) == "undefined") {
					continue;
				}
  
				var xmlLang = node.attributes.getNamedItemNS(XML.Namespaces.XML, "lang");
  
				if (xmlLang != null) {
					xmlLang  = xmlLang.value.toLowerCase();
					language = language.toLowerCase();
    
					return xmlLang.indexOf(language) == 0
						&& (language.length == xmlLang.length || language.charAt(xmlLang.length) == '-');
				}
			}

			return false;
		} ),
	"number" : new XPathFunction(false, XPathFunction.DEFAULT_NODESET, false,
		function(object) { return numberValue(object); } ),
	"sum" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(nodeSet) {
			var sum = 0;

			for (var i = 0; i < nodeSet.length; ++i) {
				sum += numberValue(xmlValue(nodeSet[i]));
			}

			return sum;
		} ),
	"floor" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(number) { return Math.floor(numberValue(number)); } ),
	"ceiling" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(number) { return Math.ceil(numberValue(number)); } ),
	"round" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(number) { return Math.round(numberValue(number)); } ),
	"boolean-from-string" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(string) {
			string = stringValue(string);

			switch (string.toLowerCase()) {
				case "true":  case "1": return true;
				case "false": case "0": return false;
				default: false;
			}
		} ),
	"if" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, true,
		function(condition, onTrue, onFalse) {
			return booleanValue(condition)? onTrue : onFalse;
		} ),
	"avg" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(nodeSet) {
			return XPathCoreFunctions.sum.evaluate(nodeSet)
				/ XPathCoreFunctions.count.evaluate(nodeSet);
		} ),
	"min" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function (nodeSet) {
			if (nodeSet.length == 0) {
				return Number.NaN;
			}
    
			var minimum = numberValue(xmlValue(nodeSet[0]));
    
			for (var i = 1; i < nodeSet.length; ++i) {
				var value = numberValue(xmlValue(nodeSet[i]));
      
				if (isNaN(value)) {
					return Number.NaN;
				}
      
				if (value < minimum) {
					minimum = value;
				}
			}
    
			return minimum;
		} ),
	"max" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function (nodeSet) {
			if (nodeSet.length == 0) {
				return Number.NaN;
			}
    
			var maximum = numberValue(xmlValue(nodeSet[0]));
    
			for (var i = 1; i < nodeSet.length; ++i) {
				var value = numberValue(xmlValue(nodeSet[i]));
      
				if (isNaN(value)) {
					return Number.NaN;
				}
      
				if (value > maximum) {
					maximum = value;
				}
			}
    
			return maximum;
		} ),
	"count-non-empty" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(nodeSet) {
			var count = 0;
			
			for (var i = 0; i < nodeSet.length; ++i) {
				if (xmlValue(nodeSet[i]).length > 0) {
					count++;
				}
			}
			
			return count;
		} ),
	"index" : new XPathFunction(true, XPathFunction.DEFAULT_NONE, false,
		function(ctx, id) {
			var xf = IdManager.find(stringValue(id)).xfElement;
			ctx.addDepElement(xf);
			return xf.index;
		} ),
	"nodeindex" : new XPathFunction(true, XPathFunction.DEFAULT_NONE, false,
		function(ctx, id) {
			var control = IdManager.find(stringValue(id));
			var node = control.node;
			ctx.addDepElement(control.xfElement);
			
			if (node) {
				ctx.addDepNode(node);
				ctx.addDepElement(node.ownerDocument.model);
			}

			return node? [ node ] : [];
		} ),
	"property" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function(name) {
			name = stringValue(name);

			switch (name) {
				case "version" : return "1.0";
				case "conformance-level" : return "full";
			}

			throw "Invalid property name: " + name;
		} ),
	"instance" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, true,
		function(idRef) {
			var name = stringValue(idRef);
			var instance = $(name);
			if (!instance) throw "instance " + name + " not found";
			return [instance.xfElement.doc];
		} ),
	"now" : new XPathFunction(false, XPathFunction.DEFAULT_NONE, false,
		function() { return I8N.format(new Date(), "yyyy-MM-ddThh:mm:ssZ"); } ),
	"current" : new XPathFunction(true, XPathFunction.DEFAULT_NONE, true,
		function(ctx) {
			ctx.addDepNode(ctx.node);
			ctx.addDepElement(ctx.node.ownerDocument.model);
			return [ctx.current];
		} ),
	"is-valid" : new XPathFunction(false, XPathFunction.DEFAULT_NODESET, false,
		function(nodeSet) {
			var valid = true;
        
			for (var i = 0; valid && i < nodeSet.length; i++) {
				valid = valid && validate_(nodeSet[i]);
			}

			return valid;
		} )
};

function validate_(node) {
		//if(node.nodeValue!=""&&node.nodeValue!=null){	
    if (!node.valid) {
    	alert("Hiba! A nem töltötte ki a "+node.nodeName+" mezőt!");
        return false;
    }

    for (var i = 0; i < node.childNodes.length; i++) {
        if (!this.validate_(node.childNodes[i])) {
            return false;
        }
    }
  	//}
    return true;
};
var XPathAxis = {
  ANCESTOR_OR_SELF: 'ancestor-or-self',
  ANCESTOR: 'ancestor',
  ATTRIBUTE: 'attribute',
  CHILD: 'child',
  DESCENDANT_OR_SELF: 'descendant-or-self',
  DESCENDANT: 'descendant',
  FOLLOWING_SIBLING: 'following-sibling',
  FOLLOWING: 'following',
  NAMESPACE: 'namespace',
  PARENT: 'parent',
  PRECEDING_SIBLING: 'preceding-sibling',
  PRECEDING: 'preceding',
  SELF: 'self'
};

var NodeType = {
    ELEMENT : 1,
    ATTRIBUTE : 2,
    TEXT : 3,
    CDATA_SECTION : 4,
    ENTITY_REFERENCE : 5,
    ENTITY : 6,
    PROCESSING_INSTRUCTION : 7,
    COMMENT : 8,
    DOCUMENT : 9,
    DOCUMENT_TYPE : 10,
    DOCUMENT_FRAGMENT : 11,
    NOTATION : 12
};
function XNode(type, ns, name, value, owner, noValues) {
    this.attributes = [];
    this.childNodes = [];
    if(noValues){
    	value="";
    }
    XNode.init.call(this, type, ns, name, value, owner);
};

XNode.prototype.appendChild = function(node) {
    if (this.childNodes.length == 0) {
        this.firstChild = node;
    }

    node.previousSibling = this.lastChild;
    node.nextSibling = null;

    if (this.lastChild) {
        this.lastChild.nextSibling = node;
    }

    node.parentNode = this;
    this.lastChild = node;
    this.childNodes.push(node);
};

XNode.prototype.replaceChild = function(newNode, oldNode) {
    if (oldNode == newNode) {
        return;
    }

    for (var i = 0; i < this.childNodes.length; ++i) {
        if (this.childNodes[i] == oldNode) {
            this.childNodes[i] = newNode;
            var p = oldNode.parentNode;
            oldNode.parentNode = null;
            newNode.parentNode = p;
            p = oldNode.previousSibling;
            oldNode.previousSibling = null;
            newNode.previousSibling = p;

            if (newNode.previousSibling) {
                newNode.previousSibling.nextSibling = newNode;
            }
      
            p = oldNode.nextSibling;
            oldNode.nextSibling = null;
            newNode.nextSibling = p;

            if (newNode.nextSibling) {
                newNode.nextSibling.previousSibling = newNode;
            }

            if (this.firstChild == oldNode) {
                this.firstChild = newNode;
            }

            if (this.lastChild == oldNode) {
                this.lastChild = newNode;
            }

            break;
        } 
    }
};

XNode.prototype.insertBefore = function(newNode, oldNode) {
    if (oldNode == newNode || oldNode.parentNode != this) {
        return;
    }

    if (newNode.parentNode) {
        newNode.parentNode.removeChild(newNode);
    }

    var newChildren = [];
    for (var i = 0; i < this.childNodes.length; ++i) {
        var c = this.childNodes[i];

        if (c == oldNode) {
            newChildren.push(newNode);
            newNode.parentNode = this;
            newNode.previousSibling = oldNode.previousSibling;
            oldNode.previousSibling = newNode;

            if (newNode.previousSibling) {
                newNode.previousSibling.nextSibling = newNode;
            }
  
            newNode.nextSibling = oldNode;

            if (this.firstChild == oldNode) {
                this.firstChild = newNode;
            } 
        }

        newChildren.push(c);
    }

    this.childNodes = newChildren;
};

XNode.prototype.removeChild = function(node) {
    var newChildren = [];

    for (var i = 0; i < this.childNodes.length; ++i) {
        var c = this.childNodes[i];

        if (c != node) {
            newChildren.push(c);
        } else {
            if (c.previousSibling) {
                c.previousSibling.nextSibling = c.nextSibling;
            }

            if (c.nextSibling) {
                c.nextSibling.previousSibling = c.previousSibling;
            }

            if (this.firstChild == c) {
                this.firstChild = c.nextSibling;
            }

            if (this.lastChild == c) {
                this.lastChild = c.previousSibling;
            }
        }
    }

    this.childNodes = newChildren;
};

XNode.prototype.setAttributeNS = function(ns, name, value) {
	var founded = false;
  
	for (var i = 0; !founded && i < this.attributes.length; i++) {
		var att = this.attributes[i];

		if (att.nodeName == name && (!ns || att.namespaceURI == ns)) {
			att.nodeValue = '' + value;
			founded = true;
		}
	}

	if (!founded) {
		var att = new XNode(NodeType.ATTRIBUTE, ns, name, value, this.ownerDocument);
		att.parentNode = this;
		this.attributes.push(att);
	}
//document.write("ns:"+ns+" name:"+name+" value:"+value+"<br>");
	if (ns == "http://www.w3.org/2001/XMLSchema-instance" && name == "type") {
		//itt száll el
    var type = Schema.getType(value);
    if (type) {
			this.type = type;
			this.schemaType = true;
		}
	}
};

XNode.prototype.getAttributeNS = function(ns, name) {
    for (var i = 0; i < this.attributes.length; ++i) {
        var att = this.attributes[i];

        if (att.nodeName == name && (!ns || att.namespaceURI == ns)) {
            return att.nodeValue;
        }
    }
    
    return null;
};

XNode.prototype.removeAttributeNS = function(ns, name) {
    var a = [];

    for (var i = 0; i < this.attributes.length; i++) {
        var att = this.attributes[i];

        if (att[i].nodeName != name && (!ns || att.namespaceURI == ns)) {
            a.push(att);
        }
    }

    this.attributes = a;
};
//Itt pitiszka!!
XNode.prototype.cloneNode = function(deep, doc, noValues) {
    var clone = null;
    doc = doc || this.ownerDocument;

    if (this.nodeType == NodeType.DOCUMENT) {
        clone = new XDocument();
        doc = clone;
    } else {
    	if(noValues==true){
    		clone = new XNode(this.nodeType, this.namespaceURI, this.nodeName, "", doc, true);
    	}
    	else{
    		clone = new XNode(this.nodeType, this.namespaceURI, this.nodeName, this.nodeValue, doc);
    	}
        
    }

    for (var i = 0; i < this.childNodes.length; i++) {
        clone.appendChild(this.childNodes[i].cloneNode(true, doc, noValues));
    }
    
    for (var i = 0; i < this.attributes.length; i++) {
        var att = this.attributes[i];
        clone.setAttributeNS(att.namespaceURI, att.nodeName, att.nodeValue);
    }

    return clone;
};

XNode.init = function(type, ns, name, value, owner) {
    this.nodeType = type;
    this.nodeName = name;
    this.namespaceURI = ns;
    this.nodeValue = value;
    this.ownerDocument = owner;
    this.firstChild = null;
    this.lastChild = null;
    this.nextSibling = null;
    this.previousSibling = null;
    this.parentNode = null;
    this.ns = null;

    this.valid = true;
    this.required= false;
    this.relevant = true;
    this.type = Schema.getType("xsd_:string");
    this.schemaType = false;
    this.bind = null;
    this.repeat = null;
    this.init = false;
    this.changes = null;
};

XNode.unused_ = [];

XNode.recycle = function(node) {
    if (node) {
        if (node.constructor == XDocument) {
            XNode.recycle(node.documentElement);
            return;
        }

        if (node.constructor != this) {
            return;
        }

        XNode.unused_.push(node);

        for (var a = 0; a < node.attributes.length; ++a) {
            XNode.recycle(node.attributes[a]);
        }

        for (var c = 0; c < node.childNodes.length; ++c) {
            XNode.recycle(node.childNodes[c]);
        }

        node.attributes.length = 0;
        node.childNodes.length = 0;
        XNode.init.call(node, 0, '', '', '', null);
    }
};

XNode.create = function(type, ns, name, value, owner) {
    if (XNode.unused_.length > 0) {
        var node = XNode.unused_.pop();
        XNode.init.call(node, type, ns, name, value, owner);
        return node;
    } else {
        return new XNode(type, ns, name, value, owner);
    }
};

function XDocument() {
    XNode.call(this, NodeType.DOCUMENT, null, '#document', null, this);
    this.documentElement = null;
};

XDocument.prototype = new XNode(NodeType.DOCUMENT, null, '#document');

XDocument.prototype.clear = function() {
    XNode.recycle(this.documentElement);
    this.documentElement = null;
};

XDocument.prototype.appendChild = function(node) {
    XNode.prototype.appendChild.call(this, node);
    this.documentElement = this.childNodes[0];
};

XDocument.prototype.createElementNS = function(ns, name) {
    return XNode.create(NodeType.ELEMENT, ns, name, null, this);
};

XDocument.prototype.createTextNode = function(value) {
    return XNode.create(NodeType.TEXT, null, '#text', value, this);
};

XDocument.prototype.createAttributeNS = function(ns, name) {
    return XNode.create(NodeType.ATTRIBUTE, ns, name, null, this);
};
XDocument.parse = function(xml) {
    var regex_empty = /\/$/;
    var regex_tagname = /^([\w|{ÁÉÍÓÖŐÚÜŰáéíóöőúüű}:-]*)/;
    var regex_attribute = /([\w{ÁÉÍÓÖŐÚÜŰáéíóöőúüű}:-]+)\s?=\s?('([^\']*)'|"([^\"]*)")/g;
    var xmldoc = new XDocument();
    var root = xmldoc;
    var stack = [];
    var parent = root;
    stack.push(parent);
    var x = stringSplit(xml, "<");

    for (var i = 1; i < x.length; ++i) {
        var xx = stringSplit(x[i], ">");
        var tag = xx[0];
        var text = xmlResolveEntities(xx[1] || "");

        if (tag.charAt(0) == "/") {
            var el = stack.pop();

            if (tag.substring(1).indexOf(el.nodeName) == -1) {
                throw "XML parser exception: endTag " + tag + " not valid. " + el.nodeName;
            }
            
            parent = stack[stack.length - 1];
        } else if (tag.charAt(0) == "?" || tag.charAt(0) == "!") {
            // Ignore XML declaration and processing instructions, notation and comments
        } else {
            var empty = tag.match(regex_empty);
            var tagname = regex_tagname.exec(tag)[1];
            //document.write(tagname);
            var atts = [];
            var ns = [];
            var att;

            while (att = regex_attribute.exec(tag)) {
                var val = xmlResolveEntities(att[3] || att[4] || "");
                var name = att[1];
                
                if (name.indexOf("xmlns") == 0) {
                    ns[name.length == 5? "" : name.substring(6)] = val;
                    ns.length++;
                } else {
                    atts.push([name, val]);
                }
            }
            
            var parsed = this.parseName_(parent, ns, tagname, false);
            var node = xmldoc.createElementNS(parsed[0], parsed[1]);
            if (ns.length > 0) {
                node.ns = ns;
            }
            for (var j = 0; j < atts.length; j++) {
                var attParsed = this.parseName_(parent, ns, atts[j][0], true);
                /*document.write(attParsed[0]+"<br>");
                document.write(attParsed[1]+"<br>");
                document.write(atts[j][1]+"<br>");*/
                node.setAttributeNS(attParsed[0], attParsed[1], atts[j][1]);
            }
      
            if (empty) {
                parent.appendChild(node);
            } else {
                parent.appendChild(node);
                parent = node;
                stack.push(node);
            }
        }

        if (text && parent != root) {
            parent.appendChild(xmldoc.createTextNode(text));
        }
    }

    return root;
};

XDocument.parseName_ = function(parent, nsList, name, att) {
    var index = name.indexOf(":");
    var prefix;
    var ns;
    
    if (index != -1) {
        prefix = name.substring(0, index);
        name = name.substring(index + 1);
    }

    if (!att || prefix) {
        ns = nsList[prefix || ""];

        while (!ns && parent) {
            if (parent.ns) {
                ns = parent.ns[prefix || ""];
            }

            parent = parent.parentNode;
        }
    }

    if (prefix && !ns) {
        throw "XML parser exception: prefix " + prefix + " not found";
    }
    
    return [ns, name];
};

function stringValue(value) {
    return typeof value != "object"? "" + value
        : (value.length == 0? "" : xmlValue(value[0]));
};

function booleanValue(value) {
    return typeof value == "undefined"? false
    	: (typeof value.length != "undefined"? value.length > 0 : !!value);
};

function numberValue(value) {
    return typeof value == "boolean"? 'A' - 0
        : (typeof value == "object"?  stringValue(value) : value) - 0;
};

function nodeSetValue(value) {
    if (typeof value != "object") {
        throw this + ' ' + Error().stack;
    }

    return value;
};

function xmlValue(node) {
    if (!node) {
        return "";
    }

    var ret = "";

    if (node.nodeType == NodeType.TEXT ||
        node.nodeType == NodeType.CDATA_SECTION ||
        node.nodeType == NodeType.ATTRIBUTE) {
        ret += node.nodeValue;
    } else if (node.nodeType == NodeType.ELEMENT ||
        node.nodeType == NodeType.DOCUMENT ||
        node.nodeType == NodeType.DOCUMENT_FRAGMENT) {

        for (var i = 0; i < node.childNodes.length; ++i) {
            ret += arguments.callee(node.childNodes[i]);
        }
    }

    return ret;
};

function xmlResolveEntities(s) {
    var parts = stringSplit(s, '&');
    var ret = parts[0];

    for (var i = 1; i < parts.length; ++i) {
        var p = parts[i];
        var index = p.indexOf(";");
        
        if (index == -1) {
            ret += parts[i];
            continue;
        }
        
        var rp = p.substring(0, index);
        var ch;

        switch (rp) {
            case 'lt': ch = '<'; break;
            case 'gt': ch = '>'; break;
            case 'amp': ch = '&'; break;
            case 'quot': ch = '"'; break;
            case 'apos': ch = '\''; break;
            case 'nbsp': ch = String.fromCharCode(160);  break;
            default:
                var span = window.document.createElement('span');
                span.innerHTML = '&' + rp + '; ';
                ch = span.childNodes[0].nodeValue.charAt(0);
        }

        ret += ch + p.substring(index + 1);
    }

    return ret;
};

function stringSplit(s, c) {
    var a = s.indexOf(c);

    if (a == -1) {
        return [ s ];
    }
  
    var parts = [];
    parts.push(s.substr(0,a));

    while (a != -1) {
        var a1 = s.indexOf(c, a + 1);

        if (a1 != -1) {
            parts.push(s.substr(a + 1, a1 - a - 1));
        } else {
            parts.push(s.substr(a + 1));
        } 

        a = a1;
    }

    return parts;
};
//Saját Stuff

function autoSave(i){
	if(i>0){
		gusztustalansag=true;
		
		//alert('saving...');
		//var wer=IdManager.find('submit').xfElement;
		//wer.submit();
		run(new XFDispatch('xforms-submit','submit',null,null),'console', "DOMActivate",true);
	}
	else{
		setTimeout("autoSave(1)", 1200000);
	}
}

function num2text(nsz) {
   
  hatv=new Array('','ezer','millió','milliárd','billió','billiárd','trillió','trilliárd', 'kvadrillió','kvadrilliárd','kvintillió','kvintilliárd','szextillió',
          'szextilliárd','szeptillió','szeptilliárd','oktillió','oktilliárd',
          'nonillió','nonilliárd','decillió','decilliárd','centillió');
     
  tizesek=new Array('','','harminc','negyven','ötven','hatvan','hetven','nyolcvan','kilencven');
   
  szamok=new Array('egy','ketto','három','négy','öt','hat','hét','nyolc','kilenc');
  tsz='';
  ej=(nsz<0?'- ':'');
  sz=trim(''+Math.floor(nsz));
  hj=0;
  if (sz=='0') {
    tsz='nulla';
  } else {
    while (sz>'') {
      hj++;
      t='';
      wsz=sz;
      wsz=wsz.substr(-3);
      wsz='00'+wsz.substr(-3);
      wsz=wsz.substr(-3);
      tizesek[0]=(wsz[2]=='0'?'tíz':'tizen');
      tizesek[1]=(wsz[2]=='0'?'húsz':'huszon');
      
      c=wsz[0];
      if (c!=0) {
          t=szamok[c-1]+'száz';
      }
      
      c=wsz[1];
      if (c!=0) {
          t+=tizesek[c-1];
      }
      
      c=wsz[2];
      if (c!=0) {
          t+=szamok[c-1];
      }
      
      tsz=(t?t+hatv[hj-1]:'')+(tsz==''?'':(nsz>2000?'-':''))+tsz;
      sz=(sz.substr(0,(sz.length-3)));
    }
  }
 return ej+tsz;
}

function trim(str) {
	return str.replace(/^\s+|\s+/g,"");
}
//else

//+ Jonas Raoni Soares Silva
     //@ http://jsfromhell.com/geral/utf-8 [v1.0]
     
     UTF8 = {
     	encode: function(s){
     		for(var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
     			s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]
  		);
  		return s.join("");
  	},
  	decode: function(s){
  		for(var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
  			((a = s[i][c](0)) & 0x80) &&
  			(s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
  			o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
  		);
  		return s.join("");
  	}
  };
function confirmation(next) {
	var answer = confirm("Nem mentett. Biztos, hogy továbblép?")
	if (answer){
		window.location = next;
	}
	else{
		
	}
}
function FormatNumberBy3(num, decpoint, sep) {
  // check for missing parameters and use defaults if so
  
  if (arguments.length == 2) {
    sep = ",";
  }
  if (arguments.length == 1) {
    sep = ",";
    decpoint = ".";
  }
  // need a string for operations
  num = num.toString();
  
  // separate the whole number and the fraction if possible
 a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";
  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    /*if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;*/
  }
  return x;
}
	


