
var g_divIds=0;




function messageForm(profileId,profileName) {

	// create a message div
	var nextId=++g_divIds;
	var d=DIV(
		{'id':'mdiv_'+nextId,
		 'style':'border: 1px solid black; background-color: #B0E6B5; width: 500px; position: absolute; left: 100px; top: 100px;'}
		);
	// NOTE IE sucks bricks and won't work with DOM
	html = '<form onsubmit="return false">';
	html +=' <table cellpadding="3" cellspacing="0" width="100%">';
	html +='  <tr style="background-color: #000000; color: #ffffff; font-size: 1.2em; font-weight: bold">';
	html +='   <td align="left">Message To: '+profileName+'</td>';
	html +='   <td align="right"><a id="'+nextId+'_link" style="color: #ffffff">[close]</a></td>';
	html +='  </tr></table>';
	html +=' <table width="100%">';
	html +='  <tr></tr><td align="right">Subject:</td>';
	html +='  <td align="left"><input type="text" id="subject_'+nextId+'" size="40" /></td></tr>';
	html +='  <tr><td align="right">Message:</td>';
	html +='   <td align="left"><textarea id="msg_'+nextId+'" rows="5" cols="40"></textarea></td></tr>';
	html +='  <tr><td>&nbsp;</td><td align="left"><input type="submit" id="submit_'+nextId+'" value="Send" /></td></tr></table>';
	html += '</form>';
	d.innerHTML=html;
	//logDebug ( toHTML(d) );
	document.body.appendChild(d);

	function closeDiv() {
		disconnectAll(nextId+'_link');
		disconnectAll('submit_'+nextId);
		removeElement('mdiv_'+nextId);
	}

	var f=function(e) {
		if ( $('subject_'+nextId).value.length > 0
			||
			 $('msg_'+nextId).value.length > 0 
			) {
			if ( !confirm ( 'Discard this message?' ) )
				return;
		}
		closeDiv();
	};
	connect ( nextId+'_link', 'onclick', f );
	
	var s=function(e) {
		$('submit_'+nextId).enabled=false;
		$('submit_'+nextId).value='Sending..';
		loadPostJSONDoc (
			'/profile/messages/send_message', 
			[ 'subject', 'message', 'to' ],
			[ $('subject_'+nextId).value , $('msg_'+nextId).value, profileId ],
			true,
			function(ret) {
				flashBack(ret);
				closeDiv();
			},
			flashErrBack
			 );
	};
	connect ( 'submit_'+nextId, 'onclick', s );
	new Draggable(d);

}


