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

function displayLength(objId,size)
{
	var tLabel = document.getElementById("iNoteLabel");
	var tNoteTextArea = document.getElementById("iNote");
	var itTextSize = parseInt(tNoteTextArea.value.length);
	var itPermittedSize = parseInt(size);
	var 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
	}
	//tLabel.innerText = "Remaining characters: " + tLabelText;   //IE
	//tLabel.textContent = "Remaining characters: " + tLabelText; //FF
}

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

//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)
{
    obj=document.getElementById('iNote_ifr');
	var 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)
{
    thefield=eval("document."+formname+"."+txtfield);
	//alert(thefield.value +":" + thefield.value.length); // <p><br mce_bogus="1"/></p>
	var rtValue = true;
    if(isNaN(maxchars))
	{ 
		//arg could contain a var name rather than a number
        maxchars=eval(maxchars); //convert var to number
    }
	var currCount = tinyMCE.get('iNote').getContent({format : 'raw'}).length;
	remainCount=maxchars-currCount+1; //fix: allow +1
    tmpvar=txtfield+'_count';   
    eval(tmpvar + '=currCount'); //assign dynamic var to char count
    displayObj=document.getElementById(displayID);
	displayObj.innerHTML=  "Remaining characters: " + eval(remainCount-1); //-1 to counteract above +1

    if(remainCount<=0)
	{ 
		//alert('Maximum text length has been reached. Please save this iNote and if necessary continue on an additional entry....');
		rtValue = false;
    }
	return rtValue;
}  
