
var scrollToForm;
var el;
var db = 0;
function AddToCart(hotid,roomid){
	if(!can_booking){
		alert(cart_error_6);
		return;
	}
	
	var di = $("cboDayStart").value;
	var mi = $("cboMonthStart").value;
	var yi = $("cboYearStart").value;	

	var da1 = NewDate(yi,mi,di);//new Date(yi,parseInt(mi)-1,parseInt(di));


	//init date is less than min allowed date
	if(da1.DaysDiff(min) < 0){
		alert(cart_error_5);
		return;
	}

	/*if(!CheckDates(false)){
		alert(cart_error_3);
		return;
	}*/
	if(typeof(lang)=="undefined")
		lang="en";

	var o = $("cboRoomsToBook_" + roomid);
	var url="/Includes/fnCartJS.php?id=" + hotid + "&roomid=" + roomid + "&" + ((lang=="es")?"lang=spa":"lang=eng");
	var data = $("frmPrefs").toQueryString();
	var quant=1;
	
	//var rooms_selected = $("cboRoomQ").getValue();
	//var rooms_now = ($("hdnRoomCount")!=null)?$("hdnRoomCount").getValue():0;
	

	if(o){
		if(o.selectedIndex>0){
			quant =  parseInt(o.getValue()); //options[o.selectedIndex].value;
			url+="&q=" + quant;

			new Ajax(url, {
				evalScripts: false,
				method: 'get',
				//update: $('divCartPreview'),
				onFailure:function(xhr){
					if(lang=="es"){
						alert("Lo sentimos, en este momento no podemos procesar su solicitud, estamos realizando tareas de mantenimiento.." +((db==1)?xhr.statusText:""));
					}else{
						alert("Sorry, at this moment we can not process your request, we are undergoing maintenance." +((db==1)?xhr.statusText:""));
					}
				},
				onException:function(headerName, value){
					if(lang=="es"){
						alert("Lo sentimos, en este momento no podemos procesar su solicitud, estamos realizando tareas de mantenimiento.." + ((db==1)?(headerName + ":"+value):""));
					}else{
						alert("Sorry, at this moment we can not process your request, we are undergoing maintenance." + ((db==1)?(headerName + ":"+value):""));
					}

				},
				onComplete: function(response) {
					
					
					try{
						if(response.length>5){

							//$('divCartPreview').innerHTML = response;

							removeFromCombo($("cboRoomsToBook_" + roomid), quant);

							window.location.href='https://' + location.hostname.replace("www.","") +"/" +((lang=="es")?"es/":"")+'CheckOut.php';

							//window.location.href='/'+((lang=="es")?"es/":"")+'booking-cart/';



						}else{
							if(typeof(seo_url)!="undefined"){
								//reload page for refresh cart content
								window.location.href= seo_url +"/rooms/"; //"Hotel.php?id="+ hotid + "&tab=rooms";
							}
						}
					}catch(e){
						if(lang=="es"){
							alert("Lo sentimos, en este momento no podemos procesar su solicitud, estamos realizando tareas de mantenimiento.."   +((db==1)?e.message:""));
						}else{
							alert("Sorry, at this moment we can not process your request, we are undergoing maintenance."   +((db==1)?e.message:""));
						}

						if(db==1){
							alert(response);
						}
					}

					

					o.selectedIndex=0;
					
					if(scrollToForm){

						scrollToForm.toTop().chain(function(){
							
							try{
								
								$("divCartTitle").setStyle('background-color','#E6F1FF');
								var fx = new Fx.Styles("divCartTitle", {duration:500, wait:false});
								fx.start({'background-color': '#FACC61',color:'#000000'}).chain(function() {
										this.start.delay(200, this,{'background-color': '#E6F1FF',color:'#006699'});
								}).chain(function() {
										this.start.delay(200, this,{'background-color': '#FACC61',color:'#000000'});
								}).chain(function() {
										this.start.delay(200, this,{'background-color': '#E6F1FF',color:'#006699'});
								})
								
							}catch(e){
								//alert(e.message);
							}
						});
						
					}
					
				}

			},this).request(data);
		}else{
			alert(cart_error_1);
		}
	}else{
		alert(cart_error_2);
	}

	

}


function DeleteRoom(room_id){
	var d = new Date();
	var url="/Includes/fnCartJS.php?act=delete&roomid=" + room_id + "&" +d.getTime();
	var callback_response = (arguments.length>1)?arguments[1]:null;

	new Ajax(url, {
		evalScripts: false,
		method: 'get',
		update: $('divCartPreview'),
		onComplete: function(response) {
			//alert(response);
			addOption($("cboRoomsToBook_" + room_id),1);
			if(callback_response)
				callback_response();
		}	

	},this).request();
	

}

function EmptyCart(){
	var d = new Date();
	var url="/Includes/fnCartJS.php?act=deleteall&" + d.getTime();

	new Ajax(url, {
		evalScripts: false,
		method: 'get',
		update: $('divCartPreview'),
		onComplete: function(response) {
			if(typeof(seo_url)!="undefined"){
				//reload page for refresh cart content
				window.location.href= seo_url +"rooms/";
			}
				//"Hotel.php?id="+ obtenerParamURL(window,"id") + "&tab=rooms";
		}
	},this).request();
}



function modifyRoomPersons(div,i,room){
	
	
	var a = $("ppr_a"+div);
	var div = $("ppr_"+div);

	el = new Element('iframe', {'src': ('/Includes/incPersonsByRoom.php?i=' + i + "&room=" + room),'frameborder': 0,'width':500,'height':200});

	if(div.innerHTML.length==0){
		a.innerHTML="Close [x]";
		div.adopt(el);
	}else{
		a.innerHTML="modify persons in room";
		div.firstChild.remove();
	}

}

function RefreshPage(){
	
	if(window.location.href){
		window.location.href = current_page;
	}else{
		window.location = current_page;
	}

}


function SeeFromDate(id, fecha){

	var url  ="/Includes/fnRoomRatesJS.php?op=1&id=" + id + "&date=" + fecha + "&"+ ((lang=="es")?"lang=spa":"lang=eng");
	
	new Ajax(url, {
				evalScripts: false,
				method: 'get',
				onFailure:function(xhr){
					
					if(lang=="es"){
						alert("Lo sentimos, en este momento no podemos procesar su solicitud, estamos realizando tareas de mantenimiento.." +((db==1)?xhr.statusText:""));
					}else{
						alert("Sorry, at this moment we can not process your request, we are undergoing maintenance." +((db==1)?xhr.statusText:""));
					}
				},
				onException:function(headerName, value){
					
					if(lang=="es"){
						alert("Lo sentimos, en este momento no podemos procesar su solicitud, estamos realizando tareas de mantenimiento.." + ((db==1)?(headerName + ":"+value):""));
					}else{
						alert("Sorry, at this moment we can not process your request, we are undergoing maintenance." + ((db==1)?(headerName + ":"+value):""));
					}

				},
				update: $('divRates')
					/*,onComplete: function(response) {alert(response);}*/

			},this).request();
}

function cancelBooking(id){
	

	var url  ="/Includes/fnCancelBookingJS.php?id=" + id + "&"+ ((lang=="es")?"lang=spa":"lang=eng");
	
	$("btnCancel").disabled=true;

	new Ajax(url, {
				evalScripts: false,
				method: 'get',
				/*update: $('divRates')*/
				onFailure:function(xhr){
					
					if(lang=="es"){
						alert("Lo sentimos, en este momento no podemos procesar su solicitud, estamos realizando tareas de mantenimiento.." +((db==1)?xhr.statusText:""));
					}else{
						alert("Sorry, at this moment we can not process your request, we are undergoing maintenance." +((db==1)?xhr.statusText:""));
					}
				},
				onException:function(headerName, value){
					
					if(lang=="es"){
						alert("Lo sentimos, en este momento no podemos procesar su solicitud, estamos realizando tareas de mantenimiento.." + ((db==1)?(headerName + ":"+value):""));
					}else{
						alert("Sorry, at this moment we can not process your request, we are undergoing maintenance." + ((db==1)?(headerName + ":"+value):""));
					}

				},
				onComplete: function(response) {
					try{						
						var r = eval(response);						
						alert(ERRORS[_lang][r.error]);
						if(r.error==0)
							window.location.href='/'+((lang=="es")?"es/":"")+'my-account/';

					}catch(e){
						alert(ERRORS[_lang][5]);
						$("btnCancel").disabled=false;
						//window.location.href='MyAccount.php';
					}

				}

			},this).request();


}


function obtenerParamURL(ventana,name ){
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp ( regexS );
	var tmpURL = ventana.location.href;
	var results = regex.exec( tmpURL );
	if( results == null )
		return"";
	else
		return results[1];
}

db = obtenerParamURL(window,"db");
