var gMaxTextSize = 65535; //size of oracle CLOB
function submitchecksize(objId,size)
{	
	var	retval = true,
		objEditor,
		chatContent,
		txtLen;
	if (objId.className == 'mceNoEditor')
	{	alert(objId.className);
	    var itTextLength = parseInt(objId.value.length),
	        itPermittedSize = parseInt(size);
	    if (itTextLength > itPermittedSize)
	    {
		alert(' Please restrict input to ' + itPermittedSize + ' characters!  Current text length is ' + itTextLength);
		retval = false;
	    }
	}
	else if (objId.className == 'iNoteEditor')
	{	
		chatContent	= tinyMCE.get('iNote').getContent({format : 'raw'}),
		txtLen		= chatContent.length;	
		if ( txtLen > gMaxTextSize)
		{
			alert(' Please restrict input to ' + gMaxTextSize + ' characters!  Current text length is ' + txtLen);
			retval = false;
		}
	}
	else
	{	
		objEditor	= CKEDITOR.instances["iNote"],
		chatContent	= objEditor.getData(),
		txtLen		= chatContent.length;
		if ( txtLen > gMaxTextSize)
		{
			alert(' Please restrict input to ' + gMaxTextSize + ' characters!  Current text length is ' + txtLen);
			retval = false;
		} 
	}
	return retval;
	
}


function displayLength(objId,size)
{	
	var tLabel = $("iNoteLabel"),
	    tNoteTextArea = $("iNote"),
	    itTextSize = parseInt(tNoteTextArea.value.length),
	    itPermittedSize = parseInt(size),
	    itRemainingCharCount = itPermittedSize - itTextSize;
	if(itRemainingCharCount <= 0)
	{
		alert('Maximum text length has been reached. Please save this iNote and if necessary continue on an additional entry.');
		tLabel.innerHTML = "Remaining characters: " + itRemainingCharCount;
	}
	else
	{
		tLabel.innerHTML = "Remaining characters: " + itRemainingCharCount;   //IE & FF
	}
}

function myCustomOnInit()
{	
	tinyMCE_timer1=setTimeout("countchars('mainForm','iNote',gMaxTextSize,'iNoteLabel')",1000); //wait a bit to avoid IE error
}

function myCustomOnInitCK(editor)
{	
	ckEditor_timer1=setTimeout("countchars('mainForm','iNote',gMaxTextSize,'iNoteLabel','ckEditor')",1000); //wait a bit to avoid IE error
}


function getEvent(e)
{	
	return e?e:(window.event?window.event:"");
}


//event when something in TinyMCE changes (which is when an undo level is added. e.g. paste)
function myCustomOnChangeHandler(inst)
{	
    tinyMCE_timer1=setTimeout("countchars('mainForm','iNote',gMaxTextSize,'iNoteLabel')",250); //slight delay before counting
}

//event such as key or mouse press
function myHandleEvent(e)
{	
	window.status = "event:" + e.type;
	if(e.type=="keyup")
	{	
        	clearTimeout(tinyMCE_timer1);
        	var retval = countchars('mainForm','iNote',gMaxTextSize,'iNoteLabel');
		if(!retval)
		{
			alert('Maximum text length has been reached. Please save this iNote and if necessary continue on an additional entry....');
		} 
	}
	return true;
}

function getHTML_TinyMCE(txtfield)
{	
	var obj=$('iNote_ifr'),
	    content;
	if(obj.contentDocument)
	{
		content=obj.contentDocument.body.innerHTML; //FireFox (getContent() messes up cursor position)
	} else {
		content=tinyMCE.get('iNote').getContent({format : 'raw'}); //IE
	}
	return content;
}

function countchars(formname,txtfield,maxchars,displayID,editorName)
{	
//    thefield=eval("document."+formname+"."+txtfield); // RGI-no need for eval here
	var	thefield = document.forms[formname][txtfield],
		rtValue = true,
		currCount,
		objEditor,
		remainCount;
	if(isNaN(maxchars))
	{ 
	//arg could contain a var name rather than a number //RGI - ???WHY is this so??? 
		maxchars=eval(maxchars); //convert var to number
	}
	if(editorName != 'ckEditor')
	{	
		currCount = tinyMCE.get('iNote').getContent({format : 'raw'}).length;
	}
	else 
	{	
		objEditor = CKEDITOR.instances["iNote"];
		currCount = objEditor.getData().length;
		remainCount=maxchars-currCount+1;
		if(remainCount<=0)
		{ 
			alert('Maximum text length has been reached. Please save this iNote and if necessary continue on an additional entry....');
		}
	}
	
	remainCount	=maxchars-currCount+1; //fix: allow +1
	tmpvar		=txtfield+'_count'; 	// RGI - why not just add a 'count' object to the txtfield and avoid the eval?
	eval(tmpvar + '=currCount'); //assign dynamic var to char count
	displayObj	=$(displayID);
	displayObj.innerHTML=  "Remaining characters: " + eval(remainCount-1); //-1 to counteract above +1
	if(editorName != 'ckEditor' && remainCount<=0)
	{ 
		rtValue = false;
	}

	if(editorName == 'ckEditor' )
	{	
		if(rtValue)
		{
			setTimeout('myCustomOnInitCK("ckEditor");',1000);
		}
		else 
		{	
			clearTimeout(ckEditor_timer1);
			rtValue = countchars('mainForm','iNote',gMaxTextSize,'iNoteLabel','ckEditor');
		}
	}
	return rtValue;
}  

