$tbo = {

book: {
	tp: 0,	/* totalPrice */
	tt: 0,	/* totalTickets */
	bf: 0,	/* bookingFee */
	df: 0,	/* ddliveryFee */
	fnt: false,
	
	init: function() {
		$(".button").button();
					
		$("#cancelDlg").dialog({autoOpen:false,modal:true,resizable:false});
		$("#cancelDlg").dialog("option", "buttons", { "Yes, Cancel": function() { window.location = $("#cancelOrder").attr('href') }, "No": function() { $(this).dialog("close"); } } );
		$("#choose").click(function(){
			$tbo.book.cp();
			if ($tbo.book.tt == 0) {
				$("#notickets").slideDown("fast");
				$tbo.book.fnt = true;
				return false;
			}
		});
		$("#cancelOrder").click(function(){
			$("#cancelDlg").dialog('open');
			return false;
		});
		$(".tixqty").live('change', function(){
			$tbo.book.cp();
			if ($tbo.book.tt != 0 && $tbo.book.fnt == true) {
				$("#notickets").slideUp("fast");
				$tbo.book.fnt = false;			
			}
		});	

		if (vws.length > 1) {
			for(var vwI in vws) {
				var vw = vws[vwI];
				var h = '<tr class="tickets"><td colspan="4"><input id="seatclass' + vw.i + '" type="radio" name="seatclass" value="' + vw.i + '"><label for="seatclass' + vw.i + '"> ' + vw.v + '</label></td></tr>';
				$(".tickets:last").after(h);
				
				var seatclassTix = "seatclassTix" +  vw.i;
				var firstSelected = null;
				
				for(j in vw.t) {
					var i = vw.t[j];
					var t = tx[i];
					
					/* Create the ticket label "ticket, allocationpool" */
					var l = t.t; 
					//if (t.a != "") l += ', ' + t.a;
					
					var h = '<tr class="tickets seatclassTix ' + seatclassTix + '"><td class="qtycol"><select name="tix' + i +'" id="tix' + i +'" class="tixqty"><option value="0"></option>';
					
					for(j=t.mn; j<=t.mx; j++) { h += '<option' + (t.q == j ? ' selected' : '') + '>' + j + '</option>' }
					
					h += '</select></td><td class="labelcol">' + l +'</td><td class="pricecol">$' + t.p.toFixed(2) + '</td><td class="totalcol" id="total_tix' + i +'">$' + (t.q * t.p).toFixed(2) + '</td></tr>';
					
					$(".tickets:last").after(h);
					
					if (t.q > 0 && firstSelected == null) {
						firstSelected = vw.i;
					}
				}
			}
			
			var seatClassChange = function(e) {
				var checked = $("input[name=seatclass]:checked");
				var sc = "seatclassTix" + checked.val();
				
				$(".seatclassTix").each(function(index, element) {
					var $e = $(element);

                	if ($e.hasClass(sc)) {
						$e.show();
					}
					else {
						$e.hide();
						$(".tixqty", $e).val('0');
					}
                });
				
				try {
					$tbo.book.cp();
					if ($tbo.book.tt != 0 && $tbo.book.fnt == true) {
						$("#notickets").slideUp("fast");
						$tbo.book.fnt = false;			
					}
				}
				catch (e) {
					
				}
            }
			
			$("input[name='seatclass']").change(seatClassChange);
			
			var elementToSelect;
			if (firstSelected) {
				elementToSelect = $("input[name=seatclass][value=" + firstSelected + "]");
			}
			else {
				elementToSelect = $("input[name=seatclass]:first");
			}
			elementToSelect.attr('checked', true);
			seatClassChange();

		}
		else {
			for(var i in tx) {
				if (i != '0') {
					var t = tx[i];
					
					/* Create the ticket label "ticket, allocationpool" */
					var l = t.t; 
					if (t.a != "") l += ', ' + t.a;
					
					var h = '<tr class="tickets"><td class="qtycol"><select name="tix' + i +'" id="tix' + i +'" class="tixqty"><option value="0"></option>';
					
					for(j=t.mn; j<=t.mx; j++) { h += '<option' + (t.q == j ? ' selected' : '') + '>' + j + '</option>' }
					
					h += '</select></td><td class="labelcol">' + l +'</td><td class="pricecol">$' + t.p.toFixed(2) + '</td><td class="totalcol" id="total_tix' + i +'">$' + (t.q * t.p).toFixed(2) + '</td></tr>';
					
					$(".tickets:last").after(h);
				}
			}
		}
		
		for(i in dlvr) {
			if (i != '0') {
				$(".deliveryfield").html('<input type="hidden" id="delivery" name="delivery" value="' + i + '">' + dlvr[i].t);
			}
		}
		
		$tbo.book.cp();	
	},

	cp: function()	{ /* calcPrices() */
		$tbo.book.tp = $tbo.book.tt = $tbo.book.bf = 0;
		$(".tixqty").each(function() {
			var e = tx[this.id.substring(3)];	/* ticket info element */
			var q = $(this).val();	/* quantity */
			var t = q*e.p;	/* total price */
			$tbo.book.tt += q;
			$tbo.book.tp += t;
			$tbo.book.bf += q*e.b;
			$("#total_"+this.id).html('$'+t.toFixed(2));
		});
		$("#subtotal").html('$'+$tbo.book.tp.toFixed(2));
		
		$tbo.book.df = dlvr[$('#delivery').val()].p;
		if ($tbo.book.tp == 0) $tbo.book.df = 0;
		
		$("#deliveryfee").html('$'+ $tbo.book.df.toFixed(2));
		$tbo.book.tp += $tbo.book.df;
		
		$tbo.book.tp += $tbo.book.bf;
		$("#bookingfee").html('$'+$tbo.book.bf.toFixed(2));
		$("#totalprice").html('$'+$tbo.book.tp.toFixed(2));
	},
	
},

details: {
	init: function(type) {
		$tbo.details.type = type;
		
		$(".button").button();
					
		$("#cancelDlg").dialog({autoOpen:false,modal:true,resizable:false});
		$("#cancelDlg").dialog("option", "buttons", { "Yes, Cancel": function() { window.location = $("#cancelOrder").attr('href') }, "No": function() { $(this).dialog("close"); } } );
		$("#cancelOrder").click(function(){
			$("#cancelDlg").dialog('open');
			return false;
		});
		$("#choose").click(function(){
			var check = $tbo.details.validate();
			
			if (check == '') return true;
			
			$("#validationtxt").text(check);
			$("#validationalert").slideDown("fast");
			
			return false;
		});
		
		var tp = 0;
		var bf = 0;
		
		var i;
		for(i in tx) {
			var t = tx[i];
			if (t.q > 0) {
				/* Create the ticket label "ticket, allocationpool" */
				var l = t.t; 
				if (t.a != "") l += ', ' + t.a;
				
				var h = '<tr class="tickets"><td class="qtycol">' + t.q +'</td><td class="labelcol">' + l +'</td><td class="totalcol" id="total_tix' + i +'">$' + (t.q * t.p).toFixed(2) + '</td></tr>';
				tp += (t.q * t.p);
				bf += (t.q * t.b);
				 
				$(".tickets:last").after(h);
			}
		}
		
		$("#subtotal").html('$'+tp.toFixed(2));
		
		for(i in dlvr) {
			$("#deliveryfield").html(dlvr[i].t);
			/* handle delivery fee */
		}
		
		$("#bookingfee").html('$'+bf.toFixed(2));
		tp += bf;
		$("#totalprice").html('$'+tp.toFixed(2));
				
	},

	validate: function() {
		switch($tbo.details.type) {
		case "signin":
			return $tbo.details.validateSignin();
			break;
		case "profile":
			return $tbo.details.validateProfile();
			break;
		case "reset":
			return $tbo.details.validateReset();
			break;			
		case "register":
		default:
			return $tbo.details.validateRegister();
			break;
		}
		
	},
	
	validateSignin: function() {
		var email = $('input[name="email"]');

		if (email.val().length == 0) {
			email.focus();
			return 'Email Address must be entered';	
		}

		if (!$tbo.details.validateEmail(email.val())) {
			email.focus();
			return 'Email Address is not a valid email';
		}
		
		var customertype = $('input[name="customertype"]:checked').val();
		if (typeof(customertype) == "undefined") {
			var customertype = $('input[name="customertype"]');
			customertype.focus();
			return 'Customer type must be chosen';
		}
		
		if (customertype == 'existing') {
			var password = $('input[name="password"]');
			if (password.val().length == 0) {
				password.focus();
				return 'Password must be entered';
			}
	
			if (password.val().length < 4) {
				password.focus();
				return 'Password is too short';
			}
		}
		
		return '';
	},
		
	validateReset: function() {
		var resetCode = $('input[name="reset"]');

		if (resetCode.val().length == 0) {
			resetCode.focus();
			return 'Reset Code must be entered';	
		}

		if (resetCode.val().length != 32) {
			resetCode.focus();
			return 'Reset Code is incorrect';
		}

		var password = $('input[name="password"]');
		var password2 = $('input[name="password2"]');
		if (password.val().length == 0) {
			password.focus();
			return 'New Password must be entered';
		}

		if (password.val().length < 4) {
			password.focus();
			return 'New Password is too short';
		}

		if (password2.val().length == 0 || password.val() != password2.val() ) {
			password2.focus();
			return 'New Passwords must match';
		}

		return '';
	},
	
	validateRegister: function() {
		var email = $('input[name="email"]');
		var email2 = $('input[name="email2"]');

		if (email.val().length == 0) {
			email.focus();
			return 'Email Address must be entered';	
		}

		if (!$tbo.details.validateEmail(email.val())) {
			email.focus();
			return 'Email Address is not a valid email';
		}

		if (email.val() != email2.val()) {
			email2.focus();
			return 'Email Addresses do not match';
		}
	
		var title = $('select[name="title"]');
		if (title.val().length == 0) {
			title.focus();
			return "Your Title must be selected";
		}

		var firstname = $('input[name="firstname"]');
		if (firstname.val().length == 0) {
			firstname.focus();
			return 'Your First Name must be entered';	
		}
		
		if (!/^\s*[A-Za-z]+/.test(firstname.val())) {
			firstname.focus();
			return "Your First Name does not appear to be valid";
		}

		var lastname = $('input[name="lastname"]');
		if (lastname.val().length == 0) {
			lastname.focus();
			return 'Your Last Name must be entered';	
		}

		if (!/^\s*[A-Za-z]{2,}/.test(lastname.val())) {
			lastname.focus();
			return "Your Last Name does not appear to be valid";
		}
		
		var street = $('input[name="address"]');
		if (street.val().length == 0) {
			street.focus();
			return 'Your Street Address must be entered';	
		}

		if (!/^\s*\S\s*\S/.test(street.val())) {
			street.focus();
			return 'Your Street Address does not appear to be valid';	
		}
		
		var suburb = $('input[name="suburb"]');
		if (suburb.val().length == 0) {
			suburb.focus();
			return 'Your Suburb/City/Town must be entered';	
		}
				
		if (!/^\s*\S\s*\S/.test(suburb.val())) {
			street.focus();
			return 'Your Suburb/City/Town does not appear to be valid';	
		}
		
		var postcode = $('input[name="postcode"]');
		if (postcode.val().length == 0) {
			postcode.focus();
			return 'Your Postcode must be entered';	
		}
		
		var state = $('select[name="state"]');
		var country = $('select[name="country"]');
		
		if (country.val().length == 0) {
			country.focus();
			return "Your Country must be selected";
		}
		
		if (country.val() == 'Australia' && (state.val() == '' || state.val() == 'Other')) {
			state.focus();
			return "An Australian State must be selected";
		}
		
		if (country.val() == 'Australia' && !/^\d*$/.test(postcode.val())) {
			postcode.focus();
			return "Your Postcode does not appear to be valid";
		};
		
		if (country.val() != 'Australia' && (state.val() != '' && state.val() != 'Other')) {
			state.focus();
			return "Select Other as your state";
		}

		var phone = $('input[name="phone"]');
		if (phone.val().length == 0) {
			phone.focus();
			return 'Your Phone Number must be entered';	
		}
		
		if (!(/^(\(?0[2-9]\)?)?\s*\d{1}[\s]*\d{1}[\s]*\d{1}[\s]*\d{1}[\s-]*\d{1}[\s]*\d{1}[\s]*\d{1}[\s]*\d{1}$/.test(phone.val()))) {
			phone.focus();
			return "The Phone Number does not appear to be valid";
		}
		
		var mobile = $('input[name="mobile"]');
		if (mobile.val().length != 0 && !(/^(\(?0[2-9]\)?)?\s*\d{1}[\s]*\d{1}[\s]*\d{1}[\s]*\d{1}[\s-]*\d{1}[\s]*\d{1}[\s]*\d{1}[\s]*\d{1}$/.test(mobile.val()))) {
			mobile.focus();
			return "The Other Phone Number does not appear to be valid";
		}
		
		var password = $('input[name="password"]');
		var password2 = $('input[name="password2"]');

		if (password.val().length == 0) {
			password.focus();
			return 'Password must be entered';
		}

		if (password.val().length < 4) {
			password.focus();
			return 'Password is too short';
		}

		if (password.val() != password2.val()) {
			password2.focus();
			return 'Passwords do not match';
		}
	
		return '';
	},

	validateProfile: function() {

		var title = $('select[name="title"]');
		if (title.val().length == 0) {
			title.focus();
			return "Your Title must be selected";
		}

		var firstname = $('input[name="firstname"]');
		if (firstname.val().length == 0) {
			firstname.focus();
			return 'Your First Name must be entered';	
		}
		
		if (!/^\s*[A-Za-z]+/.test(firstname.val())) {
			firstname.focus();
			return "Your First Name does not appear to be valid";
		}

		var lastname = $('input[name="lastname"]');
		if (lastname.val().length == 0) {
			lastname.focus();
			return 'Your Last Name must be entered';	
		}

		if (!/^\s*[A-Za-z]{2,}/.test(lastname.val())) {
			lastname.focus();
			return "Your Last Name does not appear to be valid";
		}
		
		var street = $('input[name="address"]');
		if (street.val().length == 0) {
			street.focus();
			return 'Your Street Address must be entered';	
		}

		if (!/^\s*\S\s*\S/.test(street.val())) {
			street.focus();
			return 'Your Street Address does not appear to be valid';	
		}
		
		var suburb = $('input[name="suburb"]');
		if (suburb.val().length == 0) {
			suburb.focus();
			return 'Your Suburb/City/Town must be entered';	
		}
				
		if (!/^\s*\S\s*\S/.test(suburb.val())) {
			street.focus();
			return 'Your Suburb/City/Town does not appear to be valid';	
		}
		
		var postcode = $('input[name="postcode"]');
		if (postcode.val().length == 0) {
			postcode.focus();
			return 'Your Postcode must be entered';	
		}
		
		var state = $('select[name="state"]');
		var country = $('select[name="country"]');
		
		if (country.val().length == 0) {
			country.focus();
			return "Your Country must be selected";
		}
		
		if (country.val() == 'Australia' && (state.val() == '' || state.val() == 'Other')) {
			state.focus();
			return "An Australian State must be selected";
		}
		
		if (country.val() == 'Australia' && !/^\d*$/.test(postcode.val())) {
			postcode.focus();
			return "Your Postcode does not appear to be valid";
		};
		
		if (country.val() != 'Australia' && (state.val() != '' && state.val() != 'Other')) {
			state.focus();
			return "Select Other as your state";
		}

		var phone = $('input[name="phone"]');
		if (phone.val().length == 0) {
			phone.focus();
			return 'Your Phone Number must be entered';	
		}
		
		if (!(/^(\(?0[2-9]\)?)?\s*\d{1}[\s]*\d{1}[\s]*\d{1}[\s]*\d{1}[\s-]*\d{1}[\s]*\d{1}[\s]*\d{1}[\s]*\d{1}$/.test(phone.val()))) {
			phone.focus();
			return "The Phone Number does not appear to be valid";
		}
		
		var mobile = $('input[name="mobile"]');
		if (mobile.val().length != 0 && !(/^(\(?0[2-9]\)?)?\s*\d{1}[\s]*\d{1}[\s]*\d{1}[\s]*\d{1}[\s-]*\d{1}[\s]*\d{1}[\s]*\d{1}[\s]*\d{1}$/.test(mobile.val()))) {
			mobile.focus();
			return "The Other Phone Number does not appear to be valid";
		}
	
		return '';
	},
		
	validateEmail: function(e) {
		return e.length >= 6 && /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,10})+$/.test(e); 
	}
},


payment: {
	init: function() {
		$(".button").button();
					
		$("#cancelDlg").dialog({autoOpen:false,modal:true,resizable:false});
		$("#cancelDlg").dialog("option", "buttons", { "Yes, Cancel": function() { window.location = $("#cancelOrder").attr('href') }, "No": function() { $(this).dialog("close"); } } );
		$("#cancelOrder").click(function(){
			$("#cancelDlg").dialog('open');
			return false;
		});
		$("#choose").click(function(){
			var check = $tbo.payment.validate();
			
			if (check == '') return true;
			
			$("#validationtxt").text(check);
			$("#validationalert").slideDown("fast");
			
			return false;
		});

		var tp = 0;
		var bf = 0;
		
		var i;
		for(i in tx) {
			var t = tx[i];
			if (t.q > 0) {
				/* Create the ticket label "ticket, allocationpool" */
				var l = t.t; 
				if (t.a != "") l += ', ' + t.a;
				
				var h = '<tr class="tickets"><td class="qtycol">' + t.q +'</td><td class="labelcol">' + l +'</td><td class="totalcol" id="total_tix' + i +'">$' + (t.q * t.p).toFixed(2) + '</td></tr>';
				tp += (t.q * t.p);
				bf += (t.q * t.b);
				 
				$(".tickets:last").after(h);
			}
		}
		
		$("#subtotal").html('$'+tp.toFixed(2));
		
		for(i in dlvr) {
			$("#deliveryfield").html(dlvr[i].t);
			/* handle delivery fee */
		}
		
		$("#bookingfee").html('$'+bf.toFixed(2));
		tp += bf;
		$("#totalprice").html('$'+tp.toFixed(2));
		$("#paymentamount").html('$'+tp.toFixed(2));
				
	},
	
	validate: function() {
		var accept = $('input[name="agreeterms"]:checked');
		
		if (accept.length != 1) {
			return "You must read and agree to the Ticket Sale Terms & Conditions"; 
		}
		
		var cardtype = $('select[name="cctype"]');
		if (cardtype.val().length == 0) {
			cardtype.focus();
			return "Your Credit Card Type must be selected";
		}

		var cc = $('input[name="cc"]');
		if (cc.val().length == 0) {
			cc.focus();
			return "Your Credit Card Number must be entered";
		}

		var ccname = $('input[name="ccname"]');
		if (ccname.val().length == 0) {
			ccname.focus();
			return "The Name on your Credit Card must be entered";
		}

		var cccvc = $('input[name="cccvc"]');
		if (cccvc.val().length == 0) {
			cccvc.focus();
			return "Your Credit Card Verification Code must be entered";
		}
		
		
		return "";
	}

},

finish: {
	init: function() {
		$(".button").button();
	}
},

popup: function(u) {
	(window.open(u, 'tboeventpopup', "width=400,height=600,left=100,top=100,resizable=1,status=1,titlebar=1,toolbar=1,menubar=1,location=1,scrollbars=1")).focus();
	return false;
}
};

