var a_nodes=document.getElementsByTagName('select');
var n_nodes=a_nodes.length;
var i;
for(i=0;i<n_nodes;++i)
	a_nodes.item(i).setValue = function(val){	var i;	for(i=0;i < this.options.length;++i) if(this.options[i].value == val){ this.selectedIndex=i;	return;	}}
a_nodes=document.getElementsByTagName('form');
n_nodes=a_nodes.length;
for(i=0;i<n_nodes;++i){
	a_nodes.item(i).populate = function(a_obj){
		var p,a,val,n;
		this.reset();
		if(this.elements['code']) this.elements['code'].readOnly=false;
		if(a_obj){
			if(this.elements['code']) this.elements['code'].readOnly=true;
			for(p in a_obj){
				if(this.elements[p] && (this.elements[p].type=='text' || this.elements[p].type == 'hidden'))
					this.elements[p].value = a_obj[p];
			}
			if(a = this.elements['ctrl[]']){
				n = a.length;
				val=parseInt(a_obj.ctrl);
				if(isNaN(val)) val=0;
				if(a && val){
					if(typeof n == 'undefined'){//this is not a collection only 1 ctrl
						if(a.value & val) a.checked = true;	else a.checked=false;
					}else{
						for(i=0;i < n;++i)
							if(a.item(i).value & val) a.item(i).checked = true;	else a.item(i).checked=false;
					}
				}
			}
		}
		n = this.elements.length;
		for(p = 0;p < n;++p){
			if(this.elements.item(p).type == 'text' && !this.elements.item(p).readOnly){
				this.elements.item(p).select();
				this.elements.item(p).focus();
				return;//focus to only first text element
			}
		}
	}
	a_nodes.item(i).fail = function(msg,ix){
		window.alert(msg);
		this.elements[ix].focus();
		this.elements[ix].select();
		return false;
	}
}
a_nodes = document.getElementsByTagName('div');
n_nodes = a_nodes.length;
for(i=0;i < n_nodes;++i)
	if(a_nodes.item(i).className=='dialog'){
		a_nodes.item(i).close=function(){this.style.left='-500px';document.body.dimmed(false);}
		a_nodes.item(i).display=function(x,y){
			if(!isNaN(parseInt(x))) this.style.left=x + 'px'; else this.style.left = "400px";
			if(!isNaN(parseInt(y))) this.style.top=y + 'px'; else 	this.style.top='100px';
			document.body.dimmed();
			Drag.init(this.firstChild,this);
		}
	}
document.body.dimmed=function(v){
	if(arguments.length==0 || v)this.className+=' dimmed';
	else this.className = this.className.replace('dimmed','');
}