
function FormatN(X, N) {
   var TenN = Number("1e"+N)
   var parts = String(Math.round(X * TenN)/TenN).split(".")
   if (parts[0] == '') parts[0] = "0" // Lest needed
   if (!parts[1]) parts[1] = ''
   while (parts[1].length < N) parts[1] += "0"
   return parts.join(".") }

// x = number to format... M = number of required digits before the decimal... N = number of required digits after the decimal
//i.e.  Format(5.6, 2, 3) = 05.600
function Format(X, M, N) { // X>=0.0
   var T, S=new String(Math.round(X*Number("1e"+N)))
   // if (S.search && S.search(/\D/)!=-1) { return ''+X } // was search(/e/)
   if (/\D/.test(S)) { return ''+X }
   with (new String(Prepend(S, M+N, '0')))
    return substring(0, T=(length-N)) + '.' + substring(T) }

function Prepend(Q, L, c) { var S = Q+'' // ??
   // if (!c) var c = ' '
   if (c.length>0) while (S.length<L) { S = c+S }
   return S }
   

function y2k(number) 
{ 
	if (number < 1000)
		return (number < 60) ? number + 2000 : number + 1900; 
	else
		return number; 
}

function isDate (day, month, year) 
{
// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 1 and 12, and the
// year in ccyy format.

    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    
	if (!day) 
		return false;
    
	var test = new Date(year,month,day);
    if ((y2k(test.getYear()) == year) && (month == test.getMonth()) && (day == test.getDate()))
        return true;
    else
        return false;
}

function ValidDates(theElement)
{
	var theDate = theElement.value;
	if (theDate.length == 0) return true;

	var separat = "";
	if (theDate.indexOf("/") != -1) separat = "/";
	if (theDate.indexOf(".") != -1) separat = ".";
	if (theDate.indexOf("-") != -1) separat = "-";

	if (separat == "")
	{
		alert("Invalid Date!  - " + theDate);
		theElement.focus();
		theElement.select();
		return false;
	}
	
	var theSplitDate = theDate.split(separat);
	
	if (theSplitDate.length != 3)
	{
		alert("Invalid Date!  - " + theDate);
		theElement.focus();
		theElement.select();
		return false;
	}	
	
	var month = parseFloat(theSplitDate[0]);
	var day = parseFloat(theSplitDate[1]);
	var year = parseFloat(theSplitDate[2]);
	
	year = y2k(year);
	
	//Make sure no alpha characters are present
	if(!isNaN(month) && !isNaN(day) && !isNaN(year)){}
	else
	{
		alert("Invalid Date!  - " + month + "/" + day + "/" + year);
		theElement.focus();
		theElement.select();
		return false;
	}
	
	
	
	if (isDate(day, month, year))
		return true;
	else
	{
		alert("Invalid Date!  - " + month + "/" + day + "/" + year);
		theElement.focus();
		theElement.select();
		return false;
	}
}

function validateNumeric(frmelement)
{
  var strValue = frmelement.value;	
  if (strValue != "")
  {
	  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
 
	  //check for numeric characters 
	  if(!objRegExp.test(strValue))
	  {
	  	alert("Please enter only numbers.");
		frmelement.focus();
		frmelement.select();
	  	return false;
	  }
  }
  return true;
}

function validateInt(frmelement)
{
  var strValue = frmelement.value;	
  var intValue = parseInt(strValue);
  if (strValue != "")
  {
  	if (validateNumeric(frmelement))
	{
		if (strValue != intValue)
			return false;
	}
	else
		return false;	
  }
  return true;
}

function ValidEmail(objField){
    // Create the regular expression
    var emailRegExp = /^[a-zA-Z0-9][\w\-\_\.]*@[a-zA-Z0-9]{1}[a-zA-Z0-9\.\-]*\.[a-z]{2,3}/i

	if (emailRegExp.test(objField.value)){
		return true;
	}
	else {
		alert("Invalid email address. Must be in the 'user@domain.com' format.");
		objField.focus();
		return false;
	}
}

function ItemIsEmpty(objElement, strDesc){
	if(objElement.type == "select-one"){
		if(objElement.selectedIndex == 0){
			alert("You must fill in: " + strDesc);
			objElement.focus();
			return true;
		}
	}
	else{
		if(objElement.value == ""){
			alert("You must fill in: " + strDesc);
			objElement.focus();
			return true;
		}
		//if(objElement.value == "0"){
		//	alert("You must fill in: " + strDesc);
		//	objElement.focus();
		//	return true;
		//}
	}
	return false;
}

function ChangeSorting(strChangeTo, SortBy, SortDir){
	if (strChangeTo == SortBy){
		if (SortDir == "DESC") 
			document.frmMain.OrderByDir.value = "ASC";
		else
			document.frmMain.OrderByDir.value = "DESC";
	}
	else
		document.frmMain.OrderBy.value = strChangeTo;

	frmMain.submit();
}