cj.res = {
	nights : 0,
	rooms : new Array(),
	bed : new Array(),
	service : new Array(),
	totalRoom : 0,
	totalservice : 0,
	total : 0,
	init : function(){
		var f = document.getElementById("freservationStep1");
		if (!f){
			alert("form id not set!");
			return false;
		}
		if(!f.lang){
			alert("form : lang not set");
			return false;
		}
		var lang = f.lang.value;
		//check-in/out date
		this.iy = document.getElementById("inYear");
		this.im = document.getElementById("inMonth");
		this.id = document.getElementById("inDay");
		this.oy = document.getElementById("outYear");
		this.om = document.getElementById("outMonth");
		this.od = document.getElementById("outDay");
		
		this.stayNights = document.getElementById("stayNights");
		if (!this.iy){ alert("inYear does not exist"); return false;}
		if (!this.im){ alert("inMonth does not exist"); return false;}
		if (!this.id){ alert("inDay does not exist"); return false;}
		if (!this.oy){ alert("outYear does not exist"); return false; }
		if (!this.om){ alert("outMonth does not exist"); return false; }
		if (!this.od){ alert("outDay does not exist"); return false; }
		if (!this.stayNights){ alert("stayNights does not exist"); return false; }
		
		this.iy.onchange = function(){cj.res.inChg()};
		this.im.onchange = function(){cj.res.inChg()};
		this.id.onchange = function(){cj.res.inChg()};
		this.oy.onchange = function(){cj.res.outChg()};
		this.om.onchange = function(){cj.res.outChg()};
		this.od.onchange = function(){cj.res.outChg()};
		
		//room
		if (!f.extraBedPerNight){ alert("extra bed per night not set"); return false; }
		this.extraBedPerNight = f.extraBedPerNight.value;
			
		var selects = f.getElementsByTagName("select");
		for(var i=0; i<selects.length; i++){
			var obj = selects[i];
			if (obj.name.search("room") != -1){
				obj.onchange = function(){cj.res.calTotal()};
				this.rooms[this.rooms.length] = obj;
			}
			if(obj.name.search('bed') != -1){
				obj.onchange = function(){cj.res.calTotal()};
				this.bed[this.bed.length] = obj;
			}
			//service
			if(obj.name.search('service')!=-1){
				obj.onchange = function(){cj.res.calTotal()};
				this.service[this.service.length] = obj;
			}
		}
		this.calTotal();
		
		f.onsubmit = function(){
			if (cj.res.inDay < new Date() || cj.res.inDay >= cj.res.outDay){
				alert(cj.msg.valid[lang] + cj.msg.day[lang]);
				f.inDay.focus();
				return false;
			}
			var roomSelected = false;
			var rooms = cj.res.rooms;
			for(var i=0; i<rooms.length; i++){
				if (rooms[i].selectedIndex > 0){
					roomSelected = true;
					break;
				}
			}
			var total = Number((document.getElementById("total")).innerHTML);
			if(!roomSelected || total < 1){
				alert(f.totalStr.value + " = 0");
				f.inDay.focus();
				return false;
			}
			return true;
		}
	},
	//date
	inChg : function(){
		this.setDay();
		if(this.inDay > this.outDay)
			this.outEqualIn();
		this.calTotal();
	},
	outChg : function(){
		this.setDay();
		if(this.inDay > this.outDay)
			this.inEqualOut();
		this.calTotal();
	},
	setDay : function(){
		this.inDay = new Date(this.iy.value + "/" + this.im.value + "/" + this.id.value);
		if (this.inDay.getDate() != this.id.value){
			this.id.value --;
			this.setDay();
		}
		this.outDay = new Date(this.oy.value + "/" + this.om.value + "/" + this.od.value);
		if (this.outDay.getDate() != this.od.value){
			this.od.value --;
			this.setDay();
		}
	},
	outEqualIn : function(){
		this.oy.selectedIndex = this.iy.selectedIndex;
		this.om.selectedIndex = this.im.selectedIndex;
		this.od.selectedIndex = this.id.selectedIndex;
	},
	inEqualOut : function(){
		this.iy.selectedIndex = this.oy.selectedIndex;
		this.im.selectedIndex = this.om.selectedIndex;
		this.id.selectedIndex = this.od.selectedIndex;
	},
	setStayNights : function(){
		this.setDay();
		this.nights = (this.outDay - this.inDay)/86400000;
		this.stayNights.innerHTML = this.nights;
	},
	calRoom : function(){
		this.totalRoom = 0;
		for(var i=0; i<this.rooms.length; i++){
			sum = (this.rooms[i].getAttribute("price") * this.rooms[i].selectedIndex + 
				this.extraBedPerNight * this.bed[i].selectedIndex
				) * this.nights;
			sum = Math.round(sum*100)/100;
			document.getElementById("roomSum"+i).innerHTML = sum;
			this.totalRoom += sum;
			if(!this.rooms[i].selectedIndex){
				this.bed[i].selectedIndex = 0;
			}
		}
		this.totalRoom = Math.round(this.totalRoom*100)/100;
		document.getElementById("totalRoom").innerHTML = this.totalRoom;
	},
	calservice : function(){
		this.totalService = 0;
		for(var i=0; i<this.service.length; i++){
			var sum = this.service[i].selectedIndex * this.service[i].getAttribute("price");
			sum = Math.round(sum*100)/100;
			document.getElementById("serviceSum"+i).innerHTML = sum;
			this.totalService += sum;
		}
		this.totalService = Math.round(this.totalService*100)/100;
		document.getElementById("totalService").innerHTML = this.totalService;
	},
	calTotal : function(){
		this.setStayNights();
		this.calRoom();
		this.calservice();
		this.total = this.totalRoom + this.totalService;
		this.total = Math.round(this.total*100)/100;
		document.getElementById("total").innerHTML = this.total;
	}
}
cj.evt.add(window, "load", function(){
	cj.util.loadJs("../js/lang.js");
	cj.calendar.init('inYear', 'inMonth', 'inDay', "inCalTrigger");
	cj.calendar.init('outYear', 'outMonth', 'outDay', "outCalTrigger");
	cj.res.init();	
});
