/*
	This is the JavaScript file for the AJAX Suggest Tutorial

	You may use this code in your own projects as long as this 
	copyright is left	in place.  All code is provided AS-IS.
	This code is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	
	For the rest of the code visit http://www.DynamicAJAX.com
	
	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.	

*/
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var ajaxReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function LoadPage(sSection) {
	if (ajaxReq.readyState == 4 || ajaxReq.readyState == 0) {
//alert(sSection + ' : ' + ValidFileExt(sSection));	
		// Vérifier les extensions à savoir si c est une section commune ou un fichier
//alert(sSection + ' : ' + ValidFileExt(sSection));
		if (! ValidFileExt(sSection)) {
			if (sSection.toLowerCase() == 'nouvelles') {
				var sUrlFile = '../microsites/www/nouvelles.htm';
			} else if (sSection.toLowerCase() == 'publications') {
				var sUrlFile = '../microsites/www/publications.htm';
			} else if (sSection.toLowerCase() == 'pourquoi') {
				var sUrlFile = '../microsites/www/dpmm.htm';
			} else if (sSection.toLowerCase() == 'confidentialite') {
				var sUrlFile = '../microsites/www/confidentialite.htm';
			} else if (sSection.toLowerCase() == 'traitementdesplaintes') {
				var sUrlFile = '../microsites/www/traitement-plaintes.htm';
			} else if (sSection.toLowerCase() == 'renseignementsgeneraux') {
				var sUrlFile = '../microsites/www/renseignements-generaux.htm';
			} else if (sSection.toLowerCase() == 'soumission') {
				var sUrlFile = '../microsites/www/formulaires/soumission.htm';
			} else if (sSection.toLowerCase() == 'soumission_ref') {
				var sUrlFile = '../microsites/www/formulaires/soumission_ref.htm';	
			} else if (sSection.toLowerCase() == 'reclamation333') {
				var sUrlFile = '../microsites/www/reclamation.htm';
			} else if (sSection.toLowerCase() == 'sinistre') {
				var sUrlFile = '../microsites/www/sinistre.htm';
			} else if (sSection.toLowerCase() == 'prevention') {
				var sUrlFile = '../microsites/www/prevention.htm';

			// ANGLAIS
			} else if (sSection.toLowerCase() == 'en_nouvelles') {
				var sUrlFile = '../microsites/www/en_nouvelles.htm';
			} else if (sSection.toLowerCase() == 'en_publications') {
				var sUrlFile = '../microsites/www/en_publications.htm';
			} else if (sSection.toLowerCase() == 'en_pourquoi') {
				var sUrlFile = '../microsites/www/en_dpmm.htm';
			} else if (sSection.toLowerCase() == 'en_confidentialite') {
				var sUrlFile = '../microsites/www/en_confidentialite.htm';
			} else if (sSection.toLowerCase() == 'en_traitementdesplaintes') {
				var sUrlFile = '../microsites/www/en_traitement-plaintes.htm';
			} else if (sSection.toLowerCase() == 'en_renseignementsgeneraux') {
				var sUrlFile = '../microsites/www/en_renseignements-generaux.htm';
			} else if (sSection.toLowerCase() == 'en_soumission') {
				var sUrlFile = '../microsites/www/formulaires/en_soumission.htm';
			} else if (sSection.toLowerCase() == 'en_reclamation333') {
				var sUrlFile = '../microsites/www/en_reclamation.htm';
			} else if (sSection.toLowerCase() == 'en_sinistre') {
				var sUrlFile = '../microsites/www/en_sinistre.htm';
			} else if (sSection.toLowerCase() == 'en_prevention') {
				var sUrlFile = '../microsites/www/en_prevention.htm';
			}
			
		} else if (ValidFileExt(sSection)) {
			var sUrlFile = sSection;
		} else {
			var sUrlFile = 'default.htm';
		}
//alert(sUrlFile + ' : ' + ValidFileExt(sUrlFile));
//alert(sUrlFile + ' : ' + ValidFileExt(sUrlFile) + ' : ' + CheckUrlExist('http://' + document.location.hostname + document.location.pathname + sUrlFile));
		if (ValidFileExt(sUrlFile)) {
//			externalLinks();
			ajaxReq.open('get', sUrlFile);
			ajaxReq.onreadystatechange = handleLoadPage;
			ajaxReq.send(null);
		}
	}		
}

//Called when the AJAX response is returned.
function handleLoadPage() {
	if (ajaxReq.readyState == 4) {
		var response = ajaxReq.responseText.replace(/\r?\n/ig, "");
		e = document.getElementById('middle');
		e.innerHTML = response.match(/<body>.*<\/body>/);
		// Source: http://livepipe.net/control/scrollbar
		// permet de remener le scroll de la page en haut et donc de voir le texte comme il faut lorsque nous changons d'affichage
		window.scrollTo(0,0);
	}
}

function ValidFileExt(sStringValidation) {
	var reg = new RegExp(/^[\S]+\.{1}[html|htm|html|asp|aspx|php|php4|php5]{3,4}[#-_a-zA-Z]*$/); //  /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
//	var reg = new RegExp(/^[a-zA-Z0-9]+.[html|htm|html|asp|aspx|php|php4|php5]{3,4}[#-_a-zA-Z]*$/); //  /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
	return reg.test(sStringValidation);
//	var emailRegEx = /^(\.(?i:)(?:jpg|gif|doc|pdf|htm|html))$/;
//	alert(sStringValidation.match(emailRegEx));
//	var reg = new RegExp( "\.(?i:)(?:jpg|gif|doc|pdf|htm|html)$" );
	/*alert(reg.test(sStringValidation));*/
}

function CheckUrlExist(UrlParse) {
	if (UrlParse != '') {
		obj = document.getElementById("resultat");
		// ON EFFACE LE CONTENU DU DUV
		if (obj) obj.innerHTML = "";
		if(window.XMLHttpRequest) // FIREFOX
			xhr_object = new XMLHttpRequest();
		else if(window.ActiveXObject) // IE
			xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
		else
		return(false);
	
		// ON APPELLE LA PAGE
		xhr_object.open("GET", UrlParse, true);
		xhr_object.send(null);
	
		// CONTROLE DE L'ETAT DE LA REQUETE
		// CHAQUE CHANGEMENT D'ETAT AFFICHE UNE LIGNE DANS NOTRE DIV
		xhr_object.onreadystatechange = function() {
			if(xhr_object.readyState == 1)
				if (obj) obj.innerHTML += "connexion établie<br />";
			if(xhr_object.readyState == 2)
				if (obj) obj.innerHTML+= "requête reçue<br />";
			if(xhr_object.readyState == 3)
				if (obj) obj.innerHTML += "réponse en cours<br />";
			if(xhr_object.readyState == 4) {
alert(UrlParse + ' : ' + xhr_object.status);
				return true;
				// ON CONTROLE LE STATUS (ERREUR 404, ETC)
				if(xhr_object.status == 200)
					if (obj) obj.innerHTML += "Fichier : " + xhr_object.responseText;
				else
					if (obj) obj.innerHTML +="Error code " + xhr_object.status;
				return true;
			}
		};
	}
}

function RemoveAccents(Texte){
	// Source: http://www.developpez.net/forums/d619957/webmasters-developpement-web/javascript/regex-remplacer-caracteres-speciaux-leurs-correspondants-normaux/
	var Accents = 'àâäéèêëïïöôûüùÉ';
	var NoAccen = 'aaaeeeeiioouuuE';
	Reg=new RegExp(Accents,'gi');
	Texte=Texte.replace(Reg,NoAccen);
	return Texte;
}  