// mc.js - this is the "Monitor Control" library of routines to do some
// simple HTML form control manipulation
mcNetscape = 1;
mcIE = 10;
mcFireFox = 20;
mcOpera = 30;
mcOther = 100;

//alert(navigator.appName + " - " + navigator.userAgent);

if (navigator.appName.search(/netscape/i) != -1)
{
   mcBrowser = mcNetscape;
   if (navigator.userAgent.search(/Firefox/i) != -1) mcBrowser = mcFireFox;
   if (navigator.userAgent.search(/Opera/i) != -1) mcBrowser = mcOpera;
}
else if (navigator.appName.search(/Microsoft/i) != -1)
   mcBrowser = mcIE;
else if (navigator.appName.search(/Opera/i) != -1)
   mcBrowser = mcOpera;
else
   mcBrowser = mcOther;
   
//   alert(mcBrowser);

function mcAllowEdit(c, flag)
{
   c.onkeydown = mcCheckKeyDown;
   c.mcKeyDown = flag;
}

function mcAllowChars(c, chars)
{
   c.onkeypress = mcCheckKeyPress;
   c.mcAllowChars = chars + '\r';
}

function mcDenyChars(c, chars)
{
   c.onkeypress = mcCheckKeyPress;
   c.mcDenyChars = chars;
}

function mcStripOnChange(c, chars)
{
   c.onchange = mcCheckChange;
   c.mcStripOnChange = chars;
}

function mcStripLR(c)
{
   c.onchange = mcCheckChange;
   c.mcStripLR = true;
}

function mcSelectOnFocus(c, flag)
{
   c.onfocus = mcCheckFocus;
   c.mcSelFocus = flag;
}

function mcCheckKeyDown(evnt)
{
   // get the char for the key pressed
   if (mcBrowser == mcIE || mcBrowser == mcOpera)
      k = window.event.keyCode;
   else if (mcBrowser == mcNetscape || mcBrowser == mcFireFox)
      k = evnt.which;
   else
      // don't know the browser, skip the event
      return true;

   // pass through tabs and returns
   if (k == 9)
      return true;
   else if ((k == 13) && (this.type != 'textarea'))
      return true;
   else
      return this.mcKeyDown;
}

function mcCheckKeyPress(evnt)
{
   // get the char for the key pressed
   if (mcBrowser == mcIE || mcBrowser == mcOpera)
      k = window.event.keyCode;
   else if (mcBrowser == mcNetscape || mcBrowser == mcFireFox)
      k = evnt.which;
   else
      // don't know the browser, skip the event
      return true;
   c = String.fromCharCode(k);

   // if the char is not allowed, return false
   if (this.mcAllowChars)
      if (this.mcAllowChars.indexOf(c) == -1)
         return false;

   // if the char is explicitly denied, return false
   if (this.mcDenyChars)
      if (this.mcDenyChars.indexOf(c) != -1)
         return false;

   // passed all tests, return true;
   return true;      
}

function mcCheckChange()
{
   if (this.mcStripOnChange)
      this.value = mcStripString(this.value, this.mcStripOnChange);

   if (this.mcStripLR)
      this.value = mcStripSpacesLR(this.value);
}

function mcCheckFocus()
{
   if (this.mcSelFocus)
      this.select();
}

function mcStripString(s, strip)
{
   r = "";
   l = s.length;
   for (i = 0; i < l; i++)
   {
      c = s.charAt(i);
      if (strip.indexOf(c) == -1)
         r = r + c;
   }
   return r;
}

function mcStripSpacesLR(s)
{
   // strip leading/trailing whitespace
   iStart = 0;
   iEnd = s.length - 1;
   for (i = 0; i <= iEnd; i++)
      if (s.charAt(i) != ' ')
         break;
   iStart = i;
   for (i = iEnd; i > 0; i--)
      if (s.charAt(i) != ' ')
         break;
   iEnd = i;
   return s.substring(iStart, iEnd + 1);
}

function tarMaxRowLength(objTAR, intLength)
{
	var intCRLFCtr = 0;
	var intCRLFPos = 0;
	var intOffset = 0;
	for(i=0; i<objTAR.value.length; i++)
	{
		if(objTAR.value.charCodeAt(i) == 10)
		{
			if((i+1) != objTAR.value.length)
			{
				intOffset++;
				intCRLFCtr++;
				intCRLFPos = i + intOffset;
			}
		}
	}
	if(intCRLFCtr > 0)
      intOffset--;
	if((objTAR.value.charCodeAt(objTAR.value.length-2) == 10) && (objTAR.value.charCodeAt(objTAR.value.length-1) == 10))
      intLength = intLength + 2;
	if((objTAR.value.length - (intCRLFPos - (intOffset + 1))) > intLength)
      objTAR.value = objTAR.value + '\n';
}
