function isArray(variable)
{
	return variable instanceof Array;
}

function emptyArray(a, len)
{
	for (var i = 0; i <= len; i++) {
		a[i] = null;
	}
	a.length = 0;
	return a;
}

function insertListe(lst,txt,val,skipline)
{
	try
	{
		insertpos=-1;
		o=new Option(txt,val);
		if(lst.options.length>0)
		{
			decale=false;
			if(skipline==null || skipline<=0) skipline=0;
			for(i=skipline;i<lst.options.length;i++)
			{
				//lst.selectedIndex=i;
				if(!decale)
				{
					if(txt<lst.options[i].text)
					{
						//Insertion
						b=new Option(lst.options[i].text,lst.options[i].value);
						lst.options[i]=o;
						o=b;
						decale=true;
						insertpos=i;
					}
					else
					{
						//On continue
					}
				}
				else
				{
					//Decalage
					b=new Option(lst.options[i].text,lst.options[i].value);
					lst.options[i]=o;
					o=b;
				}
			}
			lst.options[lst.options.length]=o;
		}
		else
		{
			insertpos=0;
			lst.options[lst.options.length]=o;
		}
		//lst.selectedIndex=insertpos;
	}
	catch(o)
	{
		alert("An exception occurred in the script. Error name: " + e.name + ". Error message: " + e.message)
		return;
	}
}

function bookingSynchronizeStations(f,defaultTo)
{
	//var selectValue;
	var optFrom = document.getElementById("fromstation").options;
	var optTo = document.getElementById("tostation").options;
	var lstTo = document.getElementById("tostation");

	if(optFrom.selectedIndex >= 0) {
		fromStation = optFrom[optFrom.selectedIndex].value;
		toStations = booking_stationsMap[fromStation];

		if(isArray(toStations))
		{
			var selectD = document.getElementById("tostation");
			if(document.getElementById("tgb_separator_d")) selectD.removeChild(document.getElementById("tgb_separator_d"));

			if(toStations.length == 1){
				optTo = emptyArray(optTo, optTo.length);
				toStation = toStations[0];
				insertListe(lstTo,booking_stations[toStation],toStation,0);
			}
			if(toStations.length > 1) {
				optTo = emptyArray(optTo, optTo.length);
				insertListe(lstTo,utf8_decode (label_tostation),"",0);
				optIndex = 1;
				for (var i = 0; i < toStations.length; i++) {
					toStation = toStations[i];
					if(toStations[i]!="TGB")insertListe(lstTo,booking_stations[toStation],toStation,optIndex);
				}
				if(toStations[i-1]=="TGB"){
					new_element_optgroup = document.createElement("optgroup");
					new_element_optgroup.id = "tgb_separator_d";
					new_element_optgroup.label = "----------------------------";


					new_element_opt = document.createElement("option");
					new_element_opt.innerHTML = booking_stations['TGB']; //"Trajet Toute gare Belge";
					new_element_opt.value = "TGB";

					selectD.appendChild(new_element_optgroup);
					selectD.appendChild(new_element_opt);
				}

				//optIndex = 1;
				optTo = document.getElementById("tostation").options;
				for (var i = 0; i < optTo.length; i++) {
					toStation = optTo[i].value;
					if (toStation == defaultTo) {
						//alert(defaultTo);
						optTo.selectedIndex = i;
						break;
					}
					//optIndex++;
				}
			}
		}
	}
//	alert('test');
	bookingSynchronizeTKL(f);
	synchronizeBookingCalendar();
	checkTklEnabled("stations");
	showHideTgb();
}

var body_load = 0;
function bookingSynchronizeBookingTimeTrip(f,defaultTime,defaultTimeRet)
{
	var optFrom = document.getElementById("fromstation").options;
	var optTo = document.getElementById("tostation").options;
	var optTime = document.getElementById("time_trip").options;
	if(document.getElementById("time_trip_ret")) {
		var optTimeRet = document.getElementById("time_trip_ret").options;
	} else {
		var optTimeRet = 0;
	}

	station = optFrom[optFrom.selectedIndex].value+optTo[optTo.selectedIndex].value;
	times = thalys_night_time_trip[station];

	optIndex = 0;
	if(isArray(times))
	{
		optTime = emptyArray(optTime, optTime.length);
		for (var i = 0; i < times.length; i++) {
			time = times[i];
			//alert(time);
			val = time+':00';
			opt = new Option(time,val);
			optTime[optIndex++] = opt;
			if (val == defaultTime) {
				optTime.selectedIndex = optIndex-1;
			}
		}
	}

	station = optTo[optTo.selectedIndex].value+optFrom[optFrom.selectedIndex].value;
	times_ret = thalys_night_time_trip_ret[station];

	optIndex = 0;
	if(isArray(times_ret))
	{
		optTimeRet = emptyArray(optTimeRet, optTimeRet.length);
		for (var i = 0; i < times_ret.length; i++) {
			time_ret = times_ret[i];
			//alert(times);
			val = time_ret+':00';
			opt = new Option(time_ret,val);
			optTimeRet[optIndex++] = opt;
			if (val == defaultTimeRet) {
				optTimeRet.selectedIndex = optIndex-1;
			}
		}
	}
	bookingSynchronizeTKL(f);
	synchronizeBookingCalendar();
	synchronizeBookingComfort();
	body_load = 1;

	checkTklEnabled("stations");
	showHideTgb();

}
var booking_init = false;
var booking_init_neige = false;
var new_input = true;

function bookingInit()
{
	var optFrom = document.getElementById("fromstation").options;
	var optTo = document.getElementById("tostation").options;
	booking_init = booking_season_stations.inArray(optFrom[optFrom.selectedIndex].value) || booking_season_stations.inArray(optTo[optTo.selectedIndex].value);
	booking_init_neige = booking_neige_stations.inArray(optFrom[optFrom.selectedIndex].value) || booking_season_stations.inArray(optTo[optTo.selectedIndex].value);
	synchronizeDays();

}

function synchronizeBookingCalendar()
{
	var val_id_booking = document.getElementById("booking_id").value;
	var oneway = document.getElementById("oneway").value;
	var val_oneway = 'ar';
	if(booking_always_check.inArray(val_id_booking))
	{
		booking_init = '';
	}
	booking_init = '';

	if(booking_id_season.inArray(val_id_booking)) return;

	var optFrom = document.getElementById("fromstation").options;
	var optTo = document.getElementById("tostation").options;
	var val_o = optFrom[optFrom.selectedIndex].value;
	var val_d = optTo[optTo.selectedIndex].value;

	var optTimeTrip = document.getElementById("time_trip").options;
	var optTimeTripRet = document.getElementById("time_trip_ret").options;
	var val_time_trip = optTimeTrip[optTimeTrip.selectedIndex].value;
	var val_time_trip_ret = optTimeTripRet[optTimeTripRet.selectedIndex].value;
	var val_input_date_trip_year = document.getElementById("input_date_trip_year").value;
	var val_input_date_trip_month = document.getElementById("input_date_trip_month").value;
	var val_input_date_trip_day = document.getElementById("input_date_trip_day").value;

	var val_input_date_trip_ret_year = document.getElementById("input_date_trip_ret_year").value;
	var val_input_date_trip_ret_month = document.getElementById("input_date_trip_ret_month").value;
	var val_input_date_trip_ret_day = document.getElementById("input_date_trip_ret_day").value;

	if( document.getElementById("input_date_trip_year").value !="" && document.getElementById("input_date_trip_month").value != "" && document.getElementById("input_date_trip_day").value != "")
	{
		var date_trip_val = document.getElementById("input_date_trip_year").value+"-"+document.getElementById("input_date_trip_month").value+"-"+document.getElementById("input_date_trip_day").value;
	} else {
		var date_trip_val = '';
	}

	if( document.getElementById("input_date_trip_ret_year").value !="" && document.getElementById("input_date_trip_ret_month").value != "" && document.getElementById("input_date_trip_ret_day").value != "")
	{
		var date_trip_ret_val = document.getElementById("input_date_trip_ret_year").value+"-"+document.getElementById("input_date_trip_ret_month").value+"-"+document.getElementById("input_date_trip_ret_day").value;
	} else {
		var date_trip_ret_val = '';
	}

	if(oneway.checked == true)
	{
		val_oneway = 'as';
	}


	var booking_season = booking_season_stations.inArray(optFrom[optFrom.selectedIndex].value) || booking_season_stations.inArray(optTo[optTo.selectedIndex].value);
	var booking_neige = booking_neige_stations.inArray(optFrom[optFrom.selectedIndex].value) || booking_neige_stations.inArray(optTo[optTo.selectedIndex].value);

	if(body_load == 1)
	{
		var qs = (location.href.indexOf("?") < 0)? '' : location.href.substring(location.href.indexOf("?")+1, location.href.length);
		var a = unescape(qs).split("&");
		url = (location.href.indexOf("?") < 0)? location.href : location.href.substring(0,location.href.indexOf("?"));
		started = false;
		for (i=0;i<a.length;i++)
		{

			if (a[i].substring(0,a[i].indexOf("=")) != 'o' && a[i].substring(0,a[i].indexOf("=")) != 'd' && a[i]!="")
			{
				if(started==false){
					url = url+"?" ;

					started=true;
				}
				else url = url+"&";

				url = url+ a[i];
			}
		}


		if(optFrom[optFrom.selectedIndex].value !=""){
			if(url.indexOf("?") < 0) url += "?";
			else url += "&";
			url = url+ "o="+optFrom[optFrom.selectedIndex].value;
		}
		if(optTo[optTo.selectedIndex].value !=""){
			if(url.indexOf("?") < 0) url += "?";
			else url += "&";
			url = url+ "d="+optTo[optTo.selectedIndex].value;
		}

		if((booking_init != booking_season) ||(typeof(booking_init) != typeof(booking_season))) {

			if( document.getElementById("booking_resa_dates"))
			{
				if( val_o !="" && val_d != "")
				{
					booking_init = booking_season_stations.inArray(optFrom[optFrom.selectedIndex].value) || booking_season_stations.inArray(optTo[optTo.selectedIndex].value);
					var url = "/";
					var ajax_val = 'bookingCalendar';
					var updateDiv = 'booking_resa_dates';
					var val_lg = document.getElementsByName("lg")[0].value;
					var val_id_booking = document.getElementById("booking_id").value;
					var val_country = country;
					ajaxDate = new Date();
					new Ajax.Updater(
					updateDiv,
					url,
					{
						method: 'get',
						parameters: {id_booking: val_id_booking,lg: val_lg,country: val_country, ajax: ajax_val, ts: ajaxDate.getTime(), o: val_o, d: val_d, date_trip: date_trip_val, date_trip_year: val_input_date_trip_year, date_trip_month: val_input_date_trip_month, date_trip_day: val_input_date_trip_day, date_trip_ret: date_trip_ret_val, date_trip_ret_year: val_input_date_trip_ret_year, date_trip_ret_month: val_input_date_trip_ret_month, date_trip_ret_day: val_input_date_trip_ret_day, direction: val_oneway, time_trip: val_time_trip, time_trip_ret: val_time_trip_ret},
						evalScripts : true
					}

					);

				}
			} else {
				window.location.href=url;
			}
		} else {
			if((booking_init != booking_neige) ||(typeof(booking_init) != typeof(booking_neige))) {
				if( document.getElementById("booking_resa_dates"))
				{
					if( val_o !="" && val_d != "")
					{
					booking_init = booking_season_stations.inArray(optFrom[optFrom.selectedIndex].value) || booking_season_stations.inArray(optTo[optTo.selectedIndex].value);
						var url = "/";
						var ajax_val = 'bookingCalendar';
						var updateDiv = 'booking_resa_dates';
						var val_lg = document.getElementsByName("lg")[0].value;
						var val_id_booking = document.getElementById("booking_id").value;
						var val_country = country;
						ajaxDate = new Date();
						new Ajax.Updater(
						updateDiv,
						url,
						{
							method: 'get',
							parameters: {id_booking: val_id_booking,lg: val_lg,country: val_country, ajax: ajax_val, ts: ajaxDate.getTime(), o: val_o, d: val_d, date_trip: date_trip_val, date_trip_year: val_input_date_trip_year, date_trip_month: val_input_date_trip_month, date_trip_day: val_input_date_trip_day, date_trip_ret: date_trip_ret_val, date_trip_ret_year: val_input_date_trip_ret_year, date_trip_ret_month: val_input_date_trip_ret_month, date_trip_ret_day: val_input_date_trip_ret_day, direction: val_oneway, time_trip: val_time_trip, time_trip_ret: val_time_trip_ret},
							evalScripts : true
						}

						);

					}
				} else {
					window.location.href=url;
				}
			}
		}
	} else {
		if((booking_init != booking_season) ||(typeof(booking_init) != typeof(booking_season))) {

			if( document.getElementById("booking_resa_dates"))
			{
				if( val_o !="" && val_d != "")
				{
					booking_init = booking_season_stations.inArray(optFrom[optFrom.selectedIndex].value) || booking_season_stations.inArray(optTo[optTo.selectedIndex].value);
					var url = "/";
					var ajax_val = 'bookingCalendar';
					var updateDiv = 'booking_resa_dates';
					var val_lg = document.getElementsByName("lg")[0].value;
						var val_id_booking = document.getElementById("booking_id").value;
					var val_country = country;
					ajaxDate = new Date();
					new Ajax.Updater(
					updateDiv,
					url,
					{
						method: 'get',
						parameters: {id_booking: val_id_booking,lg: val_lg,country: val_country, ajax: ajax_val, ts: ajaxDate.getTime(), o: val_o, d: val_d, date_trip: date_trip_val, date_trip_year: val_input_date_trip_year, date_trip_month: val_input_date_trip_month, date_trip_day: val_input_date_trip_day, date_trip_ret: date_trip_ret_val, date_trip_ret_year: val_input_date_trip_ret_year, date_trip_ret_month: val_input_date_trip_ret_month, date_trip_ret_day: val_input_date_trip_ret_day, direction: val_oneway, time_trip: val_time_trip, time_trip_ret: val_time_trip_ret},
						evalScripts : true
					}

					);

				}
			}
		} else {
			if((booking_init != booking_neige) ||(typeof(booking_init) != typeof(booking_neige))) {
				if( document.getElementById("booking_resa_dates"))
				{
					if( val_o !="" && val_d != "")
					{
					booking_init = booking_season_stations.inArray(optFrom[optFrom.selectedIndex].value) || booking_season_stations.inArray(optTo[optTo.selectedIndex].value);
						var url = "/";
						var ajax_val = 'bookingCalendar';
						var updateDiv = 'booking_resa_dates';
						var val_lg = document.getElementsByName("lg")[0].value;
						var val_id_booking = document.getElementById("booking_id").value;
						var val_country = country;
						ajaxDate = new Date();
						new Ajax.Updater(
						updateDiv,
						url,
						{
							method: 'get',
							parameters: {id_booking: val_id_booking,lg: val_lg,country: val_country, ajax: ajax_val, ts: ajaxDate.getTime(), o: val_o, d: val_d, date_trip: date_trip_val, date_trip_year: val_input_date_trip_year, date_trip_month: val_input_date_trip_month, date_trip_day: val_input_date_trip_day, date_trip_ret: date_trip_ret_val, date_trip_ret_year: val_input_date_trip_ret_year, date_trip_ret_month: val_input_date_trip_ret_month, date_trip_ret_day: val_input_date_trip_ret_day, direction: val_oneway, time_trip: val_time_trip, time_trip_ret: val_time_trip_ret},
							evalScripts : true
						}

						);

					}
				}
			}
		}
	}

	if(new_input == true)
	{
		if(booking_season == true)
		{
			if(booking_neige == false)
			{
				if(!booking_season_stations.inArray(optFrom[optFrom.selectedIndex].value))
				{
					val = soleil_time_trip['aller'].label;
					val_opt = soleil_time_trip['aller'].time;
					val_ret = soleil_time_trip['retour'].label;
					val_opt_ret = soleil_time_trip['retour'].time;
				} else {
					val = soleil_time_trip['retour'].label;
					val_opt = soleil_time_trip['retour'].time;
					val_ret = soleil_time_trip['aller'].label;
					val_opt_ret = soleil_time_trip['aller'].time;
				}
			} else {
				if(!booking_season_stations.inArray(optFrom[optFrom.selectedIndex].value))
				{
					val = neige_time_trip['aller'].label;
					val_opt = neige_time_trip['aller'].time;
					val_ret = neige_time_trip['retour'].label;
					val_opt_ret = neige_time_trip['retour'].time;
				} else {
					val = neige_time_trip['retour'].label;
					val_opt = neige_time_trip['retour'].time;
					val_ret = neige_time_trip['aller'].label;
					val_opt_ret = neige_time_trip['aller'].time;
				}
			}

			formulaire_booking = document.getElementById("booking_advanced");
			if(formulaire_booking)
			{
				var optTime = document.getElementById("time_trip").options;
				optTime = emptyArray(optTime, optTime.length);
				opt = new Option(val,val_opt);
				optTime[0] = opt;
				document.getElementById("time_trip").disabled=true;
				new_element = document.createElement("input");
				new_element.type = "hidden";
				new_element.name = 'time_trip';
				new_element.value = val_opt;
				formulaire_booking.appendChild(new_element);

				var optTime_ret = document.getElementById("time_trip_ret").options;
				optTime_ret = emptyArray(optTime_ret, optTime_ret.length);
				opt_ret = new Option(val_ret,val_opt_ret);
				optTime_ret[0] = opt_ret;
				document.getElementById("time_trip_ret").disabled=true;
				new_element_ret = document.createElement("input");
				new_element_ret.type = "hidden";
				new_element_ret.name = 'time_trip_ret';
				new_element_ret.value = val_opt_ret;
				formulaire_booking.appendChild(new_element_ret);
			}
		}
		new_input = false;
	}
}

Array.prototype.inArray = function ( search_phrase )
{
	for( var i = 0; i < this.length; i++ )
	{
		if( search_phrase == this[i] )
		{
			return true;
		}
	}
	return false;
}

function synchronizeBookingPassengersOptions(f)
{

	var optPass = document.getElementById("passengers").options;
	nbpass = optPass[optPass.selectedIndex].value;
	maxpass = optPass[optPass.length-1].value;

	for (var i = 0; i < maxpass; i++) {
		if(document.getElementById("id_passager_"+i)) document.getElementById("id_passager_"+i).style.display = (i < nbpass)? 'block' : 'none';
		if(document.getElementById("age_passager_"+i)){
			document.getElementById("age_passager_"+i).style.display = (i < nbpass)? 'block' : 'none';
		}
		if(!tkl){
			optLoy = document.getElementById("code_fid_passager_"+i).options;
			synchronizeCodeFidPassager(document.getElementById("code_fid_passager_"+i),'numero_fid_passenger_'+i);
		}

	}
	checkTklEnabled("passengers");
	synchronizeCodeFidPassager(document.getElementById("code_fid_passager_0"),"numero_fid_passenger_0");
}

function synchronizeCodeFidPassager(code, id_fid)
{
	var i;
	if(tkl == 1) return true;
	code_fid = code.options[code.options.selectedIndex].value;
	if(document.getElementById(id_fid)) document.getElementById(id_fid).style.display = (code_fid.toUpperCase() == 'T' )? 'block' : 'none';

	/*if(country=="be" || country=="nl"){
		for(i=1;i<6;i++){
			id_fid="numero_fid_passenger_"+i;
			if(document.getElementById(id_fid)) document.getElementById(id_fid).style.display = (code_fid.toUpperCase() == 'T' )? 'block' : 'none';
		}
	}*/

}

function synchronizeTKLNumber()
{
	var nb_tkl = document.getElementById("numero_fid_passager_0").value;
	formulaire_tkl = document.getElementById("TKL");
	if(!formulaire_tkl.num_cybelys){
		for(i=0;i<formulaire_tkl.elements.length;i++){
			if(formulaire_tkl.elements[i].tagName.toUpperCase() == "INPUT" && formulaire_tkl.elements[i].name.toUpperCase() == "EMAIL")
			{
				formulaire_tkl.elements[i].name = 'num_cybelys';
				formulaire_tkl.elements[i].id = 'input_num';
			}

			if(formulaire_tkl.elements[i].tagName.toUpperCase() == "INPUT" && formulaire_tkl.elements[i].name.toUpperCase() == "TKL_METHOD")
			{
				formulaire_tkl.elements[i].value = 'num';
			}
		}

		label = document.getElementById("lbl_tkl_type");
		label.innerHTML = lbl_tkl_num;


		lien =  document.getElementById("num_tkl");
		lien.innerHTML = lbl_tkl_acces_email;
		lien.href = lien_tkl_email;
		lien.id = "mail_tkl";
		var IE =  ((navigator.userAgent.indexOf("MSIE") != -1));
		if(IE){
			lien.onclick=function(){tklRedirect('mail_tkl');};
		}
		else lien.setAttribute("onclick","tklRedirect('mail_tkl')");
	}

	document.getElementById("input_num").value = '30840601'+nb_tkl;
}

function synchronizeBookingPassengersNumber(f)
{
	var optPax1 = document.getElementById("booking_advanced").passengers.options;
	var optPax2 = document.getElementById("booking_advanced").pax2.options;


	optPax2 = emptyArray(optPax2, optPax2.length);
	if(optPax1.selectedIndex >= 0) {
		nbpax1 = optPax1[optPax1.selectedIndex].value;

		optIndex = 0;
		nbmax = 6 - nbpax1;
		for (var i = 0; i <= nbmax; i++) {
			opt = new Option(i, i);
			optPax2[optIndex++] = opt;
		}
	}
}

function bookingSynchronizeTKL(f)
{
	var optFrom = document.getElementById("fromstation").options;
	var optTo = document.getElementById("tostation").options;
	var fromstation = optFrom[optFrom.selectedIndex].value;
	var tostation = optTo[optTo.selectedIndex].value;
	var active = 'none';
	var inactive = 'none';
	var forget = 'none';
	var booking_tkl = default_booking_tkl;
	var booking_season = booking_season_stations.inArray(optFrom[optFrom.selectedIndex].value) || booking_season_stations.inArray(optTo[optTo.selectedIndex].value);
	//	alert(booking_tkl);

	if(fromstation.substr(5,3).toUpperCase() == 'TGB' || tostation.substr(5,3).toUpperCase() == 'TGB' || booking_season == true)booking_tkl = 0;
	if(tkl == 1){
		if(booking_tkl == 1)
		{
			active = 'block';
		} else
		{
			inactive = 'block';
		}
	}
	else {
		if(oubli=="pass") forget = 'block';
		else
		{
			if(booking_tkl == 1)
			{
				active = 'block';
			} else
			{
				inactive = 'block';
			}
		}
		if (document.getElementById('tkl-auth-oublipass')) document.getElementById('tkl-auth-oublipass').style.display = forget;
	}
	//	alert(active+' '+inactive);

	if (document.getElementById(tkl_active)) document.getElementById(tkl_active).style.display = active;
	if (document.getElementById(tkl_inactive)) document.getElementById(tkl_inactive).style.display = inactive;
	bookingSynchronizeNoTKL(f);
	synchronizeDays();
}

function bookingSynchronizeNoTKL(f)
{
	var optFrom = document.getElementById("fromstation").options;
	var optTo = document.getElementById("tostation").options;
	var fromstation = optFrom[optFrom.selectedIndex].value;
	var tostation = optTo[optTo.selectedIndex].value;

	if((country == 'nl' && fromstation.substr(0,2).toUpperCase() != 'NL' && tostation.substr(0,2).toUpperCase() != 'NL' && tostation != '' && fromstation !='') || block_paper == true){
		if (document.getElementById(booking_active)) document.getElementById(booking_active).style.display = 'none';
		if (document.getElementById(booking_inactive)) document.getElementById(booking_inactive).style.display = 'block';
	} else {
		if (document.getElementById(booking_active)) document.getElementById(booking_active).style.display = 'block';
		if (document.getElementById(booking_inactive)) document.getElementById(booking_inactive).style.display = 'none';
	}
}

function synchronizeDays()
{
	//la liste des booking qui ne subissent la blocage à 7 jours se trouve dans booking.js.php
	if (document.getElementById(booking_inactive)) {
		var style_active = (document.getElementById(booking_inactive).style.display == 'none')? 'block' : 'none';
	}
	if(document.getElementById("input_date_trip_year"))
	{
		var booking_today = new Date(document.getElementById("input_date_trip_year").value,document.getElementById("input_date_trip_month").value,document.getElementById("input_date_trip_day").value);

		if(country == 'de' && !booking_not_7_days.inArray(booking_id) && Math.ceil((booking_today.getTime()-booking_today_7_open.getTime())) < 0 && document.getElementById("date_trip").value != '')
		{
			if (document.getElementById(booking_active)) document.getElementById(booking_active).style.display = 'none';
			if (document.getElementById(booking_inactive_date)) document.getElementById(booking_inactive_date).style.display = 'block';
		} else {
			if (document.getElementById(booking_active)) document.getElementById(booking_active).style.display = style_active;
			if (document.getElementById(booking_inactive_date)) document.getElementById(booking_inactive_date).style.display = 'none';
		}
	} else {
		if (document.getElementById(booking_active)) document.getElementById(booking_active).style.display = style_active;
		if (document.getElementById(booking_inactive_date)) document.getElementById(booking_inactive_date).style.display = 'none';
	}

}

function synchronizeToStations(f,defaultTime,defaultTimeRet)
{
	bookingSynchronizeBookingTimeTrip(f,defaultTime,defaultTimeRet);
	bookingSynchronizeTKL(f);
}


//TKL



function tklSaveBooking(id_form){


	formulaire_booking = document.getElementById("booking_advanced");
	formulaire_tkl = document.getElementById(id_form);

	for(i=0;i<formulaire_booking.elements.length;i++){
		if((formulaire_booking.elements[i].tagName.toUpperCase() == "INPUT" && formulaire_booking.elements[i].name.toUpperCase() != "DO" && formulaire_booking.elements[i].name.toUpperCase() != "PATH" && formulaire_booking.elements[i].type.toUpperCase()!="BUTTON" && formulaire_booking.elements[i].type.toUpperCase()!="SUBMIT" && formulaire_booking.elements[i].type.toUpperCase()!="IMAGE")|| formulaire_booking.elements[i].tagName.toUpperCase() == "SELECT"){
			if(formulaire_booking.elements[i].name!="email" && formulaire_booking.elements[i].name!="pass_tkl" && formulaire_booking.elements[i].name!="num_cybelys"&& formulaire_booking.elements[i].name!="prod_atos"){
				if(formulaire_booking.elements[i].type.toUpperCase()!="CHECKBOX"){
					new_element = document.createElement("input");
					new_element.type = "hidden";
					new_element.name = formulaire_booking.elements[i].name;
					new_element.value = formulaire_booking.elements[i].value;
					formulaire_tkl.appendChild(new_element);
				}
				else{
					if(formulaire_booking.elements[i].checked){
						new_element = document.createElement("input");
						new_element.type = "hidden";
						new_element.name = formulaire_booking.elements[i].name;
						new_element.value = formulaire_booking.elements[i].value;
						formulaire_tkl.appendChild(new_element);
					}
				}
			}
		}
	}

	formulaire_tkl.submit();
	return(true);


}

function switchCountry(id_form)
{
	var pays_retrait = (document.getElementById("pays_retrait"))? document.getElementById("pays_retrait").value : document.getElementById("booking_advanced").pays_retrait.value;
	var country = document.getElementById("switch_country").value;
	if (pays_retrait !='' && pays_retrait != country)
	{
		var path = unescape(window.location.href).split("?");
		var tab_url = unescape(path[0]).split("/");
		var url = tab_url[0]+'//'+tab_url[2]+'/'+pays_retrait+'/'+tab_url[4]+'/'+tab_url[5];
		var qs = '';
		formulaire_booking = document.getElementById("booking_advanced");

		for(i=0;i<formulaire_booking.elements.length;i++){
			if((formulaire_booking.elements[i].tagName.toUpperCase() == "INPUT" && formulaire_booking.elements[i].name.toUpperCase() != "DO" && formulaire_booking.elements[i].type.toUpperCase()!="BUTTON" && formulaire_booking.elements[i].type.toUpperCase()!="SUBMIT" && formulaire_booking.elements[i].type.toUpperCase()!="IMAGE")|| formulaire_booking.elements[i].tagName.toUpperCase() == "SELECT"){
				if(formulaire_booking.elements[i].name!="email" && formulaire_booking.elements[i].name!="pass_tkl" && formulaire_booking.elements[i].name!="num_cybelys"&& formulaire_booking.elements[i].name!="prod_atos"){
					if(formulaire_booking.elements[i].name.toUpperCase() == 'PASSENGERS')
					{
						formulaire_booking.elements[i].name = 'adults';
					}

					if(formulaire_booking.elements[i].name.toUpperCase() == 'COUNTRY')
					{
						formulaire_booking.elements[i].value = pays_retrait;
					}

					if(formulaire_booking.elements[i].name.toUpperCase() != 'PATH' && formulaire_booking.elements[i].name.toUpperCase() != 'CURRENT_PATH')
					{
						if(qs !='') qs = qs+'&';
						qs = qs+formulaire_booking.elements[i].name+'='+formulaire_booking.elements[i].value;
					}
				}
			}
		}
		url = url+'?'+qs;
		//alert(path[1].indexOf("error"));
		if(path[1] && path[1].indexOf("error") >= 0)
		{
			url = url+'&error=1';
		}
		window.location = (url);
	}
}


function tklRedirect(id_lien){

	lien = document.getElementById(id_lien);

	new_href = document.URL;
	new_href = new_href.split("?");
	new_href = new_href[0];

	old_href = lien.href;
	old_href = old_href.split("?");
	old_href = old_href[1];

	switch(id_lien){
		case "oubli_mail" : new_href += "?oubli=mail&tkl_method=num";break;
		case "oubli_pass" : new_href += "?oubli=pass";break;
		case "num_tkl" : new_href += "?tkl_method=num";break;
		case "mail_tkl" : new_href += "?tkl_method=";break;
		case "tkl_deco_top" :
		case "deco_link" : new_href += "?tkl_method="+"&"+old_href;break;
	}

	//Ajout du booking
	formulaire_booking = document.getElementById("booking_advanced");
	for(i=0;i<formulaire_booking.elements.length;i++){
		if((formulaire_booking.elements[i].tagName.toUpperCase() == "INPUT" && formulaire_booking.elements[i].name.toUpperCase() != "DO" && formulaire_booking.elements[i].name.toUpperCase() != "PASS_TKL" && formulaire_booking.elements[i].type.toUpperCase()!="BUTTON" && formulaire_booking.elements[i].type.toUpperCase()!="SUBMIT" && formulaire_booking.elements[i].type.toUpperCase()!="IMAGE")|| formulaire_booking.elements[i].tagName.toUpperCase() == "SELECT"){

			if(formulaire_booking.elements[i].type.toUpperCase()=="CHECKBOX"){
				if(formulaire_booking.elements[i].checked) new_href =  new_href + "&" + formulaire_booking.elements[i].name + "=" + formulaire_booking.elements[i].value;
			}else{
				if(formulaire_booking.elements[i].name.toUpperCase() == "PATH" ){
					actuel_path = str_replace('https://'+document.domain,'',document.URL);

					actuel_path_array = actuel_path.split('&');
					actuel_path_nb = actuel_path_array.length;

					path = '';
					for(u=0;u<actuel_path_nb;u++){
						params_array = actuel_path_array[u].split('=', 1);
						if(params_array[0]!='path'){
							if(u==0 && actuel_path_nb==1){
								sep = '';
							}else{
								sep = '%23'
							}
							path += actuel_path_array[u] + sep;
						}
					}
					//					new_href =  new_href + "&" + formulaire_booking.elements[i].name + "=" + str_replace( '&', '%26',formulaire_booking.elements[i].value );
					new_href =  new_href + "&" + formulaire_booking.elements[i].name + "=" + path;
				}else{
					new_href =  new_href + "&" + formulaire_booking.elements[i].name + "=" + formulaire_booking.elements[i].value;
				}

			}
		}
	}
	//ajout des valeurs tu formulaire tkl si besoin
	formulaire_tkl = document.getElementById("TKL");
	if(id_lien != "deco_link" && id_lien != "tkl_deco_top" && formulaire_tkl){

		for(i=0;i<formulaire_tkl.elements.length;i++){
			if((formulaire_tkl.elements[i].tagName.toUpperCase() == "INPUT" && formulaire_tkl.elements[i].name.toUpperCase() != "DO" && formulaire_tkl.elements[i].name.toUpperCase() != "PASS_TKL" && formulaire_tkl.elements[i].type.toUpperCase()!="BUTTON" && formulaire_tkl.elements[i].type.toUpperCase()!="SUBMIT" && formulaire_tkl.elements[i].type.toUpperCase()!="IMAGE")|| formulaire_tkl.elements[i].tagName.toUpperCase() == "SELECT"){
				if(formulaire_tkl.elements[i].name.toUpperCase() == "TKL_METHOD" && id_lien!="oubli_pass") continue;
				if(formulaire_tkl.elements[i].type.toUpperCase()=="CHECKBOX"){
					if(formulaire_tkl.elements[i].checked) new_href =  new_href + "&" + formulaire_tkl.elements[i].name + "=" + formulaire_tkl.elements[i].value;
				}
				else new_href =  new_href + "&" + formulaire_tkl.elements[i].name + "=" + formulaire_tkl.elements[i].value;
			}
		}
	}


	//Modification du href du lien
	lien.href=new_href;


	return (true);

}


function checkTklEnabled(type_verif){

	if(document.getElementById('passengers')){

		nb_passagers = document.getElementById('passengers').options[document.getElementById('passengers').selectedIndex].value;
		controle = new Array();
		//kid_id = 5;

		for(i=0;i<nb_passagers;i++){
			value = document.getElementById('age_passager_'+i).options[document.getElementById('age_passager_'+i).selectedIndex].value;

			c=false;
			for(k=0;k<controle.length;k++){
				if(controle[k]==value) c=true;
			}
			if(value!=kid_id && c==false) controle[controle.length]=value;
		}

		from = document.getElementById("fromstation").options[document.getElementById("fromstation").selectedIndex].value;
		to = document.getElementById("tostation").options[document.getElementById("tostation").selectedIndex].value;
		//alert(document.getElementById("bienvenue_inactive").style.background);
		if(controle.length>1 && type_verif=="passengers"){
			document.getElementById("tkl_msg_tgb").style.display = "none";
			document.getElementById("tkl_msg_voyagers").style.display = "block";
			if(document.getElementById("msg_formulaire_inactive")) document.getElementById("msg_formulaire_inactive").style.backgroundImage = "url(/img/common/bkg_no_tkl_voyagers.gif)";
			if(document.getElementById("bienvenue_inactive")) document.getElementById("bienvenue_inactive").style.backgroundImage = "url(/img/common/bkg_no_tkl_voyagers.gif)";

		}else{
			if(from.substr(from.length-3,3)=="TGB" || to.substr(to.length-3,3)=="TGB" || booking_season_stations.inArray(from) || booking_season_stations.inArray(to)){
				document.getElementById("tkl_msg_tgb").style.display = "block";
				document.getElementById("tkl_msg_voyagers").style.display = "none";
				if(document.getElementById("msg_formulaire_inactive")) document.getElementById("msg_formulaire_inactive").style.backgroundImage = "url(/img/common/bkg_no_tkl.gif)";
				if(document.getElementById("bienvenue_inactive")) document.getElementById("bienvenue_inactive").style.backgroundImage = "url(/img/common/bkg_no_tkl.gif)";
			}
		}
		if(controle.length>1 || from.substr(from.length-3,3)=="TGB" || to.substr(to.length-3,3)=="TGB" || booking_season_stations.inArray(from) || booking_season_stations.inArray(to) || default_booking_tkl==0) disabled=true;
		else disabled=false;



		if(disabled){
			if (document.getElementById(tkl_active)) document.getElementById(tkl_active).style.display = "none";
			if (document.getElementById(tkl_inactive)) document.getElementById(tkl_inactive).style.display = "block";
		}
		else{
			if (document.getElementById(tkl_active)) document.getElementById(tkl_active).style.display = "block";
			if (document.getElementById(tkl_inactive)) document.getElementById(tkl_inactive).style.display = "none";
		}
	}
}

function SynchronizeAgePassager(){

	setTimeout("SynchronizeAgePassager2()",50);

}

function SynchronizeAgePassager2(){
	nb_passagers = document.getElementById('passengers').options.length;
	nb_passagers_selected = document.getElementById('passengers').options[document.getElementById('passengers').selectedIndex].value;
	for(i=1;i<nb_passagers;i++){
		if(!tkl){
			age = document.getElementById('age_passager_'+i).options[document.getElementById('age_passager_'+i).selectedIndex].value;

			fid = document.getElementById("code_fid_passager_"+i).selectedIndex;

			if(age==kid_id){
				document.getElementById("div_cartes_"+i).style.display="none";
				document.getElementById("numero_fid_passenger_"+i).style.display="none";
			}
			else{
				document.getElementById("div_cartes_"+i).style.display="block";
				if(fid!=0) document.getElementById("numero_fid_passenger_"+i).style.display="block";
			}
		}
		if(i>nb_passagers_selected){
			document.getElementById("id_passager_"+i).style.display = "none";
		}
	}


	for(i=nb_passagers_selected;i<6;i++){
		if(document.getElementById("age_passager_"+i))document.getElementById("age_passager_"+i).style.display="block";
		if(document.getElementById("age_passager_"+i))document.getElementById("age_passager_"+i).style.display="none";
	}


	if(country=="fr" || country=="de") return checkTklEnabled("passengers");

	nb_passagers = document.getElementById('passengers').options.length;
	choix = document.getElementById('age_passager_0').options[document.getElementById('age_passager_0').selectedIndex].value;
	label = document.getElementById('age_passager_0').options[document.getElementById('age_passager_0').selectedIndex].innerHTML;

	//kid_id = 5;
	//kid_label = document.getElementById('age_passager_0').options[2].innerHTML;

	if(choix==kid_id) return true;

	for(i=1;i<nb_passagers;i++){

		old_value = document.getElementById('age_passager_'+i).options[document.getElementById('age_passager_'+i).selectedIndex].value;

		var selected;

		/*
		if(old_value==choix) selected=" SELECTED ";
		else selected = "";
		*/
		document.getElementById('age_passager_'+i).innerHTML = "";

		new_element = document.createElement("option");
		new_element.value = choix;
		if(selected!="")new_element.selected = "selected";
		new_element.innerHTML = label;
		document.getElementById('age_passager_'+i).appendChild(new_element);

		if(old_value==kid_id) selected=" SELECTED ";
		else selected = "";

		new_element = document.createElement("option");
		new_element.value = kid_id;
		if(selected!="")new_element.selected = "selected";
		new_element.innerHTML = kid_label;
		document.getElementById('age_passager_'+i).appendChild(new_element);

		//document.getElementById('age_passager_'+i).innerHTML = tmp;
	}


	for(i=nb_passagers_selected;i<6;i++){
		document.getElementById("age_passager_"+i).style.display="block";
		document.getElementById("age_passager_"+i).style.display="none";
	}


	checkTklEnabled("passengers");
}


function synchronizeNumAboCarte(){

	if(document.getElementById("num_carte_abo")){
		if(document.getElementById("carte_passager_0").selectedIndex==0){
			document.getElementById("num_carte_abo").style.display="none";
		}
		else{
			document.getElementById("num_carte_abo").style.display="block";
		}
	}

}


function showHideTgb(){


	if(document.getElementById("div_station_tgb_o")){
		if(document.getElementById("fromstation").options[document.getElementById("fromstation").selectedIndex].value=="TGB"){
			document.getElementById("div_station_tgb_o").style.display="block";

		}
		else{
			document.getElementById("div_station_tgb_o").style.display="none";

		}
	}

	if(document.getElementById("div_station_tgb_d")){
		if(document.getElementById("tostation").options[document.getElementById("tostation").selectedIndex].value=="TGB"){
			document.getElementById("div_station_tgb_d").style.display="block";
		}
		else{
			document.getElementById("div_station_tgb_d").style.display="none";
		}
	}

	if(document.getElementById("tostation").options[document.getElementById("tostation").selectedIndex].value=="TGB" || document.getElementById("fromstation").options[document.getElementById("fromstation").selectedIndex].value=="TGB"){
		if(document.getElementById("booking_label_tgb"))document.getElementById("booking_label_tgb").style.display="block";
	}
	else{
		if(document.getElementById("booking_label_tgb")) document.getElementById("booking_label_tgb").style.display="none";
	}
}


function bookingSynchronizeStationsDispo(f,defaultTo)
{
	//var selectValue;
	var optFrom = document.getElementById("dispo_fromstation").options;
	var optTo = document.getElementById("dispo_tostation").options;
	var lstTo = document.getElementById("dispo_tostation");

	if(optFrom.selectedIndex >= 0) {
		fromStation = optFrom[optFrom.selectedIndex].value;
		toStations = booking_stationsMap[fromStation];

		if(isArray(toStations))
		{
			if(toStations.length == 1){
				optTo = emptyArray(optTo, optTo.length);
				toStation = toStations[0];
				insertListe(lstTo,booking_stations[toStation],toStation,0);
			}
			if(toStations.length > 1) {
				optTo = emptyArray(optTo, optTo.length);
				insertListe(lstTo,utf8_decode (label_tostation),"",0);
				optIndex = 1;
				for (var i = 0; i < toStations.length; i++) {
					toStation = toStations[i];
					insertListe(lstTo,booking_stations[toStation],toStation,optIndex);
				}

				//optIndex = 1;
				optTo = document.getElementById("dispo_tostation").options;
				for (var i = 0; i < optTo.length; i++) {
					toStation = optTo[i].value;
					if (toStation == defaultTo) {
						//alert(defaultTo);
						optTo.selectedIndex = i;
						break;
					}
					//optIndex++;
				}
			}
		}
	}
}


function bookingSynchronizeBookingTimeTripDispo()
{
	var optFrom = document.getElementById("dispo_fromstation").options;
	var fromStation = optFrom[optFrom.selectedIndex].value;
	var optTo = document.getElementById("dispo_tostation").options;
	var toStation = optTo[optTo.selectedIndex].value;
	if((fromStation == 'FRPNO' && toStation == 'BEBMI') || (toStation == 'FRPNO' && fromStation == 'BEBMI')) {
		document.getElementById("dispo_time_trip").style.display = 'block';
	} else
	{
		document.getElementById("dispo_time_trip").style.display = 'none';
	}

}

function utf8_decode ( str_data ) {
	// http://kevin.vanzonneveld.net
	// +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
	// +      input by: Aman Gupta
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Norman "zEh" Fuchs
	// +   bugfixed by: hitwork
	// +   bugfixed by: Onno Marsman
	// *     example 1: utf8_decode('Kevin van Zonneveld');
	// *     returns 1: 'Kevin van Zonneveld'

	var tmp_arr = [], i = ac = c1 = c2 = c3 = 0;

	str_data += '';

	while ( i < str_data.length ) {
		c1 = str_data.charCodeAt(i);
		if (c1 < 128) {
			tmp_arr[ac++] = String.fromCharCode(c1);
			i++;
		} else if ((c1 > 191) && (c1 < 224)) {
			c2 = str_data.charCodeAt(i+1);
			tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
			i += 2;
		} else {
			c2 = str_data.charCodeAt(i+1);
			c3 = str_data.charCodeAt(i+2);
			tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}
	}

	return tmp_arr.join('');
}

function str_replace(search, replace, subject) {
	var f = search, r = replace, s = subject;
	var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;

	while (j = 0, i--) {
		if (s[i]) {
			while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
		}
	};

	return sa ? s : s[0];
}

function removeAddComfort2(type){

	var optCf = document.getElementById("cf").options;
	var CF = document.getElementById("cf");
	optCf = emptyArray(optCf, optCf.length);


	if(type == 'add'){
		insertListe(CF,'Comfort 2',2,0);
		insertListe(CF,'Comfort 1',1,0);
	}else{
		insertListe(CF,'Comfort 1',1,0);
	}

}

function removeAddComfort1(type){

	var optCf = document.getElementById("cf").options;
	var CF = document.getElementById("cf");
	optCf = emptyArray(optCf, optCf.length);


	if(type == 'add'){
		insertListe(CF,'Comfort 2',2,1);
		insertListe(CF,'Comfort 1',1,0);
	}else{
		insertListe(CF,'Comfort 2',2,0);
	}
}
function bookingSynchronizeStationsPartenaire50Pour100(){
	bookingSynchronizeStations();

	if(fromstation != '' && tostation != ''){
		var optFrom = document.getElementById("fromstation").options;
		var optTo = document.getElementById("tostation").options;
		var fromstation = optFrom[optFrom.selectedIndex].value;
		var tostation = optTo[optTo.selectedIndex].value;

		if((fromstation.substring(0,2) == 'BE' && tostation.substring(0,2) == 'NL') || (fromstation.substring(0,2) == 'NL' && tostation.substring(0,2) == 'BE')){
			removeAddComfort2('remove');
		}else{
			removeAddComfort2('add');
		}
	}
}
function bookingSynchronizeBookingTimeTripPartenaire50Pour100(){
	bookingSynchronizeBookingTimeTrip();

	var optFrom = document.getElementById("fromstation").options;
	var optTo = document.getElementById("tostation").options;
	var fromstation = optFrom[optFrom.selectedIndex].value;
	var tostation = optTo[optTo.selectedIndex].value;

	if((fromstation.substring(0,2) == 'BE' && tostation.substring(0,2) == 'NL') || (fromstation.substring(0,2) == 'NL' && tostation.substring(0,2) == 'BE')){
		removeAddComfort2('remove');
	}else{
		removeAddComfort2('add');
	}
}

function removeAddComfortRedDayJanvier2010(d,m,y,quit){
	addToInput(d,m,y,quit);


//	bookingSynchronizeBookingTimeTrip();

//	alert('test');

	var array_from = new Array('27/02/2010','20/03/2010');
	var array_to = new Array('06/03/2010','27/03/2010');

	var optFrom = document.getElementById("fromstation").options;
	var optTo = document.getElementById("tostation").options;
	var fromstation = optFrom[optFrom.selectedIndex].value;
	var tostation = optTo[optTo.selectedIndex].value;

	var date_aller = document.getElementById("date_trip").value;
	var date_retour = document.getElementById("date_trip_ret").value;

	if(fromstation.substring(0,2) == 'BE' || tostation.substring(0,2) == 'BE'){

		if(fromstation == 'BEABC' && (array_from.inArray(date_aller) || array_to.inArray(date_retour)) || tostation == 'BEABC' && (array_from.inArray(date_retour) || array_to.inArray(date_aller)) ){
			removeAddComfort2('remove');
		}else{
			removeAddComfort2('add');
		}
	}else{
		if(fromstation.substring(0,2) == 'NL' && (array_from.inArray(date_aller) || array_to.inArray(date_retour)) || tostation.substring(0,2) == 'NL' && (array_from.inArray(date_retour) || array_to.inArray(date_aller))){
			removeAddComfort2('remove');
		}else{
			removeAddComfort2('add');
		}
	}

}


function addToInputBooking (d,m,y,quit)
{
	if(quit == true)
	{
		var timed = new Date();
		var tt = timed.getHours() + ":" + timed.getMinutes() + ":" +timed.getSeconds() + "";
		ctlToPlaceValue.value = constructDate(d,m,y, tt );
		var inputDay = document.getElementById(selName+"_day");
		var inputMonth = document.getElementById(selName+"_month");
		var inputYear = document.getElementById(selName+"_year");
		if(null == inputDay)
		{
			inputDay = document.getElementById("input_"+selName+"_day");
		}
		if(null == inputMonth)
		{
			inputMonth = document.getElementById("input_"+selName+"_month");
		}
		if(null == inputYear)
		{
			inputYear = document.getElementById("input_"+selName+"_year");
		}
		inputDay.value = d;
		inputMonth.value = m+1;
		inputYear.value = y;
		synchronizeBookingCalendar();
	}

}