function sendForm(which) {
  $(which).request();
}

function togglePerspective(which) {
  new Ajax.Updater('projects_residents','wp-content/themes/blauwehuis/includes/projects_residents_processor.php',{
    method: 'post',
    postBody: 'perspective='+which
  });
}

function checkStatus(which) {
  var element = document.getElementById(which);
  if (element.getAttribute('image_status') != null) {
    if (element.getAttribute('image_status') == 'selected') {
      //The item is selected
      return 1;
    }
    else {
      return 0;
    }
  }
  else {
    if (element.getAttribute('class') && element.getAttribute('class') == 'theme_selected') {
      //The item is selected
      return 1;
    }
    else {
      return 0;
    }
  }
}

function toggleCategories(uri,which,status) {
  var which = which.toLowerCase();
  new Ajax.Request(uri, {
    method: 'post',
    postBody: 'category=1&'+which+'='+status,
    onSuccess: function() {
      var element = document.getElementById(which);
      var style = element.getAttribute('style');

      var image_status = element.getAttribute('image_status');

      //Check if the image_status variable is set by a previous click
      if (image_status == null) {
        //If not use the status variable passed by the original link
        if (status == 0) {
          var image = 'icon_unselect.png';
          var image_status = 'unselected';
        }
        else {
          var image = 'icon_select.png';
          var image_status = 'selected';
        }
      }
      else {
        if (image_status == 'unselected') {
          var image = 'icon_select.png';
          var image_status = 'selected';
        }
        else {
          var image = 'icon_unselect.png';
          var image_status = 'unselected';
        }
      }
      element.setAttribute('image_status',image_status);
      element.style.backgroundImage="url(wp-content/themes/blauwehuis/images/"+image+")";

      //Get all three states of the categories
      var parade = checkStatus('instantestedelijkheid');
      var geschiedenis = checkStatus('geschiedenis');
      var gastvrijheid = checkStatus('gastvrijheid');

      new Ajax.Updater('projects_container','wp-content/themes/blauwehuis/includes/project_processor.php',{
        method: 'post',
        postBody: 'parade='+parade+'&geschiedenis='+geschiedenis+'&gastvrijheid='+gastvrijheid
      });
      new Ajax.Updater('residents_container','wp-content/themes/blauwehuis/includes/resident_processor.php',{
        method: 'post',
        postBody: 'parade='+parade+'&geschiedenis='+geschiedenis+'&gastvrijheid='+gastvrijheid
      });
    }
  });
}

function highlightRelations(current,ids,category,unset,perspective) {
  var ids = ids.split(',');
  var parentElement = document.getElementById(current);
  var category = category.split(',');
  var color = new Array();
  for (i=0;i<=category.length;i=i+1) {
    if (category[i] != null) {
      switch(category[i]) {
        case '1':
          color.push('#e31a5c');
        break;
        case '2':
          color.push('#f79910');
        break;
        case '3':
          color.push('#0087a9');
        break;
      }
    }
  }
  
  if (ids != '') {
    for (i=0;i<=ids.length;i=i+1) {
      if (ids[i] != null) {
        var element = document.getElementById(perspective+'-'+ids[i]);
        if (element != null) {
          if (unset == 0) {
            parentElement.style.borderLeftColor=color[i];
            element.style.borderLeftColor=color[i];
          }
          else if (!element.getAttribute("moved")) {
            element.style.borderLeftColor='';
            parentElement.style.borderLeftColor="";
          }
        }
      }
    }
  }
}