var setColor = "", currShow="";
var opt= new Boolean(true);
var toggle=true;
var isIE;
if(navigator.appName.toUpperCase() == "MICROSOFT INTERNET EXPLORER")
	isIE=true;
else
	isIE=false;
	
function initializeRichEdit(){
	try{		
		var iframe = document.getElementById('edit');
		var editor = iframe.contentWindow.document;
		editor.designMode='on';
				
		edit.document.execCommand("FontSize", "", "2");
		edit.document.execCommand("FontName", "", "Arial");		
				
		editor.onkeypress = function(){
			editor_event();}
		
		editor.onclick = function(){
			removeColor();
			removePic();
		};
	}
	catch(e)
	{
		return;
	}
	//BuildEmotionPanel(LayObjPic,"addPicture");
	//BuildColorPanel(LayObjColor,"setFontColor");

	MSN_Image();
}

function editor_event() {
  var editor_obj  = document.getElementById('edit');       // html editor object
  var editEvent = editor_obj.contentWindow ? editor_obj.contentWindow.event : event;

  // catch keypress events
    if (editEvent && editEvent.keyCode) {
      var ord       = editEvent.keyCode;    // ascii order of key pressed
      var ctrlKey   = editEvent.ctrlKey;
      var altKey    = editEvent.altKey;
      var shiftKey  = editEvent.shiftKey;

	  if (ord == 16) { return; }  // ignore shift key by itself
      if (ord == 17) { return; }  // ignore ctrl key by itself
      if (ord == 18) { return; }  // ignore alt key by itself
	
       // cancel ENTER key and insert <BR> instead
       if (ord == 13 && editEvent.type == 'keypress') {
	   		editor_insertHTML("<BR>\n");
	   		editEvent.returnValue = false;
        	return;
       }
	}
}

function editor_insertHTML(str){
 	var editor_obj  = document.getElementById('edit');  
	var editdoc = editor_obj.contentWindow.document;
    sRange  = editdoc.selection.createRange();
	sRange.pasteHTML(str);
	
	sRange.select();
	sRange.moveEnd("character", -1);
	sRange.moveStart("character", 1);
	sRange.collapse(false);
	
}

function initCommand(cmdType,value){
	removeColor();
	removePic();
	var frame = document.getElementById('edit').contentWindow;
	var editor = frame.document;
	if(cmdType=="CreateLink"){
		editor.execCommand(cmdType, true, '');
	}else if(cmdType=="BackColor"){
		editor.execCommand(cmdType, false, value);
	}else if(cmdType=="FontName"){
		editor.execCommand(cmdType, false, value);
	}else{
		editor.execCommand(cmdType, false, value);
	}
	frame.focus();
}

function setFontColor(sColor){
	initCommand(setColor,sColor);
	if(isIE)
		initCommand(setColor,sColor);
	removeColor();
}

function showColor(setclr){
	if(currShow!=""){
		if(currShow=="showColor"){
			if(setColor!=setclr)
				removeColor();
		}else{
			removePic();
		}
	}
	setColor=setclr;
	if(opt==true){
		opt = false;
		currShow = "showColor";
		elm = document.getElementById("LayObjColor");
		var relm = document.getElementById(setclr=="ForeColor"? "btnTextColor" : "btnColorHighlight");
		if(isIE){
			elm.style.left = (findPosX(relm) + 10) + 'px';
			elm.style.top = (findPosY(relm) + 29) + 'px';
		}else{
			elm.style.left = findPosX(relm) + 'px';
			elm.style.top = (findPosY(relm) + 19) + 'px';
		}
		elm.style.display = "block";
	}else{
		removeColor();
	}
}

function removeColor(){
	opt = true;
	currShow = "";
	elm = document.getElementById("LayObjColor");
	elm.style.display = "none";
}

function showPic(){
var elm = document.getElementById('LayObjPic');
	if(currShow=="showColor") removeColor();
	if(toggle==true){
		toggle=false;
		currShow = "showPicture";
		var relm = document.getElementById("btnInsertPicture");
		if(isIE){
			elm.style.left = (findPosX(relm) + 7) + 'px';
			elm.style.top = (findPosY(relm) + 31) + 'px';
		}else{
			elm.style.left = (findPosX(relm) - 273) + 'px';
			elm.style.top = (findPosY(relm) + 21) + 'px';
		}
		elm.style.display = "block";
	}else{
		removePic();
	}
}

function removePic(){
	toggle = true;
	currShow = "";
	elm = document.getElementById("LayObjPic");
	elm.style.display = "none";
}

function addPicture(src){
	initCommand('InsertImage',src);
	if(isIE) initCommand('InsertImage',src);
	removePic();
}

function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}



function BuildEmotionPanel(div,funcname)
{
	var temp_html = '';
	var emotionURL = '';
	var emotions = new Array("emsmile.gif","emteeth.gif","emwink.gif","emsmileo.gif","emsmilep.gif","emsmiled.gif","emangry.gif","emembarrassed.gif",
				"emcrook.gif","emsad.gif","emcry.gif","emdgust.gif","emangel.gif","emlove.gif","emunlove.gif","emmessag.gif",
				"emcat.gif","emdog.gif","emmoon.gif","emstar.gif","emfilm.gif","emnote.gif","emrose.gif","emrosesad.gif",
				"emclock.gif","emlips.gif","emgift.gif","emcake.gif","emphoto.gif","emidea.gif","emtea.gif","emphone.gif",
				"emhug.gif","emhug2.gif","embeer.gif","emcocktl.gif","emmale.gif","emfemale.gif","emthup.gif","emthdown.gif");				
	var temp_emotiontitle;
	var total = emotions.length;
	var width = 8;

	temp_html += "<table cellpadding=3 bgcolor=#A5B6DE cellspacing=1 border=0 bordercolor=#666666 style='cursor: hand;font-size: 3px;'>";	
	for (var i=0; i<total; i++) {
		if ((i % width) == 0) 
			temp_html += "<tr bgcolor=#ffffff>"; 
			temp_emotiontitle = emotions[i].replace("em","").replace(".gif","");
		temp_html += '<td onClick='+funcname+'("images/emotions/'+emotions[i]+'") onmouseover="this.style.backgroundColor=\'#F1EEE7\';" onmouseout="this.style.backgroundColor=\'\'"><IMG  height="19" src="images/emotions/'+emotions[i]+'" title='+temp_emotiontitle+' width="19" unselectable=on></td>';
		if ( ((i+1)>=total) || (((i+1) % width) == 0)) { 
			temp_html += "</tr>";
		}
	}				
	temp_html += "</table>";
	
	div.innerHTML=temp_html;

}


function BuildColorPanel(div,funcname)
{
	var temp_html = '';
	var colors = new Array("#000000","#993300","#333300","#003300","#003366","#000080","#333399","#333333",
		"#800000","#FF6600","#808000","#008000","#008080","#0000FF","#666699","#808080",
		"#FF0000","#FF9900","#99CC00","#339966","#33CCCC","#3366FF","#800080","#999999",
		"#FF00FF","#FFCC00","#FFFF00","#00FF00","#00FFFF","#00CCFF","#993366","#C0C0C0",
		"#FF99CC","#FFCC99","#FFFF99","#CCFFCC","#CCFFFF","#99CCFF","#CC99FF","#FFFFFF");				
	var total = colors.length;
	var width = 8;

	temp_html += "<table cellpadding=1 bgcolor=#F9F8F7 cellspacing=5 border=1 bordercolor=#96A9C9 style='cursor: hand;font-size: 3px;'>";	
	for (var i=0; i<total; i++) {
		if ((i % width) == 0) 
			temp_html += "<tr>"; 
		temp_html += '<td bgcolor="'+colors[i]+'" height=5 width=12 onClick='+funcname+'("'+colors[i]+'") >&nbsp;</td>';
		if ( ((i+1)>=total) || (((i+1) % width) == 0)) { 
			temp_html += "</tr>";
		}
	}				
	temp_html += "</table>";
	
	div.innerHTML=temp_html;

}


/****************************************************************\
Function    : MSN_Image (call onload page)
Description : Compare text while typing and create msn image
//the data map the text to the image index
//http://messenger.msn.com/resource/Emoticons.aspx
\****************************************************************/
function MSN_Image(){
	var msnimagesarr=[
		{i:1,t:":)"},
		{i:1,t:":-)"},
		{i:2,t:":D"},
		{i:2,t:":-D"},
		{i:3,t:":O"},
		{i:3,t:":-O"},
		{i:4,t:":P"},
		{i:4,t:":-P"},
		{i:5,t:";-)"},
		{i:5,t:";)"},
		{i:6,t:":-("},
		{i:6,t:":("},
		{i:7,t:":S"},
		{i:7,t:":-S"},
		{i:8,t:":|"},
		{i:8,t:":-|"},
		{i:9,t:":'("},
		{i:10,t:":$"},
		{i:10,t:":-$"},
		{i:11,t:"(H)"},
		{i:12,t:":-@"},
		{i:12,t:":@"},
		{i:13,t:"(A)"},
		{i:14,t:"(6)"},
		{i:15,t:":-#"},
		{i:16,t:"8O|"},
		{i:17,t:"8-|"},
		{i:18,t:"^O)"},
		{i:19,t:":-*"},
		{i:20,t:"+O("},
		{i:21,t:":^)"},
		{i:22,t:"*-)"},
		{i:23,t:"<;-P"},
		{i:24,t:"8-)"},
		{i:25,t:"|-)"},
		//others
		{i:26,t:"(C)"},
		{i:27,t:"(Y)"},
		{i:28,t:"(N)"},
		{i:29,t:"(B)"},
		{i:30,t:"(D)"},
		{i:31,t:"(X)"},
		{i:32,t:"(Z)"},
		{i:33,t:"({)"},
		{i:34,t:"(})"},
		{i:35,t:":-["},
		{i:35,t:":["},
		{i:36,t:"(^)"},
		{i:37,t:"(L)"},
		{i:38,t:"(U)"},
		{i:39,t:"(K)"},
		{i:40,t:"(G)"},
		{i:41,t:"(F)"},
		{i:42,t:"(W)"},
		{i:43,t:"(P)"},
		{i:44,t:"(~)"},
		{i:45,t:"(@)"},
		{i:46,t:"(&)"},
		{i:47,t:"(T)"},
		{i:48,t:"(I)"},
		{i:49,t:"(8)"},
		{i:50,t:"(S)"},
		{i:51,t:"(*)"},
		{i:52,t:"(E)"},
		{i:53,t:"(O)"},
		{i:54,t:"(M)"},
		{i:55,t:"(SN)"},
		{i:56,t:"(BAH)"},
		{i:57,t:"(PL)"},
		{i:58,t:"(||)"},
		{i:59,t:"(PI)"},
		{i:60,t:"(SO)"},
		{i:61,t:"(AU)"},
		{i:62,t:"(AP)"},
		{i:63,t:"(UM)"},
		{i:64,t:"(IP)"},
		{i:65,t:"(CO)"},
		{i:66,t:"(MP)"},
		{i:67,t:"(ST)"},
		{i:68,t:"(LI)"},
		{i:69,t:"(MO)"},
	
		];
	if(document.namespaces)//ie6
	{
		//attach the event for MSN Images of IE6
		editor.attachEvent("onkeyup",function testformsnimages(){
			if(document.selection.type=='None')
			{
				var range=document.selection.createRange();
				
				for(var l=2;l<6;l++)
				{
					var sel=range.duplicate();
					sel.move('character',-l);
					sel.moveEnd('character',l);
					var txt=sel.text.toUpperCase();
					
					for(var n=0;n<msnimagesarr.length;n++)
					{
						var o=msnimagesarr[n];
						if(o==null||o.t.length!=l)continue;
						if(txt==o.t)
						{
							sel.select();
							sel.pasteHTML("<img width='19' height='19' src='images/msn/"+o.i+".gif'/>");
							return;
						}
					}
				}
			}
		});
	}
}

function moreEmoticons(){
	var msnimagesarr = new Array(":-) or :)",":-D or :d",":-O or :o",":-P or :p",";-) or ;)",
								 ":-( or :(",":-S or :s",":-| or :|",":'(",":-$ or :$",
								 "(H) or (h)",":-@ or :@","(A) or (a)","(6)",":-#",
								 "8o|","8-|","^o)",":-*","+o(",
								 ":^)","*-)","&lt;;-P","8-)","|-)",
								 "(C) or (c)","(Y) or (y)","(N) or (n)","(B) or (b)","(D) or (d)",
								 "(X) or (x)","(Z) or (z)","({)","(})",":-[ or :[",
								 "(^)","(L) or (l)","(U) or (u)","(K) or (k)","(G) or (g)",
								 "(F) or (f)","(W) or (w)","(P) or (p)","(~)","(@)",
								 "(&)","(T) or (t)","(I) or (i)","(8)","(S) or (s)",
								 "(*)","(E) or (e)","(O) or (o)","(M) or (m)","(SN) or (sn)",
								 "(BAH) or (bah)","(PL) or (pl)","(||)","(PI) or (pi)","(SO) or (so)",
								 "(AU) or (au)","(AP) or (ap)","(UM) or (um)","(IP) or (ip)","(CO) or (co)",
	 							 "(MP) or (mp)","(ST) or (st)","(LI) or (li)","(MO) or (mo)");				
	var msndescription = new Array("Smile","Open-mouthed","Surprised","Tongue out","Wink",
								   "Sad","Confused","Disappointed","Crying","Embarrassed",
								   "Hot","Angry","Angel","Devil","Don't tell anyone",
								   "Baring teeth","Nerd","Sarcastic","Secret telling","Sick",
								   "I don't know","Thinking","Party","Eye-rolling","Sleepy",
								   "Coffee cup","Thumbs up","Thumbs down","Beer mug","Martini glass",
								   "Girl","Boy","Left hug","Right hug","Vampire bat",
								   "Birthday cake","Red heart","Broken heart","Red lips","Gift with a bow",
								   "Red rose","Wilted rose","Camera","Filmstrip","Cat face",
								   "Dog face","Telephone receiver","Light bulb","Note","Sleeping half-moon",
								   "Star","E-mail","Clock","MSN Messenger icon","Snail",
								   "Black Sheep","Plate","Bowl","Pizza","Soccer ball",
								   "Auto","Airplane","Umbrella","Island with a palm tree","Computer",
								   "Mobile Phone","Stormy cloud","Lightning","Money");
	var total = msnimagesarr.length;
	var temp_html = '';
	var j = 0;
	temp_html += "<table cellpadding=3 cellspacing=0 width=600>";
	temp_html += "<tr class='txtbodybold'><td colspan=2>To send this:</td><td colspan=3>Type one of the following:</td></tr>"
	for (var i=0; i<total; i++) {
		if ((i % 2) == 0) 
			temp_html += "<tr>"; 
		j = i+1;
		temp_html += "<td width=25><img src='images/msn/"+j+".gif'/></td><td class='txtbody'>"+msndescription[i]+"</td><td class='txtbodybold'><nobr>"+msnimagesarr[i].replace(" or "," <span class='txtbody'>or</span> ")+"</nobr></td><td width=20></td>"
		if ( ((i+1)>=total) || (((i+1) % 2) == 0)) { 
			temp_html += "</tr>";
		}		
	}				
	temp_html += "</table>";
	document.write(temp_html);
}

