/* The variable http will hold our new XMLHttpRequest object. */
var httpObj = createRequestObject();

/*Holds a fields id when we are doing a callback */
var id=-1;

function friendRequest(){	
	enableButtons(false);
	
	var params = "action=friendrequest";
    	params    += "&message=" + document.getElementById('friendrequesttext').value;
                     
	/*httpObj.abort; NOT IE6 Compatible */
	httpObj.open('post',  'actions.inc.php');
	httpObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
    	httpObj.setRequestHeader("Content-length", params.length);    
	httpObj.onreadystatechange = updateFriendRequest;
    	httpObj.send(params);
	return true;
}

function updateFriendRequest(){		
	if(httpObj.readyState == 4){				
		if(httpObj.responseText == "1") {
			document.getElementById('friendrequestsend').innerHTML = "<p><b>Your friend request has been sent!</b></p>";
		} else {
			document.getElementById("friendrequesterror").innerHTML = "<p class=\"error\">Could not perform this action, please try again later</p>";
			document.getElementById("friendrequesterror").style.display='block';					
		}
	}
	
	enableButtons(true);
}

function shoutboxPostMessage(){	
	enableButtons(false);
	
	var params = "action=shoutboxpostmessage";
    params    += "&message=" + document.getElementById('postmessage').value;
    params    += "&sender=" + document.getElementById('sender').value;
    params    += "&recipient=" + document.getElementById('recipient').value;
                 
	/*httpObj.abort;*/
	httpObj.open('post',  'actions.inc.php');
	httpObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
    httpObj.setRequestHeader("Content-length", params.length);    
	httpObj.onreadystatechange = updateShoutboxPostMessage;	
    httpObj.send(params);
	return true;
}

function updateShoutboxPostMessage(){		
	if(httpObj.readyState == 4){					
		document.getElementById('shoutboxnewcommenttext').innerHTML = httpObj.responseText;										
		document.getElementById('shoutboxnewcomment').style.display = "block";
		document.getElementById('postmessage').value = "";


	/*document.getElementById('shoutboxnewavatar').style.display = "block";		*/
	if(document.getElementById('shoutboxnewavatar')) {
			$('#shoutboxnewavatar').fadeIn("slow");
	}
			
		if(document.getElementById("postmessagediv")) {
			document.getElementById("postmessagediv").innerHTML = "<p>Your message has been sent!</p>";			
			document.getElementById("postmessagebutton").disabled = true;
		}
		
		/*enableButtons(false);*/
	}
}

function deleteShoutBoxMessage(messageid, userid){			
	enableButtons(false);
	
	id=messageid; //set global var for callback	
	var params = "action=shoutboxdeletemessage";
	params    += "&messageid=" + messageid;
	params    += "&userid=" + userid;
	/*httpObj.abort;*/
	httpObj.open('post',  'actions.inc.php');
	httpObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
    httpObj.setRequestHeader("Content-length", params.length);    
	httpObj.onreadystatechange = updateDeleteShoutBoxMessage;	
    httpObj.send(params);
	return true;
}

function updateDeleteShoutBoxMessage(){		
	if(httpObj.readyState == 4){		
		if(httpObj.responseText == "1") {
			document.getElementById(id).style.display='none';			
		} else {
			document.getElementById("shoutboxerror").innerHTML = "<p class=\"error\">Could not perform this action, please try again later</p>";
			document.getElementById("shoutboxerror").style.display='block';				
		}
		
		enableButtons(true);
	}
}