function str_trim( str )
{
	var resultStr = "";
	
	resultStr = str_trimleft(str);
	resultStr = str_trimright(resultStr);
	
	return resultStr;
} 

function str_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;
		
  		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;
} 

function str_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 str_replacechar( findchar, replacestring, searchstring )
{
	newstring = "";
	for ( c=0; c < searchstring.length ; c++ )
	{
		string_char = searchstring.charAt( c );
		if (  string_char == findchar )
		{
			newstring = newstring + replacestring;
		}
		else
		{
			newstring = newstring + string_char;
		}
	}

	return newstring;
}

function isWhitespace(s)
{
   var i;

   // Is s empty?
   if (isEmpty(s)) return true;

   // Search through string's characters one by one
   // until we find a non-whitespace character.
   // When we do, return false; if we don't, return true.

   for (i = 0; i < s.length; i++)
   {
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
   }

   // All characters are whitespace.
   return true;
}

function isValidURL(strUrl)
{
  if (window.RegExp) 
  {
    var rs1 = "^((http://)|(https://)|(www\\.)).+\\.[a-zA-Z]{2,4}.*$";
    var reg1 = new RegExp(rs1);
    return reg1.test(strUrl);
  }
  else
  {
    if (strUrl.substr(0,4).toLowerCase=="www." || strUrl.substr(0,7).toLowerCase=="http://" || strUrl.substr(0,8).toLowerCase=="https://")
       return true;
  }
  return false;
} 

function isValidEmail(EmailAddr)
{
  var str = EmailAddr;
  if (window.RegExp) 
  {
    var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[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 isValidNumber(number)
{
  if (window.RegExp) 
  {
    var rs1 = "^[0-9]+[\\.]*[0-9]*$";
    var reg1 = new RegExp(rs1);
    return reg1.test(number.replace(/,/g, ''));    
  } 
  
  return true;
}

// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.
// The function returns true if a valid date, false if not.
// ******************************************************************
function isDate(dateStr) {
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
		alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy.");
		return false;
	}

	day		= matchArray[1];
	month	= matchArray[3]; 
	year	= matchArray[5];

	if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month " + month + " doesn`t have 31 days.")
		return false;
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn`t have " + day + " days.");
			return false;
		}
	}
	return true; // date is valid
}

function Trim(s) {
	// remove leading spaces and carriage returns
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
	{
		s = s.substring(1,s.length);
	}

	// Remove trailing spaces and carriage returns
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
	{
		s = s.substring(0,s.length-1);
	}
	return s;
}
