function CheckContent(field, DisplayName, checkOK, msgHead, msgTail) {
  var checkStr = field.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    ReportError(field, DisplayName, msgHead, msgTail);
    return (false);
  }
  return true;
}

function ReportError(field, DisplayName, msgHead, msgTail)
{
	alert(msgHead + " '" + DisplayName + "' " + msgTail);
	field.focus();
}

function trim(s)
{
	var d = String(s); 
	if (!d.length) return "";
	var i_begin = 0, i_end = d.length - 1;
	while (i_begin < i_end && d.charAt(i_begin) == " ") i_begin++;
	while (i_begin <= i_end && d.charAt(i_end) == " ") i_end--;

	return d.substr(i_begin, i_end + 1 - i_begin);
}

function ValidateInt(field, DisplayName)
{
  if(!CheckContent(field, DisplayName, "0123456789", "Please enter only digit in the", " field."))
		return (false);
	
  return (true);
}

function ValidateSelection(field, DisplayName, bNull, bMustSelect, bFirstAllowed)
{
  //alert(field.selectedIndex+ "\n" +DisplayName+ "\n" +bNull+ "\n" +bMustSelect+ "\n" +bFirstAllowed)
  if (field.selectedIndex < 0)
  {
		if(bMustSelect==1)
		{
			ReportError(field, DisplayName, "Please select one of the", "options.");
			return (false);
		} else {
			return(true);
		}
  } 
	
	if(field.selectedIndex >=(bFirstAllowed==0?0:1))
	{
		return (true);
	} else {
		ReportError(field, DisplayName, "Please select one of the", "options.");
		return (false);
	}
  return (true);
}

function ValidateString(field, DisplayName)
{
  if(!CheckContent(field, DisplayName, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~!@#$%^&*()_-+=|\\{}[]:;\'<>,.?/ \t\r\n\f", "Please enter only letter, digit and whitespace characters in the", " field."))
		return (false);
	
  return (true);
}

