/************************************************************************************
Dynamic User Preference Script.
Copyright (C) 2005 Tony Aslett 
 
http://www.csscreator.com/
Version 1.6
updated 28 July 2005

This script enables users with JavaScript and cookies enabled to select 
different styles to be applied to your site.
For more info see http://www.csscreator.com/generator/userpref.php

Please leave this notice at the top of the script.
-----------------------------------------------------------------------------------------------------------------------------
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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.  See the
GNU General Public License for more details. http://www.gnu.org/copyleft/gpl.html
************************************************************************************/

/* This display the controls */
function displayPreferenceControls(){
	/* Check if this will work before displaying controls */
	if(navigator.cookieEnabled && document.styleSheets && (document.styleSheets[0].addRule || document.styleSheets[0].insertRule)){
		document.write(
		"<form id='userpref' name='userpref'>"+

/*		"<label for='backgroundcolor'>Couleur de fond:</label>"+*/"Couleur de fond: "+
		"<select name='backgroundcolor' id='backgroundcolor' onchange='addstyle(\"body\",this.options[this.selectedIndex].value, this.id+\"_\"+this.selectedIndex)'>"+
			"<option value='background-color:clear' >Défaut</option>"+
			"<option value='background-color:#FFF' style='background-color:#FFF'></option>"+
			"<option value='background-color:#FFF' style='background-color:#0F0'></option>"+
			"<option value='background-color:#6F0' style='background-color:#6F0'></option>"+
			"<option value='background-color:#CF0' style='background-color:#CF0'></option>"+
			"<option value='background-color:#FF0' style='background-color:#FF0'></option>"+
			"<option value='background-color:#0FF' style='background-color:#0FF'></option>"+
			"<option value='background-color:#6FF' style='background-color:#6FF'></option>"+
			"<option value='background-color:#CFF' style='background-color:#CFF'></option>"+
			"<option value='background-color:#F90' style='background-color:#F90'></option>"+
			"<option value='background-color:#000' style='background-color:#000'></option>"+
			"<option value='background-color:#600' style='background-color:#600'></option>"+
			"<option value='background-color:#C00' style='background-color:#C00'></option>"+
			"<option value='background-color:#060' style='background-color:#060'></option>"+
			"<option value='background-color:#660' style='background-color:#660'></option>"+
			"<option value='background-color:#C60' style='background-color:#C60'></option>"+
		"</select>"+

/*		"<label for='color'> / Couleur de texte:</label>"*/" / Couleur de texte: "+
		"<select name='color' id='color' onchange='addstyle(\"body\",this.options[this.selectedIndex].value, this.id+\"_\"+this.selectedIndex)'>"+
			"<option value='color:clear' >Défaut</option>"+
			"<option value='color:#FFF' style='color:#FFF'>Blanc</option>"+
			"<option value='color:#0F0' style='color:#0F0'>Vert</option>"+
			"<option value='color:#6F0' style='color:#6F0'>Vert jaunâtre</option>"+
			"<option value='color:#CF0' style='color:#CF0'>Jaune sombre</option>"+
			"<option value='color:#FF0' style='color:#FF0'>Jaune</option>"+
			"<option value='color:#0FF' style='color:#0FF'>Bleu</option>"+
			"<option value='color:#6FF' style='color:#6FF'>Cyan</option>"+
			"<option value='color:#CFF' style='color:#CFF'>Eau</option>"+
			"<option value='color:#F90' style='color:#F90'>Orange sombre</option>"+
			"<option value='color:#000' style='color:#000'>Noir</option>"+
			"<option value='color:#600' style='color:#600'>Brun sombre</option>"+
			"<option value='color:#C00' style='color:#C00'>Brun rougeâtre</option>"+
			"<option value='color:#060' style='color:#060'>Vert sombre</option>"+
			"<option value='color:#660' style='color:#660'>Olive sombre</option>"+
			"<option value='color:#C60' style='color:#C60'>Ocre</option>"+
		"</select>"+

/*		"<label for='fontsize'> / Grandeur du texte:</label>"*/" / Grandeur du texte: "+
		"<select name='fontsize' id='fontsize' onchange='addstyle(\"body\",this.options[this.selectedIndex].value, this.id+\"_\"+this.selectedIndex)'>"+
			"<option value='font-size:clear' >Défaut</option>"+
			"<option value= 'font-size:xx-small'>Très petit</option>"+
			"<option value= 'font-size:x-small'>Plus petit</option>"+
			"<option value= 'font-size:small'>Petit</option>"+
			"<option value= 'font-size:medium'>Moyen</option>"+
			"<option value= 'font-size:large'>Grand</option>"+
			"<option value= 'font-size:x-large'>Plus grand</option>"+
			"<option value= 'font-size:xx-large'>Très grand</option>"+
		"</select>"+
		
		" "+

		/* Below id the 'Write Your Own Style' section*/
		/* End of 'Write Your Own Style' section */
		
		"</form>"+
	
		"<a href=..//"#/" onclick=\"eatCookie('userpref')\">Styles par défaut</a>"
		);
	}else{
		/* if their browser supports JavaScript but not some of the functions or cookies are disabled then you can output alternative content here.*/
		document.write('Ici vous devriez trouver les contrôles de la couleur de fond et la couleur et grandeur des textes. Malheureusement votre programme ne supporte ou tolère pas Javascript, langue qui doit être installée pour pouvoir comprendre ces fonctionnalités.');
	}
}	

/* sets the selected value of each form element */
function setFormSelected(frmid, cookiename){
	var cookie=getCookie(cookiename); 						/* get existing cookie */
	var cookies=cookieCutter(cookie, "|"); 

	var frm=document.getElementById(frmid);
	if(testVar(frm)){
		var selects=frm.getElementsByTagName("SELECT");
		if(testVar(selects)){
			for(var sl=0; sl < selects.length; sl++){
			
				var opts=selects[sl].getElementsByTagName("OPTION");
				if(testVar(opts)){
					var oidx=findCookie(cookies, selects[sl].id);
					if(oidx){
						opts[oidx].selected="selected";
					}
						
				}
			}
		}
	}
}


function findCookie(cookies, fid){
	
	var found = false;	
	if(testVar(cookies)){
		for(var i=0; i < cookies.length; i++){
			var cookiecrumbs=cookies[i].split('[');
			if(testVar(cookiecrumbs[1])){
				chips=cookiecrumbs[1].split("_"); 
				if(testVar(chips[0])){
					if(fid == chips[0]){
						found=parseInt(chips[1]);					
					}
				}
				
			}

		}
	}
	return found;
}


/* Set the style now and add it to a cookie for later */
function addstyle(selector, rule, optidx){
	var name;
	var cookie;
	var expdate;
	var property;
	var valu;
	optidx= "["+optidx+"]";
	
	expdate=(24 * 60 * 60 * 1000 * 365);
	name='userpref';
	if(testVar(rule) && testVar(selector) ){
		valu=rule.split(":")[1];
		if(valu.indexOf("clear") >=0 ){
			property=rule.split(":")[0];
			cookie=biteCookie(name, selector, property, '|', optidx);
			if(testVar(cookie)){
				bakeCookie(name, cookie, expdate);
			}
			this.location.reload(false);	
		}else{
			setstyle(selector, rule); 
			addToCookieJar(name, selector, rule, '|', optidx);
		}
	}else{
		return false;
	}
}

/* removes one style from the cookie */
function biteCookie(name, selector, property, seperator, optidx){
	var sone;
	var stwo;
	var cookie;
	var cookies;
	var found;
	if(getCookie(name)){
		cookie=getCookie(name); 						/* get existing cookie */
		cookies=cookieCutter(cookie, seperator); 		/* get the cookies as an array */
		if(testVar(cookies)){
			for(var i=0; i < cookies.length; i++){
				cookiecrumbs=cookies[i].split('?');			/* split cookie into selector and rule */	
				if(testVar(cookiecrumbs[1])){
					chips=cookiecrumbs[1].split(":");  			/* split rule into property and value */
					if(chips[0]== property && cookiecrumbs[0] == selector){			
						found=i; 							/* Selector and property already exist */	
						cookies.splice( found, 1);
						cookie=cookies.join(seperator);	
						return cookie;						
					}
				}
			}
		}
		
		return cookie;
	}
	return false;
}

/* Set the style now and add it to a cookie for later of a full rule entered by user */
function anyRule(userRule){
	if(testVar(userRule)){
		userRule.slice(0,userRule.indexOf("}"));
		tokens=userRule.split("{");
		setstyle(tokens[0], tokens[1]); 
		addToCookieJar('userpref', tokens[0], tokens[1], '|', "[]"  );
	}
}

/* Dynamically sets the selected style */
function setstyle(selector, rule){
	var rulecount;
	var fullrule;
	var ssheet;
	ssheet=(document.styleSheets.length==0)? document.createStyleSheet(): document.styleSheets[0];
	if( testVar(selector)  && testVar(rule) && rule.indexOf(":")  ){		
		if(ssheet.insertRule){	/* DOM */
			rulecount=ssheet.cssRules.length;
			fullrule=selector +"{"+ rule +"}";	
			ssheet.insertRule(fullrule, rulecount++ );
		}else if(document.styleSheets[0].addRule){ /* IE */
			rulecount=ssheet.rules.length;
			if(selector.indexOf(",") >0){
				selectors=selector.split(",");
				for(var s=0; s< selectors.length; s++){
					ssheet.addRule(selectors[s], rule, rulecount++);
				}
			}else{
				ssheet.addRule(selector, rule, rulecount++);	
			}
		}
		return true;
	}else{
		return false;
	}
}
/* test to see if a varable has an assigned value */
function testVar(objToTest) {
	return (objToTest == null || objToTest == undefined || objToTest == false)? false : true;
}

/* Store Cookie */
function bakeCookie(name, data, usebydate){
	var today=new Date();
	today.setTime(today.getTime() +usebydate);  
	usebydate=today.toGMTString();	
	document.cookie = name + "=" + escape(data)+ "; expires=" + usebydate +  "; path=/";
    //alert("Cookie length: "+data.length);
}

/* get the cookie and add style */
function iceCookie(name, seperator){
	if(navigator.cookieEnabled && document.styleSheets && (document.styleSheets[0].addRule || document.styleSheets[0].insertRule)){
		var vanilla;
		var crumbs;
		var opti;
		vanilla=getCookie(name);
		if(testVar(vanilla)){
			crumbs=cookieCutter(vanilla, seperator);
			for(var x=0; x< crumbs.length; x++){
				opti=crumbs[x].split("[");
				chips=opti[0].split("?");
				setstyle(chips[0],chips[1]);			
			}
		}
	}else{
		return false;
	}
}

/* get the full Cookie from the cookie jar*/
function getCookie(name) {
	var start;
	start=0;
	thisCookie = document.cookie.split("; ")
	for (i = 0; i < thisCookie.length; i++) {
		if (name == thisCookie[i].split("=")[0]) {
			chocchip=unescape(thisCookie[i].split("=")[1]);		
			return chocchip;
		}
	}
	return false;
}

/* break up the cookies */
function cookieCutter(cookie, seperator){
	return (testVar(cookie) && cookie.length >1 && cookie.indexOf(seperator) >0)? cookie.split(seperator): false;
}

function addToCookieJar(name, selector, rule, seperator, optidx){
	var found;
	var cookie;
	var cookies;
	var mix;
	var expdate;
	expdate=(24 * 60 * 60 * 1000 * 365);
	if(testVar(name) && testVar(selector) && testVar(rule) && testVar(seperator)){
		if(!getCookie(name)){
			bakeCookie(name, selector +"?"+rule, expdate); 	/* new cookie */
		}else{			
			cookie=biteCookie(name, selector, rule.split(":")[0], seperator);
			if(testVar(cookie)){
				mix=cookie+seperator+selector+"?"+rule+optidx;			
			}
			bakeCookie(name, mix, expdate); 				/*rewrite cookie */
		}
	}else{
		return false;
	}
}

/*Remove Cookies and refresh to clear styles */
function eatCookie(name){
	var yesterday=0-(24 * 60 * 60 * 1000)	
	bakeCookie(name, "", yesterday); 					/*remove cookie */
	this.location.reload(false);								/* reload to clear styles */
}

/* on page load read and set the cookie styles */
window.onload=function(){
	iceCookie('userpref', '|');
	setFormSelected('userpref', 'userpref'); 
}