// JavaScript Document
var fileClipAct="module/ajax_clipact.php";
function $(id){return document.getElementById(id);};
function $name(name){return document.getElementsByName(name);};
function $value(node,name){try{return node.getElementsByTagName(name)[0].firstChild.nodeValue;}catch(e){return "";}}
function getDataNode(p,t){try{return p.getElementsByTagName(t)[0].firstChild.nodeValue;}catch(e){return "";}};
function setCenter(id){$(id).style.left=((getWindowSizeAll(true)[0]-$(id).offsetWidth)/2+getScrollXY()[0])+"px";$(id).style.top=((getWindowSizeAll(true)[1]-$(id).offsetHeight)/2+getScrollXY()[1])+"px";};
function setThisBoxLogin(id){$(id).style.left=((getWindowSizeAll(true)[0]-$(id).offsetWidth)/2+getScrollXY()[0])+"px";$(id).style.top=((getWindowSizeAll(true)[1]-$(id).offsetHeight)/2+getScrollXY()[1]+250)+"px";};
function spacialTag(txt){txt=txt.replace(/>/g,'&gt;');txt=txt.replace(/</g,'&lt;');return txt;}
function spacialChar(txt){
	txt = txt.replace(/\\u000a/g,"<br>");
	txt = txt.replace(/\\u003c/g,"<");
	txt = txt.replace(/\\u003e/g,">");
	txt = txt.replace(/\\u0026/g,"&");
	txt = txt.replace(/\\/g,"");
	return txt;
}
////////////////////////////////////////////////////////////////
////////////////////////////User Action/////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////Set Display/////////////////////////
function disChange(userID,tvID,dis){
	cp.call(fileClipAct,'disChange',resDisChange,userID,tvID,dis);
}
function resDisChange(result){
	var Display=result.getElementsByTagName('Display')[0];
	var UID=$value(Display,'UID');
	var TV=$value(Display,'TV');
	var val=$value(Display,'val');
	
	if(val==1){
		var dis=2;
		var disTxt="Published";
		var disTxtTooltip="Click to hide.";
	}else if(val==2){
		var dis=1;
		var disTxt="Hide";
		var disTxtTooltip="Click to publish.";
	}
	document.getElementById("chgDisBTT"+TV).innerHTML=disTxt;
	document.getElementById("chgDisBTT"+TV).title=disTxtTooltip;
	document.getElementById("chgDisBTT"+TV).onclick=function(){disChange(UID,TV,dis);};
}
function delClip(userID,tvID){
	cp.call(fileClipAct,'disChange',resDelClip,userID,tvID,0);
}
function resDelClip(result){
	var Display=result.getElementsByTagName('Display')[0];
	var UID=$value(Display,'UID');
	var TV=$value(Display,'TV');
	var val=$value(Display,'val');
	window.location="myVdo.php?user="+UID;
}
function addPlayList(clipID,chkLogin,fmbID){
	if(chkLogin=="true"){
		cp.call(fileClipAct,'addPlayList',resAddPlayList,clipID,fmbID);
	}else if(chkLogin=="false"){
		//thislogin(pathLogin);
		alert("กรุณาเข้าสู่ระบบ");
	}else{
		alert("Error.");
	}
}
function resAddPlayList(result){
	var playlist=result.getElementsByTagName('playlist')[0];
	var clipID=playlist.getAttribute('id');
	var over=playlist.getElementsByTagName('over')[0].firstChild.nodeValue;
	var txtDis=playlist.getElementsByTagName('txtDis')[0].firstChild.nodeValue;
	alert(txtDis);
}
function noticeDel(clipID,chkLogin,fmbID){
//	if(chkLogin=="true"){
		cp.call(fileClipAct,'noticeDel',resNoticeDel,clipID,fmbID);
//	}else if(chkLogin=="false"){
//		thislogin(pathLogin);
//	}else{
//		alert("Error.");
//	}
}
function resNoticeDel(result){
	loadding(closeManage);
	var reason=result.getElementsByTagName('reason')[0];
	var clipID=reason.getAttribute('id');
	var fmbID=reason.getAttribute('uid');
	var reasonlist=reason.getElementsByTagName('reasonlist');
	
	var html ="";
		html+="<div class=\"popupSendDel\">";
		html+="<div class=\"txtTop\">แจ้งลบ<span class=\"popupRight\"><img src=\"images/icon/cross.png\" onclick =\"closeManage();\"></span></div>";
		html+="<div class=\"sendDelInput\">";
		html+="<div class=\"sdinput\">";
		html+="<div class=\"sdtaxt\">เลือกสาเหตุ&nbsp;:&nbsp;</div>";
		html+="<div class=\"sdinput\">";
		html+="<select id=\"reasonDel\">";
		html+="<option value=\"0\">กรุณาเลือก</option>"
		for(var i=0;i<reasonlist.length;i++){
			var reasonID=reasonlist[i].getAttribute('id');
			var reasonDetail=$value(reasonlist[i],'detail');
			html+="<option value=\""+reasonID+"\">"+reasonDetail+"</option>";
		}
		html+="</select>";
		html+="</div> ";
		html+="<div class=\"clear\"></div>";
		html+="</div>";
		html+="<div class=\"sdinput\">";
		html+="<div class=\"sdtaxt\">เหตุผล&nbsp;:&nbsp;</div>";
		html+="<div class=\"sdinput\"><textarea id=\"reasonDetail\"></textarea></div>";
		html+="<div class=\"clear\"></div>";
		html+="</div>";
		html+="</div>";
		html+="<div class=\"clear\"></div>";
		html+="<div class=\"popupInputsend\">";
		html+="<span><input type=\"button\" value=\"Report\" class=\"popupBT\" onclick=\"return addNoticeDel("+fmbID+","+clipID+");\"></span>";
		html+="<span><input type=\"button\" value=\"Cancel\" class=\"popupBT\"></span>";
		html+="</div>";
		html+="</div>";
	showManageNode=document.createElement('div');	
	showManageNode.className="popupBox";		
	showManageNode.setAttribute("id","showManage");	
	showManageNode.innerHTML=html;	
	document.body.appendChild(showManageNode);
	setThisBoxLogin('showManage');

}
function addNoticeDel(fmbID,clipID){
	var reasonID=document.getElementById("reasonDel").value;
	var reasonDetail=document.getElementById("reasonDetail").value;
	if(reasonDetail!=""){
		cp.call(fileClipAct,'addNoticeDel',resAddNoticeDel,reasonID,clipID,fmbID,reasonDetail);
	}else{
		alert("Input");
	}
}
function resAddNoticeDel(result){
	var notice=result.getElementsByTagName('notice')[0];
	var clipID=notice.getAttribute('id');
	var over=notice.getElementsByTagName('over')[0].firstChild.nodeValue;
	closeManage();
}
function addSubscriber(chkLogin,fmbID,subscb_fmb_id){
	if(chkLogin=="true"){
		cp.call(fileClipAct,'addSubscriber',resAddSubscriber,fmbID,subscb_fmb_id);
	}else if(chkLogin=="false"){
		//thislogin(pathLogin);
		alert("กรุณาเข้าสู่ระบบ");
	}else{
		alert("You already subscribed.");
	}
}
function resAddSubscriber(result){
	var Subscriber=result.getElementsByTagName('Subscriber')[0];
	var clipUserID=Subscriber.getAttribute('id');
	var over=Subscriber.getElementsByTagName('over')[0].firstChild.nodeValue;
	var txtDis=Subscriber.getElementsByTagName('txtDis')[0].firstChild.nodeValue;
	alert(txtDis);
}
function thislogin(pathLogin){
	loadding(closeManage);
	var html="";
		html+="<form name=\"fmLogin2\" id=\"fmLogin2\" action=\""+pathLogin+"\"  method=\"post\">";
		html+="<div class=\"popupsendmail\">";
		html+="<div class=\"txtTop\">"+lgLogin+"<span class=\"popupRight\">";
		html+="<a href=\"javascript:void(0)\" onclick=\"javascript:window.open('http://member.mediathai.net');closeManage(); return false;\" target=\"_blank\">"+lgRegister+"</a>&nbsp;|&nbsp;";
		html+="<a href=\"javascript:void(0)\" onclick=\"javascript:window.open('http://member.mediathai.net/module/help/message_email.php');closeManage(); return false;\">"+lgForgotPwd+"</a>&nbsp;<img src=\"images/icon/cross.png\" onclick =\"closeManage();\">";
		html+="</span></div>";
		html+="<div class=\"sendmailInput\">";
		html+="<div class=\"sminput\"><div class=\"staxt\">"+lgUsername+"&nbsp;:&nbsp;</div><div class=\"sinput\"><input type=\"text\" name=\"txt_username2\" id=\"txt_username2\" class=\"inputPopup\" value=\"\"/></div><div class=\"clear\"></div></div>";
		html+="<div class=\"sminput\"><div class=\"staxt\">"+lgPassword+"&nbsp;:&nbsp;</div><div class=\"sinput\"><input  type=\"password\" name=\"txt_password2\" id=\"txt_username2\" class=\"inputPopup\" value=\"\" /></div><div class=\"clear\"></div></div>";
		html+="</div>";
		html+="<div class=\"clear\"></div>";
		html+="<div class=\"boxloginFooter txtlogin\">";
		html+="<div class=\"floatLeft\">";
		html+="<input type=\"checkbox\" name=\"chkmemory\" style=\"vertical-align:middle;\"/>&nbsp;Remember</div>";
		html+="<div class=\"floatRight\">";
		html+="<input type=\"submit\" name=\"Button\" value=\""+lgLogin+"\" style=\"vertical-align:middle;\" class=\"popupBT\" onclick=\"return checkLoinInut2();\" />";
		html+="</div>";
		html+="<div class=\"clear\"></div>";
		html+="</div>";
		html+="</div>";
		html+="</form>";
	
	showManageNode=document.createElement('div');	
	showManageNode.className="popupBox";		
	showManageNode.setAttribute("id","showManage");	
	showManageNode.innerHTML=html;	
	document.body.appendChild(showManageNode);
	setThisBoxLogin('showManage');
}
function checkLoinInut2(){
	if(document.getElementsByName("txt_username2")[0].value==""){
		document.getElementsByName("txt_username2")[0].focus();
		alert("Iuput Username");
		return false;
	}else if(document.getElementsByName("txt_password2")[0].value==""){
		document.getElementsByName("txt_password2")[0].focus();
		alert("Iuput Password");
		return false;
	}else{
		document.forms.fmLogin2.submit();
	}
}
function selectEMBBox(){
	var tempval=document.getElementById("embedThisClip");
	document.getElementById("embedThisClip").focus();
	document.getElementById("embedThisClip").select();	
	try {
	therange=document.getElementById("embedThisClip").createTextRange()
	therange.execCommand("Copy", false, false)
	} catch (err) {}
}
function boxSendmail(path,url){
	loadding(closeManage);
	var html="";
	html+="<div class=\"popupsendmail\">";
	html+="<div class=\"txtTop\">Sead to Friend<span class=\"popupRight\"><img src=\"images/icon/cross.png\" onClick=\"closeManage();\"></span></div>";
	html+="<div class=\"sendmailInput\" id=\"responseSM\">";
	html+="<div class=\"sminput\"><div class=\"staxt\">To &nbsp; : &nbsp;</div> <div class=\"sinput\"><input type=\"taxt\" id=\"MailTo\" name=\"MailTo\" class=\"inputPopup\" /></div> <div class=\"clear\"></div></div>";
	html+="<div class=\"sminput\"><div class=\"staxt\">From &nbsp; : &nbsp;</div> <div class=\"sinput\"><input type=\"taxt\" id=\"MailFrom\" name=\"MailFrom\" class=\"inputPopup\" /></div><div class=\"clear\"></div></div>";
	html+="</div>";
	html+="<div class=\"clear\"></div>";
	html+="<div class=\"popupInputsend\" id=\"responseSMBT\">";
	html+="<span><input type=\"button\" value=\"Send\" class=\"popupBT\" onClick=\"sendmail();\"><input name=\"url\" id=\"url\" type=\"hidden\" value=\""+url+"\"></span>&nbsp;";
	html+="<span><input type=\"button\" value=\"Cancel\" class=\"popupBT\" onClick=\"closeManage();\"></span>";
	html+="</div>";
	html+="</div>";

	showManageNode=document.createElement('div');	
	showManageNode.className="popupBox";		
	showManageNode.setAttribute("id","showManage");	
	showManageNode.innerHTML=html;	
	document.body.appendChild(showManageNode);
	setThisBoxLogin('showManage');
}
function sendmail(){
	var to = $('MailTo').value;
	var from = $('MailFrom').value;
	var url = $('url').value;
	$('responseSM').innerHTML="<center><img src=\"../images/icon/loading.gif\" height=\"15\" width=\"15\" /></center>";
	$('responseSMBT').innerHTML="";
	cp.call(fileClipAct,'send',resSendmail,to,from,url);
}
function resSendmail(result){
	var ms = $value(result,'sm');
	var html="";
	html+="<center>"+ms+"</center>";
	$('responseSM').innerHTML=html;
	$('responseSMBT').innerHTML="";
}
function sendMessage(sesID,pathLogin,tofmbID){
	if(sesID==""||sesID=="0"){
		//chklogin(sesID,pathLogin);
		alert("กรุณาเข้าสู่ระบบ");
	}else if(sesID==tofmbID){
		loadding(closeManage);
		var html ="";
			html+="<div class=\"popupSendDel\">";
			html+="<div class=\"txtTop\">Send message<span class=\"popupRight\"><img src=\"images/icon/cross.png\" onclick =\"closeManage();\"></span></div>";
			html+="<div class=\"sendDelInput\">";
			html+="<div class=\"sdinput\">";
			html+="<center>คุณไม่สามารถส่งข้อความหาตัวเองได้</center>";
			html+="<div class=\"clear\"></div>";
			html+="</div>";
			html+="<div class=\"clear\"></div>";
			html+="<div class=\"popupInputsend\">";
			html+="<span><center><input type=\"button\" value=\"Close\" class=\"popupBT\" onclick =\"closeManage();\"></center></span>";
			html+="</div>";
			html+="</div>";
		showManageNode=document.createElement('div');	
		showManageNode.className="popupBox";		
		showManageNode.setAttribute("id","showManage");	
		showManageNode.innerHTML=html;	
		document.body.appendChild(showManageNode);
		setCenter('showManage');
	}else{
		loadding(closeManage);
		var html ="";
			html+="<div class=\"popupSendDel\">";
			html+="<div class=\"txtTop\">Send message";
			html+="<span class=\"popupRight\"><img src=\"images/icon/cross.png\" onclick=\"closeManage();\" /></span></div>";
			html+="<div class=\"sendDelInput\">";
			html+="<div class=\"sdinput\" id=\"boxData\" style=\"width:400px; text-align:center;\">";
			html+="<textarea name=\"MsgText\" id=\"MsgText\" style=\"width:400px; height:150px;\"></textarea>";
			html+="<div class=\"clear\"></div>";
			html+="</div>";
			html+="</div>";
			html+="<div class=\"clear\"></div>";
			html+="<div class=\"popupInputsend\" id=\"boxBTT\" style=\"width:400px; text-align:right;\">";
			html+="<input type=\"button\" value=\"Send\" class=\"popupBT\" onclick=\"submitMessage("+tofmbID+");\">&nbsp;";
			html+="<input type=\"button\" value=\"Cancel\" class=\"popupBT\" onclick =\"closeManage();\">";
			html+="</div>";
			html+="</div>";
			
		showManageNode=document.createElement('div');	
		showManageNode.className="popupBox";		
		showManageNode.setAttribute("id","showManage");	
		showManageNode.innerHTML=html;	
		document.body.appendChild(showManageNode);
		setCenter('showManage');
	}
}
function submitMessage(tofmbID,frmID){
	var messageText=document.getElementById("MsgText").value;
	document.getElementById("boxData").innerHTML="<center>Sending...<br /><img src=\"../images/icon/loading.gif\" height=\"15\" width=\"15\" /></center>";
	document.getElementById("boxBTT").innerHTML="";
	setCenter('showManage');
	cp.call(fileClipAct,'sendMessage',resSendMessage,tofmbID,frmID,messageText);
}
function resSendMessage(result){
	var sendmsg=result.getElementsByTagName('sendmsg')[0];
	var resultText=spacialChar($value(sendmsg,'sendtxt'));
	document.getElementById("boxData").innerHTML=resultText+"<div class=\"clear\"></div>";
	document.getElementById("boxBTT").innerHTML="<input type=\"button\" value=\"Close\" class=\"popupBT\" onclick =\"closeManage();\">";
}
function addFriend(sesID,pathLogin,tofmbID){
	if(sesID==""||sesID=="0"){
		//chklogin(sesID,pathLogin);
		alert("กรุณาเข้าสู่ระบบ");
	}else if(sesID==tofmbID){
		loadding(closeManage);
		var html ="";
			html+="<div class=\"popupSendDel\">";
			html+="<div class=\"txtTop\">Add Banlek Friend<span class=\"popupRight\"><img src=\"images/icon/cross.png\" onclick =\"closeManage();\"></span></div>";
			html+="<div class=\"sendDelInput\">";
			html+="<div class=\"sdinput\">";
			html+="<center>คุณไม่สามารถเพิ่มตัวเองเป็นเพื่อนได้</center>";
			html+="<div class=\"clear\"></div>";
			html+="</div>";
			html+="<div class=\"clear\"></div>";
			html+="<div class=\"popupInputsend\">";
			html+="<span><center><input type=\"button\" value=\"Close\" class=\"popupBT\" onclick =\"closeManage();\"></center></span>";
			html+="</div>";
			html+="</div>";
		showManageNode=document.createElement('div');	
		showManageNode.className="popupBox";		
		showManageNode.setAttribute("id","showManage");	
		showManageNode.innerHTML=html;	
		document.body.appendChild(showManageNode);
		setCenter('showManage');
	}else{
		loadding(closeManage);
		var html ="";
			html+="<div class=\"popupSendDel\">";
			html+="<div class=\"txtTop\">Add Banlek Friend";
			html+="<span class=\"popupRight\"><img src=\"images/icon/cross.png\" onclick=\"closeManage();\" /></span></div>";
			html+="<div class=\"sendDelInput\">";
			html+="<div class=\"sdinput\" id=\"boxData\" style=\"width:400px; text-align:center;\">";
			html+="<textarea name=\"MsgText\" id=\"MsgText\" style=\"width:400px; height:150px;\"></textarea>";
			html+="<div class=\"clear\"></div>";
			html+="</div>";
			html+="</div>";
			html+="<div class=\"clear\"></div>";
			html+="<div class=\"popupInputsend\" id=\"boxBTT\" style=\"width:400px; text-align:right;\">";
			html+="<input type=\"button\" value=\"Send\" class=\"popupBT\" onclick=\"submitFriend("+tofmbID+");\">&nbsp;";
			html+="<input type=\"button\" value=\"Cancel\" class=\"popupBT\" onclick =\"closeManage();\">";
			html+="</div>";
			html+="</div>";
			
		showManageNode=document.createElement('div');	
		showManageNode.className="popupBox";		
		showManageNode.setAttribute("id","showManage");	
		showManageNode.innerHTML=html;	
		document.body.appendChild(showManageNode);
		setCenter('showManage');
	}
}
function submitFriend(tofmbID,frmID){
	var messageText=document.getElementById("MsgText").value;
	document.getElementById("boxData").innerHTML="<center>Sending...<br /><img src=\"../images/icon/loading.gif\" height=\"15\" width=\"15\" /></center>";
	document.getElementById("boxBTT").innerHTML="";
	setCenter('showManage');
	cp.call(fileClipAct,'addFriend',resAddFriend,tofmbID,frmID,messageText);
}
function resAddFriend(result){
	var friend=result.getElementsByTagName('friend')[0];
	var resultText=spacialChar($value(friend,'resalttext'));
	document.getElementById("boxData").innerHTML=resultText+"<div class=\"clear\"></div>";
	document.getElementById("boxBTT").innerHTML="<input type=\"button\" value=\"Close\" class=\"popupBT\" onclick =\"closeManage();\">";
}
function aboutme(fmbID,act){
	cp.call(fileClipAct,'getAboutme',resGetAboutme,fmbID,act);
}
function resGetAboutme(result){
	document.getElementById("aboutmeData").innerHTML="<center><img src=\"../images/icon/loading.gif\" height=\"15\" width=\"15\" /></center>";
	var aboutme=result.getElementsByTagName('aboutme')[0];
	var act=$value(aboutme,'act');
	var userID=$value(aboutme,'user');
	var resultText=spacialChar($value(aboutme,'result'));
	var txtStr=(resultText.replace(/[\n ]+/g,' ')).length;
	
	if(act=="edit"){
		var html ="";
			html+="<div class=\"sdinput\" id=\"boxData\" style=\"width:355px; text-align:center; vertical-align:top;\">";
			html+="<textarea name=\"aboutmeTxt\" id=\"aboutmeTxt\" style=\"width:355px; height:60px;\" onkeyup=\"textCheckCount(event.keyCode);\">"+resultText+"</textarea>";
			html+="<br /><span class=\"popupRight\"><input type=\"button\" value=\"Save\" class=\"popupBT\" onclick=\"saveAboutme("+userID+");\">";
			html+="&nbsp;<input type=\"button\" value=\"Cancel\" class=\"popupBT\" onclick =\"aboutme("+userID+",'get');\"></span>";
			html+="<span class=\"popupRight\" id=\"wordCount\" style=\"color:#FF0000;\">"+txtStr+"/150</span>";
			html+="</div>";
		document.getElementById("aboutmeData").innerHTML=html;
	}else if(act=="get"){
		document.getElementById("aboutmeData").innerHTML=resultText;
	}else if(act=="save"){
		document.getElementById("aboutmeData").innerHTML=resultText;
	}
}
function saveAboutme(fmbID){
	var aboutmeTxt=document.getElementById("aboutmeTxt").value;
	cp.call(fileClipAct,'saveAboutme',resGetAboutme,fmbID,aboutmeTxt);
}
function textCheckCount(e){
	txtStr=$('aboutmeTxt').value.replace(/[\n ]+/g,' ');
	txtLength=txtStr.length;
	$('wordCount').innerHTML=(txtLength>=150)?"<font style=\"color:#FF0000;\">"+txtLength+"/150</font>":txtLength+"<font>/150</font>";
	if(e!=8){
		if(txtLength>150){ alert("word over"); return false;}else{return true;}
	}
}

