// Generic positive number decimal formatting function.
function format(expr, decplaces) {
  // Raise expr. vy power of 10 times decplaces; round to an integer, convert
  // to a string.
  var str = "" + Math.round(eval(expr) * Math.pow(10, decplaces));
  
  // Pad small value strings with zeros to the left of rounded number.
  while(str.length <= decplaces) {
    str = "0" + str; 
  }
  
  // Establish location of decimal point.
  var decpoint = str.length - decplaces;
  
  // Assemble final result and return it.
  return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);
}

// Turn an expression into a dollar value.
function dollarize(expr) {
  return "$" + format(expr, 2);
}

// Show or hide a block named divID.
function showHide(divID) {
  if(divID.style.display == "none") {
    // Show divID.
    divID.style.display = "block";
    return 0;
  }
  
  if(divID.style.display == "block") {
    // Hide divID.
    divID.style.display = "none";
    return 0;
  }
}

// Set the className of elem.
function setClass(elem, className) {
	document.getElementById(elem).className = className;
}

/******************************************************************************
 isEmpty takes a form element elem and checks whether the value is empty. If
 the value is of zero-length or null, isEmpty returns true. Otherwise isEmpty
 returns false.
 ******************************************************************************/
function isEmpty(elem) {
	var str = elem.value;
	if(str == null || str.length == 0)
		return true;
	else
		return false;
}

/******************************************************************************
 isEmailAddr takes a form element elem and checks whether the value can be a
 valid e-mail address. If the value of elem can be a valid e-mail address,
 isEmailAddr returns true. Otherwise isEmailAddr returns false.
 ******************************************************************************/
function isEmailAddr(elem) {
	var str = elem.value;
	var re = /^[a-z0-9][^\(\)\<\>\@\,\;\:\\\"\[\]]*\@[a-z0-9][a-z0-9\-\.]*\.[a-z]{2,4}$/i;
	if(!str.match(re))
		return false;
	else
		return true;
}