var rpglob={
	ni			: 1,
	eLast		: undefined,
	eLast_block	: undefined,
	iobj		: undefined,
	popup13		: undefined,
	popup13active	: undefined,

	p		: {
				pos:	0,
				a:	undefined,
				a_id:	undefined,
				a_text:	undefined,
				a_desc:	undefined,
				len:	0,
				old:	undefined
			  },

	listrp		: _listAllRP,
	getPrev		: _getPrevRP,
	findrp		: _findRP
};

// while(!document.body){};
initRPGlob();
function initRPGlob(){
	if(document.body){
		rpglob.popup13=initPopup();
		// alert(rpglob.popup13);
	}
	else{
		setTimeout(initRPGlob,300);
	}
}

// -----------------------------------------------

function initPopup(){
    if(rpglob.popup13 == undefined){
	// document.writeln('<DIV id="inpt_add">.</DIV>');
	var d1=document.createElement('DIV');
	var t1=document.createTextNode('.');
	d1.appendChild(t1);
	d1.id='inpt_add';
	d1.className='inpt_add';
	// d1.style.visibility='hidden';
	d1.style.position='absolute';
	d1.style.zIndex=1000;
	document.body.appendChild(d1);
	return d1.parentNode.removeChild(d1);
	// return d1;
    }
}

var updPopup_busy=0;

function updPopup(){
	if(updPopup_busy ==0){
		updPopup_busy=1;
		v=rpglob.iobj.value;
		if(v == '') hidePopup();
		else {
			while(rpglob.popup13.lastChild) rpglob.popup13.removeChild(rpglob.popup13.lastChild);
			var crp=rpglob.findrp(rpglob.iobj.id);

			if(v.length>0){
				v=encodeURIComponent(v);
				var link=crp.script_name+'?'+crp.script_var+'='+v;
				var res=loadDocAsync(link);
				var ary=res.match(/[^;]+/g);
				var gp=rpglob.p;
				gp.len=ary.shift();
				gp.a_id=[];
				gp.a_text=[];
				gp.a_desc=[];
				if(rpglob.p.len>0){
					for(i=0;i<ary.length;i++){
						var row=new String(ary[i]);
						var pa=row.match(/\{[^\}]*\}/g);
						for(j=0;j<pa.length;j++) pa[j]=new String(pa[j]);
						gp.a_id.push(pa[0].replace(/{|}/g,''));
						gp.a_text.push(pa[1].replace(/{|}/g,''));
						gp.a_desc.push(pa[2].replace(/{|}/g,''));
					}
					gp.pos=0;
					gp.old=v;
					var tab=genTab(gp.a_desc,crp);
					rpglob.popup13.appendChild(tab);
					var cltxt=document.createTextNode('Close (ESC)');
					var cl=document.createElement('A');
					cl.href="javascript:hidePopup();";
					cl.appendChild(cltxt);
					var div4=document.createElement('div');
					div4.className="inpt_add_close";
					div4.appendChild(cl);
					rpglob.popup13.appendChild(div4);
					rpglob.popup13active=1;
				}
			}
		}
		updPopup_busy=0;
	}
	if(rpglob.popup13active==1) showPopup();
	else hidePopup();
}

function swSels(st){
	var sel=document.getElementsByTagName('SELECT');
	for(i=0;i<sel.length;i++){
		sel[i].style.visibility=(st>0)?'visible':'hidden';
	}
}

function hidePopup(){
	if(rpglob.popup13active==1){
		swSels(1);
		rpglob.popup13.style.visibility='hidden';
		rpglob.popup13active=0;
		rpglob.popup13.parentNode.removeChild(rpglob.popup13);
	}
}

function showPopup(){
	swSels(0);
	var p=rpglob.popup13;
	var o=rpglob.iobj;
	o.parentNode.appendChild(p);
	p.style.visibility='visible';
	//alert(o.offsetParent.offsetTop);
	var pn=o;
	var otop=[];
	var oleft=[];
	var pos=[];
	/*
	while(pn){
		if(pn.offsetTop>0) otop.push(pn.offsetTop);
		if(pn.offsetLeft>0) oleft.push(pn.offsetLeft);
		pn=pn.parentNode;
	}
	*/
	p.style.top=(o.offsetHeight+o.offsetTop)+'px';
	p.style.left=o.offsetLeft+'px';

	// if(navigator.appName.match(/Explorer/)){
	//	p.style.top=(o.offsetHeight+o.offsetTop)+'px';
	//	p.style.left=(o.offsetLeft)+'px';
	// }
	// p.style.left=(o.offsetLeft+o.offsetParent.offsetLeft)+'px';
        //
        
        p.style.left='198px';
        p.style.top='0px';
        p.style.width='300px';

	o.form.onsubmit=prSubmit;
	rpglob.popup13active=1;
}

function genTab(ary,rp){
	var tab=document.createElement('TABLE');
	tab.className='inpt_add';
	var tb=document.createElement('TBODY');
	tab.appendChild(tb);
	for(i=0;i<ary.length;i++){
		// ---
		var tr=document.createElement('TR');
		tb.appendChild(tr);
		var td=document.createElement('TD');
		tr.appendChild(td);
		var a=document.createElement('A');
		td.appendChild(a);
		var txt=document.createTextNode(ary[i]);
		a.appendChild(txt);
		// ---
		a.id=rpglob.popup13.id+'_'+(i+1);
		// a.href=rp.form_action+'?'+rp.inpt_val.name+'='+Url.encode(rpglob.p.a_id[i]);
		//a.href='';
		a.className='inpt_add';
		a.onmouseover=mouseSel;
		a.onclick=mouseCh;

		td.id=rpglob.popup13.id+'_td_'+(i+1);
		td.className='inpt_add'+(((ary.length-i)>1)?'':' inpt_add_last');
		td.onmouseover=mouseSel;
		td.onclick=mouseCh;
	}
	return tab;
}

function rp(){
	this.id			= rpglob.ni;
				  rpglob.ni+=1;
	this.inpt		= undefined;
	this.inpt_old		= undefined;

	this.inpt_val		= undefined;

	this.alrt		= undefined;
	// this.form_input_id	= undefined;
	this.in_id_val		= undefined;
	this.in_id_text		= undefined;
	this.form_action	= undefined;
	this.script_name	= undefined;
	this.script_var		= undefined;
    // ------------------------ = -------------------------------------
	this.init		= __rpInit;
	this.test		= __rpTest;
    // ------------------------ = -------------------------------------
	this.prev		= undefined;
	this.next		= undefined;
	this.setPrev		= function(p){ this.prev=p; }
	this.setNext		= function(n){ this.next=n; }
	this.getPrev		= _getPrevRP;
    // ------------------------ = -------------------------------------
}

function __rpInit(){
	// this.inpt=document.getElementById(this.form_input_id);
	this.inpt=document.getElementById(this.in_id_text);
	this.inpt_val=document.getElementById(this.in_id_val);
	this.alrt=document.getElementById(this.alrt);
	this.form_action=this.inpt.form.action;
	if(rpglob.eLast){
		rpglob.eLast.setNext(this);
		this.prev=rpglob.eLast;
	}
	rpglob.eLast=this;
	reg_event(this.inpt,'keyup',prKeyUp);
	reg_event(this.inpt,'keypress',prKeyPress);
}

function reg_event(o,en,f){
	if(o.addEventListener) o.addEventListener(en,f,false);
	else if(o.attachEvent) o.attachEvent('on'+en,f);
}

function prKeyUp(ev){
	rpglob.iobj=(ev.srcElement)?ev.srcElement:this;
	var c=(ev.keyCode>0)?ev.keyCode:ev.which;
	if(c != 27 &&
	   c != 38 &&
	   c != 40 &&
	   c != 13 &&
	   c != 10){
		itemReset();
		setTimeout(updPopup,300);
	}
	else if(c==13)	prSubmit();
	else if(c==10)	prSubmit();
	else if(c==38)	moveCursor(1);
	else if(c==40)	moveCursor(2);
	else if(c==27)	hidePopup();
	return false;
}

function prKeyPress(ev){
	if(rpglob.popup13active==1){
		var c=(ev.keyCode>0)?ev.keyCode:ev.which;
		//if(c==38)	moveCursor(1);
		//else if(c==40)	moveCursor(2);
		//else if(c==27)	hidePopup();
		//else if(c==13)	alert(13);
	}
	// return false;
}

function prSubmit(){
	if(rpglob.popup13active==1){
		if(rpglob.p.pos>0){
			itemSelect(rpglob.p.pos);
			// rpglob.iobj.value=rpglob.p.a_text[rpglob.p.pos-1];
		}
	}
	hidePopup();
	setTimeout(unlockSubmit,300);
	return false;
}

function unlockSubmit(){
	if(rpglob.iobj){
		rpglob.iobj.form.onsubmit=function(){ return true; };
		rpglob.iobj=undefined;
	}
}

function moveCursor(narr){
	if(rpglob.p.len>0){
		if(rpglob.p.pos!=0) itemHL(rpglob.p.pos,0);
		if(narr==1) rpglob.p.pos=(rpglob.p.pos<=1)?rpglob.p.len:rpglob.p.pos-1;
		if(narr==2) rpglob.p.pos=(rpglob.p.pos==rpglob.p.len)?1:rpglob.p.pos+1;
		itemHL(rpglob.p.pos,1);
	}
}

function mouseSel(){
	var cur_id=this.id.match(/_(\d+)$/)[1];
	for(i=1;i<=rpglob.p.len;i++){
		itemHL(i,(i==cur_id)?1:0);
	}
	rpglob.p.pos=parseInt(cur_id);
}

function mouseCh(){
	var cur_id=this.id.match(/_(\d+)$/)[1];
	//rpglob.iobj.value=rpglob.p.a_text[cur_id-1];
	itemSelect(cur_id);
	hidePopup();
	setTimeout(unlockSubmit,300);
	return false;
}

function itemReset(){
	var crp=rpglob.findrp(rpglob.iobj.id);
	if(crp.inpt.value.length>0 && crp.inpt.value != crp.inpt_old){
		crp.inpt_val.value='';
		rpglob.error=1;
	}
	else{
		crp.inpt_val.value='';
		rpglob.error=0;
	}
}

function itemSelect(pos){
	pos--;
	var crp=rpglob.findrp(rpglob.iobj.id);
	// rpglob.iobj.value=rpglob.p.a_text[pos];
	crp.inpt.value=rpglob.p.a_text[pos];
	crp.inpt_old=rpglob.p.a_text[pos];
	crp.inpt_val.value=rpglob.p.a_id[pos];
	// crp.alrt.innerHTML='';
	rpglob.error=0;
}

function itemHL(pos,sw){
	var e=document.getElementById(rpglob.popup13.id+'_td_'+pos);
	e.style.backgroundColor=(sw>0)?'#a0a0f0':'';
}

function __rpTest(){
	alert(this.id);
}

function _listAllRP(){
	var list=new Array();
	erp=undefined;
	while(1){
		erp=this.getPrev(erp);
		if(erp==undefined) break;
		// list.push(erp.form_input_id);
		list.push(erp.in_id_text);
	}
	return list;
}

function _getPrevRP(erp){
	if(erp){
		if(erp.prev==undefined){
			this.eLast_block=undefined;
		}
		return erp.prev;
	}
	else{
		this.eLast_block=1;
		return this.eLast;
	}
}

function _findRP(id){
	var erp=undefined;
	while(1){
		erp=this.getPrev(erp);
		if(erp==undefined) break;
		// if(erp.form_input_id==id) return erp;
		if(erp.in_id_text==id) return erp;
	}
	return undefined;
}


