var forecolor="black", backcolor = "white";

var viewdoc = parent.color_view.document;
var backdoc = parent.color_top.color_back.document;
var radiodoc = parent.color_top.color_radio.document;
var radioloc = parent.color_top.color_radio;
var foredoc = parent.color_top.color_fore.document;

var teststring = new Array(4);
teststring[0] = "Test normal   "
teststring[1] = "Test bold   "
teststring[2] = "Test italic"

function colorchg(clr)
{ if (radiodoc.radioform.foreback[0].checked==true)
  { backcolor = clr; }
  else
  { forecolor = clr; }

  viewdoc.open("text/html");
  viewdoc.bgColor=backcolor;
  viewdoc.fgColor=forecolor;

  viewdoc.write("<table border=0 width=100%><tr><td align=center width=34%><font face='Comic Sans MS'>");
  viewdoc.write(teststring[0],teststring[1].bold(),teststring[2].italics());
  viewdoc.write("</font></td><th width=32%>");
  viewdoc.write(backcolor,"&nbsp;&nbsp;&nbsp;&nbsp;",forecolor);
  viewdoc.write("</font></th><td align=center width=34%>");
  viewdoc.write(teststring[0],teststring[1].bold(),teststring[2].italics());
  viewdoc.write("</td></tr></table>");
  viewdoc.close();

  var hexnum = viewdoc.bgColor;
  backdoc.backform.hexval.value = hexnum;
  backdoc.backform.Rval.value = String(parseInt(hexnum.slice(1,3), 16));
  backdoc.backform.Gval.value = String(parseInt(hexnum.slice(3,5), 16));
  backdoc.backform.Bval.value = String(parseInt(hexnum.slice(5,7), 16));

  hexnum = viewdoc.fgColor;
  foredoc.foreform.hexval.value = hexnum;
  foredoc.foreform.Rval.value = String(parseInt(hexnum.slice(1,3), 16));
  foredoc.foreform.Gval.value = String(parseInt(hexnum.slice(3,5), 16));
  foredoc.foreform.Bval.value = String(parseInt(hexnum.slice(5,7), 16));
}

function byhex(fob,hrgb)
{ var hexvalue = ""
  if (fob==0) { hexvalue = backdoc.backform.hexval.value; radioloc.setradio(0);}
  else        { hexvalue = foredoc.foreform.hexval.value; radioloc.setradio(1);}

  if (hrgb != 0)
  { var rgbnum = 0;
    var fobix =  hrgb;

    if (fob==0)
    { --fobix;
      rgbnum = parseInt(backdoc.backform.elements[fobix].value, 10);}
    else
    { rgbnum = parseInt(foredoc.foreform.elements[fobix].value, 10);}
    
    if (rgbnum < 0 || isNaN(rgbnum)) { rgbnum = 0; }
    else if (rgbnum > 255) { rgbnum = 255; }

    var rgbhex = rgbnum.toString(16);
    if (rgbhex.length<2) rgbhex = "0" + rgbhex;

    var start = 1 + (hrgb-1) * 2;
    var tempstr = hexvalue.slice(0,start) + rgbhex;
    if (start<5) { tempstr += hexvalue.slice((start+2),7);}
    hexvalue = tempstr;
  }
  colorchg(hexvalue);
}

