function addEvent( obj, type, fn )	{
  if(obj!=null) {
    if (obj.addEventListener) {
      obj.addEventListener( type, fn, false );
    } else if (obj.attachEvent)	{
      obj["e"+type+fn] = fn;
      obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
      obj.attachEvent( "on"+type, obj[type+fn] );
    }
  }
}
function initializer() {
  var frmClient = document.getElementById('frmClient');
  addEvent(frmClient, 'submit', controleClient);
  frmClient.onsubmit = function() { return false; }
}

//if (window) addEvent(window, 'load', initializer);
//addEvent(document.getElementById('items'), 'change', alert('ici'));


function displayCalendar(annee, mois) {
	try {
	  room_id = document.getElementById('selectedRoom').value;
	 } catch(e) {
	 	 alert("Sélectionnez une chambre");
	 	 return;
	 }
	 if(isNaN(room_id)) {
	    alert("Sélectionnez une chambre");
	 } else {
	    getRoomCalendar(room_id, annee, mois);
	    for(i=0; i<12; i++){
	    	document.getElementById('liMois'+i).className='';
		}
		date = new Date();
		if( mois>= (date.getMonth()+1)) {
		    i=mois-(date.getMonth()+1);
		} else {
		    i= 12+mois-(date.getMonth()+1);
		}
		document.getElementById('liMois'+i).className= 'selectedLiMois';
	 }
}



//the main function, call to the effect object
function init(){
  var hwdClient;
  var hwdCalendar;
}


function callbackDebug(request){
}

function callbackDebugCalendar() {
	hideLoader("loadingCalendar");
}

function getRoomCalendar(room_id, annee, mois) {
	showLoader("loadingCalendar");
	var postVars = "action=getRoomCalendar&location_id="+encodeURIComponent(room_id)+"&y="+encodeURIComponent(annee)+"&m="+encodeURIComponent(mois);
	hwd = new ajax ('../../../reservation/_v2/action.php', {
          postBody: postVars,
          update: $('divCalendar'),
          onComplete: callbackDebugCalendar
          });
}

function displayRoom(k) {
  if(!isReservable){
    getRoomCalendar(k,0,0);
    getRoomInfo(k);
    return false;
  } else {
    if (confirm('Vous souhaitez modifier votre séjour ?')) {
      recapituler();
      getRoomCalendar(k,0,0);
      getRoomInfo(k);
      return false;
    }
  }
}


function displayNbNuits(dateArrivee) {
	showLoader("loadingCart");
	var postVars = "action=displayNbNuits&dateArrivee="+encodeURIComponent(dateArrivee);
  	hwd = new ajax ('../../../reservation/_v2/action.php', {
          postBody: postVars,
          update: $('divNbNuits'),
          onComplete: callbackCart
          });
}

function setArrival(y,m,d) {
	document.getElementById('dateArrivee').value = LZ(d)+"/"+LZ(m)+"/"+y;
	//displayCalendar(y,m,d);
	displayNbNuits(document.getElementById('dateArrivee').value);
}


function changeNbNuits() {
  var postVars = "action=changeNbNuits&nb="+document.getElementById("nbNuits").value;
  hwdCalendar = new ajax ('../../../reservation/_v2/action.php', {
          postBody: postVars,
          update: $('divNbNuits'),
          onComplete: callbackCart
          });
}

function callbackCart(request){
  //alert(request.responseText);
  controleCart();
}

function controleCart() {
	showLoader("loadingCart");
	var postVars = "action=controleCart";
	hwd  = new ajax ('../../../reservation/_v2/action.php', {
          postBody: postVars,
          update: $('items'),
          onComplete: callbackCartDebug
          });
}
function callbackCartDebug(request){
	hideLoader("loadingCart");
}

function updateOccupants(elt, mode) {
	showLoader("loadingCart");
 	if (mode=="update") {
    	valeur = document.getElementById(elt.name).value;
    	param = elt.name;
 	} else {
    	param = elt;
    	valeur = 0;
  	}
  	var postVars = "action=updateOccupants&mode="+encodeURIComponent(mode)+"&elt="+encodeURIComponent(param)+"&value="+encodeURIComponent(valeur);
  	hwd  = new ajax ('../../../reservation/_v2/action.php', {
          postBody: postVars,
          update: $('items'),
          onComplete: callbackCartDebug
          });
}






function callbackPaiementDebug(request) {
	//addEvent(document, 'click', controlePaiement);
  	//document.onclick = function() { return false; }
}


function accessPayment() {
	document.getElementById('divBtnCloseRecap').style.visibility = 'hidden';
	document.getElementById('divRecapModif').style.visibility = 'hidden';
	var postVars = "action=callPaiement"; //&content="+str;
  	hwd  = new ajax ('../../../reservation/_v2/action.php', {
          postBody: postVars,
          update: $('divBtnRecapPaiement'),
          onComplete: callbackPaiementDebug
          });
}

function callbackCartValidate(request) {
	popRecapActivate(document.getElementById('divPopRecap'),'anchorRecap');

}
function controleAll() {
	if (!controleClient()) {
		return false;
	}
	if (!document.getElementById('chkCGV').checked) {
		alert("Vous devez accepter les Conditions Générales de Vente");
		return false;
	}
	if(document.getElementById('nbItems').value==0) {
		alert("Vous devez réserver au moins une chambre");
		return false;
	}
	if(!controleOccupants()) {
		alert("Vous devez renseigner le nombre de personnes par chambre");
		return false;
	}
	var postVars = "action=validateCart";
	postVars += '&client_civilite='+encodeURIComponent(document.getElementById('client_civilite').value);
    postVars += '&client_nom='+encodeURIComponent(document.getElementById('client_nom').value);
    postVars += '&client_prenom='+ encodeURIComponent(document.getElementById('client_prenom').value);
    postVars += '&client_adresse1='+ encodeURIComponent(document.getElementById('client_adresse1').value);
	postVars += '&client_adresse2='+ encodeURIComponent(document.getElementById('client_adresse2').value);
    postVars += '&client_codepostal='+encodeURIComponent(document.getElementById('client_codepostal').value);
    postVars += '&client_ville='+ encodeURIComponent(document.getElementById('client_ville').value);
    postVars += '&client_pays='+ encodeURIComponent(document.getElementById('client_pays').value);
    postVars += '&client_telephone='+ encodeURIComponent(document.getElementById('client_telephone').value);
    postVars += '&client_email='+ encodeURIComponent(document.getElementById('client_email').value);
    postVars += '&client_heurearrivee='+ encodeURIComponent(document.getElementById('client_heurearrivee').value);
    postVars += '&client_commentaire='+ encodeURIComponent(document.getElementById('client_commentaire').value);
	postVars += '&chkCGV='+ encodeURIComponent(document.getElementById('chkCGV').value);
 	//alert(postVars);
	hwdClient = new ajax ('../../../reservation/_v2/action.php', {
	    postBody: postVars,
	    update: $('divPopRecap'),
	    onComplete: callbackCartValidate
	    });

}

function callbackCancel(request) {
	hideLoader("loadingCart");
	document.location.href="indexV2.php";
}
function annulation() {
	showLoader("loadingCart");
	var postVars = "action=cancel";
  	hwd  = new ajax ('../../../reservation/_v2/action.php', {
          postBody: postVars,
          update: $('divPopRecap'),
          onComplete: callbackCancel
          });
}

/* declaration */
var isReservable = false;

function addRoom() {
	showLoader("loadingCart");
	var postVars = "action=addRoom&room_id="+encodeURIComponent(document.getElementById('selectAnotherRoom').value);
	hwd  = new ajax ('../../../reservation/_v2/action.php', {
          postBody: postVars,
          update: $('items'),
          onComplete: callbackCartDebug
          });
}

function removeRoom(num) {
	showLoader("loadingCart");
	var postVars = "action=removeRoom&num="+encodeURIComponent(num);
	hwd  = new ajax ('../../../reservation/_v2/action.php', {
          postBody: postVars,
          update: $('items'),
          onComplete: callbackCartDebug
          });
}




var popOptions = new PopupWindow("divPopOptions");
popOptions.offsetX= 20;
popOptions.offsetY= 20;
popOptions.autoHide();
var popOptionsInput=null;

function popOptionsActivate(obj,anchor) {
  popOptionsInput=obj;
  var postVars = "action=displayOptions&obj_id="+encodeURIComponent(obj.id);
  hwd  = new ajax ('../../../reservation/_v2/action.php', {
          postBody: postVars,
          update: $('divPopOptions'),
          onComplete: callbackCartDebug
          });
  popOptions.showPopup(anchor);
}
function popOptionsPick(k) {
  if (document.getElementById("selectOption"+k).value > 0) {
    var postVars = "action=addOption&lg="+encodeURIComponent(k);
    postVars +="&option_id="+encodeURIComponent(document.getElementById("selectOption"+k).value);
  	postVars +="&qte="+encodeURIComponent(document.getElementById("qte"+k).value);
    hwd  = new ajax ('../../../reservation/_v2/action.php', {
            postBody: postVars,
            update: $('items'),
            onComplete: callbackCartDebug
            });
    //var val = document.getElementById("selectOption"+k).value + " x " + document.getElementById("qte"+k).value;
    //popOptionsInput.innerHTML = val;
  }
  popOptions.hidePopup();
  document.getElementById('divPopOptions').innerHTML="";
}


function removeOption(num) {
  var postVars = "action=removeOption&num="+encodeURIComponent(num);
  hwd  = new ajax ('../../../reservation/_v2/action.php', {
          postBody: postVars,
          update: $('items'),
          onComplete: callbackCartDebug
          });
}


var popRecap = new PopupWindow("divPopRecap");
popRecap.offsetX= 20;
popRecap.offsetY= 20;
//popRecap.autoHide(false);
var popRecapInput=null;
function popRecapActivate(obj,anchor) {
	document.getElementById("divOverlay").style.visibility='visible';
	//var html = document.getElementById("divPopRecap").innerHTML;
	//popRecap.populate(html);
	popRecap.showPopup(anchor);
	SelectVisible("hidden",document.getElementsByTagName('select'));
	popRecap.x = 0;
	popRecap.y = 0;
	popRecap.refresh();
}


function popRecapHide() {
	try {
	popRecap.hidePopup();
    document.getElementById('divPopRecap').innerHTML="";
	document.getElementById("divOverlay").style.visibility='hidden';
	SelectVisible("visible",document.getElementsByTagName('select'));
	document.getElementById('selectOption'+(document.getElementById('nbItems').value-1)).style.visibility='hidden';
	} catch (e) {
		//TODO
	}
}
function SelectVisible(v,elem) {
	for (var i=0;i<elem.length;i++) elem[i].style.visibility=v;
}



String.prototype.trim = function()
{ return this.replace(/(^\s*)|(\s*$)/g, ""); }

function getTab(i) {
	document.getElementById('divInfosHotel1').className='hidden';
	document.getElementById('divInfosHotel2').className='hidden';
	document.getElementById('divInfosHotel3').className='hidden';
	document.getElementById('divInfosHotel'+i).className='';
}