var gstrLastHelpClicked = null
var gstrReturnRevealHelp
var gstrReturnRevealDate

/// <summary>
/// Returns the style from css file for this project
/// </summary>
/// <param name="strClassName">The name of the class being returned</param>
/// <returns>A style object containing all the attributes or said style</returns>
function getStyleClass(strClassName) {

	var re = new RegExp("\\." + strClassName + "$", "gi");

	if (window.document.all)
	{
		for (var s = 0; s < window.document.styleSheets.length; s++)
		{
			for (var r = 0; r < window.document.styleSheets[s].rules.length; r++)
			{
				if (window.document.styleSheets[s].rules[r].selectorText.search(re) != -1)
				{
					return window.document.styleSheets[s].rules[r].style;
				}
			}
		}
	}
	else if (window.document.getElementById)
	{
		for (var s = 0; s < window.document.styleSheets.length; s++)
		{
			for (var r = 0; r < window.document.styleSheets[s].cssRules.length; r++)
			{
				if (window.document.styleSheets[s].cssRules[r].selectorText.search(re) != -1)
				{
					window.document.styleSheets[s].cssRules[r].sheetIndex = s;
					window.document.styleSheets[s].cssRules[r].ruleIndex = s;
					return window.document.styleSheets[s].cssRules[r].style;
				}
			}
		}
	}
	else if (window.document.layers)
	{
		return window.document.classes[strClassName].all;
	}
	
	return null;
}

/// <summary>
/// Gets a specific attribute's value from a class
/// </summary>
/// <param name="strClassName">The name of the class being referenced</param>
/// <param name="strPropertyName">The name of the property whose value is being sought</param>
/// <returns>The value of said property for said class</returns>
function getStyleClassProperty(strClassName, strPropertyName)
{
	var styleClass = getStyleClass(strClassName);

	if(styleClass) {
		var styleClassProperty = styleClass.getAttribute(strPropertyName)
		return styleClassProperty;
	}
	else
	{ 
		return null;
	}
}

/// <summary>
/// Keeps a drag event from occuring
/// </summary>
function cancelDragEvent()
{
    window.event.returnValue = false;
}

/// <summary>
/// Keeps a select event from occuring
/// </summary>
function cancelSelectEvent()
{
    window.event.returnValue = false;
}

/// <summary>
/// Retrieves the top coordinate of an object
/// </summary>
/// <param name="obj">The object whose coordinates are being sought</param>
/// <returns>An integer representing the Y position of the object</returns>
function getTopCoordinate(obj)
{
	try
	{
		var intYPos = obj.offsetTop;
		var objTemp = obj.offsetParent;

		while (objTemp != null) {
			intYPos += objTemp.offsetTop;
			objTemp = objTemp.offsetParent;
		}

		return(intYPos + g_intSkinOffsetTop);
	}
	catch(e)
	{
		return null;
	}	
}

/// <summary>
/// Retrieves the middle top coordinate of the object
/// </summary>
/// <param name="obj">The object whose coordinates are being sought</param>
/// <returns>An integer representing the middle Y position of the object</returns>
function getTopMiddleCoordinate(obj)
{
	var intTopCoord = getTopCoordinate(obj);
	var intHeight = obj.clientHeight;
	
	return Math.round(intRightCoord + (intHeight / 2));
}

/// <summary>
/// Retrieves the left coordinate of an object
/// </summary>
/// <param name="obj">The object whose coordinates are being sought</param>
/// <returns>An integer representing the X position of the object</returns>
function getLeftCoordinate(obj)
{
	try
	{
		var intXPos = obj.offsetLeft;
		var objTemp = obj.offsetParent;

		while(objTemp != null) {
			intXPos += objTemp.offsetLeft;
			objTemp = objTemp.offsetParent;
		}
	    return(intXPos + g_intSkinOffsetLeft);
	}
	catch(e)
	{
		return null;
	}	
}

/// <summary>
/// Retrieves the middle left coordinate of the object
/// </summary>
/// <param name="obj">The object whose coordinates are being sought</param>
/// <returns>An integer representing the middle X position of the object</returns>
function getLeftMiddleCoordinate(obj)
{
	var intLeftCoord = getLeftCoordinate(obj);
	var intWidth = obj.clientWidth;
	
	return Math.round(intLeftCoord + (intWidth / 2));
}

/// <summary>
/// Retrieves the name or abbreviation of the current month
/// </summary>
/// <param name="blnAbbrev">True if the abbreviation for the month should be returned</param>
/// <returns>A string name or abbreviation of the month</returns>
function getMonthName(blnAbbrev)
{
	var strMonth;
	var strAbbrev;
	
	switch(this.getMonth())
	{
		case 0 :
			strMonth = "January";
			strAbbrev = "Jan";
			break;
		
		case 1 :
			strMonth = "February";
			strAbbrev = "Feb";
			break;
	
		case 2 :
			strMonth = "March";
			strAbbrev = "Mar";
			break;
	
		case 3 :
			strMonth = "April";
			strAbbrev = "Apr";
			break;
	
		case 4 :
			strMonth = "May";
			strAbbrev = "May";
			break;
	
		case 5 :
			strMonth = "June";
			strAbbrev = "Jun";
			break;
	
		case 6 :
			strMonth = "July";
			strAbbrev = "Jul";
			break;
	
		case 7 :
			strMonth = "August";
			strAbbrev = "Aug";
			break;
	
		case 8 :
			strMonth = "September";
			strAbbrev = "Sep";
			break;
	
		case 9 :
			strMonth = "October";
			strAbbrev = "Oct";
			break;
	
		case 10 :
			strMonth = "November";
			strAbbrev = "Nov";
			break;
	
		case 11 :
			strMonth = "December";
			strAbbrev = "Dec";
			break;
	
		default :
			strMonth = "Unknown";
			strAbbrev = "Unk";
	}
	
	if(blnAbbrev)
		return strAbbrev;
	else
		return strMonth;
}

Date.prototype.getMonthName = getMonthName;

/// <summary>
/// Swaps out the value of a specified variable in the query string with a new value
/// </summary>
/// <param name="strVariable">The variable to look for</param>
/// <param name="strValue">The new value of the variable</param>
/// <param name="strUrl">The url to modify or null to use the current window url</param>
/// <returns>The URL with the new value in place</returns>
function queryStringSwapVariable(strVariable, strValue, strUrl)
{
	var blnFound = false;

	if(strUrl == null)
		strUrl = window.location.href;
	
	arrUrl = strUrl.split("?");
	
	if(arrUrl.length > 1)
	{
		var arrQueryString = arrUrl[1].split("&");
		
		for(var i = 0; i < arrQueryString.length; i++)
		{
			var dontJoin = false;
			var arrVariable = arrQueryString[i].split("=");
			
			if(arrVariable[0].toLowerCase() == strVariable.toLowerCase())
			{
				blnFound = true;
				arrVariable[1] = strValue;
				
				if(strValue == null)
				{
					arrQueryString.splice(i, 1);
					dontJoin = true;
				}
			}
	
			if(!dontJoin)
				arrQueryString[i] = arrVariable.join("=");
		}
		
		arrUrl[1] = arrQueryString.join("&");
	}

	if(!blnFound && strValue != null)
	{
		if(arrUrl.length > 1)
			return strUrl + "&" + strVariable + "=" + strValue;
		else
			return strUrl + "?" + strVariable + "=" + strValue;
	}
	else
	{
		return arrUrl.join("?");
	}
}

/// <summary>
/// Swaps out the value of action in the query string with a new action
/// </summary>
/// <param name="strNewAction">The new action</param>
/// <returns>The URL with the new action in place</returns>
function queryStringSwapAction(strNewAction)
{
	return queryStringSwapVariable("action", strNewAction);
}

/// <summary>
/// Toggles hiding and showing of different sections
/// </summary>
/// <param name="objImg">Image object to modify</param>
function toggleShowHideSection(objImg)
{
    var objRow = objImg.parentElement;
    while(objRow.rowType != "SectionHeader" && objRow.rowType != "ButtonBar")
    {
        objRow = objRow.parentElement;
    }
    
    if(objRow.collapsed == "true")
    {
		objImg.src = changeImage(objImg.src, objImg.imgsrc_collapse);    
		showSection(objRow);
	}
	else
	{
		objImg.src = changeImage(objImg.src, objImg.imgsrc_expand);    
		hideSection(objRow);
	}
}

/// <summary>
/// Used to show a previously hidden row
/// </summary>
/// <param name="objRow">Row to display</param>
function showSection(objRow)
{
	objRow.collapsed = "false";
	var objTable = objRow.offsetParent;

	for(var i = (objRow.rowIndex + 1); i < objTable.rows.length; i++)
	{
		if(objTable.rows[i].rowType != "SectionHeader" && objTable.rows[i].rowType != "ButtonBar")
		{
			objTable.rows[i].style.display = "";
		}
		else
		{
			return;
		}
	}
}

/// <summary>
/// Used to hide a row
/// </summary>
/// <param name="objRow">Row to display</param>
function hideSection(objRow)
{
	objRow.collapsed = "true";
	var objTable = objRow.offsetParent;
	
	for(var i = (objRow.rowIndex + 1); i < objTable.rows.length; i++)
	{
		if(objTable.rows[i].rowType != "SectionHeader" && objTable.rows[i].rowType != "ButtonBar")
		{
			objTable.rows[i].style.display = "none";
		}
		else
		{
			return;
		}
	}
}

/// <summary>
/// Returns the value of the checked radio button
/// </summary>
/// <param name="objRadio">Radio button to retrieve value for</param>
function getCheckedRadioOptionValue(objRadio)
{
	for(var i = 0; i < objRadio.length; i++)
	{
		if(objRadio[i].checked)
			return objRadio[i].value;
	}
	
	return null;
}

/// <summary>
/// Returns the index of the checked radio button
/// </summary>
/// <param name="objRadio">Radio button to retrieve index for</param>
function getCheckedRadioOptionIndex(objRadio)
{
	for(var i = 0; i < objRadio.length; i++)
	{
		if(objRadio[i].checked)
			return i;
	}
	
	return null;
}

/// <summary>
/// expands and collapses hierarchy nodes
/// </summary>
/// <param name="intSpanID">ID of span tag to expand or collapse</param>
function changeNode(intSpanID) 
{
    var objSpan = eval("g_doc.span" + intSpanID);
    var objImage = eval("g_doc.handle" + intSpanID);
	
    if(objSpan.style.visibility == "hidden")
    {
		objSpan.style.visibility = "";
		objSpan.style.position = "";
		objSpan.style.display = "";
		objImage.src = changeImage(objImage.src, objImage.imgsrc_collapse);    
    }
    else
    {
        objSpan.style.visibility = "hidden";
        objSpan.style.position = "absolute";
        objSpan.style.display = "none";
		objImage.src = changeImage(objImage.src, objImage.imgsrc_expand);    
    }
}

/// <summary>
/// Launches help window in side panel.
/// </summary>
/// <param name="helpUrl">Absolute Url of help file to display</param>
function Help(helpUrl)
{ 
	// maximize main window
	var helpWidth = 225;
	var height = screen.availHeight;
	var width = screen.availWidth - helpWidth - 10;
	
	if (window.screen) 
	{
		window.moveTo(0,0);
		window.resizeTo(width ,height);
	}

	window.open(helpUrl,'XTHelp','width=' + helpWidth + ',height=' + (height-30) + ',left=' + width + ',dependent=1,top=0,toolbar=no,resizable=yes,location=no,directories=no,status=no,menubar=no,scrollbars=no,titlebar=no');
}

/// <summary>
/// Restore window fo full size after help window closes
/// </summary>
function restoreOpenerSettings()
{
	this.window.close();
	//window.opener.moveTo(intOpenerX, intOpenerY);
	window.opener.resizeTo(screen.availWidth, screen.availHeight);
}

/// <summary>
/// Resize an item to offsetSize less (or more) than the current screen height
/// </summary>
/// <param name="strName">Object to get height of</param>
/// <param name="offsetSize">Size to offset object height by</param>
function resizeRelativeToWindow(strName, offsetSize)
{
	var item = document.getElementById(strName);
	item.style.height = screen.availHeight - offsetSize;
}

/// <summary>
/// allow enabling of controls
/// </summary>
/// <param name="obj">parent object</param>
/// <param name="control">name of control to disable</param>
function enableInput(obj, control)
{
var element = getFormElement(control);

// need a way to disable validation
if(obj.value == "")
	{
		element.disabled = true;
		element.value = "";
	}
else
	element.disabled = false;	
}

/// <summary>
/// Navigates the page to a new destination
/// </summary>
/// <param name="strValue">The querystring variable to swap</param>
/// <param name="strUrl">The current URL</param>
/// <param name="selectListID">The ID of the select list</param>
function SelectNavigateTo(strValue, strUrl, selectListID)
{	
	//var objVal = document.getElementById(selectListID); 
	//document.pageform.action = queryStringSwapVariable(strValue, objVal.options[objVal.selectedIndex].value, strUrl);
	document.pageform.submit();
}

/// <summary>
/// Used in Keywords pages to display a confimration message before deleting an item
/// </summary>
/// <param name="obj">Object calling the function</param>
/// <param name="message">The confirmation message to display</param>
function ConfirmDelete(obj, message)
{ 
	if(confirm(message)) 
		eval(obj.href); 
	else 
		return false; 
} 

function redirectV3Search(projectpath, control)
{	
	var element = getFormElement(control);		
	var url = projectpath + "/user/sch/sch_user_display.asp?action=search&Search=" + element.value;	
	document.pageform.action = url;
	document.pageform.submit();
	
}



function GetQueryStringItem(strItem)
{
	var arrQS = new Array();
	var arrValues = new Array();
	var arrItems = new Array();
	var blnFound = false;
	var strCurLocation = "";

	var strCurLocation = window.location.toString();

	arrQS = strCurLocation.split("?")

	if(arrQS.length < 2)
	{
		return null;
	}
	else
	{
		arrItems = arrQS[1].split("&")
		
		for(var i = 0; i < arrItems.length; i++)
		{
			arrValues = arrItems[i].split("=");
			
			if(arrValues[0].toLowerCase() == strItem.toLowerCase())
				return arrValues[1];
		}
	}
}


//==============================================================================
// Closes all help layers and reveals the layer containing the specified help
//==============================================================================

function revealHelp(strSpanID) {

    var objImage = eval("document.img" + strSpanID)

    if(gstrLastHelpClicked != null && gstrLastHelpClicked != strSpanID){
        hideHelp(gstrLastHelpClicked)
    }

    var objHelp = eval("help" + strSpanID)

    objHelp.style.visibility = "visible"
    eval("document.img" + strSpanID).onclick = function onclick(event){ hideHelp(strSpanID) }
    eval("document.img" + strSpanID).src = "/media/images/system/forms/hide_help_button.gif"

    window.status = "Click to hide help"
    gstrLastHelpClicked = strSpanID

}

//==============================================================================
// Closes a certain help layer
//==============================================================================

function hideHelp(strSpanID) {

    var objHelp = eval("help" + strSpanID);

    objHelp.style.visibility = "hidden"
    eval("document.img" + strSpanID).onclick = function onclick(event){ revealHelp(strSpanID) }
    eval("document.img" + strSpanID).src = "/media/images/system/forms/show_help_button.gif"
    window.status = "Click to show help"
    gstrLastHelpClicked = null
}

//==============================================================================
// Closes all help layers and reveals the layer containing the specified help
//==============================================================================

function toggleDate(strSpanID, objImg)
{
    var objDate = eval("date" + strSpanID)
    if(objDate.style.display == "none")
        revealDate(strSpanID, objImg);
    else
        hideDate(strSpanID, objImg);
}

//==============================================================================
// Closes all help layers and reveals the layer containing the specified help
//==============================================================================

function revealDate(strSpanID) {

    var objImage = eval("document.img" + strSpanID)

    if(gstrLastDateClicked != null && gstrLastDateClicked != strSpanID){
        hideDate(gstrLastDateClicked);
    }

    var objDate = eval("date" + strSpanID)

    objDate.style.visibility = "visible"
    eval("document.img" + strSpanID).onclick = function onclick(event){ hideDate(strSpanID) }
    eval("document.img" + strSpanID).src = "/media/images/system/forms/hide_date_button.gif"

    window.status = "Click to hide Date"
    gstrLastDateClicked = strSpanID
}
