// GLA RUSTINE CORRECTIVE SUR MAUVAIS PARAMETRE CODLSC --> ???
var myhref=document.location.href;
if (myhref.indexOf("CODLSC=0") > 0) {
	var reg = new RegExp("(&CODLSC=0)", "g");
	var newurl = myhref.replace(reg, "");
	document.location = newurl;
}
var cookieName = 'villages';

function compare(url) {
		var compareForm = document.getElementById("compareForm");
		var inpts = compareForm.getElementsByTagName("input");
		var nbCheckChecked = 0;
		for(i=0; i < inpts.length; i++){
			if ((inpts[i].type == "checkbox") && inpts[i].checked){
				nbCheckChecked++;
			}
		}
		for (j=1; j <4; j++) {
			var hiddeSel = document.getElementById("selVil" + j);
			var hiddechk = document.getElementById("chk" + j);
			if (hiddeSel && hiddechk) {
				if (hiddechk.checked){
					hiddeSel.value = 'on';
				}
				else{
					hiddeSel.value = 'off';
				}
			}
		}
		if (nbCheckChecked < 2){
			var chaine=FormValidator.getMsg('minChecked').substitute({minChecked:'2'});
			var reg=new RegExp("(<br />)", "gi");
			//On remplace lors de l'affichage les <br/> par des \n
			alert(chaine.replace(reg,"\n"));
		}
		else{
			compareForm.action = url;
			compareForm.submit();
		}
	}

function writeCompareBloc(nbVillages, strVillages, urlCompare, urlVillage, strSelec, langue, pays, libHeader, libComparer) {
	document.write('<div class="block blockShadowRight Vmargin">');
	document.write('	<span class="topCorners">');
	document.write('		<span class="tl"></span>');
	document.write('		<span class="tr"></span>');
	document.write('	</span>');
	document.write('	<span class="sideL"></span>');
	document.write('	<span class="sideR"></span>');
	document.write('	<div class="blockInside color6">');
	document.write('		<h2 class="TpaddingSm minHeightTitle taille14 upper txtC colorWhite u_bgColor6 Prelative">');
	document.write('			<span class="cufon-heavy">' + libHeader + '</span>');
	document.write('		</h2>');
	document.write('		<div class="body bgWhite padding">');
	
	document.write('			<form id="compareForm" name="compareForm" method="post"  action="javascript:compare(\''+urlCompare+'\');">');
	document.write('				<input type="hidden" name="LANG" value="' + langue + '">');
	document.write('				<input type="hidden" name="PAYS" value="' + pays + '">');
	document.write('				<ul class="Nmargin">');
	
	strVillages = strVillages.substr(1, strVillages.length); // On supprime le 1er '$' pour ne pas rater le split
	var tVillages = strVillages.split('$');
	for (var i=0; i<nbVillages; i++) { 
		var tVillage = tVillages[i].split('/');
		var vCode = tVillage[0];
		var vLib = tVillage[1];
		var vPays = tVillage[2];
		var nameHiddx = 'vil'+(i+1);
		var nameHiddxSel = 'selVil'+(i+1);
		var nameChkbx = 'chk'+(i+1);
		document.write('					<li class="liste_20">');
		document.write('						<label>');
		document.write('							<span class="u_color taille11 upper bold LpaddingSm"><a href="'+urlVillage+'&CODLSC='+vCode+'">'+vLib+'</a></span><br/>');
		document.write('							<span>' + vPays + '</span>');
		document.write('						</label><br/>');
		document.write('						<input type="hidden" id ="' + nameHiddx + '" name="' + nameHiddx + '" value="'+vCode+'" />');
		document.write('						<input type="hidden" id ="' + nameHiddxSel + '" name="' + nameHiddxSel + '" value="off" />');
		document.write('						<input type="checkbox" class="floatL" id ="' + nameChkbx + '" name="' + nameChkbx + '"/>');
		document.write('						<p class="BpaddingSm upper">' + strSelec + '</p>');
		document.write('					</li>');
	}
	
	document.write('				</ul>');
	document.write('				<div class="txtC Tmargin HpaddingXxLg">');
	document.write('					<span class="btn submit_4">');
	document.write('						<span class="u_color">');
	document.write('							<input type="submit" value="' + libComparer + '"/>');
	document.write('						</span>');
	document.write('					</span>');
	document.write('				</div>');
	document.write('			</form>');
	document.write('		</div>');
	document.write('	</div>');
	document.write('	<span class="sideB"></span>');
	document.write('	<span class="bottomCorners">');
	document.write('		<span class="bl"></span>');
	document.write('		<span class="br"></span>');
	document.write('	</span>');
	document.write('</div>');
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*60*60*24*365));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	//alert("nameCookie= "+document.cookie.length);
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function checkVillagesCookie(vilCode, vilLib, vilPays, urlCompare, urlVillage, strSelec, langue, pays, libHeader, libComparer) {
	var villagesInCookie = readCookie(cookieName);
	var villageInfos = '$'+vilCode+'/'+vilLib+'/'+vilPays;

	if (villagesInCookie == null) {
		createCookie(cookieName, escape(villageInfos), 0);
	} else {
		villagesInCookie = unescape(villagesInCookie);
		// Verifier que le village n'est pas deja dans le cookie
		var bInCookie = false;
		if (villagesInCookie.indexOf(vilCode) > -1) {
			bInCookie = true;
		}
		
		// Calcul du nombre de villages dans la chaine sauvegardee dans le cookie
		var nbVillages = 0;
		var pos = villagesInCookie.indexOf('$');
		while (pos !=-1) {
			nbVillages++;
			pos = villagesInCookie.indexOf('$', pos+1);
		}
		
		if (bInCookie == false) {
			if (nbVillages == 3) {
				// S'il y a deja 3 villages dans le cookie, supprimer le dernier avant de rajouter le nouveau
				villagesInCookie = villagesInCookie.substr(0, villagesInCookie.lastIndexOf('$'));
			} else {
				nbVillages++;
			}
			villagesInCookie = villageInfos + villagesInCookie;
			createCookie(cookieName, escape(villagesInCookie), 0);
		}
		
		if (nbVillages > 1) {
			writeCompareBloc(nbVillages, villagesInCookie, urlCompare, urlVillage, strSelec, langue, pays, libHeader, libComparer);
		}
	}
}