﻿// Tiny MCE
	tinyMCE.init({
	mode:"exact",
	elements:"textField",
	theme : "advanced",
	theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,undo,redo,link,unlink",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	width : "612",
	height: "154",
	oninit: "counterSetup",
	setup : function (ed) {	
       ed.onKeyUp.add(
            function (ed, evt) {
                countchars('theForm',tinyMCE.selectedInstance.editorId,text1_maxchars,'msgCounter');
            }
        );
    },
    
	//handle_event_callback : "myHandleEvent",
    //onchange_callback : "myCustomOnChangeHandler",
	//theme_advanced_statusbar_location : "bottom",
	extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
	});
	
	var text1_maxchars=2000;// max plain text chars allowed
	var undobuffer="";//init string (first tinyMCE instance)
	var init=false;
	
	function counterSetup(){
		countchars('theForm',tinyMCE.selectedInstance.editorId,text1_maxchars,'msgCounter')
	}
	
	function countchars(formname,editor_id,maxchars,displayID){
	    var content=tinyMCE.activeEditor.getContent({format:'text'});
		plaintext="";
	    if (content!=null)
	    {    	
	    		var re = /(<([^>]+)>)/ig ; //strip all tags
	    		plaintext = content.replace(re, "");
	    		var re = /&nbsp;/g ; //strip all tags
	    		plaintext = plaintext.replace(re, "");
	    }
	    var currCount=plaintext.length;
	    remainCount=maxchars-currCount+1; //fix: allow +1
	    tmpvar=editor_id+'_count';   
	    eval(tmpvar + '=currCount'); //assign dynamic var to char count
	    displayObj=document.getElementById(displayID);
	    displayObj.innerHTML="You have " + eval(remainCount-1) + " characters remaining"; //-1 to counteract above +1
	    if(remainCount<=0){ //typed too much
	        displayObj.innerHTML="You are exceeding the maximimum length by "+ eval((-1 * remainCount)+1) + " characters";
	    } else {
	        undobuffer=tinyMCE.activeEditor.getContent(); //store content in buffer
	    }
	}
// end Tiny MCE