var selector;

function msgContact(id, name) {
	document.getElementById('to').value = id;
	document.getElementById('toTxt').innerHTML = '<i>'+name+'</i>';
	if(document.getElementById('cntmsg').style.display == 'none') {
		expand('cntmsg','block');
	}
	clearPageStatus();
}

function cancelMsg() {
	document.getElementById('subject').value = '';
	document.getElementById('message').value = '';
	expand('cntmsg','block');
}

function sendMsg() {
	var id = encodeURI(document.getElementById('to').value);
	var subject = encodeURI(document.getElementById('subject').value);
	var message = encodeURI(document.getElementById('message').value);
	var parameters = 'cmd=send&message='+message+"&to="+id+"&subject="+subject;
	AJAXRequest("POST","/msg.php",parameters,sendMsgHandler,true);
}

function sendMsgHandler(xmlObject) {
	if(xmlObject.responseXML) {
		if(xmlObject.responseXML.getElementsByTagName('status')) {
			expand('cntmsg','block');
			document.getElementById('page_status').innerHTML = xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue;
		}
	}
}

function delContact(id){
	selector = document.getElementById('contact_'+id);
	selector.style.background='#FFC0CB';
	if(confirm('Do you really want to delete this contact?')) {
		var parameters = 'cmd=del_friend&id='+id;
		AJAXRequest("POST","/uedit.php",parameters,delContactHandler,true);
	} else {
		selector.style.background='#fff';
	}
}

function delContactHandler(xmlObject) {
	if(xmlObject.responseXML.getElementsByTagName('status')) {
		var status = xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue;
		if(status == '1') {
			selector.style.display='none';
		} else {
			alert(status);
			selector.style.background='#fff';
		}
	}
}

function getFriends(page) {
	AJAXRequest("POST","/friends.php","cmd=list&page="+page,getFriendsHandler,true);
}

function getFriendsHandler(xmlObject) {
	var display = document.getElementById('friends');
	var html='<table border="0" width="100%" cellspacing="0" style="border: 0px; font-size: x-small">';
	var id = 0;
	var name = '';
	if(xmlObject.responseXML) {
		if(xmlObject.responseXML.getElementsByTagName('contact')) {
			var contacts = xmlObject.responseXML.getElementsByTagName('contact');
			for(var i=0; i<contacts.length; i++) {
				id = contacts[i].getElementsByTagName('id')[0].firstChild.nodeValue;
				name = contacts[i].getElementsByTagName('name')[0].firstChild.nodeValue;
				if(contacts[i].getElementsByTagName('avatar')[0].firstChild.nodeValue==1) {
					avatar = 'users/'+id+'.jpg';
				} else {
					avatar = 'homme.png';
				}
				html += '<tr id="contact_'+id+'"><td><a href="/users/'+id+'"><img style="border: 0; max-width: 45px; max-height: 45px;" src="images/'+avatar+'" /></a></td><td valign="center"><a href="/users/'+id+'">'+name+'</a></td><td>[ <a onClick="delContact(\''+id+'\')">delete</a> ]</td><td>[ <a onClick="msgContact(\''+id+'\',\''+name+'\')">send message</a> ]</td></tr>';
			}
			html += '</table>';
			html += xmlObject.responseXML.getElementsByTagName('pages')[0].firstChild.nodeValue;
			display.innerHTML = html;
		} else {
			if(xmlObject.responseXML.getElementsByTagName('status')) {
				display.innerHTML = xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue;
			}
		}
	}
}

function getFriendsRequests(page) {
	AJAXRequest("POST","/friends.php","cmd=listrequests&page="+page,getFriendsRequestsHandler,true);
}

function getFriendsRequestsHandler(xmlObject) {
	var display = document.getElementById('friends_requests');
	var html='<table border="0" width="100%" cellspacing="0" style="border: 0px; font-size: x-small">';
	var id = 0;
	var name = '';
	if(xmlObject.responseXML) {
		if(xmlObject.responseXML.getElementsByTagName('contact')) {
			var contacts = xmlObject.responseXML.getElementsByTagName('contact');
			for(var i=0; i<contacts.length; i++) {
				id = contacts[i].getElementsByTagName('id')[0].firstChild.nodeValue;
				name = contacts[i].getElementsByTagName('name')[0].firstChild.nodeValue;
				if(contacts[i].getElementsByTagName('avatar')[0].firstChild.nodeValue==1) {
					avatar = 'users/'+id+'.jpg';
				} else {
					avatar = 'homme.png';
				}
				html += '<tr id="freq_'+id+'"><td><a href="/users/'+id+'"><img style="border: 0; max-width: 45px; max-height: 45px;" src="images/'+avatar+'" /></a></td><td valign="center"><a href="/users/'+id+'">'+name+'</a></td><td><a href="#" onClick="friendsRequestApprouve(\''+id+'\')"><img src="/images/online.png" border="0" /> accept</a></td><td><a onClick="friendsRequestReject(\''+id+'\')" href="#"><img src="/images/offline.png" border="0" />  reject</a></td></tr>';
			}
			html += '</table>';
			html += xmlObject.responseXML.getElementsByTagName('pages')[0].firstChild.nodeValue;
			display.innerHTML = html;
		} else {
			if(xmlObject.responseXML.getElementsByTagName('status')) {
				display.innerHTML = xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue;
			}
		}
	}
}

function friendsRequest(id) {
	AJAXRequest("POST","/friends.php","cmd=friendsrequest&fid="+id,friendsRequestHandler,true);
}

function friendsRequestHandler(xmlObject) {
	if(xmlObject.responseXML.getElementsByTagName('status')) {
		switch(xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue) {
			case '-1':
				document.getElementById('page_status').innerHTML = '<span style="color: #900">A database error occured.</span>';
				break;
			case '0':
				document.getElementById('page_status').innerHTML = '<span style="color: #900">You are already friend with this user or a friendship request is pending.</span>';
				break;
			case '1':
				document.getElementById('page_status').innerHTML = '<span style="color: #069">Friendship requested.</span>';
				break;
			case '2':
				document.getElementById('page_status').innerHTML = '<span style="color: #900">You cannot request friendship to yourself.</span>';
				break;
		}
	}
}

function friendsRequestApprouve(id) {
	AJAXRequest("POST","/friends.php","cmd=friendsapprouve&fid="+id,friendsRequestApprouveHandler,true);
}

function friendsRequestApprouveHandler(xmlObject) {
	if(xmlObject.responseXML.getElementsByTagName('status')) {
		switch(xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue) {
			case '-1':
				document.getElementById('page_status').innerHTML = '<span style="color: #900">A database error occured.</span>';
				break;
			case '0':
				document.getElementById('page_status').innerHTML = '<span style="color: #900">No such request.</span>';
				break;
			case '1':
				document.getElementById('page_status').innerHTML = '<span style="color: #069">Friendship approuved.</span>';
				if(document.getElementById('friends')) {
					getFriends(0);
				}
				if(document.getElementById('friends_requests')) {
					document.getElementById('freq_'+xmlObject.responseXML.getElementsByTagName('fid')[0].firstChild.nodeValue).style.display = "none";
				}			
				break;
		}
	}
}

function friendsRequestReject(id) {
	AJAXRequest("POST","/friends.php","cmd=friendsreject&fid="+id,friendsRequestRejectHandler,true);
}

function friendsRequestRejectHandler(xmlObject) {
	if(xmlObject.responseXML.getElementsByTagName('status')) {
		switch(xmlObject.responseXML.getElementsByTagName('status')[0].firstChild.nodeValue) {
			case '-1':
				document.getElementById('page_status').innerHTML = '<span style="color: #900">A database error occured.</span>';
				break;
			case '0':
				document.getElementById('page_status').innerHTML = '<span style="color: #900">No such request.</span>';
				break;
			case '1':
				document.getElementById('page_status').innerHTML = '<span style="color: #069">Friendship rejected.</span>';
				if(document.getElementById('friends_requests')) {
					document.getElementById('freq_'+xmlObject.responseXML.getElementsByTagName('fid')[0].firstChild.nodeValue).style.display = "none";
				}				
				break;
		}
	}
}