// JavaScript Document
function validateContactForm(aForm) {
	var divs = [$('lastname'), $('firstname'), $('email'), $('email2'),$('telefoon')]
	var divWithErrors = [];
	
	// clear the border for all fields
	for (i = 0; i < divs.length; i++) {
		var div = divs[i];
		$(div).className = "txt";
	}
	
	// validate the fields
	if ($('lastname').value == "")
		divWithErrors.push($('lastname'));
	if ($('firstname').value == "")
		divWithErrors.push($('firstname'));
	if ($('email').value == "" || !isValidEmail($('email').value))
		divWithErrors.push($('email'));
	if ($('email').value != $('email2').value){
		divWithErrors.push($('email'));divWithErrors.push($('email2'));}
		
	if (divWithErrors.length > 0) {	
		// we have errors, mark the fields						
		for (i = 0; i < divWithErrors.length; i++) {
			var div = divWithErrors[i];
			
			$(div).className = "required";
		}
	} 
	else {
		postString = "q="+aForm+"&lastname="+$('lastname').value+"&"+
				"firstname="+$('firstname').value+"&"+
				"email="+$('email').value+"&"+
				"telefoon="+$('telefoon').value+"&"+
				"bericht="+$('bericht').value+"&"+
				"txtNumber="+$('txtNumber').value+"&"
				;
		postString = postString + '&form='+aForm;
		//alert(postString);
		var myAjax = new Ajax('ajax_php/forms.php', {
			method: 'post',
			data: postString,
			onRequest: function() { 
				$('message_busy').setStyle("display", "block");				
				$('message_busy').innerHTML = '<img src="img/ajax-loader.gif"> Please wait... processing request...'; 
					},
			onComplete: function(req){
			//alert(req);
			     if(req=='0'){$('message_busy').innerHTML = '';$('txtNumber').className = "required";}
			     else{
					   $('message_busy').innerHTML = req;
					   $('contact_form_div').setStyle('display', 'none');
          }
			}
	}).request();
	}
}

//JavaScript Document
function validateNieuwsbriefForm(aForm) {
	var divs = [$('lastname'), $('firstname'), $('email'), $('email2')]
	var divWithErrors = [];
	
	// clear the border for all fields
	for (i = 0; i < divs.length; i++) {
		var div = divs[i];
		
		$(div).className = "txt";
	}
	
	// validate the fields
	
	if ($('email').value == "" || !isValidEmail($('email').value))
		divWithErrors.push($('email'));
	if ($('email').value != $('email2').value){
		divWithErrors.push($('email'));divWithErrors.push($('email2'));}
		
	if (divWithErrors.length > 0) {	
		// we have errors, mark the fields						
		for (i = 0; i < divWithErrors.length; i++) {
			var div = divWithErrors[i];
			
			$(div).className = "required";
		}
	} 
	else {
		postString = "q=NB&lastname="+$('lastname').value+"&"+
				"firstname="+$('firstname').value+"&"+
				"email="+$('email').value;
		postString = postString + '&form='+aForm;
		
		var myAjax = new Ajax('ajax_php/forms.php', {
			method: 'post',
			data: postString,
			onRequest: function() { 
				$('message_busy').setStyle("display", "block");				
				$('message_busy').innerHTML = '<img src="img/ajax-loader.gif"> Please wait... processing request...'; 
					},
			onComplete: function(req){
						alert(req);
					$('message_busy').innerHTML = req;
					$('contact_form_div').setStyle('display', 'none');
					$('contact_success_div').setStyle('display', 'block');
			}
	}).request();
	}
}

function isValidEmail(txt) {
	if (txt == "") return false;
	
	// copy paste from various sites :)
	var regex = /[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
	return txt.match(regex);
}
