﻿
var isHTMLMode = false  
var theEditor=null;
var theEditorDocument=null;
var thisTextRange=null;
//window.onload=defineEditor;
function defineEditor(src,prefix) {
	theEditor=src;
	theEditorDocument =theEditor.document?theEditor.contentWindow.document:theEditor.contentDocument;
	theEditorDocument.designMode = "On"; 
	//theEditorDocument.onclick = alert("boo");
	if (theEditorDocument.attachEvent){
        theEditorDocument.attachEvent("onmouseup", function(){checkValidButtons(prefix); return true;});
        //theEditorDocument.attachEvent("onfocus", function(){alert("Validate"); return true;});
        //theEditorDocument.attachEvent("onkeydown", function(e){checkValidButtons(prefix); return true;}, false);
    }
    else
    {
        theEditorDocument.addEventListener("mouseup", function(){checkValidButtons(prefix); return true;}, false);
        //theEditorDocument.addEventListener("focus", function(){alert("Validate"); return true;}, false);
        //theEditorDocument.addEventListener("keydown", function(e){checkValidButtons(prefix); return true;}, false);
    }
	if (theEditorDocument.attachEvent){
        //document.body.attachEvent("onmouseup", function(){clearSelection(); return true;});
    }else{
        //document.body.addEventListener("mouseup", function(){clearSelection(); return true;});
    }
	//theEditorDocument.addEventListener("mouseup",function (e) { checkValidButtons()}, false ); 

	//theEditorDocument.focus(); 
} 
function cmdExec(src,prefix,cmd,opt) {
    var perform=false;
    if (src==null) {
        perform=true;
    }else{
        checkValidButtons(prefix);
        if (src.className=="buttonOn") perform=true;
    }
    if (perform) {
	    if (isHTMLMode) {
		    alert("Please uncheck 'Edit HTML'");
		    return;
	    }
	    obj=document.getElementById(prefix+"editorContent");
	    theEditorDocument =obj.document?obj.contentWindow.document:obj.contentDocument;
	    if (obj.document) {
            if (thisTextRange!=null) {
                //alert(thisTextRange.length);
                //document.frames[prefix+"editorContent"].document.setSelection();
                //theEditorDocument.setSelection(thisTextRange);
                //obj.document.setSelection(thisTextRange);
                thisTextRange.select();
                theEditorDocument.execCommand(cmd,"",opt);
            }
	    }else{
	        theEditorDocument.execCommand(cmd,"",opt);
	    }
	    //thisTextRange=null;
	    checkValidButtons(prefix);  
	}
}
function drawPallette(ForeColour,prefix) {

    if (document.getElementById("paletteSelector")==null) {
        cont=ForeColour?"forecolour":"backcolour";
        srcColor=document.getElementById(prefix+cont+"selected").style.backgroundColor;
	    output="";
	    output+="<tr><td colspan='6' id=\"selectedColour\" style=\"background-color:"+srcColor+"\">Selection</td></tr>";
	    //output+="<tr><td colspan=6 style=\"background-color:"+document.getElementById(cont+"selected").style.backgroundColor+";height:5px\" /></tr>";
	    palette=["#FFFFFF","#FFFFCC","#FFFF99","#FFFF66","#FFFF33","#FFFF00","#FFCCFF","#FFCCCC","#FFCC99","#FFCC66","#FFCC33","#FFCC00","#FF99FF","#FF99CC","#FF9999","#FF9966","#FF9933","#FF9900","#FF66FF","#FF66CC","#FF6699","#FF6666","#FF6633","#FF6600","#FF33FF","#FF33CC","#FF3399","#FF3366","#FF3333","#FF3300","#FF00FF","#FF00CC","#FF0099","#FF0066","#FF0033","#FF0000","#CCFFFF","#CCFFCC","#CCFF99","#CCFF66","#CCFF33","#CCFF00","#CCCCFF","#CCCCCC","#CCCC99","#CCCC66","#CCCC33","#CCCC00","#CC99FF","#CC99CC","#CC9999","#CC9966","#CC9933","#CC9900","#CC66FF","#CC66CC","#CC6699","#CC6666","#CC6633","#CC6600","#CC33FF","#CC33CC","#CC3399","#CC3366","#CC3333","#CC3300","#CC00FF","#CC00CC","#CC0099","#CC0066","#CC0033","#CC0000","#99FFFF","#99FFCC","#99FF99","#99FF66","#99FF33","#99FF00","#99CCFF","#99CCCC","#99CC99","#99CC66","#99CC33","#99CC00","#9999FF","#9999CC","#999999","#999966","#999933","#999900","#9966FF","#9966CC","#996699","#996666","#996633","#996600","#9933FF","#9933CC","#993399","#993366","#993333","#993300","#9900FF","#9900CC","#990099","#990066","#990033","#990000","#66FFFF","#66FFCC","#66FF99","#66FF66","#66FF33","#66FF00","#66CCFF","#66CCCC","#66CC99","#66CC66","#66CC33","#66CC00","#6699FF","#6699CC","#669999","#669966","#669933","#669900","#6666FF","#6666CC","#666699","#666666","#666633","#666600","#6633FF","#6633CC","#663399","#663366","#663333","#663300","#6600FF","#6600CC","#660099","#660066","#660033","#660000","#33FFFF","#33FFCC","#33FF99","#33FF66","#33FF33","#33FF00","#33CCFF","#33CCCC","#33CC99","#33CC66","#33CC33","#33CC00","#3399FF","#3399CC","#339999","#339966","#339933","#339900","#3366FF","#3366CC","#336699","#336666","#336633","#336600","#3333FF","#3333CC","#333399","#333366","#333333","#333300","#3300FF","#3300CC","#330099","#330066","#330033","#330000","#00FFFF","#00FFCC","#00FF99","#00FF66","#00FF33","#00FF00","#00CCFF","#00CCCC","#00CC99","#00CC66","#00CC33","#00CC00","#0099FF","#0099CC","#009999","#009966","#009933","#009900","#0066FF","#0066CC","#006699","#006666","#006633","#006600","#0033FF","#0033CC","#003399","#003366","#003333","#003300","#0000FF","#0000CC","#000099","#000066","#000033","#000000"];
	    if (!ForeColour) palette[palette.length-1]="";
	    theCount=0;
	    while(theCount<palette.length) {
		    output+="<tr>";
		    for (x=0;x<6;x++) {
			    output+="<td style=\"background-color:"+palette[theCount]+";cursor:pointer;width:5px;height:5px;font-size:1px\" onmouseout=\"document.getElementById('selectedColour').style.backgroundColor='"+srcColor+"'\" onmouseover=\"document.getElementById('selectedColour').style.backgroundColor='"+palette[theCount]+"'\" onclick=\"setTypeColour('"+palette[theCount]+"','"+cont+"','"+prefix+"')\" >&nbsp;</td>";
			    theCount++;
		    }
		    output+="</tr>";
	    }
	    output="<div id=\"paletteSelector\" style=\"position:absolute;z-index:2\"><table cellpadding=0 cellspacing=0 style=\"border:1px solid #000;background-color:#ffffff\" >"+output+"</table></div>";
    	
	    document.getElementById(prefix+cont).innerHTML+=output;
	}else{
	    removeTheNode("paletteSelector");
	}
}
function setTypeColour(col,coltype,prefix) {
    cont=coltype;
    removeTheNode("paletteSelector");
    //document.getElementById(cont).innerHTML="<img src=\"/skin/downArrow.gif\" />";
    document.getElementById(prefix+coltype+"selected").style.backgroundColor=col;
}
function setForeColour(src,prefix) {
    //checkValidButtons(prefix);
    //if (src.className=="buttonOn") {
        fColor=document.getElementById(prefix+"forecolourselected").style.backgroundColor;
        cmdExec(src,prefix,"ForeColor",""+fColor);
    //}
}
function setBackColour(src,prefix) {
    checkValidButtons(prefix);
    if (src.className=="buttonOn") {
        fColor=document.getElementById(prefix+"backcolourselected").style.backgroundColor;
        cmdExec(src,prefix,"BackColor",""+fColor);
    }
}
function setMode(bMode) {  
    var sTmp;  
	isHTMLMode = bMode;  
	if (isHTMLMode) {  
		sTmp = theEditorDocument.body.innerHTML;  
		theEditorDocument.body.innerText = sTmp;  
	} else {  
		sTmp = theEditorDocument.body.innerText;  
		theEditorDocument.body.innerHTML = sTmp;  
	}  
	theEditor.focus();  
}
function defineImage(src,prefix) {
    checkValidButtons(prefix);
    if (src.className=="buttonOn") {
        defineSelection(prefix);
        output="<table>";
        //output+="<tr><td>Comment</td><td><input type=\"text\" id=\"imageComment\" /></td></tr>";
        output+="<tr><td>Image URL</td><td><input type=\"text\" id=\"imageURL\" onkeydown=\"return ifReturn(event,'createHyper')\" value=\"http://\"/></td></tr>";
        output+="<tr><td colspan=\"2\" style=\"text-align:center\">";
        output+="<img id=\"createHyper\" src=\"/skin/create.gif\" style=\"cursor:pointer\" onmouseover=\"this.src='/skin/createOver.gif'\" onmouseout=\"this.src='/skin/create.gif'\" onclick=\"addImage('"+prefix+"')\" />";
        output+="<img src=\"/skin/editCancel.jpg\" style=\"cursor:pointer\" onmouseover=\"this.src='/skin/editCancelOver.jpg'\" onmouseout=\"this.src='/skin/editCancel.jpg'\" onclick=\"startFadeDown();removeTheNode('imageInsert')\" />";
        output+="</td></tr>";
        output+="</table>";
        startFadeUp();
        pasteRawHTML(returnRoundedBox("imageInsert","Insert an image",output,false,5,null,true,false),document.body);
        centerObject(document.getElementById("imageInsert"));
    }
}
function addImage(prefix) {
    imgURL=document.getElementById("imageURL").value;
    cmdExec(null,prefix,"InsertImage",imgURL);
    startFadeDown();
    removeTheNode("imageInsert");
}
function smilieDialog(src,prefix) {
    //defineSelection(prefix);
    checkValidButtons(prefix);
    if (src.className=="buttonOn") {
        defineSelection(prefix);
        smilies=["angry","cry","frown","grin","hmm","love","nasty","sad","shock","smile","surprise","tear","tongue","upset","wink"];
        output="<table>";
        n=0;
        for (x=0;x<5;x++) {
            output+="<tr>";
            for (y=0;y<3;y++) {
                output+="<td align=\"center\"><img src=\"/skin/smilies/"+smilies[n]+".png\" style=\"cursor:pointer\" onclick=\"addSmilie('"+prefix+"','"+smilies[n]+"')\" /></td>";
                n++;
            }
            output+="</tr>";
        }
        output+="</table>";
        startFadeUp();
        pasteRawHTML(returnRoundedBox("smilieInsert","Select a Smilie",output,false,5,null,true,false),document.body);
        centerObject(document.getElementById("smilieInsert"));
    }
}
function addSmilie(prefix,filename) {
    imgURL="/skin/smilies/"+filename+".png";
    cmdExec(null,prefix,"InsertImage",imgURL);
    startFadeDown();
    removeTheNode("smilieInsert");
}
//function addImage(picurl,prefix) {
    //imgURL="/picture.ashx?pid="+id;
    //alert("<img src=\""+picurl+"\" />");
    //cmdExec(null,prefix,"InsertImage",picurl);
//}
function defineHyperlink(src,prefix) {
    //defineSelection(prefix);
    checkValidButtons(prefix);
    if (src.className=="buttonOn") {
        output="<table>";
        //output+="<tr><td>Comment</td><td><input type=\"text\" id=\"imageComment\" /></td></tr>";
        output+="<tr><td>Link URL</td><td><input type=\"text\" id=\"linkURL\" value=\"http://\" onkeydown=\"return ifReturn(event,'createHyper')\" /></td></tr>";
        output+="<tr><td colspan=\"2\" style=\"text-align:center\">";
        output+="<img src=\"/skin/create.gif\" id=\"createHyper\" style=\"cursor:pointer\" onmouseover=\"this.src='/skin/createOver.gif'\" onmouseout=\"this.src='/skin/create.gif'\" onclick=\"addLink('"+prefix+"')\" />";
        output+="<img src=\"/skin/editCancel.jpg\" style=\"cursor:pointer\" onmouseover=\"this.src='/skin/editCancelOver.jpg'\" onmouseout=\"this.src='/skin/editCancel.jpg'\" onclick=\"startFadeDown();removeTheNode('linkInsert')\" />";
        output+="</td></tr>";
        output+="</table>";
        startFadeUp();
        pasteRawHTML(returnRoundedBox("linkInsert","Insert a Hyperlink",output,false,5,null,true,false),document.body);
        centerObject(document.getElementById("linkInsert"));
    }
}
function addLink(prefix) {
    imgURL=document.getElementById("linkURL").value;
    cmdExec(null,prefix,"CreateLink",imgURL);
    startFadeDown();
    removeTheNode("linkInsert");
}
function checkValidButtons(prefix) {
    defineSelection(prefix);
    obj=document.getElementById(prefix+"editorContent");
    theEditorDocument =obj.document?obj.contentWindow.document:obj.contentDocument;
    //theEditorDocument.focus();
    var valid=true;
    if (obj.document) {
        //if (theEditorDocument.selection) {
        if (thisTextRange!=null) {
            if (document.getElementById("adminbox")!=null) document.getElementById("adminbox").innerHTML=theEditorDocument.selection.type;
            //thisTextRange.select();
            //alert(thisTextRange.parentElement().tagName);
            pobj=null;
            if (theEditorDocument.selection.type=="Control") {
                valid=true;
            }else{
                if (isElementInElement(theEditorDocument,thisTextRange.parentElement())) {
                    
                    //theEditorDocument.body.style.backgroundColor='#fff';
                    //thisTextRange.select();
                }else{
                    //theEditorDocument.body.style.backgroundColor='#f00';
                    thisTextRange=null;
                    valid=false;
                }
            }
        //}else{
        //    valid=false;
        }
        //}
    }
	    
    buttonCmd=new Array();
    buttonCmd.push(["forecolourselected","cmdExec('ForeColor')","ForeColor"]);
    buttonCmd.push(["Bold","cmdExec('Bold')","Bold"]);
    buttonCmd.push(["Justify_center","cmdExec('JustifyCenter')","JustifyCenter"]);
    buttonCmd.push(["Clear_format","cmdExec('RemoveFormat')","RemoveFormat"]);
    buttonCmd.push(["Copy","cmdExec('Copy')","Copy"]);
    buttonCmd.push(["Justify_full","cmdExec('JustifyFull')","JustifyFull"]);
    buttonCmd.push(["Horizontal_line","cmdExec('InsertHorizontalRule')","InsertHorizontalRule"]);
    buttonCmd.push(["Image","defineImage()","InsertImage"]);
    buttonCmd.push(["MyImage","defineImage()","InsertImage"]);
    buttonCmd.push(["Indent","cmdExec('Indent')","Indent"]);
    buttonCmd.push(["Italic","cmdExec('Italic')","Italic"]);
    buttonCmd.push(["Justify_left","cmdExec('JustifyLeft')","JustifyLeft"]);
    buttonCmd.push(["Hyperlink","defineHyperlink()","CreateLink"]);
    buttonCmd.push(["Ordered_list","cmdExec('InsertOrderedList')","InsertOrderedList"]);
    buttonCmd.push(["Outdent","cmdExec('Outdent')","Outdent"]);
    buttonCmd.push(["Paste","cmdExec('Paste')","Paste"]);
    buttonCmd.push(["Redo","cmdExec('Redo')","Redo"]);
    buttonCmd.push(["Justify_right","cmdExec('JustifyRight')","JustifyRight"]);
    buttonCmd.push(["Smilies","smilieDialog()","InsertImage"]);
    buttonCmd.push(["Strikethrough","cmdExec('StrikeThrough')","StrikeThrough"]);
    buttonCmd.push(["Subscript","cmdExec('Subscript')","Subscript"]);
    buttonCmd.push(["Superscript","cmdExec('Superscript')","Superscript"]);
    buttonCmd.push(["Underline","cmdExec('Underline')","Underline"]);
    buttonCmd.push(["Undo","cmdExec('Undo')","Undo"]);
    buttonCmd.push(["Unlink","cmdExec('Unlink')","Unlink"]);
    buttonCmd.push(["Unordered_list","cmdExec('InsertUnorderedList')","InsertUnorderedList"]);
    
    op="";
    obj=document.getElementById(prefix+"editorContent");
	theEditorDocument =obj.document?obj.contentWindow.document:obj.contentDocument;
	
    for (i=0;i<buttonCmd.length;i++) {
        if (valid) {
            if (theEditorDocument.queryCommandEnabled(buttonCmd[i][2])) {
                fader(document.getElementById(prefix+buttonCmd[i][0]),100);
                document.getElementById(prefix+buttonCmd[i][0]).className="buttonOn";
                //op+=buttonCmd[i][0]+" - ";
            }else{
                fader(document.getElementById(prefix+buttonCmd[i][0]),40);
                document.getElementById(prefix+buttonCmd[i][0]).className="buttonOff";
            }
        }else{
            fader(document.getElementById(prefix+buttonCmd[i][0]),40);
            document.getElementById(prefix+buttonCmd[i][0]).className="buttonOff";
        }
    }
    //alert(op);
}
function isElementInElement(container,child) {
    reply=false;
    tagtype=child.tagName;
    kids=container.getElementsByTagName(tagtype);
    for(i=0;i<kids.length;i++) {
        if (kids[i]==child) reply=true;
    }
    //alert(reply?"It is":"It isn't");
    return reply;
}
function PostContent(prefix,threadid) {
    obj=document.getElementById(prefix+"editorContent");
	theEditorDocument =obj.document?obj.contentWindow.document:obj.contentDocument;
    addPost(threadid,theEditorDocument.body.innerHTML.replace("&","(amp)"));
}
function addMyImage(src,prefix) {
    //defineSelection(prefix);
    checkValidButtons(prefix);
    if (src.className=="buttonOn") {
        pictureSelector(addSelectedPicture,prefix);
    }
}
function pictureSelector(handle,prefix) {
    defineSelection(prefix);
    richTextPrefix=prefix;
    selectedPictureHandler=handle;
    var toInsert="<div id=\"pictureList\">Loading your images...<br /><img src=\"/skin/loading.gif\" /></div>";
    startFadeUp();
    pasteRawHTML(returnRoundedBox("pictureGallery","Select an image",toInsert,true,10,null,true,false),document.body);
    centerObject(document.getElementById("pictureGallery"));
    sendRequest("/inside/Admin.ashx?cmd=getMyPictures",pictureSelectorReceived);
}
function pictureSelectorReceived(reply) {
    var toInsert="<table width=\"400px\"><tr><td style=\"border-right:1px dotted #999999;vertical-align:top\">My uploaded images<br />"+reply+"</td>";
    toInsert+="<td style=\"vertical-align:top\">Upload an image...<br /><span style=\"font-size:10px;color:#999999\">Uploaded images are reduced to 300 pixels<br />"+embeduploader("pid_1","pictureUploaded")+"</td></tr></table>";
    setInnerHTMLCentered("pictureList",toInsert,"pictureGallery");
}
function pictureUploaded() {
    sendRequest("/inside/Admin.ashx?cmd=getMyPictures",pictureSelectorReceived);
}
function selectImage(imgID) {
    startFadeDown();
    removeTheNode("pictureGallery");
    imgID="/picture.ashx?pid="+imgID;
    selectedPictureHandler(imgID,richTextPrefix);
}
function addSelectedPicture(imgURL,prefix){
    cmdExec(null,prefix,"InsertImage",imgURL);
}
function viewMyPics() {
    pictureSelector(alert);
}
function deleteMyPic(id) {
    if (window.confirm("Are you sure you wish to delete this image?")) {
        setInnerHTMLCentered("pictureList","Loading your images...<br /><img src=\"/skin/loading.gif\" />","pictureGallery");

        sendRequest("/inside/Admin.ashx?cmd=deleteMyPicture&pid="+id,pictureSelectorReceived);
    }
}
function defineSelection(prefix) {
	obj=document.getElementById(prefix+"editorContent");
	theEditorDocument=obj.document?obj.contentWindow.document:obj.contentDocument;
	thisTextRange=null;
    if (obj.document) {
        if (theEditorDocument.selection) {
        thisTextRange=theEditorDocument.selection.createRange();
        }
    }
}

function ifReturn(e,buttonid)
{
    var rval=true;
    if (window.event) keycode=window.event.keyCode;
    else if (e) keycode=e.which;
    if (keycode==13)
    {
        rval=false;
        document.getElementById(buttonid).click();
    }
    return rval;
} 