//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 2002-2003, Microsoft Corporation.  All rights reserved.
//
// File: Cvobjs.js
//
// Description: Global utility functions
//
//////////////////////////////////////////////////////////////////////////////

//--------------------------------------------------------------------
// Function: ArrayPop
//
// Purpose: Removes the last element from an array. This function is
//	    provided for JScript versions that do not support this
//	    functionality (i.e. IE 5.01).
//
// Parameters: None
//
// Returns:
//	Object - The removed item
//--------------------------------------------------------------------
function ArrayPop()
{
	var oLast = this[this.length-1];
	this.length = Math.max(this.length-1,0);
	return oLast;
}

//--------------------------------------------------------------------
// Function: ArrayShift
//
// Purpose: Removes the first element from an array. This function is
//	    provided for JScript versions that do not support this
//	    functionality (i.e. IE 5.01).
//
// Parameters: None
//
// Returns: Object - The removed item
//--------------------------------------------------------------------
function ArrayShift() 
{
       	var oFirst = this[0];
       	this.reverse();
       	this.length = Math.max(this.length-1,0);
       	this.reverse();
       	return oFirst;
}

//--------------------------------------------------------------------
// Function: GetItemFromURL
//
// Purpose: Gets the value of the specified query string item.
//
// Parameters:
//	String sItem [in] - Query string item
//
// Returns: String
//--------------------------------------------------------------------
function GetItemFromURL(sItem)
{
	var sValue = "";
	var nItemLen = sItem.length;
	var aQuery = window.location.href.split("?");
	var oRegEx = new RegExp();

	oRegEx.compile(sItem,"gi");

	for (var i = 0; i < aQuery.length; i++)
	{
		if (oRegEx.test(aQuery[i]))
		{
			sValue = unescape(aQuery[i].substring(nItemLen+1,aQuery[i].length));
			break;
		}
	}

	return sValue;
}

//--------------------------------------------------------------------
// Function: AppendSlash
//
// Purpose: Appends a forward slash to the end of the supplied string,
//	    if one is not already present.
//
// Parameters:
//	String s [in] - String to update
//
// Returns: String
//--------------------------------------------------------------------
function AppendSlash(s)
{
	var sSlashes = "\\/";
	return (sSlashes.indexOf(s.charAt(s.length-1)) == -1) ? s + "/" : s;
}

//--------------------------------------------------------------------
// Function: IsBidiLang
//
// Purpose: Determines if the language code represents a bi-directional
//	    language.
//
// Parameters:
//	String sID [in] - Language code
//
// Returns: Boolean
//--------------------------------------------------------------------
function IsBidiLang(sID)
{
	var bRes = false;

	// Test for Urdu, Persian, Hebrew, Yiddish and Arabic
	if (sID == "ur" || sID == "fa" || sID == "he" || sID == "ji" || (sID.length > 3 && sID.substring(0,3) == "ar-"))
		bRes = true;

	return bRes;
}

//--------------------------------------------------------------------
// Method: GetBaseURL
//
// Purpose: Gets the viewer's URL minus the filename "viewer.htm".
//
// Parameters: None
//
// Returns: String
//--------------------------------------------------------------------
function GetBaseURL()
{
	var sBaseURL = "";

	try
	{
		var sURL = unescape(window.location.href.toLowerCase());
		
		var nPos = sURL.indexOf("viewer.htm");
		if (nPos != -1)	sBaseURL = sURL.substring(0,nPos);
	}
	catch(e)
	{	
	}

	return sBaseURL;
}

//--------------------------------------------------------------------
// Method: UnderlineHotkey
//
// Purpose: Underlines the mnemonic key within a string.
//
// Parameters: 
//	String sLabel [in] - String to modify
//	String sChar [in] - Character to underline
//
// Returns: String (HTML)
//--------------------------------------------------------------------
function UnderlineHotkey(sLabel,sChar)
{
	var sTmpLabel = sLabel.toLowerCase();
	var nPos = sTmpLabel.indexOf(sChar.toLowerCase());
	return (nPos == -1) ? sLabel : sLabel.substring(0,nPos) + "<u style='font-size: 100%' onclick='this.parentElement.click();'>" + sLabel.substring(nPos,nPos+1) + "</u>" + sLabel.substring(nPos+1,sLabel.length);
}

//--------------------------------------------------------------------
// Method: TrimStr
//
// Purpose: Removes whitespace characters from both ends of the
//	    supplied string.
//
// Parameters: 
//	String s [in] - String to trim
//
// Returns: String (HTML)
//--------------------------------------------------------------------
function TrimStr(s)
{
	var oRegEx = new RegExp("^(\\s*)([\\W\\w]*)(\\b\\s*$)","gim");
   	return s.replace(oRegEx,"$2");
}

//--------------------------------------------------------------------
// Method: ConfirmDeletion
//
// Purpose:
//
// Parameters:
//	String sMsgTxt [in] - 
//	String sReplaceTxt [in] - 
//
// Returns: Boolean
//--------------------------------------------------------------------
function ConfirmDeletion(sMsgTxt, sReplaceTxt)
{
	sMsgTxt = TextReplace(sMsgTxt,"%s",sReplaceTxt);

	var bDelete = window.confirm(sMsgTxt);

	return bDelete;
}

//--------------------------------------------------------------------
// Method: TextReplace
//
// Purpose: Replaces placeholder text with the specified value(s)
//
// Parameters:
//	String sTxt [in] - Text to parse
//	String|Array oFindTxt [in] - Placeholder(s) to locate
//	String|Array oReplaceTxt [in] - Replacement value(s)
//
// Returns: String
//--------------------------------------------------------------------
function TextReplace(sTxt,oFindTxt,oReplaceTxt)
{
	var sTmp = sTxt;

	if (typeof(oFindTxt) == "string")
		sTmp = sTmp.replace(new RegExp(oFindTxt,"gi"),oReplaceTxt);
	else if (typeof(oFindTxt) == "object" && typeof(oReplaceTxt) == "object" && oFindTxt.length == oReplaceTxt.length)
	{
		for (i = 0; i < oFindTxt.length; i++)
			sTmp = sTmp.replace(new RegExp(oFindTxt[i],"gi"),oReplaceTxt[i]);
	}

	return sTmp;
}

//--------------------------------------------------------------------
// Function: CancelEvent
//
// Purpose: Cancels the current event.
//
// Parameters: None
//
// Returns: None
//--------------------------------------------------------------------
function CancelEvent(oEvent)
{
	oEvent.cancelBubble = true;
	oEvent.returnValue = false;
}
