/*
Form validation functions
Copyright (c) 2002 Ylab, Utrecht, NL
2003-12-09; yohan@ylab.nl: Created
*/
function validateForm(frm){
  var isVolunteer = (frm.name == 'volunteer');
  var isPublication = (frm.name == 'publication');
  var isOrganisation = (frm.name == 'organisation');
  var isChatLogin = (frm.name == 'chatlogin');
  var isAlloeMail = (frm.name == 'alloe');
  var isTerugBel = (frm.name == 'terugbel');

  if (isVolunteer){
	  if (!isNotNull (frm['voornaam'], "Voornaam")) return false;
	  if (!isNotNull (frm['achternaam'], "Achternaam")) return false;
  }
  if (isPublication || isOrganisation){
	  if (!isNotNull (frm['naam'], "Naam")) return false;
	}
	if (isPublication){
	  if (!isNotNull (frm['organisatie'], "Organisatie")) return false;
	  if (!isNotNull (frm['functie'], "Functie")) return false;
  	if (!isNotNull (frm['adres'], "Postadres")) return false;
  	if (!isNotNull (frm['postcode'], "Postcode")) return false;
  	if (!isNotNull (frm['plaats'], "Plaats")) return false;
  	if (!isNotNull (frm['telefoon'], "Telefoon")) return false;
	}
	if(!isTerugBel){
		if (!isNotNull (frm['email'], "E-mailadres")) return false;
	  if (!isEmail (frm['email'], "E-mailadres")) return false;
	}
  if (isVolunteer){
  	if (!isNotNull (frm['adres'], "Adres")) return false;
  	if (!isNotNull (frm['postcode'], "Postcode")) return false;
  	if (!isNotNull (frm['woonplaats'], "Woonplaats")) return false;
  	if (!isNotNull (frm['telefoon'], "Telefoon")) return false;
	}
	if (isVolunteer || isOrganisation){
	  if (!isNotNull (frm['onderwerp'], "Onderwerp")) return false;
	}
  if (isChatLogin){
    //nothing extra
  }
	if(isTerugBel){
		if (!isNotNull (frm['organisatie'], "Naam organisatie")) return false;
	  if (!isNotNull (frm['naam'], "Naam client")) return false;
	  if (!isNotNull (frm['telefoon'], "Telefoonnummer")) return false;
	}
	return true;
}

function errormsg(errorcode, guiName, val)
{
	switch (errorcode)
	{
		case 1: alert('Het veld ' + guiName + ' moet ingevuld zijn.'); break;
		case 2: alert('Het e-mailadres is onvolledig.'); break;
		default: alert('Ongedefineerde foutcode: ' + errorcode); break;
  }
}

function isNotNull (field, guiName)
{
	//validates if a field contains a value
  //field: input element text|hidden
  //guiName: fieldname to communicate with user
	if ( (!field.value) || (field.value == "") )
	{
		errormsg(1, guiName);
		if(field.focus) {field.focus();}
		return false;
	}
	return true;
}

function isEmail (field, guiName){
	//validates if a textbox contains a valid email address
	var str = field.value;
	if (!str) return true;
	var at = str.indexOf('@');
	if ( (at < 2) || (str.indexOf('.',at+1) < 4) )
	{
		errormsg(2, guiName);
		if(field.focus) {field.focus();}
		return false;
	}
	return true;
}
