// generic script to set styles
function onLoad() {

    // Hack to prevent IE redraw issues - Part I of II
	var objSelect;
	objSelect = document.all.tags("SELECT");
	if (objSelect != null) {
		var j;
		for (j = 0; j < objSelect.length; j++) {
			if (objSelect[j].size > 1) {
				hideElement(objSelect[j].id);
			}			
		}		
	}	 
    // End Hack Part I
  
	// setWindowTimeout("applyStyleSheet()",0.25);
  
	// Hack to prevent IE redraw issues - Part II of II
	if (objSelect != null) {
		var j;
		for (j = 0; j < objSelect.length; j++) {
			if (objSelect[j].size > 1) {
				setWindowTimeout("showElement('" + objSelect[j].id + "')",0.5);
			}
		}		
	}	 
	// End Hack Part II
}    

function applyStyleSheet() {

	if (document.styleSheets.length <= 0) {
		try {
			var sCssPath = "../css/";
			var len = parent.document.styleSheets.length;
			var oStyle;
			//alert(sCssPath.substring(0,7));
			//alert("Stylesheet count: " + len);
			if (len <= 1) {
				// alert("No Stylesheet");
				oStyle = document.createStyleSheet(sCssPath + "global.css");
				oStyle = document.createStyleSheet(sCssPath + "arctic.css");  
			}
			else {
				var i;			
				for ( i = 0; i < len; i++ ) {         
					var sSrc = parent.document.styleSheets(i).href;
					//alert("Style sheet " + i + " is " + sSrc);
					sSrc = sSrc.toLowerCase();
					sSrc = sSrc.substring(0,7);
					switch (sSrc) {
					case	sCssPath:
							//alert(1);					
							break;
					default: 
							//alert(2);								
							if (parent.document.styleSheets(i).href != "undefined" && parent.document.styleSheets(i).href != "") {
								//alert (sCssPath + parent.document.styleSheets(i).href);
								oStyle = document.createStyleSheet(sCssPath + parent.document.styleSheets(i).href);		
							}
							break;
					}
				}
			}
		}
		catch (e) {
			// do nothing, fail silently...		
			// setElementInnerHtml("myTest","Error: " + e.description + "<br>"); // Used for debugging only
		}	
	}
}

function setWindowTimeout(funcName, iTimeSeconds) {
	window.setTimeout(funcName, iTimeSeconds * 1000);
}

function showElement(elemName) {

    if (elemName != "" && document.all[elemName] != null && document.all[elemName] != "undefined" && document.all[elemName] != undefined) {
       document.all[elemName].style.display = "block";       
    }
}

function hideElement(elemName) {
    if (elemName != "" && document.all[elemName] != null && document.all[elemName] != "undefined" && document.all[elemName] != undefined) {		
		document.all[elemName].style.display = "none";
	}
}

function toggleVisibility(elemName) {

    if (elemName != "" && document.all[elemName] != null && document.all[elemName] != "undefined" && document.all[elemName] != undefined) {		
		if (document.all[elemName].style.visibility == "" || document.all[elemName].style.visibility == "visible")
			document.all[elemName].style.visibility = "hidden";
		else
			document.all[elemName].style.visibility = "visible";
	}
}

function makeVisible(elemName) {
    if (elemName != "" && document.all[elemName] != null && document.all[elemName] != "undefined" && document.all[elemName] != undefined) {
       document.all[elemName].style.visibility = "visible";
    }
}

function makeInvisible(elemName) {
    if (elemName != "" && document.all[elemName] != null && document.all[elemName] != "undefined" && document.all[elemName] != undefined) {
       document.all[elemName].style.visibility = "hidden";
    }
}

function refreshPage() {

	document.parentWindow.location.reload();
}

// sets the color 
// Note: Page should have the following for this to work
//       <table id='tblHidden' class='DashNavArea' style='display:none;'><tr><td>Test</td></tr></table>
// sets the hilite colors
// Better version than the one above, it does not assume the table names 
function setHiliteColors(tblName, tblHidden) {

  if (tblName != "" && tblHidden != "" && document.all[tblName] != null && document.all[tblHidden] != null) {

     //alert(document.all[tblName].hlColor + " " + document.all[tblHidden].currentStyle.backgroundColor);
     if (document.all[tblHidden].currentStyle.backgroundColor != "transparent") {
        document.all[tblName].hlColor = document.all[tblHidden].currentStyle.backgroundColor;
        document.all[tblName].selColor = document.all[tblHidden].currentStyle.backgroundColor;
     }     
  } 
}

function elementExists(elemName) {
	
    if (elemName != "" && document.all[elemName] != null && document.all[elemName] != "undefined" && document.all[elemName] != undefined) {
		return true;
    }
    else {
		return false;
    }
}

function setElementInnerHtml(elemName, strHtml) {	
    
    if (elemName != "" && document.all[elemName] != null && document.all[elemName] != "undefined" && document.all[elemName] != undefined) {
		document.all(elemName).innerHTML = strHtml;
    }	
}

function getItemSelectedValue(selObject) {
	if (selObject.selectedIndex >= 0) {
		return selObject.options(selObject.selectedIndex).value;
	}
	else {
		return "";
	}
}

function toggleElemDisplay(elemName) {

    if (elemName != "" && document.all[elemName] != null && document.all[elemName] != "undefined" && document.all[elemName] != undefined) {

		if (document.all[elemName].style.display == "none") 
			document.all[elemName].style.display = "display";
		else
			document.all[elemName].style.display = "none";
    }
}

function getParentElement(oElem,sParentTagName) {
	var pElem = oElem.parentElement, i = 0;
	sParentTagName = sParentTagName.toUpperCase();
	while (pElem.tagName != sParentTagName && pElem.tagName != "BODY") {
		i++;
		if (i >= 100) {
			// don't go further than 100 elements
			break;
		}
		pElem = pElem.parentElement;
	} 
	if (pElem != null && pElem.tagName == sParentTagName) {
		return pElem;
	} else {
		return null;
	}
}

function signalParentClose()
{
	if (window.parent != null)
	{
		window.parent.closeChildFrame();
	}
	else
	{
		alert("null");
	}
}

function closeChildFrame()
{
	CloseFrameDiv();
}

function MouseOverExpandDiv(strDivId)
{
	var divObj = document.all[strDivId];
	
	if (divObj != null)
	{
		divObj.height = "100px";
	}
}

function MouseOutContractDiv(strDivId)
{
var divObj = document.all[strDivId];
	
	if (divObj != null)
	{
		divObj.height = "25px";
	}
}


/* Function to show a  div and set the id of the control accordingly */
/*
	Position control is idetified by strPosControlName + strId + "-" + strIdEx
	Div is identified by strDivName
	ID is set on objIdControl
*/
function ShowSelectionDiv(strId,strIdEx,strPosControlName,strDivName,objIdControl)
{
		var imgObj,parentObj,divObj;
		var posTop,posLeft;
		
		
		if (strIdEx == "")
		{
			imgObj = document.all[strPosControlName +  strId ];
		}
		else
		{
			imgObj = document.all[strPosControlName +  strId + "-" + strIdEx];
		}
		
		divObj = document.all[strDivName];
		
		if (imgObj != null && divObj != null)
		{	
			
			
			divObj.style.left = window.event.x - divObj.offsetWidth ;
			
			
			divObj.style.top = window.event.y + imgObj.height/2 + document.body.scrollTop;
			
			
			if (objIdControl != null)
			{
				objIdControl.value = strId;
			}
			divObj.style.visibility = "visible";
			
			
		}
		
}

/* Function to show a  div and set the category div selection */
/*
	Position control is idetified by strPosControlName + strId + "-" + strIdEx
	Div is identified by strDivName
	nVal values are set on strCategoryControl
	ID is set on objIdControl
*/
function ShowSelectionDivCategory(strId,strIdEx,strPosControlName,strDivName,objIdControl,nVal,strCategoryControl)
{
		var i;
		var chkBox;
			
		for ( i = 0; i < 32 ; ++i)
		{
			chkBox = document.all[strCategoryControl + "_lstBucketItems_" + i];
				
			if (chkBox != null)
			{
				
				if (( nVal & Math.pow(2,i))  != 0)
				{
					chkBox.checked = 1;
				}
				else
				{
					chkBox.checked = 0;
				}
				
			}
		}
		
		ShowSelectionDiv(strId,strIdEx,strPosControlName,strDivName,objIdControl);
		
}

/* function to hide a div */
function HideDiv(strDivId)
{
		var divObj;
		
		divObj = document.all[strDivId];
		
		if (divObj != null)
		{
			divObj.style.visibility = "hidden";
			
		}
}

function HideDiv2(strDivId)
{
		var divObj;
		
		divObj = document.all[strDivId];
		
		if (divObj != null)
		{
			divObj.style.display = "none";
			
		}
}

/* function to resize a frame */
function ResizeFrameToContent(strFrameId)
{
	var divFrameObj,divFrameObj2,divObj;

	divFrameObj = document.all[strFrameId];
	divFrameObj2 = document.frames.item(strFrameId);
	
	
	if (divFrameObj != null && divFrameObj2 != null)
	{
		//alert(divFrameObj2.document.body.scrollHeight + "  " + divFrameObj.document.body.scrollHeight);
		divFrameObj.height = divFrameObj2.document.body.scrollHeight + 20;
		divFrameObj.width = "100%";
	}
}

/* Function to open an URL in an IFRAME */

function OpenUrlInFrame(strUrl,strFrameName)
{
	var frameObj;
	
	frameObj = document.all["smalldetails"];
				
	if (frameObj != null)
	{
		frameObj.src = strUrl;
	}
}

/* Tree view control functions */

/* 
	Clears all selected node from a tree view control 
	
*/
function TreeViewClearAllSelectedNode(strSelectName,strNodeName)
{
	var col1 = document.all.tags("select");
	var myselect;
	
	for ( var i = 0 ; i < col1.length ; ++i)
	{
		var strName = col1(i).name;
		if (strName.indexOf(strSelectName) >= 0)
		{
			myselect = col1(i);
		}
	}
	
	if (myselect != null)
	{
		for (var j=0; j< myselect.options.length; j++) 
		{
			myselect.options[j].selected = false;
			var mydiv = strNodeName + myselect.options[j].value;
			if (document.all[mydiv] != null)
			{
				document.all[mydiv].style.backgroundColor = 'white';
				document.all[mydiv].style.color = '';
			}
		}
	}
}


/* Function to set selected item text on a div */
function SetTextSelectedItems(strSelectName,strValidatorName,strDivName)
{
	var i,j;
	var col1 = document.all.tags("select");
	var myselect;
	var selText;
	var bFlag = false;
	
	for ( var i = 0 ; i < col1.length ; ++i)
	{
		var strName = col1(i).name;
		if (strName.indexOf(strSelectName) >= 0)
		{
			myselect = col1(i);
		}
	}
	
	if (myselect != null)
	{
		selText = "<table>";
		for (var j=0; j< myselect.options.length && myselect.selectedIndex >= 0; j++) 
		{
			if(myselect.options[j].selected == true) 
			{
				if (document.all[strValidatorName] != null && bFlag != true)
				{
					document.all[strValidatorName].innerText = "";
					bFlag = true;
				}
				selText = selText +  "<TR><td>" + myselect.options[j].text + "</td></tr>";
				
			}
		}
		selText = selText +  "</table>";
	}
	
	if (document.all[strDivName] != null)
	{
		document.all[strDivName].innerHTML = selText;
	}
}

/* Node click function */
function TreeViewNodeClick(strSelectName,strNodeName,strValidatorName,strId,nPos)
{
	var mydiv = strNodeName + strId;
	var col1 = document.all.tags("select");
	var myselect;
	var bFlag = false;
	
	myselect = document.all[strSelectName];
	
	if (myselect == null)
	{
		for ( var i = 0 ; i < col1.length ; ++i)
		{
			var strName = col1(i).name;
			if (strName.indexOf(strSelectName) >= 0)
			{
				myselect = col1(i);
			}
		}
	}
	
	if (myselect == null)
	{
		return;
	}
	
	if (document.all[mydiv] != null && myselect.options.length>0)
	{
		var oldIndex;
		var newOption,oldOption;
		
		
		oldIndex = myselect.selectedIndex;
		newOption = myselect.options[nPos];
		if (newOption.selected == true)
		{
			newOption.selected = false;
			if (newOption.selected == false)
			{
				document.all[mydiv].style.backgroundColor = 'white';
				document.all[mydiv].style.color = '';
			}
		}
		else
		{
			if (document.all[strValidatorName] != null && bFlag != true)
			{
				document.all[strValidatorName].innerText = "";
				bFlag = true;
			}
			newOption.selected = true;
			document.all[mydiv].style.backgroundColor = '#5f8ac5';
			document.all[mydiv].style.color = 'white';
		}
		

		if (oldIndex >= 0 && nPos != oldIndex)
		{
			
			oldOption = myselect.options[oldIndex];
			var mydiv2 = strNodeName + myselect.options[oldIndex].value;
			
			if (oldOption != null && document.all[mydiv2] != null)
			{

				if (oldOption.selected == false)
				{
					oldOption.selected = false;
					document.all[mydiv2].style.backgroundColor = 'white';
					document.all[mydiv2].style.color = '';
				}
			}
		}
	}
}


/* Function to set node to selected values - Server side */
function SetNodeToSelectedValueServerSide(objIdList,strSep,strNodeName)
{
	
	if (objIdList == null)
	{
		return;
	}
	
	if (objIdList.value != "")
		{
			var idArray = new Array();
			var strVal = objIdList.value;
			
			idArray = strVal.split(strSep);
			
			for (var j=0; j< idArray.length; j++) 
			{
				var mydiv = strNodeName + idArray[j];
				if(document.all[mydiv] != null) 
				{
					document.all[mydiv].style.backgroundColor = '#5f8ac5';
					document.all[mydiv].style.color = 'white';
				}
			}
			
		}		
}

/* Function to start mouse move for a div */
var _strDivId;
var _startX, _startY;

function MoveDivToMouseLocation()
{
	var divObj;
	
	divObj = document.all[_strDivId];

	if (divObj != null)
	{
		divObj.style.left = window.event.x - _startX;
		divObj.style.top = window.event.y - _startY ;
		
	}
}

function StartMouseTracking(strDivId)
{
	
	_strDivId = strDivId;
	
	if (document.all[strDivId] != null)
	{
		_startX = window.event.x - document.all[strDivId].offsetLeft;
		_startY = window.event.y - document.all[strDivId].offsetTop;
		document.all[strDivId].title = "Drag the window to move it to a different location on the screen";
		document.all[strDivId].attachEvent("ondrag",MoveDivToMouseLocation);
		document.body.attachEvent("onmousemove",MoveDivToMouseLocation);
		document.body.attachEvent("onmouseup",_ReleaseMouseTracking);
		document.all[strDivId].attachEvent("onmouseout",_ReleaseMouseTracking);
		document.all[strDivId].attachEvent("onmouseup",_ReleaseMouseTracking);
	}
	
}

function StartMouseTrackingEx(strDivId, strPositionDivId)
{
	
	_strDivId = strPositionDivId;
	
	if (document.all[strDivId] != null && document.all[strPositionDivId] != null)
	{
		_startX = window.event.x - document.all[strPositionDivId].offsetLeft;
		_startY = window.event.y - document.all[strPositionDivId].offsetTop;
		document.all[strDivId].title = "Drag the window to move it to a different location on the screen";
		document.all[strDivId].attachEvent("ondrag",MoveDivToMouseLocation);
		document.body.attachEvent("onmousemove",MoveDivToMouseLocation);
		document.body.attachEvent("onmouseup",_ReleaseMouseTracking);
		document.all[strDivId].attachEvent("onmouseout",_ReleaseMouseTracking);
		document.all[strDivId].attachEvent("onmouseup",_ReleaseMouseTracking);
	}
	
}

function _ReleaseMouseTracking()
{
	document.body.detachEvent("onmousemove",MoveDivToMouseLocation);
	if (document.all[_strDivId] != null)
	{
		document.all[_strDivId].detachEvent("ondrag",MoveDivToMouseLocation);
	}
	_strDivId = "";
}

function ShowToolTipPopup(PopupText,ClickedObject)
{
	var oPopup = window.createPopup();
    var oPopBody = oPopup.document.body;
    oPopBody.style.backgroundColor = "lightyellow";
    oPopBody.style.border = "solid black 1px";
    oPopBody.innerHTML = PopupText;
    oPopup.show(0, 0, 180, 25, ClickedObject);
}


