// function to check all the checkboxes of inbox
function checkAllInbox(obj, tot){
	for(var i=1; i<tot; i++) {
		document.getElementById("inbox_"+i).checked = obj.checked;
	}
}

// function to check all the checkboxes of outbox
function checkAllOutbox(obj, tot){
	for(var i=1; i<tot; i++) {
		document.getElementById("outbox_"+i).checked = obj.checked;
	}
}

// function to delete the selected inbox mails
function deleteInbox(tot){
	var ids = new Array();
	for(var i=1; i<tot; i++) {
		var checkbox = document.getElementById("inbox_"+i);
		if (checkbox.checked){
			ids[i-1] = checkbox.value;
		}
	}
	if (ids.length > 0) {
		if (confirm('Voulez vous supprimer le message ' +ids.length+ ' de votre messagerie ?')) {
		    xmlHttp=GetXmlHttpObject();
			if (xmlHttp==null) {
		      alert ("Your browser does not support AJAX!");
		      return;
			}
			var url = document.getElementById('website_url').value + "mail.val";
			var params = "ids=" + ids + "&option=deleteInbox";
			xmlHttp.onreadystatechange = responseDeleteInbox;
			sendPostAjax(xmlHttp, url, params)
		}
	} else {
		alert("SVP, cochez les messages à supprimer.")
	}
	return false;
}

function responseDeleteInbox() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var result = xmlHttp.responseText;
			document.location.href = document.getElementById('website_url').value + 'members/';
		} else {
			alert("Error: "+ xmlHttp.statusText +" "+ xmlHttp.status);
		}
	}
}

// function to delete the selected outbox mails
function deleteOutbox(tot){
	var ids = new Array();
	for(var i=1; i<tot; i++) {
		var checkbox = document.getElementById("outbox_"+i);
		if (checkbox.checked){
			ids[i-1] = checkbox.value;
		}
	}
	if (ids.length > 0) {
		if (confirm('Voulez vous supprimer le message ' +ids.length+ ' de votre messagerie ?')) {
		    xmlHttp=GetXmlHttpObject();
			if (xmlHttp==null) {
		      alert ("Your browser does not support AJAX!");
		      return;
			}
			var url = document.getElementById('website_url').value + "mail.val";
			var params = "ids=" + ids + "&option=deleteOutbox";
			xmlHttp.onreadystatechange = responseDeleteOutbox;
			sendPostAjax(xmlHttp, url, params)
		}
	} else {
		alert("SVP, cochez les messages à supprimer.")
	}
	return false;
}

function responseDeleteOutbox() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var result = xmlHttp.responseText;
			document.location.href = document.getElementById('website_url').value + 'members/';
		} else {
			alert("Error: "+ xmlHttp.statusText +" "+ xmlHttp.status);
		}
	}
}

//function to cancel mail
function cancelMail(){
	var url = document.getElementById('website_url').value + 'members/';
	document.location.href = url;
}

//function to cancel mail
function cancelFriendMail(){
	var url = document.getElementById('website_url').value + 'members/';
	document.location.href = url;
}

//function to show the inbox mail
function showInboxMail(mailid){
	var url = document.getElementById('website_url').value + 'members/inbox/' +mailid+ '.html';
	document.location.href = url;
}

//function to show the outbox mail
function showOutboxMail(mailid){
	var url = document.getElementById('website_url').value + 'members/outbox/' +mailid+ '.html';
	document.location.href = url;
}

//function to redirect to reply url
function replyMail(mailid){
	var url = document.getElementById('website_url').value + 'members/inbox/' +mailid+ '/reply.html';
	document.location.href = url;
}

//function to send reply to the mail
function sendMailReply(mailid) {
	var subject = document.getElementById('reply_subject').value;
	var text = document.getElementById('mail_content_text').value;
	var textarea = /(<(\S+).*>)|(<\/(\S+).*>)/ ;
	if (text == "") {
		alert("Please enter some text.");
	} else if(text.match(textarea)){
		alert("No tags allowed")
	} else {
	    xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null) {
	      alert ("Your browser does not support AJAX!");
	      return;
		}
		var url = document.getElementById('website_url').value + "mail.val";
		var params = "option=replymail" + "&mailid=" + mailid + "&mailtext=" + text + "&subject=" + subject;
		xmlHttp.onreadystatechange = responseSendMailReply;
		sendPostAjax(xmlHttp, url, params);
		return false;
	}
}

function responseSendMailReply() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var result = xmlHttp.responseText;
			document.location.href = document.getElementById('website_url').value + 'members/';
		} else {
			alert("Error: "+ xmlHttp.statusText +" "+ xmlHttp.status);
		}
	}
}

//function to send mail url to a friend
//function sending mil to friend

function mailToFriend(friend_id) {
  xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	var url = document.getElementById('website_url').value + "validatinginvitefriends.val";
	var params      = 'friend_id='+friend_id;
	xmlHttp.onreadystatechange=responseFunctionmailToFriend;
	sendPostAjax(xmlHttp, url, params)
	return false;
}

function responseFunctionmailToFriend() {
	if (xmlHttp.readyState==4)
	{
	   var splitArray  = xmlHttp.responseText.split("//////");
	   if(parseInt(splitArray[0]) > 0) {
	      var url = document.getElementById('website_url').value + 'members/sendmail/'+splitArray[1]+'.html';
	      document.location.href = url;
	   }
	   else
	   {
		   alert(splitArray[2]);
	   }
	}
}

//function to send mail to a friend
function sendFriendMail(friend) {
	var subject = document.getElementById('send_friend_mail_sub').value;
	var text = document.getElementById('send_friend_mail_message').value;
	var textarea = /(<(\S+).*>)|(<\/(\S+).*>)/ ;
	if (subject == "") {
		alert("SVP, entrez un sujet");
	} else if (text == "") {
		alert("SVP, entrez un message");
	} else if(text.match(textarea)){
		alert("No tags allowed")
	} else {
	    xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null) {
	      alert ("Your browser does not support AJAX!");
	      return;
		}
		var url = document.getElementById('website_url').value + "mail.val";
		var params = "option=sendmail" + "&friend=" + friend + "&mailtext=" + text + "&subject=" + subject;
		xmlHttp.onreadystatechange = responseSendFriendMail;
		sendPostAjax(xmlHttp, url, params);
		return false;
	}
}

function responseSendFriendMail() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var result = xmlHttp.responseText;
			document.location.href = document.getElementById('website_url').value + 'members/';
		} else {
			alert("Error: "+ xmlHttp.statusText +" "+ xmlHttp.status);
		}
	}
}

function redirectMailtoAll(id) {
	document.location.href = document.getElementById('website_url').value + 'members/mailtoall/'+ id + '.html' ;
}

function sendClubMailToAll(clubid) {
	var subject = document.getElementById('club_mail_subject').value;
	var text = document.getElementById('club_mail_content_text').value;
	var textarea = /(<(\S+).*>)|(<\/(\S+).*>)/ ;
	if (text == "") {
		alert("Please enter some text.");
	} else if(text.match(textarea)){
		alert("No tags allowed")
	} else {
		document.getElementById('mailtoallsub').style.display = "none";
		document.getElementById('mailtoallsubc').style.display = "none";
	    xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null) {
	      alert ("Your browser does not support AJAX!");
	      return;
		}
		var url = document.getElementById('website_url').value + "mail.val";
		var params = "option=mailtoall" + "&clubid=" + clubid + "&mailtext=" + text + "&subject=" + subject;
		xmlHttp.onreadystatechange = responsesendClubMailToAll;
		sendPostAjax(xmlHttp, url, params);
		return false;
	}
}

function responsesendClubMailToAll() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.responseText) {
			//var id = document.getElementById('clubhidden').value;
			var id = xmlHttp.responseText;
			document.location.href = document.getElementById('website_url').value + 'members/administration.html';
		} else {
			alert("Error: "+ xmlHttp.statusText +" "+ xmlHttp.status);
		}
	}
}
