var formClicked = false;

function zoekType(type) {
  document.forms['zoeken'].type.value = type;
}
function refreshObjects(refList, url, type) {
  var qs = new Querystring()
  var value = refList.options[refList.selectedIndex].value;

  if (refList.selectedIndex > 0) {
    if (type == 'soortobject') {
      url += '?filtersoort=' + value;
      if (qs.get("filterprovincie")) {
        if (qs.get("filterprovincie").length > 0)
          url += '&filterprovincie=' + qs.get("filterprovincie");
      }
    }
    if (type == 'provincie') {
      url += '?filterprovincie=' + value;
      if (qs.get("filtersoort")) {
        if (qs.get("filtersoort").length > 0)
          url += '&filtersoort=' + qs.get("filtersoort");
      }
    }
    document.location.replace(url);
  } else { // alle soorten / provincies tonen
    if (type == 'provincie') {
      if (qs.get("filtersoort"))
        url += '?filtersoort=' + qs.get("filtersoort");
    }
    if (type == 'soortobject') {
      if (qs.get("filterprovincie"))
        url += '?filterprovincie=' + qs.get("filterprovincie");
    }
    document.location.replace(url);
  }
}

function formCheck(ref) {
  var valid = true;
  var msg = "De volgende verplichte velden zijn leeggelaten:\n";

  if (formClicked) return false;

  if (ref.naam) {
    if (ref.naam.value == '') {
      msg += " - Naam\n";
      valid = false;
    }
  }
  if (ref.straat_huisnummer) {
    if (ref.straat_huisnummer.value == '') {
      msg += " - Straat & huisnummer\n";
      valid = false;
    }
  }
  if (ref.postcode) {
    if (ref.postcode.value == '') {
      msg += " - Postcode\n";
      valid = false;
    }
  }
  if (ref.plaats) {
    if (ref.plaats.value == '') {
      msg += " - Plaats\n";
      valid = false;
    }
  }
  if (ref.telefoonnr) {
    if (ref.telefoonnr.value == '') {
      msg += " - Telefoonnummer\n";
      valid = false;
    }
  }
  if (ref.email) {
    if (ref.email.value == '') {
      msg += " - Email\n";
      valid = false;
    }
  }

  if (valid) {
    formClicked = true;
  }
  else {
    alert(msg);
    return false;
  }
  return true;
}

function setType(type) {
  if (document.getElementById('sel-dummy')) {
    if (type == '') {
      document.getElementById('sel-dummy').style.display = 'block';
      document.getElementById('sel-koop').style.display = 'none';
      document.getElementById('sel-huur').style.display = 'none';
    }
    else {
      if (type == 'koop') {
        document.getElementById('sel-dummy').style.display = 'none';
        document.getElementById('sel-koop').style.display = 'block';
        document.getElementById('sel-huur').style.display = 'none';
      }
      if (type == 'huur') {
        document.getElementById('sel-dummy').style.display = 'none';
        document.getElementById('sel-koop').style.display = 'none';
        document.getElementById('sel-huur').style.display = 'block';
      }
    }
  }
}

/* Client-side access to querystring name=value pairs
Version 1.2.3
22 Jun 2005
Adam Vandenberg
*/
function Querystring(qs) { // optionally pass a querystring to parse
  this.params = new Object()
  this.get = Querystring_get

  if (qs == null)
    qs = location.search.substring(1, location.search.length)

  if (qs.length == 0) return

  // Turn <plus> back to <space>
  // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
  qs = qs.replace(/\+/g, ' ')
  var args = qs.split('&') // parse out name/value pairs separated via &

  // split out each name=value pair
  for (var i = 0; i < args.length; i++) {
    var value;
    var pair = args[i].split('=')
    var name = unescape(pair[0])

    if (pair.length == 2)
      value = unescape(pair[1])
    else
      value = name

    this.params[name] = value
  }
}

function Querystring_get(key, default_) {
  // This silly looking line changes UNDEFINED to NULL
  if (default_ == null) default_ = null;

  var value = this.params[key]
  if (value == null) value = default_;

  return value
}

function flushContactFormData() {
  $('#contact-container table input[type="text"],#contact-container table select').each(function () {
    $.cookies.del($(this).attr('name'));
  });
  $('#contact-container table input[type="checkbox"],#contact-container table input[type="radio"]').each(function () {
    $.cookies.del($(this).attr('name'));
  });
}

function storeContactFormData() {
  $('#contact-container table input[type="text"],#contact-container table select').each(function () {
    $.cookies.set($(this).attr('name'), $(this).val());
  });
  $('#contact-container table input[type="checkbox"],#contact-container table input[type="radio"]').each(function () {
    $.cookies.set($(this).attr('id'), this.checked);
  });
}

function loadContactFormData() {
  $('#contact-container table input[type="text"],#contact-container table select').each(function () {
    $(this).val($.cookies.get($(this).attr('name')));
  });
  $('#contact-container table input[type="checkbox"],#contact-container table input[type="radio"]').each(function () {
    this.checked = $.cookies.get($(this).attr('id'));
  });
}
