function number_format(num, dp)
{
	if (!dp)
		dp = 0;
	if (!num)
		return '0';
	
	num = (num+'').replace(/[^0-9\.-]/g, '');  // strip non-numeric
	num = Math.round(parseFloat(num) * Math.pow(10, dp)) / Math.pow(10, dp);  // round to dp decimal places
	if (num < 0)
	{
			negative = true;
			num *= -1;
	}
	else
			negative = false;

	num2 = (num + '').split(".");

	// add commas
	foo = '';
	for (var i = 1; i <= Math.ceil(num2[0].length / 3) - 1; i++)
	{
		foo = ',' + num2[0].substr(num2[0].length - (i*3), 3) + foo;
	}
	foo = num2[0].substr(0, num2[0].length - ((Math.ceil(num2[0].length / 3) - 1) * 3)) + foo;

	bar = '';
	if (dp)
	{
		if (num2.length == 1)
		{
			bar = '.';
			for (var i = 0; i < dp; i++)
				bar += '0';
		}
		else
		{
			num2[1] = num2[1].substr(0, dp);
			bar = '.' + num2[1];
			for (var i = num2[1].length; i < dp; i++)
				bar += '0';
		}
	}
	return ((negative) ? '-' : '') + foo + bar;
}

// obj = the object which we can use to change the obj.className
// cmp = the comparison string (more often than not, obj.value, but we cannot always assume this
function input_checkString(obj, cmp)
{
	if (cmp)
	{
		obj.className = "required_complete";
		return true;
	}
	obj.className = "required_incomplete";
	return false;
}

// loop through all inputs and check whether they are required and update their class if needbe
// obj = the form object
function checkElements(obj)
{
	toReturn = true;
	for (i = 0; i < obj.elements.length; i++)
	{
		if (obj.elements[i].className == "required_incomplete")
		{
			foo = input_checkString(obj.elements[i], obj.elements[i].value);
			toReturn = (toReturn == true) ? foo : false;
		}
	}
	return toReturn;
}

function doSave()
{
	allowSave = checkElements(myForm);
	if (allowSave)
	{
		document.myForm.submit();
	}
	else
	{
		alert('Save unsuccessful.\n\nPlease complete all mandatory fields highlighted red.');
	}
}

function calUrl(unixVar, niceVar)
{
	url  = "calendar.php?unixtime=" + eval('document.' + unixVar);
	url += "&unixVar=window.opener.document." + unixVar;
	url += "&niceVar=window.opener.document." + niceVar;
	return url;
}
