// JavaScript Document
var banlekPaht="http://www.banlek.com/";
function $(id){return document.getElementById(id);}
function spacialTag(txt){
	txt=txt.replace(/>/g,'&gt;');
	txt=txt.replace(/</g,'&lt;');
	return txt;
}
function spacialChar(txt){
	txt=txt.replace(/\\u000a|\\u000d/g," ");
	txt=txt.replace(/\\u0026/g,"&");
	return txt;
}
function loadRoom(id){
	
	var rId=$('loadRoom_id').value;
	top.window.location.replace("room.php?loadRoom_id="+rId+"&fmb_id="+id+"");
}
function editNameRoom(a){
	var len=$('editName').value.length;
	var html="";
	html+="<span class=\"room_count_text\"><span id=\"spanInputCount\"";
	if(len==50){
		html+="style=\"color:#ff0000\"";
		}
	html+=" >"+len+"</span><font>/50</font></span>";
	html+="<img onclick=\"saveName("+a+");\" src=\"images/button/btn_save.gif\" style=\"cursor:pointer\" />";
	html+="<img onclick=\"cancelEditName("+a+");\" src=\"images/button/btn_cancel.gif\" style=\"cursor:pointer\" />";
	$('boxNameRoom').innerHTML=html;
	$('editName').readOnly=false;		
	$('editName').focus();						
}
function cancelEditName(a){
	var html="";
	html+="<img onclick=\"editNameRoom("+a+");\" src=\"images/button/btn_edit.gif\" style=\"cursor:pointer\" />";	
	$('boxNameRoom').innerHTML=html;	
	$('editName').value=$('hName').value;
	$('editName').readOnly=true;	
}
function saveName(id){	
	
	if(!input_check()){return false;}
	if($("editName").value.replace(/ /g,"")=="" ||$("editName").value.replace(/[\n ]+/g,"")=="" ||$("editName").value.replace(/^\s+|\s+$/g,"")==""){
		alert("Please Type Miniroom Name");
		$("editName").value="";
		$("editName").focus();
		return false;
	}
	else{
		var name=$('editName').value;
		cp.call('module/ajax_room.php','editRoomName',resShowEditName,id,name);
		return true;		
	}
}
function resShowEditName(){
	var html="";
	html+="<img onclick=\"editNameRoom("+$('hId').value+");\" src=\"images/button/btn_edit.gif\" style=\"cursor:pointer\" />";	
	$('boxNameRoom').innerHTML=html;	
	$('hName').value=$('editName').value;
	$('editName').readOnly=true;	
}
function input_check(){
	msg = $("editName").value;
	if(msg.length > 50){
		alert("Limited to 50 characters!");
		$("editName").editName.focus();
		return false;
	}
	return true;
}
function input_keyUp(e){
	var key_code=(window.event)?window.event.keyCode:e.which; 
	if((key_code>=32 && key_code<=222)||key_code==8 || key_code==13){
		msg = document.getElementById("editName").value;
		var textCount=document.getElementById("spanInputCount");
		if(msg.length >= 50){			
			document.getElementById("editName").focus();
			textCount.style.color="#FF0000";
		}else{
			textCount.style.color="#000000";
		}
		textCount.innerHTML = msg.length ;
	}
}
//====================================================>Detail Room<===============================================================
function editDetailRoom(a){
	var len=$('editDetail').value.length;	
	var html="";
	html+="<span class=\"room_count_text\"><span id=\"spanInputCountD\"";
	if(len==250){
		html+="style=\"color:#ff0000\"";
		}
	html+=" >"+len+"</span><font>/250</font></span>";
	html+="<img onclick=\"saveDetail("+a+");\" src=\"images/button/btn_save.gif\" style=\"cursor:pointer\" />";
	html+="<img onclick=\"cancelEditDetail("+a+");\" src=\"images/button/btn_cancel.gif\" style=\"cursor:pointer\" />";
	$('boxDetailRoom').innerHTML=html;
	$('editDetail').readOnly=false;		
	$('editDetail').focus();						
}
function cancelEditDetail(a){
	var html="";
	html+="<img onclick=\"editDetailRoom("+a+");\" src=\"images/button/btn_edit.gif\" style=\"cursor:pointer\" />";	
	$('boxDetailRoom').innerHTML=html;	
	$('editDetail').value=$('hDetail').value;
	$('editDetail').readOnly=true;	
}
function saveDetail(id){	
	
	if(!input_checkD()){return false;}	
		var name=$('editDetail').value;
		cp.call('module/ajax_room.php','editRoomDetail',resShowEditDetail,id,name);
		return true;	
}
function resShowEditDetail(){
	var html="";
	html+="<img onclick=\"editDetailRoom("+$('hId').value+");\" src=\"images/button/btn_edit.gif\" style=\"cursor:pointer\" />";	
	$('boxDetailRoom').innerHTML=html;	
	$('hDetail').value=$('editDetail').value;
	$('editDetail').readOnly=true;	
}
function input_checkD(){
	msg = $("editDetail").value;
	if(msg.length >250){
		alert("Limited to 250 characters!");
		msg.focus();
		return false;
	}
	return true;
}
function input_keyUpD(e){
	var key_code=(window.event)?window.event.keyCode:e.which; 
	if((key_code>=32 && key_code<=222)||key_code==8 ||key_code==13){
		msg = document.getElementById("editDetail").value;
		var textCount=document.getElementById("spanInputCountD");
		if(msg.length >= 250){
			
			document.getElementById("editDetail").focus();
			textCount.style.color="#FF0000";
		}else{
			textCount.style.color="#000000";
		}
		textCount.innerHTML = msg.length ;
	}
}
//======================================================Add Comment===============================================================
function addComment(id,fmb_id){
	
	if(fmb_id==""){
		if(confirm("คุณยังไม่ได้ล๊อกอิน คุณต้องการล๊อกอินหรือไม่?")){
			
		}else{
			return false;
		}
		return false;
	}else{
	if(!input_checkAC()){return false;}
	if($("commentTxt").value.replace(/ /g,"")=="" ||$("commentTxt").value.replace(/[\n ]+/g,"")=="" ||$("commentTxt").value.replace(/^\s+|\s+$/g,"")==""){
		alert("Please Type Miniroom Comment");
		$("commentTxt").value="";
		$("commentTxt").focus();
		return false;
	}
	else{
		var txt=$('commentTxt').value;
		if(pv=$('PvMsg').checked==true){
		var pv=$('PvMsg').value;
		}
		txt=txt.replace(/[\n ]+/g,' ');	
		$('btCRoom').disabled=true;
		cp.call('module/ajax_room.php','roomCommentAdd',resAddComment,id,fmb_id,pv,txt);
		return true;		
	}
	}
	
}
function resAddComment(result){	
	var tbl_select=result.getElementsByTagName('tbl_comment');	
	var cID=tbl_select[0].getElementsByTagName('COMMENTROOM_ID')[0].firstChild.nodeValue;
	var cDetail=spacialTag($('commentTxt').value);
	var cDate=tbl_select[0].getElementsByTagName('COMMENTROOM_DATE')[0].firstChild.nodeValue;
	var cPv=tbl_select[0].getElementsByTagName('private_status')[0].firstChild.nodeValue;
	var cfmb=tbl_select[0].getElementsByTagName('fmb_id')[0].firstChild.nodeValue;
	var cimg=tbl_select[0].getElementsByTagName('img')[0].firstChild.nodeValue;
	var cPath=tbl_select[0].getElementsByTagName('propath')[0].firstChild.nodeValue;	
	var cName=spacialChar(tbl_select[0].getElementsByTagName('display_name')[0].firstChild.nodeValue);
	var newNodeP=document.createElement("dd");
		newNodeP.className="box_comment";
		newNodeP.id=cID;
		
		var namePv="";
		if(cPv==1){
			namePv="comment_talk_private";
		}else{
			namePv="comment_talk";
		}
		var html="";		
		html+="<p class=\"comment_pic\"><a href=\""+banlekPaht+cPath+"\" title=\"cName\"><img src=\"http://member.mediathai.net/memberFile/images/"+cimg+"\" /></a></p>";
		html+="<p class=\"comment_name\"><a href=\"http://www.banlek.com/"+cPath+"\" class=\"link_name_user\" onclick=\"return isCard.start(this,"+cfmb+",event);\">"+cName+"</a>";
		if(cPv==1){html+="<img src=\"images/icon/icon_lock.jpg\" />";}
		html+="&nbsp;&nbsp;<font class=\"a_s_font_small\" id=\"cDate"+cID+"\">"+cDate+"</font></p>";
		html+="<p class=\"comment_button\" id=\"bEditCom"+cID+"\"><a href=\"#\" title=\""+$('bEc').value+"\" onclick=\"EditCommentDetail("+cID+"); return false;\"><img src=\"images/icon/comment_edit.gif\" /></a><a href=\"#\" title=\""+$('bDc').value+"\"><img src=\"images/icon/comment_delete.gif\" onclick=\"DeleteComment("+cID+"); return false;\"/></a></p>";
		
		html+="<p class=\""+namePv+"\" id=\"cDetail"+cID+"\" style=\"display:block\">"+cDetail+"</p>";
		
		newNodeP.innerHTML=html;
		$("box_comment").insertBefore(newNodeP,$("box_commentpost"));
		$('commentTxt').value="";
		$('PvMsg').checked=false;
		$('btCRoom').disabled=false;
}
function input_checkAC(){
	msg = $("commentTxt").value;
	if(msg.length >350){
		alert("Limited to 350 characters!");
		$("commentTxt").focus();
		return false;
	}
	return true;
}
function input_keyUpAC(e){
	var key_code=(window.event)?window.event.keyCode:e.which; 
	if((key_code>=32 && key_code<=222)||key_code==8 ||key_code==13){
		msg = document.getElementById("commentTxt").value;
		var textCount=document.getElementById("spanInputCountAC");
		if(msg.length >= 350){			
			document.getElementById("commentTxt").focus();
			textCount.style.color="#FF0000";
		}else{
			textCount.style.color="#000000";
		}
		textCount.innerHTML = msg.length+"<font>/350</font>" ;
	}
}
//========================================================Edit Comment==========================================================

function EditCommentDetail(id){
	/* $(b).style.display='none';	
		   
		}else{	
			$(a).className='search_open';
			 $(b).style.display='block';*/	
	if($('commentEdit'+id)){return false;}
	var commentEditNode=document.createElement('p');
	commentEditNode.className="comment_talk_edit";
	commentEditNode.setAttribute('id','commentEdit'+id);
	
	var bHtml="";	
	bHtml+="<a href=\"#\" title=\""+$('bDc').value+"\" onclick=\"DeleteComment("+id+"); return false;\">";
	bHtml+="<img src=\"images/icon/comment_delete.gif\" /></a>";					
	$("bEditCom"+id).innerHTML=bHtml;
	
	
	var detail=$("cDetail"+id).innerHTML;	
	
	var html="";
	//html+="<input type=\"hidden\" id=\"hEidtComment"+id+"\" value=\""+$("cDetail"+id).innerHTML+"\"/>";
	html+="<textarea id=\"txtEditcomment\" onkeyup=\"input_keyUpEC(event);\">"+detail+"</textarea>";						
	html+="<span class=\"side_left\"><input type=\"button\" value=\""+$("bS").value+"\" onclick=\"saveEditComment("+id+")\" />";
	html+="<input type=\"reset\" value=\""+$("bC").value+"\" onclick=\"cancelEditComment("+id+")\"; /></span>";
	html+="<span class=\"side_right\"><span id=\"spanInputCountED\">"+detail.length+"</span><font>/350</font></span>";
	commentEditNode.innerHTML=html;
	$(id).insertBefore(commentEditNode,$('cDetail'+id));
	$("cDetail"+id).style.display='none';
	//$("tDetail"+id).innerHTML=html;
	//$("txtEditcomment").focus();
	
}
function cancelEditComment(id){
	var bHtml="";
	$("cDetail"+id).style.display='block';
	$(id).removeChild($('commentEdit'+id));
	var dt=$("cDetail"+id).innerHTML;
	//alert(dt);
	bHtml+="<a href=\"#\" title=\""+$('bEc').value+"\" onclick=\"EditCommentDetail("+id+"); return false;\">";
	bHtml+="<img src=\"images/icon/comment_edit.gif\" /></a><a href=\"#\" title=\""+$('bDc').value+"\" onclick=\"DeleteComment("+id+"); return false;\">";
	bHtml+="<img src=\"images/icon/comment_delete.gif\" /></a>";					
	$("bEditCom"+id).innerHTML=bHtml;	
	//$("cDetail"+id).className="comment_talk";
	var html="";	
	$("cDetail"+id).innerHTML=dt;	
}
function input_checkEC(){
	msg = $("txtEditcomment").value;
	if(msg.length >350){
		alert("Limited to 350 characters!");
		$("txtEditcomment").focus();
		return false;
	}
	return true;
}
function input_keyUpEC(e){
	var key_code=(window.event)?window.event.keyCode:e.which; 
	if((key_code>=32 && key_code<=222)||key_code==8 ||key_code==13){
		msg = document.getElementById("txtEditcomment").value;
		var textCount=document.getElementById("spanInputCountED");
		if(msg.length >= 350){			
			document.getElementById("txtEditcomment").focus();
			textCount.style.color="#FF0000";
		}else{
			textCount.style.color="#000000";
		}
		textCount.innerHTML = msg.length ;
	}
}

function saveEditComment(id){	
	if(!input_checkEC()){return false;}
	if($("txtEditcomment").value.replace(/ /g,"")=="" ||$("txtEditcomment").value.replace(/[\n ]+/g,"")==""){
		alert("Please Type Miniroom Comment");
		$("txtEditcomment").value="";
		$("txtEditcomment").focus();
		return false;
	}
	else{
		var name=$('txtEditcomment').value;
		/*alert(id);
		alert(name);*/
		name=name.replace(/[\n]/g," ");		
		cp.call('module/ajax_room.php','editComment',resEditComment,id,name);
		return true;
		}
}

function resEditComment(result){	
	var tbl_select=result.getElementsByTagName('tbl_comment');	
	var cID=tbl_select[0].getElementsByTagName('COMMENTROOM_ID')[0].firstChild.nodeValue;
	var cDetail=spacialTag($('txtEditcomment').value);
	var cUp=tbl_select[0].getElementsByTagName('COMMENTROOM_EDITDATE')[0].firstChild.nodeValue;
	var cPv=tbl_select[0].getElementsByTagName('private_status')[0].firstChild.nodeValue;
	
	$("cDate"+cID).innerHTML=elseEdit+cUp;
	//alert(cDetail);
	$("cDetail"+cID).style.display='block';
	$(cID).removeChild($('commentEdit'+cID));
	var bHtml="";
	bHtml+="<a href=\"#\" title=\""+$('bEc').value+"\" onclick=\"EditCommentDetail("+cID+"); return false;\">";
	bHtml+="<img src=\"images/icon/comment_edit.gif\" /></a><a href=\"#\" title=\""+$('bDc').value+"\" onclick=\"DeleteComment("+cID+"); return false;\">";
	bHtml+="<img src=\"images/icon/comment_delete.gif\" /></a>";					
	$("bEditCom"+cID).innerHTML=bHtml;
	if(cPv==1){
		$("cDetail"+cID).className="comment_talk_private";
	}else{
		$("cDetail"+cID).className="comment_talk";
	}
	$("cDetail"+cID).innerHTML=cDetail;
}
//======================================================Delete Comment====================================================
function DeleteComment(id){
	//confirm.('555555');
	//alert(id);
	if(confirm("คุณต้องการที่จะลบข้อมูลหรือไม่ ")){
	cp.call('module/ajax_room.php','hiddenComment',resDeleteComment,id);	
	return false;
	}		
	//alert(55555);
}
function resDeleteComment(result){
	//alert(55566668888);
	var tbl_select=result.getElementsByTagName('tbl_comment');	
	var cID=tbl_select[0].getElementsByTagName('COMMENTROOM_ID')[0].firstChild.nodeValue;
	$('box_comment').removeChild($(cID));
}
function deleteRoom(cR,idR){
	//alert(cR);
	//alert(idR);
	if(cR==1){
		alert("ลบไม่ได้เหลือห้องเดียว");
	}else{
		if(confirm("คุณต้องการที่จะลบข้อมูลหรือไม่ ")){
			//alert("ก็ลบสิ");	
			cp.call('module/ajax_room.php','hiddenRoom',resHiddenRoom,idR);
			return false;
		}
	}
}
function resHiddenRoom(result){
	//alert(55566668888);
var tbl_select=result.getElementsByTagName('tbl_member');
var f_id=tbl_select[0].getElementsByTagName('f_id')[0].firstChild.nodeValue;
	top.window.location.replace("room.php?fmb_id="+f_id+"");
	alert("ลบเรียบร้อยแล้วนะ");
	
}




