function newMail(fId) {
	$('.'+fId+'_Confirmation').hide();
	$('.'+fId+'_Form').show();
	$('.'+fId+'_Form :input[type="text"]').val('');
	$('.'+fId+'_Form :input[type="password"]').val('');
	$('.'+fId+'_Form textarea').val('');
}

function getCaptcha(fId) {
	$.get('/occasion3/sendMail.cfm?rand='+Math.random(),{action:4},function(data) {
		$('#'+fId+' .captchaImg').attr('src','/include/captcha.cfm?w='+data);
		$('#'+fId+' .captcha').val(data);
	});
}

function toggleLoading(on,obj) {
	if (on==0) {
		$('#'+obj.id+' .loadingGif').height($('#'+obj.id).height());
		$('#'+obj.id+' .loadingGif').width($('#'+obj.id).width());
		$('#'+obj.id+'>*:not(.loadingGif)').hide();
		$('#'+obj.id+' .loadingGif').show();
	} else {
		$('#'+obj.id+'>*:not(.loadingGif)').show();
		$('#'+obj.id+' .loadingGif').hide();
	}
}

function submitForm(obj){
	$('#'+obj.id+' label.error').hide();
	$('#'+obj.id+' input.error,textarea.error').removeClass('error');
	if ( isBlank(obj.smEmail.value) ) {
		/*$('#'+obj.id+' .error1').show();*/
		$(obj.smEmail).addClass('error');
		obj.smEmail.focus();
		alert('Veuillez saisir une adresse email valide.');
		return false;
	}
	if ( !isMail(obj.smEmail.value) ) {
		//$('#'+obj.id+' .error2').show();
		$(obj.smEmail).addClass('error');
		obj.smEmail.focus();
		alert('Veuillez saisir une adresse email valide.');
		return false;
	}
	if (obj.type.value==1) {
		if (obj.smEmailTo) {
			if ( isBlank(obj.smEmailTo.value) ) {
			//	$('#'+obj.id+' .error5').show();
				$(obj.smEmailTo).addClass('error');
				obj.smEmailTo.focus();
				alert('Veuillez saisir une adresse email valide.');
				return false;
			}
			if ( !isMail(obj.smEmailTo.value) ) {
				//$('#'+obj.id+' .error6').show();
				$(obj.smEmailTo).addClass('error');
				obj.smEmailTo.focus();
				alert('Veuillez saisir une adresse email valide.');
				return false;
			}
		}
	}
	if ( isBlank(obj.smCaptcha.value) ) {
		//$('#'+obj.id+' .error3').show();
		$(obj.smCaptcha).addClass('error');
		obj.smCaptcha.focus();
		alert('Veuillez saisir le code captcha.');
		return false;
	}
	
	if ($("[name=optinPart]").size() > 0 && $("[name=optinPart]:checked").size() == 0) {
		//$('#'+obj.id+' .error7').show();
		alert("Veuillez définir si vous souhaitez recevoir les offres des partenaires de L\'argus® et des professionnels de l\'automobile.");
		return false;
	}
	$.post('/occasion3/sendMail.cfm?rand='+Math.random(),{action:3,captcha:$('#'+obj.id+' .captcha').val(),smCaptcha:$('#'+obj.id+' .smCaptcha').val()},function(data) {
		if (data==1) {
			if (obj.type.value!==1) {
				trackGaPush('Rech_occ','Contacts_Emails','Details_Annonces');
			}
			toggleLoading(0,obj);
			$.postForm('/occasion3/sendMail.cfm?rand='+Math.random(),obj,function (data) {
				toggleLoading(1,obj);
				if (data==1) {
					if ($('input[name="popup"]').val()==1) {
						parent.$.fn.colorbox.close();
					} else {
						$('.'+obj.id+'_Form').hide();
						$('.'+obj.id+'_Confirmation').show();
						$('.'+obj.id+'_Confirmation .mailReceiver').html(obj.smEmailTo.value);
						getCaptcha(obj.id);
						setTimeout('newMail("'+obj.id+'");',6000);
					}
				} else {
					alert('Echec lors de la tentative d\'envoi du mail...');
				}
			});
		} else {
			$('#'+obj.id+' .error4').show();
			$(obj.smCaptcha).val('');
			$(obj.smCaptcha).addClass('error');
			getCaptcha(obj.id);
			obj.smCaptcha.focus();
		}
	});
	return false;
}

function testConfirm() {
	obj = document.getElementById('fSendFriendMail');
	toggleLoading(1,obj);
	if (1==1) {
		if ($('input[name="popup"]').val()==1) {
			parent.$.fn.colorbox.close();
		} else {
			$('.'+obj.id+'_Form').hide();
			$('.'+obj.id+'_Confirmation').show();
			$('.'+obj.id+'_Confirmation .mailReceiver').html(obj.smEmailTo.value);
			getCaptcha(obj.id);
			//setTimeout('newMail("'+obj.id+'");',3000);
		}
	}
}
