var sGame = 0;
function selectChange(control, controlToPopulate, arrValue, arrName)
{
	var gameId = selectChangeBase(control, controlToPopulate, arrValue, arrName);
	getWareType(gameId);
}

function selectChange1(control, arrValue, arrName, selectMore) {
	var sHTML = "<table width='100%' border='0' cellspacing='2' cellpadding='2' class='small910'>";
	var i = 1;
	var x ;
	var selectVal = control.options[control.selectedIndex].value;
    if (selectMore == null) selectMore = "radio";
	
	arrValue = arrValue[control.value];
	arrName  = arrName[control.value];
 
	for ( x in arrValue)
    {
		if (i%4==1) { sHTML = sHTML + "<tr>"; }
 
		if(x == 0)
			sHTML = sHTML + "<td style='border:1px solid rgb(51,51,51);' width='1%'><INPUT type='"+selectMore+"' name ='serverChoice' value='"+arrValue[x]+"' id ='serverChoice' require='true' msg='請選擇伺服器！' onClick='clickFun("+control.value+','+arrValue[x]+");'></td>";
		else 
			sHTML = sHTML + "<td style='border:1px solid rgb(51,51,51);' width='1%'><INPUT type='"+selectMore+"' name ='serverChoice' value='"+arrValue[x]+"' id ='serverChoice' onClick='clickFun("+control.value+','+arrValue[x]+");'></td>";
		sHTML = sHTML + "<td style='border:1px solid rgb(51,51,51);'>" + arrName[x] + "</td>";
		if (i%4==0) { sHTML = sHTML + "</tr>";}
		i++;
    }
	sHTML = sHTML + "</table>";
	document.getElementById('GameSrvListSpan').innerHTML = sHTML;
	//獲取遊戲是類別（禮品，商城商品）
	getWareRadioType(selectVal);
	cancelCheck("wareType");
	if ($("accountsWare") != null) {
		$("accountsWare").style.display = "none";
		if ($("gameFacetory") != null)
			$("gameFacetory").style.display = "none";
	}
	document.getElementById("waretypetd").style.display = "block";
}

function selectChange2(control, controlToPopulate, arrValue, arrName) {
	var gameId = selectChangeBase(control, controlToPopulate, arrValue, arrName);
	getWareRadioType(gameId);
}

function selectChangeBase(control, controlToPopulate, arrValue, arrName){
	var x, i, j;
	j = 0;
	i = 0;
	for (var q=controlToPopulate.options.length;q>=0;q--) controlToPopulate.options[q]=null;
	controlToPopulate.options[j++] = new Option("全伺服器",0);
	arrValue = arrValue[control.value];
	arrName  = arrName[control.value];
	for ( x in arrValue)
	{
		controlToPopulate.options[j++] = new Option(arrName[x],arrValue[x]);
		i = 1;
	}
	if (i == 0 && arrValue != null)
	{
		controlToPopulate.options[j++] = new Option(arrName,arrValue);
	}
	return control.value;
}

function getWareRadioType(selectVal) {
	var aa = document.getElementsByName("wareType");
		if (setTypeArr[selectVal] == 0) {
			aa[5].checked = false;
			setText(aa[5],'送禮');
			aa[5].disabled = true;
		} else if (setTypeArr[selectVal] == 1) {
			aa[5].disabled = false;
			aa[5].checked = false;
			setText(aa[5],'送禮');
			aa[5].value = 5;
		} else if (setTypeArr[selectVal] == 2) {
			aa[5].disabled = false;
			aa[5].checked = false;
			setText(aa[5],'商城商品');
			aa[5].value = 7;
		}
}

function cancelCheck(name){      
   var temp=document.getElementsByName(name);
   for (i=0;i<temp.length;i++){
	 if(temp[i].checked == true){
		temp[i].checked = false;
	 }
   }
}

function setText(obj, value) {
	obj.nextSibling.nodeValue = value;
}

function clickFun(control,v){
	document.Form1.fireFox_serverChoice.value = v; 
	if(control == 71 && v == emigration && $("tianServer") != null){
		var opStr = "<select name='atServer' id='atServer' msg='請選擇您的道具或帳號現在所屬的伺服器！'><option value=''>請選擇</option>";
		for(x in arrGameItem1Val[control]){
			if(arrGameItem1Val[control][x] == emigration) continue;
			opStr = opStr + "<option value='"+arrGameItem1Val[control][x]+"'>"+arrGameItemsName[control][x]+"</option>";
		}
		opStr = opStr + "</select>";
		$("tianServer").style.display = "";
		$("tianServerDiv").innerHTML = opStr;
	}else if(control == 1817){
		document.getElementById('phoneBrand').style.display="block";
	}else{
		document.getElementById('phoneBrand').style.display="none";
		if($("tianServer") != null)
			$("tianServer").style.display = "none";


	}
}

function selectChange3(control, controlToPopulate, ItemArray, GroupArray)
{
	var x, j;
	j = 0;
	for (var q=controlToPopulate.options.length;q>=0;q--) controlToPopulate.options[q] = null;
	controlToPopulate.options[j++] = new Option("請選擇",0); 
	for(x in  ItemArray ){ 
		 if ( GroupArray[x] == control.value ) 		{
			controlToPopulate.options[j++] = new Option(ItemArray[x],x);
		}
	}
}

function fnc_Validation_IsBlank(strVal){
	var exp = /^\s*$/;
	return exp.test(strVal);
}

function fnc_Validation_Select(obj, val)
{
	if(navigator.userAgent.indexOf("Firefox") != -1){
		return ;
	}else if (navigator.userAgent.toLowerCase().indexOf('opera') > -1 ){
		return ;
	}else{
		var rang = obj.createTextRange();
		rang.findText(val);
		rang.select();
	}
}

function clickAutoAppraise()
{
	if (document.forms[1].autoAppraise.checked == true)
	{
		document.forms[1].appraiseSelect[0].disabled = false;
		document.forms[1].appraiseSelect[1].disabled = false;
		document.forms[1].appraiseContent0.disabled = false;
		document.forms[1].appraiseContent1.disabled = false;
	} else {
		document.forms[1].appraiseSelect[0].disabled = true;
		document.forms[1].appraiseSelect[1].disabled = true;
		document.forms[1].appraiseContent0.disabled = true;
		document.forms[1].appraiseContent1.disabled = true;
	}
}

function allowUploadImg(){
	if (document.Form1.wareType[0].checked == true || document.Form1.wareType[3].checked == true || document.Form1.wareType[4].checked == true) {
		alert("提示：遊戲幣、點數卡、代練類別不用上傳圖片。");
		return false;
	}
}

function doSearch(search){
	if (search.searchGame.value == '' || search.searchGame.value == 0) {
		alert('請先選擇遊戲!');
		return  false ;
	}
	search.submit();
}

function AddFavorite(url, title){
	if (window.sidebar) window.sidebar.addPanel(title, url, "");
	else if (window.opera && window.print){
		var mbm = document.createElement('a');
		mbm.setAttribute('rel', 'sidebar');
		mbm.setAttribute('href', url);
		mbm.setAttribute('title', title);
		mbm.click();
	}	else if (document.all) window.external.AddFavorite(url, title);
}

function openAnnounce(url, winname, n)
{
	window.open(url, winname, n);
}

function createOption(obj,key,val){
	var opt = document.createElement("option"); 
	var opt = new Option(key, val);
	obj.options.add(opt,6);
}

//js選中遊戲,獲取商品類別（商城商品，送禮）
function getWareType(x) {
	var type = document.getElementById("searchType");
	var typeOption = type.options[6];
	if (setTypeArr[x] == 0) {
		if (typeOption.value == 5 || typeOption.value == 7){
			type.remove(6);
		}
	} else if (setTypeArr[x] == 1) {
		if (typeOption.value == 5 || typeOption.value == 7){
			type.remove(6);
		}
		createOption(type,'送禮',5);
	} else if (setTypeArr[x] == 2) {
		if (typeOption.value == 5 || typeOption.value == 7){
			type.remove(6);
		}
		createOption(type,'商城商品',7);
	}
}

var pWin;
function openWin(url)
{
  var pos = (pos === (void 0)) ? '0' : (screen.availWidth - w - 10);
  pWin = window.open(url, 'card', 'width=500,height=430,scrollbars=no,top=0,left=0,resizable=1');
} 

function slideLine(ul, delay, speed, lh) {
	var slideBox = (typeof ul == 'string')?document.getElementById(ul):ul;
	var delay = delay||1000, speed=speed||20, lh = lh||20;
	var tid = null, pause = false;
	var start = function() {
		tid=setInterval(slide, speed);
	}
	var slide = function() {
		if (pause) return;
		slideBox.scrollTop += 2;
		if (slideBox.scrollTop % lh == 0) {
			clearInterval(tid);
			slideBox.appendChild(slideBox.getElementsByTagName('li')[0]);
			slideBox.scrollTop = 0;
			setTimeout(start, delay);
		}
	}
	slideBox.onmouseover=function(){pause=true;}
	slideBox.onmouseout=function(){pause=false;}
	setTimeout(start, delay);
}
var gameTmpArr = Array("天堂","戰槌","魔獸世界","AION","大航海");
var blueArr = Array();
var greenArr = Array("天堂","戰槌","魔獸世界","AION","大航海");
var orangeArr = Array();
var gameArr = Array();
var setTypeArr = Array();
for(i = 0;i < gameTmpArr.length-1;i++){
	var tmpArr = gameTmpArr[i].split("#");
	if(tmpArr[0] == "blue")
		blueArr[tmpArr[1]] = tmpArr[2];
	if(tmpArr[0] == "green")
		greenArr[tmpArr[1]] = tmpArr[2];
	if(tmpArr[0] == "orange")
		orangeArr[tmpArr[1]] = tmpArr[2];
	gameArr[tmpArr[1]] = tmpArr[2];
	if (tmpArr[3]){
		setTypeArr[tmpArr[1]] = tmpArr[3];
	}
}
var xTime = 1500;
var stopTag = false;
var clicks = 1;
var sel9199={
    obj:"",
    gameArr:search_array2,
    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);
		  for( x in this.gameArr){
		      this.addLi(x,this.gameArr[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;
       
		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++;
		    }
		}
		var mt  = 0;
		if(m){
		    return;
		}
		if(i==0){
			var a = '';
		    this.sgv(a);
		    this.addLi(0,"暫無您輸入的遊戲","");
		    i=1;
		}
        this.setHeight(i);
        this.setList();
    },
    getInputWord:function(){
        var s = document.searchForm.inputGame.value;
        return s.replace(/(^\s*)|(\s*$)/g,""); 
    },
    setInputWord:function(v){
        if(v == undefined)
            v = "";
        document.searchForm.inputGame.value = v;
    },
    getGameName:function(v){
        if(this.gameArr[v] == undefined)
            return "請選擇或輸入";
        return this.gameArr[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 = document.getElementById("sl");
        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(){
        document.getElementById("sl").style.display = "none";
        xTime = 1500;
    },
    showSl:function(){
        document.getElementById("sl").style.display = "";
    },
    sg:function(x,liId){
        this.setInputWord(this.gameArr[x]);            
        this.hiddenSl();
		document.searchForm.gameid.value = search_array1[x];
		if(search_array1[x] != -1){
		  getGameServer(search_array1[x]);
		  document.getElementById("serverList").style.display = "block";
		  document.getElementById("productList").style.display = "none";
		}else{
		  document.getElementById("serverList").style.display = "none";
		  document.getElementById("productList").style.display = "block";
		}
		this.setInputWord(this.gameArr[x]); 
    },
    initServerList:function(x,liId){
        var j = 0;
        var o = document.searchForm.searchServer;
        o.options.length = 0;
        if(liId !=null && liId !=""){
            for(var f=0;f<=arrGameItem1Val[3013].length-1;f++){
                var gameIds = arrGameItem1Val[3013][f];
                if(gameIds == liId){
                    var  c =  String(arrGameItemsName[3013][f]); 
                    o.options[j++] = new Option(c,liId);       
                }
    		}  
        }else{
    	    o.options[j++] = new Option("全伺服器",0);
    	}
    	for ( var m in arrGameItem1Val[x]){
    		o.options[j++] = new Option(arrGameItemsName[x][m],arrGameItem1Val[x][m]);
    	}
		getWareType(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){
	    document.searchForm.gameid.value = search_array1[x];
	  }else{
	    document.searchForm.gameid.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") {
		document.getElementById("sl").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 );
}

