// JavaScript Document

function setAvatar(avnum){
	var http=getXmlHttpRequestObject();
	var url="http://www.iwatchdrama.com/api.php?do=setavatar&aid="+avnum;
	http.open("GET",url,false);
	http.send(null);
	/*alert(http.responseText);*/
	document.getElementById("currentAvatar").src="images/avatars/"+avnum;
	
}

function uploadAvatar(tobj){
	if(document.getElementById("myfile").value ==""){
		alert("Please select a photo to upload!");	
		return;
	}
	tobj.value="Please wait...";
	tobj.disabled=true;
	document.getElementById("avatarform").submit();
}

function removeFavorite(fid){
	if(confirm("Are you sure you want to remove from your Favorite List?")){
	var http=getXmlHttpRequestObject();
	var url="http://www.iwatchdrama.com/api.php?do=removefavorite&fid="+fid;
	http.open("GET",url,false);
	http.send(null);
	document.location=document.location;
	//document.getElementById("currentAvatar").src="images/avatars/"+avnum;
	}
}
function addFavorite(fid){

	var http=getXmlHttpRequestObject();
	var url="http://www.iwatchdrama.com/api.php?do=addfavorite&fid="+fid;
	http.open("GET",url,false);
	http.send(null);

	if(http.responseText.indexOf("error")>-1){
		document.location="/login.php";	
	}else{
			alert(http.responseText);
	}	
		//document.getElementById("currentAvatar").src="images/avatars/"+avnum;
	
}


function shareMovie(mid){
	
}

function showMailForm(){
	document.getElementById("newmessage").style.display="block";	
}
function removeMessage(mid){
	if(confirm("Are you sure you want to remove this message?")){
	var http=getXmlHttpRequestObject();
	var url="http://www.iwatchdrama.com/api.php?do=removemessage&mid="+mid;
	http.open("GET",url,false);
	http.send(null);
	document.location="/mail.php";
	//document.getElementById("currentAvatar").src="images/avatars/"+avnum;
	}
}

function addFriend(fid){
	var http=getXmlHttpRequestObject();
	var url="http://www.iwatchdrama.com/api.php?do=addfriend&fid="+fid;
	http.open("GET",url,false);
	http.send(null);

	if(http.responseText.indexOf("error")>-1){
		document.location="/login.php";
	}else{
		
		alert(http.responseText);	
		
	}
}

function sendMessage(mid){
	var msgtext=document.getElementById("newText");
	var subject=document.getElementById("newSubject").value;
		if(msgtext.value==""){
		alert("Please enter text to send message.");	
		return;
	}
	
	
	var http=getXmlHttpRequestObject();
	var url="http://www.iwatchdrama.com/api.php?do=newMessage&tid="+mid+"&message="+msgtext.value+"&subject="+subject;
	http.open("GET",url,false);
	http.send(null);
	
	if(http.responseText.indexOf("done")>-1){
		document.getElementById("newmessage").style.display="none";
	}else{
		window.location="http://www.iwatchdrama.com/login.php";
		
	}
	
}


function replyMessage(toid,msgid){
	
	var msgtext=document.getElementById("replyText");
	var subject=document.getElementById("replySubject").value;
	if(msgtext.value==""){
		alert("Please enter text to reply.");	
		return;
	}
	var http=getXmlHttpRequestObject();
	var url="http://www.iwatchdrama.com/api.php?do=replyMessage&tid="+toid+"&message="+msgtext.value+"&subject="+subject+"&msgid="+msgid;
	http.open("GET",url,false);
	http.send(null);
	alert(http.responseText);
	if(http.responseText.indexOf("done")>-1){
		document.location="/mail.php";	
	}else{
		alert("There was a problem trying to reply the message. Try again later.");
		document.location="/mail.php";	
	}
	
}

function searchMembers(){
	var sText=document.getElementById("searchmember").value;
	if(sText==""){
		alert("Enter name to search");	
		return;
	}
	
	document.location="/searchmember/"+sText;
}

function searchDrama(){
	var sText=document.getElementById("searchdrama").value;
	if(sText==""){
		alert("Enter title to search");	
		return;
	}
	
	document.location="/searchdrama/"+sText;
}
function showShare(){
	var sobj=document.getElementById("sShare");
	if(sobj.style.display=="none"){
		sobj.style.display="block";	
	}else{
		sobj.style.display="none";	
		document.getElementById("urname").value="";
		document.getElementById("femail").value="";
	}
}

function shareFriend(shareFriend){
submitData(shareFriend);
}
function submitData(sform){
	if(validateForm(sform)){
		var qs=getParam(sform);
	
		loadData("API.php","signupdiv","post",qs);
	}
}

function getXmlHttpRequestObject() {if (window.XMLHttpRequest) {return new XMLHttpRequest();} else if(window.ActiveXObject) {return new ActiveXObject("Microsoft.XMLHTTP");} else {document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.';}}
