//Global Variables
var g_intSkinOffsetLeft = 0;
var g_intSkinOffsetTop = 0;
var g_strComponentResourceDirectory = null;

if(document.pageform)
	var g_doc = document.pageform.all;
else
	var g_doc = document.all;

/// <summary>
/// Removes a string from text
/// </summary>
/// <param name="strSource">The source text</param>
/// <param name="strRemove">The string to remove</param>
/// <returns>The modified string</returns>
function removeString(strSource, strRemove)
{
	re = eval("/" + strRemove + "/g");
	return strSource.replace(re, "");
}

/// <summary>
/// Sets the form to be of a particular method type
/// </summary>
/// <param name="sNewFormAction">The method to set on the form</param>
/// <param name="strFormMethod">The method to set on the form</param>
function SetFormMethod(strFormMethod)
{
	var objForm = document.forms[0];
	if(objForm.__VIEWSTATE)
	{
		objForm.__VIEWSTATE.name = 'NOVIEWSTATE';
		objForm.__VIEWSTATE.value = 'NONE';
	}
	objForm.method = strFormMethod;
}

/// <summary>
/// Gets the specified form element and returns the object
/// </summary>
/// <param name="strFieldName">The name of the object to return</param>
/// <returns>The object corresponding to that name</returns>
function getFormElement(strFieldName)
{
	strFieldName = strFieldName.toLowerCase();

	for(var i = 0; i < document.pageform.elements.length; i++)
	{
		var objElement = findFormControl(document.pageform.elements[i], strFieldName);
		if(objElement)
			return objElement;
	}

	var arrElements = document.pageform.getElementsByTagName("INPUT");
	for(var i = 0; i < arrElements.length; i++)
	{
		var objElement = findFormControl(arrElements[i], strFieldName);
		if(objElement)
			return objElement;
	}

	arrElements = document.pageform.getElementsByTagName("A");
	strFieldNameAlt = strFieldName + "_link";
	
	for(var i = 0; i < arrElements.length; i++)
	{
		var objElement = findFormControl(arrElements[i], strFieldName);
		if(objElement)
			return objElement;
	}
	
	return null;
}

function findFormControl(objElement, strFieldName)
{
	var strObjectName = objElement.name.toLowerCase();
	var intIndex = strObjectName.lastIndexOf(strFieldName)
	var intAssumedIndex = strObjectName.length - strFieldName.length;
	var charBefore = strObjectName.substr(intIndex - 1, 1);

	if(intIndex > 0 && intIndex == intAssumedIndex && 
		(charBefore == "$" || charBefore == ":" || charBefore == "_"))
	{
		return objElement;
	}

	return null;
}

/// <summary>
/// Sets the cursor focus to the appropriate field when the page loads
/// </summary>
/// <param name="strDefaultFocusField">The name of the field to set the focus to</param>
function setDefaultFocus(strDefaultFocusField)
{
	if (!isBlank(strDefaultFocusField))
	{
		var objFormElement = getFormElement(strDefaultFocusField);
		if (objFormElement != null)
		{
			objFormElement.focus();
		}
	}
}

/// <summary>
/// Simulate a click on a form button. This will allow normal form
/// processing, including validation.
/// </summary>
/// <param name="strName">The name of the button to click</param>
function clickButtonOnEnter(strName)
{
	if (event.keyCode == 13)
	{
		if(getFormElement(strName) != null);
		{
			var objElement = getFormElement(strName);
			objElement.click();
		}	
		return false;
	}
}

/// <summary>
/// Clicks a submit button on a form when the enter key is pressed. This will
/// allow normal form processing including validation.
/// </summary>
/// <param name="strFormID">Unique identifier for form submit button</param>
function submitFormOnEnter(strFormID)
{
	if (event.keyCode == 13)
	{
		var arrElements = document.pageform.getElementsByTagName("INPUT");
		for(var i = 0; i < arrElements.length; i++)
		{
			var objElement = arrElements[i];
			if(objElement.formID == strFormID + '_submit')
				{
					objElement.click();
				}
		}
		return false;
	}
}	

/// <summary>
/// Clicks a submit button on a form when the enter key is pressed. This will
/// allow normal form processing including validation. (FireFox AND IE compliant version)
/// </summary>
/// <param name="strFormID">Unique identifier for form submit button</param>
/// <param name="event">Pass in event from the text area/field</param>
function submitFormOnEnterFF(strFormID, e)
{
	if (e.keyCode == 13)
	{
		var arrElements = document.pageform.getElementsByTagName("INPUT");
		for(var i = 0; i < arrElements.length; i++)
		{
			var objElement = arrElements[i];
			var formID = objElement.getAttribute('formID');

			if(formID == strFormID + '_submit')
			{
				objElement.click();
			}
		}
		return false;
	}
}


/// <summary>
/// Inserts an object into a specified element slot in an ordered array,
///	moving the rest down one element slot
/// </summary>
/// <param name="obj">The object being inserted into the array</param>
/// <param name="intIndex">The requested slot to put the object</param>
function weldElement(obj, intIndex){

    var blnFound = false;
    var strTemp, strTemp2;

    if(intIndex < this.length) {
        var intLength = this.length + 1;
        for(var i = intIndex; i < (intLength); i++) {
            if(i == intIndex) {
                strTemp = this[i];
                this[i] = obj;
                blnFound = true;
            } else if (blnFound == true && i < this.length) {
                strTemp2 = this[i];
                this[i] = strTemp;
                strTemp = strTemp2;
            } else if (blnFound == true && i == this.length) {
                this[i] = strTemp;
            }
        }
    } else {
        this[this.length] = obj;
    }
}

Array.prototype.weld = weldElement;

/// <summary>
/// Checks to see if a string is null or empty
/// </summary>
/// <param name="str">The string to check</param>
/// <returns>True if the string is null or empty</returns>
function isBlank(str)
{
    return((str == "" || str == null) ? true : false);
}

/// <summary>
/// Decodes a Guid
/// </summary>
/// <param name="str">The string to decode</param>
/// <returns>The decoded Guid</returns>
function decodeGuid(str)
{
	if(str != null)
	    return(str.replace(/_/g, "-"));
	else
		return null;
}

/// <summary>
/// Encodes a Guid
/// </summary>
/// <param name="str">The string to encode</param>
/// <returns>The encoded Guid</returns>
function encodeGuid(str)
{
	if(str != null)
	    return(str.replace(/-/g, "_"));
	else
		return null;
}

/// <summary>
/// Writes a message to the page
/// </summary>
/// <param name="strText">The string to write</param>
/// <param name="blnAppend">If true, appends the message to the current set of messages</param>
function message(strText, blnAppend)
{
	var objMessage = document.getElementById("MessageWrite");
	if(objMessage == null)
	{
		var msg = document.createElement("SPAN");
		msg.setAttribute("id", "MessageWrite");
		msg.setAttribute("style", "font-size:11px; color: #000000; font-family: Verdana");
		document.body.appendChild(msg);
		objMessage = document.getElementById("MessageWrite");
	}
	
	if(objMessage.innerHTML == "")
		objMessage.innerHTML = "<br/>";

	if(!blnAppend)
	{
		objMessage.innerHTML = "<br/><br/>" + strText;
	}
	else
	{
		objMessage.innerHTML += "<br/>" + strText;
	}
}

/// <summary>
/// Trims trailing and leading spaces
/// </summary>
/// <param name="strValue">The string to trim</param>
/// <returns>The trimmed string</returns>
function trim(strValue)
{
	//not Netscape tested
	if (strValue) {
		while(strValue.charCodeAt(0) == 32 || strValue.charCodeAt(0) == 13
            || strValue.charCodeAt(0) == 10) {
			strValue = strValue.substring(1, strValue.length)
		}

		while(strValue.charCodeAt(strValue.length - 1) == 32 ||
            strValue.charCodeAt(strValue.length - 1) == 13 ||
            strValue.charCodeAt(strValue.length - 1) == 10) {

            strValue = strValue.substring(0, strValue.length - 1)
		}
	}

    return strValue;
}

/// <summary>
/// Given an image source, will replace keywords in the source
/// </summary>
/// <param name="str">The string representing the image source</param>
/// <param name="strLookFor">The string to search for</param>
/// <param name="strReplaceWith">The replacement string</param>
/// <returns>The altered image source</returns>
function getImage(str, strLookFor, strReplaceWith)
{
	re = eval("/" + strLookFor + "/g");
	return(str.replace(re, strReplaceWith));	
}

/// <summary>
/// Changes the source of an image if the new source is valid
/// </summary>
/// <param name="strOriginal">The string representing the original image source</param>
/// <param name="strNew">The string representing the new image source</param>
/// <returns>The valid image source</returns>
function changeImage(strOriginal, strNew)
{
	if (strNew != null && strNew != "" && strNew != "[null]" && strNew != "undefined")
		return strNew;
	else
		return strOriginal;
}

/// <summary>
/// Creates a simple popup window with the specified options.
/// </summary>
/// <param name="strUrl">String representing the url to display</param>
/// <param name="intHeight">Height of the window, if this value is not specified it will calculate the height based on screen size.</param>
/// <param name="intWidth">Width of the window, if this value is not specified it will calculate the width based on screen size.</param>
/// <param name="intLeft">Position of the left side of the window, if this value is not specified it will be centered from the left based on screen size.</param>
/// <param name="intTop">Position of the top of the window, if this value is not specified it will be centered from the top based on screen size.</param>
/// <returns>Reference to the newly created window</returns>
function openSimpleWindow(strUrl, intHeight, intWidth, intLeft, intTop)
{
	
	
	if (window.screen) {
	    if(intWidth == null)	
			intWidth = Math.floor(screen.availWidth/3);
		if(intHeight == null)	    
			intHeight = Math.floor(screen.availHeight/3);
	    if(intLeft == null)  
			intLeft = Math.floor((screen.availWidth-intWidth)/2);
	    if(intTop == null)  
			intTop = Math.floor((screen.availHeight-intHeight)/2);
	}
	else{
		intWidth = 640/3;
		intHeight = 480/3;
		intLeft = intWidth/2;
		intTop = intHeight/2;	
	}	
  
  return window.open(strUrl, "_blank", "scrollbars=no,status=no,resizable=no,height=" + intHeight + ",location=0,width=" + intWidth + ",left=" + intLeft + ",top=" + intTop);
}

/// <summary>
/// Creates a new popup window with the specified options.
/// </summary>
/// <param name="strUrl">String representing the url to display</param>
/// <param name="sName">Name of the window</param>
/// <param name="bScrollbars">True if you want to display scroll bar</param>
/// <param name="bStatus">True if you want to display status bar</param>
/// <param name="bResizable">True if you want to resize the window</param>
/// <param name="intHeight">Height of the window</param>
/// <param name="intWidth">Width of the window</param>
/// <param name="intLeft">Position of the left side of the window</param>
/// <param name="intTop">Position of the top of the window</param>
/// <returns>Reference to the newly created window</returns>
function openWindow(strUrl, sName, bScrollbars, bStatus, bResizable, intHeight, intWidth, intLeft, intTop)
{
  var scroll = 0;
  var status = 0;
  var resizable = 0; 
  
  if(bScrollbars)
	scroll = 1;

  if(bStatus)	
    status = 1;
    
  if(bResizable)  
    resizable = 1;
  
  return window.open(strUrl, sName, "scrollbars=" + scroll + ",status=" + status + ",resizable=" + resizable + ",height=" + intHeight + ",location=0,width=" + intWidth + ",left=" + intLeft + ",top=" + intTop);
}

/// <summary>
/// Creates a new popup window centered in the screen.
/// </summary>
/// <param name="strUrl">String representing the url to display</param>
/// <param name="sName">Name of the window</param>
/// <param name="bScrollbars">True if you want to display scroll bar</param>
/// <param name="bStatus">True if you want to display status bar</param>
/// <param name="bResizable">True if you want to resize the window</param>
/// <param name="intHeight">Height of the window</param>
/// <param name="intWidth">Width of the window</param>
/// <returns>Reference to the newly created window</returns>
function openCenteredWindow(strUrl, sName, bScrollbars, bStatus, bResizable, intHeight, intWidth)
{
  var thisWindow;
  var cw = intWidth/2, ch = intHeight/2;	
	if (window.screen) {
	    cw = Math.floor((screen.availWidth-intWidth)/2);
	    ch = Math.floor((screen.availHeight-intHeight)/2);
	}	
	
  return openWindow(strUrl, sName, bScrollbars, bStatus, bResizable, intHeight, intWidth, cw, ch)
  
}

/// <summary>
/// Shows/hides a layer.
/// </summary>
/// <param name="id">The id of the layer</param>
function toggleClick(id) {
	var ToolTip = document.getElementById(id);
    if (ToolTip.style.visibility == "visible") {
        ToolTip.style.visibility = "hidden";
    } else {
        ToolTip.style.visibility = "visible";
    }
    return;
}

/// <summary>
/// Shows/hides a layer.
/// </summary>
/// <param name="id">The id of the layer</param>
function toggleClickDisplay(id) {
	var ToolTip = document.getElementById(id);
    if (ToolTip.style.display == "") {
        ToolTip.style.display = "none";
    } else {
        ToolTip.style.display = "";
    }
    return;
}

function showDesignComponent(id)
{
	var div = eval("document.all." + id);
	if(div.id == id)
	{
		div.originalColor = div.style.backgroundColor;
		div.style.backgroundColor = "#EEEEEE";
	}
	else
	{
		for(i = 0; i < div.length; i++)
		{
			var divElement = div[i];
			divElement.originalColor = divElement.style.backgroundColor;
			divElement.style.backgroundColor = "#EEEEEE";
		}
	}
}

function hideDesignComponent(id)
{
	var div = eval("document.all." + id);
	if(div.id == id)
	{
		div.style.backgroundColor = div.originalColor;
	}
	else
	{
		for(i = 0; i < div.length; i++)
		{
			var divElement = div[i];
			divElement.style.backgroundColor = divElement.originalColor;
		}
	}
}


// overly simplistic test for IE
isIE = (document.all ? true : false);
// both IE5 and NS6 are DOM-compliant (well, sort of...)
isDOM = (document.getElementById ? true : false);

// get the true offset of anything on NS4, IE4/5 &amp; NS6, even if it's in a table!
function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt,"Left"); }
function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt,"Top"); }

function getAbsPos(elt,which) {
	iPos = 0;
	while (elt != null) {
	iPos += elt["offset" + which];
	elt = elt.offsetParent;
	}
	return iPos;
}

// Returns a reference to the layer as an object
function getLayerObject(divname) {
	var style;
	if (isDOM) { style = document.getElementById(divname).style; }
	else { style = isIE ? document.all[divname].style
						: document.layers[divname]; } // NS4
						
	return style;
}

// annoying detail: IE and NS6 store elt.top and elt.left as strings.
function moveBy(elt,deltaX,deltaY) {
	elt.left = parseInt(elt.left) + deltaX;
	elt.top = parseInt(elt.top) + deltaY;
}

function setLayerPosition(elt, positionername, isPlacedUnder) {
	var positioner;
	if (isIE)
	{
		positioner = document.all[positionername];
	}
	else
	{
		if (isDOM)
		{
			positioner = document.getElementById(positionername);
		}
		else
		{
			// not IE, not DOM (probably NS4)
			// if the positioner is inside a netscape4 layer this will *not* find it.
			// I should write a finder function which will recurse through all layers
			// until it finds the named image...
			positioner = document.images[positionername];
		}
	}
	elt.left = getAbsX(positioner);
	elt.top = getAbsY(positioner) + (isPlacedUnder ? positioner.height : 0);
}

// ask if the user truly wants to delete the item selected
function deleteConfirmation(destination){
	if (confirm("Are you sure you wish to delete this?\nPress OK to continue.")) {
		window.location = destination; 
	}
}

// Global Offsite Link Management routine
function CheckTags() {
	var coll = document.all.tags("a");
	var iCount;
	if (location.href.indexOf("check_link") < 0) {
		var sHostName = location.hostname;
		for (iCount = 0; iCount < coll.length; iCount++) {
			if (coll(iCount).href != "" && coll(iCount).href.indexOf("mailto") < 0 && coll(iCount).href.indexOf("javascript") < 0 && coll(iCount).href.indexOf(sHostName) < 0) {
				coll(iCount).href = "http://" + sHostName + location.pathname + "?action=check_link&moduleid=7476df34-317b-4bf0-8f65-2578c300bd78&mode=user&url=" + escape(coll(iCount).href);
			}
		}
	}
}

