﻿//On load page, init the timer which check if the there are anchor changes each 300 ms
$().ready(function(){
	setInterval("checkAnchor()", 100);
});
var currentAnchor = null;
//Function which chek if there are anchor changes, if there are, sends the ajax petition
function checkAnchor(){
	//Check if it has changes
	if(currentAnchor != document.location.hash){
		currentAnchor = document.location.hash;
		//if there is not anchor, the loads the default section
		if(!currentAnchor)
			query = "section=accueil";
		else
		{
			//Creates the  string callback. This converts the url URL/#main&id=2 in URL/?section=main&id=2
			
			var splits = currentAnchor.substring(1).split('&');
			//Get the section
			var section = splits[0];
			delete splits[0];
			//Create the params string
			var params = splits.join('&');
			var query = "section=" + section + params;
		}
		if (query == "section=accueil") 
		{
		var el = document.getElementById('acacher');
		el.style.display = "block";
		}
		else
		{
		var el = document.getElementById('acacher');
		el.style.display = "none";
		}
		//Send the petition
		$.get("callbacks.php",query, function(data){
			$("#switchcontenu").hide().html(data).fadeIn(2000);
		});
	}
}

function controle(form1) {
var titre = document.form1.titre.value;
var jour = document.form1.jour.value;
var mois = document.form1.mois.value;
var annee = document.form1.annee.value;
var type = document.form1.type_em.value;
window.location = "#emissions&type="+type+"&titre="+titre+"&a="+annee+"&m="+mois+"&j="+jour;
return false;	
}

function contact(titre,nom,prenom,mess,email,code)
{
	var OAjax;
	if (window.XMLHttpRequest) OAjax = new XMLHttpRequest();
	else if (window.ActiveXObject) OAjax = new ActiveXObject('Microsoft.XMLHTTP'); 
	OAjax.open('POST',"./scripts/signin.php",true);
	OAjax.onreadystatechange = function()
	{
		if (OAjax.readyState == 4 && OAjax.status==200)
		{
			if (document.getElementById) 
			{	
				if (OAjax.responseText =='true') {
				alert('Votre est message envoyé');
				document.location.href="#accueil";
				document.getElementById('msg').innerHTML='';		
				}else{
				document.getElementById('msg').innerHTML=OAjax.responseText;
				}
			}
		}
	}
	OAjax.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	OAjax.send('titre='+titre+'&nom='+nom+'&prenom='+prenom+'&mess='+mess+'&email='+email+'&code='+code);

}
function EnvMess(nom,prenom,mess,email,code)
{
	var OAjax;
	if (window.XMLHttpRequest) OAjax = new XMLHttpRequest();
	else if (window.ActiveXObject) OAjax = new ActiveXObject('Microsoft.XMLHTTP'); 
	OAjax.open('POST',"./script/mess.php",true);
	OAjax.onreadystatechange = function()
	{
		if (OAjax.readyState == 4 && OAjax.status==200)
		{
			if (document.getElementById) 
			{	
				if (OAjax.responseText =='true') {
				alert('Message envoyé');
				document.location.href="#accueil";
				document.getElementById('msg').innerHTML='';		
				}else{
				document.getElementById('msg').innerHTML=OAjax.responseText;
				}
			}
		}
	}
	OAjax.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	OAjax.send('nom='+nom+'&prenom='+prenom+'&mess='+mess+'&email='+email+'&code='+code);

}
function newsletter(email)
{
	var OAjax;
	if (window.XMLHttpRequest) OAjax = new XMLHttpRequest();
	else if (window.ActiveXObject) OAjax = new ActiveXObject('Microsoft.XMLHTTP'); 
	OAjax.open('POST',"./scripts/newsletter.php",true);
	OAjax.onreadystatechange = function()
	{
		if (OAjax.readyState == 4 && OAjax.status==200)
		{
			if (document.getElementById) 
			{	
				if (OAjax.responseText =='true') {
				alert('Vous êtes bien inscrit à la newsletter');
				document.getElementById('msg_newsletter').innerHTML='';		
				}else{
				document.getElementById('msg_newsletter').innerHTML=OAjax.responseText;
				}
			}
		}
	}
	OAjax.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	OAjax.send('email='+email);

}

