// JavaScript Document
$().ready(function() {
	$('#contact').jqm({trigger: '#contactModal'});
	$('#privacy').jqm({trigger: '#privacyModal'});
	$('#eurofinco').jqm({trigger: '#eurofincoModal'});
	var options = { 
		target:        '#formDiv',   // target element(s) to be updated with server response 
		beforeSubmit:  formCheck,  // pre-submit callback 
		success:       closeForm  // post-submit callback 
 
		// other available options: 
		//url:       url         // override for form's 'action' attribute 
		//type:      type        // 'get' or 'post', override for form's 'method' attribute 
		//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
		//clearForm: true        // clear all form fields after successful submit 
		//resetForm: true        // reset the form after successful submit 
 
		// $.ajax options can be used here too, for example: 
		//timeout:   3000 
	}; 
 
	// bind form using 'ajaxForm' 
	$('#contactForm').ajaxForm(options);
});

function formCheck(formData, jqForm, options) {
	
	var error = false;
	
	if(jqForm[0].txtVoornaam.value == ""){
		error = true;
		$('#reqfield1').attr("style", "color:Red; visibility:visible;");
	} else {
		$('#reqfield1').attr("style", "color:Red; visibility:hidden;");
	}
	if(jqForm[0].txtNaam.value == ""){
		error = true;
		$('#reqfield2').attr("style", "color:Red; visibility:visible;");
	} else {
		$('#reqfield2').attr("style", "color:Red; visibility:hidden;");
	}
	if(jqForm[0].txtTelefoon.value == ""){
		error = true;
		$('#reqfield3').attr("style", "color:Red; visibility:visible;");
	} else {
		$('#reqfield3').attr("style", "color:Red; visibility:hidden;");
	}
	if(!emailCheck(jqForm[0].txtEmail.value)){
		error = true;
		$('#reqfield4').attr("style", "color:Red; visibility:visible;");
	} else {
		$('#reqfield4').attr("style", "color:Red; visibility:hidden;");
	}
	if(jqForm[0].txtBericht.value == ""){
		error = true;
		$('#reqfield5').attr("style", "color:Red; visibility:visible;");
	} else {
		$('#reqfield5').attr("style", "color:Red; visibility:hidden;");
	}
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue
	if(error){
		return false;
	} else {
		return true;
	}
}

function closeForm(responseText, statusText){
	setTimeout( function() { $('#contact').jqmHide() }, 5000);
}

function emailCheck(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					
}