var cur_pics=new Array();
var max_pics=new Array();
var cur_files=new Array();


function room_next_image(roomtype_id){
	cur_pic=cur_pics[roomtype_id];
	max_pic=max_pics[roomtype_id];
	
	document.getElementById('pic_'+roomtype_id+'_'+cur_pic).style.display='none';
	if(cur_pic<max_pic){
		cur_pic++;
	}else{
		cur_pic=0;
	}
	cur_pics[roomtype_id]=cur_pic;
	document.getElementById('pic_'+roomtype_id+'_'+cur_pic).style.display='block';
	
}


function room_load_image(roomtype_id,evt){
	
	cur_pic=cur_pics[roomtype_id];
	content=document.getElementById('room_img_panel_'+roomtype_id+'_'+cur_pic);
	
	return open_rate_popup(content,evt,'upper');
}

function room_load_info(roomtype_id,evt){
	content=document.getElementById('room_info_panel_'+roomtype_id);
	return open_rate_popup(content,evt,'lower');
}

function room_load_bookingform1(roomtype_id,evt){
	content=document.getElementById('room_booking_form_'+roomtype_id);
	set_rate_popup_width('600px');
	
	x=open_rate_popup(content,evt,'upper');
	cal_roomtype=roomtype_id;
	cal_reset_charge();
	
	if(document.getElementById('sgl_num_'+cal_roomtype)){
		document.getElementById('sgl_num_'+cal_roomtype).onchange=cal_sgl_change;
	}
	if(document.getElementById('dbl_num_'+cal_roomtype)){
		document.getElementById('dbl_num_'+cal_roomtype).onchange=cal_dbl_change;
	}
	if(document.getElementById('tpl_num_'+cal_roomtype)){
		document.getElementById('tpl_num_'+cal_roomtype).onchange=cal_tpl_change;
	}
	
	document.getElementById('adult_num_'+cal_roomtype).onchange=cal_adult_change;
	document.getElementById('child_num_'+cal_roomtype).onchange=cal_child_change;
	return x;
}

/*price calculation*/
var cal_roomtype="";
var cal_charge=new Array();
var cal_cost=new Array();

function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	//var a = amount.split(".",2)
	//var d = a[1];
	//var i = parseInt(a[0]);
	var i=amount;
	if(isNaN(i)) { return ""; }
	var minus = "";
	if(i < 0) { minus = "-"; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	
	amount = n ;
	amount = minus + amount;
	return amount;
}

function validate_bookform1(){
	
	room=0;
	max_adult=0;
	if(document.getElementById("sgl_num_"+cal_roomtype)){
		num=1*document.getElementById("sgl_num_"+cal_roomtype).value;
		room +=num;
		max_adult += num;
	}
	if(document.getElementById("dbl_num_"+cal_roomtype)){
		num = 1*document.getElementById("dbl_num_"+cal_roomtype).value;
		room += num;
		max_adult +=num*2;
	}
	if(document.getElementById("tpl_num_"+cal_roomtype)){
		num = 1*document.getElementById("tpl_num_"+cal_roomtype).value;
		room += num;
		max_adult +=num*3;
	}
	adult = document.getElementById("adult_num_"+cal_roomtype).value;
	
	if(room==0 && adult==0){
		alert("Please specify number of rooms and guests for this trip.");
		return false;
	}
	
	if(room==0){
		alert("Please specify number of rooms for this trip.");
		return false;
	}
	
	if(adult==0){
		alert("Please specify number of adults for this trip. At least 1 adult is required.");
		return false;
	}
	
	if(adult>max_adult){
		alert("According to the room capacity, the number of adults should not exceed "+max_adult+".");
		return false;
	}
	
	return true;
	
}

function cal_reset_charge(){
	cal_charge["sgl"]=0;
	cal_charge["dbl"]=0;
	cal_charge["tpl"]=0;
	cal_charge["room_charge"]=0;
	cal_charge["dinner_adult"]=0;
	cal_charge["dinner_child"]=0;
	cal_charge["dinner_charge"]=0;
	cal_charge["bf_adult"]=0;
	cal_charge["bf_child"]=0;
	cal_charge["bf_charge"]=0;
	cal_charge["net_charge"]=0;
	
	sel_id="sgl_num_"+cal_roomtype;
	if(document.getElementById(sel_id)){
		document.getElementById(sel_id).selectedIndex=0;
	}
	sel_id="dbl_num_"+cal_roomtype;
	if(document.getElementById(sel_id)){
		document.getElementById(sel_id).selectedIndex=0;
	}
	sel_id="tpl_num_"+cal_roomtype;
	if(document.getElementById(sel_id)){
		document.getElementById(sel_id).selectedIndex=0;
	}
	
	document.getElementById("adult_num_"+cal_roomtype).selectedIndex=0;
	document.getElementById("child_num_"+cal_roomtype).selectedIndex=0;
	document.getElementById("infant_num_"+cal_roomtype).selectedIndex=0;
	cal_display_roomcharge();
	cal_display_personcharge();
}

function cal_sgl_change(){
	if(!cal_cost[cal_roomtype]["sgl"]){
		return;
	}
	number=document.getElementById("sgl_num_"+cal_roomtype).value;
	cal_charge["sgl"]=number*cal_cost[cal_roomtype]["sgl"];
	cal_conclude_charge();
	cal_display_roomcharge();
}
function cal_dbl_change(){
	if(!cal_cost[cal_roomtype]["dbl"]){
		return;
	}
	number=document.getElementById("dbl_num_"+cal_roomtype).value;
	cal_charge["dbl"]=number*cal_cost[cal_roomtype]["dbl"];
	cal_conclude_charge();
	cal_display_roomcharge();
}
function cal_tpl_change(){
	if(!cal_cost[cal_roomtype]["tpl"]){
		return;
	}
	number=document.getElementById("tpl_num_"+cal_roomtype).value;
	cal_charge["tpl"]=number*cal_cost[cal_roomtype]["tpl"];
	cal_conclude_charge();
	cal_display_roomcharge();
}

function cal_adult_change(){
	number=document.getElementById("adult_num_"+cal_roomtype).value;
	flag=false;
	if(document.getElementById("box_dinner_charge_"+cal_roomtype)){
		cal_charge["dinner_adult"]=number*cal_cost[cal_roomtype]["dinner_adult"];
		flag=true;
	}
	
	if(document.getElementById("box_bf_charge_"+cal_roomtype)){
		cal_charge["bf_adult"]=number*cal_cost[cal_roomtype]["breakfast"];
		flag=true;
	}
	if(flag){
		cal_conclude_charge();
		cal_display_personcharge();
	}
}

function cal_child_change(){
	number=document.getElementById("child_num_"+cal_roomtype).value;
	flag=false;
	if(document.getElementById("box_dinner_charge_"+cal_roomtype)){
		cal_charge["dinner_child"]=number*cal_cost[cal_roomtype]["dinner_child"];
		flag=true;
	}
	
	if(document.getElementById("box_bf_charge_"+cal_roomtype)){
		cal_charge["bf_child"]=number*cal_cost[cal_roomtype]["breakfast"];
		flag=true;
	}
	if(flag){
		cal_conclude_charge();
		cal_display_personcharge();
	}
}

function cal_conclude_charge(){
	cal_charge["room_charge"]=cal_charge["sgl"]+cal_charge["dbl"]+cal_charge["tpl"];
	cal_charge["dinner_charge"]=cal_charge["dinner_adult"]+cal_charge["dinner_child"];
	cal_charge["bf_charge"]=cal_charge["bf_adult"]+cal_charge["bf_child"];
	cal_charge["net_charge"]=cal_charge["room_charge"]+cal_charge["dinner_charge"]+cal_charge["bf_charge"];
	
}

function cal_display_roomcharge(){
	if(document.getElementById("box_sgl_"+cal_roomtype)){
		document.getElementById("box_sgl_"+cal_roomtype).innerHTML=cal_charge["sgl"];
	}
	
	if(document.getElementById("box_dbl_"+cal_roomtype)){
		document.getElementById("box_dbl_"+cal_roomtype).innerHTML=cal_charge["dbl"];
	}
	
	if(document.getElementById("box_tpl_"+cal_roomtype)){
		document.getElementById("box_tpl_"+cal_roomtype).innerHTML=cal_charge["tpl"];
	}
	document.getElementById("box_room_charge_"+cal_roomtype).innerHTML=cal_charge["room_charge"];
	document.getElementById("box_net_charge_"+cal_roomtype).innerHTML="USD "+CommaFormatted(cal_charge["net_charge"]);
}

function cal_display_personcharge(){
	if(document.getElementById("box_dinner_charge_"+cal_roomtype)){
		document.getElementById("box_dinner_adult_"+cal_roomtype).innerHTML=cal_charge["dinner_adult"];
		document.getElementById("box_dinner_child_"+cal_roomtype).innerHTML=cal_charge["dinner_child"];
		document.getElementById("box_dinner_charge_"+cal_roomtype).innerHTML=cal_charge["dinner_charge"];
	}
	
	if(document.getElementById("box_bf_charge_"+cal_roomtype)){
		document.getElementById("box_bf_adult_"+cal_roomtype).innerHTML=cal_charge["bf_adult"];
		document.getElementById("box_bf_child_"+cal_roomtype).innerHTML=cal_charge["bf_child"];
		document.getElementById("box_bf_charge_"+cal_roomtype).innerHTML=cal_charge["bf_charge"];
	}
	
	document.getElementById("box_net_charge_"+cal_roomtype).innerHTML="USD "+CommaFormatted(cal_charge["net_charge"]);
}

/* open-close popup */


var currContent;
function open_rate_popup(content,evt,position){
	evt = (evt)?evt:((event)?event:null);
	if(evt){
		y=(evt.layerY)? evt.layerY:evt.y;
		win_content=document.getElementById("rate_popup_content");
		if(win_content.innerHTML!=""){
			close_rate_popup();
		}
		
		
		
		currContent=content;

		win_content.innerHTML=currContent.innerHTML;
		currContent.innerHTML="";
		
		win=document.getElementById("rate_popup");
		space=document.getElementById("rate_list");
		
		win.style.display="block";
		
		if(win.offsetHeight){
			
			win_height=parseInt(win.offsetHeight);
			space_height=parseInt(space.offsetHeight);
		}else{
			
			win_height=100;
			space_height=null;
		}
		
		if(win_height>space_height){
			y=0;
		}else{
			half_height=win_height/2;
			y=y-half_height;
			if(y<0){
				y=0;
			}else if(y+win_height>space_height){
				y=space_height-win_height;
			}
		}
		
		x=(parseInt(space.offsetWidth)-parseInt(win.style.width))/2;
		
		
		win.style.top=y;
		win.style.left=x;
		
		
		return false;
	}else{
		return true;
	}
}

function close_rate_popup(){
	win_content=document.getElementById("rate_popup_content");
	currContent.innerHTML=win_content.innerHTML;
	win_content.innerHTML="";
	document.getElementById("rate_popup").style.display="none";
}

function set_rate_popup_width(width){
	document.getElementById("rate_popup").style.width=width;
}
