var ancien_no = new Array();
ancien_no.push(1);
var regExpBeginning = /^\s+/;
var regExpEnd = /\s+$/;

function listRoom(no){
<!--     
          e	=	getById('div_select_age_per_room');
          display('valider_div_select_age_per_room');
          len	=	ancien_no.length;
          
          content	=	'<label for="roomQuantity" class="marge_label">Chambres</label>';
          content	+=	'<select name="roomQuantity" id="roomQuantity" onchange="listRoom(getById(\'roomQuantity\').value);eliminateRoomChild(getById(\'roomQuantity\').value);ancien_no.push(getById(\'roomQuantity\').value);">';
		  for(i=1;i<=3;i++){
		  	content	+=	'<option value="'+i+'"'
		  	if(i==no)
		  	content += 	'selected="selected"';
		  	content +=	'>'+i+'</option>';
		  }
		  content	+=	'</select>';
          
          
          content1	=	'<label for="roomQuantity" class="chambre_unique">Chambres</label><select name="roomQuantity" id="roomQuantity" onchange="listRoom(getById(\'roomQuantity\').value);eliminateRoomChild(getById(\'roomQuantity\').value);ancien_no.push(getById(\'roomQuantity\').value);">';
          for(i=1;i<=3;i++){
         	 content1	+=	' <option value="'+i+'">'+i+'</option>';
          }
          content1	+=	'</select>';
          
          if(no>1){
          getById('chambre_unique').innerHTML	=	content;
          getById('multiple_chambres').innerHTML	=	'<span class="multiple_chambres1">Chambre 1: </span>';
          getById('multiple_chambres').className	=	'toto';
          
          for(i=2;i<=no;i++){
	          toThis  =   '';
	          toThis+=    '<span class="multiple_chambres1">Chambre '+i+': </span>';
	          toThis+=    '<span class="multiple_chambres"><label for="adultQuantity_'+i+'" class="multiple_chambres">Adulte(s):</label>';
	          toThis+=    '<select name="adultQuantity_'+i+'" id="adultQuantity_'+i+'" >';
	          for(j=1;j<=4;j++){
	               toThis+=    '<option value="'+j+'">'+j+'</option>';
	          }
	          toThis+=    '</select></span>';
	          toThis+=    '<span class="multiple_chambres"><span class="multiple_chambres"><label for="childQuantity_'+i+'" class="multiple_chambres">Enfant(s):</label>';
	          toThis+=    '<select name="childQuantity_'+i+'" id="childQuantity_'+i+'" class="chambres" onchange="showhideSelectAge('+i+',getById(\'childQuantity_'+i+'\').value)" style="display:block;">';
	          for(k=0;k<=2;k++){
	               toThis+=    	'<option value="'+k+'"';
	               if(getById('childQuantity_'+i) && getById('childQuantity_'+i).value == k){
	               		toThis+=		' selected="selected"';
	               }
	               toThis+=		'>'+k+'</option>';
	          }
	          toThis+=    '</select></span>';
	          show(i,toThis);
	          
            }
            for(l=i;l>no && l<=3;l++){
                show(l,'');
            }
        }else{
          getById('chambre_unique').innerHTML	=	'<br/>';
          getById('multiple_chambres').innerHTML	=	content1;
          getById('multiple_chambres').className	=	'multiple_chambres';
          hide('valider_div_select_age_per_room');
          hide('lien_occupation');
          if(len>1){
          	//alert('vous avez éliminé une ou plusieurs chambres');
          }
          show(2,'');
          show(3,'');
        }
        return null;
//-->       
    }

    function show(id,content){
<!--
            getById("line_"+id).innerHTML = content;
//-->
    }
    
        function display(id){
<!--
            getById(id).style.display = 'block';
 
//-->
    }
    

      	function hide(id){
<!--
            getById(id).style.display = 'none';
//-->
    }
    
    	function eliminateRoomChild(indice){
<!--
		//alert(indice);
		for(uu=3;uu>indice;uu--){
			//show('age_select_1_for_room_'+i,'');
			//show('age_select_2_for_room_'+i,'');
			//show('age_select_3_for_room_'+i,'');
			//if(uu==indice) break;
			hide('label_children_'+uu);
		}
//-->
    }
    
    
    function buidSelectAge(room,child){
<!--
		toreturn 	 =	'<label class="chambre_unique" for="room_'+room+'_child_'+child+'">Age '+i+':</label>';
		toreturn	+=	'<select id="room_'+room+'_child_'+child+'" class="child_age" name="room_'+room+'_child_'+child+'">';
		toreturn	+=	'<option value="1">&lt;=1</option>';
		for(j=2;j<=18;j++){
			toreturn	+=	'<option value="'+j+'">'+j+'</option>';
		}
		toreturn	+=	'</select>';
		if(i==2)
		toreturn	+=	'';
		
		show("age_select_"+child+"_for_room_"+room,toreturn);

//-->
    
    }
    
    
    function showhideSelectAge(room,number_of_selects){
<!--
		if(number_of_selects==0) hide("label_children_"+room);
		else display("label_children_"+room);
		
		for(i=1;i<=number_of_selects;i++){
			buidSelectAge(room,i);
		}
		
		for(l=i;l>number_of_selects && l<=2;l++){
            show("age_select_"+l+"_for_room_"+room,'');
            }
//-->
    
    }
    
        function populateCalendar(){
<!--

            dte	=	getById("DateRangeStart").value.split("\/");
            getById('duration').selectedIndex	=	0;
        
        dateStart	=	new Date(parseInt(dte[2],10), parseInt(dte[1],10)-1, parseInt(dte[0],10));
        timestamp	=	dateStart.getTime()/1000;
        timestamp2	=	timestamp+(3600*24);
        dateEnd	=	new Date(timestamp2*1000);
        mounth =	parseInt(dateEnd.getMonth(),10)+1;
        day		=	parseInt(dateEnd.getDate(),10);
        if(day<10) day	=	"0"+day;
        if(mounth<10) mounth	=	"0"+mounth;
        DateAffich	=	""+day+"/"+mounth+"/"+dateEnd.getFullYear();
       	getById("DateRangeEnd").value = DateAffich;
//-->
    }
    
    function checkCalendar(){
<!--
        dte	=	getById("DateRangeStart").value.split("\/");

        dateStart	=	new Date(parseInt(dte[2],10), parseInt(dte[1],10), parseInt(dte[0],10));
        timestamp	=	dateStart.getTime()/1000;
        timestamp2	=	timestamp+(3600*24);
        dateEnd	=	new Date(timestamp2*1000);
        //if(dateStart>DateEnd) alert('azsf');
        mounth =	parseInt(dateEnd.getMonth(),10);
        day		=	parseInt(dateEnd.getDate(),10);
        if(day<10) day	=	"0"+day;
        if(mounth<10) mounth	=	"0"+mounth;
        DateAffich	=	""+day+"/"+mounth+"/"+dateEnd.getFullYear();
        getById("DateRangeEnd").value = DateAffich;
//-->
    }
    
    function switchDiv(obj){
<!--
		
		var el = getById(obj);
		
		if(el.style.display != "block"){
		el.style.display = "block";
		}else{
		el.style.display = "none";
		}
//-->
}

	 function SetDefaultDateStart(){
<!--	 	
	 	dateStart	=	new Date();
        timestamp	=	(dateStart.getTime()/1000)+(3600*24*7);
        dateStart	=	new Date(timestamp*1000);
        mounth =	parseInt(dateStart.getMonth(),10);
        alert(mounth);
        day		=	parseInt(dateStart.getDate(),10);
        if(day<10) day	=	"0"+day;
        if(mounth<10) mounth	=	"0"+mounth;
        DateAffich	=	""+day+"/"+mounth+"/"+dateStart.getFullYear();
        getById("DateRangeStart").value = DateAffich;
       
//-->
	 }
	 
	 function SetContent(id,content){
<!--
	 	
            getById(id).value	=	content;
//-->
	 }
	 
    
     function UpdateDateRangeEnd(duration){
<!--
        
        dte	=	getById("DateRangeStart").value.split("\/");
        

        dateStart	=	new Date(parseInt(dte[2],10), parseInt(dte[1],10)-1, parseInt(dte[0],10));
        timestamp	=	dateStart.getTime()/1000;
        timestamp2	=	timestamp+(3600*24*duration);
        dateEnd	=	new Date(timestamp2*1000);
        mounth =	parseInt(dateEnd.getMonth(),10)+1;
        day		=	parseInt(dateEnd.getDate(),10);
        if(day<10) day	=	"0"+day;
        if(mounth<10) mounth	=	"0"+mounth;
        DateAffich	=	""+day+"/"+mounth+"/"+dateEnd.getFullYear();
       	getById("DateRangeEnd").value = DateAffich;
        
//-->
    }
    
    function getById(id){
<!--    	
    	if(document.getElementById){
    		toreturn	=	document.getElementById(id);
    	}else{
    		toreturn	=	document.all[id];
    	}
    	return toreturn;
//-->
    }
    
    function submitFormResa(){
<!--    

    	if((getById('mail').value != getById('cmail').value) || getById('mail').value == "" || getById('cmail').value == ""){
    		alert('les deux champs d\'adresse electronique et confirmation de l\'adresse electronique doivent être identiques et non vides');
    		getById('mail').focus();
    		return false;
    	}else if(getById('prenom').value == ""){
    		alert('le champ prenom est obligatoire');
    		getById('prenom').focus();
    		return false;
    	}else if(getById('nom').value == ""){
    		alert('le champ nom est obligatoire');
    		getById('nom').focus();
    		return false;
    	}else if(getById('adresse').value == ""){
    		alert('le champ adresse est obligatoire');
    		getById('adresse').focus();
    		return false;
    	}else if(getById('ville').value == ""){
    		alert('le champ ville est obligatoire');
    		getById('ville').focus();
    		return false;
    	}else if(getById('tel').value == ""){
    		alert('le champ numéro de télephone est obligatoire');
    		getById('tel').focus();
    		return false;
    	}else if(getById('approuve').checked != true){
    		alert('veuillez approuver la politique relative au tarif et à la chambre réservée');
    		getById('approuve').focus();
    		return false;
    	}else{
    		getById('form_resa_submit').style.display	=	'none';
    		getById('loading_resa').style.display	=	'block';
    		getById('paiement').submit();
    	}
//-->
    }
    
    function formatdate(id){
<!--
		var ey	=	getById(id);
		val	=	ey.value;
		if(val.length ==2 || val.length == 5){
			ey.value	+= '/';
		}
//-->
	}
	
	
	// Supprime les espaces inutiles en début et fin de la chaîne passée en paramètre.

	function trim(aString) {
<!--
		return aString.replace(regExpBeginning, "").replace(regExpEnd, "");
	//-->
	}
	
	function MaxArray( array ){
<!--    
    return Math.max.apply( Math, array );
//-->
}

	function MinArray( array ){
<!--
    return Math.min.apply( Math, array );
//-->
}

	// ouvre une fenêtre en popup
	
	function open_window(url, height, width){
<!--
	 window.open(url, 'popup', 'height='+height+', width='+width+', toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
//-->	
	}
	
     
     function SetCookie (name, value, expires, path, domain, secure){
        var expDate = new Date();
        expDate.setTime(expDate.getTime() + (expires * 24 * 3600 * 1000));
         		bwfr_cookie = name + "=" + escape (value) +
                  ((expires) ? "; expires=" + expDate.toGMTString() : "") +
                  ((path) ? "; path=" + path : "") +
                  ((domain) ? "; domain=" + domain : "") +
                  ((secure) ? "; secure" : "");
                  document.cookie = bwfr_cookie;
	}
	
	
	function number_format( number, decimals, dec_point, thousands_sep ) {
		var n = number, prec = decimals;
	    n = !isFinite(+n) ? 0 : +n;
	    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
	    var sep = (typeof thousands_sep == "undefined") ? ',' : thousands_sep;
	    var dec = (typeof dec_point == "undefined") ? '.' : dec_point;
	 
	    var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
	 
	    var abs = Math.abs(n).toFixed(prec);
	    var _, i;
	 
	    if (abs >= 1000) {
	        _ = abs.split(/\D/);
	        i = _[0].length % 3 || 3;
	 
	        _[0] = s.slice(0,i + (n < 0)) +
	              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
	 
	        s = _.join(dec);
	    } else {
	        s = s.replace('.', dec);
	    }
	    return s;
	}
	
	okcc	=	true;


	function check_credit_card(CardNumber,CardCode){
			function setOkCC(val){
			okcc =  val;
		}
		
		function getOkCC(){
			return okcc;
		}
		
		$.post("check_creditcard.jsp",{CardNumber:CardNumber,CardCode:CardCode},
			function success(data){ // au succès on renvoie le résultat de la requête  
            	if(trim(data)=="ok"){
            		//getById('is_valid_card').value	=	"1";
            		setOkCC(true);
            	}else{
            		//alert(trim(data));
            		//getById('is_valid_card').value	=	"0";
            		setOkCC(false);
            	}
       });
       return getOkCC();
	}
