function menuOn (obj) {
  obj.style.backgroundColor = "#00AA00";
}

function menuOff (obj) {
  obj.style.backgroundColor = '';
}

function doSection (secNum, img){
  //display the section if it's not displayed; hide it if it is displayed
  if (secNum.style.display=="none") {
    secNum.style.display="";
    img.src = '/img/tve.gif';
  } else {
    secNum.style.display="none";
    img.src = '/img/tvc.gif';
  }
}

var inline = false;	// used to switch to phone directory 'email' column visualization

function PhoneTh () {
  if (inline)
    document.writeln ('<tr><th>Стая</th><th width="40%">Длъжност</th><th width="40%">Име и фамилия</th><th>Тел.</th><th>Вътр.</th></tr>');
  else
    document.writeln ('<tr><th>Стая</th><th width="40%">Длъжност</th><th width="40%">Име и фамилия</th><th>E-mail</th><th>Тел.</th><th>Вътр.</th></tr>');
}

function PhoneTd (position, name, phone, ext, office, email) {
  if (inline) {
    document.writeln ('<tr><td align="center">' + office + '</td><td>' + position + '</td><td><a href="mailto:' + email + '@shumen.bg">' + name + '</a></td><td nowrap>' + phone + '</td><td align="center">' + ext + '</td></tr>');
  } else {
    email = (email ? '<a href="mailto:' + email + '@shumen.bg">' + email + '@shumen.bg</a>' : '');
    document.writeln ('<tr><td align="center">' + office + '</td><td>' + position + '</td><td>' + name + '</td><td>' + email + '</td><td nowrap>' + phone + '</td><td align="center">' + ext + '</td></tr>');
  }
}

function PhoneRow (position, name, phone, ext, office, email) {
  if (position == 'th')
    PhoneTh ();
  else if (position == 'dept')
    document.writeln ('<tr><td colspan=5><b>' + name + '</b></td></tr>');
  else
    PhoneTd (position, name, phone, ext, office, email);
}

function chkValue (obj, msg) {
  if (obj.value == '') {
    alert (msg);
    obj.focus ();
    return false;
  }
  return true;
}

function stripTrailingNonDigits (code) {
  while (1) {
    c = code.substr (code.length-1, 1);
    if (c>='0' && c<='9') return code;
    code = code.substr (0, code.length-1);
  }
}

function InvalidBulstat (code) {
  var sum=0, mod9, mod13, n, c;
  // Formal check (the length should be 9 or 13 digits after strip out trailing non-digit symbols
  code = stripTrailingNonDigits (code);
  if (!(code.length==9 || code.length==13)) return 1;
  if (code.length < 13) code = code.substr (0, 9);
  for (var i=0; i<8; i++) sum += (1 + i) * code.substr (i, 1);
  mod9 = sum % 11;
  if (mod9 == 10) {
    sum = 0;
    for (i=0; i<8; i++) sum += (3 + i) * code.substr (i, 1);
    mod9 = sum % 11;
  }
  n = parseInt (code.substr (8, 1));
  if (mod9 != n) return 9;
  if (code.length == 13) {
    sum = (2 * mod9) + (7 * code.substr(9, 1)) + (3 * code.substr(10,1)) + (5 * code.substr(11,1));
    mod13 = sum % 11;
    if (mod13 == 10) {
      sum = (4 * mod9) + (9 * code.substr(9, 1)) + (5 * code.substr(10,1)) + (7 * code.substr(11,1));
      mod13 = sum % 11;
      if (mod13 == 10) mod13 = 0;
    }
    n = parseInt (code.substr (12, 1));
    if (mod13 != n) return 13;
  }
  return 0;
}

function isValidBulstat (code) { return (InvalidBulstat(code)==0?true:false); }

function isValidTaxid (num) {
  if (num.length != 10)
    return (false);
  // Foreigners treatment
  if (parseInt (num.substr (0, 4)) == 1000)
    return (true);
  m = new Array (4, 3, 2, 7, 6, 5, 4, 3, 2);
  s = 0;
  for (i=0; i<9; i++)
    s += parseInt (num.substr (i, 1)) * m[i];
  c = parseInt (num.substr (9, 1));
  x = s % 11;
  e = x + c;
  if (e == 11)
    return (true);
  if (e == 0 && c == 0)
    return (true);
  return (false);
}

function isValidEgn (num) {
  // Foreigners treatment
  var s = num.toString ();
  if (s.substring (0, 4) == '1000') return (true);
  // Main logic
  if (num.length != 10) return (false);
  m = new Array (2, 4, 8, 5, 10, 9, 7, 3, 6);
  s = 0;
  for (i=0; i<9; i++)
    s += parseInt (num.substr(i, 1)) * m[i];
  x = s % 11;
  if (x == 10)
    x = 0;
  if (x == parseInt (num.substr (9, 1)))
    return (true);
  else
    return (false);
}

function qs (arg) {
  var url = this.location.toString ();
  var i = url.indexOf ('?');
  if (i == -1) return (null);
  url = url.substring (i + 1);
  i = url.indexOf (arg + '=');
  if (i == -1) return (null);
  url = url.substring (i + arg.length + 1);
  i = url.indexOf ('&');
  if (i == -1) return (url);
  url = url.substring (0, i);
  return (url);
}

function switchPage (url, id) {
  document.location = url + '?id='+id;
}

function selectPage (url, aPage) {
  id = qs ('id');
  document.write ('<form>');
  document.write ('<select onChange=switchPage("'+url+'",this.value)>');
  for (i=0; i<aPage.length; i++)
    document.write ('<option value='+i+(id==i ? ' selected':'')+'>'+aPage[i]+'</option>');
  document.write ('</select>');
  document.write ('</form>');
}
