var $j = jQuery.noConflict();

var timSession = setInterval("testSession()", 600000);
testSession = function(){
	var strMsg = new String('');	
	var strRequest ='strAjaxDo='+encodeURIComponent('session');
	new Ajax.Request(
		'/pe_ajax.php',
		{
			method: 'post',
			postBody: strRequest,
			evalScripts: true,
			asynchronous: false,
			onSuccess: function (objResponse) {
				if(objResponse.responseText=='true'){
					return true;
				} else {
					alert('Fatal Error : SESSION');
					window.location.href='/index.php';
				}
			}
		}
	);	
}

openWaitSubmit = function(){
	$('w_visitor_form_go_wait').style.display = 'block';
	$('w_visitor_form_go_btn').style.display = 'none';
}
closeWaitSubmit = function(){
	$('w_visitor_form_go_wait').style.display = 'none';
	$('w_visitor_form_go_btn').style.display = 'block';
}
openModal = function(){
	$('w_layer').style.display = 'block';
	$('w_layer_border').style.display = 'block';
}
closeModal = function(){
	$('w_layer').style.display = 'none';
	$('w_layer_border').style.display = 'none';
	$('w_layer_content').update('');
}

testPromo = function(){
	success = true;
	strMsg = '';
	if($j('#condition').val() == ''){
		success = false;
		strMsg += 'La <strong>condition</strong> doit être spécifiée<br/>';
	}
	if($j('#gamme').val() == ''){
		success = false;
		strMsg += 'La <strong>gamme</strong> doit être spécifiée<br/>';
	}
	if($j('#habituelle').val() == '' && $j('#condition').val()!='Conditions achat spéciales salon'){
		success = false;
		strMsg += 'La <strong>remise habituelle</strong> doit être spécifiée<br/>';
	}
	if($j('#salon').val() == ''){
		success = false;
		strMsg += 'La <strong>remise salon</strong> doit être spécifiée<br/>';
	}
	if($j('#exposant_id').val() == ''){
		success = false;
		strMsg = 'Bug de fonctionnement';
	}
	
	if(!success){
		modalAlert(strMsg);
		return false;
	}
	else{
		return true;
	}
}

checkdisplayPromo = function(){
	if($j('#condition').val()!='Conditions achat spéciales salon'){
		$j('#remise_hab').show();
	}
	else{
		$j('#remise_hab').hide();
		$j('#habituelle').val('');
	}
}

updateChef = function(){
	if($j('#chef').val() != ''){
		$j.ajax({
			async:false,
			url : '/promo_ajax.php',
			data : ({
				mod : 'updatechef',
				chef : $j('#chef').val()
			}),
			success : function(){
				$j('#zone_offres').show();
			},
			type : 'POST'
		});
	}
	else{
		modalAlert('Le champ doit être spécifié');
	}
}

checkNbPromo = function(id){
	$j.ajax({
		async:false,
		url : '/promo_ajax.php',
		data : ({
			mod : 'checknbpromo',
			promo : id,
			condition : $j('#condition').val(),
			exposant_id : $j('#exposant_id').val()
		}),
		success : function(msg){
			if(msg == 'OK'){
				returner = true;
			}
			else{
				returner = false;
			}
		},
		type : 'POST'
	});
	return returner;
}

validPromo = function(id){
	if(testPromo()){
		if(checkNbPromo(id)){
			$j.ajax({
				url : '/promo_ajax.php',
				data : ({
					mod : 'validpromo',
					promo : id,
					condition : $j('#condition').val(),
					gamme : $j('#gamme').val(),
					habituelle : $j('#habituelle').val(),
					salon : $j('#salon').val(),
					exposant_id : $j('#exposant_id').val()
				}),
				success : function(){
					window.location.reload();
				},
				type : 'POST'
			});
		}
		else{
			modalAlert('Vous avez déjà saisi 5 offres pour cette condition');
		}
	}
}
managePromo = function(id){
	new Ajax.Updater(
		$('w_layer_content'),
		'/promo_ajax.php',
		{
			method:'post',
			postBody:'promo='+id+'&mod=managepromo',
			evalScripts:true,
			asynchronous:false
		}
	);	
	openModal();
	checkdisplayPromo();
}
delPromo = function(id){
	if(confirm('Voulez-vous confirmer la suppression de cette offre promotionnelle ?')){
		$j.ajax({
			url : '/promo_ajax.php',
			data : ({
				mod : 'delpromo',
				promo : id
			}),
			success : function(){
				window.location.reload();
			},
			type : 'POST'
		});
	}
}

validPortable = function(num){
	num = num.replace(' ','');
	num = num.replace('-','');
	Exp = new RegExp('^0[6-7]{1}[0-9]{8}$');
	if(Exp.test(num)){
		return true;
	}
	else{
		return false;
	}
}

rdv = function(id){
	new Ajax.Updater(
		$('w_layer_content'),
		'/rdv_ajax.php',
		{
			method:'post',
			postBody:'rdv='+id+'&mod=calendrier',
			evalScripts:true,
			asynchronous:false
		}
	);	
	openModal();
}
takeRdv = function(heure,date,from,to){
	new Ajax.Updater(
		$('container_visitor_id'),
		'/rdv_ajax.php',
		{
			method:'post',
			postBody:'heure='+heure+'&date='+date+'&mod=doselect',
			evalScripts:true,
			asynchronous:false
		}
	);	
	$j('#takeRdv > #heure').val(heure);
	$j('#takeRdv > h2 > #p_heure').html(heure);
	$j('#takeRdv > #date').val(date);
	$j('#takeRdv > h2 > #p_date').html(date);
	$j('#takeRdv > #from').val(from);
	$j('#takeRdv > #to').val(to);
	$j('#takeRdv').show();
}
validRdv = function(){
	if($j('#visitor_id').val()!=null && $j('#visitor_id').val()!=''){
		if(validPortable($j('#num_portable').val())){
			str_heure = $j('#takeRdv > #heure').val();
			str_date = $j('#takeRdv > #date').val();
			int_from = $j('#takeRdv > #from').val();
			int_to = $j('#takeRdv > #to').val();
			int_vis_id = $j('#visitor_id').val();
			int_num_portable = $j('#num_portable').val();
			
			$j.ajax({
				url : '/rdv_ajax.php',
				data : ({
					mod : 'validrdv',
					heure : str_heure,
					date : str_date,
					from : int_from,
					to : int_to,
					vis_id : int_vis_id,
					num_portable : int_num_portable
				}),
				success : function(msg){
					rdv(int_to);
					if(msg == 'Already'){
						$j('#takeRdv').html(
							'<h2>La tranche horaire que vous aviez sélectionné n\'est plus disponible.</h2>'+
							'<p>Veuillez sélectionner une autre tranche horaire</p>'
						);
					} else {
						$j('#takeRdv').html();
						alert('Merci, votre demande a bien été prise en compte.');
						window.location.reload(); 
					}
				},
				type : 'POST'
			});
		}
		else{
			modalAlert('Numéro de téléphone invalide');
		}
	}
	else{
		modalAlert('Prise de rendez-vous impossible : Aucun inscrit séléctionné pour ce rendez-vous');
	}
}

confirmRdv = function(id_){
	$j.ajax({
		url : '/rdv_ajax.php',
		data : ({
			mod : 'confirmrdv',
			id : id_
		}),
		success : function(){
			window.location.reload();
		},
		type : 'POST'
	});
}
cancelRdv = function(id_){
	$j.ajax({
		url : '/rdv_ajax.php',
		data : ({
			mod : 'cancelrdv',
			id : id_
		}),
		success : function(){
			window.location.reload();
		},
		type : 'POST'
	});
}




take_rdv = function(w_sponsor_id){
	new Ajax.Updater(
		$('w_layer_content'),
		'/take_rdv_ajax.php',
		{
			method:'post',
			postBody:'rdv='+w_sponsor_id+'&mod=calendrier',
			evalScripts:true,
			asynchronous:false
		}
	);	
	openModal();
}

doselect_take_rdv = function(heure,date,from,to){
	new Ajax.Updater(
		$('container_visitor_id'),
		'/take_rdv_ajax.php',
		{
			method:'post',
			postBody:'heure='+heure+'&date='+date+'&mod=doselect',
			evalScripts:true,
			asynchronous:false
		}
	);	
	$j('#takeRdv > #heure').val(heure);
	$j('#takeRdv > h2 > #p_heure').html(heure);
	$j('#takeRdv > #date').val(date);
	$j('#takeRdv > h2 > #p_date').html(date);
	$j('#takeRdv > #from').val(from);
	$j('#takeRdv > #to').val(to);
	$j('#takeRdv').show();
}


validrdv_take_rdv = function(){
	if($j('#visitor_id').val()!=null && $j('#visitor_id').val()!=''){
		if(validPortable($j('#num_portable').val())){
			str_heure = $j('#takeRdv > #heure').val();
			str_date = $j('#takeRdv > #date').val();
			int_from = $j('#takeRdv > #from').val();
			int_to = $j('#takeRdv > #to').val();
			int_vis_id = $j('#visitor_id').val();
			int_num_portable = $j('#num_portable').val();
			
			$j.ajax({
				url : '/take_rdv_ajax.php',
				data : ({
					mod : 'validrdv',
					heure : str_heure,
					date : str_date,
					from : int_from,
					to : int_to,
					vis_id : int_vis_id,
					num_portable : int_num_portable
				}),
				success : function(msg){
					rdv(int_to);
					if(msg == 'Already'){
						$j('#takeRdv').html(
							'<h2>La tranche horaire que vous aviez sélectionné n\'est plus disponible.</h2>'+
							'<p>Veuillez sélectionner une autre tranche horaire</p>'
						);
					} else {
						$j('#takeRdv').html();
						alert('Merci, votre demande a bien été prise en compte.');
						window.location.reload(); 
					}
				},
				type : 'POST'
			});
		}
		else{
			modalAlert('Numéro de téléphone invalide');
		}
	}
	else{
		modalAlert('Prise de rendez-vous impossible : Aucun inscrit séléctionné pour ce rendez-vous');
	}
}

confirmrdv_take_rdv = function(id_){
	$j.ajax({
		url : '/take_rdv_ajax.php',
		data : ({
			mod : 'confirmrdv',
			id : id_
		}),
		success : function(){
			window.location.reload();
		},
		type : 'POST'
	});
}
cancelrdv_take_rdv = function(id_){
	$j.ajax({
		url : '/take_rdv_ajax.php',
		data : ({
			mod : 'cancelrdv',
			id : id_
		}),
		success : function(){
			window.location.reload();
		},
		type : 'POST'
	});
}



