﻿/* ############################################################################ */
(function($){
/* ############################################################################ */
/******************************************************************************
* + Public jQuery API
*/
$.fn.extend(
{
	//+ jQueryObject $("div.module-contact form").TGZForm();
	/*+ jQueryObject*/TGZForm : function()
	{
		return this.each(function()
		{
			var oForm = this;
			$(this).find("select.submitOnChange").change(function()
			{
				$(oForm).submit();
			});	
			
			// div.contactByAnExpertBlock ------------------------------------------
			if(!$("div.contactByAnExpertBlock input.checkbox").attr("checked"))
			{
				$("div.contactByAnExpertBlock select").hide();
			}
			$("div.contactByAnExpertBlock input.checkbox").click(function()
			{
				if($(this).attr("checked"))
				{
					$("div.contactByAnExpertBlock select").show();
				}
				else
				{
					$("div.contactByAnExpertBlock select").hide();
				}
			});
			// /div.contactByAnExpertBlock -----------------------------------------
			
			$(this).find("input.submit").click(function()
			{
				isFormValidate = true;

                                // Input text :
				$(this).parent().find("div.textBlock input:text").each(function()
				{
					var params = {};try{eval("params = " + $(this).attr("rel"));}catch(e){}
					var options = $.extend({
						isMandatory : false,
						regExp		: '.*',
						errorMsg	: ''
					}, params );

					if(options.isMandatory && !new RegExp(options.regExp, 'i').test( $(this).val() ))
					{
						$(this).find("+ span.error").text(options.errorMsg);
						if(isFormValidate)
						{
							$(this).focus();
						}
						isFormValidate = false;
					}
					else
					{
						$(this).find("+ span.error").text("");
					}
				});
				
				// Radio
				$(this).parent().find("div.radioBlock").each(function()
				{
					var params = {};try{eval("params = " + $(this).attr("rel"));}catch(e){}
					
					var options = $.extend({
						isMandatory : false,
						regExp		: '.*',
						errorMsg	: ''
					}, params );

					if(options.isMandatory && ($(this).find("input:radio:checked").val() == undefined))
					{
						$(this).find("span.error").text(options.errorMsg);
						
						if(isFormValidate)
						{
							$(this).find("input:radio:first").focus();
						}
						
						isFormValidate = false;
					}
					else
					{
						$(this).find("span.error").text("");
					}
				});
				
				// Checkbox
				$(this).parent().find("div.checkboxBlock").each(function()
				{
					var params = {};try{eval("params = " + $(this).attr("rel"));}catch(e){}
					
					var options = $.extend({
						isMandatory : false,
						regExp		: '.*',
						errorMsg	: ''
					}, params );
					if(options.isMandatory && ($(this).find("input:checkbox:checked:first").val() == undefined))
					{
						$(this).find("span.error").text(options.errorMsg);
						
						if(isFormValidate)
						{
							$(this).find("input:checkbox:first").focus();
						}
						isFormValidate = false;
					}
					else
					{
						$(this).find("span.error").text("");
					}
				});
				
				// Select
				$(this).parent().find("div.selectBlock select").each(function()
				{
					var params = {};try{eval("params = " + $(this).attr("rel"));}catch(e){}
					
					var options = $.extend({
						isMandatory : false,
						regExp		: '.*',
						errorMsg	: ''
					}, params );
				
					if(options.isMandatory && (($(this).val()) == -1))
					{
						// alert(options.errorMsg);
						var displayError = options.errorMsg;
						$(this).find("+ span.error").text(options.errorMsg);
						
						if(isFormValidate)
						{
							$(this).focus();
						}
						isFormValidate = false;
					}
					else
					{
						$(this).find("+ span.error").text("");
					}
				});	

                               
					
				if(!isFormValidate){
					return false;
				}
			});
		});
	}
});
/* ############################################################################ */
})(jQuery);
/* ############################################################################ */

function chiffres(event) {
   // Compatibilité IE / Firefox
   if(!event && window.event) {
                   event=window.event;
   }

   var code = event.keyCode;
   var which = event.which;

   // IE
   if ((code < 48 || code > 57) && code != 46 && code != 8 && code != 9 && code != 16 && code != 13) {
                   event.returnValue = false;
                   event.cancelBubble = true;
   }

   // DOM (dont Firefox)
   if ((which < 48 || which > 57) && (code < 37 || code > 40) && code != 46 && code != 8 && code != 9 && code != 16 && code != 13 || event.ctrlKey) {
                   event.preventDefault();
                   event.stopPropagation();
   }
}

function verif(id, nbCcahr) {
    if ((document.getElementById(id).value != '') && (document.getElementById(id).value.length != nbCcahr) ){
       // $('#phone').focus();

      //  alert('Telephone incorrect');
    }
}

