//
// START: Custom function used in conjuction with TinyMCE
//
function myCustomCleanup(type, value) {
    
	if (type == "insert_to_editor")
	{
		value = value.replace(/&lt;/gi,"<");
		value = value.replace(/&gt;/gi,">");
    }

    if (value.innerHTML != undefined) {

        //alert(value.innerHTML);

        if (value.innerHTML.toLowerCase() == "<div><br></div>") {
            value.innerHTML = "";
        }
    
        if (value.innerHTML == "<P>&nbsp;</P>") {
            value.innerHTML = "";
        }

        if (value.innerHTML.toLowerCase() == "<div><br mce_bogus=\"1\"></div>") {
            value.innerHTML = "";
        }
    }
	return value; 
}

// The custom save replaces before submitting the < > with the respective html entities (default ASP.NET validation fails otherwise)
function myCustomSaveContent(element_id, html, body) {
   
    if (html.substring(0, 3) == "<p>" && html.substring(4, 8) == "</p>" && html.charCodeAt(3) == 160) { html = ""; }
	html= html.replace(/</gi,"&lt;");
	html = html.replace(/>/gi, "&gt;");

	html = replaceAll(html, "&lt;div&gt;", "");
	html = replaceAll(html, "&lt;/div&gt;", "&lt;br&gt;");
	
	return html;
}


function replaceAll(str, initial, replacement) {
    var idx = str.indexOf(initial);

    while (idx > -1) {
        str = str.replace(initial, replacement);
        idx = str.indexOf(initial);
    }

    return str;
}

// Used to initialize specific TEXTAREA elementIDs 
// to be converted into tinyMCE editor workspaces
function tinyMceInit(elementIDs)
{	
	tinyMCE.init({
		mode : "exact",
		elements : elementIDs,
		theme : "advanced",
		skin : "o2k7",
		skin_variant: "silver",
		entity_encoding : "raw",
		plugins:'save',
		save_onsavecallback: "empySaveCallback",
		theme_advanced_buttons1: 'bold,italic,underline',
		//theme_advanced_buttons1:'bold,italic,underline,strikethrough,|,styleselect,formatselect,|,bullist,numlist,|,cut,copy,paste,|,undo,redo,|,link,unlink,|,code',
		theme_advanced_buttons2:'',
		theme_advanced_buttons3:'',
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		cleanup_callback : "myCustomCleanup",
		save_callback : "myCustomSaveContent"
	});
}

function tinyCustomMceInit(elementIDs) {
    tinyMCE.init({
        mode: "exact",
        elements: elementIDs,
        theme: "advanced",
        skin: "o2k7",
        skin_variant: "silver",
        entity_encoding: "raw",
        plugins: 'save',
        save_onsavecallback: "empySaveCallback",
        theme_advanced_buttons1: 'bold,italic,underline,strikethrough,|,formatselect,|,bullist,numlist,|,cut,copy,paste,|,undo,redo,|,code',
        theme_advanced_buttons2: '',
        theme_advanced_buttons3: '',
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        cleanup_callback: "myCustomCleanup",
        save_callback: "myCustomSaveContent"
    });
}

//
// END: Custom function used in conjuction with TinyMCE
//