// regular expressions
var reWhitespace	= /^\s+$/
var reDigit 		= /^\d/
var reFloat 		= /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
var reEmail 		= /^.+\@.+\..+$/
var reSignedInteger = /^(\+|\-)?\d+$/

//added by Hendy
var db2DecimalDelimiter = "."
var decimalDelimiter = "."
var displayThousandDelimiter = true
var thousandDelimiter = ","

function addThousand(str) {
   if (isBlank(str)) return ""

    var result = ""
   var db2DecPos = str.indexOf(db2DecimalDelimiter)
   var tmp = (db2DecPos == -1) ? str :
             (str.substring(0, db2DecPos) + decimalDelimiter +
              str.substring(db2DecPos+1, str.length))

    if (displayThousandDelimiter) {
        var decPos = tmp.indexOf(decimalDelimiter)
        var end  = (decPos == -1) ? tmp.length : decPos
        var tail = (decPos == -1) ? "" : tmp.substring(decPos, tmp.length)

        for (var i=end; i>3; i-=3) {
            result = thousandDelimiter + tmp.substring(i-3, i) + result
        }
        result = tmp.substring(0, i) + result + tail
    }
    else result = tmp

    return result
}



// Returns true if string s is empty or
// whitespace characters only.
function isWhitespace (s)
{   // Is s empty?
    return (isEmpty(s) || reWhitespace.test(s));
}

function isEmpty (inputStr)
{
	if (inputStr == "" || inputStr == null)
		return true;
	else
		return false;
}

// function to determine if value is in acceptable range for this application
function inRange (inputStr, lo, hi)
{
	var num = parseInt (inputStr, 10);
	if (num < lo || num > hi)
		return false;
	return true;
}

function select (field)
{
	field.focus();
	field.select();
}

function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function checkDate (day, month, year)
{   
    // catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (validateYear(year) && validateMonth(month) && validateDay(day))) 
    	return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year.value, 10);
    var intMonth = parseInt(month.value, 10);
    var intDay = parseInt(day.value, 10);
    
    var monthMax = new Array (31,29,31,30,31,30,31,31,30,31,30,31);
    var top = monthMax[intMonth - 1];
    var mth = new Array ('January','February','March','April','May','June','July','August','September','October','November','December');
    var monthName = mth[intMonth - 1];
    
    if (!inRange (intDay, 1, top))
    {
	var error = "Please enter a day between 1 and " + top + " for the month of " + monthName;
	alert (error);
	select (day);
	return false;
    }

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) 
    {
    	var error = "There are 29 days in February in a leap year only!";
    	alert (error);	
    	select (day);
    	return false;
    }
    return true;
}

function validateMonth (field)
{
	var input = parseInt (field.value, 10);
	if (isEmpty (input))
	{
		alert ("Please enter the month field.");
		select (field);
		return false;
	}
	else
	{
		if (isNaN (input))
		{
			alert ("Please enter a 2 digit month!");
			select (field);
			return false;
		}
		else
		{
			if (!inRange (input, 1, 12))
			{	
				alert ("Please enter a number between 1 [January] and 12 [December].");
				select (field);
				return false;
			}
		}	
	}
		
	return true;
}

function validateDay (field)
{
	var input = parseInt (field.value, 10);
	if (isEmpty (input))
	{
		alert ("Please enter the day field.");
		select (field);
		return false;
	}
	else
	{
		if (isNaN (input))
		{
			alert ("Please enter a 2 digit day!");
			select (field);
			return false;
		}
		else
		{
			if (!inRange (input, 1, 31))
			{	
				alert ("Please enter a valid day.");
				select (field);
				return false;
			}
		}
	}

	return true;
}

function validateYear (field)
{
	var input = parseInt (field.value, 10);
	if (isEmpty (input))
	{
		alert ("Please enter the year field.");
		select (field);
		return false;
	}
	else
	{
		if (isNaN (input))
		{
			alert ("Please enter a 4 digit year!");
			select (field);
			return false;
		}
		else
		{
			if (!inRange (input, 1900, 2999))
			{	
				alert ("Please enter a valid year.");
				select (field);
				return false;
			}
		}
	}
	return true;
}

function isInteger (field)
{
	//var input = parseInt (field.value, 10);
	if (isNaN(field.value))
		return false;
	else
		return true;
}

function checkEmpty(field)
{
	if (isWhitespace(field.value))
	{
		var error = "Please enter a valid value!";
		alert(error);
		select(field);
		return false;
	}	
	else 
		return true;
}

// function:
//	compareDate
// description:
//	compare 2 input dates against each other, or 1 input date against today
// parameters :		
//	date1Day		first date's day
//	date1Month		first date's month
//	date1Year		first date's year
//	date2Day [optional]	second date's day; if any part of second date not specified, is taken as comparing to today
//	date2Month [optional]	second date's month; if any part of second date not specified, is taken as comparing to today
//	date2Year [optional]	second date's year; if any part of second date not specified, is taken as comparing to today
// return :	
//	0			both dates are equal
//	1			date1 is later than date2
//	2			date1 is earlier than date2
//	3			error
// assumptions :
//
function compareDate (date1Day, date1Month, date1Year, date2Day, date2Month, date2Year)
{
	date2Day = date2Day + "";
	date2Month = date2Month + "";
	date2Year = date2Year + "";
	if (isWhitespace(date1Day) || isWhitespace(date1Month) || isWhitespace(date1Year)) {
		return 3;	
	}		

	if (date2Day == "undefined" || date2Month == "undefined" || date2Year == "undefined") {
		var today = new Date();		
		day2 = parseInt(today.getDate(), 10);
		month2 = parseInt(today.getMonth(), 10) + 1;
		year2 = parseInt(today.getYear(), 10);
/*		if (year2 <= 100) year2 +=1900; 
	*/
		year2 += 1900;	
	}
	else {
		day2 = parseInt(date2Day, 10);
		month2 = parseInt(date2Month, 10);
		year2 = parseInt(date2Year, 10);		
	}
	/*
	else {
		if (isWhitespace(date2Day) || isWhitespace(date2Month) || isWhitespace(date2Year)) {
			return 3;	
		}		
	}
	*/	
 	var day1 = parseInt (date1Day, 10);
 	var month1 = parseInt (date1Month, 10);
 	var year1 = parseInt (date1Year, 10);

	if (year1 > year2)
		return 1;
	else if (year1 == year2) {
		if (month1 > month2)
			return 1;
		else if (month1 == month2) {
			if (day1 > day2)
				return 1;
			else if (day1 == day2)
				return 0;								
		}	
	}

	return 2;
	
}
		
function convertDate (orgDate)
{   
    //orgDate = "yyyy-mm-dd"
    var year = orgDate.substr(0,4);
    var month = parseInt(orgDate.substr(5,2), 10);
    var day = orgDate.substr(8,2);

    var mth = new Array ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
    var monthName = mth[month - 1];

    newDate = day + '-' + monthName + '-' + year;

    return newDate;

}

function checkIntegerLength (i)
{
    if ((i > 2147483647) || (i < -2147483638))
        return false;
    else
        return true; 
}

// Returns true if character c is a digit
// (0 .. 9).
function isDigit (c)
{   
    return reDigit.test(c)
}

function isFloat (s)

{   if (isEmpty(s))
       if (isFloat.arguments.length == 1) return false;
       else return (isFloat.arguments[1] == true);

    return reFloat.test(s)
}		

function isEmail (s)

{   if (isEmpty(s))
       if (isEmail.arguments.length == 1) return false;
       else return (isEmail.arguments[1] == true);

    else {
       return reEmail.test(s)
    }
}

function isValidPhoneFax(s)
{
   s = "" + s
   var result = true
   var len = s.length
   var i = 0
   while ( i < len && result == true)
   {
      if (s.charAt(i) == '/' || s.charAt(i) == '(' || s.charAt(i) == ')' || s.charAt(i) == '-' || isDigit(s.charAt(i)) || s.charAt(i) == ' ')
         i++
      else
         result = false
   }
   return result
}

function checkOthers(choice, others, field)
{
   if (choice != 'Others' && !isWhitespace(others.value))
   {
      alert("Please enter in the Others field only with the Others option selected in the " + field + " list!");
      return false;
   }

   if (choice == "Others" && isWhitespace(others.value))
   {
      alert("Please enter the " + field + " in the Others field provided!");
      select(others);
      return false;
   }

   return true;
}

function checklength(field,maxlength)
{
   if (field.value.length > maxlength)
   {
      var maxstring = "" + maxlength ;
      alert ("Input field has a max of " + maxstring + " characters!");
      return false;
   }
   
   return true;
}

function isNonnegativeInteger (s)
{   
    var secondArg = false;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isSignedInteger (s)
{   
    if (isEmpty(s))
       
       if (isSignedInteger.arguments.length == 1) return false;
       else return (isSignedInteger.arguments[1] == true);

    else {
       return reSignedInteger.test(s)
    }
}

function dropLeadingAndTrailingBlanks( s ) {
    // alert( "ENTER dropLeadingAndTrailingBlanks(), s = " + s )
    if( isBlank( s ) ) return ""
    var fwd = 0
    while( isBlank( s.charAt( fwd ) ) ) {
        fwd++
    }
    var back = s.length - 1
    while( isBlank( s.charAt( back ) ) ) {
        back--
    }
    var newS = s.substring( fwd, back + 1 )
    // alert( "RETURN newS = " + newS )
    return newS
}

function trimBlanks( s )
{
    return( dropLeadingAndTrailingBlanks( s ) );
}

function isBlank(str) {
    if ((str != null) && (str != "")) {
        for (var i=0; i < str.length; i++) {
            var ch = str.charAt(i)
            if ((ch != ' ') && (ch != '\r') && (ch != '\n') && (ch != '\t')) {
                return false
            }
        }
    }

    return true
}

