function ips_timer_close()
{
	var timer = parseInt($('#ips_wcb_waiting_time').html());
	if (timer <= 0)
	{
		$('body').unblock();
		$('#ips_wcb_form_contain_wcb_tab2').attr('style', 'display: none');
		$('#wcb_tab2').attr('style', 'display: none');
		$('#ips_wcb_form_contain_wcb_tab1').removeAttr('style');
		$('#wcb_tab1').removeAttr('style');
		$('#ips_wcb_form').removeAttr('style');
		$('#ips_wcb_form').attr('style', 'display: none');
		$('#ips_wcb_waiting_time').html(10);
	}
	else
	{
		timer -= 1;
		$('#ips_wcb_waiting_time').html(timer);
		setTimeout("ips_timer_close()",1000);
	}
}

function ips_phone_is_valid(div)
{
	if (div.val() != '')
	{
		if (div.val().match(/^[0-9]{10}$/))
		{
			div.attr('style', '-moz-box-shadow: 0 0 5px 4px #2DAD11;-webkit-box-shadow: 0 0 5px 4px #2DAD11;box-shadow: 0 0 5px 4px #2DAD11;');
		}
		else
		{
			div.attr('style', '-moz-box-shadow: 0 0 5px 4px #FF0A0A;-webkit-box-shadow: 0 0 5px 4px #FF0A0A;box-shadow: 0 0 5px 4px #FF0A0A;');
		}
	}
	else
		div.removeAttr('style');
}

function ips_center_form(div)
{
	var divw = div.width();
	var divh = div.height();
	var winw = $('#content').width();
	var winh = $(window).height();
	div.attr("style", 'position: absolute;top: ' + parseInt((winh / 2) - (divh / 2)) + 'px;left: ' + parseInt((winw / 2) - (divw / 2)) + 'px;');
}

$(window).ready(function(){
	// On verifie au chargement de la page et a chaque fois que l'input "phone" change, que le numero est valide oupa
	ips_phone_is_valid($('#ips_wcb_form_tab1_inp_phone'));
	$('#ips_wcb_form_tab1_inp_phone').keyup(function(){
		ips_phone_is_valid($(this));
	});
	
	// On fait apparaitre / disparaitre le formulaire de rappel
	$('#ips_wcb_container').click(function(){
		$('#ips_wcb_form').removeAttr('style');
		ips_center_form($('#ips_wcb_form'));
	});
	$('#ips_wcb_form_close').click(function(){
		$('#ips_wcb_form_contain_wcb_tab2').attr('style', 'display: none');
		$('#wcb_tab2').attr('style', 'display: none');
		$('#ips_wcb_form_contain_wcb_tab1').removeAttr('style');
		$('#wcb_tab1').removeAttr('style');
		$('#ips_wcb_form').removeAttr('style');
		$('#ips_wcb_form').attr('style', 'display: none');
	});
	
	// On envoie un rappel immediat
	$('#ips_wcb_form_tab1_inp_button').click(function(){
		$('body').block({message: $('#ips_wcb_waiting')});
		$.ajax({
			type: "POST",
			url: "webcallback/php/contact_ovh.php",
			data: "phone=" + $('#ips_wcb_form_tab1_inp_phone').val(),
			success: function(html){
				if (html != 'futur')
					$('body').block({message: $('#ips_wcb_' + html)});
				if (html == 'call')
					ips_timer_close();
				if (html == 'futur')
				{
					$('body').unblock();
					$('#ips_wcb_form_contain_wcb_tab2').removeAttr('style');
					$('#wcb_tab2').removeAttr('style');
					$('#ips_wcb_form_contain_wcb_tab1').attr('style', 'display: none');
					$('#wcb_tab1').attr('style', 'display: none');
					$('#ips_wcb_form_tab2_inp_phone').val($('#ips_wcb_form_tab1_inp_phone').val());
				}
			},
			error: function(jqXHR, errorType, errorText){
				alert(errorText);
			}
		});
	});
	
	// On envoie un rappel différé
	$('#ips_wcb_form_tab2_inp_button').click(function(){
		$.ajax({
			type: "POST",
			url: "webcallback/php/contact_differe.php",
			data: "phone=" + $('#ips_wcb_form_tab2_inp_phone').val() +
				  "&date=" + $('#ips_wcb_form_tab2_inp_date').val() +
				  "&horaire=" + $('#ips_wcb_form_tab2_inp_horaire').val() +
				  "&message=" + document.getElementById('ips_wcb_form_tab2_inp_message').value,
			success: function(html){
				$('body').block({message: $('#ips_wcb_' + html)});
				if(html == 'call')
					ips_timer_close();
			},
			error: function(jqXHR, errorType, errorText){
				alert(errorText);
			}
		});
	});
	
	// On ferme l'UI block
	$('.ips_wcb_close_ui').click(function(){
		$('body').unblock();
	});
	
	$('.ips_wcb_close_ui_all').click(function(){
		$('body').unblock();
		$('#ips_wcb_form_contain_wcb_tab2').attr('style', 'display: none');
		$('#wcb_tab2').attr('style', 'display: none');
		$('#ips_wcb_form_contain_wcb_tab1').removeAttr('style');
		$('#wcb_tab1').removeAttr('style');
		$('#ips_wcb_form').removeAttr('style');
		$('#ips_wcb_form').attr('style', 'display: none');
	});
});
