/*
 * Love Framework by Neurik
 * © All rights reserved 2011 
 */

var LANG = {};

function extend(o1,o2){
	for(var i in o2){
		if(o2.hasOwnProperty(i)){
			o1[i] = o2[i];
		}
	}
}

extend(Object.prototype,{
	extend:function(anObject){
		extend(anObject,this);
	},
	implement:function(anObject){
		extend(this,anObject);
	},
	each:function(fn){
		for(var i in this){
			if(this.hasOwnProperty(i)){
				fn.apply(this[i],[i]);
			}
		}
	},
	keys:function(){
		var keys=[];
		this.each(function(k){ keys.push(k); });
		return keys;
	},
	toURI : function() {
		var str = [];
		this.each(function(k){ str.push(k + "=" + encodeURIComponent(this)); });
		return str.join("&");
	}
});

extend(Array.prototype,{
	each : function(fn){
		for(var i=0;i<this.length;i++){
			fn.apply(this[i],[i]);
		}
	},
	indexOf : function(item){
		for(var i=0; i<this.length; i++){
			if(this[i]==item){
				return i;
			}
		}
		return -1;
	},
	remove : function(item){
		var i = this.indexOf(item);
		if(i>-1){
			this.splice(this.indexOf(item),1);
		}
		return this;
	},
	toColor : function(){
		var r = [0,0,0];
		for(var i=0;i<3;i++){
			r[i]=parseInt(this[i] || 0).toString(16);
			if(r[i]<10) r[i] = "0"+r[i];
		}
		return ("#"+r[0]+r[1]+r[2]);
	}
});

extend(String.prototype,{
	toInt:function(){
		return parseInt(this,10);
	},
	toCamelCase : function(){
		return this.replace(/(\-[a-z])/g, function(match){
			return match.toUpperCase().replace('-','');
		});
	},
	toDashCase : function(){
		return this.replace(/([A-Z])/g, function(match){
			return "-" + match.toLowerCase();
		});
	},
	toColor : function(){
		var r = [0,0,0];
		var rgb = function(r,g,b){ return [r,g,b]; }
		var rgba = function(r,g,b,a){ return [r,g,b]; }
		if(this.match(/rgba/)) eval("r="+this);
		else if(this.match(/rgb/)) eval("r="+this);
		else {
			this.replace("#","");
			switch(this.length){
				case 3:
					r[0]=(this.charAt(0)+this.charAt(0)).toInt(16);
					r[1]=(this.charAt(1)+this.charAt(1)).toInt(16);
					r[2]=(this.charAt(2)+this.charAt(2)).toInt(16);
				case 6:
					r[0]=(this.charAt(0)+this.charAt(1)).toInt(16);
					r[1]=(this.charAt(2)+this.charAt(3)).toInt(16);
					r[2]=(this.charAt(4)+this.charAt(5)).toInt(16);
			}
		}
		return r;
	},
	a:1
});

var Events = {
	eventExtensions : {
		stop : function(){
			this.stopped=true;
			this.cancelBubble = true;
			if(this.returnValue) this.returnValue = false;
			if(this.PreventDefault) this.preventDefault();
			if(this.stopPropagation) this.stopPropagation();
		},
		key : function(){
			return (this.keyCode || this.which);
		}
	},
	
	addEvent : function(type,fn){
		if(!this.events) this.events = {};
		if(!this.events[type]) this.events[type] = [];
		this.events[type].push(fn);
		this["on"+type] = function(event){
			this.fireEvent(type,event||window.event);
		};
	},
	
	removeEvent : function(type,fn){
		var fn = fn || false;
		this.events[type] = fn ? this.events[type].remove(fn) : [];
		if(!fn) this["on"+type] = function(){};
	},
	
	fireEvent : function(type,event){
		var event = event || {};
		extend(event,this.eventExtensions);
		var context = this;
		if(!this.events) return;
		if(!this.events[type]) return;
		this.events[type].each(function(){
			if(!event.stopped) this.apply(context,[event]);
		});
	}
};

var Queue = function(f){
	var q = {
		items : [],
		remove_img:function(i){
			var q = this;
			var n = new Image();
			n.onload = function(){ q.remove(i); }
			n.onerror = function(){ q.remove(i); }
			n.src = i.src;
		},
		remove_link:function(i){
			var q = this;
			var t = setInterval(function(){
				if(true||i.cssRules||i.rules){
					q.remove(i);
					clearInterval(t);
				}
			},5);
		},
		remove_script:function(i,callback){
			var q = this;
			i.onreadystatechange= function(){ if (this.readyState == 'complete') q.remove(i); };
			i.onload = function(){ q.remove(i);  };
		},
		add:function(item){
			this["remove_"+item.tagName.toLowerCase()]($(item));
			this.items.qIndex = this.items.length;
			this.items.push(item);
		},
		remove:function(item){
			this.items.splice(item.qIndex,1);
			this.trigger();
			
		},
		trigger:function(){
			if(this.isReady && this.items.length==0){
				this.fireEvent("complete");
			}
		},
		ready:function(){ this.isReady=true; this.trigger(); },
		isReady:false
	};
	q.implement(Events);
	q.addEvent("complete",f);
	return q;
}

var Request = function(o){
	var R = {
		url:window.location,
		data:undefined,
		async:true,
		loadScripts:true,
		loadStyles:true,
		loadImages:true,
		evalScripts:true,
		scripts:"",
		response:"",
		target:false,
		method:"POST",
		complete:function(){
			var x;
			if(this.target){
				while(this.target.hasChildNodes()){
					x = this.target.removeChild(this.target.lastChild);
					x = undefined;
				}
				$(this.target).innerHTML = this.response;
			}
			if(this.evalScripts && this.scripts!="") eval("(function(Ajax){" + this.scripts + "})(this);");
		},
		success:function(){
			var R = this;
			var pre = "<div style='display:none'>__</div>";
			var tmp=$(document.createElement("div"));
			tmp.innerHTML=pre+this.xhr.responseText;
			tmp.removeChild($("div",tmp)[0]);
			var q=false;
			if(this.loadScripts || this.loadStyles || this.loadImages){
				var head = $("head")[0];
				q = new Queue(function(){ R.fireEvent("complete"); });
			}
			var scl = head.getElements("script");
			var scn = tmp.getElements("script") || [];
			var sci = [];
			scn.each(function(){
				if(!this.src){
					R.scripts+="\n"+this.innerHTML;
				} else {
					var found = false;
					if(R.loadScripts){
						for(var i=0;i<scl.length;i++){
							if(scl[i].src==this.src) found=true;
						}
						if(!found){
							var s = document.createElement("script");
							s.type = "text/javascript";
							s.src = this.src;
							q.add(s);
							head.appendChild(s);
						}
					}
				}
				tmp.removeChild(this);
			});
			
			var ll = head.getElements("link");
			var ln = tmp.getElements("link") || [];
			var li = [];
			ln.each(function(){
				var found = false;
				if(R.loadScripts){
					for(var i=0;i<ll.length;i++){
						if(ll[i].href==this.href) found=true;
					}
					if(!found){
						q.add(this);
						head.appendChild(this);
					}
				}
			});
			var st = tmp.getElements("style") || [];
			st.each(function(){
				if(R.loadStyles){
					head.appendChild(this)
				}
			});
			if(this.loadImages){
				var i = tmp.getElements("img") || [];
				i.each(function(){
					q.add(this);
				});
			}
			this.response = tmp.innerHTML;
			tmp = undefined;
			if(false===q){
				this.fireEvent("complete");
			} else {		
				q.ready();
			}
		},
		error:function(){
			var x;
			if(this.target){
				while(this.target.hasChildNodes()){
					x = this.target.removeChild(this.target.lastChild);
					x = undefined;
				}
				$(this.target).innerHTML = "Unable to fetch data. Response status : "+this.xhr.status;
			}
		},
		load:function(){},
		stateChange:function(xhr){
			if(xhr.readyState==4){
				if(this.target){
					var el = $(this.target);
					while(!el.getElements(".overlay")){
						el = $(el.parentNode);
						if(!el || null===el) break;
					}
					if(!el || null===el) return;
					var o = el.getElements(".overlay")[0];
					delete o;
				}
				this.fireEvent((xhr.status==200 ? "success" : "error"));
			}
		},
		get:function(o){ this.send("GET",o||{}); },
		post:function(o){ this.send("POST",o||{}); },
		send:function(M,o){
			var M = M || this.method;
			this.implement(o||{});
			if(this.target) loading(this.target);
			this.xhr = new XMLHttpRequest();
			var R = this;
			this.xhr.onreadystatechange = function(){ R.stateChange.apply(R,[R.xhr]); };
			this.xhr.open(M,this.url,this.async);
			if(M!="GET") this.xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			this.data = this.data ? this.data.toURI() : undefined;
			this.xhr.send(this.data);
		}
	};
	R.implement(Events);
	R.addEvent("success",function(){ this.success(); });
	R.addEvent("complete",function(){ this.complete(); });
	R.addEvent("error",function(){ this.error(); });
	R.implement(o);
	return R;
};

function Animation(el,options){
	var _this = this;
	this.subject = $(el);
	var settings = {
		from : false,
		to : false,
		frames : 100,
		interval : 10,
		transition : "linear"
	};
	settings.implement(options)
	this.options = settings;
	this.frames = [];
	for(var i=0;i<this.options.frames;i++) this.frames.push({});
	var to = this.parseStyle(this.options.to || "adummyclass");
	var from = this.parseStyle(this.options.from || "adummyclass");
	to.each(function(i){
		if(!from[i] || !to[i]) return;
		if(from[i]==to[i]) return;
		_this.make(String(i),from[i],to[i]);
	});
}
Animation.prototype = {
	direction : 1,
	properties : {
		discrete : ['background','background-attachment','background-image','background-repeat','border-collapse','border-style','border-top-style','border-right-style','border-bottom-style','border-left-style','clear','cursor','direction','display','float','font','font-family','font-size-adjust','font-stretch','font-style','font-variant','font-weight','list-style','list-style-image','list-style-position','list-style-type','outline','outline-style','overflow','position','text-align','text-decoration','vertical-align','visibility','white-space','overflow-x','overflow-y'],
		numerical : ['border-spacing','border-top','border-top-width','border-right-width','border-bottom-width','border-left-width','border-width','bottom','font-size','height','left','letter-spacing','line-height','margin','margin-top','margin-right','margin-bottom','margin-left','max-height','max-width','min-height','min-width','outline-width','padding','padding-top','padding-right','padding-bottom','padding-left','right','top','width','word-spacing','z-index','opacity'],
		color : ['background-color','border-color','border-top-color','border-right-color','border-bottom-color','border-left-color','color','outline-color']
	},
	parseStyle : function(style){
		if(typeof(style)=="object") return style;
		var parsed = {};
		if(typeof(style)=="string"){
			var oldClass = this.subject.className;
			var oldHeight = this.subject.style.height;
			var oldWidth = this.subject.style.width;
			this.subject.className = style;
			this.subject.style.height = this.subject.innerHeight();
			this.subject.style.width = this.subject.innerWidth();
			var styles = this.properties.discrete.concat(this.properties.numerical,this.properties.color);
			for(var i=0;i<styles.length;i++){
				parsed[styles[i]] = this.subject.getStyle(styles[i]);
			}
			this.subject.className = oldClass;
			this.subject.style.height = oldHeight;
			this.subject.style.width = oldWidth;
		}
		return parsed;
	},
	make_sprite : function(from,to){
		var r=[],p=0;
		for(var i=0;i<this.frames.length;i++){
			if(i==0) r.push(from);
			else if(i==this.frames.length-1) r.push(to);
			else {
				p=this.transitions[this.options.transition](i/this.frames.length);
				r.push((from+(p*(to-from))).toFixed());
			}
		}
		return r;
	},
	make_discrete : function(property,from,to){
		this.frames[0][property] = from;
		this.frames[this.frames.length-1][property] = to;
	},
	make_numerical : function(property,from,to){
		var u = to.match(/[a-zA-Z]./) || "";
		var from = parseFloat(from);
		var to = parseFloat(to);
		var r = this.make_sprite(from,to);
		var p = "ADummyValue";
		for(var i=0;i<r.length;i++){
			if(r[i]==p) continue;
			this.frames[i][property] = r[i]+u;
			p=r[i];
		}
	},
	make_color : function(property,from,to){
		var from = from.toColor();
		var to = to.toColor();
		var r = this.make_sprite(from[0],to[0]);
		var g = this.make_sprite(from[1],to[1]);
		var b = this.make_sprite(from[2],to[2]);
		var rtn = [];
		for(var i=0;i<r.legnth;i++){
			rtn.push([r[i],g[i],b[i]].toColor());
		}
	},
	make : function(property,from,to){
		if(this.properties.color.indexOf(property)>-1) return this.make_color(property,from,to);
		if(this.properties.numerical.indexOf(property)>-1) return this.make_numerical(property,from,to);
		return this.make_discrete(property,from,to);
	},
	transitions : {
		linear:function(p){ return p; },
		easeIn:function(p){ return Math.pow(p,3); },
		easeOut:function(p) { return 1-Math.pow(1-p,3); },
		easeInOut: function(p){ return ((-Math.cos(p*Math.PI)/2) + 0.5); }
	},
	animation : null,
	play : function(){
		var _this = this;
		if(this.frames.length==0) return;
		if(!this.playhead) this.playhead=0;
		if(this.direction==1 && this.playhead==this.frames.length-1) return;
		if(this.direction==0 && this.playhead==0) return;
		this.animation = setInterval(function(){ 
			_this.frames[_this.playhead].each(function(k){
				_this.subject.setStyle(k,this);
			});
			if(_this.direction==1 && _this.playhead<_this.frames.length-1){
				return _this.playhead++;
			}
			if(_this.direction==0 && _this.playhead>0){
				return _this.playhead--;
			}
			clearInterval(_this.animation);
		},this.options.interval);
	},
	reverse : function(){
		this.direction = 0;
		this.play();
	},
	toggle : function(){
		this.pause();
		this.direction = this.direction==0 ? 1 : 0;
		this.play();
	},
	pause : function(){
		if(this.animation) clearInterval(this.animation);
	},
	stop : function(){
		this.pause();
		this.playhead = 0;
		this.direction = 1;
	}
};

var Element = {
	getElements: function(selector){
		return $(selector,this);
	},
	replaceWith : function(node){
		this.parentNode.replaceChild(node,this);
	},
	append : function(node){
		if(typeof(node)=="string"){
			this.innerHTML+=node;
		} else {
			this.appendChild(node);
		}
	},
	prepend : function(node){
		if(typeof(node)=="string"){
			this.innerHTML = node+this.innerHTML;
		} else {
			this.insertBefore(node,this.firstChild);
		}
	},
	hasClass : function (value) {
		return !!(this.className.match(new RegExp('(\\s|^)' + value + '(\\s|$)')));
	},
	addClass : function (value) {
		if (!this.hasClass(value)) this.className += " " + value;
		return this;
	},
	removeClass : function (value) {
		if (this.hasClass(value)) {
			this.className = this.className.replace(new RegExp('(\\s|^)' + value + '(\\s|$)'), ' ');
		}
		return this;
	},
	toggleClass : function (value) {
		this.hasClass(value) ? this.removeClass(value) : this.addClass(value);
		return this;
	},
	getStyle : function(property){
		if (this.currentStyle){
			switch(property){
				case "float":
					return this.currentStyle["styleFloat"];
				case "opacity":
					var re = /\Wopacity\s*=\s*([\d]+)/i;
					return ((!re.test(this.currentStyle.filter)) ? 1 : re.exec(this.currentStyle.filter)[1]/100) ;
				default:
					if(!this.currentStyle[property]) return "0";
					return this.currentStyle[property];
			} 
		} else {
			 return document.defaultView.getComputedStyle(this,null).getPropertyValue(property);
		}
	},
	getStyles : function(styles,className){
		var className = className || false, el = this, r = {}, remove=false;
		if(className && !this.hasClass(className)){
			this.addClass(className);
			remove = true;
		}
		styles.each(function(){
			r[this] = el.getStyle(this);
		});
		if(remove) this.removeClass(className);
		return r;
	},
	setStyle : function(property, value){
		if(!property) return;
		switch(property){
			case "float" : this.currentStyle ? this.style.styleFloat = value : this.style.cssFloat = value ; break;
			case "opacity" :
				if(this.currentStyle){
					if(!this.currentStyle.hasLayout) this.style.zoom = 1;
					this.style.filter = (value == 1) ? '' : 'alpha(opacity=' + value * 100 + ')';
					break;
				}
			default : 
				this.style[property.toCamelCase()] = value;
		}
	},
	setStyles : function(styles){
		var styles = styles || {};
		for(var i in styles) if(styles.hasOwnProperty(i)) this.setStyle(i,styles[i]);
	},
	innerWidth : function(){
		return this.offsetWidth;
	},
	innerHeight : function(){
		return this.offsetHeight;
	},
	outerWidth : function(){
		return (
			this.innerWidth()
			+ this.getStyle("border-left-width").replace("px","").toInt()
			+ this.getStyle("border-right-width").replace("px","").toInt()
		);
	},
	outerHeight : function(){
		return (
			this.innerHeight()
			+ this.getStyle("border-top-width").replace("px","").toInt()
			+ this.getStyle("border-bottom-width").replace("px","").toInt()
		);
	},
	click : function(fn){
		this.setStyle("cursor","pointer");
		this.addEvent("click",fn);
		this.hover();
	},
	hover : function(fnIn,fnOut){
		this.addEvent("mouseover",function(){
			this.addClass("hover");
		});
		this.addEvent("mouseout",function(){
			this.removeClass("hover");
		});
		if(fnIn) this.addEvent("mouseover",fnIn);
		if(fnOut) this.addEvent("mouseover",fnOut);
	},
	ajax:function(options){
		var r = new Request(options);
		r.send();
	},
	get:function(url,options){
		var options = options || {};
		options.url = url;
		if(false!==options.target) options.target = this;
		var r = new Request();
		r.get(options);
	},
	post:function(data,options){
		var options = options || {};
		options.data = data;
		if(false!==options.target) options.target = this;
		var r = new Request();
		r.post(options);
	},
	slide : function(options){
		this.addClass("fx_slide");
		var settings = {
			transition : "linear",
			frames : 20,
			interval : 10
		};
		settings.implement(options||{});
		if(!this.fx) this.fx={};
		if(!this.fx.slider){
			settings.from = this.hasClass("fx_slide_out") || this.innerHeight()==0 ? "fx_slide_out" : "fx_slide_in";
			settings.to = settings.from=="fx_slide_in" ? "fx_slide_out" : "fx_slide_in";
			this.removeClass("fx_slide_in");
			this.removeClass("fx_slide_out");
			this.fx.slider = new Animation(this,settings);
			this.fx.slider.play();
		} else {
			this.fx.slider.toggle();
		}
	},
	extended : true
};

var loading = function(el){
	var over = document.createElement("div");
	over.className = "loader overlay";
	var t = document.createElement("table");
	var r,c = null;
	for(var i=0;i<3;i++){
		r = t.insertRow(i);
		for(var j=0;j<3;j++){
			c = r.insertCell(j);
			if((i==0 && j==0) || (i==2 && j==2)){
				c.className = "spacer";
			}
		}
	}
	var spinner = document.createElement("div");
	spinner.className = "spinner";
	t.rows[1].cells[1].appendChild(spinner);
	over.appendChild(t);
	var pos = $(el).getStyle("position");
	if(pos=="relative"){
		over.style.height = "100%";
		over.style.width = "100%";
		over.style.top = "0px";
		over.style.left = "0px";
		el.appendChild(over);
	} else {
		var par = $(el.parentNode);
		while(!par.getStyle("position")=="relative"){
			par = $(par.parentNode);
		}
		par.appendChild(over);
		over.sync = setInterval(function(){
			if(!over.parentNode) clearInterval(over.sync);
			over.style.height = el.style.height;
			over.style.width = el.style.width;
			over.style.top = el.style.top;
			over.style.left = el.style.left;
		},50);
	}
	over.spin = setInterval(function(){
		if(!over.parentNode) clearInterval(over.spin);
		if(spinner.style.backgroundPosition=="-280px"){
			spinner.style.backgroundPosition = "0px";
		} else {
			spinner.style.backgroundPosition = (spinner.style.backgroundPosition.toInt()||0) - 40 + "px";
		}
	},100);
}

var Select = function(item,context){
	var E = function(e,filter){
		if(!e) return false;
		if(e.extended) return e;
		var r = [];
		var re = new RegExp('(\\s|^)' + (filter||"") + '(\\s|$)');
		if(e.tagName){
			e.extended = true;
			extend(e,Element);
			return e;
		}
		for(var i=0;i<e.length;i++){
			if(!filter || e[i].className.match(re)){
				if(!e[i].extended){
					e[i].extended = true;
					extend(e[i],Element);
				}
				r.push(e[i]);
			}
		}
		return (r.length==0 ? null : r);
	}
	var context = context || document;
	if(item.extended) return item;
	if(item.tagName) return E(item);
	switch(item[0]){
		case "#": return E(document.getElementById(item.replace("#","")));
		case ".": return E(context.getElementsByTagName("*"),item.replace(".",""));
		default : return E(context.getElementsByTagName(item),false);
	}
}

var $ = Select;
extend(window,Events);
extend(document,Events);
extend(Element,Events);

var Viewport = {
	docEle : function(){
		if (typeof document.documentElement != 'undefined'
		&& typeof document.documentElement.clientWidth != 'undefined'
		&& document.documentElement.clientWidth != 0) return true;
		return false
	},
	x : function(){
		if(window.innerWidth) return window.innerWidth;
		if(this.docEle) return document.documentElement.clientWidth;
		return false;
	},
	y : function(){
		if(window.innerHeight) return window.innerHeight;
		if(this.docEle) return document.documentElement.clientHeight;
		return false;
	}
};

if(!window.innerWidth){
	window.addEvent("resize",function(){
		window.innerWidth = Viewport.x();
		window.innerHeight = Viewport.y();
	});
	window.fireEvent("resize");
}

window.addEvent("load",function(){ this.isloaded=true; });
var Ready = function(fn){
	window.onload = function(){ window.fireEvent("load"); };
	if(window.isloaded) fn();
	else window.addEvent("load",fn);
};


var validationRules = {
	required : function(v){ return v ? true : false; },
	numeric : function(v){  return !isNaN(parseFloat(n)) && isFinite(n); },
	email : function(v){ return (/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i.test(v)); },
	url : function(v){ return (/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(v)); },
	date : function(v){
		var v = v;
		if(!v) return true;
		if(v.length!=10) return false;
		v = v.split("-");
		if(v.length!==3)  return false;
		for(var i=0;i<3;i++) v[i] = v[i]*1;
		var days = [0,31,(parseInt(v[0])%4==0?29:28),31,30,31,30,31,31,30,31,30,31];
		if(parseInt(v[0])<1000 || parseInt(v[0]>3000)) return false;
		if(parseInt(v[1])<1 || parseInt(v[1])>12) return false; 
		if(parseInt(v[2])<1 || parseInt(v[2])>days[parseInt(v[1])]) return false; 
		return true;
	},
	greaterThan : function(v,l){ return v>l; },
	lessThan : function(v,l){ return v<l; },
	sqlname : function(v){ return true; return (/^[a-zA-Z][a-zA-Z0-9_]*$/.test(v)); }
}
LANG.validate = {
	required : "required",
	numeric : "must be number",
	email : "must be a valid email address",
	date : "dates must be in ISO format\n(example:2011-01-31)",
	url : "this field must be a valid URL\n(example:http://www.mydomain.com)",
	greaterThan : "must be greater than %",
	lessThan : "must be less than %",
	sqlname : "name must start with a letter and contain only letters and numbers."
}
function Validate(form,rules){
	var form = $(form);
	if(!form) return false;
	if(!rules) return true;
	var result = true;
	var values = {};
	for(var i=0;i<form.elements.length;i++){
		if(!form.elements[i].name) continue;
		if(values[form.elements[i].name]) continue;
		values[form.elements[i].name] = form.elements[i].value;
	}
	for(var name in rules){
		if(!rules.hasOwnProperty(name)) continue;
		for(var i=0,fn="",param="";i<rules[name].length;i++){
			switch(typeof(rules[name][i])){
				case "string":
					fn = rules[name][i];
					param = null;
					break;
				case "object":
					fn = rules[name][i].keys()[0];
					param = rules[name][i][fn];
					break;
			}
			if(fn=="required" && values[name]==""){
				result = false;
			} else if(fn!="required" && values[name]!=""){
				result = validationRules[fn+""](values[name],param);
			}
			if(!result){
				form.elements[name].focus();
				if(!LANG.validate[fn]) alert(fn);
				alert(LANG.validate[fn].replace("%",param));
				return false;
			}
		}
	}
	return true;
}

function FieldType(options){
	var type = {
		filters : [
			"contains","does_not_contain",
			"equals","does_not_equal",
			"is_greater_than", "is_less_than",
			"is_empty","is_not_empty"
		],
		filter_boolean : function(){
			return "<input type='hidden' class='value_field' value='1'>";
		},
		filter_text : function(){
			return "<input type='text' class='value_field' value=''>";
		},
		filter_contains : function(){
			return this.filter_text();
		},
		filter_does_not_contain : function(){
			return this.filter_text();
		},
		filter_equals : function(){
			return this.filter_text();
		},
		filter_does_not_equal : function(){
			return this.filter_text();
		},
		filter_is_greater_than : function(){
			return this.filter_text();
		},
		filter_is_less_than : function(){
			return this.filter_text();
		},
		filter_is_empty : function(){
			return this.filter_boolean();
		},
		filter_is_not_empty : function(){
			return this.filter_boolean();
		},
		validate_required : function(v){
			return (v=="" ? "This field is required" : true);
		}
	};
	extend(type,options||{});
	return type;
}


function changeTheme(name){
	var styles = $("link");
	var prev = "";
	var head = document.head;
	for(var i=0;i<styles.length;i++){
		if(!styles[i].href.match(/\/themes\//)) continue;
		current = styles[i];
		prev = styles[i].href.split("/");
		prev = prev[prev.length-2];
		break;
	}
	var style = document.createElement("link");
	style.rel = "stylesheet";
	style.type = "text/css";
	style.href = current.href.replace(prev,name);
	//document.getElementsByTagName("head")[0].replaceChild(style,current);
	document.getElementsByTagName("head")[0].removeChild(current);
	document.getElementsByTagName("head")[0].appendChild(style);
}

