﻿var gsug=null;
var apdata = apdata_gn;

function textInit(){
	gsug=new apsug();
	gsug.init();

}
function getFromSugData()
{
	var ret=[];
	var j=0;
	for(i=0;i<fromcities.length;i++){
			if(fromcities[i]<apdata_gj.length)
			{ret[j]=apdata_gj[fromcities[i]];j++;}
	}
	return ret;
}
///enable useless
function getSugData()
{
	var code=getCode(document.getElementById("frmInputcity").value);
	var ret=[];
	var j=0;
	var str="";
	for(i=1;i<apdata_gj.length;i++){
			if(code!=apdata_gj[i][0] )
			{
		        if(code.indexOf('GJ')==0)
				{
					if(apdata_gj[i][0].indexOf('GJ')!=0)
					{
						ret[j]=apdata_gj[i];
				        j++;
					}
				}
				else{
					if(apdata_gj[i][0].indexOf('GJ')==0)
					{
						ret[j]=apdata_gj[i];
				        j++;
					}
				}
			 
			}
	}
	return ret;
}

function  getData(inputid)
{
	var dataarr;
	if(inputid=="frmInputcity"){
		dataarr=getFromSugData();
	}//else if(inputid=="tocity"){
//		dataarr=getSugData();
//	}
	else{
		apsug.isInternational = true;
		return apdata;
	}
	return dataarr;
}
function isUpDownKey(kc){
	if(kc==40 || kc==38 || kc == 33 || kc== 34){//down,up,pageup,pagedown,enter
		return true;
	}else{
		return false;
	}
}
function isCtrlKey(kc){
	if(kc==16 || kc==17 || kc == 18){//shift,ctrl,alt
		return true;
	}else{
		return false;
	}
}
function apsug() {
	var inputobj = null;
	var sugdiv = null;

	var focusBySelect = false;
	var currInputObj = null;
	var prevInputValue = null;
	
	var showarr = null;
	var showfocus = -1;
	var FOCity = null;
	var onDocumentClick = null;
	var isInternational=false;

}
apsug.prototype.setInputObj = function(o){
	o.onkeydown = OnInputKeyDown;
	o.onfocus = OnInputFocus;
	o.onblur = OnInputBlur;
}
apsug.prototype.init = function(){
	if(this.sugdiv==null){
		var blankifram=document.getElementById("DropdowncitiesFram");
		this.sugdiv=document.createElement('DIV');
		this.sugdiv.id = 'DivShowCities';
		this.sugdiv.style.display='none';
		this.sugdiv.className='sugDiv';
		this.sugdiv.innerHTML='';
		this.sugdiv.onscroll=OnSugScroll;
		var bs=document.getElementsByTagName('BODY');
		bs[0].appendChild(this.sugdiv);
	}
	this.focusBySelect = false;
}
function OnSugScroll(){
	if(gsug.currInputObj==null){return;}
	gsug.focusBySelect=true;
	gsug.currInputObj.focus();
}
function OnDocClick(){
	if( typeof( document.getElementById('DivShowCities') )!='undefined' )
		document.getElementById('DivShowCities').style.display="none";
}
var global_focus_flag=false;
var inString ='';
function OnInputFocus(){
	if(gsug.currInputObj != null && gsug.currInputObj != this)
	{
		gsug.select_blur();
	}
	global_focus_flag = true;
	gsug.currInputObj = this;
	gsug.prevInputValue = null;
	if(this.value == inString) {
		this.value= '';
	}
	this.style.color = '';
	if(gsug.focusBySelect){
		gsug.focusBySelect=false;
		return;
	}
	gsug.focusBySelect=false;
	InputChecking();

}
function OnInputBlur(){
	var obj = gsug.currInputObj;
	if(obj != null)
	{
		if(obj.value == '')
		{
			if(gsug.sugdiv.style.display == "none")
			{
				obj.value = inString;
				obj.style.color = "#5a5a5a";
			}
		}
		else
		{
			obj.style.color = "#5a5a5a";
		}
		loadCityAreaArray();
	}
	return;
}
function InputChecking(){
	if(gsug.currInputObj == null){return;}
	var v=gsug.currInputObj.value;
	if(gsug.prevInputValue==null || gsug.prevInputValue!=v){
		gsug.update(v);
	}
	setTimeout("InputChecking()", 10);
}
function OnInputKeyDown(e){		
	if(gsug.currInputObj==null){
		gsug.currInputObj = this;
		gsug.prevInputValue = null;
		InputChecking();
	}
	var bCtrlDown=0;
	if(!e && window.event){
		e=window.event;
	}
	if(window.event){
		bCtrlDown=e.ctrlKey;
	}else{
		bCtrlDown=(e.modifiers & Event.CONTROL_MASK) > 0;
	}
	if(bCtrlDown){
		return true;
	}
	var kc=e.keyCode;
	if(isCtrlKey(kc)){
		return true;
	}
	return (InputRealKeyDown(kc));
}
function InputRealKeyDown(kc) {
	global_focus_flag = false;

	if(isUpDownKey(kc)){
		if(kc==38){// up
			gsug.move(1,0);
		}else if(kc==40){//down
			gsug.move(1,1);
		}else if(kc==33){//pageup
			gsug.move(0,0);
		}else if(kc==34){//pagedown
			gsug.move(0,1);
		}
		return false;
	}else if(kc==13){//enter		
		if(gsug.sugdiv.style.display==''){
			gsug.select();
			showHiddenCity('none');
			showHiddenIframBg('none');
			loadCityAreaArray();
			return false;
		}else{
			gsug.select();
		}
	}else if(kc==9){//tab
		gsug.select();
		return true;
	}else if(kc==27){//esc
		gsug.hide();
		return true;
	}
	return true;
}
//Check the spread of the box
apsug.prototype.select = function() {
	if(this.sugdiv.style.display=='none'){return false;}
	if(this.showfocus>=0 && this.showarr!=null && this.showfocus<this.showarr.length && this.currInputObj!=null){
		var i=this.showarr[this.showfocus];
		this.currInputObj.value = bEnglish ? curData[i][0] : curData[i][2];
		this.focusBySelect=true;
		this.currInputObj.focus();
		if(window.event){
			var r=this.currInputObj.createTextRange();
			r.move("character", r.text.length);
			r.select();
		}else{
			var v=curData[i][0].length;
			this.currInputObj.setSelectionRange(v, v);
		}
		this.currInputObj.style.color = "#000";
	}
	this.hide();
	this.currInputObj=null;
}

apsug.prototype.select_blur = function() {
	if(this.sugdiv.style.display=='none')
	{
		return false;
	}
	if(this.showfocus>=0 && this.showarr!=null && this.showfocus<this.showarr.length && this.currInputObj!=null){
		var i=this.showarr[this.showfocus];
		this.currInputObj.value=curData[i][0];
		if(window.event){
			var r=this.currInputObj.createTextRange();
			r.move("character", r.text.length);
		}else{
			var v=curData[i][0].length;
			this.currInputObj.setSelectionRange(v, v);
		}
	}

	var obj = this.currInputObj;
	if(obj.value == '' || obj.value == inString)
	{
		obj.style.color = "#666";
	}
	else
	{
		obj.style.color = "#000";
	}
	this.hide();
}
apsug.prototype.highlight = function(idx) {
	if(this.sugdiv.style.display=='none'){return false;}
	if(idx==this.showfocus){return true;}
	var i;
	var ls = this.sugdiv.getElementsByTagName('LI');
	if(this.showfocus>=0&&this.showfocus<ls.length){
		ls[this.showfocus].className='';
	}
	if(idx>=0&&this.showfocus<ls.length){
		ls[idx].className='hover';
		this.showfocus=parseInt(idx);
	}

}
apsug.prototype.move = function(m,n){
	var a=this.showfocus;
	if(m==1&&n==0){
		a--;
	}else if(m==1&&n==1){
		a++;
	}else if(m==0&&n==0){
		a-=5;
	}else if(m==0&&n==1){
		a+=5;
	}
	if(a<0){
		a=0;
	}else if(a>=this.showarr.length){
		a=this.showarr.length-1;
	}
	var i,j,k;
	i=(this.sugdiv.scrollTop)/20;
	if(a<=i){
		this.sugdiv.scrollTop=a*20;
	}else if(a>=i+10){
		this.sugdiv.scrollTop=(a-9)*20;
	}
	if(this.showarr[0][0]=="")
	   a=a-1;
	   if(a<0)
	     a=0;
	this.highlight(a);
}
apsug.prototype.hide = function(v) {
	if(this.sugdiv.style.display!='none'){
		this.sugdiv.style.display='none';
		//document.onclick=this.onDocumentClick;
	}
}
var curData;
var lastInputData;
apsug.prototype.update = function(v) {
	if( lastInputData==v )
		return;
	lastInputData = v;
	this.prevInputValue = v;
	if(apsug.isInternational){
		curData = getData(this.currInputObj.id);
		if(!(curData instanceof Array))
			return;
	}else{
		curData = apdata;
	}
	v=v.toLowerCase();
	if(this.showfocus == null)
		this.showfocus = -1;
	if(this.showarr==null){
		this.showarr = new Array();
	}else{
		this.showarr.length = 0;
	}
	var i,j,s;
	if(v.length==0)
	{
		
		this.showarr = new Array();	
		showHiddenIframBg('none');
	}
	else{
		for(i=0;i<curData.length;i++)
		{
			var count = bEnglish ? 2 : 3;
			for(j=0;j<count;j++)
			{
				if(curData[i][j].toLowerCase().indexOf(v) == 0)
				{
					this.showarr[this.showarr.length]=i;
					break;
				}
				showHiddenIframBg('none');
			}
		}
	}

	if(this.showarr.length<=0||(this.showarr.length==1&&v==curData[this.showarr[0]][1])){
		this.hide();
		return;
	}
	if(global_focus_flag){
		global_focus_flag = false;
		return;
	}
	
	var obj = this.currInputObj;
	var isMSIE = (navigator.appName == "Microsoft Internet Explorer");
	var strStyle="";
	if((isMSIE)&&(obj.id=="cjfromcity")){ strStyle = "left:" + (gELeft(obj)-6) + "px; top:" + gEBottom(obj) + "px;";}
	else{strStyle = "left:" + gELeft(obj) + "px; top:" + gEBottom(obj) + "px;";}
	var deltaX = 3;
	strStyle += "width:" + (obj.offsetWidth - deltaX) + "px;";
	strStyle += "display:'';";
	this.sugdiv.setAttribute("style", strStyle);
	this.sugdiv.style.cssText = strStyle;
	document.getElementById('DropdowncitiesFram').style.cssText = "width:" + (obj.offsetWidth - deltaX-2) + "px;"; 
	s='<ul>';
	for(i=0;i<this.showarr.length;i++){
		if(curData[this.showarr[i]][0]!=""){
		showHiddenCity('none');	
		showHiddenIframBg('none');
			var showName = bEnglish ? curData[this.showarr[i]][0] : curData[this.showarr[i]][2];
			s+='<li'+(i==0?' class="hover"':'')+' onmousemove="gsug.highlight('+i+')" onclick="OnSugClick();">'+
			showName+'&nbsp;('+curData[this.showarr[i]][1]+')</li>';		
		}
	}
	s+='</ul>';
	this.sugdiv.innerHTML=s;
	var dropdown = document.getElementById('DropdowncitiesFram').style.display='';
	this.showfocus=0;
	this.sugdiv.scrollTop = 0;
	//this.onDocumentClick=document.onclick;
}

function OnSugClick(){
	gsug.select();
}
bClickCityInput = 0; //BodyOnload();
function ClickTopCity(objText)
{	
	lastInputData = objText;
	document.getElementById('frmInputcity').value = objText;
	showHiddenCity('none');
	showHiddenIframBg('none');
	document.getElementById('Checkin').select();
	showCalendar('Checkin',false,'Checkin');
    loadCityAreaArray();
}

function showHiddenCity(state)
{	
	var topHotCityDiv = document.getElementById('HotCitiesFrame');
	topHotCityDiv.style.display = (state=='none' ? "none" : "block");
	showHiddenIframBg('none');
	
}
function showHiddenIframBg(state){
	var dropdowncitiesFram = document.getElementById('DropdowncitiesFram');
	dropdowncitiesFram.style.display = (state == 'none' ? "none" : "block");
}

function loadCity()
{
	textInit();
	gsug.setInputObj(document.getElementById("frmInputcity"));
}
var lastCityName = '';
function loadCityAreaArray() 
{
    if( document.getElementById('inputDirection')==null )
      return;
    var cityName = document.getElementById('frmInputcity').value;
    if( cityName=='' || lastCityName==cityName )
        return;
    lastCityName = cityName;
    arrayCityArea=null;
    document.getElementById('inputDirection').value = "";
    RunJavascript("/Data/GetCityArea.ashx?city="+encodeURIComponent(cityName));
}

