/*
© 2006 - Centennial Software Limited - All rights reserved.

This work contains copyrighted, confidential, 
proprietary and trade secret information owned by Centennial Software Limited. 
No part of this work may be used, practiced, performed, copied, distributed, 
revised, modified, translated, abridged, condensed, expanded, collected, 
compiled, linked, recast or adapted without the prior written consent of 
Centennial Software Limited. Any use or exploitation of this work without full 
written authorization directly from Centennial Software Limited will constitute 
an infringement of Copyright.
*/

// Basic functions used on many different pages.

// [cj 19.09.2006] Function to allow a default button to be specified and clicked for a textbox control
function clickButton(e, buttonid)
{ 
	var bt = document.getElementById(buttonid); 
	
    if (typeof bt == 'object')
    { 
		if(navigator.appName.indexOf("Netscape")>(-1))
			{
				if (e.keyCode == 13)
				{ 
                        bt.click(); 
                        return false; 
                 } 
            } 
            if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1))
            { 
                  if (event.keyCode == 13)
                  { 
                        bt.click(); 
                        return false; 
                  } 
            } 
      } 
} 

// Show or hide a dashboard report options form.
function toggleReportForm(page, e) 
{
	if (document.getElementById(page))
		toggleElement(page, this);
	else
		toggleElement("report-" + number + "-form", this);
		
	return false;
}

// Show or hide any element by id. Works only for elements with
// display = block. A link with the id of the element followed
// by "-toggle" will have it background image changed.
function toggleElement(id, w) {
	if (document.getElementById(id)) {
		var element = document.getElementById(id);
		var elementDisplay = "none";
		if (element.style.display == "none") {
			elementDisplay = "block";
		}
		element.style.display = elementDisplay;
		try
		{
			if (elementDisplay == "block") 
			{
				w.style.backgroundImage = "url(../images/widget_collapse.gif)";
			}
			else 
			{
				w.style.backgroundImage = "url(../images/widget_expand.gif)";
			}
		}
		catch (e)
		{
		alert(w)
		}
	}
}

// Allows the left hand panel to be hidden. Not currently used.
var panelVisible = true;
function togglePanel() {
	
	if (panelVisible) {
		document.getElementsByTagName("body").item(0).className = "no-panel";
		document.getElementById("options").style.display = "none";
		document.getElementById("content").style.marginLeft = "0";
	} else {
		document.getElementsByTagName("body").item(0).className = "panel";
		document.getElementById("options").style.display = "block";
		document.getElementById("content").style.marginLeft = "207px";
	}
	
	panelVisible = !panelVisible;
	
}

// Called by a "select all" checkbox when it is clicked, this method will
// check or uncheck all other checkboxes in the same fieldset as appropriate.
function selectAll(checkbox) {
	
	// Find the parent fieldset element and select all checkboxes in it.
	
	var fieldset = null;
	var parent = checkbox;
	while (fieldset == null) {
		parent = parent.parentNode;
		if (parent.tagName.toLowerCase() == "fieldset" || parent.tagName.toLowerCase() == "table" ) {
			fieldset = parent;
		}
	}
	if (fieldset != null) {
		var inputs = fieldset.getElementsByTagName("input");
		for (i = 0; i < inputs.length; i++) {
			var input = inputs[i];
			if (input.getAttribute("type") == "checkbox") {
				if (checkbox.checked) {
					input.checked = true;
				} else {
					input.checked = false;
				}
			}
		}
	}
}


function CheckAll(control, name) {
	var i, el;
	
	// alert(control.checked);
	el = control.form.elements;
	for(i=0; i < el.length; i++) {
		if(el[i].type == "checkbox" && el[i].name.indexOf(name) == 0) {
			el[i].checked = control.checked;
		}
	}
}




/********************************************************************************************************************************

validation methods

********************************************************************************************************************************/

function initValidation(frmId)
{
	var f = document.getElementById(frmId)
	if (f)
		f.onsubmit = validateSystemCosts;
}


function fixNumber(n)
{
	var ret = n;
	//n = n.replace(/,/g, "");
	//ret=parseFloat(n);

	return ret;
}

function validateSystemCosts(e)
{
	var a= this.getElementsByTagName("input");
	var ret = true;
	for (var i=0; i<a.length;i++)
	{
	
		switch (a[i].name)
		{
			case "SystemCost":
			case "SystemLabCost":
			case "SystemLabTime":
			case "ProcessorCost":
			case "ProcessorLabCost":
			case "ProcessorLabTime":
			case "MemoryCost":
			case "MemoryLabCost":
			case "MemoryLabTime":
			case "DiskCost":
			case "DiskLabCost":
			case "DiskLabTime":
				try
				{
					//var v = fixNumber(a[i].value);
					var v = a[i].value;
				}
				catch (f)
				{
					warnAbout(a[i]);
					ret = false;
				}
				if (isNaN(v))
				{
					warnAbout(a[i]);
					ret = false;
				}
				else
				{
					a[i].value = v;
				}
				
					
				break;
			default:
				break;
		}
	}
	
	if (!ret && event)
		event.cancelBubble=true;
		
	return ret;
}

function warnAbout(el)
{
	if (el)
	{
		el.style.backgroundColor = "yellow";
		//el.focus();
		//el.onkeypress = new function(){this.style.backgroundColor="";};
	}
	
	alert(xlat("V.IsNumber", "All values must by numeric"));
	
}


