Global_run_event_hook = true;
Global_combo_array    = new Array();
Array.prototype.remove=function(dx){ 
    if(isNaN(dx)||dx>this.length){
		self.status='::::'+dx;
		return false;
	}
    for(var i=0,n=0;i<this.length;i++){  
        if(this[i]!=this[dx]){
            this[n++]=this[i];
        }
    }
    this.length-=1;
}
function ComboBox_make(){
    var bt,nm;
    nm = this.name+"txt";
    this.txtview = document.createElement("INPUT");
    this.txtview.type = "text";
    this.txtview.name = nm;
	ndivg=document.all?eval(this.name+"divg"):document.getElementById(this.name+"divg");
    this.txtview.value = ndivg.innerHTML;
    this.txtview.id = nm;
    this.txtview.className = "combo-input";
    this.view.appendChild(this.txtview);
   	this.txtview.onfocus = function(){this.blur();}
   	this.txtview.onselectstart = function () { return false; }
	this.txtview.onclick = new Function ("", this.name + ".toggle()");
    this.valcon = document.createElement("INPUT");
    this.valcon.type = "hidden";
    this.view.appendChild(this.valcon);
    var tmp = document.createElement("IMG");
    tmp.src = "___";
    tmp.style.width = "1px";
    tmp.style.height = "0";
    this.view.appendChild(tmp);
    var tmp = document.createElement("BUTTON");
    tmp.width = "20px";
    tmp.height = "20px";
    tmp.appendChild(document.createTextNode(""));
    tmp.className = "combo-button";
	this.view.appendChild(tmp);
   	tmp.onfocus = function () { this.blur(); }
	tmp.onclick = new Function ("", this.name + ".toggle()");
}

function ComboBox_choose(realval,txtval){
    if (this.name == "urun") {
	xmlsorgula("konum_tur2.php", "urun=" + realval, document.getElementById("tmp"));
//	xmlsorgula("economy2.php", "urun=" + realval, document.getElementById("tmp2"));
	var urundivg=document.getElementById("urundivg");
	urundivg.innerHtml = txtval;
	if (realval == 'otel') {
		document.getElementById("combo3").style.display = "block";
//		document.getElementById("combo4").style.display = "block";
	} else {
		document.getElementById("combo3").style.display = "none";
//		document.getElementById("combo4").style.display = "none";
	}
    }
    if (this.name == "bolge") {
	var bolgedivg=document.getElementById("bolgedivg");
	bolgedivg.innerHtml = txtval;
    }
    if (this.name == "urun_turu") {
	var urun_turudivg=document.getElementById("urun_turudivg");
	urun_turudivg.innerHtml = txtval;
    }
    this.value         = realval;
    var samstring = this.name+".view.childNodes[0].value='"+txtval+"'";
    window.setTimeout(samstring,1);
    this.valcon.value  = realval;
}

function ComboBox_mouseDown(e){
    var obj,len,el,i;
    el = e.target ? e.target : e.srcElement;
    while (el.nodeType != 1) el = el.parentNode;
    var elcl = el.className;
    if(elcl.indexOf("combo-")!=0){
        len=Global_combo_array.length;
        for(i=0;i<len;i++){
            curobj = Global_combo_array[i];
            if(curobj.opslist){
                curobj.opslist.style.display='none';
            }
        }
    }
}

function ComboBox_handleKey(e){
    var key,obj,eobj,el,strname;
    eobj = e;
    key  = eobj.keyCode;
    el = e.target ? e.target : e.srcElement;
    while (el.nodeType != 1) el = el.parentNode;
    elcl = el.className;
    if(elcl.indexOf("combo-")==0){
        if(elcl.split("-")[1]=="input"){
            strname = el.id.split("txt")[0];
            obj = window[strname];
            obj.expops.length=0;
            obj.update();
            obj.build(obj.expops,(this.deg>0?true:false));
			this.deg=1;
            if(obj.expops.length==1&&obj.expops[0].text==""){}//empty
            else{
				obj.opslist.style.display='block';
				}
            obj.value = el.value;
            obj.valcon.value = el.value;
        }
     }
}

function ComboBox_update(){
    var opart,astr,alen,opln,i,boo;
    boo=false;
    opln = this.opts.length;
    astr = this.txtview.value.toLowerCase();
    alen = astr.length;
    if(alen==0){
        for(i=0;i<opln;i++){
            this.expops[this.expops.length]=this.opts[i];
			boo=true;
        }
    } else {
        for(i=0;i<opln;i++){
            opart=this.opts[i].text.toLowerCase().substring(0,alen);
            if(astr==opart){
                this.expops[this.expops.length]=this.opts[i];
				boo=true;
            }
        }
    }
    if(!boo){
		this.expops[0]=new ComboBoxItem("","");
	}
}

function ComboBox_remove(index){
    this.opts.remove(index);
}

function ComboBox_add(){
    var i,arglen;
    arglen=arguments.length;
    for(i=0;i<arglen;i++){
        this.opts[this.opts.length]=arguments[i];
    }
}

function ComboBox_build(arr,kont){
    var str,arrlen;
    arrlen=arr.length;
    str = '<table class="combo-list-width" cellpadding=0 cellspacing=0>';
    var strs = new Array(arrlen);
    for(var i=0;i<arrlen;i++){
        strs[i] = '<tr>' +
			'<td class="combo-item" onClick="'+this.name+'.choose(\''+arr[i].value+'\',\''+arr[i].text+'\');'+this.name+'.opslist.style.display=\'none\';"' +
			'onMouseOver="this.className=\'combo-hilite\';" onMouseOut="this.className=\'combo-item\'" >&nbsp;'+arr[i].text+'&nbsp;</td>' +
			'</tr>';
    }
    str = str + strs.join("") + '</table>';
    if(this.opslist){
		this.view.removeChild(this.opslist);
	}
    this.opslist = document.createElement("DIV");
    this.obrl = document.createElement("BR");
    this.opslist.innerHTML=str;
    this.opslist.style.display='none';
    this.opslist.className = "combo-list";
    this.opslist.onselectstart=returnFalse;
    if(!kont){ this.view.appendChild(this.obrl);}
    this.view.appendChild(this.opslist);
}

function ComboBox_toggle(){
    if(this.opslist){
        if(this.opslist.style.display=="block"){
            this.opslist.style.display="none";
        } else {
            this.update();
            this.build(this.opts,true);
            this.view.style.zIndex = ++ComboBox.prototype.COMBOBOXZINDEX;
            this.opslist.style.display="block";
        }
    } else {
        this.update();
        this.build(this.opts,false);
        this.view.style.zIndex = ++ComboBox.prototype.COMBOBOXZINDEX;
        this.opslist.style.display="block";
    }
}

function ComboBox(){
    if(arguments.length==0){
        self.status="::::";
    }
    this.name     = arguments[0];
    this.mval     = arguments[2];
    this.par      = arguments[1]||document.body;
    this.view     = document.createElement("DIV");
    this.view.style.position='relative';
    this.opts  = new Array();
    this.expops   = new Array();
    this.value    = arguments[3]||0;
	this.deg=0;

	this.build  = ComboBox_build;
    this.make   = ComboBox_make;
    this.choose = ComboBox_choose;
    this.add    = ComboBox_add;
    this.toggle = ComboBox_toggle;
    this.update = ComboBox_update;
    this.remove = ComboBox_remove;

    this.make();
    this.txtview = this.view.childNodes[0];
    this.valcon  = this.view.childNodes[1];
    
    this.par.appendChild(this.view)

    Global_combo_array[Global_combo_array.length]=this;
    if(Global_run_event_hook){
		ComboBox_init();
	}
}

ComboBox.prototype.COMBOBOXZINDEX = 1000; //change this if you must

function ComboBox_init(){
	if (document.addEventListener){
		//document.addEventListener("keyup", ComboBox_handleKey, false );
		document.addEventListener("mousedown", ComboBox_mouseDown, false );
	}
	else if (document.attachEvent){
		//document.attachEvent("onkeyup", function () { ComboBox_handleKey(window.event); } );
		document.attachEvent("onmousedown", function () { ComboBox_mouseDown(window.event); } );
	}
	
    Global_run_event_hook = false;
}

function returnFalse(){
	return false;
}

function ComboBoxItem(text,value){
    this.text  = text;
    this.value = value;
}

document.write('<link rel="STYLESHEET" type="text/css" href="ComboBox.css">');
