var search_game_area = 0; //1:由首頁上方遊戲搜尋來, 2:由刊登玩家拍賣單遊戲搜尋來, 3:由供應商後台，刊登販售單遊戲搜尋來, 4:由供應商後台，刊登收購單遊戲搜尋來。
var gameArr = Array();
var gameArr_auction = Array();
var xTime = 1500;
var stopTag = false;
var clicks = 1;
var sel9199={
    obj:"",
    gameArr:search_array2,
	gameArr_auction:search_array4,
    gameDiv:"",
    maxHeight:395,
    height:0,
    stopTag:false,
    isFF:(navigator.userAgent.indexOf("Firefox")!=-1),
    isIE:(navigator.userAgent.indexOf("MSIE")!=-1),
    setObj:function(o){
        this.obj = o;
    },
    setHeight:function(num){
        if(num*20 > this.maxHeight){
            this.height = this.maxHeight;
        }else{
            this.height = num*30;
        }
    },
    createAllGameList:function(){
		  this.setInputWord("");
          this.sgv(0);
          var i=0;
          var ul = document.createElement("ul");
		  ul.className = "ul_style";
          this.setObj(ul);
		  if(search_game_area == 1){
		    for( x in this.gameArr){
		      this.addLi(x,this.gameArr[x],"");
		      i++;
		    }
		  }else if(search_game_area == 2 || search_game_area == 3 || search_game_area == 4){
		    for( x in this.gameArr_auction){
		      this.addLi(x,this.gameArr_auction[x],"");
		      i++;
		    }
		  }
		

          this.setHeight(i);
          this.setList();
	},
    addLi:function(x,name,classd,liId){
        var li = document.createElement("li");
        if(liId !=null && liId !=""){
            li.onmouseover = function(){sel9199.setST();this.className = "bg1";};    
        }else{
            li.onmouseover = function(){sel9199.setST();this.className = "bgover";};
        }
        li.onmouseout = function(){sel9199.setSF();this.className = "searchlist_style";};
        if(x > -1)
            li.onclick = function(){sel9199.sg( x );};
        if(classd != "")
            li.className = classd;
		li.className = "searchlist_style";
        if(this.getInputWord() == name)
			li.className = "bg";
        li.innerHTML = name;
        this.obj.appendChild(li);
        if(liId !=null && liId !=""){
            li.id=liId;
            li.onclick = function(){sel9199.sg(x,liId);};    
        }
    },        
    createSearchGameList:function(){
		if (! this.getInputWord() || this.getInputWord() == "請選擇或輸入") return;
		var ul = document.createElement("ul");
		ul.className = "ul_style";
        this.setObj(ul);
        var i = 0;
        var a = "";
        var b = "";
        var m = false;
	    if(search_game_area == 1){
		  for( x in this.gameArr){
		    a = this.gameArr[x].toLowerCase();
		    b = this.getInputWord().toLowerCase();
		    if(a == b){
		        this.addLi(x,this.gameArr[x],"");
		        this.sgv(x);
		        m = true;
		        i++;
		    }else if(a.indexOf(b) >= 0){
		        m = false;
		        this.addLi(x,this.gameArr[x],"");
		        i++;
		    }
		  }
		}else if(search_game_area == 2 || search_game_area == 3 || search_game_area == 4){
		  for( x in this.gameArr_auction){
		    a = this.gameArr_auction[x].toLowerCase();
		    b = this.getInputWord().toLowerCase();
		    if(a == b){
		        this.addLi(x,this.gameArr_auction[x],"");
		        this.sgv(x);
		        m = true;
		        i++;
		    }else if(a.indexOf(b) >= 0){
		        m = false;
		        this.addLi(x,this.gameArr_auction[x],"");
		        i++;
		    }
		  }
		}
		var mt  = 0;
		if(m){
		    return;
		}
		this.setHeight(i);
        this.setList();
		if(i==0){
			var a = '';
		    this.sgv(a);
		    //this.addLi(0,"暫無您輸入的遊戲","");
		    i=1;
		}
		//this.setHeight(i);
        //this.setList();
    },
    getInputWord:function(){
        var s = "";
		if(search_game_area == 1)
		  s = document.searchForm.inputGame.value;
		else if(search_game_area == 2)
		  s = document.mainform.inputGame.value;
		else if(search_game_area == 3 || search_game_area == 4)
		  s = document.searchGameForm.inputGame.value;
        return s.replace(/(^\s*)|(\s*$)/g,""); 
    },
    setInputWord:function(v){
        if(v == undefined)
            v = "";
	    if(search_game_area == 1)
          document.searchForm.inputGame.value = v;
		else if(search_game_area == 2)
		  document.mainform.inputGame.value = v;
		else if(search_game_area == 3 || search_game_area == 4)
		  document.searchGameForm.inputGame.value = v;
    },
    getGameName:function(v){
	  if(search_game_area == 1){
        if(this.gameArr[v] == undefined)
            return "請選擇或輸入";
        return this.gameArr[v];
	  }else if(search_game_area == 2 || search_game_area == 3 || search_game_area == 4){
	    if(this.gameArr_auction[v] == undefined)
            return "請選擇或輸入";
        return this.gameArr_auction[v];
	  }
    },
    focusEmpty:function(){
        var w = this.getInputWord();
        if(w == "請選擇或輸入"){
           this.setInputWord("");
           this.setST();
        }else if(w.length > 0){
            this.createSearchGameList();
        }
    },
    setList:function(){
        if(this.height == 0){
            return ;
        }
        this.showSl();
        var div = "";
		if(search_game_area == 1)
		  div = document.getElementById("sl");
		else if(search_game_area == 2 || search_game_area == 3 || search_game_area == 4)
		  div = document.getElementById("sl_auction");
        div.innerHTML = "";
        div.style.height = this.height + "px";
        if(this.height < this.maxHeight){
            div.style.overflowY = "hidden";
        }else{
            div.style.overflowY = "scroll";
        }
        div.style.border = "1px solid #000";
        div.appendChild(this.obj);
        this.setST();
    },
    hiddenSl:function(){
		if(search_game_area == 1)
          document.getElementById("sl").style.display = "none";
		else if(search_game_area == 2 || search_game_area == 3 || search_game_area == 4)
		  document.getElementById("sl_auction").style.display = "none";
        xTime = 1500;
    },
    showSl:function(){
		if(search_game_area == 1){
          document.getElementById("sl").style.display = "";
		}
		else if(search_game_area == 2 || search_game_area == 3 || search_game_area == 4)
		  document.getElementById("sl_auction").style.display = "";
    },
    sg:function(x,liId){
		if(search_game_area == 1)
          this.setInputWord(this.gameArr[x]); 
		else if(search_game_area == 2 || search_game_area == 3 || search_game_area == 4)
		  this.setInputWord(this.gameArr_auction[x]); 
        this.hiddenSl();
		if(search_game_area == 1)
		  document.searchForm.gameid.value = search_array1[x];
		else if(search_game_area == 2){
		  document.mainform.gameid.value = search_array3[x];
		  document.mainform.gamename.value = search_array4[x];
		}
		if(search_array1[x] != -1){
		  if(search_game_area == 1){
		    getGameServer(search_array1[x]);
		    document.getElementById("serverList").style.display = "block";
		    document.getElementById("productList").style.display = "none";
		  }else if(search_game_area == 2){
		    getGameServerfor_auction(search_array3[x]);
		  }else if(search_game_area == 3 || search_game_area == 4){
		    qyServer(search_array3[x]);
		  }
		}else{
		  document.getElementById("serverList").style.display = "none";
		  document.getElementById("productList").style.display = "block";
		}
		if(search_game_area == 1)
		  this.setInputWord(this.gameArr[x]); 
		else if(search_game_area == 2 || search_game_area == 3 || search_game_area == 4)
		  this.setInputWord(this.gameArr_auction[x]);
    },
    setSF:function(){
        stopTag = false;
    },
    setST:function(){
        stopTag = true;
    },
    addT:function(){
        xTime += 100;
        if(xTime > 1500){
            xTime = 1500;
        }
    },
    setBlur:function(){
        return ;
    },
    sgv:function(x){
	  if(x.length > 0){
		if(search_game_area == 1)
	      document.searchForm.gameid.value = search_array1[x];
		else if(search_game_area == 2){
		  document.mainform.gameid.value = search_array3[x];
		  document.mainform.gamename.value = search_array4[x];
		}
	  }else{
		if(search_game_area == 1)
	      document.searchForm.gameid.value = '';
		else if(search_game_area == 2){
		  document.mainform.gameid.value = '';
		  document.mainform.gamename.value = '';
		}
	  }
    },
	inArray:function(arr,val) {
		for (x in arr){
			if (arr[x] == val){
				return true;
			}
		}
		return false;
	}
}
function selectBack(e) {
	var clickElement = "good";
	e = window.event || e;
	if (e.srcElement != null){
		clickElement = e.srcElement.id;
	} else {
		clickElement = e.target.id;
	}
	if (clickElement != "selectButton" && clickElement != "good") {
		if(search_game_area == 1)
		  document.getElementById("sl").style.display = "none";
		else if(search_game_area == 2 || search_game_area == 3 || search_game_area == 4)
		  document.getElementById("sl_auction").style.display = "none";
	}
}

function addEvent( obj, type, fn ) {
     if ( obj.attachEvent ) {
        obj['e'+type+fn] = fn;
        obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
        obj.attachEvent( 'on'+type, obj[type+fn] );
     } else
        obj.addEventListener( type, fn, false );
}

function anticheckGameid(){
  var gamename = "";
  var gamename2 = "";
  var gamename3 = "";
  var i = 0;
  if(search_game_area == 1){
    gamename = document.searchForm.inputGame.value;
	if(gamename != "" && gamename != "請選擇或輸入"){
      for(i = 0; i < search_array2.length ; i++){
	    if(search_array2[i] == gamename){
		  $('#serverList').empty();
	      getGameServer(search_array1[i]);
		}
	  }
    }
  }else if(search_game_area == 2){
    gamename2 = document.mainform.inputGame.value;
	if(gamename2 != "" && gamename2 != "請選擇或輸入"){
	  for(i = 0; i < search_array4.length ; i++){
	    if(search_array4[i] == gamename2){
		  $('#serverList_auction').empty();
		  getGameServerfor_auction(search_array3[i]);
		}
	  }
    }
  }else if(search_game_area == 3 || search_game_area == 4){
    gamename3 = document.searchGameForm.inputGame.value;
	if(gamename3 != "" && gamename3 != "請選擇或輸入"){
	  for(i = 0; i < search_array4.length ; i++){
	    if(search_array4[i] == gamename3){
		  $('#servercontent').empty();
		  qyServer(search_array3[i]);
		}
	  }
    }
  }
}





