/////////////////////////////////////////////////////////////////////////////////////////
function MTB_I_Make_RegExp(strName)
{
	return new RegExp(strName.replace(/%/g, '\\x').replace(/\+/g, ' ').replace(/\*/g, '\\*').replace(/\[/g, '\\[').replace(/\]/g, '\\]'), '');
}

function MTB_I_EscapeText(strSrc)
{
	return strSrc.replace(/&/g,'&amp;').replace(/</g,'&lt;');
}

function MTB_I_UnescapeText(strSrc)
{
	return strSrc.replace(/&lt;/g, '<').replace(/&amp;/g, '&');
}
/////////////////////////////////////////////////////////////////////////////////////////
function MTB_I_StdAction(strText)
{
	this.obj.restore(strText);
}
function MTB_I_DummyObj()
{
	this.value	= '';
	this.name	= 'MTB_Dummy.action(this.document.forms[0].msg_body.value), parent.opener.MTB_Dummy';
	this.obj	= null;
}
MTB_I_DummyObj.prototype.action	= MTB_I_StdAction;
var MTB_Dummy	= new MTB_I_DummyObj();
/////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
function MTB_I_Compose_Text(bSimple)
{
	var	pCol	= eval(	this.m_strDoc + '.' + 
						this.m_strCollection);
	var nColLen	= pCol.length;
	var	strResult;
	var i,j;
	var count	= 0;

	if (bSimple)
		strResult	= '';
	else
		strResult	= this.m_strBegin;
    var items = this.m_strNames.split(',');
    var s = ''; s1 = '';
    var i, j;
    
    for (i = 0; i < items.length; i++)
    {
    	var ctrl = eval(items[i]);
    	var s_ctrl = '';
    	var s_val = '';
    	
    	if (ctrl && (ctrl.type == 'text' || ctrl.type == 'textarea'))
    	{
    		s1 += ctrl.value + "\n";
    		s_ctrl = items[i];
    		s_val = ctrl.value
    	}
    	else if (ctrl && ctrl.innerHTML)
    	{
    		s1 += ctrl.innerHTML + "\n";
    		s_ctrl = items[i];
    		s_val = ctrl.innerHTML
    	}
    	// else do nothing

		if (!bSimple)
			strResult	+= this.m_strRBegin + '_x_path="' + s_ctrl + '"' + this.m_strRMiddle;
		strResult		+= s_val;
		if (!bSimple)	
			strResult	+= this.m_strREnd;
		else
			strResult	+= this.m_strSDiv;
		count++;
    	
    	s += items[i] + "\n";
    }

	if (!bSimple)
		strResult	+= this.m_strEnd;
	return strResult;
}
/////////////////////////////////////////////////////////////////////////////////////////
function MTB_I_Restore(strValue)
{
	var aE	= MTB_I_UnescapeText(strValue).split(this.m_strSplitter);
	var i;
	var sPath;
	var nBeg,nEnd;
	
	for (i = 0; i < aE.length - 1; i++)
	{
		nBeg				= aE[i].indexOf('_x_path="') + '_x_path="'.length;
		nEnd				= aE[i].indexOf('"' + this.m_strRMiddle, nBeg);
		sPath 				= aE[i].substring(nBeg, nEnd);

		var ctrl = eval(sPath);
		if (ctrl && (ctrl.type == 'text' || ctrl.type == 'textarea'))
		{
			ctrl.value = aE[i].substring(nEnd + this.m_strRMiddle.length + 1, aE[i].length);
		} 
		else if (ctrl && ctrl.innerHTML)
		{
			ctrl.innerHTML = aE[i].substring(nEnd + this.m_strRMiddle.length + 1, aE[i].length);
		}
	}
}
/////////////////////////////////////////////////////////////////////////////////////////
function MTB_I_Check()
{
	MTB_Dummy.value		= this.compose(false);
	MTB_Dummy.obj    	= this;

	if (MTB_Dummy.value == (this.m_strBegin + this.m_strEnd))
		alert('Nothing to check');
	else
		doSpell(this.m_strLang, MTB_Dummy, this.m_strSPath, true);
}
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
function MTB_I_ASC_Check()
{
	MTB_Dummy.value		= this.compose(false);
	MTB_Dummy.obj    	= this;

	if (MTB_Dummy.value == (this.m_strBegin + this.m_strEnd))
		alert('Nothing to check');
	else
	{
		return document['AutoChecker'].checkText(MTB_Dummy);
	}
}
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
function MTBChecker(strNames, strLang, strSproxyPath)
{
	this.m_strNames	= strNames;
	this.m_strLang	= strLang;
	this.m_strSPath	= strSproxyPath;
}

MTBChecker.prototype.check		= MTB_I_Check;
MTBChecker.prototype.ASC_check		= MTB_I_ASC_Check;
MTBChecker.prototype.compose	= MTB_I_Compose_Text;
MTBChecker.prototype.restore	= MTB_I_Restore;

MTBChecker.prototype.m_strBegin		= '<table border=1 _bordercolor=silver width=100%>';
MTBChecker.prototype.m_strEnd		= '</table>';
MTBChecker.prototype.m_strRBegin	= '<TR tr_x_tr><TD ';
MTBChecker.prototype.m_strRMiddle	= '>';
MTBChecker.prototype.m_strREnd		= '</TD tr_x_tr></TR tr_x_tr>';
MTBChecker.prototype.m_strSplitter	= "</TD tr_x_tr></TR tr_x_tr>";
MTBChecker.prototype.m_strSDiv		= '\n\n';

MTBChecker.prototype.m_strDoc			= 'document';
MTBChecker.prototype.m_strCollection	= 'forms';
MTBChecker.prototype.m_strSubCollection	= 'elements';
MTBChecker.prototype.m_strTypes			= 'text,textarea';

MTBChecker.prototype.m_objSubmitForm = "";

function FCH_CheckFormTextControls(strNames, strLanf, strSproxyPath)
{
	var pChecker	= new MTBChecker(strNames, strLanf, strSproxyPath);
	pChecker.check();
}

function FCH_ASCFormTextControls(objForm, strNames)
{
	if (!document['AutoChecker'])
		return true;

	if (document['AutoChecker'].checkText)
	{
		var pChecker = new MTBChecker(strNames, "", "");
		pChecker.m_objSubmitForm = objForm;
		return pChecker.ASC_check();
	}
	else
		return !document.forms[frmname].enabling.checked;
}

function FCH_CheckForm(objForm, strLanf, strSproxyPath)
{
  var strNames = "";

  var i = 0;		// counter to be used in loops
  var nItems;		// number of items in form
  var oItem;		// current HTML item
  var tagName;		// for example, "INPUT", "TEXTAREA"
  var itemName;		// name in form "Subject"
  var itemFullName;	// name in form "document.forms[0].Subject"
  var sFormName;	// reference to the form (either name or "forms[i]")

  // Obtaining referece to the form
  sFormName = objForm.name;

  // Great if name found, but form can be unnamed, retrieving form index
  if ("" == sFormName)
  {
    var nForms = document.forms.length;
    var oForm;

    // looping through forms available in this document
    for (i = 0; i < nForms; ++i)
    {
      oForm = document.forms[i];
      if (oForm == objForm)
        sFormName = "forms[" + i + "]";
    }
  }

  nItems = objForm.elements.length;
  // Identifying needed items. Currently, these are "INPUT TYPE=TEXT" and "TEXTAREA"
  for (i = 0; i < nItems; ++i)
  {
    oItem = objForm.elements[i];
    tagName = oItem.tagName.toUpperCase();

    // Add other controls to be analyzed here
    if ((("INPUT" == tagName) && ("TEXT" == oItem.type.toUpperCase()))
      || ("TEXTAREA" == tagName))
    {
      itemName = oItem.name;

      // If item is named, use name. If not - use index.
      if ("" != itemName)
      {
        // Despite that we obtained name, several controls may have
        // the same name. If this happens, use index as well.

        if (1 < eval("document." + sFormName + "." + itemName + ".length"))
          itemFullName = "document." + sFormName + ".elements[" + i + "]";
        else
          itemFullName = "document." + sFormName + "." + itemName;
      }
      else
        itemFullName = "document." + sFormName + ".elements[" + i + "]";

      if ("" != strNames)
        strNames += ",";

      strNames += itemFullName;
    };
  };

  if ("" != strNames)
    FCH_CheckFormTextControls(strNames, strLanf, strSproxyPath);
};
