var moz=document.getElementById&&!document.all;
var viewMode = 1;

function Init_1()
{
    iView.document.designMode = 'On';
}

function selOn(ctrl)
{
  ctrl.style.borderColor = '#000000';
  ctrl.style.backgroundColor = '#8492B5';
  ctrl.style.cursor = 'hand';
}
  
  function selOff(ctrl)
  {
   ctrl.style.borderColor = '#FFFFFF';  
   ctrl.style.backgroundColor = '#E9E7E7';
  }
  
  function selDown(ctrl)
  {
   ctrl.style.backgroundColor = '#8492B5';
  }
  
  function selUp(ctrl)
  {
    ctrl.style.backgroundColor = '#B5BED6';
  }
    
  function doBold(ind)
  {
     doWork(ind,'bold');
  }
  function doItalic(ind)
  {
   doWork(ind,'italic');
  }

 function doImage(ind)
  {
   imgSrc = "http://90.0.0.95/russell/admin/img/";
   doWork(ind,'insertimage');
  }
  function doUnderline(ind)
  {
   doWork(ind,'underline');
  }
  function doLeft(ind)
  {
    doWork(ind,'justifyleft');
  }
  function doCenter(ind)
  {
    doWork(ind,'justifycenter');
  }
  function doRight(ind)
  {
    doWork(ind,'justifyright');
  }
  function doJustify(ind)
  {
    doWork(ind,'justify');
  }
  function doOrdList(ind)
  {
    doWork(ind,'insertorderedlist');
  }
  function doBulList(ind)
  {
    doWork(ind,'insertunorderedlist');
  }
  function doForeCol(ind)
  {
    var fCol = prompt('Enter foreground color', '');

    if(fCol != null)
      doWork4Obj(ind,'forecolor',fCol);
  }
  function doBackCol(ind)
  {
    var bCol = prompt('Enter background color', '');

    if(bCol != null)
      doWork4Obj(ind,'backcolor',bCol);
  }

  function doLink(ind)
  {
    if(ind==0)
    {
        iView.document.execCommand('createlink');
    }
  }
  function doRule(ind)
  {
     doWork(ind,'inserthorizontalrule');
  }
  function doFont(fName,ind)
  {
    if(fName != '')
      doWork4Obj(ind,'fontname', fName);
  }
  
  function doSize(fSize,ind)
  {
    if(fSize != '')
    {
    doWork4Obj(ind,'fontsize', fSize);
    }
  }

  function doHead(hType,ind){
      if(hType != ''){
         doWork4Obj(ind,'formatblock', hType);
         doFont(document.disfetchdetail.selFont.options[document.disfetchdetail.selFont.selectedIndex].value,ind);
      }
   }

  function doToggleView(ctrl){
                    if(viewMode == 1){
                         iHTML = ctrl.document.body.innerHTML;
                         ctrl.document.body.innerText = iHTML;
                         tblCtrls.style.display = 'none';
                         disMessage.style.display = 'inline';
                         document.fetchdetail.selFont.style.display = 'none';
                         document.fetchdetail.selSize.style.display = 'none';
                         document.fetchdetail.selHeading.style.display = 'none';
                         document.fetchdetail.btnSubmit.disabled=true;
                         document.fetchdetail.btnReset.disabled=true;

                         ctrl.focus();
                         viewMode = 2;
                    }
                    else{
                         iText = ctrl.document.body.innerText;
                         ctrl.document.body.innerHTML = iText;
                         tblCtrls.style.display = 'inline';
                         disMessage.style.display = 'none';

                         document.fetchdetail.selFont.style.display = 'inline';
                         document.fetchdetail.selSize.style.display = 'inline';
                         document.fetchdetail.selHeading.style.display = 'inline';
                         document.fetchdetail.btnSubmit.disabled=false;
                         document.fetchdetail.btnReset.disabled=false;
                         ctrl.focus();
                         viewMode = 1;
                    }                  
   }

function MM_callJS(jsStr)
{
  return eval(jsStr);
}

function getContent()
{
var htmlCode = ctrl.document.body.innerHTML;
document.fetchdetail.Content.value = htmlCode;
}

 ////Common Function/////
 function doWork(ind,prop)
 {
    if(ind==0)
    {
        iView.document.execCommand(prop, false, null);
    }
 }

 function doWork4Obj(ind,prop,obj)
  {
    if(!moz)
    {
        document.iView.focus();
    }
    else
    {
        window.frames['iView'].focus();
    }
    if(ind==0)
    {
        iView.document.execCommand(prop, false, obj);
    }
  }

function myTest()
{
    iView.document.execCommand('InsertImage', false,obj);
}

function getIframe()
{
    var htmlCode = iView.document.body.innerHTML;
    if(document.getElementById("hdnWebPageContent").value=="")
    {
      document.getElementById("hdnWebPageContent").value = htmlCode;
      document.disfetchdetail.submit();
    }
}

function doForeCol(ind)
{
    var fCol = prompt('Enter foreground color', '');
    
    if(fCol != null)
      doWork4Obj(ind,'forecolor',fCol);
}
function savePage()
{
    var htmlCode = iView.document.body.innerHTML;
    if(htmlCode=="")
    {
       alert("Please enter the Page Content");
    }
    else
    {
      document.getElementById("hdnWebPageContent").value = htmlCode;
      document.getElementById("disfetchdetail").action="page_add.php?mode=ADD";
      document.getElementById("disfetchdetail").submit();
    }
}

var intError=0;
function validation(obj,text)
{
  if(document.getElementById(obj).value=="")
  {
    alert("Please Enter the "+text);
    document.getElementById(obj).focus();
    intError=1;
    return false;
  }
}

function numericVal(e)
{
  if (!e) var e = window.event;
  var keycode = (e.keyCode) ? e.keyCode : e.which;
     if (keycode > 47 && keycode < 58 || keycode == 8 || keycode == 9 || keycode == 46)
    {
       return true;
    }
    else
    { 
     keycode=0;
     return false;
    }
}

function validate_programAdd()
{
     intError=0;
     validation("programName","Program Name");

     var htmlCode = iView.document.body.innerHTML;
     if(intError==0)
     {
       if (htmlCode=="")
       {
            alert('Please Enter the Introduction.');
            return false;
       }
       validation("programCost","Cost of 1Program");
     }
     document.getElementById("hdnWebPageContent").value = htmlCode;
    //
     if(intError==1)
     {
         return false;
     }
     else
     {
         return true;
     }


}

function validate_moduleAdd()
{
     intError=0;
     validation("moduleName","Module Name");

     var htmlCode = iView.document.body.innerHTML;
     if(intError==0)
     {
       if (htmlCode==""  &&  htmlCode=="<P>&nbsp;</P>")
       {
            alert('Please Enter the Module Introduction.');
            return false;
       }
     }

     if(intError==1)
     {
         return false;
     }
     else
     {
         document.getElementById("hdnWebPageContent").value = htmlCode;
         return true;
     }
}

function validate_moduleCon()
{

//Module.as.well.as.program
     var htmlCode = iView.document.body.innerHTML;
     if (htmlCode==""  &&  htmlCode=="<P>&nbsp;</P>")
     {
            alert('Please Enter the Module Conclusion');
            return false;
     }

     document.getElementById("hdnWebPageContent").value = htmlCode;
     return true;
}


var myCode="";
function getHTML(htmlCode)
   {
   myCode=htmlCode;
//       MM_callJS('Init_1()');
       /* iView.document.designMode = 'On';
       var range = iView.document.selection.createRange();
          range.pasteHTML(htmlCode);
          alert(range);
 */
 //iView.document.body.innerHTML =htmlCode;

   }

function Init_Edit()
{
    iView.document.designMode = 'On';
}

function validate_questionAdd()
{
    var intError=0;
     // alert(document.getElementById("opt_1").checked);
     if(document.getElementById("opt1").checked==false && document.getElementById("opt2").checked==false && document.getElementById("opt3").checked==false && document.getElementById("opt4").checked==false && document.getElementById("opt5").checked==false)
     {
         alert("Please select the answer");
         intError=1;
         return false;
     }

     for(i=1;i<=Number(document.getElementById("numopt").value);i++)
     {
         if(document.getElementById("answerText_"+i).value=="")
         {
             alert("Please enter the answer");
             document.getElementById("answerText_"+i).focus();
             intError=1;
             return false;
         }
     }
     var htmlCode = iView.document.body.innerHTML;
     if (htmlCode==""  &&  htmlCode=="<P>&nbsp;</P>")
       {
            alert('Please Enter the Module Introduction.');
            return false;
       }

     //alert(intError);
     if(intError==1)
     {
         return false;
     }
     else
     {
         document.getElementById("hdnWebPageContent").value = htmlCode;
         return true;
     }
}



var TCP = new TColorPicker();
var TCP1 = new TColorPicker1();
var fCol="";
var bCol="";

function TCPopup1(palette) {
    this.initPalette = !palette || palette > 3 ? 0 : palette;
    var w = 194, h = 240,
    move = screen ? 
        ',left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '', 
    o_colWindow = window.open('pickerbg.html', null, "help=no,status=no,scrollbars=no,resizable=no" + move + ",width=" + w + ",height=" + h + ",dependent=yes", true);
    o_colWindow.opener = window;
    o_colWindow.focus();
}

function TCPopup(palette) {
    //this.field = field;
    this.initPalette = !palette || palette > 3 ? 0 : palette;
    var w = 194, h = 240,
    move = screen ? 
        ',left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '', 
    o_colWindow = window.open('picker.html', null, "help=no,status=no,scrollbars=no,resizable=no" + move + ",width=" + w + ",height=" + h + ",dependent=yes", true);
    o_colWindow.opener = window;
    o_colWindow.focus();
}

function TCBuildCell (R, G, B, w, h) {
    return '<td bgcolor="#' + this.dec2hex((R << 16) + (G << 8) + B) + '"><a href="javascript:P.S(\'' + this.dec2hex((R << 16) + (G << 8) + B) + '\')" onmouseover="P.P(\'' + this.dec2hex((R << 16) + (G << 8) + B) + '\')"><img src="pixel.gif" width="' + w + '" height="' + h + '" border="0"></a></td>';
}

function TCSelect(c) {
         fCol= '#' + c.toUpperCase();
    //this.field.value = '#' + c.toUpperCase();
    this.win.close();
    doWork4Obj(0,'forecolor',fCol);
}

function TCSelect1(c) {
         bCol= '#' + c.toUpperCase();
    //this.field.value = '#' + c.toUpperCase();
    this.win.close();
    doWork4Obj(0,'backcolor',bCol);
}

function TCPaint(c, b_noPref) {
    c = (b_noPref ? '' : '#') + c.toUpperCase();
    if (this.o_samp) 
        this.o_samp.innerHTML = '<font face=Tahoma size=2>' + c +' <font color=white>' + c + '</font></font>'
    if(this.doc.layers)
        this.sample.bgColor = c;
    else { 
        if (this.sample.backgroundColor != null) this.sample.backgroundColor = c;
        else if (this.sample.background != null) this.sample.background = c;
    }
}

function TCGenerateSafe() {
    var s = '';
    for (j = 0; j < 12; j ++) {
        s += "<tr>";
        for (k = 0; k < 3; k ++)
            for (i = 0; i <= 5; i ++)
                s += this.bldCell(k * 51 + (j % 2) * 51 * 3, Math.floor(j / 2) * 51, i * 51, 8, 10);
        s += "</tr>";
    }
    return s;
}

function TCGenerateWind() {
    var s = '';
    for (j = 0; j < 12; j ++) {
        s += "<tr>";
        for (k = 0; k < 3; k ++)
            for (i = 0; i <= 5; i++)
                s += this.bldCell(i * 51, k * 51 + (j % 2) * 51 * 3, Math.floor(j / 2) * 51, 8, 10);
        s += "</tr>";
    }
    return s    
}
function TCGenerateMac() {
    var s = '';
    var c = 0,n = 1;
    var r,g,b;
    for (j = 0; j < 15; j ++) {
        s += "<tr>";
        for (k = 0; k < 3; k ++)
            for (i = 0; i <= 5; i++){
                if(j<12){
                s += this.bldCell( 255-(Math.floor(j / 2) * 51), 255-(k * 51 + (j % 2) * 51 * 3),255-(i * 51), 8, 10);
                }else{
                    if(n<=14){
                        r = 255-(n * 17);
                        g=b=0;
                    }else if(n>14 && n<=28){
                        g = 255-((n-14) * 17);
                        r=b=0;
                    }else if(n>28 && n<=42){
                        b = 255-((n-28) * 17);
                        r=g=0;
                    }else{
                        r=g=b=255-((n-42) * 17);
                    }
                    s += this.bldCell( r, g,b, 8, 10);
                    n++;
                }
            }
        s += "</tr>";
    }
    return s;
}

function TCGenerateGray() {
    var s = '';
    for (j = 0; j <= 15; j ++) {
        s += "<tr>";
        for (k = 0; k <= 15; k ++) {
            g = Math.floor((k + j * 16) % 256);
            s += this.bldCell(g, g, g, 9, 7);
        }
        s += '</tr>';
    }
    return s
}

function TCDec2Hex(v) {
    v = v.toString(16);
    for(; v.length < 6; v = '0' + v);
    return v;
}

function TCChgMode(v) {
    for (var k in this.divs) this.hide(k);
    this.show(v);
}

function TColorPicker() {
    this.build0 = TCGenerateSafe;
    this.build1 = TCGenerateWind;
    this.build2 = TCGenerateGray;
    this.build3 = TCGenerateMac;
    this.show = document.layers ? 
        function (div) { this.divs[div].visibility = 'show' } :
        function (div) { this.divs[div].visibility = 'visible' };
    this.hide = document.layers ? 
        function (div) { this.divs[div].visibility = 'hide' } :
        function (div) { this.divs[div].visibility = 'hidden' };
    // event handlers
    this.C       = TCChgMode;
    this.S       = TCSelect;
    this.P       = TCPaint;
    this.popup   = TCPopup;
    this.draw    = TCDraw;
    this.dec2hex = TCDec2Hex;
    this.bldCell = TCBuildCell;
    this.divs = [];
}

function TColorPicker1() {
    this.build0 = TCGenerateSafe;
    this.build1 = TCGenerateWind;
    this.build2 = TCGenerateGray;
    this.build3 = TCGenerateMac;
    this.show = document.layers ? 
        function (div) { this.divs[div].visibility = 'show' } :
        function (div) { this.divs[div].visibility = 'visible' };
    this.hide = document.layers ? 
        function (div) { this.divs[div].visibility = 'hide' } :
        function (div) { this.divs[div].visibility = 'hidden' };
    // event handlers
    this.C       = TCChgMode;
    this.S       = TCSelect1;
    this.P       = TCPaint;
    this.popup   = TCPopup1;
    this.draw    = TCDraw;
    this.dec2hex = TCDec2Hex;
    this.bldCell = TCBuildCell;
    this.divs = [];
}

function TCDraw(o_win, o_doc) {
    this.win = o_win;
    this.doc = o_doc;
    var 
    s_tag_openT  = o_doc.layers ? 
        'layer visibility=hidden top=54 left=5 width=182' : 
        'div style=visibility:hidden;position:absolute;left:6px;top:54px;width:182px;height:0',
    s_tag_openS  = o_doc.layers ? 'layer top=32 left=6' : 'div',
    s_tag_close  = o_doc.layers ? 'layer' : 'div'
        
    this.doc.write('<' + s_tag_openS + ' id=sam name=sam><table cellpadding=0 cellspacing=0 border=1 width=181 align=center class=bd><tr><td align=center height=18><div id="samp"><font face=Tahoma size=2>sample <font color=white>sample</font></font></div></td></tr></table></' + s_tag_close + '>');
    this.sample = o_doc.layers ? o_doc.layers['sam'] : 
        o_doc.getElementById ? o_doc.getElementById('sam').style : o_doc.all['sam'].style

    for (var k = 0; k < 4; k ++) {
        this.doc.write('<' + s_tag_openT + ' id="p' + k + '" name="p' + k + '"><table cellpadding=0 cellspacing=0 border=1 align=center>' + this['build' + k]() + '</table></' + s_tag_close + '>');
        this.divs[k] = o_doc.layers 
            ? o_doc.layers['p' + k] : o_doc.all 
                ? o_doc.all['p' + k].style : o_doc.getElementById('p' + k).style
    }
    if (!o_doc.layers && o_doc.body.innerHTML) 
        this.o_samp = o_doc.all 
            ? o_doc.all.samp : o_doc.getElementById('samp');
    this.C(this.initPalette);
    //if (this.field.value) this.P(this.field.value, true)
}

