/*---------------------------------------------------------------------------------------------------
$ // Raccourci pour avoir document.getElementById
---------------------------------------------------------------------------------------------------*/
//function $(id){
//	return document.getElementById(id);
//}

/*---------------------------------------------------------------------------------------------------
getElementsByClassName // Retourne tout les élément d'une certaine classe
ex1: getElementsByClassName(document, "a", "className");
ex2: getElementsByClassName(document, "*", "className2");
---------------------------------------------------------------------------------------------------*/
//function getElementsByClassName(el, strTagName, strClassName){
//	var arrElements = (strTagName == "*" && el.all)? el.all : el.getElementsByTagName(strTagName);
//	var arrReturnElements = new Array();
//	strClassName = strClassName.replace(/\-/g, "\\-");
//	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
//	var oElement;
//	for(var i=0; i<arrElements.length; i++){
//		oElement = arrElements[i];		
//		if(oRegExp.test(oElement.className)){
//			arrReturnElements.push(oElement);
//		}	
//	}
//	return (arrReturnElements)
//}

/*---------------------------------------------------------------------------------------------------
getElementsByAttribute // Permet d'avoir tout les élément avec un certain attribut, et si vous voulez avec une valeur pour cette attribut
ex1: getElementsByAttribute(document.body, "*", "id");
ex2: getElementsByAttribute(document.getElementById("the-form"), "input", "type", "text");
---------------------------------------------------------------------------------------------------*/
//function getElementsByAttribute(el, strTagName, strAttributeName, strAttributeValue){
//	var arrElements = (strTagName == "*" && el.all)? el.all : el.getElementsByTagName(strTagName);
//	var arrReturnElements = new Array();
//	var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
//	var oCurrent;
//	var oAttribute;
//	for(var i=0; i<arrElements.length; i++){
//		oCurrent = arrElements[i];
//		oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
//		if(typeof oAttribute == "string" && oAttribute.length > 0){
//			if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
//				arrReturnElements.push(oCurrent);
//			}
//		}
//	}
//	return arrReturnElements;
//}


/*---------------------------------------------------------------------------------------------------
getStyle // Permet d'avoir le style css rendu d'un élément
ex1: getStyle(document.getElementById("container"), "font-size");
---------------------------------------------------------------------------------------------------*/
//function getStyle(el, strCssRule){
//	var strValue = "";
//	if(document.defaultView && document.defaultView.getComputedStyle){
//		strValue = document.defaultView.getComputedStyle(el, "").getPropertyValue(strCssRule);
//	}
//	else if(el.currentStyle){
//		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
//			return p1.toUpperCase();
//		});
//		strValue = el.currentStyle[strCssRule];
//	}
//	return strValue;
//}


/*---------------------------------------------------------------------------------------------------
addClass // Ajoute une classe à un élément
---------------------------------------------------------------------------------------------------*/
//function addClass(el, strClassName){
//	var strCurrentClass = el.className;
//	if(!new RegExp(strClassName, "i").test(strCurrentClass)){
//		el.className = strCurrentClass + ((strCurrentClass.length > 0)? " " : "") + strClassName;
//	}
//}


/*---------------------------------------------------------------------------------------------------
removeClass // Supprime une classe d'un élément
---------------------------------------------------------------------------------------------------*/
//function removeClass(el, strClassName){
//	var oClassToRemove = new RegExp((strClassName + "\s?"), "i");
//	el.className = el.className.replace(oClassToRemove, "").replace(/^\s?|\s?$/g, "");
//}


/*---------------------------------------------------------------------------------------------------
changeClass // Change une classe en une autre classe
---------------------------------------------------------------------------------------------------*/
//function changeClass(el,startClass,endClass) {
//	var classNameString = el.className;
//	var newClassName = classNameString.replace(startClass,endClass);
//	el.className = newClassName;
//}


/*---------------------------------------------------------------------------------------------------
addLoadEvent // Ajoute une fonction à la liste de fonction à executé lors de l'événement onload
---------------------------------------------------------------------------------------------------*/
function addLoadEvent(func) {
	if ( Prototype ) {
		Prototype.onReady(func);
	} else {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
			};
		}
	}
}

/*---------------------------------------------------------------------------------------------------
addUnloadEvent // Ajoute une fonction à la liste de fonction à executé lors de l'événement onUnload
---------------------------------------------------------------------------------------------------*/
function addUnloadEvent(func) {
  var oldonload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  } else {
    window.onunload = function() {
      oldonload();
      func();
    }
  }
}


/*---------------------------------------------------------------------------------------------------
insertAfter // Ajoute un élément après un autre élément
---------------------------------------------------------------------------------------------------*/
//function insertAfter(newElement,targetElement) {
//  var parent = targetElement.parentNode;
//  if (parent.lastChild == targetElement) {
//    parent.appendChild(newElement);
//  } else {
//    parent.insertBefore(newElement,targetElement.nextSibling);
//  }
//}


/*---------------------------------------------------------------------------------------------------
getNextElement // Retourne l'élément suivant d'un élément
---------------------------------------------------------------------------------------------------*/
//function getNextElement(node) {
//  if(node.nodeType == 1) {
//	return node;
//  }
//  if (node.nextSibling) {
//    return getNextElement(node.nextSibling);
//  }
//  return null;
//}

/*---------------------------------------------------------------------------------------------------
chgSrc // Change the flash in the right column
---------------------------------------------------------------------------------------------------*/
//function chgSrc(el, newsrc) {
//  
//}


/*---------------------------------------------------------------------------------------------------
initRollOverImg // Créer des rollover selon le nom d'une classe et l'extension des images rollover, preload les images également
ex1: initRollOverImg(el,"rollover","rollover_on", "_on");
---------------------------------------------------------------------------------------------------*/
function initRollOverImg(containerElement,startClassName,endClassName,rollOverSuffix, activateName){
	$(containerElement).descendants().each(function(el) {
		var startSrc = el.readAttribute('src');
		if ( !startSrc ) return;
		if ( !el.hasClassName(startClassName) ) return;
		var startSrcLen = startSrc.length;
		var filePath    = startSrc.substring(0,startSrcLen-4);
		var fileExt     = startSrc.substring(startSrcLen-4); 
		var rollSrc     = filePath + rollOverSuffix + fileExt;
		el.rollSrc = rollSrc;
		el.startSrc = startSrc;
		
		if ( el.hasClassName(activateName) ) {
			el.writeAttribute("src", el.rollSrc);
			el.addClassName(endClassName);
		} else {
			//MouseOver
			el.observe('mouseover', function(event) {
				var el = event.element();
				el.writeAttribute('src', el.rollSrc);
				el.removeClassName(startClassName);
				el.addClassName(endClassName);
			});
//			//MouseOut
			el.observe('mouseout', function(event) {
				var el = event.element();
				el.writeAttribute('src', el.startSrc);
				el.removeClassName(endClassName);
				el.addClassName(startClassName);
			});
			//Preloading
			var refPreloading = new Element('img', { src: rollSrc } );
		}
	});
}


/*---------------------------------------------------------------------------------------------------
getHTTPObject // retourne un objet pour faire une connexion XMLHttpRequest
---------------------------------------------------------------------------------------------------*/
//function getHTTPObject(){
//	if(window.ActiveXObject){
//		var objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
//	}else if (window.XMLHttpRequest){
//		var objXMLHttp = new XMLHttpRequest();
//	}else{
//		var objXMLHttp = false;
//	}
//	return objXMLHttp;
//}

/*---------------------------------------------------------------------------------------------------
toggleVisibility // Initialise les forms vdaemon (ajoute 
---------------------------------------------------------------------------------------------------*/
//function toggleVisibility(containerElement,startAsHidden,classContainer,classHideBtn,classShowBtn,classToggleBtn,classContent,classContentSwitch){
//	if (!document.getElementById) return false;
//	var containers = getElementsByClassName(containerElement, "*", classContainer);
//	if(containers.length <= 0)return;
//	for(var i=0; i<containers.length; i++){
//		var container = containers[i];
//		var hideBtn   = getElementsByClassName(container, "*", classHideBtn)[0];
//		var showBtn   = getElementsByClassName(container, "*", classShowBtn)[0];
//		var toggleBtn = getElementsByClassName(container, "*", classToggleBtn)[0];
//		var content   = getElementsByClassName(container, "*", classContent)[0];
//		if(classContentSwitch != null){
//			var contentSwitch = getElementsByClassName(container, "*", classContentSwitch)[0];
//		}else{
//			var contentSwitch = false;
//		}
//		
//		if(startAsHidden){
//			if(showBtn)showBtn.style.display = 'block';
//			if(hideBtn)hideBtn.style.display = 'none';
//			if(content)content.style.display = 'none';
//			if(contentSwitch)contentSwitch.style.display = 'block';
//		}else{
//			if(showBtn)showBtn.style.display = 'none';
//			if(hideBtn)hideBtn.style.display = 'block';
//			if(content)content.style.display = 'block';
//			if(contentSwitch)contentSwitch.style.display = 'none';
//		}
//		
//		//showBtn
//		if(showBtn){
//			showBtn.hideBtn = hideBtn;
//			showBtn.content = content;
//			showBtn.contentSwitch = contentSwitch;
//			showBtn.onclick = function(){
//				this.style.display = 'none';
//				this.hideBtn.style.display = 'block';
//				this.content.style.display = 'block';
//				if(this.contentSwitch)this.contentSwitch.style.display = 'none';
//			}
//		}
//		
//		//hideBtn
//		if(hideBtn){
//			hideBtn.showBtn = showBtn;
//			hideBtn.content = content;
//			hideBtn.contentSwitch = contentSwitch;
//			hideBtn.onclick = function(){
//				this.style.display = 'none';
//				this.showBtn.style.display = 'block';
//				this.content.style.display = 'none';
//				if(this.contentSwitch)this.contentSwitch.style.display = 'block';
//			}
//		}
//		
//		//toggleBtn
//		if(toggleBtn){
//			toggleBtn.content = content;
//			toggleBtn.contentSwitch = contentSwitch;
//			toggleBtn.onclick = function(){
//				if(this.content.style.display == 'none'){
//					this.content.style.display = 'block';
//					if(this.contentSwitch)this.contentSwitch.style.display = 'none';
//				}else if(this.content.style.display == 'block'){
//					this.content.style.display = 'none';
//					if(this.contentSwitch)this.contentSwitch.style.display = 'block';
//				}
//				
//			}
//		}
//	}
//}

/*---------------------------------------------------------------------------------------------------
initGlossary
This function initialize the tooltips on the links which have the right class. It calls the glossary
page and parse its content to get the tooltip.
---------------------------------------------------------------------------------------------------*/
//function initGlossary(containerElement,className){
//	if (!document.getElementsByTagName) return false;
//	if (!document.getElementById) return false;
//	if (!containerElement.getElementsByTagName) return false;
//	var els = getElementsByClassName(containerElement, 'a', className);
//	
//	for(var i=0; i < els.length; i++){	
//		var alink = els[i];
//		alink.linkto = alink.getAttribute("href");
//		var pos = alink.linkto.indexOf('#');
//		var url = alink.linkto.substr(0,pos);
//		alink.param = alink.linkto.substr(pos+1, alink.linkto.length);
//		
//		if(!url || !alink.param) continue;
//		
//		alink.onmouseover = function(){
//			//faire apparaitre info-bulle
//			obj = getHTTPObject();
//			//alert(this.param);
//			obj.open("GET", url+"?action=tooltip&param="+this.param, true);
//			obj.onreadystatechange = alink.showToolTip;
//			obj.send(null);
//		}
//		alink.onmouseout = function(){
//			//faire disparaitre info-bulle
//			hidetrail();
//		}
//		
//		alink.showToolTip = function(){
//			if(obj.readyState > 0){
//				if(obj.readyState == 4){
//					if(obj.responseText == 'error'){
//						return false;
//					}else{
//						//show the tooltip
//						showtrail('', obj.responseText);
//					}			
//				}
//			}
//		}
//	}
//}

/*---------------------------------------------------------------------------------------------------
initCalculateForm
This function initialize the calculation of percentage and total for forms.
---------------------------------------------------------------------------------------------------*/
function initCalculateForm(className,totalElement,pourcentElement,inputClass,mainTotalElement){
	var forms = $$("form." + className);
	forms.each(function(form) {
		var formId = Element.identify(form);
		var fields = $$('#' + formId + ' input.' + inputClass);
		var pourcent = $$('#' + formId + ' div.' + pourcentElement).first();
		var total = $$('#' + formId + ' div.' + totalElement).first();
		if ( !pourcent || !total || fields.size() == 0 ) return false;

		fields.each(function(field) {
			if ( field.checked ) { 
				field.checked = false;
			}
			Element.observe(field,'click',function(event) {
				var el = event.element();
				var t = 0;
				var count = fields.inject(0, function(acc, field) {
					if ( field.checked ) {
						t += parseInt($F(field) || 0);
						acc++;
					}
					return acc;
				});
				var fieldCount = fields.size()/6;
				Element.update(total,t);
				Element.update(pourcent,Math.round((t/(fieldCount))*100/5) + '%');
				if ( $(mainTotalElement) ) {
					recalculateTotal();
				} else {
					total[count>=fieldCount?'show':'hide']();
				}
			});
		});
		Element.hide(total);
		
		function recalculateTotal() {
			var mainTotalId = Element.identify(mainTotalElement);
			var bigTotal = 0;
			//var bigPourcent = 0;
			var btEl = $$('#' + mainTotalId + " span." + totalElement).first();
			var bpEl = $$('#' + mainTotalId + " span." + pourcentElement).first();
			
			forms.each(function(form) {
				var total = $$('#' + Element.identify(form) + " div." + totalElement).first();
				var pourcent = $$('#' + Element.identify(form) + " div." + pourcentElement).first();
				bigTotal += parseInt(total.innerHTML);
			});
			Element.update(btEl,bigTotal);
			Element.update(bpEl,(bigTotal/forms.size()) + '%');	
		}
	});
}

/*---------------------------------------------------------------------------------------------------
html_entity_decode
This script and many more are available free online at The JavaScript Source!! http://javascript.internet.com Created by: Ultimater | http://webdeveloper.com/forum/member.php?u=30185 
---------------------------------------------------------------------------------------------------*/
//function html_entity_decode(str) {
//  var ta=document.createElement("textarea");
//  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
//  return ta.value;
//}

//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************

//function initToggleVisibility(){
//	document.write("\n<style>\n<!--\n");
//	document.write(".toggleHideContent{display:none;}\n");
//	document.write(".contentHide{display:none;}\n");
//	document.write(".jsOnlyContent{display:block !important;}\n");
//	document.write("-->\n</style>\n");
//	addLoadEvent(prepareToggleVisibility);
//}

//function prepareToggleVisibility(){
//	var el = document.getElementById("wrapper");
//	toggleVisibility(el,true,"toggleHide","hideBtn","showBtn","toggleBtn","toggleHideContent");
//	toggleVisibility(el,false,"toggleShow","hideBtn","showBtn","toggleBtn","toggleShowContent");
//	toggleVisibility(el,false,"toggleContent","hideBtn","showBtn","toggleBtn","contentShow","contentHide");
//}

function prepareRollOverImg(){
	var el = document.getElementById("wrapper");
	initRollOverImg(el,"rollover","rollover_on", "_on", "activate");
}

//function prepareGlossary(){
//	var el = document.getElementById("content");
//	initGlossary(el,"tooltip");
//}

function prepareCalculateForm(){
	//var el = document.getElementById("calculate");
	initCalculateForm("calculate","total","pourcent","radio","mainTotal");
}

//initToggleVisibility();
addLoadEvent(prepareRollOverImg);
//addLoadEvent(prepareGlossary);
addLoadEvent(prepareCalculateForm);
