﻿var mainWindow;
var colourPanel;
var vals = new Array('00', '33', '66', '99', 'CC', 'FF');
var validChars = "0123456789ABCDEFabcdef";
var pattern = "<(.|\n)*?>";
var reHTML = new RegExp(/<(.|\n)*?>/);

window.onload = function() {
    if (document.getElementById('hfLogin').value == "true") {
        mainWindow = document.getElementById('mainTextArea').contentWindow;
        colourPanel = document.getElementById('colourPanel');
        for (var i = 0; i < 6; i++) {
            for (var j = 0; j < 6; j++) {
                for (var k = 0; k < 6; k++) {
                    var hash = "#" + vals[i] + vals[j] + vals[k];
                    var colourDiv = document.createElement("input");
                    colourDiv.setAttribute("id", hash);
                    colourDiv.setAttribute("type", "button");
                    colourDiv.setAttribute("value", "");
                    colourDiv.setAttribute("class", "colourDiv");
                    colourDiv.setAttribute("onmouseover", "setMouseOverColour(this.id)");
                    colourDiv.setAttribute("onclick", "setSelectedColour(this.id)");
                    colourDiv.style.background = "none";
                    colourDiv.style.backgroundColor = hash;
                    colourPanel.appendChild(colourDiv);
                }
            }
        }
        for (var i = 5; i > 0; i--) {
            var hash = "#" + vals[i] + vals[i] + vals[i];
            var colourDiv = document.createElement("input");
            colourDiv.setAttribute("id", hash);
            colourDiv.setAttribute("type", "button");
            colourDiv.setAttribute("value", "");
            colourDiv.setAttribute("class", "colourDiv");
            colourDiv.setAttribute("onmouseover", "setMouseOverColour(this.id)");
            colourDiv.setAttribute("onclick", "setSelectedColour(this.id)");
            colourDiv.style.background = "none";
            colourDiv.style.backgroundColor = hash;
            colourPanel.appendChild(colourDiv);
        }
        var tempArray = document.getElementById('hfURLs').value.split("^^");
        for (var i = 0; i < tempArray.length; i++) {
            if (tempArray[i].length > 12) {
                var elem = document.createElement("input");
                elem.setAttribute("type", "button");
                elem.setAttribute("value", tempArray[i]);
                elem.setAttribute("class", "dropDownPickerVLarge");
                elem.setAttribute("onclick", "setExisting('" + tempArray[i] + "')");
                document.getElementById('existingContainer').appendChild(elem);
            }
        }
    }
    else {
        alert("Session has expired, please login again");
        parent.window.location.href = "http://www.locallinkup.com";
    }
}

function setText(strText) {
    mainWindow.document.body.innerHTML = strText;
}

function closeWin() {
    parent.hideModalWindow('textEditModal');
}

function save() {
    var text = "";
    mainWindow.focus();
    if (document.getElementById('hfIsIE').value == "true") {
        text = mainWindow.document.body.createTextRange().text;
    }
    else {
        doRichEditCommand("selectall", "")
        text = mainWindow.getSelection();
    }
    if (detectHTML(text)) {
        alert("HTML detected");
    }
    else {
        var htmlText = mainWindow.document.body.innerHTML.toString();
        if (htmlText.indexOf("<p>") == 0 || htmlText.indexOf("<P>") == 0) {
            htmlText = htmlText.substring(htmlText.indexOf(">") + 1)
            htmlText = htmlText.substring(0, htmlText.lastIndexOf("<"))
        }
        parent.closeTextEditSetText(htmlText);
    }
}

function detectHTML(text) {
    var result = false;
    if (reHTML.test(text)) {
        result = true;
    }
    return result;
}

function doRichEditCommand(aName, aArg) {
    mainWindow.focus();
    if (aName == "fontsize") {
        showDropDown("fontSizeContainer", "");
    }
    if (aName == "fontname") {
        showDropDown("fontFamilyContainer", "");
    }
    if (aName == "formatblock") {
        showDropDown("formatContainer", "");
    }
    getIFrameDocument('mainTextArea').execCommand(aName, false, aArg);
    mainWindow.focus();
}

function getIFrameDocument(id) {
    var frame = document.getElementById(id);
    var doc = frame.contentDocument;
    if (doc == undefined) // Internet Explorer
        doc = frame.contentWindow.document;
    return doc;
}

function iconRollOver(element, onOff, size) {
    if (onOff == "on") {
        if (element.className == "toolBlock24") {
            element.style.backgroundImage = "url(http://image.locallinkup.com/console/rover_" + size + ".png)";
        }
        if (element.className == "dropDown") {
            element.style.backgroundImage = "url(http://image.locallinkup.com/console/dropdown_on.png)";
        }
    }
    else {
        if (element.className == "toolBlock24") {
            element.style.backgroundImage = "none";
        }
        if (element.className == "dropDown") {
            element.style.backgroundImage = "url(http://image.locallinkup.com/console/dropdown_off.png)";
        }
    }
}

var colourItem = "";

function showDropDown(panelID, colourID) {
    var element = document.getElementById(panelID);
    if (panelID == "colourChooser") {
        if (colourItem != colourID && element.style.display == "inline") {
            element.style.display = "none";
        }
        colourItem = colourID;
    }
    var show = false;
    if (element.style.display == "none" || element.style.display == "") {
        show = true;
    }
    document.getElementById('fontFamilyContainer').style.display = "none";
    document.getElementById('fontSizeContainer').style.display = "none";
    document.getElementById('formatContainer').style.display = "none";
    document.getElementById('linkBuilder').style.display = "none";
    document.getElementById('colourChooser').style.display = "none";
    document.getElementById('insertSymbol').style.display = "none";
    if (show) {
        element.style.display = "inline";
        if (panelID == "linkBuilder") {
            document.getElementById('linkText').value = getSelText();
        }
    }
}

function setSelectedColour(hash) {
    document.getElementById('selectedColour').style.backgroundColor = hash;
    document.getElementById('selectedHash').value = hash;
}

function setElementColour() {
    if (colourItem != "") {
        var hash = document.getElementById('selectedColour').style.backgroundColor;
        if (colourItem == "font") {
            doRichEditCommand('forecolor', hash);
        }
        if (colourItem == "back") {
            doRichEditCommand('backcolor', hash);
        }
        showDropDown("colourChooser", colourItem);
    }
}

function setMouseOverColour(hash) {
    document.getElementById('hoverColour').style.backgroundColor = hash;
    document.getElementById('hoverHash').value = hash;
}

function setHashColour(element) {
    var strText = element.value;
    if (strText.length == 7) {
        if (strText.charAt(0) == "#") {
            if (validateHash(strText.replace("#", ""))) {
                setSelectedColour(strText);
            }
        }
    }
}

function validateHash(strText) {
    var valid = true;
    var Char;
    for (i = 0; i < strText.length && valid == true; i++) {
        Char = strText.charAt(i);
        if (validChars.indexOf(Char) == -1) {
            valid = false;
        }
    }
    return valid;
}

var linkType = "url";

function setLinkType(element) {
    if (element.style.backgroundImage.indexOf("selected") < 0) {
        if (element.id == "linkButton") {
            linkType = "url";
            document.getElementById('linkAddressLabel').innerHTML = "URL";
        }
        else {
            linkType = "mail";
            document.getElementById('linkAddressLabel').innerHTML = "Email";
        }
        document.getElementById('existingContainer').innerHTML = "";
        var tempArray = "";
        var tempArray2 = "";
        var tempArray3 = "";
        if (linkType == "url") {
            tempArray = document.getElementById('hfURLs').value.split("^^");
        }
        if (linkType == "mail") {
            tempArray = document.getElementById('hfEmails').value.split("^^");
        }
        for (var i = 0; i < tempArray.length; i++) {
            if (tempArray[i].length > 12) {
                var elem = document.createElement("input");
                elem.setAttribute("type", "button");
                elem.setAttribute("value", tempArray[i]);
                elem.setAttribute("class", "dropDownPickerVLarge");
                elem.setAttribute("onclick", "setExisting('" + tempArray[i] + "')");
                document.getElementById('existingContainer').appendChild(elem);
            }
        }
        document.getElementById('linkAddress').value = "";
    }
}

function showTargetDropDown() {
    if (document.getElementById('existingContainer').style.display == "inline") {
        document.getElementById('existingContainer').style.display = "none"
    }
    if (document.getElementById('targetContainer').style.display == "inline") {
        document.getElementById('targetContainer').style.display = "none"
    }
    else {
        document.getElementById('targetContainer').style.display = "inline"
    }
}

var linkTarget = "_blank";

function setTarget(element, type) {
    linkTarget = type;
    document.getElementById('target').innerHTML = element.value;
    showTargetDropDown();
}

function showExistingDropDown() {
    if (document.getElementById('targetContainer').style.display == "inline") {
        document.getElementById('targetContainer').style.display = "none"
    }
    if (document.getElementById('existingContainer').style.display == "inline") {
        document.getElementById('existingContainer').style.display = "none"
    }
    else {
        document.getElementById('existingContainer').style.display = "inline"
    }
}

var linkAddress = "";

function setExisting(addr) {
    if (linkType == "mail") {
        linkAddress = "mailto:" + addr;
    }
    else {
        linkAddress = addr;
    }
    document.getElementById('linkAddress').value = addr;
    showExistingDropDown();
}

var index = 0;
var selectedText = "";
var selectedTextLength = 0;

function getSelText() {
    var txt = '';
    mainWindow.focus();
    if (document.getElementById('hfIsIE').value == "true") {
        txt = mainWindow.document.selection.createRange().text;
        var tr = mainWindow.document.body.createTextRange();
        if (txt != "") {
            tr.setEndPoint("EndToStart", mainWindow.document.selection.createRange());
            index = tr.text.length;
        }
        else {
            var cur = mainWindow.document.selection.createRange();
            var pos = 0;
            while (cur.compareEndPoints("EndToStart", tr) > 0) {
                tr.moveStart("character", 1);
                pos++;
            }
            index = pos;
        }
    }
    else {
        var sel = mainWindow.getSelection();
        var range = sel.getRangeAt(0);
        index = range.startOffset
        txt = sel;
    }
    selectedText = txt;
    selectedTextLength = txt.length;
    return txt;
}

function addLink() {
    setCursorPosition(index, index + selectedTextLength);
    if (document.getElementById('linkText').value != "" && linkAddress != "") {
        if (selectedText == "") {
            if (document.getElementById('hfIsIE').value == "true") {
                sel = mainWindow.document.selection.createRange();
                sel.text = document.getElementById('linkText').value;
                setCursorPosition(index, index + document.getElementById('linkText').value.length);
                doRichEditCommand('createlink', linkAddress);
            }
            else {
                var newLinkText = "<a href=\"" + linkAddress + "\" alt=\"\">" + document.getElementById('linkText').value + "</a>";
                doRichEditCommand("inserthtml", newLinkText);
            }
        }
        else {
            doRichEditCommand('createlink', linkAddress);
        }

        document.getElementById('linkText').value = "";
        document.getElementById('linkAddress').value = "";
        linkAddress = "";
        showDropDown("linkBuilder", "");
    }
}

function setCursorPosition(oStart, oEnd) {
    mainWindow.focus();
    if (document.getElementById('hfIsIE').value == "true") {
        var range = mainWindow.document.body.createTextRange();
        range.collapse(true);
        range.moveEnd('character', oEnd);
        range.moveStart('character', oStart);
        range.select();
    }
    else {
        if (mainWindow.setSelectionRange) {
            mainWindow.setSelectionRange(oStart, oEnd);
        }
    }
}

function addSymbol(element) {
    mainWindow.focus();
    if (document.getElementById('hfIsIE').value == "true") {
        sel = mainWindow.document.selection.createRange();
        sel.text = element.value;
    }
    else {
        doRichEditCommand("inserthtml", element.value);
    }
    showDropDown("insertSymbol", "");
}
