function cookietest()
{
   if (document.cookie == "")
   {
     if( document.login )
       document.login.cookieexists.value ="false";
   }
   else
   {
    if( document.login )
       document.login.cookieexists.value ="true";
   }
}

document.cookie = 'killme' + escape('nothing');

function has_parent()
{
  return parent.mainFrame;
}

function EmptyValue(control, message)
{
  input = control['value'];
  key   = control['name'];

  if (input == '')
  {
    set_error_mark( key, message );
    return true;
  }
  return false;
}

function is_value(input, key, value, message)
{
  if (input == value)
  {
    set_error_mark( key, message );
    return true;
  }
  else
  {
    return false;
  }
}

function check_length(control, min, max, b_mandatory, message, mand_message)
{
  input = control['value'];
  key   = control['name'];

  if ((input.length == 0) && b_mandatory)
  {
    set_error_mark( key, mand_message );
    return false;
  }
  if (input != '')
  {
    if (input.length < min || input.length > max)
    {
      set_error_mark( key, message );
      return false;
    }
  }
  return true;
}

function check_nan_length( input, key, b_mandatory, min, max, msg, mand_message )
{
  if ((input.length == 0) && b_mandatory)
  {
    set_error_mark( key, mand_message );
    return false;
  }
  
  if( !isNaN( input ) )
  {
    return true;
  }

  if (input != '')
  {
    if (input.length < min || input.length > max)
    {
      set_error_mark( key, msg );
      return false;
    }
  }
  return true;
}

// used for 2 input fields !!
function check_equal(control1, control2, message)
{
  input1  = control1['value'];
  key1    = control1['name'];
  input2  = control2['value'];
  key2    = control2['name'];

  if (input1 != input2)
  {
    set_error_mark( key1, message );
    set_error_mark( key2, message );
    return false;
  }
  else
    return true;
}

// used for 2 input fields !!
function check_min_max(control1, control2, message)
{
  input1  = parseInt( control1['value'], 10 );
  key1    = control1['name'];
  input2  = parseInt( control2['value'], 10 );
  key2    = control2['name'];

  if ( (input1 != "") && (input2 != ""))
    if (input1 > input2)
    {
      set_error_mark( key1, message );
      set_error_mark( key2, message );
      return false;
    }
    else
      return true;
  else
    return true;
}

// used for 2 ips fields !!
function check_ip_min_max(controlFromB0, controlFromB1, controlFromB2, controlFromB3, controlToB0, controlToB1, controlToB2, controlToB3, message)
{
  var keyIpFrom, keyIpTo;
  
  keyIpFrom = 'framedipfrom';
  keyIpTo   = 'framedipto';
  
  IpFrom  = "SPANframedipfrom";
  IpTo    = "SPANframedipto";
  FromB0  = parseInt( controlFromB0['value'], 10 );
  FromB1  = parseInt( controlFromB1['value'], 10 );
  FromB2  = parseInt( controlFromB2['value'], 10 );
  FromB3  = parseInt( controlFromB3['value'], 10 );
  ToB0    = parseInt( controlToB0['value'], 10 );
  ToB1    = parseInt( controlToB1['value'], 10 );
  ToB2    = parseInt( controlToB2['value'], 10 );
  ToB3    = parseInt( controlToB3['value'], 10 );

  FromIP = FromB0 * 16777216; //256^3
  FromIP = FromIP + (FromB1 * 65536);    //256^2
  FromIP = FromIP + (FromB2 * 256);
  FromIP = FromIP + FromB3;
  ToIP = ToB0 * 16777216; //256^3
  ToIP = ToIP + (ToB1 * 65536);    //256^2
  ToIP = ToIP + (ToB2 * 256);
  ToIP = ToIP + ToB3;

  if ( ( !isNaN(FromIP) && isNaN(ToIP) )  || ( FromIP > ToIP ) )
  {
    set_error_mark( keyIpFrom, message );
    set_error_mark( keyIpTo, message );
    return false;
  }
  else
  {
    return true;
  }

}

// used for 2 ips fields !!
function check_ip_min_max_version2(controlFrom, controlTo, controlFromB0, controlFromB1, controlFromB2, controlFromB3, controlToB0, controlToB1, controlToB2, controlToB3, message, mandatory_message)
{
  IpFrom  = "SPAN" + controlFrom;
  IpTo    = "SPAN" + controlTo;
  if ( (controlFromB0['value'] == '') && (controlToB0['value'] != '') )
  {
    set_error_mark( controlFrom, mandatory_message );
    return false;
  }
  if ( (controlToB0['value'] == '') && (controlFromB0['value'] != '') )
  {
    set_error_mark( controlTo, mandatory_message );
    return false;
  }

  FromB0  = parseInt( controlFromB0['value'], 10 );
  FromB1  = parseInt( controlFromB1['value'], 10 );
  FromB2  = parseInt( controlFromB2['value'], 10 );
  FromB3  = parseInt( controlFromB3['value'], 10 );
  ToB0    = parseInt( controlToB0['value'], 10 );
  ToB1    = parseInt( controlToB1['value'], 10 );
  ToB2    = parseInt( controlToB2['value'], 10 );
  ToB3    = parseInt( controlToB3['value'], 10 );

  FromIP = FromB0 * 16777216; //256^3
  FromIP = FromIP + (FromB1 * 65536);    //256^2
  FromIP = FromIP + (FromB2 * 256);
  FromIP = FromIP + FromB3;
  ToIP = ToB0 * 16777216; //256^3
  ToIP = ToIP + (ToB1 * 65536);    //256^2
  ToIP = ToIP + (ToB2 * 256);
  ToIP = ToIP + ToB3;

  if ( ( !isNaN(FromIP) && isNaN(ToIP) )  || ( FromIP > ToIP ) )
  {
    set_error_mark( controlFrom, message );
    set_error_mark( controlTo, message );
    return false;
  }
  else
  {
    return true;
  }

}

// internal function
function check_integer_value (value)
{
  var   i;
  var   aChar;
  var   len;
  var   skip;

  len = value.length;

  for (i = 0; i < len; i++)
  {
    aChar = value.substring(i, i + 1);
    if ((i==0) && (aChar=="-"))
    {
      skip = 1;
    }
    else
    {
      if ((aChar < "0") || (aChar > "9"))
        return false;
      }
  }
  return true;
}

function check_range(control, min, max, b_mandatory, message, mand_message)
{
  var   i;
  var   aChar;
  var   len;

  input = control['value'];
  key   = control['name'];

  if ((input.length == 0) && b_mandatory)
  {
    set_error_mark( key, mand_message );
    return false;
  }

  if (input != '')
  {
    if (! check_integer_value(input))
    {
      set_error_mark( key, message );
      return false;
    }

    if (input < min || input > max)
    {
      set_error_mark( key, message );
      return false;
    }
  }
  return true;
}

// internal function
function check_float_value (value)
{
  var i;
  var aChar;
  var len;
  var seenDecimalPoint = false;
  var skip;

  len = value.length;

  for (i = 0; i < len; i++)
  {
    aChar = value.substring(i, i + 1);
    if ((i==0) && (aChar=="-"))
    {
      continue;
    }
    else
    {
      if ((aChar < "0") || (aChar > "9"))
      {
        if (seenDecimalPoint)
          return false;

        if ( aChar == "." || aChar == "," )
          seenDecimalPoint = true;
        else
          return false;
      }
    }
  }
  return true;
}

function check_float(control, b_mandatory, message, mand_message)
{
  var   i;
  var   aChar;
  var   len;

  input = control['value'];
  key   = control['name'];

  if ((input.length == 0) && b_mandatory)
  {
    set_error_mark( key, mand_message );
    return false;
  }

  if (input != '')
  {
    if (! check_float_value(input))
    {
      set_error_mark( key, message );
      return false;
    }
  }
  return true;
}


function check_ip_address(control0, control1, control2, control3, key, b_mandatory, message)
{
  var   bAlert;
  var   iInput = new Array(4);
  var   i;

  input0  = control0['value'];
  input1  = control1['value'];
  input2  = control2['value'];
  input3  = control3['value'];

  if (! b_mandatory)
  {
    if ((input0 == '') && (input1 == '') && (input2 == '') && (input3 == ''))
      return(true);
  }

  bAlert = false;
  if ((! bAlert) && (! check_integer_value(input0)))
    bAlert = true;
  if ((! bAlert) && (! check_integer_value(input1)))
    bAlert = true;
  if ((! bAlert) && (! check_integer_value(input2)))
    bAlert = true;
  if ((! bAlert) && (! check_integer_value(input3)))
    bAlert = true;

  if (bAlert)
  {
    set_error_mark( key, message );
    return false;
  }

  iInput[0] = parseInt(input0, 10);
  iInput[1] = parseInt(input1, 10);
  iInput[2] = parseInt(input2, 10);
  iInput[3] = parseInt(input3, 10);

  for (i = 0; i < 4; i++)
  {
    if (isNaN(iInput[i]))
    {
      bAlert = true;
      break;
    }
    if ((iInput[i] < 0) || (iInput[i] > 255))
    {
      bAlert = true;
      break;
    }
  }

  if (bAlert)
  {
    set_error_mark( key, message );
    return false;
  }

  bAlert = true;
  for (i = 0; i < 4; i++)
  {
    if (iInput[i] != 255)
      bAlert = false;
  }

  if (bAlert)
  {
    set_error_mark( key, message );
    return false;
  }

  return true;
}

function check_netmask_address(control0, control1, control2, control3, key, b_mandatory, b_spec_netmask, message)
{
  var   bAlert;
  var   iInput = new Array(4);
  var   i, value, len;
  var   valid_ip_address = new Array(0x000000ff,         // first field 255
          0x000080ff,
          0x0000C0ff,
          0x0000E0ff,
          0x0000f0ff,
          0x0000f8ff,
          0x0000fcff,
          0x0000feff,
          0x0000ffff,
          0x0080ffff,
          0x00e0ffff,
          0x00c0ffff,
          0x00f0ffff,
          0x00f8ffff,
          0x00fcffff,
          0x00feffff,
          0x00ffffff,
          0x80ffffff,
          0xc0ffffff,
          0xe0ffffff,
          0xf0ffffff,
          0xf8ffffff,
          0xfcffffff);
//          0xfeffffff,
//          0xffffffff);

  input0  = control0['value'];
  input1  = control1['value'];
  input2  = control2['value'];
  input3  = control3['value'];

  if (! b_mandatory)
  {
    if ((input0 == '') && (input1 == '') && (input2 == '') && (input3 == ''))
      return(true);
  }

  bAlert = false;
  if ((! bAlert) && (! check_integer_value(input0)))
    bAlert = true;
  if ((! bAlert) && (! check_integer_value(input1)))
    bAlert = true;
  if ((! bAlert) && (! check_integer_value(input2)))
    bAlert = true;
  if ((! bAlert) && (! check_integer_value(input3)))
    bAlert = true;

  if (bAlert)
  {
    set_error_mark( key, message );
    return false;
  }

  iInput[0] = parseInt(input0, 10);
  iInput[1] = parseInt(input1, 10);
  iInput[2] = parseInt(input2, 10);
  iInput[3] = parseInt(input3, 10);

  for (i = 0; i < 4; i++)
  {
    if (isNaN(iInput[i]))
    {
      bAlert = true;
      break;
    }
    if ((iInput[i] < 0) || (iInput[i] > 255))
    {
      bAlert = true;
      break;
    }
  }

  if (bAlert)
  {
    set_error_mark( key, message );
    return false;
  }

  value = 0;
  for (i = 3; i >= 0; i--)
  {
    value = value * 256;
    value += iInput[i];
  }

  if (b_spec_netmask)
  {
    if (value == 0xfeffffff)
      return true;
    if (value == 0xffffffff)
      return true;
  }

  if (bAlert)
  {
    set_error_mark( key, message );
    return false;
  }

  bAlert = true;
  len = valid_ip_address.length;
  for (i = 0; i < len; i++)
  {
    if (value == valid_ip_address[i])
    {
      bAlert = false;
      break;
    }
  }

  if (bAlert)
  {
    set_error_mark( key, message );
    return false;
  }

  return true;
}

function check_date_time(i_days, i_months, i_years, i_hours, i_minutes, i_seconds, key,
                         b_min, min_days, min_months, min_years, min_hours, min_minutes, min_seconds,
                         b_max, max_days, max_months, max_years, max_hours, max_minutes, max_seconds,
                         b_mandatory, message, message_min, message_max, display)
{
  var   check_date;
  var   bAlert;
  var   iInput = new Array(6);
  var   i;
  var   year;

  if (! b_mandatory)
  {
    if (display == 2)
    {
      if ((i_hours == '') && (i_minutes == '') && (i_seconds == ''))
        return(true);
    }
    else
    {
      if ((i_days == '') && (i_months == '') && (i_years == '') && (i_hours == '') && (i_minutes == '') && (i_seconds == ''))
        return(true);
    }
  }

  bAlert = false;

  if ((! bAlert) && (! check_integer_value(i_days)))
    bAlert = true;
  if ((! bAlert) && (! check_integer_value(i_months)))
    bAlert = true;
  if ((! bAlert) && (! check_integer_value(i_years)))
    bAlert = true;
  if ((! bAlert) && (! check_integer_value(i_hours)))
    bAlert = true;
  if ((! bAlert) && (! check_integer_value(i_minutes)))
    bAlert = true;
  if ((! bAlert) && (! check_integer_value(i_seconds)))
    bAlert = true;

  if (bAlert)
  {
    set_error_mark( key, message );
    return false;
  }

  if (display == 3)
  {
    if (i_hours == '' && i_minutes == '' && i_seconds == '')
    {
      i_hours = 0;
      i_minutes = 0;
      i_seconds = 0;
    }
  }

  iInput[0] = parseInt(i_days, 10);
  iInput[1] = parseInt(i_months, 10);
  iInput[2] = parseInt(i_years, 10);
  iInput[3] = parseInt(i_hours, 10);
  iInput[4] = parseInt(i_minutes, 10);
  iInput[5] = parseInt(i_seconds, 10);

  for (i = 0; i < 6; i++)
  {
    if (isNaN(iInput[i]))
    {
      bAlert = true;
      break;
    }
  }

  if (bAlert)
  {
    set_error_mark( key, message );
    return false;
  }

  if (iInput[2] < 1900)
    bAlert = true;
  else if (iInput[2] > 3000)
    bAlert = true;

  if (bAlert)
  {
    set_error_mark( key, message );
    return false;
  }

  iInput[1] = iInput[1] - 1;
  check_date = new Date(iInput[2], iInput[1], iInput[0], iInput[3], iInput[4], iInput[5]);

  year = check_date.getYear();
  if (year < 1900)
    year = year + 1900;
  if (bAlert || (year != iInput[2]))
    bAlert = true;
  if (bAlert || (check_date.getMonth() != iInput[1]))
    bAlert = true;
  if (bAlert || (check_date.getDate() != iInput[0]))
    bAlert = true;

  if (bAlert || (check_date.getHours() != iInput[3]))
    bAlert = true;
  if (bAlert || (check_date.getMinutes() != iInput[4]))
    bAlert = true;
  if (bAlert || (check_date.getSeconds() != iInput[5]))
    bAlert = true;

  if (bAlert)
  {
    set_error_mark( key, message );
    return false;
  }

  if (b_min)
  {
    min_date = new Date(min_years, min_months - 1, min_days, min_hours, min_minutes, min_seconds);
    if (check_date < min_date)
    {
      set_error_mark( key, message_min );
      return false;
    }
  }

  if (b_max)
  {
    max_date = new Date(max_years, max_months - 1, max_days, max_hours, max_minutes, max_seconds);
    if (check_date > max_date)
    {
      set_error_mark( key, message_max );
      return false;
    }
  }

  return true;
}

function check_one_char(control, testchar, message)
{
  var   pos = 0;
  var   num = -1;
  var   i = -1;

  input = control['value'];
  key   = control['name'];

  while (pos != -1)
  {
    pos = input.indexOf(testchar, i + 1);
    num += 1;
    i = pos;
  }

  if (num > 1)
  {
    set_error_mark( key, message );
    return false;
  }

  return true;
}

function check_text_filter(control, filter, message)
{
  var   i, len, filter_len;
  var   bnInFilter;
  var   inputChar;
  var   pos;

  input = control['value'];
  key   = control['name'];

  len = input.length;
  if (len == 0)
    return true;

  filter_len = filter.length;

  for (i = 0; i < len; i++)
  {
    inputChar = input.substring(i, i + 1);
    pos = filter.indexOf(inputChar, 0);
    if (pos == -1)
    {
      set_error_mark( key, message );
      return false;
    }
  }

  return true;
}

function check_text_filter2(input, key, filter, message)
{
  var   i, len, filter_len;
  var   bnInFilter;
  var   inputChar;
  var   pos;

  len = input.length;
  if (len == 0)
    return true;

  filter_len = filter.length;

  for (i = 0; i < len; i++)
  {
    inputChar = input.substring(i, i + 1);
    pos = filter.indexOf(inputChar, 0);
    if (pos == -1)
    {
      set_error_mark( key, message );
      return false;
    }
  }

  return true;
}

function message_frame_write(type, message)
{
  var styleHeader;
  var styleFooter;

  if ( document.getElementById )
  {
    if (!parent.MessageFrame.document.getElementById("messagedivid"))
      return true;
  }
  else if (document.all)
  {
    if (!parent.MessageFrame.document.all("messagedivid") )
      return true;
  }
  else if (document.layers)
  {
    if (! parent.MessageFrame.document.layers["messagelayerid"])
      return true;
  }
  else
     return true;

  if( type == 0)
  {
    styleHeader = "<center><table><tr><td>";
    styleFooter = "</td></tr></table></center>";
  }
  else if( type == 1)
  {
    styleHeader = "<center><table class=message><tr><td><font class=message><b>";
    styleFooter = "</b></font></td></tr></table></center>";
  }
  else if( type == 2)
  {
    styleHeader = "<center><table class=error><tr><td><font class=error><b>";
    styleFooter = "</b></font></td></tr></table></center>";
  }
  if (document.getElementById)
  {
    control = parent.MessageFrame.document.getElementById("messagedivid");
    control.innerHTML = styleHeader.concat( message ).concat(styleFooter);
  }
  else if (document.all)
  {
    control = parent.MessageFrame.document.all("messagedivid");
    control.innerHTML = styleHeader.concat( message ).concat(styleFooter);

  }
  else if (document.layers)
  {
    control = parent.MessageFrame.document.layers["messagelayerid"];
    control.document.open();
    control.document.write(styleHeader.concat( message ).concat(styleFooter));

    control.document.close();
  }
}

function clear_error_marks ()
{
  var   aStr, aName;
  var   id;

  if (document.getElementById)
  {
    arrayofspans = document.getElementsByTagName("span");
    for (var i = 0; i < arrayofspans.length; i++)
    {
      id = arrayofspans[i].id;
      aStr = id.substring(0, 4);
      if (aStr == "SPAN")
        document.getElementById(id).innerHTML = '';
    }
    
    arrayotables = document.getElementsByTagName("table");
    for (var i = 0; i < arrayotables.length; i++)
    {
      id = arrayotables[i].id;
      aStr = id.substring(0, 5);
      if (aStr == "TABLE")
        document.getElementById(id).className = '';
    }
  }
  else if (document.all)
  {
    for (var i = 0; i < document.all.length; i++)
    {
      aName = document.all(i).id;
      aStr = aName.substring(0, 4);
      if (aStr == "SPAN")
        document.all(i).innerHTML = '';
      
      aStr = aName.substring(0, 5);
      if (aStr == "TABLE")
        document.all(i).className = '';
    }
  }
  else if (document.layers)
  {
    for (var i = 0; i < document.layers.length; i++)
    {
      aName = document.layers[i].id;
      aStr = aName.substring(0, 4);
      if (aStr == "SPAN")
        document.layers[i].innerHTML = '';
      
      aStr = aName.substring(0, 5);
      if (aStr == "TABLE")
        document.layers[i].className = '';
    }
  }
  return true;

}

function clear_error_mark ( id )
{
  var   aStr, aName, idTable, idSpan;

  id = id.replace(/_/ig,"");
  id = id.replace(":","");

  idTable = 'TABLE' + id;
  idSpan  = 'SPAN' + id;
  
  if (document.getElementById)
  {
    document.getElementById(idTable).className = '';
    document.getElementById(idSpan).innerHTML  = '';
  }
  else if (document.all)
  {
    document.all(idTable).className = '';
    document.all(idSpan).innerHTML  = '';
  }
  else if (document.layers)
  {
    document.layers[idTable].className = '';
    document.layers[idSpan].innerHTML  = '';
  }

  return true;
}

function clear_login_message ()
{
  var   aStr, aName;
  var   id;

  if (document.all)
  {
    for (var i = 0; i < document.all.length; i++)
    {
      aName = document.all(i).id;
      aStr = aName.substring(0, 9);
      if (aStr == "LOGINSPAN")
        document.all(i).style.visibility = 'hidden';
    }
  }
  else if (document.layers)
  {
    for (var i = 0; i < document.layers.length; i++)
    {
      aName = document.layers[i].id;
      aStr = aName.substring(0, 9);
      if (aStr == "LOGINSPAN")
        document.layers[i].visibility = 'hide';
    }
  }
  else if (document.getElementById)
  {
    arrayofspans = document.getElementsByTagName("span");
    for (var i = 0; i < arrayofspans.length; i++)
    {
      id = arrayofspans[i].id;
      aStr = id.substring(0, 9);
      if (aStr == "LOGINSPAN")
        document.getElementById(id).style.visibility = 'hidden';
    }
  }
  return true;

}

function set_error_mark( id, message )
{
  var   aStr, aName, idTable, idSpan;

  id = id.replace(/_/ig,"");
  id = id.replace(":","");

  idTable = 'TABLE' + id;
  idSpan  = 'SPAN' + id;
  
  if (document.getElementById)
  {
    document.getElementById(idTable).className = 'errortable';
    document.getElementById(idSpan).innerHTML  = '<font class="downerror">' + message + '</font>';
  }
  else if (document.all)
  {
    document.all(idTable).className = 'errortable';
    document.all(idSpan).innerHTML  = '<font class="downerror">' + message + '</font>';
  }
  else if (document.layers)
  {
    document.layers[idTable].className = 'errortable';
    document.layers[idSpan].innerHTML  = '<font class="downerror">' + message + '</font>';
  }
}

function show_java_error(message)
{
  alert(message);
//  message_frame_write(2, message);
}


function check_two_date_and_time_version2( d_t_before, d_t_after, dayB, monthB, yearB, hourB, minB, secB, dayA, monthA, yearA, hourA, minA, secA, message )
{
  var aux, key;
  
  dayB   = parseInt( dayB['value'], 10 );
  monthB = parseInt( monthB['value'], 10 );
  yearB  = parseInt( yearB['value'], 10 );
  hourB  = parseInt( hourB['value'], 10 );
  minB   = parseInt( minB ['value'], 10 );
  secB   = parseInt( secB['value'], 10 );

  dayA   = parseInt( dayA['value'], 10 );
  monthA = parseInt( monthA['value'], 10 );
  yearA  = parseInt( yearA['value'], 10 );
  hourA  = parseInt( hourA['value'], 10 );
  minA   = parseInt( minA['value'], 10 );
  secA   = parseInt( secA['value'], 10 );


  //Although this conversion isn't completly correct it does the trick
  dateBefore = ( yearB * 365 )  + ( monthB * 12 ) + dayB;
  timeBefore = ( hourB * 3600 ) + ( minB   * 60 ) + secB;
  dateAfter  = ( yearA * 365 )  + ( monthA * 12 ) + dayA;
  timeAfter  = ( hourA * 3600 ) + ( minA   * 60 ) + secA;

  if( dateBefore > dateAfter )
  {
    set_error_mark( d_t_before );
    set_error_mark( d_t_after );
    show_java_error( message );
    return false;
  }

  if( dateAfter > dateBefore )
  {
    return true;
  }
  else
  {
    if( timeBefore > timeAfter )
    {
      aux = d_t_before.substring( 0, 4 );
      if( ( aux == 'SPAN' ) && ( d_t_before.length > 4 ) )
      {
        key = d_t_before.substring( 5, d_t_before.length-4 );
      }
      else
      {
        key = d_t_before;
      }    
      set_error_mark( key, message );
      
      aux = d_t_after.substring( 0, 4 );
      if( ( aux == 'SPAN' ) && ( d_t_after.length > 4 ) )
      {
        key = d_t_after.substring( 5, d_t_after.length-4 );
      }
      else
      {
        key = d_t_after;
      }
      set_error_mark( key, message );
      return false;
    }
    else
    {
      return true;
    }
  }

  return true;
}


function check_two_date_and_time( dayB, monthB, yearB, hourB, minB, secB, dayA, monthA, yearA, hourA, minA, secA, message )
{
  keyD_T_Before = 'startedb';
  keyD_T_After  = 'startedA';
  
  D_T_Before  = "SPANstartedb";
  D_T_After   = "SPANstartedA";

  dayB   = parseInt( dayB['value'] );
  monthB = parseInt( monthB['value'] );
  yearB  = parseInt( yearB['value'] );
  hourB  = parseInt( hourB['value'] );
  minB   = parseInt( minB ['value'] );
  secB   = parseInt( secB['value'] );

  dayA   = parseInt( dayA['value'] );
  monthA = parseInt( monthA['value'] );
  yearA  = parseInt( yearA['value'] );
  hourA  = parseInt( hourA['value'] );
  minA   = parseInt( minA['value'] );
  secA   = parseInt( secA['value'] );


  //Although this conversion isn't completly correct it does the trick
  dateBefore = ( yearB * 365 )  + ( monthB * 12 ) + dayB;
  timeBefore = ( hourB * 3600 ) + ( minB   * 60 ) + secB;
  dateAfter  = ( yearA * 365 )  + ( monthA * 12 ) + dayA;
  timeAfter  = ( hourA * 3600 ) + ( minA   * 60 ) + secA;

  if( dateBefore > dateAfter )
  {
    set_error_mark( keyD_T_Before, message );
    set_error_mark( keyD_T_After, message );
    return false;
  }

  if( dateAfter > dateBefore )
  {
    return true;
  }
  else
  {
    if( timeBefore > timeAfter )
    {
      set_error_mark( keyD_T_Before, message );
      set_error_mark( keyD_T_After, message );
      return false;
    }
    else
    {
      return true;
    }
  }

  return true;
}

function db_cl_select_deselect_all(form_name, toggle_name)
{
  var   aStr, aName;
  var   checked;

  if (document.forms[form_name].elements[toggle_name].value == 0)
    checked = 1;
  else
    checked = 0;

  for (var i = 0; i < document.forms[form_name].elements.length; i++)
  {
    aName = document.forms[form_name].elements[i].name;
    aStr = aName.substring(0, 6);
    if (aStr == "DB_CL_")
      document.forms[form_name].elements[i].checked = checked;
  }

  document.forms[form_name].elements[toggle_name].value = 1 - document.forms[form_name].elements[toggle_name].value;

  return true;
}

function select_all(form_name, prefix)
{
  var   aStr, aName;
  var   checked;
  var   prefix_len;

  prefix_len = prefix.length;

  for (var i = 0; i < document.forms[form_name].elements.length; i++)
  {
    aName = document.forms[form_name].elements[i].name;
    aStr = aName.substring(0, prefix_len);
    if (aStr == prefix)
      document.forms[form_name].elements[i].checked = 1;
  }

  return true;
}

function deselect_all(form_name, prefix)
{
  var   aStr, aName;
  var   checked;
  var   prefix_len;

  prefix_len = prefix.length;

  for (var i = 0; i < document.forms[form_name].elements.length; i++)
  {
    aName = document.forms[form_name].elements[i].name;
    aStr = aName.substring(0, prefix_len);
    if (aStr == prefix)
      document.forms[form_name].elements[i].checked = 0;
  }

  return true;
}

function select_all_part(form_name, prefix_all, prefix_part)
{
  deselect_all(form_name, prefix_all);
  select_all(form_name, prefix_part);
}


function clear_control(control)
{
  control.value = '';
  return true;
}

function set_focus(control)
{
  control.focus();
  return true;
}

// used for 2 input fields !!
// if control_a <> "" -> control_b == "" -> message_a
// if control_a == "" -> control_b <> "" -> message_b
function check_ip_and_netmask(control_a_0, control_a_1, control_a_2, control_a_3, key_a, message_a,
                              control_b_0, control_b_1, control_b_2, control_b_3, key_b, message_b)
{
  input0  = control_a_0['value'];
  input1  = control_a_1['value'];
  input2  = control_a_2['value'];
  input3  = control_a_3['value'];

  control_a_empty = ((input0 == '') && (input1 == '') && (input2 == '') && (input3 == ''));

  input0  = control_b_0['value'];
  input1  = control_b_1['value'];
  input2  = control_b_2['value'];
  input3  = control_b_3['value'];

  control_b_empty = ((input0 == '') && (input1 == '') && (input2 == '') && (input3 == ''));

  if (control_a_empty && (! control_b_empty))
  {
    set_error_mark( key_a, message_a );
    return false;
  }

  if ((! control_a_empty) && control_b_empty)
  {
    set_error_mark( key_b, message_b );
    return false;
  }
  return true;
}

function check_VCSS_value_field(control_combo, control_value, orig_dest_label, value_label, orig_dest_text1, orig_dest_text2, orig_dest_text3, orig_dest_text4, msg1, msg2, msg3, msg4)
{
  combo_value = control_combo['value'];
  value = control_value['value'];
  key   = control_value['name'];

  if (combo_value == 1)
    yyy = orig_dest_text1;
  else if (combo_value == 2)
    yyy = orig_dest_text2;
  else if (combo_value == 3)
    yyy = orig_dest_text3;
  else if (combo_value == 4)
    yyy = orig_dest_text4;

  if ((combo_value == 1) || (combo_value == 3))
  {
    len = value.length;

    // length check
    if (len > 32)
    {
      msg1 = msg1.replace("XXX", orig_dest_label);
      msg1 = msg1.replace("YYY", yyy);
      msg1 = msg1.replace("ZZZ", value_label);
      set_error_mark( key, msg1 );
      return false;
    }

    // filter check
    if (len > 0)
    {
      filter = "0123456789*";
      filter_len = filter.length;
      for (i = 0; i < len; i++)
      {
        inputChar = value.substring(i, i + 1);
        pos = filter.indexOf(inputChar, 0);
        if (pos == -1)
        {
          msg2 = msg2.replace("XXX", orig_dest_label);
          msg2 = msg2.replace("YYY", yyy);
          msg2 = msg2.replace("ZZZ", value_label);
          set_error_mark( key, msg2 );
          return false;
        }
      }
    }

    // test 1 *
    pos = 0;
    num = -1;
    i = -1;

    while (pos != -1)
    {
      pos = value.indexOf('*', i + 1);

      if ((pos != -1) && (pos != (len - 1)))
      {
        msg3 = msg3.replace("XXX", orig_dest_label);
        msg3 = msg3.replace("YYY", yyy);
        msg3 = msg3.replace("ZZZ", value_label);
        set_error_mark( key, msg3 );
        return false;
      }
      i = pos;
    }
  }
  else
  {
    len = value.length;

    // test 1 *
    pos = 0;
    num = -1;
    i = -1;

    while (pos != -1)
    {
      pos = value.indexOf('*', i + 1);

      if ((pos != -1) && (pos != 0))
      {
        msg4 = msg4.replace("XXX", orig_dest_label);
        msg4 = msg4.replace("YYY", yyy);
        msg4 = msg4.replace("ZZZ", value_label);
        set_error_mark( key, msg4 );
        return false;
      }
      i = pos;
    }
  }
  return true;
}

function check_VCSS_E164_value_field1(control_value1, control_value2, msg1, msg2, msg3)
{
  value1 = control_value1['value'];
  key1   = control_value1['name'];
  value2 = control_value2['value'];

  len = value1.length;
  value2Empty = (value2.length == 0);

  // filter check
  if (len > 0)
  {
    if (value2Empty)
      filter = "0123456789*";
    else
      filter = "0123456789";
    filter_len = filter.length;
    for (i = 0; i < len; i++)
    {
      inputChar = value1.substring(i, i + 1);
      pos = filter.indexOf(inputChar, 0);
      if (pos == -1)
      {
        if (value2Empty)
          set_error_mark( key1, msg1 );
        else
          set_error_mark( key1, msg2 );
        return false;
      }
    }
  }

  // test 1 *
  pos = 0;
  num = -1;
  i = -1;

  while (pos != -1)
  {
    pos = value1.indexOf('*', i + 1);

    if ((pos != -1) && (pos != (len - 1)))
    {
      set_error_mark( key, msg3 );
      return false;
    }
    i = pos;
  }

  return true;
}

function check_VCSS_E164_value_field2(control_value2, msg)
{
  value2 = control_value2['value'];
  key2   = control_value2['name'];

  len = value2.length;

  // filter check
  if (len > 0)
  {
    filter = "0123456789";
    filter_len = filter.length;
    for (i = 0; i < len; i++)
    {
      inputChar = value2.substring(i, i + 1);
      pos = filter.indexOf(inputChar, 0);
      if (pos == -1)
      {
        set_error_mark( key, msg );
        return false;
      }
    }
  }

  // test 1 *
  pos = 0;
  num = -1;
  i = -1;

  while (pos != -1)
  {
    pos = value2.indexOf('*', i + 1);

    if ((pos != -1) && (pos != (len - 1)))
    {
      set_error_mark( key, msg );
      return false;
    }
    i = pos;
  }
  return true;
}

function check_VCSS_username_value_field(control_value2, msg)
{
  value = control_value2['value'];
  key   = control_value2['name'];

  len = value.length;

  // test 1 *
  pos = 0;
  num = -1;
  i = -1;

  while (pos != -1)
  {
    pos = value.indexOf('*', i + 1);

    if ((pos != -1) && (pos != 0))
    {
      set_error_mark( key, msg );
      return false;
    }
    i = pos;
  }
  return true;
}


function reset_combos()
{
  var box = document.forms[0].elements;
  for (var i=0;i<box.length;i++)
  {
    var name = box[i].name;
    if (name.substring(0,5) == "combo")
      box[i].selectedIndex = '-1';
  }
}


function password_power( id )
{
  //One to Four are status for the password.
  var value = document.getElementById( id ).value;
  var num   = document.getElementById( id ).value.length;
  var power = password_power_calculator( value, num );
  
  return power;
}


function password_power_calculator( val, num )
{
  var res;
  var special = 0;
  
  //Check how many special characters
  res = val.match( /[\!\@\#\$\%\^\&\*\(\)\+\=\-\[\]\\\'\;\,\.\/\{\}\|\"\:\<\>\?]/g );

  if( res == null )
    res = new Array(0);

  special = res.length * 3;

  //Check how many characters are letters.
  res = val.match( /[a-zA-Z]/g );

  if( res == null )
    res = new Array(0);

  if( special > 0 && res.length > 0 ) {
    special += res.length + 4;
  } else {
    special += res.length;
  }

  //Check if all characters are numerics.
  res = val.match( /[0-9]/g );
  
  if( res == null )
    res = new Array(0);
    
  if( special > 0 && res.length > 0 ) {
    special += res.length + 4;    
  } else {
    special += res.length;
  }

  return special;
}

function getCookie(NameOfCookie)
{
  // First we check to see if there is a cookie stored.
  // Otherwise the length of document.cookie would be zero.
  if (document.cookie.length > 0)
  {
  
  // Second we check to see if the cookie's name is stored in the
  // "document.cookie" object for the page.
  
  // Since more than one cookie can be set on a
  // single page it is possible that our cookie
  // is not present, even though the "document.cookie" object
  // is not just an empty text.
  // If our cookie name is not present the value -1 is stored
  // in the variable called "begin".
  
  begin = document.cookie.indexOf(NameOfCookie+"=");
  if (begin != -1) // Note: != means "is not equal to"
  {
  
  // Our cookie was set.
  // The value stored in the cookie is returned from the function.
  
  begin += NameOfCookie.length+1;
  end = document.cookie.indexOf(";", begin);
  if (end == -1) end = document.cookie.length;
  return unescape(document.cookie.substring(begin, end)); }
  }
  return null;
  
  // Our cookie was not set.
  // The value "null" is returned from the function.

}

function delCookie (NameOfCookie)
{
  // The function simply checks to see if the cookie is set.
  // If so, the expiration date is set to Jan. 1st 1970.

  if (getCookie(NameOfCookie)) {
    document.cookie = NameOfCookie + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


function check_characters( str, characters )
{
  var array = characters.split(',');
  for( i=0; i<array.length; i++ )
    if( str == array[i] )
      return true;
  return false;
}


function check_fixed_phone( control, mandatory, msg, mand_msg, country )
{
  var result = true;
  number = document.getElementsById( control )[0].value;
  
  if( mandatory && number.length == 0 )
  {
    return mand_msg;
  }
  
  switch( country )
  {
    case 'spain':
      //Check for length (9 digits, or 11 with 34 prefix).
      if( number.length != 9 && number.length !=11 )
        return msg;
        
      //Check if is a number.
      if( !check_integer_value( number ) )
        return msg;  
        
      //Check initial character.
      if( number.length == 9 && ( number.charAt(0) != '9' ) )
        return msg;  
      if( number.length == 11 && !check_characters( number.substring(0,3), '349' ) )
        return msg;  
    break;
  }
  
  return "";
}

function check_mobile_phone( control, mandatory, msg, mand_msg, country )
{
  var result = true;
  number = document.getElementsByName( control )[0].value;

  if( mandatory && number.length == 0 )
  {
    return mand_msg;
  }
  switch( country )
  {
    case 'spain':
    {
      //Check for length (9 digits, or 11 with 34 prefix).
      if( number.length != 9 && number.length !=11 )
        return msg;

      //Check if is a number.
      if( !check_integer_value( number ) )
        return msg;  
       
      //Check initial character.
      if( number.length == 9 && ( number.charAt(0) != '6' ) )
        return msg;  
      if( number.length == 11 && !check_characters( number.substring(0,3), '346' ) )
        return msg;  
      break;
    }   
    case 'peru':
    {    
      if( number.length != 9 && number.length !=8 && number.length != 11 )
        return msg;

      //Check if is a number.
      if( !check_integer_value( number ) )
        return msg;  
       
      //Check initial character.
      if( number.length == 8 && ( number.charAt(0) != '9' ) )
        return msg;  
      if( number.length == 9 )
        return msg;  
      if( number.length == 11 && !check_characters( number.substring(0,2), '51' ) )
        return msg;  
        
        
      break;
    }
  }
  
  return "";
}

function check_phone_number( type, control, mandatory, msg, mand_msg, country )
{ 
  //Differences between fixed numbers and mobile numbers depend on country. This is the reason for separated functions.
  var result;

  switch( type )
  {
    case 1:
      result = check_mobile_phone( control, mandatory, msg, mand_msg, country );
      break;
      
    case 2:
      result = check_fixed_phone( control, mandatory, msg, mand_msg, country );
      break;
      
    default:
      var res_mob = check_mobile_phone( control, mandatory, msg, mand_msg, country );
      var res_fix = check_fixed_phone( control, mandatory, msg, mand_msg, country );

      if( res_mob == "" || res_fix == "" )
        result = "";
      else
        result = res_mob; //For example, both of them are equal.
      break;
  }
  
  if( result == "")
    return true;
  else
  {
    set_error_mark( control, result );
    return false;
  }
}

function change_tristate_selection( id )
{
  var chk = document.getElementById( id );
  var hid = document.getElementById( 'CHK_HID_' + id );
  var val = hid.value;
  
  switch( val )
  {
    case '0':
      chk.src   = '../images/checked.gif';
      hid.value = '1';
      break;
      
    case '1':
      chk.src   = '../images/unchecked.gif';
      hid.value = '0';
      break;
      
    case '2':
      chk.src   = '../images/checked.gif';
      hid.value = '1';
      break;
  }
}