var searchObj=null;
var selectObjId=null;
var id=0;

function press(evt) {
	evt = (evt) ? evt : window.event;
 	if(evt) {
 		 var code = evt.keyCode;
 		 switch(code) {
 		 	case 13:
	      	if (searchObj){
	      		selectObjOnKeyUp(searchObj);
	      		removeDropDownList();
	      	}
	      	else gotoPage(getSearchURL(document.getElementById('searchCity').value));
 		 	break;

 		 	case 17:
 		 	case 18:
 		 	case 37:
 		 	case 39:
 		 	break;

 		 	case 38:
 		 	unselectObj(-1);
     		searchObj=selectObj(-1);
       		break;

 		 	case 40:
     		unselectObj(1);
     		searchObj=selectObj(1);
       		break;

 		  	default:
 		  		selectObjId=-1;
 		 		removeDropDownList();
		    	var obj=document.getElementById("searchCity");
		    	if (checkDropDownTList("search_text",obj.value)) {
		    		clearTimeout(timeOut);
		    		initDropDownList("dropDownTList","search_text");
		    		getDropDownTList(escape2(obj.value));
		    	}
 		 }
	}
}

function checkDropDownTList(parentElement,inputValue){	var obj=document.getElementById(parentElement);
	if(!obj) return false;
	obj=document.getElementById("dropDownTList");
	if ((!obj) && (inputValue!="")) return true;
	else return false;
}

function initDropDownList(divId,parentElement){
  var div = document.createElement("div");
  div.setAttribute("id", divId);
  var s = div.style;
  document.getElementById(parentElement).appendChild(div);
  document.getElementById(divId).innerHTML="<div id='"+divId+"_content'><br><br></div><div style='float:right;font-size:11px; cursor:pointer;' onclick=removeDropDownList('"+parentElement+"','"+divId+"');>X&nbsp;</div>";
}

function removeDropDownList(parentElement,removeElement) {
	var obj=document.getElementById('dropDownTList');
	if (obj) document.getElementById('search_text').removeChild(obj);
}

function selectObj(step){	var obj=null;
    selectObjId=selectObjId+step;
    var obj=document.getElementById("dropDownRow"+selectObjId);

    if (!obj) {    	if (step==1) selectObjId=0;
    	if (step==-1) {
    		for (var i=11; i>selectObjId; i--){    			obj=document.getElementById("dropDownRow"+i);
    			if (obj) { selectObjId=i; break; }            }    	}    }
    obj=document.getElementById("dropDownRow"+selectObjId);
    obj.style.backgroundColor="#c7d2ff";
	return obj;
}

function unselectObj(step) {
    if (step==1) var FromObjId=selectObjId;
    if (step==-1) var FromObjId=selectObjId;
    obj=document.getElementById("dropDownRow"+FromObjId);

    if (!obj) {
    	if(step==1) {
    		for (i=11; i>FromObjId; i--){
    			obj=document.getElementById("dropDownRow"+i);
    			if (obj) { FromObjId=i; break; }
            }
    	}
    	if (step==-1) FromObjId=0;
    }
    obj=document.getElementById("dropDownRow"+FromObjId);
    obj.style.background="#eff2ff";}

function selectObjOnKeyUp(obj){
	var id=obj.getAttribute('id');
	var cindex=0;
	if (document.getElementById(id+"cindex")) cindex=document.getElementById(id+"cindex").value;
	if (document.getElementById(id+"tname")) selectCity(obj, cindex);
	else if (document.getElementById(id+"cname"))  selectCountry(obj);
		 else selectMegaSearch();
}

function selectCity(obj, cindex) {
  var id=obj.getAttribute('id');
  URL=setURL(cindex);
  gotoPage(URL+"/frc/"+document.getElementById(id+"value").value+".htm");
}

function selectCountry(obj) {
//  var id=obj.getAttribute('id');
//  var cindex=document.getElementById(id+"value").value;
//  URL=setURL(cindex);
//  gotoPage(URL+"/find.htm?searchby=regions&cindex="+cindex);
  gotoPage(URL+"/search/?req=listbycountry&country="+escape2(obj.html()));
}

function selectMegaSearch() {
	gotoPage("/mgfind.htm?"+escape2(document.getElementById('searchCity').value));
}

function getDropDownTList(fChar) {
	var obj=document.getElementById("dropDownTList_content");
	if (obj) {
	  setPreloaderPict("dropDownTList_content","preloader_dropDownTList","mini");
	  setPreloaderStyle("dropDownTList_content","preloader_dropDownTList");
    }
	id++;
	timeOut=window.setTimeout(function() {sendAjaxRequest(URL+"/loadXML.php?id="+id+"&searchby=firstchar&fChar="+fChar+"&mcntcities=8&mcntcountries=3&r="+Math.random(),"GET", fOkDropDownList, fError)}, 200);
}

function fOkDropDownList(xrequest) {
	var listrecords=xrequest.responseXML.getElementsByTagName('id');
	if (parseInt(listrecords[0].getAttribute("id"))!=id) return;

	var ListTRecords=xrequest.responseXML.getElementsByTagName('t');
	var cnt=Math.min(8,ListTRecords.length);
  	var str=showDDTList(ListTRecords);
  	ListCRecords=xrequest.responseXML.getElementsByTagName('c');
  	if (ListCRecords.length>0)
  		str+="<tr><td style='width:100%; height:2px; background:#4b60bb'></td></tr>";
  	str+=showDDCList(ListCRecords,cnt);
  	str+="<tr><td style='width:100%; height:2px; background:#4b60bb'></td></tr>";
  	cnt+=Math.min(3,ListCRecords.length);
  	var strOnClick="OnClick=selectMegaSearch();";
  	str+="<tr><td id='dropDownRow"+cnt+"' "+strOnClick+" OnMouseOver='setBackground(this,&quot;#c7d2ff&quot,&quot;#eff2ff&quot;);' OnMouseOut='setBackground(this,&quot;#eff2ff&quot;,&quot;#c7d2ff&quot);'>Нет моего города</a></td></tr>";
  	document.getElementById("dropDownTList_content").innerHTML="<table width='100%'>"+str+"</table>";
}

function showDDTList(listrecords) {
	var str="";
	for (i=0; i<Math.min(8,listrecords.length); i++) {
  	try {
  		var name=listrecords[i].getAttribute("n");
  		var index=listrecords[i].getAttribute("i");
  		var cindex=listrecords[i].getAttribute("ci");
  		var strOnClick="OnClick='selectCity(this, "+cindex+"); removeDropDownList();'";
      	str+="<tr><td id='dropDownRow"+i+"' "+strOnClick+"  OnMouseOver='setBackground(this,&quot;#c7d2ff&quot,&quot;#eff2ff&quot;);' OnMouseOut='setBackground(this,&quot;#eff2ff&quot;,&quot;#c7d2ff&quot);'><input type='hidden' id='dropDownRow"+i+"cindex' value='"+cindex+"'><input type='hidden' id='dropDownRow"+i+"value' value='"+index+"'><input type='hidden' id='dropDownRow"+i+"tname' value='"+name+"'>"+name+"</td></tr>";
    }
    catch(err) {
    }
  }
  return str;
}

function showDDCList(listrecords,cnt) {
	var str="";
	for (i=0; i<Math.min(3,listrecords.length); i++) {
  	try {
  		var strname=listrecords[i].getAttribute("n");
  		var strindex=listrecords[i].getAttribute("i");
  		var strOnClick="OnClick='selectCountry(this); removeDropDownList();'";
      str+="<tr><td id='dropDownRow"+(i+cnt)+"' "+strOnClick+" OnMouseOver='setBackground(this,&quot;#c7d2ff&quot,&quot;#eff2ff&quot;);' OnMouseOut='setBackground(this,&quot;#eff2ff&quot;,&quot;#c7d2ff&quot);'><input type='hidden' id='dropDownRow"+(i+cnt)+"cname' value='"+strname+"'><input type='hidden' id='dropDownRow"+(i+cnt)+"value' value='"+strindex+"'>"+strname+"</td></tr>";
    }
    catch(err) {
    }
  }
  return str;
}

function fError(xrequest) {
	xrequest=null;
}

function setBackground(obj,color1,color2) {
	for (i=0; i<12;i++) {
	  var obj1=document.getElementById("dropDownRow"+i);
		if (obj1)
			if (obj1==obj) {
				obj1.style.backgroundColor=color1;
				selectObjId=i;
			}
		  	else {
      			var objBgSelect=obj1.style.backgroundColor;
      			if ((objBgSelect=="rgb(199, 210, 255)") || (objBgSelect=="#c7d2ff"))
		  			obj1.style.background=color2;
		  	}
  	}
}
