// Form validation
function validateForm() {
	var error = 0;
	$('input.ess').each(function(index) {
		//Controleer of de eerdere tabel niet de class 'by2' heeft, by2 moet namelijk ingevult worden door de monteur
		if ($(this).closest('table').attr('class') != 'by2') {
	    if ($(this).val() == '') {
			$(this).parent().children('.excl').css('display','inline-block');
			error = 1;			
		} else {
			$(this).parent().children('.excl').css('display','none');
		}
		
		
		if ($(this).attr('inputtype') !== undefined) {
			//Check op type
			var inputtype = $(this).attr('inputtype');
			if (inputtype == 'date') {
				if(!valDate($(this).val())) {
					$(this).parent().children('.excl').css('display','inline-block');
					error = 1;
				} else {
					$(this).parent().children('.excl').css('display','none');
				};
			}
			if (inputtype == 'email') {
				if(!valMail($(this).val())) {
					$(this).parent().children('.excl').css('display','inline-block');
					error = 1;
				} else {
					$(this).parent().children('.excl').css('display','none');
				};
			}
			
			if (inputtype == 'reference') {
				alert(valRef($(this).val()));
				if(valRef($(this).val()) == 'fout') {
					$(this).parent().children('.excl').css('display','inline-block');
					error = 1;
				} else {
					$(this).parent().children('.excl').css('display','none');
				};
			}
		}
		} // Einde zoeken naar closest table
  	});	
	
	
	$('textarea.ess').each(function(index) {
		if ($(this).closest('table').attr('class') != 'by2') {
		 if ($(this).val() == '') {
			$(this).parent().children('.excl').css('display','inline-block');
			error = 1;			
		} else {
			$(this).parent().children('.excl').css('display','none');
		}
		}
	});
	
	if (error == 0) {
		return true;
	} else {
		alert ('Please fill in all the required fields.');
		return false;
		//return true;
	}
}


function valDate(input) {
	//Valideer datum input, dd-mm-yyyy
	var regs = input.split("-");
	if (!regs[2]) {
		return false;
	}
	if(regs[0] < 1 || regs[0] > 31) {
		return false;
	}
	if(regs[1] < 1 || regs[1] > 12) {
		return false;
	}
	if(regs[2] < 2010 || regs[2] > 2014) {
		return false;
	}
	
	return true;
}

function valMail(input) {
	//Valideer email input
	var atpos=input.indexOf("@");
	var dotpos=input.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=input.length) {
	  return false;
	}
	return true;
}




$.ajaxSetup( { "async": false } );

 
 
 
function valRef(input) {
	//AJAX call met DB om te checken of het referentienummer nog niet bestaat..
	//JSON request
	var result = '';
	$.ajaxSetup( { "async": false } );
	$.getJSON("ajax/checkref.php?ref=" + input,

		function(data){
		  $.each(data, function(i,item){
					if (item.field == "exists") {
						//Goede callback
					  if (item.value == "yes") {
						  //Bestaat al, dus false terug
						result = 'fout';
					  }
					  if (item.value == "no") {
						  //Bestaat nog niet, dus true terug
						result = 'goed';
					  } 
					} else {
						// Geen exists in callback, dus fout...
						result = 'fout';
					}	
	
		});

	});
	
	return result;
	
}
