//<!--
  function CV_CheckNumber(sender, args)
  {
    if (isNaN(args.Value))
    {
         args.IsValid = false;
         return false;
    }

    args.IsValid = true;
    return true;
  }
  
  function CV_CheckWholeNumber(sender, args)
  {
    if (isNaN(args.Value.replace(/\,/g, "")))
    {
         args.IsValid = false;
         return false;
    }

    if (args.Value.indexOf('.') > 0)
    {
         args.IsValid = false;
         return false;
    }

    args.IsValid = true;
    return true;
  }
  
  function CV_CheckIsYear(sender, args)
  {
    if (! CV_CheckWholeNumber(sender, args))
    {
         args.IsValid = false;
         return false;
    }

    if (args.Value.length < 4)
    {
         args.IsValid = false;
         return false;
    }

    args.IsValid = true;
    return true;
  }
  
  function CV_isValidDate(sender, args)
  {
    var object_value = args.Value;
    
	//Returns true if value is a date format or is NULL, otherwise returns false	

    if (object_value.length == 0)
    {
        args.IsValid = false;
    	return true;
    }

   //Returns true if value is a date in the mm/dd/yyyy format
	isplit = object_value.indexOf('/');

	if (isplit == -1 || isplit == object_value.length)
	{
	    args.IsValid = false;
		return false;
	}

   sMonth = object_value.substring(0, isplit);

	if (sMonth.length == 0)
	{
	    args.IsValid = false;
        return false;
    }

	isplit = object_value.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == object_value.length)
	{
	    args.IsValid = false;
		return false;
	}

   sDay = object_value.substring((sMonth.length + 1), isplit);

	if (sDay.length == 0)
	{
	    args.IsValid = false;
        return false;
    }

	sYear = object_value.substring(isplit + 1);

	if (!checkinteger(sMonth)) //check month
	{
	    args.IsValid = false;
		return false;
	}
	else if (!checkrange(sMonth, 1, 12)) //check month
	{
	    args.IsValid = false;
		return false;
	}
	else if (!checkinteger(sYear)) //check year
	{
	    args.IsValid = false;
		return false;
	}
	else if (!checkrange(sYear, 0, 9999)) //check year
	{
	    args.IsValid = false;
		return false;
	}
	else if (!checkinteger(sDay)) //check day
	{
	    args.IsValid = false;
		return false;
	}
	else if (!checkday(sYear, sMonth, sDay)) // check day
	{
	    args.IsValid = false;
		return false;
	}
	else
	{
	    args.IsValid = true;
		return true;
	}
}

function checkday(checkYear, checkMonth, checkDay) {
	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 ||
			checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else
	if (checkMonth == 2)	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else
		if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}
	return checkrange(checkDay, 1, maxDay); //check day
}
function checkinteger(object_value) {
	//Returns true if value is a number or is NULL
   //otherwise returns false	

   if (object_value.length == 0)
      return true;

   //Returns true if value is an integer defined as
   //   having an optional leading + or -.
   //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

   //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
   //Was it a decimal?
   if (check_char < 1)
		return checknumber(object_value);
   else
		return false;
}
function checkrange(object_value, min_value, max_value) {
   //if value is in range then return true else return false

   if (object_value.length == 0)
      return true;

   if (!checknumber(object_value))	{
		return false;
	}
   else {
		return (numberrange((eval(object_value)), min_value, max_value));
	}
	
   //All tests passed, so...
   return true;
}
function checknumber(object_value) {
	//Returns true if value is a number or is NULL
   //otherwise returns false	

   if (object_value.length == 0)
        return true;

   //Returns true if value is a number defined as
   //   having an optional leading + or -.
   //   having at most 1 decimal point.
   //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

   //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
   //Was it a decimal?
	if (check_char == 1)
		decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++) {
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1) {
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0) {
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks
		}
      else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
   //All tests passed, so...
   return true
}
function numberrange(object_value, min_value, max_value) {
	// check minimum
   if (min_value != null) {
      if (object_value < min_value)
			return false;
	}

   // check maximum
   if (max_value != null) {
	if (object_value > max_value)
		return false;
	}
	
	//All tests passed, so...
   return true;
}

function update_char_count(comments_id, numchars_id, max_length)
{
    var object_comments = document.getElementById(comments_id);
    var object_numchars = document.getElementById(numchars_id);
    
    object_numchars.value = (max_length - parseInt(object_comments.value.length));
}

function handle_maxlength(comments_id, max_length)
{
    var object_comments = document.getElementById(comments_id);
    
    if (object_comments.value.length > max_length-1)
        object_comments.value = object_comments.value.substring(0, max_length-1);
}

// -->
