
	/* #############################################################
	Name: Niceforms
	Version: 0.9
	Author: Lucian Slatineanu
	URL: http://www.badboy.ro/
	
	Feel free to use and modify but please provide credits.
	############################################################# */
	
	// Global variables that can be used by all the functions on this page.
	var selects;
	
	var selectsDivGenerated = [];
	var optionsDivGenerated = [];
	
	var inputs;
	
	var radios = [];
	var radiosAreaGenerated = [];
	var radios_pY = [];
	
	var checkboxes = [];
	var checkboxesAreaGenerated = [];
	var checkboxes_pY = [];

	var hovers = [];
	var buttons = [];
	var selectText = "Choisir";
	
	//this function runs when the page is loaded so put all your other onload stuff in here too.
	function NiceForms_Init()
	{
		
		// Check if styles are enabled and only then start replacing elements
/*		if(document.getElementById('stylesheetTest'))
		{
			replaceSelects();
			replaceRadios();
			replaceCheckboxes();

		}
		if(typeof(Observe_WinScroll) == "function") window.setInterval("Observe_WinScroll('contenu-intern')", 10)
*/
		hoverEffects();
		buttonHovers();
	}
	
	function replaceRadios()
	{
		//get all the radio buttons on the page
		var inputs = document.getElementsByTagName('input');
		var j = 0;
		for(var i=0;i<inputs.length;i++)
		{
			if(inputs[i].type == 'radio')
			{
				radios[j] = inputs[i];
				++j;
			}
		}
		
		//cycle through the radio inputs
		for(var i=0;i<radios.length;i++)
		{
			// Make them transparent
			radios[i].className = "transparent";
			
			//get their position
			var x = findPosX(radios[i]);
			var y = radios_pY[radios_pY.length] = findPosY(radios[i]);
			
			//build new div
			var radioArea = radiosAreaGenerated[radiosAreaGenerated.length] = document.createElement('div');
			
			if(radios[i].checked)
			{
				radios[i].nextSibling.className = "radioChosen";
				radioArea.className = "radioAreaChecked";
			}
			else if(!radios[i].checked)
			{
				radioArea.className = "radioAreaUnchecked";
			}
			
			radioArea.style.left = x + 'px';
			radioArea.style.top = y + 'px';
			
			radioArea.id = 'myRadio'+i;
			radios[i].onclick = new Function('checkRadio('+i+')');
			
			//insert div
			document.getElementsByTagName("body")[0].appendChild(radioArea);
			
		}
	}
	
	function replaceCheckboxes()
	{
		//get all the checkboxes on the page
		var inputs = document.getElementsByTagName('input');
		var j = 0;
		for (var i2=0; i2 < inputs.length; i2++) {
			if(inputs[i2].type=='checkbox') {
				checkboxes[j] = inputs[i2];
				++j;
			}
		}
	
		//cycle through the checkboxes
		for(var i2=0;i2<checkboxes.length; i2++)
		{
	
			//make them transparent
			checkboxes[i2].className = "transparent";
	
			//get their position
			var x = findPosX(checkboxes[i2]);
			var y = checkboxes_pY[checkboxes_pY.length] = findPosY(checkboxes[i2]);
	
			//build new div
			var checkboxArea = checkboxesAreaGenerated[checkboxesAreaGenerated.length] = document.createElement('div');
			if(checkboxes[i2].checked)
			{
				checkboxes[i2].nextSibling.className = "chosen";
				checkboxArea.className = "checkboxAreaChecked";
			}
			else if(!checkboxes[i2].checked)
			{
				checkboxArea.className = "checkboxAreaUnchecked";
			}
			
			checkboxArea.style.left = x + 'px';
			checkboxArea.style.top = y + 'px';
			
			checkboxArea.id = 'myCheck'+i2;
			checkboxes[i2].onclick = new Function('checkCheck('+i2+')');
			
			
			checkboxArea.onclick = new Function('checkCheck('+i2+')');
	
			//insert div
			document.getElementsByTagName("body")[0].appendChild(checkboxArea);
		}
	}
	
	
	var NiceCombo = Class.create();
	
	NiceCombo.prototype =
	{
		
		initialize:function()
		{
			
		}
		
	}
	
	//N_Combo = new NiceCombo();
	
	// --->	  Generated HTML
	/*
	
	<div class="selectArea">
		<div class="left"></div>
		<div class="center" id="mySelectText[x]">Choisir</div>
		<div class="right"><a href="javascript:showOptions([x])"></a></div>
	</div>
	
	<div class="optionsDivVisible" id="optionsDiv[x]" style="left:534px; top:576px; visibility:visible;">
		<div class="optionsDivContainer" id="optionsDivContainer[x]">
			<div class="optionsDivContent" id="optionsDivContent[x]">
				<a href="javascript:showOptions([x], 'HideIt'); selectMe('pays',0,[x]);">Allemagne</a>
				<a href="javascript:showOptions([x], 'HideIt'); selectMe('pays',1,[x]);">Autriche</a>
				<a href="javascript:showOptions([x], 'HideIt'); selectMe('pays',2,[x]);">Belgique</a>
				<a href="javascript:showOptions([x], 'HideIt'); selectMe('pays',3,[x]);">Espagne</a>
				<a href="javascript:showOptions([x], 'HideIt'); selectMe('pays',4,[x]);">France</a>
				<a href="javascript:showOptions([x], 'HideIt'); selectMe('pays',5,[x]);">Italie</a>
				<a href="javascript:showOptions([x], 'HideIt'); selectMe('pays',6,[x]);">Liban</a>
				<a href="javascript:showOptions([x], 'HideIt'); selectMe('pays',7,[x]);">Luxembourg</a>
				<a href="javascript:showOptions([x], 'HideIt'); selectMe('pays',8,[x]);">Pays bas</a>
				<a href="javascript:showOptions([x], 'HideIt'); selectMe('pays',9,[x]);">Portugal</a>
				<a href="javascript:showOptions([x], 'HideIt'); selectMe('pays',10,[x]);">Suisse</a>
			</div>
		</div>
		<a class="arrowUp" id="arrowUp[x]" href="#"></a>
		<a class="arrowDown" id="arrowDown[x]" href="#"></a>
	</div>

	
	*/
	
	function replaceSelects()
	{
		//get all the select fields on the page
		selects = document.getElementsByTagName('select');
		
		//cycle trough the select fields
		for(var i=0;i<selects.length;i++)
		{
			
			//create and build div structure
			var selectArea = selectsDivGenerated[selectsDivGenerated.length] = document.createElement('div');
			selectArea.id = "selectArea"+i;

			var left = document.createElement('div');
			var right = document.createElement('div');
			var center = document.createElement('a');
			var button = document.createElement('a');
			
			var text = document.createTextNode(selectText);
			center.id = "mySelectText"+i;
			
			// Button
			center.href = button.href = "javascript:showOptions(" + i + ")";
			center.onfocus = button.onfocus = function(){this.blur();};
			
			selectArea.className = "selectArea";
			left.className = "left";
			right.className = "right";
			center.className = "center";
			
			right.appendChild(button);
			center.appendChild(text);
			
			selectArea.appendChild(left);
			selectArea.appendChild(center);
			selectArea.appendChild(right);
			
			//hide the select field
			selects[i].style.display = "none"; 
			
			//insert select div
			selects[i].parentNode.insertBefore(selectArea, selects[i]);



			// Build & place options div
			
			// Global container generation
			var optionsDiv = optionsDivGenerated[optionsDivGenerated.length] = document.createElement('div');
			optionsDiv.className = "optionsDivInvisible";
			optionsDiv.id = "optionsDiv" + i;
			
			optionsDiv.style.left = findPosX(selectArea) + 'px';
			optionsDiv.style.top = findPosY(selectArea) + 19 + 'px';
			
			// Global container generation
			var optionsDivContainer = document.createElement('div');
			optionsDivContainer.className = "optionsDivContainer";
			optionsDivContainer.id = "optionsDivContainer" + i;
			optionsDiv.appendChild(optionsDivContainer);
			
			// Global container generation
			var optionsDivContent = document.createElement('div');
			optionsDivContent.className = "optionsDivContent";
			optionsDivContent.id = "optionsDivContent" + i;
			optionsDivContainer.appendChild(optionsDivContent);
			
			// Get select's options and add to options div
			for(var j=0;j<selects[i].options.length;j++)
			{
				var optionHolder = document.createElement('p');
				var optionLink = document.createElement('a');
				var optionTxt = document.createTextNode(selects[i].options[j].text);
				optionLink.href = "javascript:showOptions(" + i + ", 'HideIt'); selectMe('" + selects[i].id + "'," + j + "," + i + ");";
				optionLink.appendChild(optionTxt);
				optionHolder.appendChild(optionLink);
				// Append to divContent
				optionsDivContent.appendChild(optionHolder);
				
			}
						
			document.getElementsByTagName("body")[0].appendChild(optionsDiv);
			
			// Size_Difference
			Size_Difference = optionsDivContent.offsetHeight - optionsDivContainer.offsetHeight;
			
			numRecord = optionsDivContent.getElementsByTagName('a'.toUpperCase()).length;
			lastRecord = optionsDivContent.getElementsByTagName('a'.toUpperCase())[numRecord-1];
			
			if(optionsDivContent.offsetHeight > optionsDivContainer.offsetHeight)
			{
				
				if(Size_Difference > lastRecord.offsetHeight)
				{
					
					var arrowUp = document.createElement('a');
					arrowUp.className = "arrowUp";
					arrowUp.id = "arrowUp"+i;
					optionsDiv.appendChild(arrowUp);
					
					var arrowDown = document.createElement('a');
					arrowDown.className = "arrowDown";
					arrowDown.id = "arrowDown"+i;
					optionsDiv.appendChild(arrowDown);
					
					// Get value to increment according to the difference
					inc = 1;
					for(j=0;j<10;j++) if(Size_Difference % j == 0) inc = j;
					
					// Mousewheel
					wl = 0;
					mwEvent = function(e)
					{
						if(!e) e = window.event;
						wl = (e.wheelDelta <= 0 || e.detail > 0)?inc:-inc;
						
						witness.innerHTML = wl;

						if(wl > 0) MoveAreaDown(optionsDivContent.id, optionsDivContainer.id, (wl));
						else MoveAreaUp(optionsDivContent.id, optionsDivContainer.id, (wl));
					}
					
/*					Event.observe((document.attachEvent)?document:window, (document.attachEvent)?"mousewheel":"DOMMouseScroll", mwEvent, false)
*/					
					// Set behavior
					arrowUp.onmousedown = new Function('PerformScroll("'+optionsDivContent.id+'", "'+optionsDivContainer.id+'", '+(-inc)+')');
					arrowDown.onmousedown = new Function('PerformScroll("'+optionsDivContent.id+'", "'+optionsDivContainer.id+'", '+inc+')');
					arrowUp.onmouseup = arrowDown.onmouseup = CeaseScroll;
					
					var witness = document.createElement('div');
					witness.id = "witness";
					witness.className = "witness";
					document.body.appendChild(witness);

				}
				// Adjust size - increase
				else
				{
					//
					optionsDiv.style.height = (optionsDiv.offsetHeight + Size_Difference) + "px";
					
					//
					optionsDivContainer.style.height = (optionsDivContainer.offsetHeight + Size_Difference) + "px";
					optionsDivContainer.style.clip = "rect(0 " + optionsDivContainer.offsetWidth + "px " + (optionsDivContainer.offsetHeight + Size_Difference) + "px 0)";
					
					//
					optionsDivContent.style.height = (optionsDivContent.offsetHeight + Size_Difference) + "px";
				}
			}
			// Adjust size - decrease
			else
			{
				optionsDiv.style.height = (optionsDiv.offsetHeight + Size_Difference) + "px";
				optionsDivContainer.style.height = (optionsDivContainer.offsetHeight + Size_Difference) + "px";
				/*optionsDivContent.style.height = (optionsDivContent.offsetHeight + Size_Difference) + "px";*/
			}
			
			
			Event.observe(document, "click", 
				function(e)
				{
					// Restaure
					if(Event.element(e).className != "arrowUp" && Event.element(e).className != "arrowDown")
					{
						for(i=0;i<optionsDivGenerated.length;i++)
						{
							optionsDivGenerated[i].className = "optionsDivInvisible";
						}
					}
				}
			);
			
/*			w = window.open();
			w.document.write(document.body.innerHTML);
*/
			
		}
	}
	
	var speed = 1 
	var loop, timer 
	
	function MoveArea(obj, x, y)
	{
		if(x != null) $(obj).style.left = x+"px"
		if(y != null) $(obj).style.top = y+"px"
	} 
	 
	function MoveAreaDown(obj, objContainer, move)
	{
		if($(obj).offsetTop > -$(obj).offsetHeight + ($(objContainer).offsetHeight))
		{ 
			MoveArea(obj, null, $(obj).offsetTop - move) 
			if(loop) setTimeout("MoveAreaDown('"+obj+"', '"+objContainer+"', "+move+")", speed)
		}
		/*
		else
		{
			MoveArea(obj, null, -($(obj).offsetHeight - $(objContainer).offsetHeight))
		}
		*/
	} 
	
	function MoveAreaUp(obj, objContainer, move)
	{
		if($(obj).offsetTop < 0)
		{
			MoveArea(obj, null, $(obj).offsetTop - move) 
			if(loop) setTimeout("MoveAreaUp('"+obj+"', '"+objContainer+"', "+move+")", speed)
		}
		/*
		else
		{
			MoveArea(obj, null, 0)
		}
		*/
	} 
	 
	function PerformScroll(obj, objContainer, speed)
	{
		loop=true; 
		if(speed > 0) MoveAreaDown(obj, objContainer, speed) 
		else MoveAreaUp(obj, objContainer, speed) 
	} 
	 
	function CeaseScroll(){ 
		loop=false 
		if(timer) clearTimeout(timer) 
	} 

	function showOptions(g, h)
	{
		elem = $("optionsDiv"+g);
		elem.className = (elem.className == "optionsDivVisible" || h == "HideIt")?"optionsDivInvisible":"optionsDivVisible";
	}
	
	function selectMe(selectFieldId, linkNo, selectNo)
	{
		//feed selected option to the actual select field
		selectField = $(selectFieldId);
		for(var k=0;k<selectField.options.length;k++)
		{
			if(k == linkNo)
			{
				selectField.options[k].selected = "selected";
			}
			else
			{
				selectField.options[k].selected = "";
			}
		}
		//show selected option
		textVar = $("mySelectText"+selectNo);
		var newText = document.createTextNode(selectField.options[linkNo].text);
		textVar.replaceChild(newText, textVar.childNodes[0]);
		
		if(typeof(Set_Onchange_Behavior) == "function")
		{
			Set_Onchange_Behavior();
		}
	}
	
	function findPosY(obj)
	{
		var posTop = 0;
		while (obj.offsetParent) {
			posTop += obj.offsetTop;
			obj = obj.offsetParent;
		}
		return posTop;
	}
	function findPosX(obj) {
		var posLeft = 0;
		while (obj.offsetParent) {
			posLeft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
		return posLeft;
	}
	
	function checkRadio(g) {
		if(radios[g].checked) {
			for (var k = 0; k < radios.length; k++)
			{
				if(k != g) {
					document.getElementById('myRadio'+k).className = "radioAreaUnchecked";
					radios[k].nextSibling.className = "radio";
				}
				else if(k == g) {
					document.getElementById('myRadio'+k).className = "radioAreaChecked";
					radios[g].nextSibling.className = "radioChosen";
				}
			}
		}
		else if(!radios[g].checked)
		{
			$('myRadio'+g).className = "radioAreaUnchecked";
			radios[g].nextSibling.className = "";
		}
	}
	
	function checkCheck(g)
	{
		if(checkboxes[g].checked)
		{
			for(var k=0;k<checkboxes.length;k++)
			{
				if(k == g)
				{
					$('myCheck'+k).className = "checkboxAreaChecked";
					if(checkboxes[g].nextSibling)
					{
						checkboxes[g].nextSibling.className = "chosen";
					}
				}
			}
		}
		else if(!checkboxes[g].checked)
		{
			$('myCheck'+g).className = "checkboxAreaUnchecked";
			
			if(checkboxes[g].nextSibling.tagName == "label".toUpperCase())
			{
				checkboxes[g].nextSibling.className = "checkbox";
			}
		}
	}
	
	function hoverEffects()
	{
		//get all elements (text inputs, passwords inputs, textareas)
		var elements = document.getElementsByTagName('input');
		var j = 0;
		for (var i4 = 0; i4 < elements.length; i4++) {
			if((elements[i4].type=='text')||(elements[i4].type=='password')) {
				hovers[j] = elements[i4];
				++j;
			}
		}
		elements = document.getElementsByTagName('textarea');
		for (var i4 = 0; i4 < elements.length; i4++) {
			hovers[j] = elements[i4];
			++j;
		}
		
		//add focus effects
		for (var i4 = 0; i4 < hovers.length; i4++) {
			hovers[i4].onfocus = function() {this.className += "Hovered";}
			hovers[i4].onblur = function() {this.className = this.className.replace(/Hovered/g, "");}
		}
	}
	
	function buttonHovers() {
		//get all buttons
		var elements = document.getElementsByTagName('input');
		var j = 0;
		for (var i5 = 0; i5 < elements.length; i5++) {
			if(elements[i5].type=='submit') {
				buttons[j] = elements[i5];
				++j;
			}
		}
		
		//add hover effects
		for (var i5 = 0; i5 < buttons.length; i5++) {
			buttons[i5].onmouseover = function() {this.className += "Hovered";}
			buttons[i5].onmouseout = function() {this.className = this.className.replace(/Hovered/g, "");}
		}
	}
	
	
