/*
function contact() {
	//function for contact form dropdown
	if ($("#contactForm").is(":hidden")){
		//$("div.contact").css('display', 'none');
		$("#contactForm").slideDown("slow");
		$("#backgroundPopup").css({"opacity": "0.9"});
		$("#backgroundPopup").fadeIn("slow"); 
	}
	else{
		dismissForm();
	}
}
*/

function addFormHandler(formContainer, type) {
	$(formContainer + ' #contactForm .contactForm').submit(function() { return validateForm(formContainer, type); });
	$(formContainer + ' input.cancel').click(function() {
		dismissForm();
	});
}
/*
function offerte(type) {
	$.ajax({
		url: 'forms/offerte.html',
		beforeSend: function() {
			//$("div#mainContent div#mainContentText").html('Loading...');
		},
		success: function(d) {
			$("div#formContainer").html(d);
			//$("div.contact").css('display', 'none');
			$("div#formContainer #contactForm").slideDown("slow");
			$("#backgroundPopup").css({"opacity": "0.9"});
			$("#backgroundPopup").fadeIn("slow");
			addFormHandler("div#formContainer", type);
		},
		error: function() {
			$("div#mainContent div#mainContentText").html('De pagina bestaat niet');
		}
	});
}
*/
function loadForm(type) {
	var urlToLoad = 'forms/offerte.html';
	if(type == 'contact') {
		urlToLoad = 'forms/contact.html';
	}
	$.ajax({
		url: urlToLoad,
		beforeSend: function() {
			//$("div#mainContent div#mainContentText").html('Loading...');
		},
		success: function(d) {
			$("div#formContainer").html(d);
			$("div#formContainer").show();
			$("div#formContainer #contactForm").slideDown("slow");
			$("#backgroundPopup").css({"opacity": "0.9"});
			$("#backgroundPopup").fadeIn("slow");
			addFormHandler("div#formContainer", type);
		},
		error: function() {
			$("div#mainContent div#mainContentText").html('De pagina bestaat niet');
		}
	});
}
/*
function contactForm() {
	$.ajax({
		url: 'forms/contact.html',
		beforeSend: function() {
			//$("div#mainContent div#mainContentText").html('Loading...');
		},
		success: function(d) {
			$("div#formContainer").html(d);
			//$("div.contact").css('display', 'none');
			$("div#formContainer #contactForm").slideDown("slow");
			$("#backgroundPopup").css({"opacity": "0.9"});
			$("#backgroundPopup").fadeIn("slow");
			addFormHandler("div#formContainer", "contact");
		},
		error: function() {
			$("div#mainContent div#mainContentText").html('De pagina bestaat niet');
		}
	});
}*/
	/*
$('input.cancel').click(function() {
	dismissForm();
});
*/
$('div#backgroundPopup').click(function() {
	dismissForm();
});

dismissForm = function() {
	//$("#contactForm").slideUp("fast", function() {
		//$('div.contact').css('display', '');
	//});
	
	$("div#formContainer #contactForm").slideUp("slow", function() {
	//alert('ofk');
		$("div#formContainer").html('');
		$("div#formContainer").css('display', 'none');	
		
	});
	$("#backgroundPopup").fadeOut("slow");
}

validateForm = function(formContainer, type) {
	//statements to validate the form	
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	var name = $(formContainer+" input#name").val();
	var email = $(formContainer+" input#email").val();
	var message = $(formContainer+" textarea#message").val();
	
	if (!filter.test(email)) {
		$(formContainer+' .email-missing').show();
	} else {$(formContainer+' .email-missing').hide();}
	if (name == "") {
		$(formContainer+' .name-missing').show();
	} else {$(formContainer+' .name-missing').hide();}	
	if (message == "") {
		$(formContainer+' .message-missing').show();
	} else {$(formContainer+' .message-missing').hide();}		
	if ((name == "") || (!filter.test(email)) || (message == "")){
		return false;
	} 
	
	if ((name != "") && (filter.test(email)) && (message != "")) {
		//hide the form
		$(formContainer+' .contactForm').hide();
	
		//show the loading bar
		$(formContainer+' .loader').append($(formContainer+' .bar'));
		$(formContainer+' .bar').css({display:'block'});
	/*
		//send the ajax request
		$.post('mail.php',{'name':name,
						  'email':email,
						  'message':message,
						  'type':type},
	*/
		$(formContainer+' .bar').css({display:'none'});
		$(formContainer+' .loader').html('<br><h3>Formulier aan het verzenden...</h3>');
		 	
		$.ajax({
		  type: 'POST',
		  url: 'mail.php',
		  data: {'name':name,
				  'email':email,
				  'message':message,
				  'type':type},
		  error: function(r, textStatus, errorThrown) {
		  	$(formContainer+' .loader').html('<br><h3>Er is een fout opgetreden...</h3><p>Graag vragen wij u telefonisch contact op te nemen.');
		  	setTimeout('$("'+formContainer+' .loader").html(""); $("'+formContainer+' .contactForm").show();', 4000);
		  },
		  success: function(d) {
		 	$(formContainer+' .loader').html(d);
		 	//waits 2000, then closes the form and fades out
		 	setTimeout('$("#backgroundPopup").fadeOut("slow"); $("'+formContainer+' #contactForm").slideUp("slow");', 4000);
		  },
		  dataType: 'html'
		});
		/*
		//return the data
		function(data){
		  //hide the graphic
		  $(formContainer+' .bar').css({display:'none'});
		  $(formContainer+' .loader').append(data);
		});
		*/
		
		
		
		//stay on the page
		return false;
	} 
}

 //submission scripts
//$('.contactForm').submit(function() { return validateForm("div#contactFormContainer", "contact"); });


$(document).ready(function(){
	/*
	//animation for same page links #
	$('a[href*=#]').each(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname
		&& this.hash.replace(/#/,'') ) {
		  var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
		  var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
			if ($(this.hash).length) {
				$(this).click(function(event) {
					var targetOffset = $(this.hash).offset().top;
					var target = this.hash;
					event.preventDefault();			   
					$('html, body').animate({scrollTop: targetOffset}, 500);
					return false;
				});
			}
		}
	});
  */
	 
	$("#backgroundPopup").css({  
		"height": '100%'
	});
	
	//$(".contactLink").click(function(){contact()});
	//$(".contact").click(function(){contact()});
	
	//addContactHandlers();
	
});

addContactHandlers = function(ajaxCall) {
	//run contact form when any contact link is clicked
	$("div#mainContent div#mainContentText .contactLink").click(function(){loadForm("contact")});
	
	$(".offerteVideo").click(function(){loadForm("offerteVideo")});
	$(".offerteWebsite").click(function(){loadForm("offerteWebsite")});
	if(!ajaxCall) {
		$(".contactFormLink").click(function(){loadForm("contact")});
	}
}
