var allow = true;
var current_msg = new String;

function populateMsgs() {
	if ($("messages")) {
		var echo = "<ul class='msg'>";
		for (i=0; i<msg_ar.length; i++) {
			title = (msg_ar[i][0] == "") ? "..." : msg_ar[i][0];
			if (msg_ar[i][4]==1) {
				echo += "<li><span onclick='popMsg("+i+");'>"+title+"</span></li>";
			} else {
				echo += "<li><span class='read' onclick='popMsg("+i+");'>"+title+"</span></li>";
			}
		}
		echo += "</ul>";
		$("messages").innerHTML = echo;
	}
}

function popMsg(x) {
	if ($('msg')) {
		removeObject('msg');
	}
	create_msg();
	if (allow == true) {
		current_msg = x;
		ajaxGet(msg_ar[x][1]);
		//title.innerHTML = msg_ar[x][0]+" - <a href='manager.php?id="+msg_ar[x][2]+"'>"+msg_ar[x][3]+"</a>";
		showElement("fundiv");
		showElement("msg");
		$("msgreply").innerHTML = "";
		$("msgtxt").innerHTML = "<img src='pics/msgsave.gif' />";
		rePos();
		$("echo").style.visibility = "hidden";
		$("fundiv").style.width = "100%";
		$("fundiv").style.height = "100%";
		try	{ clearTimeout(ec); } catch(err) {};
		try	{ clearTimeout(ecc); } catch(err) {};
	} else {
		alert("Sending Message");
	}
}
function closeMsg(x) {
	if (current_msg != 'none') {
		if (x == "delete") {
			ajaxSet(msg_ar[current_msg][1],4);
			msg_ar.splice(current_msg,1); 
		} else if (x == "unread") {
			ajaxSet(msg_ar[current_msg][1],1);
			msg_ar[current_msg][4] = 1;
		} else {
			ajaxSet(msg_ar[current_msg][1],2);
			msg_ar[current_msg][4] = 2;
		}
	}
	//
	hideElement("fundiv");
	hideElement("msg");
	hideElement("echo");
	try	{ $("msgreply").innerHTML = ""; } catch(err) {};
	populateMsgs();
}
function reply() {
	$("msgreply").innerHTML = "<input id='subject' class='subject' type='text' value='"+msg_ar[current_msg][0]+"' /><textarea id='txt' class='msgreply'></textarea><ul id='msgbuttons_list'><li><a href='javascript:sendMsg();'>Send</a></li><li><a href='javascript:closeMsg();'>Discard</a></li></ul>";
	rePos();
}
function sendMsg() {
	allow = false;
	//
	if ($("to")) {
		if ($("to").value == "") {
			alert("You need to specify a receiver")
		} else if ($("subject").value == "") {
			alert("You need to write subject")
		} else {
			ajaxSend($("to").value, $("subject").value, $("txt").value);
		}
	} else {
		ajaxSend(msg_ar[current_msg][6], $("subject").value, $("txt").value);
	}
}
function closeMsgSent() {
	hideElement("fundiv");
	hideElement("echo");	
}	
function msgSent(x) {
	if (x!="error") {
		$("echo").innerHTML = "Message Sent! <img src='pics/good.gif' />";
	} else {
		$("echo").innerHTML = "An Error Occurred!";
	}
	var ecc = window.setTimeout('closeMsgSent()',5000);
	allow = true;
}
var xmlHttp;
function ajaxGet(id) { 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url="ajax_getmail.php?id="+id;
	xmlHttp.onreadystatechange=msgGotten;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function msgGotten() {
	if (xmlHttp.readyState==4) { 
		if (xmlHttp.responseText != "error") {
			$("msgfrom").innerHTML = "<strong><span>"+msg_ar[current_msg][0]+"</span>From: "+msg_ar[current_msg][7]+"</strong>";
			$("msgtxt").innerHTML = xmlHttp.responseText;
		}
	}
}
function ajaxSet(id,s) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url="ajax_setmail.php?id="+id;
	url=url+"&status="+s;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function ajaxSend(id, s, t) {
	closeMsg();
	$("fundiv").style.width = "100%";
	$("fundiv").style.height = "2em";
	$("echo").innerHTML = "Sending Message... <img src='pics/msgsave.gif' />";
	showElement("fundiv");
	showElement("echo");
	//
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url="ajax_sendmail.php";
	s = encodeURIComponent(s);
	t = encodeURIComponent(t);
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send("id="+id+"&s="+s+"&t="+t);
}
function stateChanged() {
	if (xmlHttp.readyState==4) { 
		if (xmlHttp.responseText == "success") {
			msgSent();
		} else {
			msgSent("error");
		}
	}
}
function rePos() {
	if($('msg')) {
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth ||document.documentElement.clientHeight ) ) {
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		var msg = $("msg");
		var top = myHeight/2-(msg.offsetHeight/2);
		top = (top < 5) ? 5 : top;
		var left = myWidth/2-300;
		left = (left < 5) ? 5 : left;
		var xy = getScrollXY();
		placeElement("msg",left,(top+xy[1]));
		placeElement("fundiv",0,0);
		placeElement("echo",0,0);
	}
}
function start_msg(x, y) {
	current_msg = 'none';
	x = x || '';
	if (allow == true) {
		if ($('msg')) {
			removeObject('msg');
		}
		create_fundiv();
		$("fundiv").onclick = function () {
			closeMsg();
		}
		if (!$('echo')) {
			newDiv = document.createElement("div");
			newDiv.id = "echo";
			document.body.appendChild(newDiv);
		}
		newDiv = document.createElement("div");
		newDiv.id = "msg";
		div_content =  "<div id='msgcontent'>";
		div_content += "	<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
		div_content += "		<tr><td class='right bold' width='10px'>Receiver: </td><td><input id='to' onkeyup='getHeadCoachName(this.value)' class='msg_input' type='text' value='"+x+"' /></td><td style='text-align:left;width:200px;overflow:hidden;' id='reciever'></td></tr>";
		if (y) {
			div_content += "		<tr><td class='right bold'>Subject: </td><td><input id='subject' class='msg_input' style='margin-top:6px' type='text' value='"+y+"' /></td></tr>";
		} else {
			div_content += "		<tr><td class='right bold'>Subject: </td><td><input id='subject' class='msg_input' style='margin-top:6px' type='text' value='' /></td></tr>";
		}
		div_content += "	</table>";
		div_content += "	<textarea id='txt' class='msgreply'></textarea>";
		div_content += "	<ul id='msgbuttons_list'>";
		div_content += "		<li><a href='javascript:sendMsg(true);'>Send</a></li>";
		div_content += "		<li><a href='javascript:closeMsg();'>Discard</a></li>";
		div_content += "	</ul>";
		div_content += "</div>";
		newDiv.innerHTML = div_content;
		document.body.appendChild(newDiv);
		showElement("fundiv");
		showElement("msg");
		rePos();
		
	} else {
		alert("Sending Message");
	}
}
function getHeadCoachName(val) {
	if (val != "") {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	xmlHttp.onreadystatechange=nameStateChanged;
	xmlHttp.open("POST","ajax_get_hc_name.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send("id="+val);
	}
}
function nameStateChanged() {
	if (xmlHttp.readyState==4) { 
		if (xmlHttp.responseText != "error") {
			$("reciever").innerHTML = "<span style='color:#ccff00;display:inline;'>&raquo;</span> "+xmlHttp.responseText;
		} else {
			$("reciever").innerHTML = "<span style='color:#ff9900;display:inline;'>&raquo;</span> No Matching Manager";
		}
	}
}

function create_msg() {
	create_fundiv();
	$("fundiv").onclick = function () {
		closeMsg();
	}
	
	if (!$('echo')) {
		newDiv = document.createElement("div");
		newDiv.id = "echo";
		document.body.appendChild(newDiv);
	}
	
	if (!$('msg')) {
		newDiv = document.createElement("div");
		newDiv.id = "msg";
		div_content = '<div id="msgcontent"><div id="title"></div>';
		div_content += '<div id="msgfrom"></div>';
		div_content += '<div id="msgtxt"></div>';
		div_content += '	<ul id="msgbuttons_list">';
		div_content += '		<li><a href="javascript:reply();">Reply</a></li>';
		div_content += '		<li><a href="javascript:closeMsg(\'delete\');">Delete</a></li>';
		div_content += '		<li><a href="javascript:closeMsg(\'unread\');">Mark as Unread</a></li>';
		div_content += '		<li><a href="javascript:closeMsg();">Close</a></li>';
		div_content += '	</ul>';
		div_content += '	<div id="msgreply"></div>';
		div_content += '</div>';
		newDiv.innerHTML = div_content;	
		document.body.appendChild(newDiv);
	}
}