// ============================================================================
// Destineo - Marque Blanche - FUNCTIONS
// ============================================================================
//
//
// ============================================================================
// dmbSetDefaultValues
// ============================================================================
function dmbSetDefaultValues() {
	// Setting form action
	document.getElementById("dmb_form_recherche").action = DMB_URL_BASE_SEARCH_URL;
		
	if(document.getElementById("dmb_dep_point")!=null){
		document.getElementById("dmb_dep_point").value = DMB_DEFAULT_VALUE_POINT_SRCE;
	}
	if(document.getElementById("dmb_arr_point") !=null){
		document.getElementById("dmb_arr_point").value = DMB_DEFAULT_VALUE_POINT_DEST;
	}
	if(document.getElementById("dmb_dep_ville")!=null){
		document.getElementById("dmb_dep_ville").value = DMB_DEFAULT_VALUE_POINT_CITY_SRCE;
	}
	if(document.getElementById("dmb_arr_ville")!=null){
		document.getElementById("dmb_arr_ville").value = DMB_DEFAULT_VALUE_POINT_CITY_DEST;
	}
}

// ============================================================================
// dmbClearFields
// ============================================================================
function dmbClearFields()
{
	if(document.getElementById("dmb_dep_point")!=null){
		document.getElementById("dmb_dep_point").value = "";
		}
	if(document.getElementById("dmb_arr_point")!=null){
		document.getElementById("dmb_arr_point").value = "";
	}
	if(document.getElementById("dmb_dep_ville")!=null){
		document.getElementById("dmb_dep_ville").value = "";
	}
	if(document.getElementById("dmb_arr_ville")!=null){
	document.getElementById("dmb_arr_ville").value = "";
	}
	
}
//=========================================================================
// clean the field departure
//========================================
function dmbCleanDeparture(){
	
	if(DMB_DEPARTURE_FIELDS_READONLY == false &&
		document.getElementById("dmb_dep_point").value == DMB_DEFAULT_VALUE_POINT_SRCE && DMB_CLEAN_FIELDS_DEPARTURE==true)
	{
		document.getElementById("dmb_dep_point").value = "";
	}
}
//=========================================================================
// clean the field city departure
//========================================
function dmbCleanDepartureCity(){
	if(DMB_DEPARTURE_FIELDS_READONLY == false &&
		document.getElementById("dmb_dep_ville").value == DMB_DEFAULT_VALUE_POINT_CITY_SRCE && DMB_CLEAN_FIELDS_DEPARTURE==true)
	{
		document.getElementById("dmb_dep_ville").value = "";
	}
}
	
//=========================================================================
// clean the field  arrived
//========================================
function dmbCleanArrived(){
	if(DMB_ARRIVAL_FIELDS_READONLY == false &&
		document.getElementById("dmb_arr_point").value == DMB_DEFAULT_VALUE_POINT_DEST && DMB_CLEAN_FIELDS_ARRIVAL==true)
	{
		document.getElementById("dmb_arr_point").value = "";
	}
	}
	
//=========================================================================
// clean the field city arrived
//========================================
function dmbCleanArrivedCity(){
	if(DMB_ARRIVAL_FIELDS_READONLY == false &&
		document.getElementById("dmb_arr_ville").value == DMB_DEFAULT_VALUE_POINT_CITY_DEST && DMB_CLEAN_FIELDS_ARRIVAL==true)
	{
		document.getElementById("dmb_arr_ville").value = "";
	}
	}
// ============================================================================
// dmbInitialize
// ============================================================================
function dmbInitialize() {
	dmbClearFields();
	dmbSetDefaultValues();
	
	if (DMB_TIME_SEARCH_TYPE == 1) {
		document.getElementById("dmb_form_recherche").dmb_sens.options[0].selected = true;
	}
	else {
		document.getElementById("dmb_form_recherche").dmb_sens.options[1].selected = true;
	}

	if(document.getElementById("dmb_dep_point")!=null){
		document.getElementById("dmb_form_recherche").dmb_dep_point.readOnly = DMB_DEPARTURE_FIELDS_READONLY;
	}
	if(document.getElementById("dmb_dep_ville")!=null){
		document.getElementById("dmb_form_recherche").dmb_dep_ville.readOnly = DMB_DEPARTURE_FIELDS_READONLY;
	}
	if(document.getElementById("dmb_arr_point") !=null){
		document.getElementById("dmb_form_recherche").dmb_arr_point.readOnly = DMB_ARRIVAL_FIELDS_READONLY;
	}
	if(document.getElementById("dmb_arr_ville")!=null){
		document.getElementById("dmb_form_recherche").dmb_arr_ville.readOnly = DMB_ARRIVAL_FIELDS_READONLY;
		}
	
	//Affichage des champs date, heure et option
	if(SHOW_DATE){
	    document.getElementById("label_date").style.display = '';
	    document.getElementById("dmb_form_recherche").dmb_date_jour.style.display = '';
	    document.getElementById("dmb_form_recherche").dmb_date_mois.style.display = '';
	    document.getElementById("dmb_form_recherche").dmb_date_annee.style.display = '';
	}
	if(SHOW_HOUR){
	    document.getElementById("label_heure").style.display = '';
	    document.getElementById("dmb_form_recherche").dmb_horr_heure.style.display = '';
	    document.getElementById("dmb_form_recherche").dmb_horr_minute.style.display = '';
	    document.getElementById("dmb_form_recherche").dmb_sens.style.display = '';
	}
	if(SHOW_OPTION){
	    document.getElementById("label_option").style.display = '';
	    document.getElementById("dmb_form_recherche").dmb_option.style.display = '';
	}	
	
	dmbGetDate();
	dmbGetTime();
}

// ============================================================================
// dmbSubmitForm
// ============================================================================
function dmbSubmitForm() {
	// Supprime le contenu du champ s'il est identique à la donnée de départ
	dmbEmpyFieldAlert('dmb_dep_point', DMB_DEFAULT_VALUE_POINT_SRCE);
	dmbEmpyFieldAlert('dmb_dep_ville', DMB_DEFAULT_VALUE_POINT_CITY_SRCE);
	dmbEmpyFieldAlert('dmb_arr_point', DMB_DEFAULT_VALUE_POINT_DEST);
	dmbEmpyFieldAlert('dmb_arr_ville', DMB_DEFAULT_VALUE_POINT_CITY_DEST);
}

// ============================================================================
// dmbEmpyFieldAlert
// ============================================================================
function dmbEmpyFieldAlert (sdmbFieldId, sdmbInit) {
	if (document.getElementById(sdmbFieldId).value==sdmbInit) {
		document.getElementById(sdmbFieldId).value = '';
		return false;
	}
	return true;
}

// ============================================================================
// dmbGetDate
// ============================================================================
function dmbGetDate()
{	
	var odmbToday = new Date();
	var idmbDay   = 0;
	var idmbMonth = 0;
	var idmbYear  = 0;
	
	if (
		DMB_DATE_DAY != DMB_DATE_DEFAULT_VALUE
		&& 
		DMB_DATE_MONTH != DMB_DATE_DEFAULT_VALUE
		&& 
		DMB_DATE_YEAR != DMB_DATE_DEFAULT_VALUE
	) {
		idmbDay   = DMB_DATE_DAY;
		idmbMonth = DMB_DATE_MONTH;
		idmbYear  = DMB_DATE_YEAR;		
	}
	else {
		idmbDay   = (odmbToday.getDate());
		idmbMonth = (odmbToday.getMonth()) + 1;
		idmbYear  = (odmbToday.getFullYear());
	}

	// Listing date
	dmbGetMonthList(idmbMonth);
	dmbGetYearList(idmbYear);
	dmbGetDayList(idmbDay);
}

// ============================================================================
// dmbGetMonthList
// ============================================================================
function dmbGetMonthList (idmbMonth) {
	var idmbMonthNumber = 12;
	var idmbBase        = 10;
	
	for (i=1; i<=idmbMonthNumber; i++) {
		var sdmbMonth = i;
		if (i < idmbBase) {
			sdmbMonth = "0" + i;
		}
		var sdmbMonthLib = "Janvier";
		if (sdmbMonth == "02") {
			sdmbMonthLib = "Fevrier";
		} else if (sdmbMonth == "03") {
			sdmbMonthLib = "Mars";
		} else if (sdmbMonth == "04") {
			sdmbMonthLib = "Avril";
		} else if (sdmbMonth == "05") {
			sdmbMonthLib = "Mai";
		} else if (sdmbMonth == "06") {
			sdmbMonthLib = "Juin";
		} else if (sdmbMonth == "07") {
			sdmbMonthLib = "Juillet";
		} else if (sdmbMonth == "08") {
			sdmbMonthLib = "Aout";
		} else if (sdmbMonth == "09") {
			sdmbMonthLib = "Septembre";
		} else if (sdmbMonth == "10") {
			sdmbMonthLib = "Octobre";
		} else if (sdmbMonth == "11") {
			sdmbMonthLib = "Novembre";
		} else if (sdmbMonth == "12") {
			sdmbMonthLib = "Decembre";
		} 
		
		var odmbOptionMonth = new Option(sdmbMonthLib, sdmbMonth);
		document.getElementById("dmb_form_recherche").dmb_date_mois.options[i-1] = odmbOptionMonth;

		// If i is the current month, we select it
		if (i == idmbMonth) {
			document.getElementById("dmb_form_recherche").dmb_date_mois.options[i-1].selected = true;
		}				
	}	
}
// ============================================================================
// dmbGetYearList
// ============================================================================
function dmbGetYearList (idmbYear) {
	var odmbOptionYear     = new Option(idmbYear, idmbYear);
	var odmbOptionNextYear = new Option(idmbYear+1, idmbYear+1);

	document.getElementById("dmb_form_recherche").dmb_date_annee.options[0]          = odmbOptionYear;
	document.getElementById("dmb_form_recherche").dmb_date_annee.options[0].selected = true;
	document.getElementById("dmb_form_recherche").dmb_date_annee.options[1]          = odmbOptionNextYear;
}

// ============================================================================
// dmbGetDayList
// ============================================================================
function dmbGetDayList (idmbDay) {
	// Listing days
	var idmbMonth = document.getElementById("dmb_form_recherche").dmb_date_mois.options[document.getElementById("dmb_form_recherche").dmb_date_mois.options.selectedIndex].value;
	var idmbYear  = document.getElementById("dmb_form_recherche").dmb_date_annee.options[document.getElementById("dmb_form_recherche").dmb_date_annee.options.selectedIndex].value;
	var odmbToday = idmbDay;
	dmbComputeDayValue(idmbDay, idmbMonth, idmbYear);
}

// ============================================================================
// dmbGetTime
// ============================================================================
function dmbGetTime()
{   
	var odmbToday  = new Date();
	var idmbHour   = DMB_DATE_HOUR;
	var idmbMinute = DMB_DATE_MINUTE;

	if(DMB_DATE_HOUR == -1){
		idmbHour   = odmbToday.getHours();
	}
	if(DMB_DATE_MINUTE == -1) {
		idmbMinute = odmbToday.getMinutes();
	}
	
	// Listing hours
	for (i=0; i<=23; i++) {
		// Prefixing hour by 0 if hour is less than 10.
		var sdmbHour = i;
		if (i < 10) {
			sdmbHour = "0" + i;
		}
		var odmbOptionHour = new Option(sdmbHour + "h", sdmbHour);
		document.getElementById("dmb_form_recherche").dmb_horr_heure.options[i] = odmbOptionHour;
	}
	
	for (j=0; j<=23; j++) {
		// If j is the current hour, we select it
		if (j == idmbHour) {
				// Proche de l'heure, on change d'heure
				if (idmbMinute <= 55) {
					document.getElementById("dmb_form_recherche").dmb_horr_heure.options[j].selected = true;
				}
				if (idmbMinute > 55) {
				    if(j < 23){
				        j = j+1;
								document.getElementById("dmb_form_recherche").dmb_horr_heure.options[j].selected = true;
				    }else{
				        document.getElementById("dmb_form_recherche").dmb_horr_heure.options[0].selected = true;
				    }
				  
				}
		}
	}
	
	// Listing minutes
	for (i=0; i<=11; i++) {
		// Prefixing hour by 0 if minutes are less than 10.
		var sdmbMinutes = i*5;
		if (sdmbMinutes < 10) {
			sdmbMinutes = "0" + sdmbMinutes;
		}
		
		var odmbOptionMinute = new Option(sdmbMinutes, sdmbMinutes);
		document.getElementById("dmb_form_recherche").dmb_horr_minute.options[i] = odmbOptionMinute;

		// If i is the current minute we select it
		if (i > 0 && (i-1)*5 < idmbMinute && i*5 >= idmbMinute) {
			// Proche de l'heure, on change d'heure
			if (idmbMinute <= 55) {
				document.getElementById("dmb_form_recherche").dmb_horr_minute.options[i].selected = true;
			} else {
				document.getElementById("dmb_form_recherche").dmb_horr_minute.options[0].selected = true;
			}
			
			if (idmbMinute == 0) {
		      document.getElementById("dmb_form_recherche").dmb_horr_minute.options[0].selected = true;
		  }
		}
	}

}

// ============================================================================
// dmbComputeDayValue
// ============================================================================
function dmbComputeDayValue(idmbDay, idmbMonth, idmbYear)
{
	var idmbStop = 31;
	
	// Setting upper bound of day for 30 day monthes
	if (
		idmbMonth==4 ||
		idmbMonth==6 || 
		idmbMonth==9 || 
		idmbMonth==11 
	) {
		--idmbStop;
	}
	// Setting upper bound of day for february
	if (idmbMonth==2) 
	{
		idmbStop = idmbStop - 3;
		if (idmbYear%4==0) {
			idmbStop++;
		}
		if (idmbYear%100==0) {
			idmbStop--;
		}
		if (idmbYear%400==0) {
			idmbStop++;
		}
	}

	document.getElementById("dmb_form_recherche").dmb_date_jour.options.length = 0;
	document.getElementById("dmb_form_recherche").dmb_date_jour.clear;

	var idmbCount = 0;
	var sdmbDay   = 0;			
	for(idmbCount=1; idmbCount <= idmbStop; idmbCount++)
	{
		//concaténation d'un 0 si le mois est < 10.
		sdmbDay = idmbCount;
		if (idmbCount < 10)
		{
			sdmbDay = "0" + idmbCount;
		}
		var odmbOptionDay = new Option(sdmbDay, sdmbDay);
		document.getElementById("dmb_form_recherche").dmb_date_jour.options[idmbCount-1] = odmbOptionDay;
		//Si c'est le jour en cours, on selectionne l'entrée.
		if(idmbCount == idmbDay)
		{
			document.getElementById("dmb_form_recherche").dmb_date_jour.options[idmbCount-1].selected = true;
		}
	}
	
}

/*function fullscr(url) {
	
  window.open(url,'','top=0,left=0,width='+screen.width
     +',height='+screen.height+',directories=yes,status=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes');
  } */
