//<script language="JavaScript" Runat=SERVER>

/*
Robert Seber
Objects to handle room allocation and pricing
*/

//---------------------------Model-----------------------------------

function Model(availableRoomPropertiesArray,boardOptionsArray,peopleProperties){
	this.room = new Array();
	this.prebook = false;
	this.availableRoomProperties = availableRoomPropertiesArray;
	this.peopleProperties = peopleProperties;
	this.adjustmentPerAdultOrChild = 0;
	this.depositPerAdultOrChild = 0;
	this.flight = new Flight();
	this.boardOptions = boardOptionsArray;
	this.board = new Board();
	this.flightOptions = new Array();
	this.maxAdultsAndChildren = 0;
	this.tfrFixedModifier = 0;
	this.tfrPercentageModifier = 0;
	return (this);
}
Model.prototype.balance = function(){
	return (this.estimateOfViewdataPrice()-this.deposit()+this.tfrModifier());
}
Model.prototype.price = function(){
	return (this.deposit()+this.balance());
}
Model.prototype.tfrModifier = function(){
	var vp = this.estimateOfViewdataPrice();
	var fm = this.tfrFixedModifier;
	var pm = this.tfrPercentageModifier;
	return vp*pm/100+fm*this.people();
}
Model.prototype.estimateOfViewdataPrice = function(){
	var price = 0;
	for (var c=0;c<this.room.length;c++){
		price+=this.room[c].price();
	}
	price += parseFloat(this.board.price)+parseFloat(this.flight.price)*this.adultsAndChildren();
	if (this.prebook){
		price += this.flight.prebookChildPrice * this.children();
		price += this.flight.prebookAdultPrice * this.adults();
	}
	price += this.adjustmentPerAdultOrChild*this.adultsAndChildren();
	return (price);
}
Model.prototype.deposit = function(){
	return (this.depositPerAdultOrChild*this.adultsAndChildren());
}
Model.prototype.adultsAndChildren = function(){
	var total = 0;
	for (var c=0;c<this.room.length;c++){
		total+=this.room[c].adults+this.room[c].children;
	}
	return (total);
}
Model.prototype.people = function(){
	var total = 0;
	for (var c=0;c<this.room.length;c++){
		total+=this.room[c].adults+this.room[c].children+this.room[c].infants;
	}
	return (total);
}
Model.prototype.adults = function(){
	var total = 0;
	for (var c=0;c<this.room.length;c++){
		total+=this.room[c].adults;
	}
	return (total);
}
Model.prototype.children = function(){
	var total = 0;
	for (var c=0;c<this.room.length;c++){
		total+=this.room[c].children;
	}
	return (total);
}
Model.prototype.infants = function(){
	var total = 0;
	for (var c=0;c<this.room.length;c++){
		total+=this.room[c].infants;
	}
	return (total);
}

//---------------------------/Model-----------------------------------

//---------------------------Room-----------------------------------

function Room(peopleProperties){
	this.properties = new Array();
	this.peopleProperties = peopleProperties;
	this.adults = 0;
	this.children = 0;
	this.infants = 0;
	return (this);
}
Room.prototype.people = function(){
	return (this.adults+this.children+this.infants);
}
Room.prototype.adultsAndChildren = function(){
	return (this.adults+this.children);
}
Room.prototype.setAdults = function(adults){
	var temp = adults;
	if (temp>this.properties.maxAdults){//Only allow max number of adults in room
		temp = this.properties.maxAdults;
	}
	while (temp+this.children>this.properties.maxAdultsAndChildren){
		this.children--;//Reduce number of children
	}
	this.adults = temp;
	return (true);
}
Room.prototype.setChildren = function(children){
	var temp = children;
	if (temp>this.properties.maxChildren){//Only allow max number of children in room
		temp = this.properties.maxChildren;
	}
	while (temp+this.adults>this.properties.maxAdultsAndChildren){
		this.adults--;//Reduce number of adults
	}
	this.children = temp;
	return (true);
}
Room.prototype.setInfants = function(infants){
	var temp = infants;
	if (temp>this.properties.maxInfants){//Only allow max number of children in room
		temp = this.properties.maxInfants;
	}
	this.infants = temp;
	return (true);
}
Room.prototype.setProperties = function(properties){
	this.properties = properties;
	if(this.adults>this.properties.maxAdults){
		this.setAdults(this.properties.maxAdults);
	}
	while (this.adultsAndChildren()>this.properties.maxAdultsAndChildren){
		this.children--;//Reduce number of children
	}
	if(this.infants>this.properties.maxInfants){
		this.setInfants(this.properties.maxInfants);
	}
	return (true);
}
Room.prototype.price = function(room){
	var price = 0;
	var adults = this.adults;
	var children = this.children;
	var infants = this.infants;
	while ((adults<2)&&children){
		children--;
		adults++;
	}
	price+=sumArray(this.peopleProperties.adultPrice,adults);
	price+=sumArray(this.peopleProperties.childPrice,children);
	price+=sumArray(this.peopleProperties.infantPrice,infants);
	price+=this.properties.price*this.adultsAndChildren();
	return (price);
}

//---------------------------/Room-----------------------------------

function Board(viewDataCode,englishDescription,price){
	this.viewDataCode = viewDataCode;
	this.englishDescription = englishDescription;
	this.price = price;	
	return (this);
}

function Flight(time,price){
	this.outboundSeriesCode = "";
	this.inboundAirline = "";
	this.outboundAirline = "";
	this.startingAirport = "";
	this.destinationAirport = "";
	this.time = time;
	this.price = price;
	this.prebookChildPrice = 0;
	this.prebookAdultPrice = 0;
	return (this);
}

function RoomProperties(){
	this.viewDataDescription =		arguments[0];
	this.price			 =				arguments[1];
	this.englishDescription =		arguments[2];
	this.minAdultsAndChildren =	arguments[3];
	this.maxAdultsAndChildren =	arguments[4];
	this.minAdults =					arguments[5];
	this.maxAdults =					arguments[6];
	this.minChildren =				arguments[7];
	this.maxChildren =				arguments[8];
	this.minInfants =					arguments[9];
	this.maxInfants =					arguments[10];
	this.availability =				arguments[11];
	return (this);
}

function PeopleProperties(adultPriceArray,viewDataAdultDefinition,childPriceArray,viewDataChildDefinition,infantPriceArray,viewDataInfantDefinition){
	this.viewDataAdultDefinition = viewDataAdultDefinition;
	this.adultPrice = adultPriceArray;
	this.viewDataChildDefinition = viewDataChildDefinition;
	this.childPrice = childPriceArray;
	this.viewDataInfantDefinition = viewDataInfantDefinition;
	this.infantPrice = infantPriceArray;
	return(this);
}

//</script>