window.onload = function()
{
    editFrame.document.designMode = "on";  
}

var popDivId;
var htmlContent;
var indentValue = 30;
var colorArray = new Array(8);
colorArray[0] = new Array("#cc66cc","#cc99cc","#cccccc","#ccffcc","#ccff33","#cccc33","@cc9933","#cc3333");
colorArray[1] = new Array("#006699","#009999","#00cc99","#00ff99","#00ff00","#00cc00","#009900","#006600");
colorArray[2] = new Array("#ff00ff","#ff33ff","#ff66ff","#ff99ff","#ffccff","#ffffff","#ffff66","#ffcc66");
colorArray[3] = new Array("#0066cc","#0099cc","#00cccc","#00ffcc","#00ff33","#00cc33","#009933","#006633");
colorArray[4] = new Array("#40e0d0","#556b2f","#708090","#00008b","#191970","#4b0082","#8b008b","#b8860b");
colorArray[5] = new Array("#98fb98","#7888f4","#7fffd4","#87cefa","#dda0dd","#ffc0cb","#f5deb3","#ffefd3");
colorArray[6] = new Array("#ba55d3","#fa8072","#e0ffff","#cd5c5c","#dc143c","#ffe4c4","#f8f8ff","#a9a9a9");
colorArray[7] = new Array("#8a2be2","#9932cc","#bc8f8f","#6495ed","#48d1cc","#2e8b57","#5f9ea0","#afeeee");

function showColorDiv(colorTo)
{
    popDivId = document.getElementById('popDiv');
    if(popDivId)
    {
        document.body.removeChild(popDivId);
    }
    popDivId = document.createElement('div');
    popDivId.id = "popDiv";
    var tableStr = "<table cellspacing='0' bgcolor='#99ccff' cellpadding='0' width='120' height='120'>";
    tableStr += "<tr><td colspan='8' align='right'><img style='cursor:hand' src='editorimages/close.gif' unselectable='on' onclick=\"popDivId.style.display='none'\" /></td></tr>";
    for(i=0;i<8;i++)
    {   
        tableStr += "<tr>";
        for(j=0;j<8;j++)
        {
            tableStr += "<td class='colortd' id='td" + i + "_" + j + "' width='15' height='15' unselectable='on' onclick=setColor('"+ colorTo +"',"+ i + "," + j + ") style='background-color:"+ colorArray[i][j] +";'>&nbsp;</td>";
        }
        tableStr += "</tr>";
    }
    tableStr += "</table>";
    popDivId.className = "innerDivStyle";
    popDivId.style.top = window.event.clientY+10;
    popDivId.style.left = window.event.clientX-10;
    popDivId.style.position = "absolute";
    popDivId.innerHTML = tableStr;
    document.body.appendChild(popDivId);
}

function doActionWithValues(command,value1,value2)
{
    editFrame.focus();
    editFrame.document.execCommand(command,value1,value2);
}

function setColor(colorTo,x,y)
{
    var sText = editFrame.document.selection.createRange();
    if (sText.text != "")
    {
        sText.execCommand(colorTo,true, colorArray[x][y]);
    }
    else
    {
        editFrame.document.execCommand(colorTo,true, colorArray[x][y]);
    }

    editFrame.document.execCommand(colorTo,true, colorArray[x][y]);
    popDivId.style.display = "none";
}

function showFontDiv()
{
    popDivId = document.getElementById('popDiv');
    if(popDivId)
    {
        document.body.removeChild(popDivId);
    }
    popDivId = document.createElement('div');
    popDivId.id = "popDiv";
    var tablestr = "<table width='125' bgcolor='#99ccff'>";
    tablestr += "<tr><td align='right'><img style='cursor:hand' src='editorimages/close.gif' onclick=\"popDivId.style.display='none'\" /></td></tr>";
    tablestr += "<tr><td class='arialtd' unselectable='on' onclick=\"setFont('Arial')\">Arial</td></tr>";
    tablestr += "<tr><td class='tmrtd' unselectable='on' onclick=\"setFont('Times New Roman')\">Times New Roman</td></tr>";
    tablestr += "<tr><td class='verdanatd' unselectable='on' onclick=\"setFont('Verdana')\">Verdana</td></tr>";
    tablestr += "<tr><td class='comictd' unselectable='on' onclick=\"setFont('Comic Sans MS')\">Comic Sans MS</td></tr>";
    tablestr += "<tr><td class='cnewtd' unselectable='on' onclick=\"setFont('Courier New')\">Courier New</td></tr>";
    tablestr += "<tr><td class='impacttd' unselectable='on' onclick=\"setFont('Impact')\">Impact</td></tr>";
    tablestr += "<tr><td class='geotd' unselectable='on' onclick=\"setFont('Georgia')\">Georgia</td></tr>";
    tablestr += "<tr><td class='tahomatd' unselectable='on' onclick=\"setFont('Tahoma')\">Tahoma</td></tr>";
    tablestr += "</table>";
    popDivId.className = "innerDivStyle";
    popDivId.style.top = window.event.clientY+10;
    popDivId.style.left = window.event.clientX-10;
    popDivId.style.display="block";
    popDivId.style.position = "absolute";
    popDivId.innerHTML = tablestr;
    document.body.appendChild(popDivId);
}

function setFont(fontName)
{

    var sText = editFrame.document.selection.createRange();
    if (sText.text != "")
    {
        sText.execCommand("FontName", false, fontName);
    }
    else
    {
        editFrame.document.execCommand("FontName", false, fontName);
    }

    popDivId.style.display = "none";

}

function showFontSizeDiv()
{
    popDivId = document.getElementById("popDiv");
    if(popDivId)
    {
       document.body.removeChild(popDivId);
    }
    popDivId = document.createElement('div');
    popDivId.id = "popDiv";
    var tablestr = "<table width='150' bgcolor='#99ccff'>";
    tablestr += "<tr><td align='right'><img style='cursor:hand' src='editorimages/close.gif' onclick=\"popDivId.style.display='none'\" /></td></tr>";
    tablestr += "<tr><td class='smalltd' unselectable='on' onclick=\"setFontSize('1')\">Small</td></tr>";
    tablestr += "<tr><td class='mediumtd' unselectable='on' onclick=\"setFontSize('3')\">Medium</td></tr>";
    tablestr += "<tr><td class='largetd' unselectable='on' onclick=\"setFontSize('5')\">Large</td></tr>";
    tablestr += "<tr><td class='verylargetd' unselectable='on' onclick=\"setFontSize('6')\">Very Large</td></tr>";
    tablestr += "<tr><td class='xxlargetd' unselectable='on' onclick=\"setFontSize('7')\">XX-Large</td></tr>";
    tablestr += "</table>";
    popDivId.className = "innerDivStyle";
    popDivId.style.top = window.event.clientY+10;
    popDivId.style.left = window.event.clientX-10;
    popDivId.style.display="block";
    popDivId.style.position = "absolute";
    popDivId.innerHTML = tablestr;
    document.body.appendChild(popDivId);
}

function setFontSize(fontSize)
{

    var sText = editFrame.document.selection.createRange();
    if (sText.text != "")
    {
        sText.execCommand("FontSize", false, fontSize);
    }
    else
    {
        alert("Please select some text!");
    }
    popDivId.style.display = "none";
}


function showDateDiv()
{
    popDivId = document.getElementById("popDiv");
    if(popDivId)
    {
       document.body.removeChild(popDivId);
    }
    popDivId = document.createElement('div');
    popDivId.id = "popDiv";
    var tablestr = "<table width='150' bgcolor='#99ccFF'>";
    tablestr += "<tr><td align='right'><img style='cursor:hand' src='editorimages/close.gif' onclick=\"popDivId.style.display='none'\" /></td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setDate('1')\">dd/mm/yyyy</td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setDate('2')\">mm/dd/yyyy</td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setDate('3')\">MMMM dd, yyyy</td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setDate('4')\">ddd MMMM dd, yyyy</td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setDate('5')\">dddd MMMM dd, yyyy</td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setDate('6')\">ddd MMM dd, yyyy</td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setDate('7')\">MMMM dd</td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setDate('8')\">dd MMMM</td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setDate('9')\">MMMM, yyyy</td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setDate('10')\">yyyy-mm-dd</td></tr>";
    tablestr += "</table>";
    popDivId.className = "innerDivStyle";
    popDivId.style.top = window.event.clientY+10;
    popDivId.style.left = window.event.clientX-10;
    popDivId.style.display="block";
    popDivId.style.position = "absolute";
    popDivId.innerHTML = tablestr;
    document.body.appendChild(popDivId);
}

function setDate(val)
{

    var selectedRegion = editFrame.document.selection.createRange();
    var now = new Date();
    var dateArr = now.toString().split(" ");
    var weekday = dateArr[0];
    var weekdayStr;
    switch(weekday)
    {
        case "Sun": weekdayStr = "Sunday"; break;
        case "Mon": weekdayStr = "Monday"; break;
        case "Tue": weekdayStr = "Tuesday"; break;
        case "Wed": weekdayStr = "Wednesday"; break;
        case "Thu": weekdayStr = "Thursday"; break;
        case "Fri": weekdayStr = "Friday"; break;
        case "Sat": weekdayStr = "Saturday"; break;
    }
    var month = dateArr[1];
    var monthStr;
    var monthNumStr;
    switch(month)
    {
        case "Jan": monthStr="January";  monthNumStr="01"; break;
        case "Feb": monthStr="February"; monthNumStr="02"; break;
        case "Mar": monthStr="March"; monthNumStr="03"; break;
        case "Apr": monthStr="April"; monthNumStr="04"; break;
        case "May": monthStr="May"; monthNumStr="05"; break;
        case "Jun": monthStr="June"; monthNumStr="06"; break;
        case "Jul": monthStr="July"; monthNumStr="07"; break;
        case "Aug": monthStr="August"; monthNumStr="08"; break;
        case "Sep": monthStr="September"; monthNumStr="09"; break;
        case "Oct": monthStr="October"; monthNumStr="10"; break;
        case "Nov": monthStr="November"; monthNumStr="11"; break;
        case "Dec": monthStr="December"; monthNumStr="12"; break;
    }

    var dateStr = dateArr[2];
    if(parseInt(dateStr) <10)
        dateStr = "0" + dateStr;
    var yearStr = dateArr[5];
    var resultStr="";
    switch(parseInt(val))
    {
        case 1:
            resultStr = dateStr + "/" + monthNumStr + "/" + yearStr;
            break;
        case 2:
            resultStr = monthNumStr + "/" + dateStr + "/" + yearStr;
            break;
        case 3:
            resultStr = monthStr + " " + dateStr + ", " + yearStr;
            break;
        case 4:
            resultStr = weekday + " " + monthStr + " " + dateStr + ", " + yearStr;
            break;
        case 5:
            resultStr = weekdayStr + " " + monthStr + " " + dateStr + ", " + yearStr;
            break;
        case 6:
            resultStr = weekday + " " + month + " " + dateStr + ", " + yearStr;
            break;
        case 7:
            resultStr = monthStr + " " + dateStr;
            break;
        case 8:
            resultStr = dateStr + " " + monthStr;
            break;
        case 9:
            resultStr = monthStr + ", " + yearStr;
            break;
        case 10:
             resultStr = yearStr + "-" + monthNumStr + "-" + dateStr;        
            break;
    }
    selectedRegion.pasteHTML(resultStr);
    popDivId.style.display = "none";
}

function showTimeDiv()
{
    popDivId = document.getElementById("popDiv");
    if(popDivId)
    {
       document.body.removeChild(popDivId);
    }
    popDivId = document.createElement('div');
    popDivId.id = "popDiv";
    var tablestr = "<table width='150' bgcolor='#99ccff'>";
    tablestr += "<tr><td align='right'><img style='cursor:hand' src='editorimages/close.gif' onclick=\"popDivId.style.display='none'\" /></td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setTime('1')\">hh:mm tt</td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setTime('2')\">hh:mm:ss tt</td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setTime('3')\">HH:mm</td></tr>";
    tablestr += "<tr><td class='txtstyle' unselectable='on' onclick=\"setTime('4')\">HH:mm:ss</td></tr>";
    tablestr += "</table>";
    popDivId.className = "innerDivStyle";
    popDivId.style.top = window.event.clientY+10;
    popDivId.style.left = window.event.clientX-10;
    popDivId.style.display="block";
    popDivId.style.position = "absolute";
    popDivId.innerHTML = tablestr;
    document.body.appendChild(popDivId);
}

function setTime(val)
{

    var selectedRegion = editFrame.document.selection.createRange();
    var now = new Date();
    var dateArr = now.toString().split(" ");
    var timeStr = dateArr[3].split(":");
    var hourStr = timeStr[0];
    var hourStr12 = hourStr;
    var hourInt = parseInt(hourStr);
    var apm = "AM";
    if(hourInt >= 12)
    {
        apm = "PM";
        if(hourInt >12)
        {
            hourInt = hourInt - 12;
            hourStr = hourInt.toString();
            if(hourInt < 10)
                hourStr = "0" + hourStr; 
        }
    }
    var minStr = timeStr[1];
    var secStr = timeStr[2];
    var resultStr="";
    switch(parseInt(val))
    {
        case 1:
            resultStr = hourStr + ":" + minStr + " " + apm;        
            break;
        case 2:
            resultStr = hourStr + ":" + minStr + ":" + secStr + " " + apm;
            break;
        case 3:
            resultStr = hourStr12 + ":" + minStr;  
            break;
        case 4:
            resultStr = hourStr12 + ":" + minStr + ":" + secStr;
            break;
    }
    selectedRegion.pasteHTML(resultStr);
    popDivId.style.display = "none";
    
}


function doAction(action)
{
    editFrame.focus();
    if(action == "ClearAll")
    {
        editFrame.document.execCommand("SelectAll",true);
        action = "Delete";
    }
    editFrame.document.execCommand(action,true);
} 

function showDialog(link,w, h)//380, 125
{
    if(link == "li")
        link = "InsertLinks.htm";
    else if(link == "sc")
        link = "SpecialChars.htm";
    var returnedTxt= showModalDialog(link,"", "dialogWidth:"+ w +"px; dialogHeight:"+ h +"px; status:no; unselectable='on'; center:yes");
    editFrame.focus();
    if(returnedTxt)
    {
        var theRange = editFrame.document.selection.createRange();
        theRange.pasteHTML(returnedTxt);
    }
}

var cWnd;
function showInsertPopup(opt, w, h) // w=350, h=340
{
    var srcfile = "imgWindow.php";
    if(opt == "im")
        srcfile = "imgWindow.php";
    else if(opt == "fl")
        srcfile = "flashWindow.php";
    else if(opt == "me")
        srcfile = "mediaWindow.php";
    else if(opt == "tm")
        srcfile = "templateWindow.php";
    else if(opt == "st")
        srcfile = "saveWindow.php";
    else if(opt == "tb")
        srcfile = "InsertTable.htm";
    var left = (window.screen.width - parseInt(w))/2;
    var top = (window.screen.height - parseInt(h))/2;
    if(cWnd)
        cWnd.close();
    cWnd = window.open(srcfile,"", "width="+ w +",height="+ h +",location=no, status=no, top="+ top +", left="+ left);
}

function doHtmlAction(atype)
{
    editFrame.focus();
    var selectedRegion = editFrame.document.selection.createRange();
    if(atype == "hr")
        selectedRegion.pasteHTML("<hr width='100%' size='1' />");
    else if(atype == "lower")
        selectedRegion.text = selectedRegion.text.toLowerCase();
    else if(atype == "upper")
        selectedRegion.text = selectedRegion.text.toUpperCase();
}

function setZoom(zoomSize)
{
    var zoomValue = editFrame.document.body.style.zoom;
    if(zoomValue)
    {
        var zoomInt = parseInt(zoomValue);
        if(zoomSize == "p")
        {
            if(zoomInt <= 4)
                editFrame.document.body.style.zoom = zoomInt + 1;
        }
        if(zoomSize == "m")
        {
            if(zoomInt >= 1)
            editFrame.document.body.style.zoom = zoomInt - 1;
        }
    }
    else
    {
        editFrame.document.body.style.zoom = 2;
    }
}

function showPreview(atype)
{
    htmlContent = editFrame.document.body.innerHTML;
    if(atype == "s")
    {
        document.getElementById('textContent').value = htmlContent;
        document.getElementById('frameDiv').style.display = "none";
        document.getElementById('txtDiv').style.display = "block";
        document.getElementById('btnDesign').removeAttribute("disabled");
        document.getElementById('btnOutput').removeAttribute("disabled");
        document.getElementById('btnPreview').disabled = "disabled";
    }
    else if(atype == "d")
    {
        var txtcontent = document.getElementById("textContent")
        if(txtcontent)
        {   
            htmlContent = txtcontent.value;      
            document.getElementById('frameDiv').style.display = "block";
            document.getElementById('txtDiv').style.display = "none";
            document.getElementById('btnDesign').disabled = "disabled";
            document.getElementById('btnOutput').removeAttribute("disabled");
            document.getElementById('btnPreview').removeAttribute("disabled");
            editFrame.document.body.innerHTML = htmlContent;
        }
    }
    else if(atype == "o")
    {
        var win = window.open("","");
        win.document.write(htmlContent);
    }
}
