// JavaScript Document
function makeItBlack(e){
	var zone = Event.element(e);
	//zone.tagName
	if (zone.tagName != "LI") {
		zone = zone.up();
	}
	var expression = /(span)/i;
	if (!expression.test(zone.innerHTML)) {
		zone.style.background = 'black';
		zone.style.color = 'white';
	}
}

function makeItWhite(e){
	var zone = Event.element(e);
	if (zone.tagName != "LI") {
		zone = zone.up();
	}
	var expression = /(span)/i;
	if (!expression.test(zone.innerHTML)) {
		zone.style.background = 'white';
		zone.style.color = 'black';
	}
}

function selectValue(e){
	var zone = Event.element(e);
	var txt = zone.innerHTML;
	var idinVal = "in"+zone.id;
	if ($(idinVal)) {
		var input = $(idinVal).value;
	
		var expression = /(Indisponible)/;
	
		if( !expression.test( txt ) ){
		
			var replace = zone.parentNode.id.replace( 'ul','text' );
			$(replace).update(txt);
			var replace2 = zone.parentNode.id.replace( 'ul','' );
			$(replace2).value = input;
		}
			
	}

}

function clearPopup (id){
	$('text'+id).update('&nbsp;');
	$('ul'+id).update('');
}

function populatePopup(id, value){
	
	var first = true;
	for (var i = 0 ; i< value.length; i++) {
		
		
		var li = document.createElement('li');
		var input = document.createElement('input');
		
		li.setAttribute('id','li'+id+i);
		li.style.padding = '0 5px';
		$('ul'+id).appendChild( li );
		$('li'+id+i).update(value[i].value);
		
		
		input.setAttribute('id','inli'+id+i);
		input.setAttribute('value',value[i].id);
		input.setAttribute('type','hidden');
		$('ul'+id).appendChild( input );
		
		
		/*Event.observe(li, 'mouseover' , makeItBlack );
		Event.observe(li, 'mouseout' , makeItWhite );
		Event.observe(li, 'click' , selectValue );*/
		Event.observe('li'+id+i, 'mouseover' , makeItBlack );
		Event.observe('li'+id+i, 'mouseout' , makeItWhite );
		if (value[i].change != undefined && value[i].change != null){
			Event.observe('li'+id+i, 'click' , value[i].change );
		} else {
			Event.observe('li'+id+i, 'click' , selectValue );
		}
		
		//alert("click : "+li);
		if (first) {
			$('text'+id).update(value[i].value);
			$(id).value = value[i].id;
			first = false;
		} 
		
	}
	
}

function creatPopup (id, parent, width) {
	var addstyle = '';
	
	if (width != null) {
		addstyle += 'style="width:'+width+'px"';
	}
	
	
	var popup = '<div id="select'+id+'" >';
	popup += '<div class="global" '+addstyle+'>';						
	popup += '<p onclick="colorPopup(\'test'+id+'\');">';							
	popup += '  <span class="liste" id="text'+id+'">';
	popup += '  </span>';
	popup += '  <input type="hidden" value="0" id="'+id+'" name="'+id+'"/>';
	popup += '  <img src="http://media.brandalley.com/travel/shops/1/common/picto_fleche.gif" />';					
	popup += '</p>';
	popup += '<div class="href" id="test'+id+'" '+addstyle+'>';
	popup += '	<ul id="ul'+id+'">';
	popup += '	</ul>';
	popup += '</div>';
	popup += '</div>';
	popup += '</div>';
	
	$(parent).innerHTML += popup;
}

function repopulatePopup (id, value){
	clearPopup(id);
	populatePopup(id, value);
}

function colorPopup(id) {
	
	if( typeof( rememberid ) != 'undefined' && rememberid != id )
		$(rememberid).style.display = 'none';
		
	select_zone = $(id);
	var styleDisplay = select_zone.getStyle('display');
	
	switch( styleDisplay ){
		case 'block':
			select_zone.style.display = 'none';
		break;
		default:
			select_zone.style.display = 'block';
			rememberid = id;
	}

	closeit( 0 );
}

function closeit( incr ){

	document.getElementsByTagName('body')[0].onclick  = function(){
		if( incr > 0 ){
			select_zone.style.display = 'none';
		}
		incr++;
	}
}

function required() {
    tochecked = document.getElementById('Oui');
    tochecked.checked = 'checked';
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function showSearch() {
	if ( $('menuSearchForm').style.display == "none" ) {
		Effect.BlindDown('menuSearchForm', {duration:0.5});
	} else {
		Effect.BlindUp('menuSearchForm', {duration:0.5});
	}
}


function showDestCity() {
	var countryId = $F('searchDestCountry');
	
	
//	var rang = 1;
//	$('searchDestCity').options.length = 0;
//	$('searchDestCity')[0] = new Option('', '---');
//	for (var i in startCity ) {
//		if (startCity[i].countryId == countryId) {
//			//alert(startCity[i].name);
//			$(destCity)[rang] = new Option(startCity[i].name, startCity[i].code);
//			rang++;
//		}
//	}
	
	var compt = 1;
	var value = new Array();
	value[0] = {
			id : '---',
			value : 'Indiff&eacute;rent'
	};
	
	for ( var i in startCity) {
		if (startCity[i].countryId == countryId) {
			value[compt] = {
					id : startCity[i].code,
					value : startCity[i].name
			};
			compt++;
		}
	}
	repopulatePopup('searchDestCity', value);
	
	
	if($('divsearchDestCity').style.display == "none"){
		Effect.BlindDown('divsearchDestCity', {duration:0.5});;
	} else if ( value.length == 1 ) {
		Effect.BlindUp('divsearchDestCity', {duration:0.5});;
	}
}

function resetSearchForm() {
	
	$('textsearchDep').update('Indiff&eacute;rent');
	$('searchDep').value = '---';
	
	$('textsearchDestCountry').update('Indiff&eacute;rent');
	$('searchDestCountry').value = '---';
	
	$('textsearchDestCity').update('Indiff&eacute;rent');
	$('searchDestCity').value = '---';

	$('searchDateStart').value = '';
	
	$('textsearchDayFlex').update('Indiff&eacute;rent');
	$('searchDayFlex').value = '---';
	
	
	
	if ($('textcategory') != undefined) {
		$('textcategory').update('Indiff&eacute;rent');
		$('category').value = '---';
	}
	
	if ($('textsearchNbDays') != undefined) {
		$('textsearchNbDays').update('Indiff&eacute;rent');
		$('searchNbDays').value = '0';
	}
	
	if ($('textsearchBudget') != undefined) {
		$('textsearchBudget').update('Indiff&eacute;rent');
		$('searchBudget').value = '0';
	}
	
	if ($('textcr2') != undefined) {
		$('textcr2').update('Indiff&eacute;rent');
		$('cr2').value = '0';
	}
	
	if ($('textcr1') != undefined) {
		$('textcr1').update('Indiff&eacute;rent');
		$('cr1').value = '12';
	}
	
}

Array.prototype.in_array = function(p_val) {
    for(var i = 0, l = this.length; i < l; i++) {
        if(this[i] == p_val) {
            rowid = i;
            return true;
        }
    }
    return false;
}


