
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();
}

function mOver(ele){
	ele.style.background='#406252';
	ele.style.color='white';
}

function mOut(ele){
	ele.style.background='#D4D0C8';
	ele.style.color='black';
}

function winBRopen(theURL, Name, popW, popH, scroll, resize) { // V 1.0
	var winleft = (screen.width - popW) / 2;
	var winUp = (screen.height - popH) / 2;
	winProp = 'width='+popW+',height='+popH+',left='+winleft+',top='+winUp+',scrollbars='+scroll+',resizable='+resize+'';
	Win = window.open(theURL, Name, winProp);
	Win.window.focus();
}

function goto_URL(object) {
	    window.location.href = object.options[object.selectedIndex].value;
}





// global flag
var isIE = false;
// global request and XML document objects
var req;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        //req.onreadystatechange = processReqChange;
        req.open("GET", url, false);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            //req.onreadystatechange = processReqChange;
            req.open("GET", url, false);
            req.send();
        }
    }
    
    //sleep until the results are in
	while (req.readyState != 4) {
	}
	
	if (req.status == 200){
        return true;
    } 
    else {
        return false;
    }
	
}

function loadXMLDoc2(url, blnAsync) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        isIE = true;
        try {
			req = new ActiveXObject("Msxml2.XMLHTTP")
		}catch(e){
			alert("tried msxlm2 FAIL");
			try{
				req = new ActiveXObject("Microsoft.XMLHTTP")
			}catch(e){}
		}
   }
    if(typeof(blnAsync) == 'undefined'){
		//sleep until the results are in
	    req.open("GET", url, false);
        req.send();
		while (req.readyState != 4) {
		}
		
		if (req.status == 200){
			return true;
		} 
		else {
			return false;
		}
	}else{
		req.open('GET', url, true)
		req.send(null)
		return true;
	}
}

function GetXMLData(req, strElement){
    if(req.status == 200 && strElement != ""){
        var items = req.responseXML.getElementsByTagName(strElement);
        try{
            if(items.length > 0){
	            var result = items[0].firstChild.nodeValue;
            }
            return result;
        }
        catch(err){
            return "";
        }
    }
    return false;
}

function GetXMLDataArray(req, strElement){
    if(req.status == 200 && strElement != ""){
        var items = req.responseXML.getElementsByTagName(strElement);
        try{
            if(items.length > 0){
	      //      var result = items[0].firstChild.nodeValue;
			var result = items;
            }
            return result;
        }
        catch(err){
            return err+"";
        }
    }
    return false;
}


function IsNumeric(sTextObj)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char, sText;
   sText = sTextObj.value;
      
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         alert("Please enter numeric values only");
         sTextObj.value = "";
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function IsNumericWithDecimal(sTextObj)
{
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char, sText;
    sText = sTextObj.value;
 for (i = 0; i < sText.length && IsNumber == true; i++) 
  { 
  Char = sText.charAt(i); 
  if (ValidChars.indexOf(Char) == -1) 
     {
     alert("Please enter numeric values only");
     sTextObj.value = "";
     IsNumber = false;
     }
  }
return IsNumber;
}
