/* Merged with HDIQ by rmore on 2004-02-10. */

function _GetMSDEBucketMessage(TokenNumber)
{
	var res = clmArr[TokenNumber];
	if (res == null)
	//force null rather than undefined
		return null;
	return res;
}

function _GetHTMLMsg(sErrNum)
{	
	var iTokenNumber;
	if (typeof(sErrNum) == typeof(1))
		iTokenNumber = sErrNum;
	else
		iTokenNumber = _clmGetToken(sErrNum);
	
	var sReturnVal=null;
	
	if (null != iTokenNumber)
		sReturnVal=_GetMSDEBucketMessage(iTokenNumber);
	
	if (null == sReturnVal)
		sReturnVal="";
	
	return sReturnVal;
}

function _clmReplacePropertyPlain(obj, prop)
{
	var propVal = obj[prop];
	if (null == propVal)
		return false;
	
	var resArray = _clmGetToken(propVal, true);
	if (null == resArray)
		return false;
	
	var sFinalInt = resArray[0];
	var args = resArray[1];
	
	obj[prop]=_clmGetMessageForString(sFinalInt, args, true);
	return true;
}

/**
 * Returns the fully substituted string.
 *
 *  id   - Message id to be looked up and returned.
 *  args - List of substitions arguments. The first (at 0 offset) arg
 *  is substituted for {0}, the 2nd for {1} and so on.
 */
function _clmGetMessageForString(id, args, plain)
{
	var strReadHTML=GetHTMLMsg(id);
	
	if (plain)
		return _GetstringPlain(strReadHTML, args);
	else
		return _Getstring(strReadHTML, args);
}

var clmMsgAttr="MGC_PL_LABEL";
var clmMsgPrefix=clmMsgAttr + "_";

function _clmReplacePropertyBasedOnMsgAttr(obj, prop, plain)
{
	var sFinalvalue = obj.getAttribute(clmMsgAttr);
	if (!sFinalvalue)
		return false;
	
	var resArray = _clmParseValue(sFinalvalue, true);
	if (null == resArray)
	{
		return false;
	}
	
	var sFinalInt = resArray[0];
	var args = resArray[1];
	
	res=_clmGetMessageForString(sFinalInt, args, plain);

	try
	{
		obj[prop]=res;
	}
	catch (e)
	{
		if ("innerHTML" == prop)
		{
			//fall back to inner text due to IE bug
			res = _GetstringPlain(strReadHTML, args);
			obj.innerText = res;
		} else
		{
			throw e;
		}
	}
	return true;
}

function _clmReplaceTitleAndAlt(obj)
{
	_clmReplacePropertyPlain(obj, 'title');
	_clmReplacePropertyPlain(obj, 'alt');
}

function _clmExtractAccessKeyFromHtml(html)
{
	var html = html.toUpperCase();
	var li_BefLike=html.indexOf("<U>");
	var li_AfterLike=html.indexOf("</U>");

	if ((li_BefLike== -1) || (li_AfterLike == -1))
	//if the open and close tags not found
	// then no access key should be assigned
		return '';
	
	var str_data=html.substring((li_BefLike+3),li_AfterLike);
	return str_data;
}

function _clmSetAccessKeyFromText(curObj, text)
{
	var str_data=_clmExtractAccessKeyFromHtml(text);
	if ("" == str_data)
	{
		return false;
	}
	curObj.accessKey=str_data;
	return true;
}
//Added For Collaborative Feature.
function _clmReplaceInnerHtmlForSpan(curObj)
{
  _clmReplacePropertyBasedOnMsgAttr(curObj,'innerHTML')
}
function _clmGetText(parentObj){
	var strReadHTML="";
	var sFinalvalue="";
	var allObjects = null;;
	
	if (null == parentObj)
		parentObj = document;
	
	if (document == parentObj)
		allObjects=document.all;
	
	
	if (null == allObjects)
	{
		//mozilla/netscape compatibility
		allObjects = parentObj.getElementsByTagName("*");
	}
	
	
	if (document == parentObj)
	{
		_clmReplacePropertyPlain(document, 'title');
	}
	
	for(label_index=0; label_index < allObjects.length ;label_index++)
	{
		var curObj = allObjects[label_index];
		
		var tagName = curObj.tagName;
		if (tagName == null)
			tagName = '';
		tagName = tagName.toUpperCase();

		switch (tagName)
		{
			case "TD":
			case "TH":
				//do one or the other, but not both
				_clmReplacePropertyBasedOnMsgAttr(curObj, 'innerHTML') ||
				_clmReplacePropertyPlain(curObj,'innerHTML','innerHTML');
				
				_clmReplacePropertyPlain(curObj,'title');
				break;
			case "OPTION":
				//Options cannot have HTML markup. It is not allowed
				// and IE is very flakey about it.
				_clmReplacePropertyBasedOnMsgAttr(curObj, 'text', true);
				break;
			case "SELECT":
			case "TEXTAREA":
			case "P":
				_clmReplacePropertyPlain(curObj,'title');
				break;
			case "CENTER":
			case "DIV": //pre MAPLE div just got innertext replaced
			case "FONT":
			case "H1":
			case "H2":
			case "H3":
			case "H4":
			case "LEGEND":
			case "STRIKE":
			case "STRONG":
				_clmReplacePropertyBasedOnMsgAttr(curObj, 'innerHTML');
				break;
			case "IMG":
				_clmReplaceTitleAndAlt(curObj);
				break;
			case "INPUT":
				if(curObj.type=="image")
				{
					_clmReplaceTitleAndAlt(curObj);
				}
				else
				{
					_clmReplacePropertyPlain(curObj,'title');
					//value is plain since inputs do not render HTML tags
					//based on values
					_clmReplacePropertyBasedOnMsgAttr(curObj, 'value', true);
				}
				break;
			case "LABEL":
				_clmReplacePropertyPlain(curObj,'title');
				if (_clmReplacePropertyBasedOnMsgAttr(curObj,'innerHTML') ||
					_clmReplacePropertyPlain(curObj,'innerHTML','innerHTML'))
				{
					var sForId = curObj.htmlFor;
					if ((sForId != null) && (sForId != ''))
					{
						forObj = document.getElementById(sForId);
						if (forObj)
						{
							_clmSetAccessKeyFromText(forObj, curObj.innerHTML);
						}
					}
				}
				break;
			case "A":
				_clmReplacePropertyBasedOnMsgAttr(curObj,'innerHTML',true);
				_clmReplacePropertyPlain(curObj,'title');
				break; 
			case "SPAN":
				_clmReplaceInnerHtmlForSpan(curObj);
				break;
			case "BUTTON":
				_clmReplacePropertyPlain(curObj,'title');
				if (_clmReplacePropertyBasedOnMsgAttr(curObj,'innerHTML'))
				{
					_clmSetAccessKeyFromText(curObj, curObj.innerHTML);
				}
				break;
      		} //end switch
	} //end for loop
} //end of getText function

//clmGetText is the preferred way to call getText since it
//given an indication of being part of the Common_Label_Message (clm) file.
//We'll keep getText around for backward compatibility.
_getText = _clmGetText;

//Regular expression to remove multuple <U> and </U>
//Regular expression replacement S&R usually at least 10x faster
//than hand coded replacement
var _GetstringPlainRegex = /<\/?[Uu]>/g;

//Returns message text without U tags
function _GetstringPlain(testhtml, localArray)
{
	var outHtml = _Getstring(testhtml, localArray);
	if (!outHtml)
		return outHtml;
	outHtml = outHtml.replace(GetstringPlainRegex, "");
	return outHtml;
}

function _Getstring(testhtml, localArray)
{

	if (null == localArray) 
	{
		localArray = [];
		try
		{
			localArray = arrDisplay;
		} 
		catch(e){} //ok if arrDisplay does not exist at this point
	}
	var sRetStr	= GetStringCommon(testhtml,localArray); //commented for 35035. as in sshd, there is no substitution needed. rahul
	//var sRetStr	= testhtml;
	return sRetStr;
}

//should be public interface
_clmGetstring = _Getstring;
_clmGetstringPlain = _GetstringPlain;

var _clmAttrRe=/^([0-9]+)(\|.*)?$/;
var _clmAttrTokenRe=/^MGC_PL_LABEL_([0-9]+.*)$/;

/**
 *
 * Returns the parsed out string id, or optionally the string id and the
 * parse out arguments.
 *
 * inStr - String like "MSDE_LTOKEN_2002" identifying message string.
 * returnArgs - If true return the parsed out arguments, else just return
 *              the parsed out id.
 */
function _clmParseValue(inStr, returnArgs)
{
	var match = _clmAttrRe.exec(inStr);
	if (null == match)
		return null;
	
	var id = parseInt(match[1]);
	if (!returnArgs)
		return id;

	//parse the args as needed
	var argsStr = match[2];
	var args;
	if ((null == argsStr) ||('' == argsStr))
		args=null;
	else
	{
		args = argsStr.substr(1).split('|');
		for (var i = 0; i < args.length; i++)
			args[i]=decodeURIComponent(args[i]);
	}

	return [id,args]
}

function _clmGetToken(tokenhtml, returnArgs)
{
	var match = _clmAttrTokenRe.exec(tokenhtml);
	if (null == match)
		return null;

	res=_clmParseValue(match[1], returnArgs);
	return res;
}

//most effecient/compatible way it to implement this as an array.
//Since arrays in javascript are sparse there is really no
//significant disadvantage.

var CLM_MAX_ID=3999; //insert new maximum value here

var clmArr = new Array(CLM_MAX_ID+1);



/* © Copyright 1998, 2004-2010 BMC Software, Inc. */
/* The source code embodied herein is a trade secret of BMC Software, Inc. All use, disclosure, and/or reproduction not specifically and expressly authorized, in writing, by BMC Software, Inc. is prohibited. */
