// JavaScript Document

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 addComment(friend,fmb_id){
	/*var a=$("commentTxt").value;
		a=a.replace(/^\s+|\s+$/g,"<br>");
		alert(a);
		if(a=="<br>"){alert(55555);}**/
	if(friend==""){
		if(confirm("คุณยังไม่ได้ล๊อกอิน คุณต้องการล๊อกอินหรือไม่?")){
			window.location.href=('index.php');
		}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 Banlek Comment");
			$("commentTxt").value="";
			$("commentTxt").focus();
			return false;
		}else{
			var txt=$('commentTxt').value;		
			//txt=txt.replace(/[\n ]+/g,' ');	
			$('ButCom').disabled=true;
			cp.call('module/ajax_banlekComment.php','banlekCommentAdd',resAddComment,friend,fmb_id,txt);//$friend,$fmb_id,$Ip,$txt
			return true;	
			
		}
	}
		
}
function resAddComment(result){	
	/*var tbl_check=result.getElementsByTagName('checktext');	
	var chk=tbl_check[0].getElementsByTagName('Ctxt')[0].firstChild.nodeValue;
	alert(chk);
	if(chk==0){
		alert("Please Type Banlek Comment");
		$("commentTxt").value="";
		$("commentTxt").focus();
		return false;
	}else if(chk==1){*/
		var tbl_select=result.getElementsByTagName('tbl_comment');	
		var cID=tbl_select[0].getElementsByTagName('COMMENTROOM_ID')[0].firstChild.nodeValue;
		var cDetail=spacialTag($('commentTxt').value);
			cDetail=cDetail.replace(/\n/g,'<br>');
		var cDate=tbl_select[0].getElementsByTagName('COMMENTROOM_DATE')[0].firstChild.nodeValue;
		var cfmb=tbl_select[0].getElementsByTagName('fmb_id')[0].firstChild.nodeValue;
		var cfriend=tbl_select[0].getElementsByTagName('friend')[0].firstChild.nodeValue;
		var cimg=tbl_select[0].getElementsByTagName('img')[0].firstChild.nodeValue;
		var cName=spacialChar(tbl_select[0].getElementsByTagName('display_name')[0].firstChild.nodeValue);
		var cPath=tbl_select[0].getElementsByTagName('propath')[0].firstChild.nodeValue;
		
		var html="";	
		var newNodeP=document.createElement("p");
			newNodeP.className="guestbook showLine";
			newNodeP.id=cID;			
			html+="<span class=\"show_pic\"><a href=http://banlek.com/"+cPath+" title="+cName+"><img src=\"http://member.mediathai.net/memberFile/images/"+cimg+"\" /></a></span>";
			//html+="<span class=\"show_link\"><a href=\"guestbook_view.php\">"+banlekGuestbookViewComment+" "+cName+"</a></span>";
			html+="<span class=\"show_name\"><a href=\"http://talk.mediathai.net/"+cPath+"/\" class=\"link_name_user\" onclick=\"return isCard.start(this,"+cfriend+",event);\">"+cName+" </a>";
			html+="<font class=\"a_s_font_small\">"+cDate+"</font></span>";
			html+="<span class=\"show_comment\">"+cDetail+"</span>";		
			if(cfmb==cfriend){
				html+="<span class=\"show_btn\"><a href=\"#\" onclick=\"DeleteComment("+cID+"); return false;\">"+banlekButtonDeleteComment+"</a></span>";
			}
			
		newNodeP.innerHTML=html;
		var a=$('box_comment').getElementsByTagName("p");
		if(a[1]){
			$("box_comment").insertBefore(newNodeP,$(a[1].id));
		}else{
			$("box_comment").appendChild(newNodeP);
		}	
		$('commentTxt').value="";	
	//}
	
	$('ButCom').disabled=false;	
}
function input_checkAC(){
	msg = $("commentTxt").value;
	
	if(msg.length >500){
		alert("Limited to 500 characters!");
		$("commentTxt").focus();
		return false;
	}
	return true;
}
function addGuestbook(friend,fmb_id){
	//alert($("commentTxt").value);
	if(friend==""){
		if(confirm("คุณยังไม่ได้ล๊อกอิน คุณต้องการล๊อกอินหรือไม่?")){
			window.location.href=('index.php');
		}else{
			return false;
		}
		return false;
	}else{
	
		
	if($('commentTxt').style.color=="rgb(102, 102, 102)" || $('commentTxt').style.color=="#666666"){	
	alert("Please Type Banlek Comment");	
		return false;
	}
	
	if(!input_checkAC()){return false;}
	if($("commentTxt").value.replace(/ /g,"")=="" ||$("commentTxt").value.replace(/[\n ]+/g,"")=="" ||$("commentTxt").value.replace(/^\s+|\s+$/g,"")==""){
		alert("Please Type Banlek Comment");
		$('commentTxt').style.color="#000000";
		//$("commentTxt").value="";
		$("commentTxt").focus();
		return false;
	}
	else{
		var txt=$('commentTxt').value;		
		//txt=txt.replace(/[\n ]+/g,' ');	
		$('btCom').disabled=true;
		cp.call('module/ajax_banlekComment.php','banlekCommentAdd',resAddGuestbook,friend,fmb_id,txt);//$friend,$fmb_id,$Ip,$txt
		return true;		
	}
	}
	
}
function resAddGuestbook(result){
	var tbl_select=result.getElementsByTagName('tbl_comment');	
	var cfmb=tbl_select[0].getElementsByTagName('fmb_id')[0].firstChild.nodeValue;
	alert("complete");
	$('btCom').disabled=false;
	top.window.location.replace("guestbook.php?fmb_id="+cfmb+"");
}
function addGuestbookView(friend,fmb_id){
	if(friend==""){
		if(confirm("คุณยังไม่ได้ล๊อกอิน คุณต้องการล๊อกอินหรือไม่?")){
			window.location.href=('index.php');
		}else{
			return false;
		}
		return false;
	}else{
	if($('commentTxt').style.color=="rgb(102, 102, 102)" || $('commentTxt').style.color=="#666666"){	
	alert("Please Type Banlek Comment");	
		return false;
	}
	
	if(!input_checkAC()){return false;}
	if($("commentTxt").value.replace(/ /g,"")=="" ||$("commentTxt").value.replace(/[\n ]+/g,"")=="" ||$("commentTxt").value.replace(/^\s+|\s+$/g,"")==""){
		alert("Please Type Banlek Comment");
		$('commentTxt').style.color="#000000";
		//$("commentTxt").value="";
		$("commentTxt").focus();
		return false;
	}
	else{
		var txt=$('commentTxt').value;		
		//txt=txt.replace(/[\n ]+/g,' ');	
		$('btCom').disabled=true;
		cp.call('module/ajax_banlekComment.php','banlekCommentAdd',resAddGuestbookView,friend,fmb_id,txt);//$friend,$fmb_id,$Ip,$txt
		return true;		
	}
	}
}
function resAddGuestbookView(result){
	var tbl_select=result.getElementsByTagName('tbl_comment');	
	var cfriend=tbl_select[0].getElementsByTagName('friend')[0].firstChild.nodeValue;
	var cfmb=tbl_select[0].getElementsByTagName('fmb_id')[0].firstChild.nodeValue;
	
	alert("complete");
	//alert(cfriend);
	//guestbook_view.php?f_id=262&fmb_id=1998
	$('btCom').disabled=false;
	top.window.location.replace("guestbook_view.php?f_id="+cfmb+"&fmb_id="+cfriend+"");
	
	//top.window.location.replace("guestbook.php?fmb_id="+cfmb+"");
	//alert(cfmb);
}
//======================Delete Comment==============================
function DeleteComment(id){
	//confirm.('555555');
	//alert(id);
	if(confirm("คุณต้องการที่จะลบข้อมูลหรือไม่ ")){
	cp.call('module/ajax_banlekComment.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 checkTextStyle(e){

if($("commentTxt").value.replace(/ /g,"")=="" ||$("commentTxt").value.replace(/[\n ]+/g,"")=="" ||$("commentTxt").value.replace(/\n /g,'')==''){
	e.style.color="#666666";
	$('commentTxt').value=$('commentTxt').getAttribute('guestBookTo');
}else{
	if(e.style.color=="rgb(102, 102, 102)" || e.style.color=="#666666"){
	$('commentTxt').value="";
	e.style.color="#000000";
	}
}
//this.style.color='#000'	
}
//==================================================================
/*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>" ;
	}
}*/
