/*
 * Validation du mini formulaire de contact
 */

$(function()
{
    // vérification envoi email
    if (getURLParam("email") != null) {
		$("body").append("<div id=\"messageBox\" title=\"Information\">Votre demande de télédémonstration a bien été reçue.</div>");
		$("#messageBox").dialog({
            draggable: false,
			closeText: "Fermer",
			resizable: false,
			modal: true,
            buttons: {
                Fermer: function() {
                    $(this).dialog('close');
                }
            }
        });
    }

    /* Modification Marie Anamorphik Studio
	$("#MINIFORM input, #MINIFORM textarea").focus(function(){
        $(this).attr("reset","reset");
        //$(this).val("");
        $(this).removeClass("error-2");
    });
	*/
	$("#MINIFORM input, #MINIFORM textarea").focus(function(){
		champ = $(this).val();
		if ( champ == "Champ obligatoire" || champ == "Email non valide" || champ == "Nombre non valide" ) {
			$(this).attr("reset","reset");
			$(this).val("");
			$(this).removeClass("error-2");
		} else {
			$(this).attr("reset","reset");
        	//$(this).val("");
			$(this).removeClass("error-2");
		}
	});

    $("#SubmitMiniForm").click(function(){
        var errorCount = 0;
		/*
        $("#MINIFORM input, #MINIFORM textarea").each(function(){
            if ( $(this).attr("reset") != "reset" || $(this).val() == "" )
            {
                $(this).addClass("error");
                errorCount += 1;
            }
        });
		*/
				
		// nom
		var nom = $("#nom").val();
		if ( nom == "" ) {
			errorCount += 1;
			$("#nom").addClass("error-2");
			$("#nom").val("Champ obligatoire");
		}
		//if ( $("#nom").val() == "" )
		//{
			//$(this).addClass("error");
			//errorCount += 1;
		//}
		
		// numéro de téléphone
		var tel = $("#numtel").val();
		if ( tel == "" ) {
			errorCount += 1;
			$("#numtel").addClass("error-2");
			$("#numtel").val("Champ obligatoire");
		}
		
		// email
		var email = $("#emailce").val();
		regEx = new RegExp("((([a-z1-9]*)\\.?){1,})@([a-z\\-1-9A-Z]*)\\.([a-z]{2,3})");
		
		if ( !email.match(regEx) )
		{
			errorCount += 1;
			$("#emailce").addClass("error-2");
			$("#emailce").val("Email non valide");
		}
		
		// effectif
		var effectif = $("#effectifce").val();
		//Modif Marie Anamorphik Studio 
		//
		if ( effectif == "" )
		 {
			errorCount += 1;
			$("#effectifce").addClass("error-2");
		}
		
		// vérifie que le champs est un nombre s'il est rempli
		//if ( effectif != "" && parseInt(effectif) != effectif-0 )
		//{
			//errorCount += 1;
			//$("#effectifce").addClass("error");
			//$("#effectifce").val("Nombre");
		//}
		
		//if ( parseInt(effectif) != effectif-0 )
		//{
		//	errorCount += 1;
		//	$("#effectifce").addClass("error");
		//}
		// Fin Modif Marie Anamorphik Studio 
		
		
		// code postal
		var cp = $("#cpce").val();
		if ( cp == "" || cp == $("#cpce").attr("Title") )
		{
			errorCount += 1;
			$("#cpce").addClass("error-2");
			$("#cpce").val("Nombre non valide");
		}
		
        if ( errorCount == 0 )
        {
            $("#MINIFORM").submit();
        }
    });
});

// merci à l'auteur
var getURLParam = function(strParamName){
    var strReturn = "";
    var strHref = window.location.href;
    var bFound=false;

    var cmpstring = strParamName + "=";
    var cmplen = cmpstring.length;

    if ( strHref.indexOf("?") > -1 ){
        var strQueryString = strHref.substr(strHref.indexOf("?")+1);
        var aQueryString = strQueryString.split("&");
        for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
            if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                bFound=true;
                break;
            }

        }
    }
    if (bFound==false) return null;
    return strReturn;
}

