<!--

/* ======================================================================
FUNCTION:	Trim

INPUT:  		str (string): the string to be altered

RETURN: 		A string with no leading or trailing spaces;
				returns null if invalid arguments were passed

DESC:			This function removes all leading and tralining spaces from a string.

CALLS:		This function depends on the functions TrimLeft & TrimRight
				They must be included in order for this function to work properly.
====================================================================== */
function Trim( str ) {
	var resultStr = "";
	resultStr = TrimLeft(str);
	resultStr = TrimRight(resultStr);

	return resultStr;
} // end Trim

/* ======================================================================
FUNCTION:	TrimLeft

INPUT:  		str (string): the string to be altered

RETURN: 		A string with no leading spaces;
				returns null if invalid arguments were passed

DESC:			This function removes all leading spaces from a string.
====================================================================== */
function TrimLeft( str ) {
	var resultStr = "";
	var i = len = 0;

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)
		return null;

	// Make sure the argument is a string
	str += "";

	if (str.length == 0)
		resultStr = "";
	else {
  		// Loop through string starting at the beginning as long as there
  		// are spaces.
//	  	len = str.length - 1;
		len = str.length;

  		while ((i <= len) && (str.charAt(i) == " "))
			i++;

   	// When the loop is done, we're sitting at the first non-space char,
 		// so return that char plus the remaining chars of the string.
  		resultStr = str.substring(i, len);
  	}

  	return resultStr;
} // end TrimLeft

/* ======================================================================
FUNCTION:	TrimRight

INPUT:  		str (string): the string to be altered

RETURN: 		A string with no trailing spaces;
				returns null if invalid arguments were passed

DESC:			This function removes all trailing spaces from a string.
====================================================================== */
function TrimRight( str ) {
	var resultStr = "";
	var i = 0;

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)
		return null;

	// Make sure the argument is a string
	str += "";

	if (str.length == 0)
		resultStr = "";
	else {
  		// Loop through string starting at the end as long as there
  		// are spaces.
  		i = str.length - 1;
  		while ((i >= 0) && (str.charAt(i) == " "))
 			i--;

 		// When the loop is done, we're sitting at the last non-space char,
 		// so return that char plus all previous chars of the string.
  		resultStr = str.substring(0, i + 1);
  	}

  	return resultStr;
} // end TrimRight

/* ======================================================================
FUNCTION:  	IsNum

INPUT:  		numstr (string/number) - the string that will be tested to ensure
      										 that the value is a number (int or float)

RETURN:  	true, if all characters represent a valid integer or float
     			false, otherwise.

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsNum( numstr ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")
		return false;

	var isValid = true;
	var decCount = 0;		// number of decimal points in the string

	// convert to a string for performing string comparisons.
	numstr += "";

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special cases for negative numbers (first char == '-')
	// and a single decimal point (any one char in string == '.').
	for (i = 0; i < numstr.length; i++) {
		// track number of decimal points
		if (numstr.charAt(i) == ".")
			decCount++;

    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") ||
				(numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) ||
				(numstr.charAt(i) == "." && numstr.length == 1) ||
			  (numstr.charAt(i) == "." && decCount > 1)) {
       	isValid = false;
       	break;
      }
   } // END for

   	return isValid;
}  // end IsNum

/* ======================================================================
FUNCTION:	IsAlphaNumOrSeparator

INPUT:		str (string) - the string to be tested
                separator (char) - the character that can be present

RETURN:  	true, if the string contains only alphanumeric characters or separator.
				false, otherwise.

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsAlphaNumOrSeparator( str, separator ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
		return false;

	var isValid = true;

	str += "";	// convert to a string for performing string comparisons.
	// Loop through string one character at a time. If non-alpha numeric
	// is found then, break out of loop and return a false result

	for (i = 0; i < str.length; i++)
   	{
		// Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
      		if ( !( ((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) ||
      			((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ||
      			(str.charAt(i) == separator) ) )
      		{
   				isValid = false;
         		break;
      		}

	} // END for

	return isValid;

}  // end IsAlphaNumOrSeparator

/* ======================================================================
FUNCTION:  	IsAlpha

INPUT:		str (string) - the string to be tested

RETURN:  	true, if the string contains only alphabetic characters
				false, otherwise.

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsAlpha( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
		return false;

	var isValid = true;

	str += "";	// convert to a string for performing string comparisons.

	// Loop through string one character at time,  breaking out of for
	// loop when an non Alpha character is found.
  	for (i = 0; i < str.length; i++) {
		// Alpha must be between "A"-"Z", or "a"-"z"
		if ( !( ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ) ) {
         				isValid = false;
         				break;
      			}
   } // end for loop

	return isValid;
}  // end IsAlpha

/* ======================================================================
FUNCTION:  	IsPositiveInt

INPUT:  		numstr (string/number) 	 - the string that will be tested to ensure
      										   that each character is a digit

RETURN:  	true, if all characters in the string are a character from 0-9,
     			false, otherwise.

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsPositiveInt( numstr ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")
		return false;

	var isValid = true;

	// convert to a string for performing string comparisons.
	numstr += "";

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special case for negative numbers (first char == '-').
	for (i = 0; i < numstr.length; i++) {
    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9"))) {
       	isValid = false;
       	break;
      }

   } // END for

   	return isValid;
}  // end IsPositiveInt


function IsValidDay(numstr) {
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")
		return false;

	if (!IsPositiveInt(numstr)) {
	return false
	}

	if ((numstr < 1) || (numstr > 31)) {
	return false;
	}

	return true;
}


function IsValidMonth(numstr) {
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")
		return false;

	if (!IsPositiveInt(numstr)) {
	return false
	}

	if ((numstr < 0) || (numstr > 11)) {
	return false;
	}

	return true;
}

function IsValidYear(numstr) {
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")
		return false;
	if (!IsPositiveInt(numstr)) {
	return false
	}
	if ((numstr.length !=2) && (numstr.length != 4)) {
	return false;
	}
	return true;
}


function IsValidAlphaMonth(str) {
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
		return false;

	if (!IsAlpha(str)) {
	return false;
	}

	if (GetMonthNumber(str) == -1) {
        return false;
        }


	return true;
}

function GetMonthNumber(str) {
	var monthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");

	for (i=0;i<12;i++) {
	if (str.toLowerCase() == monthArray[i])
	return i;
	}

	return -1;
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }


/* ======================================================================
FUNCTION:  	IsPositiveNum

INPUT:  		numstr (string/number) - the string that will be tested to ensure
      										 that the value is a number (int or float)

RETURN:  	true, if all characters represent a valid integer or float
     			false, otherwise.

PLATFORMS:	Netscape Navigator 3.01 and higher,
			  	Microsoft Internet Explorer 3.02 and higher,
			  	Netscape Enterprise Server 3.0,
			  	Microsoft IIS/ASP 3.0.
====================================================================== */
function IsPositiveNum( numstr ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")
		return false;

	var isValid = true;
	var decCount = 0;		// number of decimal points in the string

	// convert to a string for performing string comparisons.
	numstr += "";

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special case for a single decimal point (any one char in string == '.').
	for (i = 0; i < numstr.length; i++) {
		// track number of decimal points
		if (numstr.charAt(i) == ".")
			decCount++;

    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") ||
				(numstr.charAt(i) == "."))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "." && numstr.length == 1) ||
			  (numstr.charAt(i) == "." && decCount > 1)) {
       	isValid = false;
       	break;
      }
   } // END for

   	return isValid;
}  // end IsPositiveNum


function checkNPANXX(npa, nxx) {
  if (Trim(npa) == '') {
    alert("Area Code should not be empty.");
    return false;
  }

  if (!IsPositiveInt(npa)) {
    alert("Area Code should be a number.");
    return false;
  }

  if (npa.length < 3) {
    alert("Area Code should be 3 characters long.");
    return false;
  }

  if (npa.charAt(0) == 0) {
    alert("Area Code cannot start with a 0.");
    return false;
  }

  if (Trim(nxx) == '') {
    alert("Prefix should not be empty.");
    return false;
  }

  if (!IsPositiveInt(nxx)) {
    alert("Prefix should be a number.");
    return false;
  }

  if (nxx.length < 3) {
    alert("Prefix should be 3 characters long.");
    return false;
  }

  if (nxx.charAt(0) == 0) {
    alert("Prefix cannot start with a 0.");
    return false;
  }

  return true;
}


function checkDateFormatDDMMYYhyphen(strDate) {

// Check if any other character present
// other than alphanumeric or separator

  if (!IsAlphaNumOrSeparator(strDate,'-')) {
    alert("Not a valid date");
    return false;
  }

// Check if there are 3 parts
  var splitArray = strDate.split('-');

  if (splitArray.length != 3) {
    alert('Invalid Date');
    return false;
  }

// Get the month, day & year

  day = splitArray[0];
  if (!IsValidDay(day)) {
    alert("Day is not valid!");
    return false;
  }

  month = splitArray[1]-1;
  if (!IsValidMonth(month)) {
    alert("Month is not valid!");
    return false;
  }

  year = splitArray[2];
  if (!IsValidYear(year)) {
    alert("Year is not valid!");
    return false;
  }

  if (year.length == 2) {
    year = '20' + year;
  }

// Check whether the whole date is as such valid
// to eliminate dates such as 30-feb,31-jun
  var dt = new Date(year,month,day);

  if ((dt.getFullYear() == year) &&
         (month == dt.getMonth()) &&
         (day == dt.getDate())) {
    return true;
  }
  else {
    alert('Does not comply with format dd-mm-yy');
    return false;
  }
}

function checkDateFormatMMDDYYslash(strDate) {

// Check if any other character present
// other than alphanumeric or separator

  if (!IsAlphaNumOrSeparator(strDate,'/')) {
    alert("Not a valid date");
    return false;
  }

// Check if there are 3 parts
  var splitArray = strDate.split('/');

  if (splitArray.length != 3) {
    alert('Invalid Date');
    return false;
  }

// Get the month, day & year

  month = splitArray[0] - 1;
  if (!IsValidMonth(month)) {
    alert("Month is not valid!");
    return false;
  }
  day = splitArray[1];
  if (!IsValidDay(day)) {
    alert("Day is not valid!");
    return false;
  }

  year = splitArray[2];
  if (!IsValidYear(year)) {
    alert("Year is not valid!");
    return false;
  }

  if (year.length == 2) {
    year = '20' + year;
  }

// Check whether the whole date is as such valid
// to eliminate dates such as 30-feb,31-jun
  var dt = new Date(year,month,day);

  if ((dt.getFullYear() == year) &&
         (month == dt.getMonth()) &&
         (day == dt.getDate())) {
    return true;
  }
  else {
    alert('Does not comply with format mm/dd/yy');
    return false;
  }
}

function checkDateFormatDDMMYYslash(strDate) {

// Check if any other character present
// other than alphanumeric or separator

  if (!IsAlphaNumOrSeparator(strDate,'/')) {
    alert("Not a valid date");
    return false;
  }

// Check if there are 3 parts
  var splitArray = strDate.split('/');

  if (splitArray.length != 3) {
    alert('Invalid Date');
    return false;
  }

// Get the month, day & year

  day = splitArray[0];
  if (!IsValidDay(day)) {
    alert("Day is not valid!");
    return false;
  }

  month = splitArray[1] - 1;
  if (!IsValidMonth(month)) {
    alert("Month is not valid!");
    return false;
  }

  year = splitArray[2];
  if (!IsValidYear(year)) {
    alert("Year is not valid!");
    return false;
  }

  if (year.length == 2) {
    year = '20' + year;
  }

// Check whether the whole date is as such valid
// to eliminate dates such as 30-feb,31-jun


  var test = new Date(year,month,day);

  if ((test.getFullYear() == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate())) {
    return true;
  }
  else {
    alert('Does not comply with format dd/mm/yy');
    return false;
  }
}


function checkDateFormatDDMONYYhyphen(strDate) {

// Check if any other character present
// other than alphanumeric or separator

  if (!IsAlphaNumOrSeparator(strDate,'-')) {
    alert("Not a valid date");
    return false;
  }

// Check if there are 3 parts
  var splitArray = strDate.split('-');

  if (splitArray.length != 3) {
    alert('Invalid Date');
    return false;
  }

// Get the month, day & year

  day = splitArray[0];
  if (!IsValidDay(day)) {
    alert("Day is not valid!");
    return false;
  }

  month = splitArray[1];
  if (!IsValidAlphaMonth(month)) {
    alert("Month is not valid!");
    return false;
  }

  month = GetMonthNumber(month);

  year = splitArray[2];
  if (!IsValidYear(year)) {
    alert("Year is not valid!");
    return false;
  }

  if (year.length == 2) {
    year = '20' + year;
  }

// Check whether the whole date is as such valid
// to eliminate dates such as 30-feb,31-jun
  var dt = new Date(year,month,day);

  if ((dt.getFullYear() == year) &&
         (month == dt.getMonth()) &&
         (day == dt.getDate())) {
    return true;
  }
  else {
    alert('Does not comply with format dd-mon-yy');
    return false;
  }
}

function checkAndConvertMMDDYYYYslash(strDate) {

// Check if any other character present
// other than alphanumeric or separator

  if (!IsAlphaNumOrSeparator(strDate,'/')) {
    alert("Not a valid date");
    return '';
  }

// Check if there are 3 parts
  var splitArray = strDate.split('/');

  if (splitArray.length != 3) {
    alert('Invalid Date');
    return '';
  }

// Get the month, day & year

  month = splitArray[0] - 1;
  if (!IsValidMonth(month)) {
    alert("Month is not valid!");
    return '';
  }
  day = splitArray[1];
  if (!IsValidDay(day)) {
    alert("Day is not valid!");
    return '';
  }

  year = splitArray[2];
  if (!IsValidYear(year)) {
    alert("Year is not valid!");
    return '';
  }

  if (year.length == 2) {
    year = '20' + year;
  }

// Check whether the whole date is as such valid
// to eliminate dates such as 30-feb,31-jun
  var dt = new Date(year,month,day);

  if ((dt.getFullYear() == year) &&
         (month == dt.getMonth()) &&
         (day == dt.getDate())) {
  }
  else {
    alert('Does not comply with format mm/dd/yy');
    return '';
  }


// To get the original month as javascript treats months as 0-11

  month = (month + 1) + '';

  if (month.length == 1) {
    month = '0' + month;
  }

  if (day.length == 1) {
    day = '0' + day;
  }

  return month + '/' + day + '/' + year;

}

function check9999(n) {
  if (!IsPositiveInt(n)) {
    alert('Does not comply with format 9999');
    return false;
  }
  if (n.length > 4 ) {
    alert('Does not comply with format 9999');
    return false;
  }
  return true;
}

function checkDollar999point99(str) {
  if (str.substring(0,1) != '$') {
    alert('Does not comply with format $999.99');
    return false;
  }

  n = str.substring(1);

  if (!IsPositiveNum(n))
    return false;

// Check whether a decimal point exists
  var pos = n.indexOf('.');

  if (pos == -1)
    return true;

// Get number of characters before point
  var wholepart = n.substring(0,pos);
  var fractionpart = n.substring(pos+1);

  if (wholepart.length > 3) {
    alert('Does not comply with format $999.99');
    return false;
  }

  if (fractionpart.length > 2) {
    alert('Does not comply with format $999.99');
    return false;
  }

  return true;
}

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported)
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function roundNumber(n, numDecimals) {
// This function is needed because javascript doesnot have any function
// to round to n decimals

  var tmp = Math.pow(10, numDecimals);
  var result = Math.round(n * tmp) / tmp;

  return result;

}

function numberOfFractionDigits(n) {
// Check whether a decimal point exists
  var pos = n.indexOf('.');

  if (pos == -1)
    return 0;

// Get number of characters after point
  var fractionpart = n.substring(pos+1);

  return fractionpart.length;

}

function wholePart(n) {
// Check whether a decimal point exists
  var pos = n.indexOf('.');

  if (pos == -1)
    return n;

// Get number of characters after point
  var wholepart = n.substring(0,pos);

  return Number(wholepart);

}

function formatNumber(inputNumber, numDecimals) {

	var workNumber = Number(inputNumber);
	var power10 = 1;
	for(var i=1; i <= numDecimals; i++) {power10 *= 10;}

	workNumber = Math.abs((Math.round(workNumber * power10) / power10));

	var strNumber = workNumber + '';

	if (strNumber.indexOf('.') == -1) {strNumber += ".";}

	var decimalString = strNumber.substr(strNumber.indexOf('.'));
	var wholeString = strNumber.substr(0, strNumber.indexOf('.'));
	var wholeNumber = Number(wholeString);

	if (wholeNumber > 1000) {
		var len = wholeString.length;
		wholeString = parseInt("" + (wholeNumber / 1000)) + "," + wholeString.substring(len - 3, len);
	}
	if (wholeNumber > 1000000) {
		var len = wholeString.length;
		wholeString = parseInt("" + (wholeNumber / 1000000)) + "," + wholeString.substring(len - 7, len);
	}

	while(decimalString.length <= numDecimals) {decimalString += "0";}

	if (numDecimals > 0) {
		retVal = wholeString + decimalString;
	}
	else {
		retVal = wholeString;
	}
	return retVal;
}

// Function that mimics javscript function Array.splice
// This function is available only from Javascript 1.2
function splice(aArray, arrayIndex) {

	if (!aArray) {
		return;
	}

	var myArray = new Array();
	var newIndex = 0;
	for (var  i = 0; i < aArray.length; i++) {
		if (i != index) {
		  myArray[newIndex++] = aArray[i];
		}
	}

	aArray = myArray;

}
//-->

