function spellTip() {
	var date = new Date();
	date.setTime(date.getTime()+(86400*365*1000));
	if(!document.getElementById('spelltip').checked){
		setSideCookie('spelltip', "false", date, "", "ohdict.com", "");
	}
	else{
		setSideCookie('spelltip', "true", date, "", "ohdict.com", "");
	}
}

function autoComplete(q) {

	makeRequest("ajax.php?ajax_display=autoComplete&"+new Date(),ajax_autoComplete_result,"POST","seekname="+encodeURI(q)+"&category="+document.getElementById('category').value);
}	

function ajax_autoComplete_result() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			if(http_request.responseText){		
				inputEx.tip=Array();
				wordList=http_request.responseText.split(',');

				for(i=0;i<wordList.length-1;i++){
		
					inputEx.addEx(new ComboBoxItem(wordList[i],wordList[i]));
				}
				inputEx.showTip();
				
			}else{
				inputEx.tip=Array();
				inputEx.showTip();
			}
		} else {
			alert('There was a problem with the request. Check your permission or contact with administrator.'+http_request.responseText);
		}
	}
}


/*
 *	ComboBox
 *	By Jared Nuzzolillo
 *
 *	Updated by sunny
 *	http://www.ohdict.com/
 *	2008-01-31
 *
 */

Global_run_event_hook = true;
Global_combo_array    = new Array();

Array.prototype.remove=function(dx)
{ 
    if(isNaN(dx)||dx>this.length){self.status='Array_remove:invalid request-'+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.derive||this.name+"txt";
	

	this.txtview = document.createElement("INPUT");
	this.txtview.type = "text";
	this.txtview.name = nm;
	this.txtview.id = nm;
	this.txtview.className = "ipt";
	
	this.txtview.onkeyup=function(e){
				
			if(this.value.length==0){
				inputEx.tip=Array();
				inputEx.showTip();
				
			}
			if (e == null) { // ie
				key = event.keyCode;
			} else { // mozilla
				key = e.which;
			}

			if(this.value.length>0 && document.getElementById('spelltip').checked && key!=37 && key!=38 && key!=39 && key!=40 && key!=13 && key!=17) {
				autoComplete(this.value);
			}
			if(event.keyCode==13){
				/*var date = new Date();
				date.setTime(date.getTime()+(86400*365*1000));
				setSideCookie('dict'+document.getElementById('category').value+'['+this.value+']', this.value, date, "", "", "");
				*/
				if(inputEx.opslist)inputEx.opslist.style.display="none";
			}
	}
	
	this.txtview.setAttribute("autocomplete","off");
	
	this.txtview.onfocus=function(){this.select();};
	this.txtview.onmouseover=function(){this.focus()};

	this.view.appendChild(this.txtview);

    
        
    this.valcon = document.createElement("INPUT");
    this.valcon.type = "hidden";
    this.view.appendChild(this.valcon)
	
	wnm = document.createElement("input");
	wnm.type = "hidden";
	wnm.value = this.name;
  	this.view.appendChild(wnm);
  
    
   /* var tmp = document.createElement("input");
	tmp.type = "BUTTON";
    tmp.className = "combo-button";

	tmp.title = "查看历史记录";
    
	this.view.appendChild(tmp);
   	tmp.onfocus = function () { this.blur(); };
	tmp.onclick = new Function ("", this.name + ".toggle()");*/
}

function ComboBox_choose(realval,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=="ipt")
    {
		strname = el.parentNode.childNodes[2].value;
		obj = window[strname];

		if((key==38 || key==40) && obj.opslist.style.display!="none") {
			///响应鼠标上移事件
			
			if(obj.index>-1 && obj.index<obj.indexMax) {
				document.getElementById("index_"+obj.index).onmouseout();
			}
			if(key==38 && obj.index>-1){		
				obj.index--;
				if(document.getElementById("index_"+obj.index)==null)obj.index=obj.indexMax-1;
				document.getElementById("index_"+obj.index).onmouseover();		
			}
			//响应鼠标下移事件
			if(key==40 && obj.index<obj.indexMax+1) {
				
				if(obj.indexMax==obj.index){obj.index=-1};
				obj.index++;
				if(document.getElementById("index_"+obj.index)==null)obj.index=0;
					document.getElementById("index_"+obj.index).onmouseover();		
			} 
			eobj.keyCode=0
		}
     }
}



function ComboBox_update()
{
    /*var opart,astr,alen,opln,i,boo;
    boo=false;
    opln = this.options.length;
    astr = this.txtview.value.toLowerCase();
    alen = astr.length;
    if(alen==0)
    {
        for(i=0;i<opln;i++)
        {
            this.expops[this.expops.length]=this.options[i];boo=true;
        }
    }
    else
    {
        for(i=0;i<opln;i++)
        {
            opart=this.options[i].text.toLowerCase().substring(0,alen)
            if(astr==opart)
            {
                this.expops[this.expops.length]=this.options[i];boo=true;
            }
        }
    }
    if(!boo){this.expops[0]=new ComboBoxItem("(No matches)","")}*/
}



function ComboBox_remove(index)
{
    this.options.remove(index)
}

function ComboBox_add()
{
    var i,arglen;
    arglen=arguments.length

	for(i=0;i<arglen;i++)
	{
		this.options[this.options.length]=arguments[i]
	}
}

function ComboBox_addEx()
{
    var i,arglen;
    arglen=arguments.length

	for(i=0;i<arglen;i++)
	{
		this.tip[this.tip.length]=arguments[i]
	}
}


function ComboBox_build(arr)
{	
    var str,arrlen
    arrlen=arr.length;
	//重新初始化当前索引值
	this.index = -1;
	this.indexMax = arrlen;
    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 id="index_'+i+'" class="combo-item" onClick="'+this.name+'.choose(\''+arr[i].value+'\',\''+arr[i].text+'\');'+this.name+'.opslist.style.display=\'none\';" ' +
			'onMouseOver="'+this.name+'.choose(\''+arr[i].value+'\',\''+arr[i].text+'\');this.className=\'combo-hilite\';'+this.name+'.index='+ i +';" 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.opslist.innerHTML=str;
    this.opslist.style.display='none';
    this.opslist.className = "combo-list";
	this.opslist.style.width = (this.txtview.clientWidth-2) +"px";
    this.opslist.onselectstart=returnFalse;
	this.opslist.style.position = 'absolute';
	
	//alert( this.txtview.clientWidth);
	
	this.opslist.style.left = 0 ;
	this.opslist.style.top = this.txtview.offsetHeight;
    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.options);
            this.view.style.zIndex = ++ComboBox.prototype.COMBOBOXZINDEX
			if(this.options.length>0)this.opslist.style.display="block"
            
        }
    }
    else
    {
        //this.update();
        this.build(this.options);
        this.view.style.zIndex = ++ComboBox.prototype.COMBOBOXZINDEX
        if(this.options.length>0)this.opslist.style.display="block"
    }
}

function ComboBox_Tip()
{
        this.build(this.tip);
        this.view.style.zIndex = ++ComboBox.prototype.COMBOBOXZINDEX
        if(this.tip.length>0)this.opslist.style.display="block"
}

function ComboBox()
{
    if(arguments.length==0)
    {
        self.status="ComboBox invalid - no name arg"
    }

    this.name     = arguments[0];
    this.par      = arguments[1]||document.body
	this.derive	  = arguments[2];
	this.view	  = document.createElement("SPAN");
	
	this.view.style.position='relative';
    this.options  = new Array();
    this.tip   = new Array();
    this.value    = "";
	this.index	  = -1;
	this.indexMax = 0;
	
	this.par.appendChild(this.view)
	if(document.getElementById(this.derive))
		this.par.replaceChild(this.view,document.getElementById(this.derive));

    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.addEx=ComboBox_addEx
	this.showTip= ComboBox_Tip;

    this.make()
    this.txtview = this.view.childNodes[0]
    this.valcon  = this.view.childNodes[1]
    
   

    Global_combo_array[Global_combo_array.length]=this;
    if(Global_run_event_hook){ComboBox_init()}
}

ComboBox.prototype.COMBOBOXZINDEX = 99999 //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;
}