// JavaScript Document
$(function(){
	$('form #sendbutton').click(function(){
		var form = $(this).parent('div').parent('form');
		var fid = '';
		form.find('.form').each(function(){
			if( ($(this).attr('alt') == 'Required' && $(this).val() == '') || 
				( $(this).hasClass('email') && !validMail($(this).val()) ) || 
				( $(this).attr('type') == 'checkbox' && !validMail($(this).val()) && $(this).attr('alt') == 'Required' && !$(this).is(':checked') ) ){
				fid = $(this);
				return false;
			};
		});
		
		if(fid != ''){
			alertform(fid);
		}else{			
			form.submit();
			
		};
		
	});
});
function alertform(field){
	var label = field.siblings('label').html().replace(' *', '');
	var parent = field.parent('div');
	var input = field.get(0).tagName;
	var lang = $('html').attr('lang');
	
	switch(lang){
		case 'nl' : 
			var closeWindow = 'sluit';
			var ncomment = "Sommige velden in dit formulier zijn verplicht in te vullen. Gelieve hier <strong>uw "+label.toLowerCase()+"</strong> in te vullen.";
			var emcomment = "Gelieve uw e-mail adres correct in te vullen. Indien dit niet zo is, kunt u uw bericht niet versturen.";
		break;
		case 'en' : 
			var closeWindow = 'close';
			var ncomment = "Some fields are obligatory. Please enter your <strong>"+label.toLowerCase()+"</strong> here.";
			var emcomment = "Please enter a correct email address. If not, your message will not be sent.";
		break;
		case 'fr' : 
			var closeWindow = 'fermer';
			var ncomment = "Certains champs repris dans ce formulaire sont obligatoires. Veuillez indiquer ici <strong>votre "+label.toLowerCase()+"</strong>.";
			var emcomment = "Veuillez indiquer votre adresse e-mail correcte. Si elle n'est pas correcte, vous ne pouvez pas envoyer votre message. ";
		break;
		case 'de' : 
			var closeWindow = 'schließen';
			var ncomment = "Einige Felder in diesem Formular sind Pflichtfelder. Wir bitten Sie, hier <strong>Ihren "+label.toLowerCase()+"</strong> einzugeben.";
			var emcomment = "Bitte geben Sie Ihre E-Mail-Adresse korrekt ein. Andernfalls können Sie Ihren Bericht nicht versenden.";
		break;
		case 'es' : 
			var closeWindow = 'Cerrar';
			var ncomment = "Algunos campos de este formulario son obligatorios. Indique <strong>su "+label.toLowerCase()+"</strong> aquí.";
			var emcomment = "Indique su dirección de correo electrónico correctamente. De lo contrario, no podrá enviar su mensaje.";
		break;
		case 'it' : 
			var closeWindow = 'Chiudere';
			var ncomment = "Alcuni campi del presente modulo sono obbligatori. Inserire qui <strong>il "+label.toLowerCase()+"</strong>.";
			var emcomment = "Inserire un indirizzo e-mail valido. In caso contrario non sarà possibile inviare il messaggio.";
		break;
	};
	
	var comment = (field.hasClass('email') && field.val() != '')? emcomment:ncomment;
	
	$('body').append('<div id="alertform"></div>');
	var obj = $("#alertform");
	obj.show();
	obj.html('<span id="af_close"></span><div><div id="alertformcontainer"><div><a href="#" id="af_sluit">X '+closeWindow+'</a><span><p>'+comment+'<br /><br /><span id="field"></span> <button id="af_btn">Ok</button></p></span></div></div></div>');
	$('#alertformcontainer').css({'margin' : Math.round((obj.height()-$("#alertformcontainer").height())/2)+'px 0px 0px 0px'});
	field.appendTo('#field');
	field.focus();
	$('#alertform').keyup(function(e) {
		if(e.keyCode == 13) {
			close_alertform(field, parent, obj);
		}
	});
	
	$("#af_close, #af_sluit, #af_btn").click(function(){
		close_alertform(field, parent, obj);
	});
};

function close_alertform(field, parent, obj){
	if(field.attr('type') == 'checkbox'){
		field.prependTo(parent);
	}else{
		field.appendTo(parent);
	};
	obj.remove();
};

function validMail(str){
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if(str.indexOf(at)==-1){return false;};
	if(str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false;};
	if(str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false;};
	if(str.indexOf(at,(lat+1))!=-1){return false;};
	if(str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false;};
	if(str.indexOf(dot,(lat+2))==-1){return false;};
	if(str.indexOf(" ")!=-1){return false;};
	return true;		
};

function af_sent(){
	$('body').append('<div id="alertform"></div>');
	var obj = $("#alertform");
	obj.css({'display': 'block'})
	var lang = $('html').attr('lang');
	switch(lang){
		case 'nl' : 
			var comment = "Uw e-mail werd succesvol verzonden.";
			var closeWindow = 'sluit';
		break;
		case 'fr' : 
			var comment = "Votre e-mail a été envoyé avec succès.";
			var closeWindow = 'fermer';
		break;
		case 'en' : 
			var comment = "Your email was sent successfully.";
			var closeWindow = 'close';
		break;
		case 'de' : 
			var comment = "Ihre E-Mail wurde erfolgreich versendet.";
			var closeWindow = 'schließen';
		break;
		case 'es' : 
			var comment = "Su correo electrónico se ha enviado correctamente.";
			var closeWindow = 'Cerrar';
		break;
		case 'it' : 
			var comment = "Il messaggio è stato inviato con successo.";
			var closeWindow = 'Chiudere';
		break;
	};
	
	obj.html('<span id="af_close"></span><div><div id="alertformcontainer"><div><a href="#" id="af_sluit">X '+closeWindow+'</a><span><p>'+comment+'<button id="af_btn">Ok</button></p></span></div></div></div>');
	$('#alertformbutton').focus();
	$('#af_close, #af_sluit, , #af_btn').click(function(){
		obj.remove();
	})
	
	$("#alertformcontainer").css({'margin' : Math.round((obj.height()-$("#alertformcontainer").height())/2)+'px 0px 0px 0px'})
};
