// Return 's' without leading and trailing spaces.
function strip_spaces (s)
{
  var i, j;
  for (i = 0; i < s.length && 
              (s.charAt (i) == " " || s.charAt(i) == "\t"); i++);
  for (j = s.length - 1; j > 0 && 
                        (s.charAt (j) == " " || s.charAt (j) == "\t"); j--);
  if (j < i)
    return "";
  else
    return s.substring (i, j + 1);
}

// Return position of the first character that is not a digit or -1 if all
// the characters are digits.
function find_not_digit (s)
{
  var i;
  for (i = 0; i < s.length && s.charAt (i) >= "0" && s.charAt (i) <= "9"; i++);
  if (i < s.length)
    return i;
  else
    return -1;
}

// Return "" if 'v' is chacked, otherwise error message.
function is_checked (v,descr)
{
  var i;
  for (i=0; i<v.length; i++)
  { 
    if (v[i].checked )
    {
      return "";
    }
  }
  return "Ïîëå '"+descr+"' äîëæíî áûòü çàïîëíåíî";
}



// Return "" if 'v' is not empty, otherwise error message.
function is_not_empty (vv,descr)
{
  var v=vv.value;
  if (strip_spaces (v) != "")
    {
      return "";
    }
  else
    {
        return "Ïîëå '"+descr+"' äîëæíî áûòü çàïîëíåíî";
    }
}

// Return "" if 'v' is a number, otherwise error message.
function is_number (vv,descr)
{
  var v=vv.value;
  if (v != "" && isNaN (parseFloat (v)))
    {
        return "Ïîëå '"+descr+"' äîëæíî áûòü çàïîëíåíî";
    }
  else
    {
      return "";
    }
}

// Return "" if 'v' is an integer number, otherwise error message.
function is_int (vv,descr)
{
  var v=vv.value;
  v = strip_spaces (v);
  if (v == "")
    return "";
  if (v.charAt (0) == "+" || v.charAt (0) == "-")
    v = v.substring (1);
  if (find_not_digit (v) != -1)
    {
      return "ðÏÌÅ '"+descr+"' ÄÏÌÖÎÏ ÓÏÄÅÒÖÁÔØ ÃÅÌÏÅ ÞÉÓÌÏ";
    }
  else
    {
      return "";
    }
}

// True if date format DMY, false if MDY.
DMY_dates = true;

// Return "" if 'v' is a date, otherwise error message.
function is_date (vv,descr)
{
  var v=vv.value;
  var i;
  var sep;
  var year;
  var month;
  var day;
  var num_days_in_month;

  num_days_in_month = new Array ();
  num_days_in_month[1] = 31;
  num_days_in_month[2] = 28;
  num_days_in_month[3] = 31;
  num_days_in_month[4] = 30;
  num_days_in_month[5] = 31;
  num_days_in_month[6] = 30;
  num_days_in_month[7] = 31;
  num_days_in_month[8] = 31;
  num_days_in_month[9] = 30;
  num_days_in_month[10] = 31;
  num_days_in_month[11] = 30;
  num_days_in_month[12] = 31;

  v = strip_spaces (vv.value);
  if (v == "")
    return "";

  // DD/MM/YYYY
  //   ^
  i = find_not_digit (v);
  if (i > 0)
    {
      // sep is '/', v is 'MM/YYYY', day is 'DD'.
      sep = v.charAt (i);
      if (DMY_dates)
        day = parseInt (v.substring (0, i), 10);
      else
        month = parseInt (v.substring (0, i), 10);
      v = v.substring (i + 1);
      if (sep == "/" || sep == "." || sep == "-")
        {
          // MM/YYYY
          //   ^
          i = find_not_digit (v);
          
          if (i > 0 && v.charAt (i) == sep) 
            {
              // v is YYYY, month is MM
              if (DMY_dates)
                month = parseInt (v.substring (0, i), 10);
              else
                day = parseInt (v.substring (0, i), 10);
              v = v.substring (i+1);
              if (v != "" && find_not_digit (v) == -1)
                {
                   year = parseInt (v, 10);
                   if ((year >= 0 && month > 0 && month <= 12 && day > 0) &&
                       (day <= num_days_in_month[month] ||
                        (month == 2 && day == 29 && year % 4 == 0 &&
                         (year % 100 != 0 || year % 400 == 0))))
                     return "";
                }
            }
        }
    }
  return "ðÏÌÅ '"+descr+"' ÄÏÌÖÎÏ ÓÏÄÅÒÖÁÔØ ÄÁÔÕ";
}

// Return array of two items: field_name and predicate.
function validate (field_name, field_desc, predicate)
{
  var a;
  a = new Array ();
  a[0] = field_name;
  a[1] = field_desc;
  a[2] = predicate;
  return a;
}

// 'form' is a HTML from object. Each item of array 'field_list' is 
// an array: the first item is name of a field in the 'form', the second is field description and
// the rest items are functions that validate the value of this field.
// For each field the validation fuctions are applied to the field value.
// If a validation function returns an error message, the message is
// displayed and false is returned, otherwise true is returned.
function validate_fields (form, field_list)
{
  var i;
  for (i = 0; i < field_list.length; i++)
    {
      var field_name = field_list[i][0];
      var j;
      for (j = 2; j < field_list[i].length; j++)
        {
          var check_func = field_list[i][j];
          //alert("checking "+field_list[i][1]+" of "+check_func);
          var res = check_func (form[field_name],field_list[i][1]);
          if (res != "")
            {
              alert ("Âíèìàíèå: " + res);
//              form[field_name].focus ();
//              form[field_name].select ();
              return false;
            }
         }
    }
  return true;
}

// Return 's' with all newlines replaced by spaces.
function remove_newlines (s)
{
  var s1, i;
  s1 = "";
  for (i = 0; i < s.length; i++)
    if (s.charAt (i) == "\r" || s.charAt (i) == "\n")
       s1 += " ";
    else 
       s1 += s.charAt (i);
  return s1;
}

