//<script type="text/javascript">
//alert("load!");
$(document).ready(function() {
//alert("ready!");
jQuery.validator.messages.required = "";
$("#demoform").validate({
	invalidHandler: function(e, validator) {
		var errors = validator.numberOfInvalids();
		if (errors) {
			var message = errors == 1
				? 'You missed 1 field. It has been highlighted in red'
				: 'You missed ' + errors + ' fields.  Please fill out all required information.';
			$("div.error span").html(message);
			$("div.error").show();
		} else {
			$("div.error").hide();
		}
	},

	messages: {
		mail: {
			required: " ",
			email: "Please submit a valid e-mail address"
//				,
	//		remote: jQuery.validator.format("{0} is already taken, please enter a different address.")	
		},
		Akkoord : {
			required: "You have to agree to the terms of use."
		},
		land : {
			required: "Please select your country"
		},
		doelgroepen : {
			required: "Please select your industry"
		}
	},
	onkeyup: false,
	submitHandler: function(form) {
		$("div.error").hide();
		$('#submit_demo').hide();  //TODOFCL knop weg zodat er niet meerdere keren een submit komt!
		$('#submit_demo_please_wait').show();
		testPostOk("demoform");
	},
	debug:true
});

function testPostOk (postform) {
	$.post("demorequest.php", $("#" + postform).serialize(),					
			function(resp, resultaat) 
			{
		$('#submit_demo_please_wait').hide();
		$('#submit_demo').show();  //TODOFCL knop weg zodat er niet meerdere keren een submit komt!
//		demo_request_reset_fields();
				if (resp.errorr=="succes") {
					demo_request_reset_fields();
					fancyOK("A message is send to your e-mail address. Please confirm this e-mail and we will validate your request. Thank you and have a nice day. The Qtag team", 
							function(ret) { 
								if (ret) {	
//									$(location).attr('href', '/client/list/page/9999999');
								} 
								else 
								{
									alert("test email: error...");
								}
							}
						);	
				} else {
					alert("An error is detected due to unknown circumstances. Please try again or try later. Thank you for your patience.");
				}
//				console.log("klaar");
//				console.log(resp, resultaat);
			}, 'json');
	return true;
};


function fancyOK(msg,callback) {
    var ret;
    jQuery.fancybox({
        modal : true,
        content : "<div style=\"margin:1px;width:240px;\">"+msg+"<div style=\"text-align:right;margin-top:10px;\"><input id=\"fancyConfirm_ok\" style=\"margin:3px;padding:0px;\" type=\"button\" value=\"OK\"></div></div>",
        onComplete : function() {
            jQuery("#fancyConfirm_ok").click(function() {
                ret = true; 
                jQuery.fancybox.close();
            })
        },
        onClosed : function() {
            callback.call(this,ret);
        }
    });
}

$('#submit_demo').click(function() {
	$('#reden').attr('value', 'demo');
//	if (controleer_demo_aanvraag(document.demoform)) {
//alert("invoer ok..");
		$('#demoform').submit(); 		
//	}
});

$('#submit_aanschaf').click(function() {
	$('#reden').attr('value', 'aanschaf');	
	$('#demoform').submit(); 
});

function demo_request_reset_fields() {
	$("#firma").val("");
	$("#aanhef").val("Hr.");
	$("#naam").val("");
	$("#telefoon").val("");
	$("#mail").val("");
	$("#straat").val("");
	$("#postcode").val("");
	$("#stad").val("");
	$("#land").val("");
	$("#doelgroepen").val("");
	$("#reason").val("");
	$('input[name=Akkoord]').attr('checked', false); 
      $('input[name=Newsletter]').attr('checked', false); 	
}

function controleer_demo_aanvraag(ditblad) {
	if ($("#firma").val()=="") {
		alert("Incorrect company name");
		$("#firma").focus();
		return false;
	}
	if ($("#naam").val()=="") {
		alert("Incorrect name");
		$("#naam").focus();
		return false;		
	}
	if ($("#telefoon").val()=="") {
		alert("Incorrect phone number");
		$("#telefoon").focus();
		return false;		
	}

	if ($("#mail").val()=="") {
	    alert("Submit a correct e-mail address, a confirmation will be forwarded.");
		$("#mail").focus();
		return false;		
	}	
	var check_email = $("#mail").val();	
	if (valid_email(check_email) == false) {
	    alert("Submit a correct e-mail address, a confirmation will be forwarded.");
		$("#mail").focus();
	    return (false);
	}	
	
	if ($("#straat").val()=="") {
		alert("Incorrect address");
		$("#straat").focus();
		return false;		
	}
	if ($("#postcode").val()=="") {
		alert("incorrect Zip-code");
		$("#postcode").focus();
		return false;		
	}
	if ($("#stad").val()=="") {
		alert("Submit a correct city");
		$("#stad").focus();
		return false;		
	}
	if ($("#land").val()==0) {
		alert("Select your country");
		$("#land").focus();
		return false;		
	}
	if ($("#doelgroepen").val()==0) {
		alert("Select your industry");
		$("#doelgroepen").focus();
		return false;		
	}
	if ($('#Akkoord').is(':checked') == false) {
		alert("You need to accept the terms of use.");
		$("#Akkoord").focus();
		return false;			
	}
	
/*	if (ditblad.mail.value == "") {
	    alert("Uw email adres moet correct ingevuld zijn. De demo aanvraag gegevens worden naar dit email-adres vestuurd.");
	    ditblad.mail.focus();
	    return (false);
	}*/

	return (true);
}


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


});

//</script>

