
//PK廣場打賞表單
function plazaGaveCoinsFm(candId){
	pkdwr.plazaPrepareGiveCoins(candId, {
		callback:function(dataFromServer){
		showPlazaGaveCoinsFm(dataFromServer);	}
	});
}

function showPlazaGaveCoinsFm(data ){
	
	var status = data['status'];

	if( status == 'ok'){
		jpk("#remainCoins").html(data['remainCoins']); 
		jpk("#maxCoins").html(data['maxCoins']); 
		jpk("#maxCoinsValue").val(data['maxCoins']);
		jpk("#coinFmCanvas").dialog('open');
	}else if(status == "notLogin"){
		alertMessage('請先登入');
	}else if(status == "notEnough"){
		alertMessage('您剩餘的賞銀不足');
	}else if(status == "notFound"){
		alertMessage('找不到此參賽者');
	}else if(status == "overflow"){
		alertMessage('您已達到今天可以打賞此候選人賞銀上限');
	}
	
}

//PK廣場 給參賽者鼓勵
function plazaEncourage(candId){
	pkdwr.plazaEncourage(candId, {
		callback:function(dataFromServer){
		showPlazaEncourageResult(dataFromServer);	}
	});
}

function showPlazaEncourageResult(data){
	jpk("#encourageCount").html(data); 
	alertMessageModal('謝謝你的支持!!我會更努力的~~~~^_^');
}



//PK廣場 給留言
function plazaComment(candId, msg){
	pkdwr.plazaSaveComment(candId, msg, {
		callback:function(dataFromServer){
		showPlazaCommentResult(dataFromServer, candId);	}
	});
}

function showPlazaCommentResult(data, candId){
	if(data == 'ok'){
		plazaCommentList(candId,0);
	}else if(data='denied'){
		jpk("#commentFmCanvas").dialog('close');
		alertMessage('喝口水，兩分鐘後再留言吧！');
	}
	jpk("#commentFmCanvas").dialog('close');
}



//PK廣場 回覆留言
function plazaReplyComment(candId,commentId, msg, offset){
	
	pkdwr.plazaReplyComment(commentId, msg,  {
		callback:function(dataFromServer){
		showPlazaReplyCommentResult(dataFromServer, candId, offset);	}
	});
}

function showPlazaReplyCommentResult(data, candId, offset){
	if(data == 'ok'){
		plazaCommentList(candId,offset);
	}
}

//PK廣場 刪除回覆
function plazaDeleteCommentRelpy(candId,commentId, offset){
	pkdwr.plazaDeleteCommentReply(commentId,  {
		callback:function(dataFromServer){
		showPlazaDeleteCommentReplyResult(dataFromServer, candId, offset);	}
	});
}

function showPlazaDeleteCommentReplyResult(data, candId, offset){
	if(data == 'ok'){
		plazaCommentList(candId,offset);
	}
}

//PK廣場 刪除留言
function plazaDeleteComment(candId,commentId){
	pkdwr.plazaDeleteComment(commentId,  {
		callback:function(dataFromServer){
		showPlazaDeleteCommentResult(dataFromServer, candId);	}
	});
}

function showPlazaDeleteCommentResult(data, candId){
	if(data == 'ok'){
		plazaCommentList(candId,0);
	}
}


//PK廣場留言列表
function plazaCommentList(candId, offset){
	pkdwr.plazaFindComments(candId, offset, {
		callback:function(dataFromServer){
		showPlazaCommentList(dataFromServer, candId);	}
	});
}

function showPlazaCommentList(data, candId){
	if(data['total'] > 0){
		var htmlText = "";
		var comments = data['comments'];
		var startPage = data['startPage'];
		var endPage = data['endPage'];
		var currentPage = data['currentPage'];
		var maxPageItems = data['maxPageItems'];
		var mine = data['mine'];
		var offset = parseInt(currentPage -1);
		offset = offset * maxPageItems;
		for(var prop in comments){
			var comment = comments[prop];
			
			var idx = parseInt(offset)+parseInt(prop)+parseInt('1');
			
		    
		    htmlText += "<table width=\"600\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"1\"><tr><td>";
	        htmlText += "<table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr>";
	        htmlText += "<td width=\"44\" height=\"44\" align=\"center\" valign=\"top\" bgcolor=\"#FFFFFF\"><span class=\"T10Gary\">";
	        htmlText += "<img src=\""+comment.extraInfo2+"\" width=\"40\" height=\"40\" align=\"absmiddle\" />";
	        htmlText += "</span></td><td width=\"10\"></td>";
	        htmlText += "<td width=\"376\"><a href=\"/mem/vwmem.do?acc="+comment.extraInfo5+"\" class=\"MemberLink\">"+comment.extraInfo1+"</a> "+comment.message+"<br />";
	        htmlText += "<span class=\"T10Gary\">"+comment.extraInfo3;
	        if(mine == "true"){
	        	htmlText += " &lt;<a href=\"#comment\" onclick=\"plazaDeleteComment("+comment.candidateId+","+comment.id+")\">刪除</a>&gt;";
	        }
	        htmlText += "</span></td>";
	        htmlText += "<td width=\"70\" align=\"right\" valign=\top\"><span class=\"T10Gary\">#"+idx+"</span>";
	        if(mine == "true"){
	        	if(comment.replyMessage == null || comment.replyMessage == ''){
	        	htmlText += "｜<a href=\"#comment\" onclick=\"jpk('#replyFm"+comment.id+"').show()\" >回覆</a>｜<br />";
	        	}
	        }
	        htmlText += "</td></tr></table>";
	        htmlText += "<table width=\"100\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td height=\"5\"></td></tr></table>";
	        
	        htmlText += "<table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"44\" align=\"center\" bgcolor=\"#FFFFFF\">&nbsp;</td>";
	        htmlText += "<td width=\"10\"></td><td width=\"398\">";
	        if(comment.replyMessage != null && comment.replyMessage != ''){
	        	htmlText += "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\"><tr><td bgcolor=\"#EEEEEE\">";
	        	htmlText += comment.replyMessage ;
	        	htmlText += "<span class=\"T10Gary\"><br />版主 於 "+comment.extraInfo4+" 回覆";	
	        	if(mine == "true"){
	        		htmlText += "&lt;<a href=\"#comment\" onclick=\"plazaDeleteCommentRelpy("+comment.candidateId+","+comment.id+","+offset+")\">刪除</a>&gt;";
	        	}
	        	htmlText +="</span></td></tr></table>";
	        }else{
	        	if(mine == "true"){
	        		htmlText += "<div id=\"replyFm"+comment.id+"\" style=\"display:none\">";
	        		htmlText += "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\"><tr>";
	        		htmlText += "<td bgcolor=\"#EEEEEE\"><span class=\"T10Gary\">";
	        		htmlText += "<input name=\"replyMsg"+comment.id+"\" type=\"text\" class=\"PkTextField03\" id=\"replyMsg"+comment.id+"\" size=\"50\" />";
	        		htmlText += "<input name=\"button\" type=\"button\" class=\"LoginBtn01\" id=\"button\" onclick=\"sendReply("+comment.id+","+offset+");\" value=\"留言\" />";
	        		htmlText += "</span></td></tr></table>";
	        		htmlText += "</div>";
	        	}
	        }
	        htmlText += "</td><td width=\"48\">&nbsp;</td></tr></table>";
	            
	        htmlText +="</td></tr></table>" ;
		    
		}
		
		htmlText += "<table width=\"600\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" id=\"MemberPage\"><tr><td align=\"center\">";
		for(var i = startPage; i<=endPage ; i++){
			if(i==currentPage){
				htmlText += i;
			}else{
				htmlText += " <a href=\"#comment\" onclick=\"plazaCommentList("+candId+","+(i-1)*maxPageItems+")\">"+i+"</a> ";
			}
		}
		htmlText += "</td></tr></table>";
		jpk('#commentls').html(htmlText);
	}
}

//PK廣場 連結貼紙訊息
function plazaStampMsg(candId, msg){
	pkdwr.plazaGenerateStampMessageImage(candId, msg, {
		callback:function(dataFromServer){
		showPlazaStampMsg(dataFromServer);	}
	});
}

function showPlazaStampMsg(data){
	
	var stampurl =  jpk('#stampSrc').attr("src");
	stampurl = stampurl+"?1";
	jpk('#stampSrc').attr("src", stampurl);
}


//PK廣場　前後排名
function plazaGrade(topicId, grade, idx){
	pkdwr.plazaFindCandidateGrade(topicId, grade, {
		callback:function(dataFromServer){
		showPlazaGrade(dataFromServer,topicId, idx);	}
	});
}

function showPlazaGrade(data,topicId, idx){
	var htmlText = "";
	var nextCands = data['next'];
	var prevCands = data['prev'];
	if(nextCands == null || nextCands.length ==0){
		jpk('#next0'+"_"+topicId+"_"+idx).hide();
		jpk('#next1'+"_"+topicId+"_"+idx).hide();
	}else{
		for( var prop in nextCands){
			var cand = nextCands[prop];
			jpk('#next'+prop+"_"+topicId+"_"+idx).show();
			jpk('#next'+prop+"_"+topicId+"_"+idx+"_imgLink").attr("href", "/plaza/vwcand.do?cid="+cand.id);
			jpk('#next'+prop+"_"+topicId+"_"+idx+"_img").attr("src", cand.extraInfo2);
			jpk('#next'+prop+"_"+topicId+"_"+idx+"_rank").html("No."+cand.extraInfo1);
			jpk('#next'+prop+"_"+topicId+"_"+idx+"_grade").html("戰鬥力："+cand.supportRank+"<br>"+"魅力值："+cand.coinsRank+"<br><a href='/plaza/vwcand.do?cid="+cand.id+"'>詳細資料</a>");
		}
		if(nextCands.length==1){
			jpk('#next1'+"_"+topicId+"_"+idx).hide();
		}
	}
	
	if(prevCands == null || prevCands.length ==0){
		jpk('#prev0'+"_"+topicId+"_"+idx).hide();
		jpk('#prev1'+"_"+topicId+"_"+idx).hide();
	}else{
		for( var prop in prevCands){
			var cand = prevCands[prop];
			jpk('#prev'+(prevCands.length - prop -1)+"_"+topicId+"_"+idx+"_imgLink").attr("href", "/plaza/vwcand.do?cid="+cand.id);
			jpk('#prev'+(prevCands.length - prop -1)+"_"+topicId+"_"+idx).show();
			jpk('#prev'+(prevCands.length - prop -1)+"_"+topicId+"_"+idx+"_img").attr("src", cand.extraInfo2);
			jpk('#prev'+(prevCands.length - prop -1)+"_"+topicId+"_"+idx+"_rank").html("No."+cand.extraInfo1);
			jpk('#prev'+(prevCands.length - prop -1)+"_"+topicId+"_"+idx+"_grade").html("戰鬥力："+cand.supportRank+"<br>"+"魅力值："+cand.coinsRank+"<br><a href='/plaza/vwcand.do?cid="+cand.id+"'>詳細資料</a>");
		}
		if(prevCands.length==1){
			jpk('#prev1'+"_"+topicId+"_"+idx).hide();
		}
	}
}


//PK廣場　主題子選項
function plazaFindTopicItem(topic, selectItem, selectItemArea, titleId, imgDescMsg){
    jpk('#'+selectItem).find('option').remove();
    jpk('#'+selectItemArea).hide();
    jpk('#'+titleId).html("");
    pkdwr.plazaFindTopicItems(topic, {
    	callback:function(dataFromServer){
    		plazaFillTopicItemOption(dataFromServer, selectItem, selectItemArea, titleId, imgDescMsg);
    	}
    });
}

//顯示主題子選項
function plazaFillTopicItemOption(data, selectItem, selectItemArea, titleId, imgDescMsg) {
	jpk('#'+imgDescMsg).html(data[0]+"");
	jpk('#notice').html("<a href=\""+data[1]+"\" target=\"_blank\">注意事項</a>");
	if(data.length > 3){
		
		jpk('#'+titleId).html(data[2]+"");
		for(var prop = 3; prop<data.length ; prop++){
			var item = data[prop];
			if(item.parentId == -1){
				jpk('#'+selectItem).get(0).options.add(new Option(item.name, -1));
			}else{
				jpk('#'+selectItem).get(0).options.add(new Option('　'+item.name, item.id));
			}
		}
 	jpk('#'+selectItemArea).show();
	}
 	
}



//找出遊戲選項
function findGameOptions(selectId){
	pkdwr.findOpenGames({
		callback:function(dataFromServer){
		showGameOptions(dataFromServer,selectId);	}
	});
}

function showGameOptions(data,selectId){
	for(var prop in data){
		var game = data[prop];
		jpk('#'+selectId).get(0).options.add(new Option(game.chineseName, game.id));
	}
}

//檢查帳號
function checkAccountExistence(acc){
	if(acc==""){
		jpk('#divAccountInfo').html("<font color=\"red\"><b>請輸入帳號</b></font>");
	}else{
		pkdwr.checkAccountExistence(acc, {
			callback:function(dataFromServer){
			showCheckAccountExistenceResult(dataFromServer);	}
		});
	}
}

//檢查帳號
function checkAccountExistence(acc, t2o){

	if(acc==""){
		jpk('#divAccountInfo').html("<font color=\"red\"><b>請輸入帳號</b></font>");
	}else{
		
		if(t2o == "true"){
			pkdwr.checkAccountExistenceT2O(acc, {
				callback:function(dataFromServer){
				showCheckAccountExistenceResult(dataFromServer);	}
			});
		}
		else{
			pkdwr.checkAccountExistence(acc, {
				callback:function(dataFromServer){
				showCheckAccountExistenceResult(dataFromServer);	}
			});
		}
	}
}



function showCheckAccountExistenceResult(data){
	
	if(data=="duplicate"){
		jpk('#divAccountInfo').html("<font color=\"red\"><b>該帳號已存在，請使用其他帳號!</b></font>");
	}else if(data=="formatError"){
		jpk('#divAccountInfo').html("<font color=\"red\"><b>帳號不可全數字</b></font>");
	}else if(data == "empty"){
		jpk('#divAccountInfo').html("<font color=\"red\"><b>請輸入帳號</b></font>");
	}else if(data=="ok"){
		jpk('#divAccountInfo').html("<img src=\"/images/check_yes.jpg\" width=\"16\" height=\"16\"/>");
		
	}
}

//檢查帳號
/*
function checkMobileExistence(mobile){
	if(mobile == ""){
		jpk('#divMobileInfo').html("<font color=\"red\"><b>請輸入行動電話!</b></font>");
	}else{
		pkdwr.checkMobileExistence(mobile, {
			callback:function(dataFromServer){
			showCheckMobileExistenceResult(dataFromServer);	}
		});
	}
}

function showCheckMobileExistenceResult(data){
	if(data){
		jpk('#divMobileInfo').html("<font color=\"red\"><b>該行動電話已存在，請使用其他行動電話!</b></font>");
	}else{
		jpk('#divMobileInfo').html("<img src=\"/images/check_yes.jpg\" width=\"16\" height=\"16\"/>");
		
	}
}
*/

//更改縣市選項
function changeCounty(code, selectTownObj, zipObj){
    DWRUtil.removeAllOptions(selectTownObj);
    zipObj.value = "";
    pkdwr.findTownsByCounty(code, {
    	callback:function(dataFromServer){
    		fillTownOption(dataFromServer, selectTownObj, zipObj)
    	}
    });
}

//顯示區域選項
function fillTownOption(region, selectTownObj, zipObj) {
	var getCode = function(unit) { return unit.code };
 	var getName = function(unit) { return unit.value };
 	if(region.length > 0)
 	{
	  	if(region.length > 1){
	    	DWRUtil.addOptions(selectTownObj, [{name:"請選擇", id:""}], getCode, getName ); 
	    	DWRUtil.addOptions(selectTownObj, region, getCode, getName);
	    	//zipObj.value = DWRUtil._getValueFrom(region[0], getCode);
	    }else{
	    	DWRUtil.addOptions(selectTownObj, region, getCode, getName);
	    	zipObj.value = DWRUtil._getValueFrom(region[0], getCode);
	    }
    }else
    {
	    DWRUtil.addOptions(selectTownObj, [  { code:'', name:'' }], "code", "value");
    	zipObj.value = '';
    }
}


//加入好友 
function addFriend(acc){
	pkdwr.addFriend(acc, {
		callback:function(dataFromServer){
		showAddFriendResult(dataFromServer,acc);	}
	});
}

function showAddFriendResult(data, acc ){
	if( data == 'ok'){
		alertMessage('成功加入'+acc+'為好友');
	}else if(data == "notLogin"){
		alertMessage('請先登入');
	}else if(data == "fail"){
		alertMessage('失敗');
	}else if(data == "notExist"){
		alertMessage('找不到'+acc);
	}else if(data == "duplicate"){
		alertMessage(acc+'已經是您的好友');
	}
}

//送心心
function giveHeart(receiverId){
	pkdwr.giveHeart(receiverId, {
		callback:function(dataFromServer){
		showGiveHeartResult(dataFromServer);	}
	});
}

function showGiveHeartResult(data ){
	if( data == 'ok'){
		alertMessage('送心心成功');
	}else if(data == "notLogin"){
		alertMessage('請先登入');
	}else if(data == "fail"){
		alertMessage('失敗');
	}else if(data == "hasDone"){
		alertMessage('4小時內只能送1次');
	}
}

//送手機驗証碼
function sendMobileConfirmCode(msisdn){
	pkdwr.sendMobileConfirmCode(msisdn, {
		callback:function(dataFromServer){
		showSendMobileConfirmCodeResult(dataFromServer,msisdn);	}
	});
}

function showSendMobileConfirmCodeResult(data, msisdn){
	jpk("#alertMsgBox").dialog('close');
	if( data == 'ok'){
		alertMessage('驗証碼已發送至'+msisdn);
		jpk('#confirmBt').attr("disabled", false);
		jpk('#button').attr("disabled", true);
		jpk('#msisdn').html(msisdn);
	}else if(data == "notLogin"){
		alertMessage('請先登入');
	}else if(data == "fail"){
		alertMessage('發送失敗');
	}else if(data == "hasDone"){
		alertMessage('此行動電話已通過認證');
	}else if(data == "duplicate"){
		alertMessage(msisdn+",該行動電話已存在，請使用其他行動電話!");
	}
}


// 取得測試帳號
function getTrialAccountFromAjax(){
	
	pkdwr.getTrialAccount({
		callback:function(dataFromServer){
		showTrialAccountFromAjax(dataFromServer);}
	});
}

function showTrialAccountFromAjax(data){
	
	var trialAccount = 	"<input type=\"hidden\" name=\"account\" value=\"" + data['account'] + "\">" + 
						"<font color=red><b>" + data['account'] + "</b></font>";
	
	var trialPassword = "<input type=\"hidden\" name=\"password\" value=\"" + data['password'] + "\">" + 
						"<font color=red><b>" + data['password'] + "</b></font>";
	
	jpk('#trialAccount').html(trialAccount);
	jpk('#trialPassword').html(trialPassword);
}


//點數兌換 伺服器列表
function remitFindGameServer(game, selectServerObj){
    DWRUtil.removeAllOptions(selectServerObj);
    pkdwr.findShoppingItemsBySiteCode(game, {
    	callback:function(dataFromServer){
    	remitFillGameServerOption(dataFromServer, selectServerObj);
    	}
    });
}

//顯示伺服器選項
function remitFillGameServerOption(servers, selectServerObj) {
	var getCode = function(unit) { return unit.code; };
 	var getName = function(unit) { return unit.name; };
 	if(servers.length > 0)
 	{
	  	if(servers.length > 1){
	    	DWRUtil.addOptions(selectServerObj, [{name:"請選擇", name:""}], getCode, getName ); 
	    	DWRUtil.addOptions(selectServerObj, servers, getCode, getName);
	    }else{
	    	DWRUtil.addOptions(selectServerObj, servers, getCode, getName);
	    }
    }else
    {
	    DWRUtil.addOptions(selectServerObj, [  { code:'請選擇', name:'' }], "code", "name");
    }
}


//測試帳號轉正式
function doTrialAccount2Official(acc, pwd){
	
	pkdwr.doTrialAccount2Official(acc, pwd,{
		callback:function(dataFromServer){
		showTrialAccount2Official(dataFromServer);}
	});
}

function showTrialAccount2Official(data){
	if(data == "inputError"){
		jpk('#t2oinfo').html("您輸入的帳號或密碼錯誤");
	}else{
		window.location = "/mem/t2o.do?acc="+data;
	}
}



//遊戲伺服器列表
function ccFindGameServer(game, selectServerObj){
    DWRUtil.removeAllOptions(selectServerObj);
    pkdwr.findGameServersByGameId(game, {
    	callback:function(dataFromServer){
    	ccFillGameServerOption(dataFromServer, selectServerObj);
    	}
    });
}

//顯示遊戲伺服器選項
function ccFillGameServerOption(servers, selectServerObj) {
	
 	var getName = function(unit) { return unit.name; };
 	if(servers.length > 0)
 	{
	    DWRUtil.addOptions(selectServerObj, servers, getName, getName);
    }else
    {
	    DWRUtil.addOptions(selectServerObj, [  { name:'', name:'請選擇' }], "name", "name");
    }
}