// use general one, assume its already loaded
isIE4 = document.all? true : false;
isIE6 = document.getElementById && document.all ? true : false;
isNS4 = document.layers? true : false;
isNS6 = document.getElementById && !document.all ? true : false;
function _getFPObject (documentID){
	if (isIE4){return document.all[documentID];
	}else if(isIE6){return document.getElementById(documentID);
	}else if (isNS4){return document.layers[documentID];
	}else if (isNS6){return document.getElementById(documentID);
	}
}
function getFPObject (dId){
   var obj=null;
   try
   {
	   if (isIE4){obj= document.all[dId];
	   }else if(isIE6){obj= document.getElementById(dId);
	   }else if (isNS4){obj= document.layers[dId];
	   }else if (isNS6){obj= document.getElementById(dId);
	   }
   }
   catch(e)
   {
      obj=null;
   }
   return obj;
}

var FONT_CONTAINER_PREFIX = "FONT_CONTAINER";
var FONT_DISPLAY_PREFIX = "FONT_DISPLAY";
var FONT_STATUSDISPLAY_PREFIX = "FONT_STATUSDISPLAY";
var FONT_FONTFACEDROPDOWN_PREFIX = "FONT_FONTFACEDROPDOWN";
var FONT_FONTSIZEDROPDOWN_PREFIX = "FONT_FONTSIZEDROPDOWN";
var FONT_FONTCOLOURDROPDOWN_PREFIX = "FONT_FONTCOLOURDROPDOWN";

//  31-May-06 13:39 HvdB 
var FONT_FONTFACE_PREFIX = "FONT_FONTFACE";
var FONT_FONTSIZE_PREFIX = "FONT_FONTSIZE";
var FONT_FONTBOLD_PREFIX = "FONT_FONTBOLD";
var FONT_FONTITALIC_PREFIX = "FONT_FONTITALIC";
var FONT_FONTUNDERLINE_PREFIX = "FONT_FONTUNDERLINE";
var FONT_FONTJUSTL_PREFIX = "FONT_FONTJUST0";
var FONT_FONTJUSTC_PREFIX = "FONT_FONTJUST1";
var FONT_FONTJUSTR_PREFIX = "FONT_FONTJUST2";
var fontPickerApplyToID = null;  //control whose text is to be formatted based on fontpicker info
var fontPickerStyleToID = null;  //control where style info is to be saved

var FontPicker_idGenerator = new IDGenerator(0);
var fontPickerMap = new Object();
var fontPickerIDGenerator = null;
var fontPickerID = 0;            //assume only one color picker active at a time
var fontPickerColorRgb = [
['000000','ffffff','008BDC','62911B','1C2852','0073B5','295644','EE1313','912B2D'],
['ffff00','919191','407374','5C1C66','FC7B07','513324','F3DA9F','EDE71F']
];
var fontPickerColorName = [
['Black/402','White/401','lightBlue/403','Green/403','navyBlue/405','royalBlue/406','forestGreen/407','red/408','bordeaux/409'],
['yellow/410','grey/412','turquoise/413','purple/414','orange/415','brown/416','beige/417','lemon/419']
];
var fontPickerFontStrings = [
	"Arial",
	"Arial Black",
	"Arial Narrow",
	"Autumn",
	"Century Gothic",
	"Cordia New",
	"Tahoma",
	"Verdana",
	"Comic Sans MS"
];
//Utility Functions
function IDGenerator(nextID){
	this.nextID = nextID;
	this.GenerateID = IDGeneratorGenerateID;
}
function IDGeneratorGenerateID(){
	return this.nextID++;
}
function FontPicker (boundControl, scriptAction){
//Object Variables
	this.idGenerator = FontPicker_idGenerator;
	this.instantiated = false;
	this.scriptAction = scriptAction;
	this.valueControl = '';
	this.displayControl = '';
	this.boundControl = boundControl;
	
	this.demoMessage = "The quick brown fox jumps over the lazy dog...";
	this.showDemo = false;
	this.showBold = true;
	this.showItalic = true;
	this.showUnderline = true;
	this.showStatus = false;
	this.showFontFace = true;
	this.showFontSize = true;
	this.showColor = true;
	this.showColorTxt = false;
	
	this.objectFonts = fontPickerFontStrings;
	
	this.fontSize_minimum = 12;
	this.fontSize_inc = 2;
	this.fontSize_maximum = 30;
	this.CSSSettings_Status = "font: 8px verdana;";
	this.CSSSettings_Button = "padding:0 0 0 0; border:1px raised; font: 12px verdana;";
	this.CSSSettings_Object = "border: none; background-color:#FFFFFF;";
	this.CSSSettings_Control = "border: 1px solid #000000;";
	this.CSSSettings_Demo = "border: 1px solid #000000;";
	this.width = "300px";
//Font Decoration Variables
	this.colorrgb = "#000000";
	this.colorname = "Black";
	this.bold = false;
	this.italic = false;
	this.underline = false;
	this.fontsize = "20";
	this.fontfamily = "Arial";
   this.align = 1;   // left

//Functions
	this.Instantiate = FontPicker_Instantiate;
	this.parseCSSString = FontPicker_parseInitialCSSString;
	this.updateDisplay = FontPicker_UpdateDisplay;
	
	this.Instantiate;
}
function FontPicker_UpdateDisplay(){
	FontPicker_SetDisplayAreaParams(this.id);
}
function FontPicker_GetFontDilimeters(fontName){
	if (fontName.indexOf(" ") != -1)return "'" + fontName + "'";
	return fontName;
}
function FontPicker_Instantiate(){
	if (this.instantiated) {
		return;
	}
	this.id = this.idGenerator.GenerateID();
	fontPickerMap[this.id] = this;
	fontPickerIDGenerator = this.idGenerator;
	var html = "";
//Set the event styles
   var colorcell = 20;
	html += "<STYLE>";
	html += "#ATR_SELECT {display: block; background-color:#FFFFFF; color:#000000; text-decoration:none; width:100%; height:20px;}";
	html += "#ATR_SELECT:hover {background-color:#CCCCCC;}";
	html += "#ATR_COLOR {display: block; text-decoration:none; width:"+colorcell+"px; height:"+colorcell+"px;}";
	html += "";
	html += "";
	html += "</STYLE>";
//Create the object container
//Create the buttons for the subsequent dropdowns
	html += "<TABLE id='fp1' Cellspacing=0 cellpadding=0 bgcolor=red> ";
	html += "<tr><td colspan=6>";
	html += "<table id='fp2' cellspacing=0 cellpadding=0 border=0 width='100%' bgcolor=red>";
//Top Row
	html += "<tr>";
//Font face
	html += "<td id='fp-f' valign='center'>";
	html += "<input type='text' id='"+FONT_FONTFACE_PREFIX + this.id + "' readonly='readonly'>";
	html += "</td>";
	html += "<td id='fp-f'>";
	html +=	"<a href=\"javascript:FontPicker_ShowPicker(" + this.id + ", 0);\">&nbsp;&nbsp;&nbsp;&nbsp</a>";
	html += "</td>";

//Size 
	html += "<td id='fp-s' valign='center'>";
	html += "<input type='text' id='"+FONT_FONTSIZE_PREFIX + this.id + "' readonly='readonly'>";
	html += "</td>";
	html += "<td id='fp-s'>";
	html +=	"<a href=\"javascript:FontPicker_ShowPicker(" + this.id + ", 1);\">&nbsp;&nbsp;&nbsp;&nbsp</a>";
	html += "</td>";
//Color Button	
	html += "<td>";
	if (this.showColor)html += "<input onmouseover=\"this.style.cursor='hand'\" name='btnFontColourPicker_Button" + this.id + "' type='button' id='btnFontColourPicker_Button" + this.id + "' style='width:20px; height:20px; border:2px ridge #CCCCCC;' value='' onClick='FontPicker_ShowPicker(" + this.id + ", 2);'>";
	html += "</td>";
//Color Input Box
	html += "<td >";	
	if (this.showColorTxt)html += "<input type='text'  name='txtFontColourPicker_TextBox" + this.id + "' id='txtFontColourPicker_TextBox" + this.id + "' style='width:50px; height:20px; border:1px solid #CCCCCC; font: 10 Arial' onblur='FontPicker_SetBackgroundColour(" + this.id + ",this.value)'></td>";
	html += "</td>";
//Status Display	
	html += "<td rowspan=2 width=100% id='" + FONT_STATUSDISPLAY_PREFIX + this.id + "' name='id=" + FONT_STATUSDISPLAY_PREFIX + this.id + "' style='" + this.CSSSettings_Status + "'></td>";
//Second Row - Dropdowns
	html += "</tr><tr>";
	html += "<td valign='top'>";
//Font Face Dropdown
	html += "<div id='"+FONT_FONTFACEDROPDOWN_PREFIX + this.id + "' name='"+FONT_FONTFACEDROPDOWN_PREFIX + this.id + "' style='background-color:#F3F3E9;height: 180px; width:120px; display:none; position:absolute; overflow: auto; border: 2px solid #CCCCCC; z-index: 100;'>"
	html += "<table id='fp-fdd' width=120px cellpadding=0 cellspacing=0 border=0 bgcolor='blue'>" 
	for (i = 0; i < this.objectFonts.length; i += 1){
		html +=	"<tr><td nowrap><a id=\"ATR_SELECT\" href=\"javascript:FontPicker_SetFontFace(" + this.id + ",'" + this.objectFonts[i] + "');void(0);\" style=\"font: 14px " + FontPicker_GetFontDilimeters(this.objectFonts[i]) + ";\">" + this.objectFonts[i] + "</a></td></tr>";
	}
	html += "</table>"
	html += "</div>"
	html += "</td>";
	html += "<td colspan=1></td>";
	html += "<td align='top'>";
//Font Size Dropdown
 	html += "<div id='"+FONT_FONTSIZEDROPDOWN_PREFIX + this.id + "' name='"+FONT_FONTSIZEDROPDOWN_PREFIX + this.id + "' style='height: 240px; width=100px; display:none; position:absolute; overflow: auto; border: 2px solid #CCCCCC; background-color:#F3F3E9;'>"
	html += "<table id='fp-sdd' width=60px cellpadding=0 cellspacing=0 bgcolor='#FFFFFF'>"
   
	for (i = this.fontSize_minimum; i <= this.fontSize_maximum; i += this.fontSize_inc){
		html += "<tr><td nowrap valign='center' ><a id=\"ATR_SELECT\" href=\"javascript:FontPicker_SetFontSize(" + this.id + ",'"+i+"');void(0);\" style=\"font-size:" + i + "px;height:" + (i+2) + "px;\">" + i + "</a></td></tr>"
	}
//		html += "<tr><td nowrap valign='center' ><a  href=\"#\" >&nbsp;</a></td></tr>"
	html +=	"</table>"
	html +=	"</div>"
	html += "</td>";
 //	html += "<td colspan=1></td>";
	html += "<td colspan=1 valign='top'>";
	//Color Swatch Dropdown
   var colorcell = 20;
	var csWidth = fontPickerColorRgb[0].length * colorcell;
	var csHeight = fontPickerColorRgb.length * colorcell + colorcell;
	var divWidth = fontPickerColorRgb[0].length * (colorcell + 0);
	var divHeight = fontPickerColorRgb.length * (colorcell + 2) ;//+ colorcell + 2;
	html += "<DIV id='"+FONT_FONTCOLOURDROPDOWN_PREFIX+this.id+"' name='"+FONT_FONTCOLOURDROPDOWN_PREFIX+this.id+"' style='position:absolute; display:none; width:"+divWidth+"px; height:"+divHeight+"px;border:1px solid #999999;z-index:1; background-color:#FFFFFF; overflow:visible;'>"
	html += "<table width="+csWidth+" height="+eval(csHeight-colorcell)+" border=1 cellspacing=0 cellpadding=0 bgcolor='#FFFFFF'>";
	for (i=0; i < fontPickerColorRgb.length ; i++) {
	html += "<tr>";
	for (ii=0; ii<fontPickerColorRgb[i].length; ii++) {
		html += "<td valign=top width="+colorcell+" height="+colorcell+" bgcolor=#"+fontPickerColorRgb[i][ii]+" style='border:1px solid white;' onmouseover='this.style.border=\"1px inset black\";window.status=\"#" + fontPickerColorRgb[i][ii] + "\";FontPicker_modifyText(\"thisColourObject"+this.id+"\",\"" + fontPickerColorName[i][ii] + "\");return true;' onmouseout='this.style.border=\"1px solid white\";window.status=window.defaultStatus;\'><a id=\"ATR_COLOR\" href=\"javascript:FontPicker_SetColour("+this.id+","+i+","+ii+");void(0);\" style=\"text-decoration:none; font: 10px verdana\">&nbsp;</a></td>";
		}
	html += "</tr>";
	}
	html += "</table>";
	html += "<table width="+csWidth+" border=0 cellspacing=0 cellpadding=0 bgcolor='#FFFFFF'>";
	html += "<tr>";
	html += "<td NOWRAP width=\"75%\" align=right><SPAN id='thisColourObject"+this.id+"' name='thisColourObject"+this.id+"' style=\"font:10px Arial\">No Colour</SPAN></td>";
	html += "</tr>";
	html += "</table>";
	html += "</DIV>";
	html += "</td>";
	html += "</tr></table></td></tr>";
//Bold Button	
	html += "<tr id='fp-button'> ";
	html += "<td id='fp-w'>";
	html +=	"<a onClick='if(this.blur)this.blur()' href=\"javascript:FontPicker_SetFontBold(" + this.id + ");\" id='"+FONT_FONTBOLD_PREFIX + this.id + "'>&nbsp;&nbsp;&nbsp;&nbsp</a>";
	html += "</td>";
//Italic Button	
	html += "<td id='fp-i'>";
	html +=	"<a onClick='if(this.blur)this.blur()' href=\"javascript:FontPicker_SetFontItalic(" + this.id + ");\" id='"+FONT_FONTITALIC_PREFIX + this.id + "'>&nbsp;&nbsp;&nbsp;&nbsp</a>";
	html += "</td>";
//Underline Button
	html += "<td id='fp-u'>";
	html +=	"<a onClick='if(this.blur)this.blur()' href=\"javascript:FontPicker_SetFontUnderline(" + this.id + ");\" id='"+FONT_FONTUNDERLINE_PREFIX + this.id + "'>&nbsp;&nbsp;&nbsp;&nbsp</a>";
	html += "</td>";
//Text justification Buttons
	html += "<td id='fp-l'>";
	html +=	"<a onClick='if(this.blur)this.blur()' href=\"javascript:FontPicker_SetFontJust(" + this.id + ", 0);\" id='"+FONT_FONTJUSTL_PREFIX + this.id + "'>&nbsp;&nbsp;&nbsp;&nbsp</a>";
	html += "</td>";
	html += "<td id='fp-c'>";
	html +=	"<a onClick='if(this.blur)this.blur()' href=\"javascript:FontPicker_SetFontJust(" + this.id + ", 1);\" id='"+FONT_FONTJUSTC_PREFIX + this.id + "'>&nbsp;&nbsp;&nbsp;&nbsp</a>";
	html += "</td>";
	html += "<td id='fp-r'>";
	html +=	"<a onClick='if(this.blur)this.blur()' href=\"javascript:FontPicker_SetFontJust(" + this.id + ", 2);\" id='"+FONT_FONTJUSTR_PREFIX + this.id + "'>&nbsp;&nbsp;&nbsp;&nbsp</a>";
	html += "</td>";
	html += "</tr>";
	if (this.showDemo)
   {
	html += "<tr><td name='"+FONT_DISPLAY_PREFIX+this.id+"' id='"+FONT_DISPLAY_PREFIX+this.id+"' align='center' valign='middle' style='"+this.CSSSettings_Demo+"'>" +this.demoMessage+ "</td></tr>";
	}
   /*
   else{
	html += "<tr><td> </td></tr>";
   }
   */
	html += "</table>";
	//info(html);
	document.write (html);
	this.parseCSSString();
	FontPicker_SetDisplayAreaParams(this.id); 
	this.instantiated = true;
}
function FontPicker_parseInitialCSSString(){
	var isBoldRegExp = new RegExp("(bold)","gi");
	var isItalicRegExp = new RegExp("(italic)","gi");
	var isUnderlineRegExp = new RegExp("(underline)","gi");
	var isColorDefined = new RegExp("(#[A-Fa-f0-9]{6})","gi");
	var isFontFaceDefine = new RegExp("(andale mono ipa)|(Arial)|(arial narrow)|(arial black)|(arial unicode ms)|(batang)|(book antiqua)|(bookman old style)|(Century)|(century gothic)|(comic sans ms)|(Courier)|(fantasy)|(fixedsys)|(garamond)|(haettenschweiler)|(impact)|(lucida console)|(lucida sans unicode)|(microsoft sans serif)|(monotype corsiva)|(ms mincho)|(ms sans serif)|(marlett)|(modern)|(monospace)|(mt extra)|(palatino linotype)|(Roman)|(script)|(small font)|(simsun)|(symbol)|(system)|(tahoma)|(terminal)|(times new roman)|(trebuchet ms)|(verdana)|(wingdings)","gi");
	var initialCSSString = "";
	initialCSSString = getFPObject (this.boundControl).value
	if (initialCSSString == "")return;
	var colourString = initialCSSString.match(isColorDefined);
	var fontsizestring = initialCSSString.match(/\d+px/gi);
	var fontfacestring = initialCSSString.match(isFontFaceDefine);
	if (fontfacestring.length > 0)this.fontfamily = fontfacestring[0];
	if (fontsizestring.length > 0)this.fontsize = fontsizestring[0];
	if (colourString.length > 0)this.colorrgb = colourString[0];
	if (initialCSSString.search(isBoldRegExp)!= '-1')this.bold=true;
	if (initialCSSString.search(isItalicRegExp)!= '-1')this.italic=true;
	if (initialCSSString.search(isUnderlineRegExp)!= '-1')this.underline=true;
	
}
function FontPicker_SetFontJust(id, align){
	fontPickerMap[id].align = align;
   FontPicker_SetFontJustBtn(id);
	FontPicker_SetDisplayAreaParams(id);
	eval(fontPickerMap[id].scriptAction);	
}
function FontPicker_SetFontSize(id, size){
	fontPickerMap[id].fontsize = size;
	FontPicker_SetDisplayAreaParams(id);
	FontPicker_ShowPicker(id, 1);
	eval(fontPickerMap[id].scriptAction);	
	getFPObject (FONT_FONTSIZE_PREFIX+id).value = size;
}
function FontPicker_SetFontFace(id, font){
	fontPickerMap[id].fontfamily = font;
	FontPicker_SetDisplayAreaParams(id);
	FontPicker_ShowPicker(id, 0);
	eval(fontPickerMap[id].scriptAction);	
	getFPObject (FONT_FONTFACE_PREFIX+id).value = font;
}
function FontPicker_SetFontBold(id){
	fontPickerMap[id].bold = !fontPickerMap[id].bold;
   FontPicker_SetFontBoldBtn(id);
   FontPicker_SetDisplayAreaParams(id);
	eval(fontPickerMap[id].scriptAction);	
}
function FontPicker_SetFontItalic(id){
	fontPickerMap[id].italic = !fontPickerMap[id].italic;
   FontPicker_SetFontItalicBtn(id);
	FontPicker_SetDisplayAreaParams(id);
	eval(fontPickerMap[id].scriptAction);	
}
function FontPicker_SetFontUnderline(id){
	fontPickerMap[id].underline = !fontPickerMap[id].underline;
   FontPicker_SetFontUnderlineBtn(id);
	FontPicker_SetDisplayAreaParams(id);
	eval(fontPickerMap[id].scriptAction);	
}
function FontPicker_getStyleString(id){
	var styleString = "";
	styleString += 'font-size:' + fontPickerMap[id].fontsize + 'px;';
	styleString += 'font-family:' + FontPicker_GetFontDilimeters(fontPickerMap[id].fontfamily) + ';';
	styleString += 'color:' + fontPickerMap[id].colorrgb + ';';
	if (fontPickerMap[id].bold){
		styleString +=  'font-weight:bold;';
	}else{
		styleString +=  'font-weight:normal;';
	}
	if (fontPickerMap[id].italic){
		styleString +=  'font-style:italic;';
	}else{
		styleString +=  'font-style:normal;';
	}
	if (fontPickerMap[id].underline){
		styleString +=  'text-decoration:underline;';
	}else{
		styleString +=  'text-decoration:none;';
	}
	return styleString;
}
function FontPicker_SetDisplayAreaParams(id){
//Construct the status display string
	if (fontPickerMap[id].showStatus) 
   {
	   var statusString = "";
	   statusString += "<b>Font-Face:</b> " + fontPickerMap[id].fontfamily + "<BR>";
	   statusString += "<b>Font-Size:</b> " + fontPickerMap[id].fontsize + "px<BR>";
	   statusString += "<b>Bold:</b> " + fontPickerMap[id].bold + "<BR>";
	   statusString += "<b>Italic:</b> " + fontPickerMap[id].italic + "<BR>";
	   statusString += "<b>Underline:</b> " + fontPickerMap[id].underline;
	   getFPObject (FONT_STATUSDISPLAY_PREFIX + id).innerHTML = statusString;
   }
	getFPObject (fontPickerMap[id].boundControl).value = FontPicker_getStyleString(id);
	if (fontPickerMap[id].showColorTxt)getFPObject ('txtFontColourPicker_TextBox' + id).value = fontPickerMap[id].colorrgb;
	if (fontPickerMap[id].showColor)getFPObject ('btnFontColourPicker_Button' + id).style.backgroundColor = fontPickerMap[id].colorrgb;
	if (fontPickerMap[id].showDemo){
//		getFPObject (FONT_DISPLAY_PREFIX + id).innerHTML = fontPickerMap[id].demoMessage;
	}
   FontPicker_Apply();
}
function isColor(color){
	var myRegExp = new RegExp("#[A-Fa-f0-9]{6}","gi");
	if (color.search(myRegExp)!=-1)return true;
	return false;
}
function FontPicker_SetColour(id, i, ii){
   var color = "#"+fontPickerColorRgb[i][ii];
	color = color.toUpperCase();
	if (!isColor(color) && color != '')
      ;
   else
   {      
	   fontPickerMap[id].colorrgb = color;
	   fontPickerMap[id].colorname = fontPickerColorName[i][ii];
	   FontPicker_SetDisplayAreaParams(id);
   }
	FontPicker_ShowPicker(id, 2);
	eval(fontPickerMap[id].scriptAction);
}
function FontPicker_SetBackgroundColour(id, color){
	color = color.toUpperCase();
	if (!isColor(color) && color != '')return;
	fontPickerMap[id].colorrgb = color;
	FontPicker_SetDisplayAreaParams(id);
	FontPicker_ShowPicker(id, 2);
	eval(fontPickerMap[id].scriptAction);
} 
function FontPicker_ShowPicker(id, pickerID){
	switch (pickerID){
		case 0:
			getFPObject (FONT_FONTFACEDROPDOWN_PREFIX+id).style.display = (String(getFPObject (FONT_FONTFACEDROPDOWN_PREFIX+id).style.display).toLowerCase() == 'inline' ? "none" : "inline"); 
			getFPObject (FONT_FONTSIZEDROPDOWN_PREFIX+id).style.display = 'none'; 
			getFPObject (FONT_FONTCOLOURDROPDOWN_PREFIX+id).style.display = 'none'; 
			break;
		case 1:
			getFPObject (FONT_FONTSIZEDROPDOWN_PREFIX+id).style.display = (String(getFPObject (FONT_FONTSIZEDROPDOWN_PREFIX+id).style.display).toLowerCase() == 'inline' ? "none" : "inline"); 
			getFPObject (FONT_FONTFACEDROPDOWN_PREFIX+id).style.display = 'none'; 
			getFPObject (FONT_FONTCOLOURDROPDOWN_PREFIX+id).style.display = 'none'; 
			break;
		case 2:
			getFPObject (FONT_FONTCOLOURDROPDOWN_PREFIX+id).style.display = (String(getFPObject (FONT_FONTCOLOURDROPDOWN_PREFIX+id).style.display).toLowerCase() == 'inline' ? "none" : "inline"); 
			getFPObject (FONT_FONTFACEDROPDOWN_PREFIX+id).style.display = 'none'; 
			getFPObject (FONT_FONTFACEDROPDOWN_PREFIX+id).style.display = 'none'; 
			break;
	}
}
function FontPicker_modifyText(id, text) {
	if(document.getElementById && text != '') {
		obj = document.getElementById(id);
		obj.childNodes[0].data = text;
	}
}

function FontPicker_Apply(){
   id = fontPickerApplyToID ;
   if (id != null )
   {
   //alert('id:'+id);
	   getDOMObject (id).style.fontSize = fontPickerMap[fontPickerID].fontsize+'px';
	   getDOMObject (id).style.fontFamily = FontPicker_GetFontDilimeters(fontPickerMap[fontPickerID].fontfamily);
	   getDOMObject (id).style.color = fontPickerMap[fontPickerID].colorrgb;
	   getDOMObject (id).style.fontWeight = (fontPickerMap[fontPickerID].bold ? "bold": "normal");
	   getDOMObject (id).style.fontStyle = (fontPickerMap[fontPickerID].italic ? "italic": "normal");
	   getDOMObject (id).style.textDecoration = (fontPickerMap[fontPickerID].underline ? "underline": "none");
      var align = fontPickerMap[fontPickerID].align;
      var tx = 'left';
      if (align == 1)
         tx = 'center';
      else if (align == 2)
         tx = 'right';
     getDOMObject (id).style.textAlign = tx;
   }
   FontPicker_styleOut();
}

function FontPicker_ApplyTo(id1, id2){
   FontPicker_styleOut();
  fontPickerApplyToID = id1;     //control whose text is to be formatted based on fontpicker info
   fontPickerStyleToID = id2;     //control where style info is to be saved
   FontPicker_styleIn();
   FontPicker_Apply();
}

function FontPicker_styleOut(){
   id = fontPickerStyleToID;
   if (id != null )
   {
	   var styleString = "";
	   styleString += 's:' + fontPickerMap[fontPickerID].fontsize + ';';
	   styleString += 'f:' + fontPickerMap[fontPickerID].fontfamily + ';';
	   styleString += 'c:' + fontPickerMap[fontPickerID].colorrgb + ';';
	   styleString += 'n:' + fontPickerMap[fontPickerID].colorname + ';';
	   styleString += 'w:' + fontPickerMap[fontPickerID].bold + ';';
	   styleString += 'i:' + fontPickerMap[fontPickerID].italic + ';';
	   styleString += 'u:' + fontPickerMap[fontPickerID].underline + ';';
	   styleString += 'j:' + fontPickerMap[fontPickerID].align + ';';
	   getDOMObject (id).value = styleString;
   }
}

function getArgs(dvalue)
{
	   var dstring = dvalue;
      var args = new Object ( );
      var pairs = dstring.split ( ';' ); // Break at ;. 
      for ( var i = 0; i < pairs.length; i++ )
      {
         var pos = pairs[i].indexOf ( ':' ); // Look for "name=value". 
         if ( pos == -1 ) continue; // If not found, skip. 
         var argname = pairs[i].substring ( 0,pos ); // Extract the name. 
         var value = pairs[i].substring ( pos+1 ); // Extract the value. 
         args[argname] = value; // Store as a property. // In JavaScript 1.5, use decodeURIComponent() instead of escape() } 
      }
      return args;
}
function FontPicker_styleIn()
{
   var id = fontPickerStyleToID;
   if (id != null )
   {
      var args = getArgs(getDOMObject (id).value);
	   var colorrgb = "#000000";
	   var colorname = "Black";
	   var bold = false;
	   var italic = false;
	   var underline = false;
	   var fontsize = "20";
	   var fontfamily = "Arial";
      var align = 1;   // left
      if ( args.s ) fontsize = args.s;
      if ( args.f ) fontfamily = args.f;
      if ( args.c ) colorrgb = args.c;
      if ( args.n ) colorname = args.n;
      if ( args.w ) bold = (args.w=='true'? true: false);   // remember string false is NOT boolean false, so we do this
      if ( args.i ) italic = (args.i=='true'? true: false);
      if ( args.u ) underline = (args.u=='true'? true: false);
      if ( args.j ) align = args.j;
	   fontPickerMap[fontPickerID].fontsize = fontsize;
	   fontPickerMap[fontPickerID].fontfamily = fontfamily;
	   fontPickerMap[fontPickerID].colorrgb = colorrgb;
	   fontPickerMap[fontPickerID].colorname = colorname;
	   fontPickerMap[fontPickerID].bold = bold;
	   fontPickerMap[fontPickerID].italic = italic;
	   fontPickerMap[fontPickerID].underline = underline;
	   fontPickerMap[fontPickerID].align = align;
  	if (!fontPickerMap[fontPickerID].instantiated) {
		return;
	}
    
      id = fontPickerID;
      getFPObject ('btnFontColourPicker_Button' + id).style.backgroundColor = fontPickerMap[id].colorrgb;

      // now set buttons in right state
      FontPicker_SetFontBoldBtn(id);
      FontPicker_SetFontItalicBtn(id);
      FontPicker_SetFontUnderlineBtn(id);
      FontPicker_SetFontJustBtn(id);

	   getFPObject (fontPickerMap[id].boundControl).value = FontPicker_getStyleString(id);
	   getFPObject (FONT_FONTSIZE_PREFIX+id).value = fontsize;
	   getFPObject (FONT_FONTFACE_PREFIX+id).value = fontfamily;
   }   
}
function FontPicker_SetFontBoldBtn(id){
	getFPObject (FONT_FONTBOLD_PREFIX+id).className="active" + (fontPickerMap[fontPickerID].bold ? "1": "0");
   }
function FontPicker_SetFontItalicBtn(id){
	getFPObject (FONT_FONTITALIC_PREFIX+id).className="active" + (fontPickerMap[fontPickerID].italic ? "1": "0");
}

function FontPicker_SetFontUnderlineBtn(id){
	getFPObject (FONT_FONTUNDERLINE_PREFIX+id).className="active" + (fontPickerMap[fontPickerID].underline? "1": "0");
}
function FontPicker_SetFontJustBtn(id){
   var align = fontPickerMap[fontPickerID].align;
	getFPObject (FONT_FONTJUSTL_PREFIX+id).className="active" + (align==0? "1": "0");
	getFPObject (FONT_FONTJUSTC_PREFIX+id).className="active" + (align==1? "1": "0");
	getFPObject (FONT_FONTJUSTR_PREFIX+id).className="active" + (align==2? "1": "0");
}

