﻿var sInitColor = null;

function cr() {

    if (window.event.keyCode == 13) {

        e = event.srcElement.id;
        if (isRTE(e)) {
            document.getElementById(e).focus();
            sel = document.selection.createRange();
            sel.pasteHTML("<br/><newline/>");
            return false;
        }
    }
}


function RichTextEditor(uniqueID, divID) {
    this.ID = uniqueID;
    this.DivID = divID;
}

RichTextEditor.prototype.format = format;

function GetEditor(current) {
try{
    var thisControl = current.parentNode.parentNode.parentNode;
    }catch(ex){}
    
    CMSTextBox = $(".CMSTextBox", thisControl);
    CMSDiv = $(".CMSDiv", thisControl);

    return;


}
function isRTE() {
    for (i = 0; i < objects.length; i++) {
        if (objects[i].DivID == e)
            return true;
    }
    return false;
}

function copyContent() {
    var newContent = CMSDiv.html();
    CMSTextBox.text(newContent);
}

function callFormatting(str, current) {
    try {
        GetEditor(current);
        
        //document.getElementById(ed.DivID).focus();
        CMSDiv[0].focus();
        document.execCommand(str);
        //ed.format(str);
    } catch (e) {
        alert("Unable to preform format operation");
    }
    return false;
}

function format(str) {
    document.execCommand(str);
}

function currentEditor(current) {
    //Retrieve the actual ContactList id using the HTML button received.
    current = current.parentNode.parentNode.parentNode.parentNode.id;
    //current = document.getElementById("contentManagementTextBox").id;
    var i;
    for (i = 0; i < objects.length; i++)
        if (objects[i].UniqueId == current)
            return objects[i];
}

function setFont(val, e) {
    document.execCommand('fontname', false, val);
}
function setFontSize(val, e) {
    document.execCommand('fontsize', false, val);
}
function setHeader(val) {
    document.execCommand('formatblock', false, val);
}

function setStyle(value) {
    document.execCommand('removeFormat');
    document.execCommand('formatBlock', '', 'Normal');
    s = document.selection.createRange();

    html = '<font class=\'' + value + '\'>' + s.htmlText + '</font>';
    s.pasteHTML(html);
}
function makeLink(current) {
    GetEditor(current);
    
    var szURL = prompt("Enter the URL", "http://");

    document.execCommand('createlink', false, szURL);
    return false;
}
function selectStyle(e) {
    GetEditor(e);
    document.getElementById(ed.DivID).focus();
    index = document.getElementById(ed.ID + "_cssSelect").selectedIndex;
    setStyle(document.getElementById(ed.ID + "_cssSelect").options[index].text);
}

function insertTime(e) {
    var d = new Date();
    insertString(d.toLocaleTimeString(), e);
}
function insertDate(e) {
    var d = new Date();
    insertString(d.toLocaleDateString() + " " + d.toLocaleTimeString(), e);
}

function insertString(val, e) {
    
    GetEditor(e);
    
    //document.getElementById(ed.DivID).focus();
    CMSDiv[0].focus();
    alert("W4");
    sel = document.selection.createRange();
    alert(val);
    sel.pasteHTML(val);
    return false;
}

function insertTable(e, path) {
    var arr = new Array(window, e);
    showModalDialog(path + "insert_table.html", arr,
                                 "resizable: yes; help: no; status: no; scroll: no; ");
    return false;
}
function insertImage(e, path) {
    var arr = new Array(window, e);
    showModalDialog(path + "insert_image.aspx", arr,
                                 "resizable: yes; help: no; status: no; scroll: no; ");
    return false;
}
function about(path) {
    showModalDialog(path + "about.html", window,
                                 "resizable: no; help: no; status: no; scroll: no; ");
    return false;
}

function changeForeColour() {

    //if sInitColor is null, the color dialog box has not yet been called
    if (sInitColor == null)
        var sColor = document.dlgHelper.ChooseColorDlg();
    else
    //call the dialog box and initialize the color to the color previously chosen
        var sColor = document.dlgHelper.ChooseColorDlg(sInitColor);
    //change the return value from a decimal value to a hex value and make sure the value has 6
    //digits to represent the RRGGBB schema required by the color table

    sColor = sColor.toString(16);
    if (sColor.length < 6) {
        var sTempString = "000000".substring(0, 6 - sColor.length);
        sColor = sTempString.concat(sColor);
    }

    document.execCommand("ForeColor", false, sColor);
    //set the initialization color to the color chosen</FONT>
    sInitColor = sColor;

}

function changeBackColour(e) {
    GetEditor(e);
    //if sInitColor is null, the color dialog box has not yet been called
    if (sInitColor == null)
        var sColor = document.dlgHelper.ChooseColorDlg();
    else
    //call the dialog box and initialize the color to the color previously chosen
        var sColor = document.dlgHelper.ChooseColorDlg(sInitColor);
    //change the return value from a decimal value to a hex value and make sure the value has 6
    //digits to represent the RRGGBB schema required by the color table

    sColor = sColor.toString(16);
    if (sColor.length < 6) {
        var sTempString = "000000".substring(0, 6 - sColor.length);
        sColor = sTempString.concat(sColor);
    }

    document.execCommand('BackColor', true, sColor);

    //  //set the initialization color to the color chosen</FONT>
    sInitColor = sColor;
}
function showDesign(e) {
    GetEditor(e);
    CMSDiv.html(CMSTextBox.val());
    CMSTextBox.css("display", "none");
    CMSDiv.css("display", "block");
    return false;
}

function showHTML(e) {
    GetEditor(e);
    CMSTextBox.css("height", CMSDiv[0].clientHeight - 5);
    CMSTextBox.css("width", CMSDiv[0].clientWidth - 4);
    CMSTextBox.val(CMSDiv.html());
    CMSTextBox.css("display", "block");
    CMSDiv.css("display", "none");
    return false;
}
function saveContent(contentId, context) {
    GetEditor(context);
    copyContent(context);
    var content = CMSTextBox.html();
    $.ajax({type:'POST',
        url: "ajax/content_update.aspx", data:"name=" + contentId + "&content=" + escape(content),
        success: function () { alert("saved"); },
        error: function () { alert("Excetion"); }
    });

    return false;
}
