// JavaScript Document
var TipsX3 = new Class({
	Implements: [Events, Options],

	options: { // modded for X3
		onShow: function(tip){
			tip.setStyle('visibility', 'visible');
		},
		onHide: function(tip){
			tip.setStyle('visibility', 'hidden');
		},
		maxTitleChars: 60,
		showDelay: 100,
		hideDelay: 100,
		className: 'tool',
		offsets: {'x': 16, 'y': 16},
		fixed: false,
		loadingText: 'Loading...',
		errTitle: 'Oops..',
		errText: 'There was a problem retrieving the tooltip.'
	},

	initialize: function(elements, options){
		this.setOptions(options);
		this.toolTip = new Element('div', {
			'class': this.options.className + '-tip',
			'styles': {
				'position': 'absolute',
				'top': '0',
				'left': '0',
				'visibility': 'hidden'
			}
		}).inject(document.body);
		this.wrapper = new Element('div').inject(this.toolTip);
		$$(elements).each(this.build, this);
		if (this.options.initialize) this.options.initialize.call(this);
	},

	build: function(el){ // modded for X3
		el.myTitle = (el.href && el.get('tag') == 'a') ? el.href.replace('http://', '') : (el.rel || false);
		if (el.title){

			// check if we need to extract contents from a DOM element
			if (el.title.test('^DOM:', 'i')) {
				el.title = $(el.title.split(':')[1].trim()).innerHTML;
			}

			// check for an URL to retrieve content from
			if (el.title.test('^AJAX:', 'i')) {
				el.title = this.options.loadingText + '::' + el.title;
			}

			var dual = el.title.split('::');
			if (dual.length > 1) {
				el.myTitle = dual[0].trim();
				el.myText = dual[1].trim();
			} else {
				el.myText = el.title;
			}

			el.removeAttribute('title');
		} else {
			el.myText = false;
		}
		if (el.myTitle && el.myTitle.length > this.options.maxTitleChars) el.myTitle = el.myTitle.substr(0, this.options.maxTitleChars - 1) + "&hellip;";
		el.addEvent('mouseenter', function(event){
			this.start(el);
			if (!this.options.fixed) this.locate(event);
			else this.position(el);
		}.bind(this));
		if (!this.options.fixed) el.addEvent('mousemove', this.locate.bindWithEvent(this));
		var end = this.end.bind(this);
		el.addEvent('mouseleave', end);
		el.addEvent('trash', end);
	},

	start: function(el){ // modded for X3
		this.wrapper.empty();

		// check if we have an AJAX request - if so, show a loading animation and launch the request
		if (el.myText && el.myText.test('^AJAX:', 'i')) {
			//if (this.ajax) this.ajax.cancel();
			this.ajax = new Request ({url: el.myText.replace(/AJAX:/i,''),
				onComplete: function (responseText, responseXML) {
					el.title = responseText;
					this.build(el);
					this.start(el);
					}.bind(this),
				onFailure: function () {
					el.title = this.options.errTitle + '::' + this.options.errText;
					this.build(el);
					this.start(el);
					}.bind(this),
				method: 'get'
				}).send();
			el.myText = '<div class="' + this.options.className + '-loading">&nbsp;</div>';
		}

		if (el.myTitle){
			this.title = new Element('span').inject(
				new Element('div', {'class': this.options.className + '-title'}).inject(this.wrapper)
			).set('html', el.myTitle);
		}
		if (el.myText){
			this.text = new Element('span').inject(
				new Element('div', {'class': this.options.className + '-text'}).inject(this.wrapper)
			).set('html', el.myText);
		}
		$clear(this.timer);
		this.timer = this.show.delay(this.options.showDelay, this);
	},

	end: function(event){
		$clear(this.timer);
		this.timer = this.hide.delay(this.options.hideDelay, this);
	},

	position: function(element){
		var pos = element.getPosition();
		this.toolTip.setStyles({
			'left': pos.x + this.options.offsets.x,
			'top': pos.y + this.options.offsets.y
		});
	},

	locate: function(event){
		var win = {'x': window.getWidth(), 'y': window.getHeight()};
		var scroll = {'x': window.getScrollLeft(), 'y': window.getScrollTop()};
		var tip = {'x': this.toolTip.offsetWidth, 'y': this.toolTip.offsetHeight};
		var prop = {'x': 'left', 'y': 'top'};
		for (var z in prop){
			var pos = event.page[z] + this.options.offsets[z];
			if ((pos + tip[z] - scroll[z]) > win[z]) pos = event.page[z] - this.options.offsets[z] - tip[z];
			this.toolTip.setStyle(prop[z], pos);
		};
	},

	show: function(){
		if (this.options.timeout) this.timer = this.hide.delay(this.options.timeout, this);
		this.fireEvent('onShow', [this.toolTip]);
	},

	hide: function(){
		this.fireEvent('onHide', [this.toolTip]);
	}

});

TipsX3.implement(new Events, new Options);

// - verification du nombre de case coch�e
nb_check = 0;
function Testcheck(el) {
	if(el.checked) {
		if(nb_check >= 3) return false;
		nb_check++;
		return true;
	}
	else {
		nb_check--;
		return true;
	}
}

// - Gestion de l'activation
function change_value(id, value)
{
	var divID = id;
	Pdiv = document.getElementById( id );
	Pdiv.value = value;
}

// - Gestion de l'effet apparaitre ou non
function see1(div1)
{
	var divID = div1;
	Pdiv = document.getElementById( divID );
	Pdiv.className = ( Pdiv.className.match('cachediv')) ? Pdiv.className.replace(/cachediv/,"") : Pdiv.className+' '+'cachediv';
}

function displayHeb( sId )
{
	$$('ul.cachediv.underul').each( function(elem)
		{
			elem.setStyle('display','none');
		}
	);

	if( $chk(sId) && $(sId) )
		$(sId).setStyle('display','block');
		
	// add validate required class on date field
	if( $('date_deb_heberg') && !$('date_deb_heberg').hasClass( "validate['required']" ) )
		$('date_deb_heberg').addClass( "validate['required']" );
		
	if( $('date_fin_heberg') && !$('date_fin_heberg').hasClass( "validate['required']" ) )
		$('date_fin_heberg').addClass( "validate['required']" );
	
	if( $('hebergement_adultes') && !$('hebergement_adultes').hasClass( "validate['required']" ) )
		$('hebergement_adultes').addClass( "validate['required']" );
		
	if( $('hebergement_enfants') && !$('hebergement_enfants').hasClass( "validate['required']" ) )
		$('hebergement_enfants').addClass( "validate['required']" );
		
	post_form_contact.register( $('date_deb_heberg') );
	post_form_contact.register( $('date_fin_heberg') );
	post_form_contact.register( $('hebergement_adultes') );
	post_form_contact.register( $('hebergement_enfants') );
}

function see_broch(div1,div2)
{
	var divID = div1;
	Pdiv = document.getElementById( divID );
	Pdiv.className = ( Pdiv.className.match('cachediv')) ? Pdiv.className.replace(/cachediv/,"cachediv") : Pdiv.className+' '+'cachediv';

	var divID = div2;
	Pdiv = document.getElementById( divID );
	Pdiv.className = ( Pdiv.className.match('cachediv')) ? Pdiv.className.replace(/cachediv/,"") : Pdiv.className;
}

function see_t(div1)
{
	for(x = 1;x <= 99; x++)
	{
		var divTMP = 'typbc_' + x;
		if(document.getElementById( divTMP ))
		{
			tESt = document.getElementById( divTMP );
			tESt.className = ( tESt.className.match('cachediv')) ? tESt.className : tESt.className+' '+'cachediv';
		}
	}

	var divID = div1;
	Pdiv = document.getElementById( divID );
	Pdiv.className = ( Pdiv.className.match('cachediv')) ? Pdiv.className.replace(/cachediv/,"") : Pdiv.className+' '+'cachediv';
}

function cache1(div1)
{
	var divID = div1;
	Pdiv = document.getElementById( divID );
	Pdiv.className = ( Pdiv.className.match('cachediv')) ? Pdiv.className.replace(/cachediv/,"cachediv") : Pdiv.className+' '+'cachediv';
}

// - Gestion de la contrainte des dates
//<![CDATA[
function makeTwoChars(inp) {
	return String(inp).length < 2 ? "0" + inp : inp;
}

var initAttempts = 0;
function setReservationDates(e) {
	// - Internet Explorer will not have created the datePickers yet so we poll the datePickerController Object using a setTimeout
	// - until they become available (a maximum of ten times in case something has gone horribly wrong)
	
	try {
		var sd = datePickerController.getDatePicker("date_deb_heberg");
		var ed = datePickerController.getDatePicker("date_fin_heberg");
		var sj = datePickerController.getDatePicker("date_deb_sejour");
		var ej = datePickerController.getDatePicker("date_fin_sejour");	
	} catch (err) {
		if(initAttempts++ < 10) setTimeout("setReservationDates()", 50);
		return;
	}
	
	// - Check the value of the input is a date of the correct format
	var dt = datePickerController.dateFormat(this.value, sd.format.charAt(0) == "m");
	var dj = datePickerController.dateFormat(this.value, sj.format.charAt(0) == "m");
	
	// - If the input's value cannot be parsed as a valid date then return
	if(dt == 0) return;
	if(dj == 0) return;
	
	// - At this stage we have a valid YYYYMMDD date
	
	// - Grab the value set within the endDate input and parse it using the dateFormat method
	// - N.B: The second parameter to the dateFormat function, if TRUE, tells the function to favour the m-d-y date format
	var edv = datePickerController.dateFormat(document.getElementById("date_fin_heberg").value, ed.format.charAt(0) == "m");
	var edj = datePickerController.dateFormat(document.getElementById("date_fin_sejour").value, ej.format.charAt(0) == "m");
	
	// - Set the low range of the second datePicker to be the date parsed from the first
	ed.setRangeLow( dt );
	ej.setRangeLow( dj );
	
	// - If theres a value already present within the end date input and it's smaller than the start date
	// - then clear the end date value
	if(edv < dt) {
			document.getElementById("date_fin_heberg").value = "";
	}
	if(edj < dj) {
			document.getElementById("date_fin_sejour").value = "";
	}
}

function removeInputEvents() {
	// - Remove the onchange event handler set within the function initialiseInputs
	datePickerController.removeEvent(document.getElementById("date_deb_heberg"), "change", setReservationDates);
	datePickerController.removeEvent(document.getElementById("date_deb_sejour"), "change", setReservationDates);
}

// - Gestion de l'affichage des champs pour l'age des enfants
function afficherAutre(type)
{
	if (type == "h")
	{
		var nombr = parseInt(document.broch.hebergement_enfants.value, 10);
		var nombr2 =  document.broch.hebergement_enfants.value + 1 ;
	} else {
		var nombr = parseInt(document.broch.sejour_enfants.value, 10);
		var nombr2 =  document.broch.sejour_enfants.value + 1 ;
	}
	
	if (parseInt(nombr + 1, 10) > 16)
	{
		Pdiv = document.getElementById(type + "_enfants");
		Pdiv.className = Pdiv.className.replace(/cachediv/,"");
	} else {
		Pdiv = document.getElementById(type + "_enfants");
		Pdiv.className = Pdiv.className+' '+'cachediv';
	}
	
	//for (i = 21; i > nombr; i--) {
	var oElementInput = null;
	for (i = 17; i <= 22; i++) {
		document.getElementById(type + "_enfant" + i).style.display = "none";
		
		// remove element to formcheck selections and remove class
		var aElementInput = $$('#'+type + "_enfant" + i + ' input');
		var oElementInput = aElementInput[0];
		oElementInput.className = oElementInput.className.replace(/validate\['required'\]/,"");
	}	
	
	for (i = 17; i <= parseInt(nombr + 1) ; i++) {
		document.getElementById(type + "_enfant" + i).style.display = "block";	
		
		// add element to formcheck selections and remove class
		var aElementInput = $$('#' + type + "_enfant" + i + ' input');
		var oElementInput = aElementInput[0];
		oElementInput.addClass( "validate['required']" );
		post_form_contact.register( oElementInput );
	}
}

if( 
	document.getElementById("date_deb_heberg") != null 
	&& document.getElementById("date_fin_heberg") != null 
	&& document.getElementById("date_deb_sejour") != null 
	&& document.getElementById("date_fin_sejour") != null 
)
{
	datePickerController.addEvent(window, 'load', initialiseInputs);
	datePickerController.addEvent(window, 'unload', removeInputEvents);
}

//]]>
