var GREEN = 0;
var YELLOW = 1;
var RED = 2;


// create a full url from the given path - uses the appContext param.
function toUrl(thePath) {
	// does the path begin with a "/" ?
	if (thePath.indexOf('/')!=0) {
		thePath = "/"+thePath;
	}
	theAppContext = appContext;
	
	if (theAppContext.indexOf('/')!=0 && theAppContext!='') {
		theAppContext="/"+theAppContext;
	}

     aStr = location.protocol+"//"+location.host+theAppContext+thePath;
     return aStr;
}


/**************************************************************************
							cookies.
***************************************************************************/
function setCookie(theName, theValue, theExpire,thePath)
{
  document.cookie = theName + '=' + escape(theValue)
  + ((theExpire == null) ? '' : ('; expires=' + theExpire.toGMTString()))
  + ((thePath==null) ? '' : ('; path='+thePath));
}

function getCookie(theName)
{
  var aSearch = theName + '='

  if (document.cookie.length > 0)
  {
    aOffset = document.cookie.indexOf(aSearch);
	if (aOffset != -1)
	{
	  // if cookie exists
	  aOffset += aSearch.length;
	  // set index of beginning of value
	  aEnd = document.cookie.indexOf(';', aOffset);
	  // set index of end of cookie value
	
	  if (aEnd == -1)
	    aEnd = document.cookie.length;

	  return unescape(document.cookie.substring(aOffset, aEnd));
	}
  }
}

function hideQuickSelWhenInstructionsAreShown()
{
	if (getCookie("instructions")=="true")
	{
		document.write('<div class="block_hide">');
	}
	else
		document.write('<div>');
}

/**************************************************************************
							Misc.
***************************************************************************/
// check that theParam contains either an URL or a HOST, used by transaction filter manager.
function checkParam(theParam){
	is_ok = (theParam.match(/^\s*((URL)|(HOST))\s*=\s*\w\w*/i)) ? true : false;

	if (!is_ok)
		alert("Syntax: URL | HOST = parameter");
	return is_ok;
}

function parseUrl(theArg)
{
	var aPos, aArgname, aValue, aQueryString, aPairs;
	// get the string following the question mark
	aQueryString = location.search.substring(1);
	// split parameters into pairs, assuming pairs are separated by ampersands
	aPairs = aQueryString.split("&");
    for (i = 0; i < aPairs.length; i++)
    {
		aPos = aPairs[i].indexOf('=');
		if (aPos == -1)
		{
			continue;
		}
		aArgname = aPairs[i].substring(0,aPos);
		if (aArgname == theArg)
 		return aPairs[i].substring(aPos+1);
  }
}


function submitFrm(theFrm)
{
	document.forms[theFrm].submit();
}

function showWarningBeforeSubmit(theFrm, theWarning)
{
   if (alert(theWarning))
   {
      submitFrm(theFrm);       
   }
}


function showWarning(theWarning)
{
   return confirm(theWarning);
}

function isNumber(aVal) 
{
    aDotFound = false;

    for (var i = 0; i < aVal.length; i++)
    {
        var aOneChar = aVal.substring(i, i + 1);
        if (aOneChar < "0" || aOneChar > "9") 
        {
            return false;
        }
    }
    return true;
}

function validateIPForm(aForm)
{
    for(i=1;i<5;i++)
    {
        aVal = aForm.elements['ip_addr_'+i].value;
        if (aVal == "" || aVal == null)
          continue;

        if (aVal < 0 || aVal > 255 || isNumber(aVal)== false )
        {
            alert(aVal+' is not legal it must be empty or a number between 0 and 255 (both included)');
            aForm.elements['ip_addr_'+i].focus();
            aForm.elements['ip_addr_'+i].select();
            return false;
        }   
    }
    return true;
}

function ToggleTwoLayers(First,Last)
{
    ToggleLayer(First);
    ToggleLayer(Last);
}

function ToggleLayer(theNo)
{
	if (theNo==null)
		return;

	if (document.getElementById(theNo).className == 'block_hide')
		document.getElementById(theNo).className = 'block_show';
	else
		document.getElementById(theNo).className = 'block_hide';
}


function ToggleLayerWithImage(theNo,theImg,ImgOn,ImgOff)
{
	if (theNo==null)
		return;

	if (document.getElementById(theNo).className == 'block_hide')
	{
		document.getElementById(theNo).className = 'block_show';
		document.images[theImg].src=ImgOn;
	}
	else
	{
		document.getElementById(theNo).className = 'block_hide';
		document.images[theImg].src=ImgOff;
	}
}

function displayMenuOnLoad(header,content,image,menu_area) {
	if (getCookie('show_menu_block')=='closed')
		toggleMenu(header,content,image,menu_area);
}	


function toggleMenu(header,content,image,menu_area) {
	var hdr_obj=document.getElementById(header);
	var content_obj=document.getElementById(content);
	var menu_obj=document.getElementById(menu_area);
	
	if (menu_obj.className=='block_show') {
		document.images[image].src=toUrl('/i/plus.gif');
		hdr_obj.className='menuContentTopBarOff';
		content_obj.className='menuContentOff';
		menu_obj.className='block_hide';		
		setCookie('show_menu_block','closed',null);
		
	}
	else {
		document.images[image].src=toUrl('/i/minus.gif');
		hdr_obj.className='menuContentTopBar';
		content_obj.className='menuContent';
		menu_obj.className='block_show';		
		setCookie('show_menu_block','open',null);	
	}
}

function showSelectedMenu(theSelected) {
	var menu_arrays = new Array()
	menu_arrays[0] = "menu_graphs";
	menu_arrays[1] = "menu_overview";
	menu_arrays[2] = "menu_report";
	menu_arrays[3] = "menu_agent";
	menu_arrays[4] = "menu_citrix";
	menu_arrays[5] = "menu_admin";
	menu_arrays[6] = "menu_help";	
	for(i=0;i<menu_arrays.length;i++) {
		if (menu_arrays[i]==theSelected )
			document.getElementById(menu_arrays[i]).className = 'block_show';
		else	
			document.getElementById(menu_arrays[i]).className = 'block_hide';
	}
}

function setBlockCookie(theCookie,theNo)
{
	if (theNo==null)
		return;

	if (document.getElementById(theNo).className == 'block_hide')
		setCookie(theCookie,'off',null);
	else
		setCookie(theCookie,'on',null);
}

function toggleMenuOnCookie(theCookie,theNo)
{
	if (theNo==null)
		return;

	if (document.getElementById(theNo).className == 'block_hide')
	{
		setCookie(theCookie,'on',null);
		document.getElementById(theNo).className = 'block_show';
		alert(theNo + '0');
	} 
	else
	{
		setCookie(theCookie,'off',null);
		document.getElementById(theNo).className = 'block_hide';
		alert(theNo + '1');		
	}
}

function SetLayerClass(theNo,theClass)
{
	if (theNo==null)
		return;
	
	aObj = 	document.getElementById(theNo);
	
	if (aObj != null)
		aObj.className = theClass;

}


function SetAutoRefresh(theNewVal)
{
   var aToday = new Date()
   var aExpires = new Date()
   aExpires.setTime(aToday.getTime() +1000*60*60*24*365); // expires next year

   setCookie("autorefresh",theNewVal,aExpires); 
   window.location.href = location;
}


// Next four methods are concerned with combining the selection state of
// multiple checkboxes into a commaseparated string.

myChkBoxSelections = new Array();

function page_chkbox(id, status, type)
{
  this.id = id;
  this.status = status;
  this.type = type;
}

function InsertCheckBox(theId, theType, theName)
{
  document.write('<input type="checkbox" id="'+theName+'"  value="chk_'+theId+'" onchange="ToggleCheckboxSel('+theId+','+theType+')">');
}

/**
	Builds a global array of page_chkboxes. If an element with the same id already exists in the array,
	the status is toggled.
*/
function ToggleCheckboxSel(theId,theType)
{
  var aFound = -1;
  var i;
  // Check if a page_chkbox with the same id already exists.
  for(i=0;i<myChkBoxSelections.length;i++)
  {
    if (myChkBoxSelections[i].id == theId)
    {
      aFound = i;
      break;
    }
  }
  if (aFound == -1)
  {
	// It did not exist. Insert a new.
    myChkBoxSelections[myChkBoxSelections.length] = new page_chkbox(theId,1,theType);
  }
  else
  {
	// Did exist. Toggle status.
    myChkBoxSelections[aFound].status = (myChkBoxSelections[aFound].status==0) ? 1 : 0;
  }
  //  alert("id "+theId);
}

function WriteSelectionToField(formName,theType,theAction,theField,theTypeField,theConfirm)
{
  var i;
  var aFirst = true;
  aTxt = '';
  
  for(i=0;i<myChkBoxSelections.length;i++)
  {
      if(myChkBoxSelections[i].status == 1 && myChkBoxSelections[i].type==theType)
      {
        if (aFirst==false)
          aTxt+=',';
          
        aTxt +=myChkBoxSelections[i].id;
        aFirst = false;
      }
  }
  document.forms[formName].elements[theField].value = aTxt;
  document.forms[formName].elements[theTypeField].value = theAction;
  // alert(aTxt);

  if (theConfirm == null)
    return true;
 else
   return confirm(theConfirm);
}

function us_to_ms(theUs)
{
  document.write(theUs/1000);
}


function validate_form(n)
{
	 var f=null;
     if (isNumber(n)) { 
			  f=document.forms[n];
	 }
	 else {
	  f = document.getElementsByName(n)[0];
		 }
      var l = f.elements.length;

        for(i = 0; i < l; i++) {

          with (f.elements[i]) {

            // -- we're only interested in text fields
            if (! type.match(/text/i)) continue;

            // -- check if field was required and empty
            if (className.match(/required/i) && value == '') {
              alert('Required field \'' + name + '\' was empty');
              return false;
            }

            // -- bump field data against appropriate validating regexp
            var fmt = className.match(/float|integer|alpha|char|folder/);

            var is_ok = true;


            if (fmt == 'float')
                is_ok = (value.match(/^(-)?\d*\.?\d*$/)) ? true : false;

            if (fmt == 'integer')
                is_ok = (value.match(/^(-)?\d+$/)) ? true : false;
                
            if (fmt == 'integer_required')
                is_ok = (value.match(/^(-)?\d+$/)) ? true : false;    

            if (fmt == 'alpha')
                is_ok = (value.match(/\W/g)) ? true : false;

            if (fmt == 'char')
             is_ok = (value.match(/\w+$/)) ? true : false;
             
            if (fmt == 'folder')
             is_ok = (value.match('[\/:*?"<>|]')) ? false : true;

            if (fmt == 'folder_required') {
             is_ok = (value.match('[\/:*?"<>|]') || values.replace( /^\s+/g, "").replace(/\s+$/g, "")=="") ? false : true;
            }
            
            if (fmt == 'char_required')
             is_ok = (value.match(/\w+$/)) ? true : false; 
             
             if (fmt=='integer' && value=='')
                is_ok=true;
             
             if (fmt == 'integer_required' && value == '')
             	is_ok=false;

            if (fmt == 'char_required' && value == '')
             	is_ok=false; 	

	        if (is_ok==false)
	        {
              // -- didn't match any of the regexp's.  Bail out!
              if (value == '' && (fmt == 'char_required' || fmt == 'integer_required'))
              {
              	alert('Field ' + name + ' must be filled in');
              }
              else
                alert('Field ' + name + ' not in ' + fmt + ' format');
              
              
              f.elements[i].focus();
              return false;
   	        }
          }
      }

      // -- data OK.  Tell browser to submit form data
      return true;
}

function selectIntervalNoCustomAxis(theSelIndex,theFrm)
{
	document[theFrm].interval.selectedIndex = theSelIndex;
	document.forms[theFrm].submit();
}

function selectInterval(theSelIndex,theFrm)
{
	document[theFrm].interval.selectedIndex = theSelIndex;
	document[theFrm].custom_axis.checked=false;
	document.forms[theFrm].submit();
}

function setYAxis(theFromFrm,theToFrm)
{
	document[theToFrm].yaxis_min.value=document[theFromFrm].yaxis_min.value;
	document[theToFrm].yaxis_max.value=document[theFromFrm].yaxis_max.value;
	if (validate_form(theToFrm)) 
		document.forms[theToFrm].submit();
}

function setXAxis(theFromFrm,theToFrm)
{
	document[theToFrm].xaxis_min.value=document[theFromFrm].xaxis_min.value;
	document[theToFrm].xaxis_max.value=document[theFromFrm].xaxis_max.value;
	document.forms[theToFrm].submit();
}

function center(theWnd,theWidth,theHeight)
{
  if(navigator.appName == "Microsoft Internet Explorer") 
  {
    screenY = document.body.offsetHeight;
    screenX = window.screen.availWidth;
    PFW.moveTo((screenX - theWidth) / 2,(screenY - theHeight) / 2)
  }
  else 
  {
    screenY = window.outerHeight
    screenX = window.outerWidth
    leftvar = (screenX - theWidth) / 2;
    rightvar = (screenY - theHeight) / 2; 
    theWnd.moveTo(leftvar - pageXOffset,rightvar - pageYOffset)
  }
}


function FromToList (theFromList, theToList,theBtn) 
{
	for (var i = theFromList.length-1; i >=0;i--) 
	{
		if ((theFromList.options[i] != null) && (theFromList.options[i].selected)) 
		{
			theToList.options[theToList.length] = new Option (theFromList.options[i].text,theFromList.options[i].value); 
			theFromList.options[i]=null;

			if (theBtn!=null)
				theBtn.disabled=false;
			
		}
	}
}

function FromToListAll (theFromList, theToList,theBtn) 
{
	for (var i = theFromList.length-1; i >=0;i--) 
	{
		if ((theFromList.options[i] != null)) 
		{
			theToList.options[theToList.length] = new Option (theFromList.options[i].text,theFromList.options[i].value); 
			theFromList.options[i]=null;
			if (theBtn!=null)
				theBtn.disabled=false;
		}
	}
}

function SelectAll(theList) 
{
	for (var i = theList.length-1; i >=0;i--) 
	{
		if ((theList.options[i] != null)) 
		{
			theList.options[i].selected=true;
		}
	}
}

/*
Get browser type
0=IE
1=safari
2=Opera
3=Mozilla
4=Konqueror
5=?
*/
function getBrowserType()
{
	
	var browser;
	if (checkIt('konqueror'))
	{
		browser = 4;
	}
	else if (checkIt('safari')) browser = 1
	else if (checkIt('opera')) browser = 2
	else if (checkIt('msie')) browser = 0
	else if (!checkIt('compatible'))
	{
		browser = 3
	}
	else browser = 5;	
	return browser;
}

function checkIt(theString)
{
	var detect = navigator.userAgent.toLowerCase();
	place = detect.indexOf(theString) + 1;
	return place;
}

function checkAll(theField,theFlag) 
{
	if (theField.length!=null)
	{ 
		for (i = 0; i < theField.length; i++) 
		{
			theField[i].checked = theFlag;
		}
	}
}

// Finds all input-boxes  inside the elementName
//  Then checks / unchecks it like the Field
function checkUncheckAll(theField,elementName)
{
	var base = document.getElementById(elementName);
	var boxes = base.getElementsByTagName('input');
	for(i=0; i<boxes.length;i++)
    {
        if(boxes[i].type == 'checkbox' && boxes[i].name != 'checkAll')
        {
	        boxes[i].checked = theField.checked;
            //Force the onchange event to fire
            boxes[i].onchange();
        }
     }
}

function toggle_check(theField) 
{
	if (theField.length!=null)
	{ 
		for (i = 0; i < theField.length; i++) 
		{
			theField[i].checked =!theField[i].checked;
		}
	}
}


var TipToShow=null;
function ShowTip(e)
{
	if (!e)
        e = event;
	
	var tooltipOBJ = document.getElementById(TipToShow);

	if (tooltipOBJ != null) {

		var scrollX = 0; // Horisontal viewport offset
		var scrollY = 0; // Vertical viewport offset
		var sizeX   = 0; // Viewport width
		var sizeY   = 0; // Viewport height
		var relX    = e.clientX; // Mouse horisontal position in the viewport
		var relY    = e.clientY; // Mouse vertical position in the viewport
		var X       = 0; // Tip horisontal position in the wiewport
		var Y       = 0; // Tip vertical position in the wiewport
		var W       = 0; // Final tip width
		var H       = 0; // Final tip height
		var border  = 5; // Virtural tip border

		if( document.documentElement.scrollLeft &&
			 (!document.body.scrollLeft || (document.body.scrollLeft == 0 && document.documentElement.scrollLeft > 0)))
	 	{
			scrollX = document.documentElement.scrollLeft;
		}
		else if (document.body.scrollLeft )
		{
			scrollX = document.body.scrollLeft;
		}

		if( document.documentElement.scrollTop &&
			 (!document.body.scrollTop || (document.body.scrollTop == 0 && document.documentElement.scrollTop > 0)))
	 	{
			scrollY = document.documentElement.scrollTop;
		}
		else if (document.body.scrollTop)
		{
			scrollY = document.body.scrollTop;
		}
		
		if( document.documentElement.scrollLeft &&
			 (!document.body.clientWidth || (document.body.clientWidth == 0 && document.documentElement.clientWidth > 0)))
	 	{
			sizeX   = document.documentElement.clientWidth;
		}
		else if (document.body.clientWidth )
		{
			sizeX   = document.body.clientWidth;
		}

		if( document.documentElement.clientHeight &&
			 (!document.body.clientHeight || (document.body.clientHeight == 0 && document.documentElement.clientHeight > 0)))
	 	{
			sizeY   = document.documentElement.clientHeight;
		}
		else if (document.body.clientHeight)
		{
			sizeY   = document.body.clientHeight;
		}

/*
		alert("Scroll = ["+ scrollX + ", " + scrollY + "]\n\r"
			+ "RelPos = ["+ relX + ", " + relY + "]\n\r"
			+ "sizeX/Y = [" + sizeX + ", " + sizeY + "]\n\r"
			+ "tooltipOBJ.offsetWidth/Height = [" + tooltipOBJ.offsetWidth + ", " + tooltipOBJ.offsetHeight + "]\n\r"
			);
*/
		// Check the horisontal location:
		// If it goes over the window right side and there is more space at the left side, we will show it there.
		var neededHorisontalSpace = tooltipOBJ.offsetWidth + 2*border;
		if (neededHorisontalSpace > (sizeX - relX) && relX > (sizeX/2)) {
			// Place it to the left
			if (neededHorisontalSpace > relX) {
				X = border;
				W = Math.min(tooltipOBJ.offsetWidth, sizeX - 2*border);
			} else {
				X = relX - neededHorisontalSpace + border;
				W = tooltipOBJ.offsetWidth;
			}
		} else {
			X = relX + border;
			W = Math.min(tooltipOBJ.offsetWidth, sizeX - relX - 2*border);
		}
		tooltipOBJ.style.left  = scrollX + X + "px";
		tooltipOBJ.style.width = W + "px";

		// In Firefox, tooltipOBJ.style.width don't get us the wanted value in tooltipOBJ.offsetWidth - so try again
		var diff = tooltipOBJ.offsetWidth - W;
		if ( 0 != diff && W > diff) {
			W -= diff;
			tooltipOBJ.style.width = W + "px";
		}

		// Check the vertial location:
		// If it goes over the window top and there is more space at the bottom, we will show it there.
		var neededVerticalSpace = tooltipOBJ.offsetHeight + 2*border;
		if (X == border && neededVerticalSpace > relY && relY < (sizeY/2)) {
			// Place it at the bottom
			Y = relY + border;
		} else {
			if (neededVerticalSpace > relY) {
				Y = border;
			} else {
				Y = relY - neededVerticalSpace + border;
			}
		}
		tooltipOBJ.style.top  = scrollY + Y + "px";

		hideSelectBoxes(X, Y, tooltipOBJ.offsetWidth, tooltipOBJ.offsetHeight);
		tooltipOBJ.style.visibility = "visible";
	}
}

var hiddenFields = Array();
function HideTip(theTip)
{
	theSaveTip = theTip;
	var tooltipOBJ = document.getElementById(theTip);
	if (tooltipOBJ != null) {
	    tooltipOBJ.style.visibility = "hidden";
	}
	
	for (i=0;i<hiddenFields.length;i++) {
			hiddenFields[i].style.visibility = "visible";
	}
}



function toggleTableRows(theTableId,theOpenImg,theCloseImg,theImgid)
{
	isHiding = true;
	aRows=document.getElementById(theTableId).rows;
	
	for (i=1;i<aRows.length;i++) 
	{
		if (aRows[i].style.display=="none") {
			aRows[i].style.display=""
			isHiding=false;
		}
		else	
			aRows[i].style.display="none"	
	}
	
	if (isHiding)
		document.images[theImgid].src=theCloseImg;	
	else	
		document.images[theImgid].src=theOpenImg;	
}


function hideSelectBoxes(x,y,w,h) {
    hiddenFields.length=0;
    var selx,sely,selw,selh,i
	var sel  = document.getElementsByTagName("SELECT")
 
    for(i=0;i<sel.length;i++) {
		selx=0; 
		sely=0; 
		var selp;
		hiddenFields.push( sel[i] );
		if(sel[i].offsetParent) {
			selp=sel[i]; 
			while(selp.offsetParent) {
				selp=selp.offsetParent; 
				selx+=selp.offsetLeft; 
				sely+=selp.offsetTop;
			}
		}
		selx+=sel[i].offsetLeft; 
		sely+=sel[i].offsetTop
		selw=sel[i].offsetWidth; 
		selh=sel[i].offsetHeight			
			
		if(selx+selw>x && selx<x+w && sely+selh>y && sely<y+h) {
			sel[i].style.visibility="hidden"; 
		}
	 }
}


function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function IntToHex(n) {
var result = n.toString(16);
if (result.length==1) result = "0"+result;
return result;
}




function removeItemsFromArray(theArray,theIndex,theCount){
	theArray.splice(theIndex,theCount);
}

function setInnerHtml(id,val) {
	var aObj = document.getElementById(id);
	if (aObj!=null)
		aObj.innerHTML = val; 
}


function encodeHTMLSpecialChars(inString) {
	inString.replace(/</,"&lt;");
	inString.replace(/>/,"&gt;");
	inString.replace(/&/,"&amp;");
	return inString;
}

function decodeHTMLSpecialChars(inString) {
	inString.replace(/&lt;/,"<");
	inString.replace(/&gt;/,">");
	inString.replace(/&amp;/,"&");
	return inString;
}

function containsSpecialChars(inString) {

	regStr = /(<|>|&|\\)/;
	return regStr.test(inString);
}

// create a progress bar in the footer status area 
function showUpdateStatus(cnt,max,ServerTime) {
	var str = "<span class='status'><table cellpadding=0 cellspacing=0><tr><td>Next update:&nbsp;</td> ";
	for(i=0;i<max;i++) {
		if (i<=cnt)
			str+="<td ><img src='"+toUrl("/i/loading_blue.gif")+"'/></td>";
		else	
			str+="<td ><img src='"+toUrl("/i/loading_gray.gif")+"'/></td>";  
    }
	setInnerHtml("status",str);
}



/******************************** DOM update  *********************************************/

function addElement(theId,theContent,theClass,parentElem,elemType) {
  var aParent = document.getElementsByTagName("body").item(0);
  if (parentElem!=null) 
  	aParent = 	document.getElementById(parentElem);

  if (elemType==null) elemType='span';
  var aNewDiv = document.createElement(elemType);
  aNewDiv.setAttribute('id',theId);
  aNewDiv.innerHTML = theContent;
  if (theClass!=null) {	
	  aNewDiv.className=theClass;
  }
  aParent.appendChild(aNewDiv);
  return aNewDiv;
}


function addAbsolutPositionedElement(theId,theContent,theClass,theXpos,theYpos) {
  var aParent = document.getElementsByTagName("body").item(0);
  var aNewDiv = document.createElement('div');
  aNewDiv.setAttribute('id',theId);
  aNewDiv.style.left = theXpos+"px";
  aNewDiv.style.top = theYpos+"px";
  aNewDiv.style.zIndex=10; // new elements are always on top

  aNewDiv.style.position = 'absolute';

  aNewDiv.innerHTML = theContent;
  if (theClass!=null) {	
	  aNewDiv.className=theClass;
  }
  aParent.appendChild(aNewDiv);
  return aNewDiv;
}

function removeElement(theId) {
  var aElem = document.getElementById(theId);
  
  if (aElem!=null)
	  aElem.parentNode.removeChild(aElem);
}

// Update the listbox identified by theId
// The items are a nameValue array of the new items, theSel is the selected item (if any).
function updateListBox(theItems,theId) {
	var aListBox = document.getElementById(theId);
	var aSelected = 0;
	if (aListBox==null) 
		return false;
		
	for (var i=aListBox.length-1; i>=0; i--)
    	aListBox.options[i] = null;
    // check the array is not null	
	if (theItems!=null) {
		for (var i = 0; i < theItems.length; i++){
		    aListBox.options[i]=  new Option(theItems[i].name,theItems[i].value);
		    if (theItems[i].selected == "true") 
		    	aSelected = i;
		}
	}
	aListBox.selectedIndex = aSelected;		
	return true;	
}

function getCData(theXml,theNodeName,theIndex) {
	var aTag = theXml.getElementsByTagName(theNodeName);
	if (aTag==null) return "undefined";
	if (getBrowserType()==0) // msie
		return aTag[theIndex].childNodes[0].nodeValue;
	else	
		return aTag[theIndex].childNodes[1].nodeValue;
}


function getListBoxElements(theId) {
	var items =Array();
	
	var aListBox = document.getElementById(theId);
	var aSelected = 0;
	if (aListBox!=null) {
		for (var i=0;i<aListBox.length; i++)
    		items[i] = new nameValue(aListBox.options[i].name,aListBox.options[i].value);
    }		
    	
    return items;	
}

function clearListBox(theId) {
	var aListBox = document.getElementById(theId);
	if (aListBox==null) 
		return false;
		
	for (var i=aListBox.length-1; i>=0; i--)
    	aListBox.options[i] = null;
	return true;	
}

function enableCheckBox(theId,theStatus) {
	
	var aChkBox = document.getElementById(theId);
	if (aChkBox==null) 
		return false;
 	
	aChkBox.checked	 = theStatus;
}

// change a row 
function highlightRow(row_id,highlight,selectedClass,deselectedClass)	 {
	var aObj = document.getElementById(row_id);

	if (aObj!=null) {
		if (highlight==true) aObj.className=selectedClass;
		else aObj.className=deselectedClass;
	}
}


// Get the position and size of the element identified by theId.
// The values are returned as an array with values in order x,y,width,height
function findPosition( theId,theUseSingleMethodsForIE ) {
	var aObj = document.getElementById(theId);
	if (aObj==null) 
		return [0,0,0,0];

		// is it mozilla/firefox ?
	if (document.getBoxObjectFor) { 
		var aBox = document.getBoxObjectFor(aObj); 
		return [aBox.x,aBox.y,aBox.width,aBox.height];
	}
	
//	alert(aBody.scrollLeft+","+aBody.scrollTop);
	
	if (theUseSingleMethodsForIE==true) // fix a bug in IE7 - arrrrgggggggg
		return [findPosX(aObj),findPosY(aObj),0,0];
	
	var aBody = document.getElementsByTagName("body").item(0);
	if (aObj.getBoundingClientRect) { 
		var aRect = aObj.getBoundingClientRect(); 
		return [aRect.left + aBody.scrollLeft, aRect.top+aBody.scrollTop , aRect.right - aRect.left,aRect.bottom - aRect.top];
	}
	return [0,0,0,0];
}

/********************************** Name value array, used for select box option lists and similar. **************************/
function updateValue(theResponse,theId) {
	var aObj = document.getElementById(theId);
	if (aObj==null) 
		return false;
	else 
		aObj.innerHTML = theResponse;
	return true;	
}

function nameValue(name,value,selected) {
	this.name = name;
	this.value = value;
	this.selected = selected;
}

