/*
    eArea - a simple web-based text editor
    Copyright (C) 2006  Oliver Moran

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

var strMailTrans = ""
var strStudID = ""
var mailForm = ""
var mailAddress = ""
var containerID = ""

function replyMail(strStudentNo)
{
  strMailTrans = 'MAILREPLY';
  strStudID = strStudentNo;
  showMailEdit();
}

function showMailEdit()
{
    mailForm = "";
	insertEditableArea("myEditArea", 440, 230,
		"[bold][italic][underlined][align-left][justify][align-center][align-right]<br />[edit-area]",
		"font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:Black;"
	);
   strMailTrans = ""
   strStudID = ""	
   mailForm = "";
}

function showContactUs()
{
    mailForm = "CONTACTUS";
	insertEditableArea("myEditArea", 540, 270,
		"[bold][italic][underlined][align-left][justify][align-center][align-right]<br />[edit-area]",
		"font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:Black;"
	);
   strMailTrans = ""
   strStudID = ""	
   mailForm = "";
}

function showReplyMail(mailID, emailAdd)
{
   mailForm = "REPLYMAIL";
   containerID = mailID;	
   mailAddress = emailAdd;
	insertEditableArea("myEditArea"+mailID, 660, 150,
		"[bold][italic][underlined][align-left][justify][align-center][align-right]<br />[edit-area]",
		"font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:Black;"
	);

}

function insertEditableArea(editableAreaName, editableAreaWidth, editableAreaHeight, editableAreaLayout, editableAreaStyle) {
	var retString;
	var boldButton = '<input type="image" src="images/icons/stock_text_bold.png" value="Bold" alt="Bold" name="' + editableAreaName + '" class="editableAreaButton" id="bold" style="width:24; height:24;">';
	var italicButton = '<input type="image" src="images/icons/stock_text_italic.png" value="Italic" alt="Italic" name="' + editableAreaName + '" class="editableAreaButton" id="italic" style="width:24; height:24;">';
	var underlinedButton = '<input type="image" src="images/icons/stock_text_underlined.png" value="Underlined" alt="Underlined" name="' + editableAreaName + '" class="editableAreaButton" id="underline" style="width:24; height:24;">';
	var alignLeftButton = '<input type="image" src="images/icons/stock_text_left.png" value="Align Left" alt="Align Left" name="' + editableAreaName + '" class="editableAreaButton" id="justifyleft" style="width:24; height:24;">';
	var justifyButton = '<input type="image" src="images/icons/stock_text_justify.png" value="Justify" alt="Justify" name="' + editableAreaName + '" class="editableAreaButton" id="justifyfull" style="width:24; height:24;">';
	var alignCenterButton = '<input type="image" src="images/icons/stock_text_center.png" value="Align Center" alt="Align Center" name="' + editableAreaName + '" class="editableAreaButton" id="justifycenter" style="width:24; height:24;">';
	var alignRightButton = '<input type="image" src="images/icons/stock_text_right.png" value="Align Right" alt="Align Right" name="' + editableAreaName + '" class="editableAreaButton" id="justifyright" style="width:24; height:24;">';
	var editableArea = '<iframe src="images/icons/blank.html?style=' + editableAreaStyle + '" id="' + editableAreaName + '" \n style="width:' + editableAreaWidth + 'px; height:' + editableAreaHeight + 'px; border-style:inset; border-width:thin;" frameborder="0px"></iframe>'

	var editableAreaHTML = editableAreaLayout;
	
	editableAreaHTML = editableAreaHTML.replace("[bold]", boldButton);
	editableAreaHTML = editableAreaHTML.replace("[italic]", italicButton);
	editableAreaHTML = editableAreaHTML.replace("[underlined]", underlinedButton);
	editableAreaHTML = editableAreaHTML.replace("[align-left]", alignLeftButton);
	editableAreaHTML = editableAreaHTML.replace("[justify]", justifyButton);
	editableAreaHTML = editableAreaHTML.replace("[align-center]", alignCenterButton);
	editableAreaHTML = editableAreaHTML.replace("[align-right]", alignRightButton);
	editableAreaHTML = editableAreaHTML.replace("[edit-area]", editableArea);
	
	if (document.designMode) 
	{
		if(mailForm == "CONTACTUS")
		{
			retString = '<table><tr><td>' + pvalue('To:')+'</td><td align="left"><input type="text" id="field1" name="field1" value="IAU Personnel" size="50" readonly>&nbsp;&nbsp;<input type="button" id="butsend" name="butsend" value=" Send " onclick="transMail(\'SENDMESSAGE\')"></td>' 
			retString = retString + '<tr><td>'+pvalue('Subject:')+'</td><td align="left"><input type="text" id="field2" name="field2" size="50"></td></tr>'
			retString = retString + '<tr><td nowrap>'+pvalue('Sender\'s Email:')+'</td><td align="left"><input type="text" id="field3" name="field3" size="50">'+pvalue('&nbsp;[Valid Address]')+'</td></tr></table><br>'
			retString = retString + editableAreaHTML;
			addtoContainer(retString);		
		}
		else if(mailForm == "REPLYMAIL")
		{
		  var messageContainer = "reply"+containerID;
		  
		  	retString = '<span><div><hr>' + pvalue('Email:')+'<input type="text" id="field1" name="field1" value="'+mailAddress+'" size="50">&nbsp;&nbsp;<input type="button" id="butsend" name="butsend" value=" Send " onclick="processCMSMail(\'CMSSENDMAIL\','+containerID+')"></td>' 
			retString = retString + '</div>'
			retString = retString + "<br>"+editableAreaHTML+'</span>' ;
		    //alert(retString);
		    addtoMailContainer(messageContainer, retString);
		  //addtoMailContainer(messageContainer, editableAreaHTML);
		}
		else
		{
			retString = '<table><tr><td>' + pvalue('To [ID #]:')+'</td><td><input type="text" id="field1" name="field1" value="'+strStudID+'" size="50">&nbsp;&nbsp;<input type="button" id="butsend" name="butsend" value=" Send " onclick="transMail(\'SENDMESSAGE\')"></td>' 
			retString = retString + '<tr><td>'+pvalue('Subject:')+'</td><td><input type="text" id="field2" name="field2" size="50"></td></tr></table><br>'
			retString = retString + editableAreaHTML;
			//mailContainer.innerHTML = retString;
			addtoContainer(retString);
		}
		ititButtons(editableAreaName);		
	}
	else 
	{
	    retString = '<textarea id="' + editableAreaName + '" style="width:' + editableAreaWidth + 'px; height:' + editableAreaHeight + 'px; ' + editableAreaStyle + '"></textarea>';
		//mailContainer.innerHTML = retString;	
		addtoContainer(retString);
	}
}

function addtoContainer(retString)
{
    var strEncloseHTML = "";
    strEncloseHTML = '<div class="containercurvy" style="width:560px; padding:5px;border:0px solid #111;">';
    strEncloseHTML = strEncloseHTML + '<div class="c hoverme" align="left" style="background:#CCCCFF;padding:10px;border:2px solid #CCFFFF;">';
	strEncloseHTML = strEncloseHTML + '<p>' + retString + '</p></div></div>';
    mainContainer.innerHTML = strEncloseHTML;
		$(function(){
		$('.c').corner("tr bl");
	});
}

function addtoMailContainer(strContainer, strHTML)
{
  var ovalue = document.getElementById(strContainer)
  //var strEncloseHTML = "";
  //alert(strContainer);
  ovalue.innerHTML = strHTML;
}

function editableAreaContents(editableAreaName) {
	if (document.designMode) {
		// Explorer reformats HTML during document.write() removing quotes on element ID names
		// so we need to address Explorer elements as window[elementID]
		if (window[editableAreaName]) return window[editableAreaName].document.body.innerHTML;
		return document.getElementById(editableAreaName).contentWindow.document.body.innerHTML;
	} else {
		// return the value from the <textarea> if document.designMode does not exist
		return document.getElementById(editableAreaName).value;
	}
}

function ititButtons(editableAreaName) {
	var kids = document.getElementsByTagName('input');

	for (var i=0; i < kids.length; i++) {
		if (kids[i].className == "editableAreaButton" && kids[i].name == editableAreaName) {
			kids[i].onmouseover = buttonMouseOver;
			kids[i].onmouseout = buttonMouseOut;
			kids[i].onmouseup = buttonMouseUp;
			kids[i].onmousedown = buttonMouseDown;
			kids[i].onclick = buttonOnClick;
		}
	}
}

function buttonMouseOver() {
	// events for mouseOver on buttons
	// e.g. this.style.xxx = xxx
}

function buttonMouseOut() {
	// events for mouseOut on buttons
	// e.g. this.style.xxx = xxx
}

function buttonMouseUp() {
	// events for mouseUp on buttons
	// e.g. this.style.xxx = xxx
}

function buttonMouseDown(e) {
	// events for mouseDown on buttons
	// e.g. this.style.xxx = xxx

	// prevent default event (i.e. don't remove focus from text area)
	var evt = e ? e : window.event; 

	if (evt.returnValue) {
		evt.returnValue = false;
	} else if (evt.preventDefault) {
		evt.preventDefault( );
	} else {
		return false;
	}
}

function buttonOnClick() {
	// Explorer reformats HTML during document.write() removing quotes on element ID names
	// so we need to address Explorer elements as window[elementID]
	if (window[this.name]) { window[this.name].document.execCommand(this.id, false, null); }
	else { document.getElementById(this.name).contentWindow.document.execCommand(this.id, false, null); }
}