
jQuery(document).ready(function($)
{

	$globalError = false; // global error


	$.fn.beforeShowTab = function(){}

	$.fn.verifForm = function()
	{
		if(typeof($simulCredit)!='undefined')
		{
			if(!$simulCredit)
			{
				if($globalError == true)
				{
					$('span.error').show();
				}
				else
				{
					$globalError = false;
					$('span.error').hide();
				}
				alert("Estimez vos mensualités de remboursement en cliquant sur 'Calculer'");
				$('html,body').animate(
				{
					scrollTop: $('#main-content').offset().top
				}, 'fast');
				return false
			}
		}

		if($globalError == true)
		{
			$('span.error').show();	// show span error

			$('html,body').animate(	// back to the top
			{
				scrollTop: $(this).offset().top
			}, 'fast');

    		return false;
		}
		else
		{
			$globalError = false;	// reset global errors
			$('span.error').hide();	// hide span error
			return true;
		}
	}

	// method to display errors
	$.fn.displayErrors = function(hide)
	{
		if(hide == 1)
		{
			$globalError = true;
			$(this).after('<span class="error-field">*</span>');
			$(this).css('background', errColorField);
			$(this).addClass('error_input');
		}
		else
		{
			(hide == 'reset')?$(this).next('.error-field').remove():$(this).next('span.error-field').remove();
			$(this).css('background', normColorField);
			$(this).removeClass('error_input');
		}
	}

	$.fn.isRequire = function()
	{
		var currentTab 	= $(this).attr('id');
		var disError 	= false;

		$('#'+currentTab+' .require:visible').each(function()
		{
			// Reset Class
			$(this).displayErrors('reset');

			// classic require field
			if($(this).val().length < 1)
			{
				disError = true;
			}
			else if($(this).attr('class').match(/\email/) && !$(this).isEmail())
			{
				disError = true;
			}
			else if($(this).attr('class').match(/\zip/) && !$(this).isZip())
			{
				disError = true;
			}
			else
			{
				disError = false;
			}

			$(this).displayErrors(disError);
		});
	}

	$.fn.isSelectDate = function()
	{
		var currentTab 	= $(this).attr('id');
		var disError 	= false;

		$('#'+currentTab+' .selectDate:visible').each(function()
		{
			// Reset Class
			$(this).displayErrors('reset');

			($(this).val()==0)?disError = true:disError = false;

			$(this).displayErrors(disError);
		});
	}

	$.fn.isDate = function()
	{

		var currentTab 	= $(this).attr('id');
		var disError 	= false;
		var dateRef 	= /^\d{2}\/\d{2}\/\d{4}?$/;

		$('#'+currentTab+' .date:visible').each(function()
		{
			// Reset Class
			$(this).displayErrors('reset');

			($(this).val().length < 1 || !dateRef.test($(this).val()))?disError = true : disError = false;

			$(this).displayErrors(disError);
		});
	}

	$.fn.isZip = function()
	{
		if($(this).val().length != 5 || ($(this).val() - 0) !=  $(this).val())return false;
		return true;
	}

	$.fn.isPhone = function(tel1, tel2)
	{
		var currentTab 	= $(this).attr('id');

		$tel1 = $('#'+currentTab+' #INTERNAUTE_TELP');
		$tel2 = $('#'+currentTab+' #INTERNAUTE_TGSM');

		if(tel1) $tel1 = $('#'+currentTab+' .'+tel1);
		if(tel2) $tel2 = $('#'+currentTab+' .'+tel2);

		if( $tel1.is(':visible') )
		{
			$tel1.displayErrors(0);
			$tel2.displayErrors(0);

			if( $tel1.val().length < 10 && $tel2.val().length < 10)
			{
				$tel1.displayErrors(1);
				$tel2.displayErrors(1);
			}
		}
		else return false;
	}

	$('.phone').keyup(function()
	{
		if($(this).attr('class').match(/\error_input/))
		{
			$tel1 = $('#INTERNAUTE_TELP');
			$tel2 = $('#INTERNAUTE_TGSM');

			$tel1.displayErrors(0);
			$tel2.displayErrors(0);

			if($tel1.val().length < 10 && $tel2.val().length < 10)
			{
				$tel1.displayErrors(1);
				$tel2.displayErrors(1);
			}
		}
	});

	$.fn.isChecked = function()
	{
		var currentTab 	= $(this).attr('id');

		// Reset Class
		$('#'+currentTab+' table.compare-table input').displayErrors('reset');

		if(!$('#'+currentTab+' table.compare-table input').is(':checked'))
		{
			$('#'+currentTab+' table.compare-table input').displayErrors(1);
		}
		else
		{
			return true;
		}
	}

	$.fn.isEmail = function()
	{
		var currentTab 	= $(this).attr('id');
		var disError 	= false;
		var emailReg 	= /^([\w-\.]+@([\w-]+\.)+([\w-]){2,4})?$/;

		if(emailReg.test($(this).val()))
		{
			return true;
		}
		else return false;
	}

	$.fn.isNumber = function()
	{
		var currentTab 	= $(this).attr('id');
		var disError 	= false;

		$('#'+currentTab+' .number:visible').each(function()
		{
			$(this).displayErrors('reset');	// Reset Class

			// classic require field
			($(this).val().length < 1 || ($(this).val() - 0) !=  $(this).val())?disError = true : disError = false;
			$(this).displayErrors(disError);
		});
	}

	$('.content.form .next a, .btn-valid-form').click(function ()
	{
		$globalError 	= false;

		($(this).parent().is('.next'))?$currentTab = $(this).parent().parent().parent().attr('id') : $currentTab = $(this).parent().parent().attr('id');

		$(this).formValidation($currentTab);

		var $verif=$('#'+$currentTab).verifForm();

		if(($verif)&&($currentTab == 'fTab'))return true;

		if(($verif)&&($currentTab != 'fTab'))			
		{
			$nextTab = $(this).attr("href");

			$('.zone .tab').hide();
			$($nextTab).show();
			$('html,body').animate(
			{
				scrollTop: $($nextTab).offset().top
			}, 'fast');

			$(this).beforeShowTab();

			//return false;
		}

		return false;
	});
	

	$('.require').keyup(function()
	{
		if($(this).attr('class').match(/\error_input/))
		{
			$(this).displayErrors(0);
			var disError = false;

			if($(this).val().length < 1)	// classic require field
			{
				disError = true;
			}
			else if($(this).attr('class').match(/\email/) && !$(this).isEmail())
			{
				disError = true;
			}
			else if($(this).attr('class').match(/\zip/) && !$(this).isZip())
			{
				disError = true;
			}
			else disError = false;

			$(this).displayErrors(disError);
		}
	});

	$('.selectDate').change(function()
	{
		if($(this).attr('class').match(/\error_input/))
		{
			$(this).displayErrors(0);
			var disError = false;

			// classic require field
			($(this).val() == 0)?disError = true : disError = false;
			$(this).displayErrors(disError);
		}
	});
});
