/*
 * $Id: extras.js,v 1.6 2010/02/05 15:55:36 chris Exp $
 */

var extras      = null;
var guestCounts = null;

function Criteria(dateRequired, stockControlledInd, minimumAge, maximumAge, minimumDuration, maximumDuration) {
	this.dateRequired       = dateRequired;
	this.stockControlledInd = stockControlledInd;
	this.minimumAge         = minimumAge;
	this.maximumAge         = maximumAge;
	this.minimumDuration    = minimumDuration;
	this.maximumDuration    = maximumDuration;
}

function DateRange(start,end,duration) {
	this.start    = start;
	this.end      = end;
	this.duration = duration;
}

function Extra(rph,code,name,type,quantity,applyTo,selectionType) {
	this.rph                    = rph;
	this.code                   = code;
	this.name                   = name;
	this.type                   = type;
	this.quantity               = quantity;
	this.applyTo                = applyTo;
	this.selectionType          = selectionType;
	this.listOfPassengerRPH     = new Array();
	this.listOfSubTypes         = null;
	this.subCategory            = null;
	this.subCategories          = null;
	this.date                   = null;
	this.criteria               = null;
	this.periods                = null;
	this.additionalInfoPrompt   = null;
	this.additionalInfoResponse = null;
	this.reversalCode           = null;
	this.show                   = true;

	/*
	 * This prices an individual extra, it relies on the set of guestCounts for passengers
	 */
	this.priceExtra = function priceExtra(guests) {

		if (this.applyTo==null) {

			if (this.periods!= null) {
				for (var x=0;x<this.periods.length;x++) {
					if (this.periods[x].prices!=null) {
						for (var y=0;y<this.periods[x].prices.length;y++) {

							var lAmount = this.periods[x].prices[y].amount;
							return lAmount;
						}
					}
				}
			}

		} else if(this.applyTo=="AllPax") {

			var lTotalPrice = null;

			if (guests != null) {
				for (var paxNo=0;paxNo<guests.length;paxNo++) {

					var paxPrice = this.pricePax(guests[paxNo]);

					if (paxPrice != null) {

						if (lTotalPrice == null)
							lTotalPrice=0;

						lTotalPrice += paxPrice;

					}
				}
			}

			if (lTotalPrice != null)
				return lTotalPrice;

		} else if(this.applyTo=="SelectedPax") {

			var lTotalPrice = 0;

			for (var paxNo=0;paxNo<this.listOfPassengerRPH.length;paxNo++) {

				var rph      = this.listOfPassengerRPH[paxNo];
				var paxPrice = this.pricePax(guests[(rph - 1)]);

				if (paxPrice != null) {
					lTotalPrice += paxPrice;
				}

			}

			return lTotalPrice;

		}

		return 0;
	}

	/*
	 * This prices an individual passenger
	 */

	this.pricePax = function pricePax(guestCount) {

		if (guestCount.code==7) // Infant
			return null;

		if (this.periods!= null) {
			for (var x=0;x<this.periods.length;x++) {
				if (this.periods[x].prices!=null) {

					var lPrice = null;

					for (var y=0;y<this.periods[x].prices.length;y++) {

						if(this.periods[x].prices[y].code!= null) {

							if (guestCount.code!=this.periods[x].prices[y].code)
								continue;

							if (guestCount.code!=10 &&
								 guestCount.age!=null &&
								  this.periods[x].prices[y].age != null &&
								   guestCount.age>this.periods[x].prices[y].age)
								continue;
						}

						lPrice = this.periods[x].prices[y];
						break;

					}

					if (lPrice == null) {

						window.console && console.log("Failed to find price for guest type:" + guestCount.code + " age " +  guestCount.age);

						return null;

					} else {
						var lAmount     = lPrice.amount;
						var lQty        = guestCount.count;
						var lTotalPrice = (lQty * lAmount);

						trace && window.console && console.log("got here qty [" + lQty + "] price [" + lAmount + "] total [" + lTotalPrice + "]");

						return lTotalPrice;
					}
				}
			}
		}
		return null;
	}
}

function ExtraGroup(listOfExtrasRPH, ruleCode) {
	this.listOfExtrasRPH = listOfExtrasRPH;
	this.ruleCode = ruleCode;
}

function GuestCount(code, birthdate, age, count) {
	this.code      = code;
	this.birthdate = birthdate;
	this.count     = count;
	this.age       = age;
}

function Period(start, duration, prices) {
	this.start    = start;
	this.duration = duration;
	this.prices   = prices;
}

function Price(age, code, amount) {
	this.age    = age;
	this.code   = code;
	this.amount = amount;
}

function SubCategory(name) {
	this.name = name;
}

/*
 * current noPax of specified type
 */
function firstPaxOfType(type) {
	for (var x=0;x<guestCounts.length;x++)
		if (guestCounts[x].code==type)
			return x;

	return null;
}

/*
 * current nopax of specified type
 */
function noPaxOfType(type) {
	var total = 0;

	for (var x=0;x<guestCounts.length;x++)
		if (guestCounts[x].code==type)
			total += guestCounts[x].count;

	return total;
}

function currentBasePrice(currentTotal) {

	if (extras == null)
		extras = loadExtras();

	if (guestCounts == null)
		guestCounts = loadGuests();

	var price = 0

	for (key in extras) {
		extra = extras[key];

        if (extra.code==undefined)
            continue;

		if (!reverseMandatoryOptionals && extra.type=="MND" && extra.reversalCode != null) {
			debug && window.console && console.log("I would remove this from the total " + extra.code + " price " + extra.priceExtra(guestCounts));
			price += extra.priceExtra(guestCounts);
		}
	}

	price = currentTotal - price;

	debug && window.console && console.log("Current price adjustment is " + convertToPounds(price));

	return price;

}


/*
 * General function to cost all extras
 */
function priceAllExtras() {

	var totalExtras = 0;

	for (key in extras) {

		extra=extras[key];

		if (!extra.show)
			continue;

		var itemPrice = 0;

		if (extra.applyTo==null && extra.quantity != null && extra.quantity >0)
			itemPrice = extra.priceExtra(guestCounts);

		else if (extra.applyTo=="AllPax" && extra.quantity != null && extra.quantity >0)
			itemPrice = extra.priceExtra(guestCounts);

		else if (extra.applyTo=="SelectedPax" && extra.listOfPassengerRPH.length > 0)
			itemPrice = extra.priceExtra(guestCounts);

		if (itemPrice != 0)
			debug && window.console && console.log('Extra price [' + extra.code + '] cost ' + itemPrice + ' pence');

	    totalExtras += itemPrice;

	}

    debug && window.console && console.log('Total extras ' + totalExtras + ' pence');

    return totalExtras;
}