// Send an imported user a confirmation e-mail
function confirmSingleUser(id) {
  if(confirmAction())
    popup("switch.php?single_confirm=true&id="+id);
}

// Mark everyone as having payed for their membership (or not)
function switchAllPay(state) {
  if(confirm("Let op! Deze actie heeft gevolgen voor alle gebruikers en kan niet ongedaan worden gemaakt!"))
    popup("switch.php?pay="+((state)?"all":"none"));
}

// Mark everyone as having received their sticker (or not)
function switchAllSticker(state) {
  if(confirm("Let op! Deze actie heeft gevolgen voor alle gebruikers en kan niet ongedaan worden gemaakt!"))
    popup("switch.php?sticker="+((state)?"all":"none"));
}

// Send comment text via GET to bypass nested form-tags
function submitComment(page,subject,table) {
  var msg_obj = document.getElementById("new_comment");
  msg = msg_obj.value;
  msg_obj.value = "";
  if(msg!=="")
    location.href = "commentaar.php?page="+page+"&subject="+subject+"&table="+table+"&msg="+msg;
  else
    alert("Uw berichttekst is te kort!");
}

// Delete an image from a fotoalbum
function deleteImage(id,page,album) {
  if(confirmAction())
    location.href = "?p="+page+"&action=delete_image&image="+id+"&id="+album;
}

// Delete a comment
function deleteComment(id,table) {
  if(confirmAction())
    location.href = "commentaar.php?action=delete&id="+id+"&table="+table;
}

// Send guestbook entry text via GET to bypass nested form-tags
function submitGuestbookComment() {
  var msg_obj = document.getElementById("new_comment");
  msg = msg_obj.value;
  msg_obj.value = "";
  var name = document.getElementById("new_name").value;
  if(msg!==""&&name!=""&&name!=="gastgebruiker")
    location.href = "commentaar.php?guestbook=true&name="+name+"&msg="+msg;
  else if(name=="gastgebruiker"||name=="")
    alert("Vergeet niet uw naam in te vullen!");
  else
    alert("Uw berichttekst is te kort!");
}

// Return the size of the page
function pageSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth,myHeight];
}

// See if there are new uploaded photos, and display.
var updated_image_count = 0;
function showNewPhotos(id,count,type,container) {
  var old = count + updated_image_count;
  ajax_add("ajax.php","?id="+id+"&show_new="+old+"&type="+type,container);
}

// Creates a simple pop-up
function popup(url) {
  var email = window.open(url,"email","width=400,height=300,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1");
  email.moveTo(100,100);
  email.focus();
}

// Use an array to check all input fields
function validateAll(validation) {
  for(var i = 0; i < validation.length; i++)
    validation[i][2] = validate(document.getElementById(validation[i][0]),validation[i][1]);
  return validation;
}

// Check all inputs and return true if all is well
function submitValidateAll(validation) {
  validation = validateAll(validation);
  var wrong = new Array();
  for(var i = 0; i < validation.length; i++)
    if(!validation[i][2])
      wrong[wrong.length] = validation[i][0];
  if(wrong.length==0)
    return true;
  alert("Nog niet alle gegevens zijn voldoende ingevuld. "
      + "De volgende eigenschappen voldoen niet aan de eisen:\n\n"
      + wrong.join(", ")
      + "\n\nCorrigeer de invoer en probeer opnieuw. Neem contact op met het bestuur als het probleem zich blijft voordoen."
    );
  document.getElementById(wrong[0]).focus();
  document.getElementById(wrong[0]).select();
  return false;
}

// Validates some object's value using a regex
function validate(obj,regex) {
  if(obj.value.match(new RegExp("^"+regex+"$"))) {
    obj.style.background = "url('image/goed.png') no-repeat 99% 50% #ffffff";
    return true;
  }
  else if(obj.value=="")
    obj.style.background = "url('image/leeg.png') no-repeat 99% 50% #ffffff";
  else
    obj.style.background = "url('image/fout.png') no-repeat 99% 50% #ffffff";
  return false;
}

// The second password also has to be equal to the first
function passcheck(obj,other,regex) {
  if(obj.value==document.getElementById(other).value)
    validate(obj,regex);
  else if(obj.value=="")
    obj.style.background = "url('image/leeg.png') no-repeat 99% 50% #ffffff";
  else
    obj.style.background = "url('image/fout.png') no-repeat 99% 50% #ffffff";
  return false;
}

// Show input fields in stead of button
function changePassword() {
  document.getElementById("pw0").style.display = "none";
  document.getElementById("pw1").style.display = "table-row";
  document.getElementById("pw2").style.display = "table-row";
  document.getElementById("pw3").style.display = "table-row";
}

// Set a hidden value for checkbox posting
function setCheckbox(obj,target) {
  document.getElementById(target).value = (obj.checked)?"1":"0";
}

// Default confirmation dialog.
function confirmAction() {
  return confirm("Let op! Deze actie kan niet ongedaan gemaakt worden.");
}

// Returns a new list with item omitted
function remove(item,list) {
  var result = new Array();
  for(var i = 0; i < list.length; i++) {
    var cur = list[i];
    if(cur!=item)
      result.push(cur);
  }
  return result;
}

// Standard ajax function to append results to a container
function ajax_add(page,param,container) {
  var xhr;
  try { xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
  catch (e) {
    try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
    catch (e2) {
      try { xhr = new XMLHttpRequest(); }
      catch (e3) { xhr = false; }
    }
  }

  xhr.onreadystatechange = function() {
    if(xhr.readyState == 4) {
      if(xhr.status == 200) {
        var result = xhr.responseText;
        if(result!="false")
          document.getElementById(container).innerHTML += result;
      }
      else
        document.getElementById(container).innerHTML = "Error code " + xhr.status;
    }
  };

  xhr.open("GET", page + param,  true);
  xhr.send(null);
}

// Find user for the admin page.
function leden_ajax() {
  var wel_betaald = document.getElementById("wel_betaald").checked;
  var niet_betaald = document.getElementById("niet_betaald").checked;
  var wel_opgezegd = document.getElementById("wel_opgezegd").checked;
  var niet_opgezegd = document.getElementById("niet_opgezegd").checked;
  var betaald = wel_betaald * 3 + niet_betaald * 4;
  var opgezegd = wel_opgezegd * 3 + niet_opgezegd * 4;
  var search = document.getElementById("leden_ajax_search").value;
  ajax_container("?leden=true&search=" + search + "&betaald=" + betaald + "&opgezegd=" + opgezegd,"leden_ajax_result");
}

// Standard ajax function to write results into a container
function ajax_container(param,container) {
  var xhr;
  try { xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
  catch (e) {
    try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
    catch (e2) {
      try { xhr = new XMLHttpRequest(); }
      catch (e3) { xhr = false; }
    }
  }

  xhr.onreadystatechange = function() {
    if(xhr.readyState == 4) {
      if(xhr.status == 200)
        document.getElementById(container).innerHTML = xhr.responseText;
      else
        document.getElementById(container).innerHTML = "Error code " + xhr.status;
    }
  };

  xhr.open("GET", "ajax.php" + param,  true);
  xhr.send(null);
}

// Fisher-Yates
function array_shuffle(myArray) {
  var i = myArray.length;
  if(i==0)
    return false;
  while(--i) {
    var j = Math.floor(Math.random()*(i+1));
    var temp = myArray[i];
    myArray[i] = myArray[j];
    myArray[j] = temp;
  }
  return myArray;
}

function toggleAgendaInschrijvingOptions(step, status) {
  var show = document.getElementById("inschr_true").checked;
  var obj = document.getElementById("inschr_options");
  if(step == 0) {
    if(show) {
      obj.style.display = "block";
      resize(obj.id, [350, 0], [350, 80], 200, "toggleAgendaInschrijvingOptions(1,1)");
    } else {
      alpha(obj.id, 1, 0, 200, "toggleAgendaInschrijvingOptions(1,0)");
    }
  } else {
    if(status) {
      alpha(obj.id, 0, 1, 200);
    } else {
      resize(obj.id, [350, 80], [350, 0], 200, "document.getElementById('inschr_options').style.display='none';");
    }
  }
}
