// JavaScript Document

var quotePrice=new Array();

// Sets the object with id sTarget's value to fPrice
function setTargetPrice(fPrice,sTarget) {
	priceObj = document.getElementById(sTarget);
	if(priceObj!=null) {
		if(fPrice >= 0) {
			priceObj.innerHTML = fPrice.toFixed(0);
		} else {
			priceObj.innerHTML = 'Option not available';
		}
	}
	return true;
}

// Shows the Total Price
function showTotal() {
	oParty = document.getElementById('pax');
	if(oParty!=null) {
			pSize = oParty.value;
	}

totalPrice = 0;
	for(keyVar in quotePrice) {
		if(typeof(quotePrice[keyVar])=='number') {
			setTargetPrice(quotePrice[keyVar],keyVar + 'Price');
			if($(quotePrice[keyVar],keyVar + 'PricePP')) {
				setTargetPrice(Math.ceil(quotePrice[keyVar]/parseFloat(pSize)),keyVar + 'PricePP');
			}
			if(quotePrice[keyVar] >= 0) {
				totalPrice += parseFloat(quotePrice[keyVar]);
			}
		}
	}
	setTargetPrice(Math.ceil(totalPrice),'QuotePrice');
	setTargetPrice(Math.ceil(totalPrice/parseFloat(pSize)),'QuotePricePerPerson');
}

// Sets the sArea price to fPrice
function setPrice(fPrice,sArea) {
	quotePrice[sArea] = parseFloat(fPrice);
	showTotal();
}

// Adds fPrice to the sArea Price
function addPrice(fPrice,sArea) {
	quotePrice[sArea] = (parseFloat(quotePrice[sArea])||0) + parseFloat(fPrice);
	showTotal();
}

// Returns the current price of sArea
function getPrice(sArea) {
	return quotePrice[sArea];
}

// AJAX Functions to retrieve a price from a server script, scripts _MUST_ return a number

var priceXmlHttp = new Array();

function requestPrice(sURL,sArea) {
	// Display 'Working' block if exists
	oWorking = document.getElementById(sArea + '_working');
	oWorking.style.display = 'inline';

	// Initilise XML Request
	priceXmlHttp[sArea]=GetXmlHttpObject();
	if(priceXmlHttp[sArea]==null) {
		setPrice(-1,sArea);
		oWorking.style.display = 'none';
		return;
	}
	priceXmlHttp[sArea].onreadystatechange=priceStateChanged
	priceXmlHttp[sArea].open("GET",sURL,true);
	priceXmlHttp[sArea].send(null);
}

function priceStateChanged() { 
	for(keyVar in priceXmlHttp) {
		if(typeof(priceXmlHttp[keyVar])=='object') {			
			sArea = keyVar;
			if (priceXmlHttp[sArea].readyState==4 || priceXmlHttp[sArea].readyState=="complete") { 
				price = priceXmlHttp[sArea].responseText
				setPrice(price,sArea);
				
				oWorking = document.getElementById(sArea + '_working');
				oWorking.style.display = 'none';
			} 
		}
	}
} 

function GetXmlHttpObject() { 
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp;
} 

function arraySum(ar) {
	fTotal=0;
	for(i=0;i<ar.length;i++) {
		fTotal += Math.max(0,parseFloat(ar[i]));
	}
	return fTotal;
}
