// -----------------------------------------------
function jstools_is_valid_email(str) 
{
		var at = "@";
		var dot = ".";
		var lat = str.indexOf(at);
		var lstr = str.length;
		var ldot = str.indexOf(dot);
		if (str.indexOf(at)==-1)
		   return false;
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		   return false;
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		    return false;
		 if (str.indexOf(at,(lat+1))!=-1)
		    return false;
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		    return false;
		 if (str.indexOf(dot,(lat+2))==-1)
		    return false;
		 if (str.indexOf(" ")!=-1)
		    return false;
 		 return true	
}
// end jstools_is_valid_email()


// ------------------------------------------------------------
// return the trimmed string
function jstools_trim(str, charset)
{
	if (typeof charset == 'undefined' || charset == "")
		charset = "\r\n\t";
	return jstools_trim_right(jstools_trim_left(str, charset), charset);
}
// end jstools_trim()

// ------------------------------------------------------------
// return the trimmed string
function jstools_trim_left(str, charset)
{
	if (charset == "")
		charset = new String(" \t\n\r ");
    // last space character is not a space, but alt+0160,
    // another invisible char.
  var s = new String(str);
  if (charset.indexOf(s.charAt(0)) != -1) 
  {
  	var j=0, i = s.length;
    // Iterate from the far left of string until we
    // don't have any more char in charset ...
    while (j < i && charset.indexOf(s.charAt(j)) != -1)
    	j++;
    // Get the substring from the first non-charset
    // character to the end of the string...
    s = s.substring(j, i);
	}
  return s;
}
// end jstools_trim_left()

// ------------------------------------------------------------
// return the trimmed string
function jstools_trim_right(str, charset)
{
	if (charset == "")
		charset = new String(" \t\n\r ");
    // last space character is not a space, but alt+0160,
    // another invisible char.
  var s = new String(str);
  if (charset.indexOf(s.charAt(s.length-1)) != -1) 
  {
  	// We have a string with trailing char(s) from charset ...
    var i = s.length - 1;       // Get length of string
    // Iterate from the far right of string until we
    // don't have any more chars in charset ...
    while (i >= 0 && charset.indexOf(s.charAt(i)) != -1)
    	i--;
    // Get the substring from the front of the string to
    // where the last non-charset character is...
    s = s.substring(0, i+1);
	}
  return s;
}
// end jstools_trim_right()

// ------------------------------------------------------------
// return the int
function jstools_parse_int(str)
{
	str = jstools_trim_left(str, " \t\n\r 0");
	if (str == "") str = "0";
	return parseInt(str);
}
// end jstools_parse_int()

// ------------------------------------------------------------
// date_string has format j/m/y
// return null if date is invalid
function jstools_get_date_object_from_date_string(date_string)
{
	var arDate = new Array();
	arDate  = date_string.split('/');
	if (arDate.length != 3)
		return null;

	var oDate    = new Date(arDate[2], arDate[1] - 1, arDate[0]);
	var iYear    = oDate.getFullYear();
	var iMonth   = oDate.getMonth() + 1;
	var iDay     = oDate.getDate();

	if (iYear != jstools_parse_int(arDate[2]) || iMonth != jstools_parse_int(arDate[1]) || iDay != jstools_parse_int(arDate[0]))
		return null;

	return oDate;	
}
// end jstools_get_date_object_from_date_string()

// ------------------------------------------------------------
// return null if date is invalid
function jstools_get_date_object_from_date_parts(day, month, year)
{
	var oDate    = new Date(year, month - 1, day);
	var iYear    = oDate.getFullYear();
	var iMonth   = oDate.getMonth() + 1;
	var iDay     = oDate.getDate();

	if (iYear != jstools_parse_int(year) || iMonth != jstools_parse_int(month) || iDay != jstools_parse_int(day))
		return null;

	return oDate;	
}
// end jstools_get_date_object_from_date_parts()

// ------------------------------------------------------------
// return formatted time HH:MM
// input str can have format "hmm" "hhmm" "hh:mm" "h:mm"
// if time was invalid return ""
function jstools_format_time_hh_colon_mm(str)
{
	var arTime = jstools_get_time_hh_mm_parts(str);
	if (arTime == null)
		return "";
	
	iHours 		= arTime[0];
	iMinutes	= arTime[1];
		
	ret = iHours < 10 ? "0" + iHours : "" + iHours;
	ret += ":";
	ret += iMinutes < 10 ? "0" + iMinutes : "" + iMinutes;
	
	return ret;
		
}
// end jstools_format_time_hh_colon_mm()

// ------------------------------------------------------------
// return array with hours (array[0]) and minutes (array[1]) in integers
// input str can have format "hmm" "hhmm" "hh:mm" "h:mm"
// if time was invalid return null
function jstools_get_time_hh_mm_parts(str)
{
	var arTime = new Array();
	arTime  = str.split(':');
	if (arTime.length == 1)
	{
		if (str.length == 3)
		{
			iHours 		= jstools_parse_int(str.substr(0, 1));
			iMinutes	= jstools_parse_int(str.substr(1, 2));
		}
		else if (str.length == 4)
		{
			iHours 		= jstools_parse_int(str.substr(0, 2));
			iMinutes	= jstools_parse_int(str.substr(2, 2));
		}
		else
		{
			return null;
		}
	}
	else if (arTime.length == 2)
	{
		iHours 		= jstools_parse_int(arTime[0]);
		iMinutes	= jstools_parse_int(arTime[1]);
	}
	else
	{
		return null;
	}
	
	if (iHours < 0 || iHours > 23 || iMinutes < 0 || iMinutes > 59)
		return null;
		
	ret = new Array();
	ret[0] = iHours;
	ret[1] = iMinutes;
	return ret;		
}
// end jstools_get_time_hh_mm_parts()

