	Array.prototype.foreach = function(predicate)
	{
		for(var i=0;i<this.length;i++)
			predicate(this[i]);
	};
	var PriceManager =
	{
		Total : {SPAN:null, Value:0, PAYPAL_FIELD:null},
		TypeDropDownList : null,
		Options : [],
		Label : "",
		Init : function()
		{
			//get total price panel
			this.Total.SPAN = document.getElementById("totalPrice");
			this.Total.PAYPAL_FIELD = document.getElementById("amountToPay");
			//get ddl for types
			this.TypeDropDownList = document.getElementById("select");
			//get all instances of options
			var inputs = document.getElementsByTagName("INPUT");
			for(var idx=0;idx<inputs.length;idx++)
				if(inputs[idx].getAttribute("type").toString().toLowerCase()=="radio"&&inputs[idx].getAttribute("concessionary").toString().toLowerCase()!="")
				{
					//add event on click then add to collection
					inputs[idx].onclick = PriceManager.CountTotal;
					inputs[idx].onmouseup = PriceManager.Check;
					this.Options.push(inputs[idx]);
				}
			//start showing total
			this.CountTotal();
		},
		Check : function()
		{
			var cbo = this;
			if(cbo.checked) cbo.setAttribute("unmark","true");
		},
		CountTotal : function()
		{
			
			
			//reset price
			PriceManager.Total.Value = 0;
			PriceManager.Label = "";
			
			
			PriceManager.Options.foreach( function(p)
			{
				if(p.getAttribute("unmark")=="true")
				{
					p.setAttribute("unmark","false");	
					p.checked = false;
				}
				if(p.checked)
				{
					PriceManager.Total.Value += (PriceManager.TypeDropDownList.value=="Normal Pricing")?parseInt(p.getAttribute("pricing")):parseInt(p.getAttribute("concessionary"));
					PriceManager.Label +="<strong>"+p.name+"</strong><br/>"+ p.value+" : &pound;"+((PriceManager.TypeDropDownList.value=="Normal Pricing")?p.getAttribute("pricing"):(p.getAttribute("concessionary")))+"<br/>";
				}
			});

			for(var i=PriceManager.Total.SPAN.childNodes.length-1;i>-1;i--)
				PriceManager.Total.SPAN.removeChild(PriceManager.Total.SPAN.childNodes[i]);

			//output price
			PriceManager.Total.SPAN.appendChild(document.createTextNode(PriceManager.Total.Value));
			PriceManager.Total.PAYPAL_FIELD.value = PriceManager.Total.Value;
		}
	};

	var Form =
	{
		Obj : null,
		Fields : "",
		Validate : function()
		{
			var _blnValidated = true;
			Form.Fields ="Booking=Evening Classes";
			var __debug = "";
			var _Errors = "";
			var inputs = Form.Obj.getElementsByTagName("INPUT");
			//var textareas = Form.Obj.getElementsByTagName("TEXTAREA");
			for(var idx=0;idx<inputs.length;idx++)
				if((inputs[idx].name!=null&&inputs[idx].name!="")&&inputs[idx].getAttribute("exclude")!="true")
				{
					Form.Fields+="&"+inputs[idx].name+"="+escape(inputs[idx].value);
					__debug+="&"+inputs[idx].name+"="+escape(inputs[idx].value);
					
					if(inputs[idx].name=="Email")
					{
						if(inputs[idx].value.toString().indexOf("@")<0) 
						{
							_blnValidated=false;
							_Errors += inputs[idx].name+": Please enter a valid email address\r\n";
						}
							
						Form.Fields+="&Ticket_Type="+escape(PriceManager.TypeDropDownList.value);
					}
					else if(inputs[idx].getAttribute("mandatory")=="true"&&inputs[idx].value.toString().replace(/^(\s)*/, '').replace(/(\s)*$/, '')=="")
					{
						_blnValidated=false;
						_Errors += inputs[idx].name+": You MUST fill in this field\r\n";
					}
					
				}

			//check price
			if(PriceManager.Total.Value==0)
			{
				_blnValidated=false;
				_Errors += "Bookings: Please choose which class you would like to attend";
			}
			else
			{
				Form.Fields+="&Bookings="+escape(PriceManager.Label);
			}

			if(!_blnValidated)
				alert(_Errors);

			return  _blnValidated;
		},
		Submit : function( _obj )
		{
			Form.Obj = _obj;
			if(Form.Validate())
			{
				//send email, with response to send form
				AJAX_SetTarget(null, Form.SubmitAsync, "bookinggateway.php", "POST", Form.Fields);
			}

			return false;
		},
		SubmitAsync : function()
		{
			Form.Obj.submit();
		},
		AmendTarget : function( _btn )
		{
			Form.Obj = _btn.parentNode;
			while(Form.Obj.tagName!="FORM")
				Form.Obj = Form.Obj.parentNode;
				
			Form.Obj.setAttribute("action", _btn.getAttribute("response_url"));
			Form.Obj.setAttribute("method", _btn.getAttribute("method"));
			document.getElementById("booking_payment_type").value = _btn.getAttribute("payment");
		}

	};

var functionToExec;
var xmlHttp;
var widgetToReturn;

function AJAX_SetTarget(widget, callback_function, URL, GET_POST, params)
{
		widgetToReturn = widget;
		functionToExec=callback_function;
		xmlHttp=AJAX_CreateRequestObject(AJAX_StateChanged);
		xmlHttp.open(GET_POST, URL , true);
		if(GET_POST=="POST")
		{
            		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		}

		xmlHttp.send(params);
}

function AJAX_StateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		try{
			functionToExec(widgetToReturn, xmlHttp);
		}catch(e){}
	}
}

function AJAX_CreateRequestObject(handler)
{
	var ro;
	var browser = navigator.appName;

	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}

	ro.onreadystatechange = handler;
	return ro;
}

