function hasId(elem, id){
	return elem && (elem.id == id);
}
ObjectParser = function(){
	this.rules = new Array();
	this.defaultFnc = function(){return true;};
	this.parseObjects = function(){
		for (var i=0; i<this.rules.length; i++){
			this.rule_tree = this.rules[i].rule_tree;
			this.handler = this.rules[i].handler;

			this.putRule.apply(this,[document,0]);
		}
	};
	this.execRule = function(rule){
		var ruleInd = 0;
		if (typeof rule == "string"){
			for (var i=0; i<this.rules.length; i++){
				if (this.rules[i].rule == rule){
					ind = i;
				}
			}
		}else if (typeof rule == "number"){
			ruleInd = parseInt(rule) || 0;
		}
		if (ruleInd > 0){
			this.rule_tree = this.rules[ruleInd].rule_tree;
			this.handler = this.rules[ruleInd].handler;

			this.putRule.apply(this,[document,0]);
		}
	};
	this.addRule = function(str_rule, handler){
		var objs = str_rule.split(" ");
		var parseRule = new RegExp('^([a-zA-Z0-9\_]*)(?:([\.]|[\#])([a-zA-Z0-9\_]+))?$');

		var matches = [];
		var ruleInd = this.rules.push({rule:str_rule, "handler" : handler,rule_tree : []})-1;
	 	for (var i=0; i<objs.length; i++){
			matches = objs[i].match(parseRule);
	/* 		var tag = matches[1];
			var tp = matches[2];
			var prop = matches[3]; */
	                this.rules[ruleInd].rule_tree.push([matches[1],matches[2],matches[3]]);
		}
		return ruleInd;
	};
	this.putRule = function(lastp, last_ind){
		var j=last_ind;
		//tag name
		var elems;
		if (this.rule_tree[last_ind]){
			if (this.rule_tree[last_ind][0] != ''){
				elems = lastp.getElementsByTagName(this.rule_tree[last_ind][0]);
			}else{
				if (this.rule_tree[last_ind][1] == '#'){
					elems = [document.getElementById(this.rule_tree[last_ind][2])];
				}else{
					elems = lastp == document ? document.body.childNodes : lastp.childNodes;
				}
			}
			//class or id
			var testFnc = this.defaultFnc;
			if (this.rule_tree[last_ind][1] != '' && this.rule_tree[last_ind][2] != ''){
				switch(this.rule_tree[last_ind][1]){
					case '.' : testFnc = whi.has_class; break;
					case '#' : testFnc = hasId; break;
				}
			}
			for (var k=0; k<elems.length; k++){
				if (elems[k]){
					if (testFnc(elems[k], this.rule_tree[last_ind][2]) != false){
						if (last_ind == this.rule_tree.length-1){
							this.handler.apply(elems[k]);
						}else{
							this.putRule.apply(this, [elems[k], last_ind+1]);
						}
					}
				}
			}
		}
	};
	this.getElements = function(){
	};
};

var jsRules = new ObjectParser();
jsRules.addRule("form.js_submit",function(){
	//whi.events.addEvent(this,'keydown',function(e){submitEnter(e, this)});
	this.onkeydown = function(e){
		enterSubmit(e, this);
	}
	//TODO: nu merge daca e si onsubmit
});
jsRules.addRule("form.js_validate",function(){
	whi.events.addEvent(this,'submit',function(){return validate_form(this); });
});
jsRules.addRule("a.js_external",function(){
	this.setAttribute("target","_blank");
});
jsRules.addRule("a.js_submit_button",function(){
	var p = this;
	var frm = null;
	while(p = p.parentNode){
		if (p.tagName && p.tagName.toLowerCase() == "form"){
			frm = p;
			break;
		}
	}
	if (frm){
		for (var i=0; i<document.forms.length; i++){
			if (document.forms[i] == frm){
				this.href = "javascript:submit_form("+i+");";
			}
		}
	}
});

function submit_form(frm){
	try{
		var cr_form;
		if (typeof frm == "object") cr_form = frm;
		else{
			cr_form = document.forms[typeof frm == "undefined" ? 1 : frm];
		}
		if (typeof cr_form.onsubmit == "function"){
			if (cr_form.onsubmit() != false){
				cr_form.submit();
			}
		}else{
			cr_form.submit();
		}
	}catch(e){}
}

//whi.events.addEvent(window,'load',jsRules.parseObjects.bind(jsRules));
whi.events.addEvent(window,'DOMContentLoaded',jsRules.parseObjects.bind(jsRules));

