$(document).ready(function(){
	$.get("inc/token.asp",function(txt){
		$("#formContact").append('<input type="hidden" name="ts" value="'+txt+'" />');
	});
});

// Validate form
function validateForm(){
	var strError = "";
	var elmContent = document.getElementById("content");
	if(document.getElementById("error")){
		elmContent.removeChild(document.getElementById("error"));
	}
	valid = false;
	for(var i = 1; document.getElementById("quebec" + i); i++){
		if(document.getElementById("quebec" + i).checked){
			valid = true;
			break;
		}
	}
	if(!valid){
		strError += "<p>Vous devez spécifier votre localisation.</p>";
	}
	if(document.getElementById("quebec1").checked){
		var sel = document.getElementById("region");
		if(document.getElementById("region").options[sel.selectedIndex].value == ""){
			strError += "<p>Vous devez spécifier votre région.</p>";
		}
	}
	if(!validateEmpty("nom")){
		strError += "<p>Vous devez écrire votre nom.</p>";
	}
	/*if(document.getElementById("parCourriel").checked == true){
		if(!validateMail("courriel", false)){
			strError += "<p>Vous devez écrire un courriel valide.</p>";
		}
	}else{
		if(!validateMail("courriel", true)){
			strError += "<p>Vous devez écrire un courriel valide.</p>";
		}
	}*/
	if(!validateMail("courriel", false)){
		strError += "<p>Vous devez écrire un courriel valide.</p>";
	}
	if(!validateEmpty("_subject")){
		strError += "<p>Vous devez écrire un sujet.</p>";
	}
	if(!validateEmpty("demande")){
		strError += "<p>Vous devez écrire un commentaire ou une question.</p>";
	}
	/*valid = jcap();
	if(!valid){
		strError += "<p>Vous devez écrire le texte de validation.</p>"
	}*/
	if(strError != ""){
		var elm = document.createElement("div");
		elmContent.insertBefore(elm, document.getElementById("formContact"));
		elm.setAttribute("id", "error");
		elm.innerHTML = "<p><strong>Veuillez compléter correctement tous les champs.</strong></p>" + strError;
		window.location.href = "#error";
		return false;
	}else{
		if(document.getElementById("quebec2").checked){
			var sel = document.getElementById("region");
			document.getElementById("region").options[sel.selectedIndex].value = "";
		}
		return true;
	}
}

// Validate empty field
function validateEmpty(field){
	var strField = document.getElementById(field);
	if(strField.value == ""){
		return false;
	}else{
		return true;
	}
}

// Validate main field
function validateMail(field, empty){
    var strField = document.getElementById(field);
	var regExp = /^([a-zA-Z0-9_\.\-\'])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(empty == true && strField.value == ""){
		return true;
	}else{
		if(!regExp.test(strField.value)){
			return false;
		}else{
			return true;
		}
	}
}
