// ---------------------------------
// Author:	Edwin Mak
// Date: 05/17/2000
//
// -----------------------------------------------------------------



// -----------------------------------------------------------------
//	
// function isDate()			
// Check a string if it is a valid date, 
// Accept Date in format 'mm/dd/yyyy'
//
function isDate(d) {
	var dateFormat = /^\d{1,2}\/\d{1,2}\/\d{4,4}$/
	var daysInMonth = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
	
	if (! dateFormat.test(d))
		return false;

	var ss= d.split("/");
	var dd = ss[1],  mm = ss[0], yyyy = ss[2];	
	
	if (yyyy < '1753') return false; // the smallest value that SQL Server can accept
	
	if (!(mm > 0 && mm < 13) || !(dd > 0 && dd < 32))
		return false;
	if (dd > daysInMonth[mm])
		return false;
	if ((mm=="2" || mm=="02") && dd > daysInFebruary(yyyy))
		return false;
	return true;
}

// -----------------------------------------------------------------
//	
// function isSmalldate()			
// Check a string if it is a valid date, 
// Accept Date in format 'mm/dd/yy' or 'mm/dd/yyyy'
//
function isSmalldate(d) {
	var dateFormat = /^\d{1,2}\/\d{1,2}\/\d{2,4}$/
	var daysInMonth = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
	
	if (! dateFormat.test(d))
		return false;

	var ss= d.split("/");
	var dd = ss[1],  mm = ss[0], yyyy = ss[2];	
	
	if (yyyy < 1900 || yyyy > 2078) return false; // the smallest value that SQL Server can accept
	
	if (!(mm > 0 && mm < 13) || !(dd > 0 && dd < 32))
		return false;
	if (dd > daysInMonth[mm])
		return false;
	if ((mm=="2" || mm=="02") && dd > daysInFebruary(yyyy))
		return false;
	return true;
}

// -----------------------------------------------------------------
//
// function daysInFebruary(whichYear)		
// Return the number of days in February of a given year
//
function daysInFebruary(whichYear) {
    return (whichYear % 4 == 0 && (!(whichYear % 100 == 0) || (whichYear % 400 == 0)) ? 29 : 28);
}


function numDaysInMonth(mm,yyyy) {
	var daysInMonth = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
	
	if (!(mm > 0 && mm < 13)) return false;
	
	if ((mm=="2" || mm=="02")) return daysInFebruary(yyyy);	

	return daysInMonth[mm];
}




// -----------------------------------------------------------------
//
// function DateDiff()		
// Return the differnece between two dates in number of days
//
function DateDiff(d1,d2) {
	
	if (isDate(d1) && isDate(d2)) {
		var ADate = new Date(d1), DDate = new Date(d2), diff  = new Date();

		diff.setTime(Math.abs(DDate.getTime() - ADate.getTime()));
		var v = Math.floor(diff.getTime() / (1000 * 60 * 60 * 24));
		return (DDate.getTime() > ADate.getTime() ? v : v * -1);
	}
	return NaN;
}

// -----------------------------------------------------------------
// function isFloat(n)
// Check if a string is a float numeric. Empty string not count.
//
function isFloat(n)
{
	return (isNaN(parseFloat(n)) || parseFloat(n) != n ? false : true);
}
// -----------------------------------------------------------------
function isInt(n)
{
	var v = parseInt(n);
	if (isNaN(v)|| v != n ) return false;
	return (v >= -2147483648 && v <= 2147483647 ? true : false);
}
// -----------------------------------------------------------------
function isSmallint(n)
{
	var v = parseInt(n);
	if (isNaN(v) || v != n ) return false;
	return (v >= -32768 && v <= 32767 ? true : false);
}
// -----------------------------------------------------------------
function isTinyint(n)
{
	var v = parseInt(n);
	if (isNaN(v)|| v != n ) return false;
	return (v >= 0 && v <= 255 ? true : false);
}
// -----------------------------------------------------------------
//
// function isMonthYear(n)
// Check if the string is in MMYY format. MM is in range 01 to 12 and YY in range 00-99
//
function isMonthYear(n)
{
	var MonthYearFormat = /^0[1-9]\d{2}$|^1[0-2]\d{2}$/
	return (MonthYearFormat.test(n));	
}
// -----------------------------------------------------------------
//
// function isBlank(n)
// Check if a string is empty or contains only white spaces.
//
function isBlank(n)
{
	var s = /^[ ]*$/
	return (s.test(n));	
}
// -----------------------------------------------------------------
// function trim(str)
// trim off leading and trailing spaces
function trim(str)
{
	if ('' == str) 
		return('');
	
	var s = 0, f = str.length;
	for (i=0; i < str.length; i++)
		if (' ' != str.charAt(i)) {
			s = i;
			break;
		}	
	for (i = str.length-1; i > s; i--)
		if (' ' != str.charAt(i)) {
			f = i+1;
			break;
		}
	return (str.substring(s,f));
}

// -----------------------------------------------------------------
// function isEmail(str)
// check the string is in a valid email address format

function isEmail(str) {
  if (window.RegExp) {
    var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
    var reg1 = new RegExp(reg1str);
    var reg2 = new RegExp(reg2str);
    if (!reg1.test(str) && reg2.test(str))
      return true;

    return false;
  } else {
    if(str.indexOf("@") >= 0)
      return true;

    return false;
  }
}

	
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');

	if(isNaN(num)) num = "0";
	cents = Math.floor((num*100+0.5)%100);
	num = Math.floor((num*100+0.5)/100).toString();
	if(cents < 10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));

	return ('$' + num + '.' + cents);
}  

function CA(){

	for (var i=0;i<frm.elements.length;i++) {
		var e = frm.elements[i];

		if ((e.name != 'allbox') && (e.type=='checkbox')) {
			e.checked = frm.allbox.checked;
			if (frm.allbox.checked)
				hL(e);
			else
				dL(e);
		}
	}
}

function CCA(CB){
	if (CB.checked)
		hL(CB);
	else
		dL(CB);

	var TB=TO=0;

	for (var i=0;i<frm.elements.length;i++) {
		var e = frm.elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox')) {
			TB++;
			if (e.checked)
				TO++;
		}
	}

	if (TO==TB)
		frm.allbox.checked=true;
	else
		frm.allbox.checked=false;
}

function hL(E){

	while (E.tagName!="TR")
		{E=E.parentElement;}

	E.className = "H";
}

function dL(E){
	
	while (E.tagName!="TR")
		{E=E.parentElement;}

	E.className = "";
}


function reportXMLError(err) {
var errMsg;
	errMsg = "XML file load failed." + "<BR>";
	errMsg += err.reason +  "<BR>";
	errMsg += "Line: " + err.line + "<BR>";
	errMsg += "Char: " + err.linepos + "<BR>";
	errMsg += "Text: " + err.srctext + "<BR>";
	return errMsg;
}

function reportCustomError(xmlDoc) {
var errMsg;
	errMsg = "Processing error." + "<BR>";
	errMsg += "Source: " + xmlDoc.documentElement.lastChild.text + "<BR>";
	errMsg += "Description: " + xmlDoc.documentElement.firstChild.text + "<BR>";
	return errMsg;
}

function showhelp() {
	var sFeatures, left = window.event.x, top = window.event.y;

	sFeatures = 'top=' + top +'; left=' + left-500;
	sFeatures = 'height=270; width=500; ' + sFeatures + ';  scrollbars=1; resizable=0; status=0; menubar=0; titlebar=0;'
	window.open("../help/help.htm","",sFeatures);	
	
}
