/**
 *
 * Copyright World.Net Services Ltd, all rights reserved, 2004 - 2009.
 */

/**
 * Broswer class.
 *
 */
function Browser(){
  this.dom = document.getElementById?1:0;
  this.ie4 = (document.all && !this.dom)?1:0;
  this.ns4 = (document.layers && !this.dom)?1:0;
  this.ns6 = (this.dom && !document.all)?1:0;
  this.ie5 = (this.dom && document.all)?1:0;
  this.ok = this.dom || this.ie4 || this.ns4;
  this.platform = navigator.platform;
}
var browser = new Browser();


/**
 *get dom object
 */
function $(vl)
{
	return (document.getElementById(vl) ? document.getElementById(vl) : null);
}




/**
 * Rate Calculator Class
 * 
 */

function RateCalc()
{
	this.num=0;
	this.prds = new Array();
	this.numPairing=0;
	this.pairings = new Array();
	this.qty=0;
	this.currency;
	this.symbol='$';
	this.maxCapacity=8;

	/*
		_err_no_select_rooms		Please select your required rooms.
		_err_not_enough_rooms		You have not selected enough rooms with capacity for all adults/children.
		_err_to_many_rooms		You have selected too many rooms for your required adults/children.
	 */

	this.msg1='Please select your required rooms.';
	this.msg2='You have not selected enough rooms with capacity for all adults/children.';
	this.msg3='You have selected too many rooms for your required adults/children.';
}

/**
 * Initial program initialisation.
 */
RateCalc.prototype.run = function()
{
	return true;
	for(var i=0; i<this.num; i++) {
		var p=this.prds[i];
		var count=p.num;
		alert(p.pcode+' '+count);
	}
}


RateCalc.prototype.setCurrency = function(currency, symbol)
{
	this.currency=currency;
	this.symbol=symbol;
}

RateCalc.prototype.add = function(pcode)
{
	p=new Product(pcode);
	this.prds[this.num]=p;
	this.num++;
	return p;
}

RateCalc.prototype.setQty = function(qty)
{
	this.qty=qty;
}

RateCalc.prototype.pairing= function(qty, adults, children)
{
	var o=new Object();
	o.pax=adults+children;
	this.pairings[this.numPairing]=o;
	this.numPairing++;
	return o;
}

RateCalc.prototype.submit = function(form, pcode)
{
	alert('submit');
	form.submit();
}

RateCalc.prototype.validate = function(form)
{
	return this.process(form, 'submit');
}

RateCalc.prototype.process = function(form, mode)
{
	var a=form.id.split('-');
	var p=this.getProduct(a[1]);
	if(!p) {
		alert('Failed to find input');
		return false;
	}

	var pcode=a[1]; //initialise pcode.
	var count=0;
	var price=0.0;
	var items=0;
	var capacities=new Array();
        for(var j=0; j<form.length; j++) {
                var e=form.elements[j];

		if(e.type=='select-one' || e.type=='radio') {
			var amt=null;

			if(e.type=='radio' && e.checked) {
				var a=e.value.split('-');
				var amt=1;
			}
			else if(e.type=="select-one" && e.selectedIndex) {
				var a=e.id.split('-');
				var amt=parseInt(e.selectedIndex);				
			}

			if(amt) {
				var pcode=a[0];
				var rcno=a[1];
				var rtno=a[2];

				count += amt;
				for(k=0; k<p.rates.length; k++) {
					var rObj=p.rates[k];
					if(rObj.rcno==rcno && rObj.rtno==rtno) {
						price+=(amt*parseFloat(rObj.price));
						capacities[items]={'capacity':rObj.capacity, 'amt':amt, 'taken':0};
						items++;
					}
				}
			}
		}
	}


	p.priceObj=document.getElementById('price-'+pcode);
	p.qtyObj=document.getElementById('qty-'+pcode);
	p.btnObj=document.getElementById('button-'+pcode);

	p.qtyObj.innerHTML=(count) ? count : "";
	p.priceObj.innerHTML=(price) ? this.formatCurrency(price, this.symbol) : "";
	p.btnObj.style.display= (count) ?  'block' : 'none';


	if(mode=='change') {
		return false;
	}

	if(count==0) {
		alert(this.msg1);
		return false;
	}

	if(count<this.qty) {
		alert(this.msg2);
		return false;
	}
	else if(count>this.qty) {
		alert(this.msg3);
		return false;
	}

	// Capacity checking...
	//alert(this.numPairing+' '+items+' '+this.maxCapacity);
	for(var i=0; i<this.numPairing; i++) {
		var pObj=this.pairings[i];
		var roomMatched=false;
		//alert('room: ['+i+'] pax='+pObj.pax);

		// Match rooms to max capacity.
		for(var thisCapacity=1; thisCapacity<=this.maxCapacity && !roomMatched; thisCapacity++) {
			for(var j=0; j<items && !roomMatched; j++) {
				var cObj=capacities[j];
				if(cObj.capacity==thisCapacity && cObj.capacity>=pObj.pax && cObj.taken<cObj.amt) {
					cObj.taken++;
					roomMatched=true;
					//alert('Matched: '+cObj.capacity+' '+thisCapacity);
				}
			}
		}

		if(!roomMatched) {
			alert(this.msg2);
			return false;
		}
	}

	for(var j=0; j<items; j++) {
		var cObj=capacities[j];
		if(cObj.taken<cObj.amt) {
			alert(this.msg3);
			return false;
		}
	}
	return true;
	return false;
}

RateCalc.prototype.getProduct = function(pcode)
{
	for(var i=0; i<this.num; i++) {
		if(this.prds[i].pcode==pcode) {
			return this.prds[i];
		}
	}
}

RateCalc.prototype.change = function(ref)
{
	var a=ref.id.split('-');
	return this.process(document.getElementById('form-'+a[0]),'change');
}

RateCalc.prototype.radio = function(ref)
{
	var a=ref.id;
	alert('here:'+a);
	return;
	var a=ref.id.split('-');
	return this.process(document.getElementById('form-'+a[0]),'change');
}

/**
 *
 */
RateCalc.prototype.formatCurrency = function(num, symbol) 
{
	num=num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num="0";
	sign=(num==(num=Math.abs(num)));
	num=Math.floor(num*100+0.50000000001);
	cents=num%100;

	num=Math.floor(num/100).toString();
	if(cents<10)
		cents="0"+cents;
	for(var i=0; i<Math.floor((num.length-(1+i))/3); i++)
		num=num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));

	if(num=="0")
		return "";

	return (((sign)?'':'-') + symbol + num + ((this.display_cents) ?  '.'+cents : ''));
}

/**
 * Product Object definition.
 */

function Product(pcode)
{
	this.pcode=pcode;
	this.num=0;
	this.rates=new Array();
	this.priceObj=null;
	this.priceObj=null;
}

Product.prototype.add = function(rcno, rtno, capacity, price, cPrice)
{
//	alert(rcno+' '+rtno+' '+price); 
	this.r=new Object();
	this.r.rcno=rcno;
	this.r.rtno=rtno;
	this.r.capacity=capacity;
	this.r.price=price;
	this.r.cPrice=cPrice;

	this.rates[this.num]=this.r;
	this.num++;

	return this.r;

}
