function anyCheck(form, max) {
  var total  = 0;
  var prefix = 'vid';
  for(i = 0; i < max; i++) {
    if(form[prefix + i].checked == true) total++;
  }
  var cnt = document.getElementById('cnt');
  cnt.innerText = total;
}

function format(str) {
  index = str.indexOf(".");
  if(index < 0) {
    str = str + ".00";
  } else {
    str = str.substring(0, index + 3);
    if(str.length < (index + 3)) str += "0";
  }
  return str;
}

function makeParent() {
  if(window != top) top.location.href = location.href;
}

function numberFilter(input) {
  s = input.value + '';
  filteredValues = "1234567890.";
  var i;
  var returnString = "";
  for (i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if (filteredValues.indexOf(c) != -1) returnString += c;
  }
  input.value = returnString;
}

function openbasepopup(popurl, width, height) {
  var args = "width=" + width + ",height=" + height;
  var winpops;
  winpops=window.open(popurl,"",args);
}

function openextpopup(popurl, width, height, name) {
  var args = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=" + width + ",height=" + height + ",left=0,top=0";
  var winpops;
  winpops=window.open(popurl,name,args);
}

function openscrollpopup(popurl, width, height, name) {
  var args = "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=" + width + ",height=" + height + ",left=0,top=0";
  var winpops;
  winpops=window.open(popurl,name,args);
}

function openwindow(popurl, name) {
  var args = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,left=0,top=0";
  var winpops;
  winpops=window.open(popurl,name,args);
}

function profile(speler) {
  var src = "/profile.php?showprofile=" + speler;
  var profpop;
  if(speler) profpop=window.open(src,'profiel','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=800,height=700,left=0,top=0');
}

function putmakeup(txt) {
  window.opener.document.newmessage.bericht.value = window.opener.document.newmessage.bericht.value + txt;
  window.opener.document.newmessage.bericht.focus();
  window.close();
}

function refreshAll() {
  top.location.href = 'index.php';
}

function refreshMenu() {
  //index3
  if(top.right && typeof top.right.update == 'function') top.right.update();
  //new interface
  if(top.menu && typeof top.menu.update == 'function') top.menu.update();
  if(top.main && top.main.menu && typeof top.main.menu.topmenu.reload == 'function') top.main.menu.topmenu.reload();
}

function refreshSaldo(newcash, newbank) {
  //new interface
  if(top.menu && typeof top.menu.update == 'function') top.menu.update();
  if(top.main) top.main.menu.topmenu.document.getElementById('cash').innerHTML = format(newcash.toString()) + " ISH";
  if(top.main) top.main.menu.topmenu.document.getElementById('bank').innerHTML = format(newbank.toString()) + " ISH";
}

function refreshMenuAndShops() {
  refreshMenu();
  top.main.text.location.href = 'street.php';
}

function refreshRightMenu() {
  if(top.right && top.main.menu) top.right.location.href = 'right.php'
}

function refreshTopMenu() {
  top.upper.location.href = 'top.php';
}

function setBankSaldo() {
  var bet;
  if(bankfrom == bankto + 0.01 || bankfrom == bankto - 0.01) bankfrom = bankto;
  bet = Math.round((bankfrom + ((bankto - bankfrom) / 2)) * 100, 2) / 100;
  bankfrom = bet;
  dyn = document.getElementById('bank');
  dyn.innerText = format(bet.toString()) + ' ISH';
  if(bankfrom != bankto) window.setTimeout('setBankSaldo()', 100);
}

function setCashSaldo() {
  var bet;
  if(cashfrom == cashto + 0.01 || cashfrom == cashto - 0.01) cashfrom = cashto;
  bet = Math.round((cashfrom + ((cashto - cashfrom) / 2)) * 100, 2) / 100;
  cashfrom = bet;
  dyn = document.getElementById('cash');
  dyn.innerText = format(bet.toString()) + ' ISH';
  if(cashfrom != cashto) window.setTimeout('setCashSaldo()', 100);
}

function setFocus(field) {
  field.focus();
}

function setRightMenuName() {
  window.name='right';
}

function supdatePos() {
  window.setTimeout("updatePos();", 5000);
}

function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit) field.value = field.value.substring(0, maxlimit);
  else countfield.value = maxlimit - field.value.length;
}

function textFilter (input) {
  s = input.value;
  filteredValues = "\"\'\\<=>$&+";
  var i;
  var returnString = "";
  for (i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if (filteredValues.indexOf(c) == -1) returnString += c;
  }
  input.value = returnString;
}

function updatePos() {
  if(top.main.menu) {
    top.main.menu.midmenu.location.href = 'midmenu.php'
  }
}

function mapopen(mapstring) {
  var mappop;
  mappop=window.open('/map.php?' + mapstring,'map','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=556,height=596,left=0,top=0');
}

function landinfo(xpos, ypos) {
  var landinfo;
  landinfo=window.open('landinfo.php?xpos=' + xpos + '&ypos=' + ypos,'landinfo','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=500,left=0,top=0');
  document.location.href='midmenu.php';
}

function clrel() {
  window.opener.location.reload();
  close();
}

function createRequestObject() {
  var ro;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer") {
    ro = new ActiveXObject("Microsoft.XMLHTTP");
  } else ro = new XMLHttpRequest();
  return ro;
}

var bro = createRequestObject();

function handleBuildingObject() {
  if(bro.readyState == 4) {
    var response = bro.responseText;
    document.getElementById('b_preview').innerHTML = response;
  }
}

function updBuPr(type, name, altname, b_col, d_col) {
  var url = '/ajax/building.php?type=' + encodeURIComponent(type) + '&name=' + encodeURIComponent(name) + '&altname=' + encodeURIComponent(altname) + '&b_col=' + encodeURIComponent(b_col) + '&d_col=' + encodeURIComponent(d_col);
  bro = createRequestObject();
  bro.open('get', url);
  bro.onreadystatechange = handleBuildingObject;
  bro.send(null);
}
