function xinput(name,scope){

	loadScript('/textbox.cls.js');
	loadScript('/grid.cls.js');
	loadScript('/dropdown.cls.js');

	this._name = name;
	this.scope = scope;
	this.inputWidth=280;
	this.captionWidth=60;
	this.useDropdown=false
	this.noHeadingDropDown=false
	
	var frameIdDot = (this.scope==undefined || this.scope==window)? '': this.scope.name+'.';
	this._objName = frameIdDot+this._name;
	
	if($('overlay')){
		this.overlay =  $('overlay')
	}else{
		var overlay= document.createElement('div');
		this.overlay =  document.body.appendChild(overlay);
		this.overlay.style.visibility='hidden'
		this.overlay.className="overlay";	
		this.overlay.id='overlay';
		linkEvent(document.body, 'mousemove', new Function ('event', this._objName+'._mouseMove(event)'))
	
	}
	
	if($(this._name)){	
		this.box = $(this._name)
	}else{ 
		var box= document.createElement('div');
		this.box =  document.body.appendChild(box);

		this.box.style.visibility='hidden'
		this.box.style.top=0
		this.box.style.left=0
		this.box.className="xinput";
		this.box.id=this._name;
	}
	
	this.box.innerHTML=
	'<div id="'+this._name+'_title" class="xinput_title" \
		onclick="'+this._objName+'._titleClick(event)" \
		onmousedown="'+this._objName+'._mouseDown(event)" \
		onmouseup="'+this._objName+'._mouseUp(event)" >\
	</div> \
	<div id="'+this._name+'_content" class="xinput_content"> </div>\
	<div style="float:right;padding:4px;margin:3px;text-align:center;vertical-align:middle">\
	<input id="'+this._name+'_ok" type="button" style="background:url(/ok.png) no-repeat 1px center;padding-left:15px;width:60px" value="OK" onclick="'+this._objName+'._okClick()" tabindex=2>\
	<br><input id="'+this._name+'_cancel" type="button" style="background:url(/cancel.png) no-repeat 1px center;padding-left:15px;width:60px" value="Cancel" onclick="'+this._objName+'._cancelClick()" tabindex=3>\
	</div>	';
	
	
	this.itemKeys= [];	
	this.add= function(name,caption,value,lov,conn,sql,height,required){
		var xi = new xiItem(name,'s',caption,value,lov,conn,sql,height,required);
		this.itemKeys[this.itemKeys.length]=name;
		this[name]=xi;
	}
	this.addDate= function(name,caption,value,lov,conn,sql,height,required){
		var xi = new xiItem(name,'d',caption,value,lov,conn,sql,height,required);
		this.itemKeys[this.itemKeys.length]=name;
		this[name]=xi;
	}
	this.addNum= function(name,caption,value,lov,conn,sql,height,required){
		var xi = new xiItem(name,'n',caption,value,lov,conn,sql,height,required);
		this.itemKeys[this.itemKeys.length]=name;
		this[name]=xi;
	}
	this.clear= function(){		
		this.itemKeys=[]		
	}
	this._refresh= function(){
		var aItems ='Array(';
		for (var i = 0; i < this.itemKeys.length; i++){
		 aItems+= this[this.itemKeys[i]].phpArray() ;
		 if(i < this.itemKeys.length-1)aItems+= ",";
		} 
		aItems+= ")";
		srv = new clsAjaxCall('xinput.cls', 0, 'show()')
		srv.setVar ("name",this._name)
		srv.setVar ("aItems",aItems)		
		srv.setVar ("inputWidth",this.inputWidth)
		srv.setVar ("captionWidth",this.captionWidth)
		srv.setVar ("useDropdown",this.useDropdown)
		srv.setVar ("noHeadingDropDown",this.noHeadingDropDown)
		
		srv.element = this._name+'_content';
		srv.runAJAX(); 
		linkChangeFocus(this.box);
		for (var i = 0; i < this.itemKeys.length; i++){
		 var txtb = this._name+'_'+this.itemKeys[i];
		 linkEvent( $(txtb+'_txt'), 'keydown',new Function ('event', this._objName+'._keydown('+i+',event)'));	
		}
		this.__loaded=true;
	}
	
	this.show= function(title, inputWidth,captionWidth){
//alert(textbox)
		if(title!==undefined) this._title=title;
		if(inputWidth!==undefined) this.inputWidth=inputWidth;
		if(captionWidth!==undefined) this.captionWidth=captionWidth;
		if(!this.scope) throw('xinput scope not defined')
		this.__fid = this.scope.__fid
		if(!this.__loaded)this._refresh();
		
		this.box.style.width=this.inputWidth;
		if(this._title==undefined) this._title='Input data';
		$(this._name+'_title').innerHTML=title;
		this.box.style.left = (getWindowSize('w')- this.box.offsetWidth)/2 
		this.box.style.top = (getWindowSize('h')- this.box.offsetHeight)/2.5
		this.overlay.style.visibility='visible'
		this.box.style.visibility='visible'
		this.box.focus()
			
		this.contentBox=$(this._name+'_content');
		disableSelection($(this._name+'_title'));

		
		if(typeof textbox !== 'function') {
			var scrFixload=''			
			for (var i = 0; i < this.itemKeys.length; i++){
			 var txtb = this._name+'_'+this.itemKeys[i];			 
			 scrFixload+=txtb+".control = textbox;"+txtb+".control();"+txtb+".captionWidth="+captionWidth+";"+txtb+".resize();"
			}
			setTimeout(new Function (scrFixload),100)
		}		

		$(this._name+'_'+this.itemKeys[0]+'_txt').focus()		
		
	}
	this._keydown = function(keyIndex,e){
		kc=keycode(e);		
		
		if(this[this.itemKeys[keyIndex]].hasDropdown() ) {
			if(this.useDropdown ) {
				if(eval(this._name+'_'+this.itemKeys[keyIndex]+'_dd').dropped) return
			}else{
				return
			}
		}
		
		if(kc==40 || kc==38){
			if(kc==40)	nextKey = keyIndex+1;
			if(kc==38)	nextKey = keyIndex-1;
			if(nextKey<0) nextKey=0		
			if(nextKey>=this.itemKeys.length) nextKey=this.itemKeys.length-1
			
			var nextTxt = $(this._name+'_'+this.itemKeys[nextKey]+'_txt');
			nextTxt.focus()
			nextTxt.focus()
			stopEvent(e);
		}
		if(kc==13 && e.ctrlKey) {stopEvent(e);this._okClick();}
		if(kc==27) {stopEvent(e);this._cancelClick();}
		
	
	}
	this.hide= function(){	
		this.overlay.style.visibility='hidden'
		this.box.style.visibility='hidden'
	}

	this._titleClick = function(e){
		e=mouseEvent(this.box,e)
		this.mX=e.targetX
		this.mY=e.targetY
		if(this.mX > $(this._name+'_title').offsetWidth-16) this._cancelClick()		
	}
	this._mouseDown = function(e){
		e=mouseEvent(this.box,e)
		this.mX=e.targetX
		this.mY=e.targetY
		if(this.mX > $(this._name+'_title').offsetWidth-16) return
		this.mDown=true
	}
	this._mouseUp = function(e){
		this.mDown=false
	}
	this._mouseMove = function(e){		
		if(!this.mDown) return
		this.box.style.left = e.clientX - this.mX
		this.box.style.top = e.clientY - this.mY
	}
	this._okClick= function(){
		for (var i = 0; i < this.itemKeys.length; i++){
			this[this.itemKeys[i]].value = eval(this._name + '_' + this.itemKeys[i] + '.value()')
			if(this[this.itemKeys[i]].required && !this[this.itemKeys[i]].value) {
				alert('Enter '+this[this.itemKeys[i]].name) ; 
				$(this._name + '_' + this.itemKeys[i] + '_txt').focus()
				return
				}
		}	
		this.hide()
		if(this.ok) this.ok(this);
		return


	}
	this._cancelClick= function(){	
		this.hide()
		if(this.cancel) this.cancel(this);
	}
}

function xiItem(name,type,caption,value,lov,conn,sql,height,required){
		if(value==undefined)value='';
		if(lov==undefined)lov='';
		if(conn==undefined)conn='';
		if(sql==undefined)sql='';
		if(type==undefined)type='s';

		this.name=name
		this.caption=caption
		this.value=value;
		this.lov=lov;		
		this.conn=conn;
		this.sql=sql;
		this.type=type;
		this.height = ((height!==undefined)?height:1)
		this.required = ((required!==undefined)?required:false)
		
		this.hasDropdown = function(){
		return (sql) ? true :false
		}
		this.phpArray= function(){
			return 'Array("'+this.name+'","'+this.type+'","'+this.caption+'","'+this.value+'","'+this.lov+'","'+this.conn+'","'+this.sql+'","'+this.height+'","'+this.required+'")';
		}
		this.toString = function(){
			return this.value;
		}
}