﻿/**
 * productInit.js
 * @author Pierre-Anthony clolus (TS)
 * Last modified August 7, 2008 by Pierre-Anthony clolus(TS)
 */

function checkVisible2(value) {
   // --- Check status of check box, if checked => display list
   if ( value=='XXX' ){
     document.getElementById('city').disabled = true;
     document.getElementById('withoutTransport').value='XXX';
	 document.getElementById('withoutTransport').checked=true;
   }
   // --- Else disable list
   else {
     document.getElementById('city').disabled = false;
     document.getElementById('withoutTransport').value='';
	 document.getElementById('withoutTransport').checked=false;
   }
   ///alert(document.getElementById('withoutTransport').value);
	}


	function promotionvalue2(value) {
   // --- Check status of check box, if checked => display list
   if ( value=='1' ){
     document.getElementById('onSale').value='1';
	 document.getElementById('onSale').checked=true;
   }
   // --- Else disable list
   else {
     document.getElementById('onSale').value='';
	 document.getElementById('onSale').checked=false;
   }
   ///alert(document.getElementById('withoutTransport').value);
	}

function checkVisible() {
   // --- Check status of check box, if checked => display list
   if ( document.getElementById('withoutTransport').checked ){
	 document.getElementById('city').selectedIndex = 0;
     document.getElementById('city').disabled = true;
     document.getElementById('withoutTransport').value='XXX';
   }
   // --- Else disable list
   else {
     document.getElementById('city').disabled = false;
     document.getElementById('withoutTransport').value='';
   }
   ///alert(document.getElementById('withoutTransport').value);
	}

/**
 *  put the value 'oui' for the protion check box
 */
	function promotionvalue() {
   // --- Check status of check box, if checked => value='oui'
   if ( document.getElementById('onSale').checked ){
     document.getElementById('onSale').value='1';
   }
   // --- Else value=''
   else {
     document.getElementById('onSale').value='';
   }
   ///alert(document.getElementById('onSale').value);
	}

var destCities = [];

/**
* Ajout des villes de destination
* @param {cityLabel) label of the city
* @param {cityCode) code of the label
* @param {citiesList) list of cities
* @param {labelsList) list of cities
*/
	function addDestCity(cityLabel, cityCode, citiesList, labelsList) {
	if (!citiesList) {
		citiesList = [];
	}
	var index = 0;
	var foundPosition = false;
	while (index < labelsList.length && !foundPosition) {
		var currentLabel = labelsList[index];
		if (currentLabel > cityLabel) {
			foundPosition = true;
		}
		index++;
	}
	if((cityCode!="N/A")||(cityLabel!="Non communiqué")){
		labelsList.splice(index, 0, cityLabel);
		citiesList.splice(index, 0, cityCode + '||' + cityLabel);
	}
}

/**
* mise a jour de la liste des villes de depart
*/
function updateDepCities() {

	var citiesSelectBoxOptions = document.getElementById('cityOfDestination').options;
	var selectedCountryIndex = document.getElementById('destination').selectedIndex;

	citiesSelectBoxOptions.length = 0;
	citiesSelectBoxOptions[0] = new Option('Indifférent', '');

	if (selectedCountryIndex >= 1) {
		var citiesList = destCities[selectedCountryIndex - 1];
		var i;
		for (i = 0; i < citiesList.length; i++) {
			var city = citiesList[i];
			var separatorIndex = city.indexOf('||');
			var cityCode = city.substring(0, separatorIndex);
			var cityLabel = city.substring(separatorIndex + 2, city.length);
			citiesSelectBoxOptions[citiesSelectBoxOptions.length] = new Option(cityLabel, cityCode);
		}
	}
}

/**
* mise a jour de la liste des villes de depart avec prise en compte de
* de la valeur presente dans l'url
*/
function keepCity(value) {

	///alert(value);
	var citiesSelectBoxOptions = document.getElementById('cityOfDestination').options;
	var selectedCountryIndex = document.getElementById('destination').selectedIndex;
	citiesSelectBoxOptions.length = 0;
	citiesSelectBoxOptions[0] = new Option('Indifférent', '');

	if (selectedCountryIndex >= 1) {
		var citiesList = destCities[selectedCountryIndex - 1];
		var i;

		for (i = 0; i < citiesList.length; i++) {
			var isSelected=false;
			var city = citiesList[i];
			var separatorIndex = city.indexOf('||');
			var cityCode = city.substring(0, separatorIndex);
			var cityLabel = city.substring(separatorIndex + 2, city.length);
			if(value==cityCode){isSelected=true;}
			citiesSelectBoxOptions[citiesSelectBoxOptions.length] = new Option(cityLabel, cityCode,false,isSelected);
		}
	}
}

/**
* Recuperation de la valeur du tri
* @param {value) value of the sort
*/
function setSortType(value) {
    document.getElementById('st').value = value;
    document.forms[0].submit();
}



function getQueryString(queryString,name){

var parameters = queryString.split("&");

var pos, paraName, paraValue;

for(var i=0; i<parameters.length; i++){
pos = parameters[i].indexOf('=');

if(pos == -1) { continue; }

paraName = parameters[i].substring(0, pos);

paraValue = parameters[i].substring(pos + 1);

if(paraName == name){
return unescape(paraValue.replace(/\+/g, " "));
}
}
return '';

}



function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 ;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for ( i = 0; i < a_all_cookies.length; i++ )
    {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split( '=' );


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if ( cookie_name == check_name )
        {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if ( a_temp_cookie.length > 1 )
            {
                cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if ( !b_cookie_found )
    {
        return null;
    }
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


function initialSelection(criteria){

var destination=getQueryString(criteria,'dtco');
var promotion=getQueryString(criteria,'pro');
var city=getQueryString(criteria,'dpci');
var destinationCity=getQueryString(criteria,'dtci');
var dd=getQueryString(criteria,'dd');
var dmy=getQueryString(criteria,'dmy');
var aj=getQueryString(criteria,'aj');
var duration=getQueryString(criteria,'minMan');
var type=getQueryString(criteria,'c.TYP');
var budget=getQueryString(criteria,'mmp');

if(promotion==1)
document.getElementById('onSale').checked=true;
if(city=='XXX')
document.getElementById('withoutTransport').checked=true;
if(document.getElementById('time'))
document.getElementById('time').value=duration;
if(document.getElementById('formules'))
document.getElementById('formules').value=type;
if(document.getElementById('priceRange'))
document.getElementById('priceRange').value=budget;
if(document.getElementById('dtci'))
document.getElementById('dtci').value=destinationCity;
if(document.getElementById('city'))
document.getElementById('city').value=city;
if(document.getElementById('dateOfDeparture')){
	if(dd!=''){
		document.getElementById('dateOfDeparture').value=dd+"/"+dmy;
	}
}
if(document.getElementById('aj'))
document.getElementById('aj').value=aj;
if(document.getElementById('destination'))
document.getElementById('destination').value=destination;

if(document.getElementById('cityOfDestination')){
		document.getElementById('cityOfDestination').disabled = false;
		var citiesSelectBoxOptions = document.getElementById('cityOfDestination').options;
		var selectedCountryIndex = document.getElementById('destination').selectedIndex;

		citiesSelectBoxOptions.length = 0;
		citiesSelectBoxOptions[0] = new Option('Indifférent', '');

		if (selectedCountryIndex >= 1) {
			var citiesList = destCities[selectedCountryIndex - 1];
			var i;
			for (i = 0; i < citiesList.length; i++) {
				var city = citiesList[i];
				var separatorIndex = city.indexOf('||');
				var cityCode = city.substring(0, separatorIndex);
				var cityLabel = city.substring(separatorIndex + 2, city.length);
				if(cityCode==destinationCity){
				citiesSelectBoxOptions[citiesSelectBoxOptions.length] = new Option(cityLabel, cityCode, false,true);}
				else{citiesSelectBoxOptions[citiesSelectBoxOptions.length] = new Option(cityLabel, cityCode);}
			}
		}
	}
}

// function to rebuilt the search url
function buildFormQuerySearchEngine() {
    var result = "";
    for (var i = 0; i < document.forms[0].elements.length; i++) {
        var el = document.forms[0].elements[i];

        if (el.tagName.toLowerCase() == "select") {

            for (var j = 0; j < el.options.length; j++) {
                var op = el.options[j];
                if (op.selected && op.value!="")
                    result += "&" + encodeURI(el.name) + "=" + encodeURI(op.value);
            }
        } else if (el.tagName.toLowerCase() == "textarea") {
            result += "&" + encodeURI(el.name) + "=" + encodeURI(el.value);
        } else if (el.tagName.toLowerCase() == "input") {
            if (el.type.toLowerCase() == "checkbox" || el.type.toLowerCase() == "radio") {
                if (el.checked)
                    result += "&" + encodeURI(el.name) + "=" + encodeURI(el.value);
            } else if (el.type.toLowerCase() == "submit") {
               // if (el == submitButton) // is "el" the submit button that fired the form submit?
               //     result += "&" + encodeURI(el.name) + "=" + encodeURI(el.value);
            } else if (el.type.toLowerCase() != "button") {
                if(el.style.display != 'none' && el.value!="") {
                        result += "&" + encodeURI(el.name) + "=" + encodeURI(el.value);
                }
            }
        }
    }
    return result.substr(1, result.length - 1);
}

function buildFormSearchEngine() {
      var actionStr = document.forms[0].action;
      var getUrl;
      getUrl=actionStr;
      window.location.href= getUrl+ '?' + buildFormQuerySearchEngine();
}