/*
 * Common JavaScript functions used thoughout the DPM 
 * Configuration Web Application.
 */
 
/*
 * Call to show or hide a layer in a browser independant way.
 */
function setLayerVisible(layerName, visible) {
	
	 // determine appropriate value
		isNS4 = (document.layers) ? true : false;
		isIE4 = (document.all && !document.getElementById) ? true : false;
		isIE5 = (document.all && document.getElementById) ? true : false;
		isNS6 = (!document.all && document.getElementById) ? true : false;
		if (isNS4) {
			show = "show";
			hide = "hide";
		}
		else if (isIE4 || isIE5 || isNS6) {
			show = "visible";
			hide = "hidden";
		}
		
		// show/hide
		getLayer(layerName).style.visibility = visible ? show : hide;
}


/*
 * Returns whether a layer is visible in a browser neutral way.
 */
function isLayerVisible(layerName) {
	
	 // determine appropriate value
		isNS4 = (document.layers) ? true : false;
		isIE4 = (document.all && !document.getElementById) ? true : false;
		isIE5 = (document.all && document.getElementById) ? true : false;
		isNS6 = (!document.all && document.getElementById) ? true : false;
		if (isNS4) {
			show = "show";
		}
		else if (isIE4 || isIE5 || isNS6) {
			show = "visible";
		}
		
		// show/hide
		return getLayer(layerName).style.visibility == show;
}


/**
 * Return the named layer in a browser independant way.
 */
function getLayer(layerName) {
			if (document.all) {
			  return document.all[layerName];
			}
		 else if(document.getElementById) {
		    return document.getElementById(layerName);
		 }
		 else {
		     return document.layers[layerName];
		 }
}


/*
 * Return the named form in a browser independant way.
 */
function getForm(formName) {
		return document.forms[formName];
}


/*
 * Called to do a confirm then call the logout servlet, which ultimately sends them
 * home.
 */
function cancelConfiguration(message, context)  {
  if(confirm(message)) {
	window.location.href="/" + context + "/logout";
  }
}

/*
 * Called to do a confirm then call the logout servlet, which ultimately sends them
 * home.
 */
function toHome(context)  {
	window.location.href="/" + context + "/logout";
}

/*
 * Called to do a confirm then call the logout servlet, which ultimately sends them
 * cad home page.
 */
function toCADHome(context)  {
	window.location.href="/" + context + "/logout?cad_home=true";
}


/*
 * Sets a value in a input parameter called direction in a form called "MainForm" to
 * 'Next'.
 */
function setValueToNext() {
	document.MainForm.direction.value="Next";
}
	
	
/*
 * Sets a value in a input parameter called direction in a form called "MainForm" to
 * 'Prev'.
 */
function setValueToPrev() {
	document.MainForm.direction.value="Prev";
}


/*
 * Returns true if this value is a number. This is a cheap test.
 */
function isNumber(value) 
{
	// validate number
	if(value != null) {
		
		if(value == "" || isNaN(parseFloat(value)) || parseFloat(value) < 0.0 || (parseFloat(value) == 0.0 && value!="0")) {
		  return false;
	 	}
	 	else {
	   		return true;
		}
 	}
  	else {
	  	return false;
  	}
}

/*
 * Opens the url in a separate window.
 */
function linkToWindow(url, title, width, height) {
	 dim = "width=" + width + ",height=" + height + ",scrollbars=yes";
		newwindow=window.open(url,'name',dim);
	 if (window.focus) {
	 	newwindow.focus();
	 }
	 return false;
}

