/* FORMULAIRE CONTACT (ajax popup) */
var dialog = false;

function openDialogWCB()
{
	$('#DialogContactContainerWCB').dialog({
		resizable: false, draggable: false, modal: true,
		bgiframe: true, autoOpen: false, width: 720, height:680
	});
	$('#DialogContactContainerWCB').dialog('open')
}

function openDialog()
{
	if (!dialog)
	{
		dialog = true;
		$.ajax({
	url: "../ressources/html/FormulaireContact.html",
	cache: false,
	success: function(html){createDialog(html);
  }
});
	}
	else
	{
		ReInitDialog();
		$('#DialogContactForm').dialog('open');
	}	
}
function ReInitDialog()
{
	$('#DialogContactForm #contentform').show();
	$('#DialogContactForm #envoi_ok').hide();	
	$('#DialogContactForm #envoi_erreur').hide();
	$('#datamsg span').removeClass('highlight').html('<i>&nbsp;</i>');
	AddButtonEnvoyer();
}
function createDialog(html)
{
	$('#DialogContactContainer').append(html);
	$('#DialogContactForm').dialog({
		resizable: false, draggable: false, modal: true,
		bgiframe: true, autoOpen: false, width: 650
	});
	$('#datasujets #chk :input:checkbox')
		.click( function(){
				$('#eptica').val($('.ui-dialog #datasujets :input:radio[id=sujet3]').attr('checked', true).attr('eptica'));
		} );
	$('.ui-dialog #datasujets :input:radio')		
		.click( function(){
			$('#eptica').val($(this).attr('eptica'));
			var id = $(this).attr('id');
			var spanmsg = $('#datamsg span');
			if (id!='sujet3')
			{
				$('#datasujets #chk :input:checkbox').attr('checked', false);
			}
			var code = $(this).next('label').children('code');
			if (code.size()==1)
			{
				spanmsg.addClass('highlight').html( code.html() );
				return;
			}
			spanmsg.removeClass('highlight').html('<i>&nbsp;</i>');
		} );
	
	$('#dataperso').before('<input id="eptica" name="eptica" type="hidden">');
	
	$('#datamsg textarea').focus(function () {
			$('#datamsg span.highlight')
				.effect("highlight",{color:'#FFBD2B'},1500);
		} );

	ReInitDialog();

	$('#DialogContactForm').dialog('open');
}
function AddButtonEnvoyer()
{
	$('#DialogContactForm').dialog('option', 'buttons',
			{ "Envoyer": function() {
					if ( validatefields() )
					{
						ajaxmail();
					}
				}
			});

	embedButton();

}
function ajaxmail()
{
	var fields = $('#DialogContactForm :input').serializeArray();
	$.ajax(
		{
			type:'POST',
			url:"../rendering/post.aspx",
			data:fields,
			success:function(data, textStatus)
			{
				$('#DialogContactForm #envoi_ok #envoi_ok_bluestreak').html('<img alt="" src="http://s0b.bluestreak.com/ix.e?bb&t=40461" width="1" height="1" border="0" /><script type="text/javascript">bi_ref="Contact e-mail";bi_comment="Ma mutulle en ligne / Contact par e-mail";add_tag_roi();</script>');				
				bi_ref='Contact e-mail';
				bi_comment='Ma mutulle en ligne / Contact par e-mail';				
				add_tag_roi();
				
				$('#DialogContactForm #envoi_ok').show();
			},
			error:function (XMLHttpRequest, textStatus, errorThrown) {
				$('#DialogContactForm #envoi_erreur').show();
			},
			complete:function(textStatus) {
				$('#DialogContactForm #contentform').hide();				
				$('#DialogContactForm').dialog('option', 'buttons', { "Ok": function() { $('#DialogContactForm #envoi_ok #envoi_ok_bluestreak').html('');$(this).dialog("close"); } });
				embedButton();
			}
		});
}
function validatefields()
{
	var nom = $('#Nom');var prenom = $('#Prenom');var email = $('#Email');

	var bValid = (nom.size()>0) && (prenom.size()>0) && (email.size()>0);
	
	$([]).add(nom).add(email).add(prenom).add($('#datasujets #chk')).removeClass('ui-state-error');

	bValid = bValid && checkMandatory(nom,"nom");

	bValid = bValid && checkMandatory(prenom,"prenom");

	bValid = bValid && checkMandatory(email,"e-mail");

	bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,'Le format du champ E-mail est incorrect. Exemple: contact@reunica.com');

	bValid = bValid && checkAutresProduits();
	
	return bValid;
}

function checkAutresProduits()
{
	var id = $('#datasujets input:radio:checked').attr('id');
	var nbboxchk = $('#datasujets #chk :input:checkbox:checked').size();
	
	if ( (id=='sujet3') && (nbboxchk==0) )
	{
		$('#datasujets #chk').addClass('ui-state-error');
		updateTips("Cochez au moins un produit");
		return false;
	}
	return true;
}
function checkLength(o,n,min,max)
{	
	if ( (o.size()==0) || (o.val().length>max) || (o.val().length<min) )
	{
		o.addClass('ui-state-error');
		updateTips('La longueur du champ ' + n + ' doit etre entre '+min+' et '+max+'.');
		return false;
	}
	return true;
}
function checkMandatory(o,n)
{	
	if ( (o.size()==0) || (o.val().length==0) )
	{		
		o.addClass('ui-state-error');
		updateTips('Le champs ' + n + ' est obligatoire.');
		return false;
	}
	return true;
}
function checkRegexp(o,regexp,n)
{
	if ( (o.size()==0) || (!regexp.test(o.val())) )
	{
		o.addClass('ui-state-error');
		updateTips(n);
		return false;
	}
	return true;
}
function updateTips(t)
{
	$.scrollTo('#validateTips',400);
	$('#validateTips').text(t).effect('highlight',{color:'#cd0a0a'},1500);
}
function embedButton()
{
	$('.ui-dialog .ui-dialog-buttonpane :button')
		.before('<img src="../ressources/img/topmenu-g-btn.gif" width="5px" height="23px" />')
		.after('<img src="../ressources/img/topmenu-d-btn.gif" width="5px" height="23px" />');
}
