function viewAllEvents(tag) {
	var i, j, o, nodes, pnl;
	
	tag.parentNode.style.display = 'none';
	if ((o = findObj('pnlCategory')) != null)
		o.style.display = '';
	
	pnl = findObj('pnlRegister');
	if (pnl != null && pnl.hasChildNodes()) {
		for (i = 1; i < 99; i++) {
			if ((o = findObj('rptSites__ctl' + i + '_rowCategory')) != null) {
				o.style.display = '';
				if ((o = findObj('rptSites__ctl' + i + '_rowEvent')) != null)
					o.style.display = '';
				if ((o = findObj('rptSites__ctl' + i + '_rowLine')) != null)
					o.style.display = '';
				if ((o = findObj('rptSites__ctl' + i + '_rowOther')) != null)
					o.style.display = '';
				if ((o = findObj('rptSites__ctl' + i + '_rowViewOther')) != null)
					o.style.display = 'none';

				for (j = 0; j < 99; j++) {
					if ((o = findObj('rptSites__ctl' + i + '_rptSiteItems__ctl' + j + '_rowItem')) != null) {
						o.style.display = '';
						if ((o = findObj('rptSites__ctl' + i + '_rptSiteItems__ctl' + j + '_rowDesc')) != null)
							o.style.display = '';
						if ((o = findObj('rptSites__ctl' + i + '_rptSiteItems__ctl' + (j + 1) + '_rowLegend')) != null)
							o.style.display = '';
					}
					else
						break;
				}
			}
			else
				break;
		}
	}
	return false;
}

function copyCompany(tb) {
	var i, j, o, o2;
	for (i = 0; i < 99; i++) {
		if ((o = findObj('rptBookingItems__ctl' + i + '_rptBookingDelegates__ctl0_pnlDelegateHeader')) != null) {
			for (j = 0; j < 99; j++) {
				if ((o = findObj('rptBookingItems__ctl' + i + '_rptBookingDelegates__ctl' + j + '_tbCompany')) != null || (o2 = findObj('rptBookingItems__ctl' + i + '_rptBookingDelegates__ctl' + j + '_tbGuestCompany')) != null) {
					if (o != null && o.value == '') o.value = tb.value
					if (o2 != null && o2.value == '') o2.value = tb.value;
				}
				else
					break;
			}
		}
		else
			break;
	}
}

function showDelegate(id) {
	var o;
	if ((o = findObj(id + '_imgExpand')) != null) {
		if (o.src.indexOf('collapse') > -1) {
			//hide this item
			showHideItem(id, false);
		}
		else {
			showHideItem(did, false);
			showHideItem(id, true);
		}
	}
	return false;
}

function firstDelegate() {
	if (!cval)
		setCompanyValidators(true);
	//if (did != '')
	//	showHideItem(did, false);

	if (typeof(Page_ClientValidate) == 'function' && !Page_ClientValidate()) {
		alert('Please complete all required fields before continuing.');
		return false;
	}
	setCompanyValidators(false);

	showHideItem('rptBookingItems__ctl0_rptBookingDelegates__ctl0', true, false, true);
	return false;
}

function nextDelegate(id) {
	if (cval)
		setCompanyValidators(false);

	if (typeof(Page_ClientValidate) == 'function' && !Page_ClientValidate()) {
		alert('Please complete all required fields before continuing.');
		return false;
	}
	
	showHideItem(id, false);
	var i, j, o, ri, si, sj;
	si = parseInt(id.substring(id.indexOf('__ctl') + 5, id.indexOf('_', id.indexOf('__ctl') + 5)));
	sj = parseInt(id.substring(id.indexOf('__ctl', id.length - 10) + 5)) + 1;

	for (i = si; i < 999; i++) {
		for (j = sj; j < 999; j++) {
			ri = findObj('rptBookingItems__ctl' + i + '_rptBookingDelegates__ctl' + j + '_imgExpand');
			if (ri != null)
				break;	//found
			else {
				if (j > (sj + msic))
					break;	//not found at this level
			}
		}
		if (ri != null)
			break;	//found
		else {
			sj = 0;
			if (i > (si + msic))
				break;	//not found at all
		}
	}

	if (ri != null) {
		showHideItem('rptBookingItems__ctl' + i + '_rptBookingDelegates__ctl' + j, true, false, true);
	}
	else {
		alert('No further items available - please submit your booking.');
		if ((o = findObj('btnSubmit')) != null)
			o.focus();
	}
	return false;
}

function showHideItem(id, show, loop, focus) {
	if (id != '') {
		var disp, hide, o;
		if (show) {
			if (!loop) did = id;
			disp = '';
			hide = 'none';
		}
		else {
			if (!loop) did = '';
			disp = 'none';
			hide = '';
		}

		setDelegateValidators(id, show);

		//set display type
		if ((o = findObj(id + '_imgExpand')) != null) {
			if (disp == '')
				o.src = 'include' + '/' + 'images/collapse.gif';
			else
				o.src = 'include' + '/' + 'images/expand.gif';
		}
		if ((o = findObj(id + '_hlExpand')) != null)
			o.style.display = hide;
		if ((o = findObj(id + '_pnlDelegateFields')) != null)
			o.style.display = disp;
		if ((o = findObj(id + '_pnlGuestFields')) != null) {
			o.style.display = disp;
			if ((o = findObj(id + '_pnlRemovePackage')) != null)
				o.style.display = disp;
			if ((o = findObj(id + '_pnlGuestHeader')) != null)
				o.style.display = disp;
			if ((o = findObj(id + '_pnlGuestSpecialReqs')) != null)
				o.style.display = disp;
			if ((o = findObj(id + '_pnlGuestSpacer')) != null)
				o.style.display = disp;
		}

		if ((o = findObj(id + '_lblSubItemCount')) != null) {
			//show or hide other sub items as well
			var i, j;
			j = parseInt(id.substring(id.length - 1));
			for (i = 1; i < o.innerHTML; i++) {
				showHideItem(id.substring(0, id.length - 1) + (j + i), show, true);
			}
		}

		if (show && !loop && focus) {
			if ((o = findObj(id + '_pnlDelegateHeader')) != null)
				o.scrollIntoView();
			if ((o = findObj(id + '_cbAutoFill')) != null)
				o.focus();
			else {
				if ((o = findObj(id + '_ddlTitle')) != null)
					o.focus();
				if ((o = findObj(id + '_ddlGuestTitle')) != null)
					o.focus();
			}
		}

		var ddl;
		if (!loop && (ddl = findObj(id + '_ddlSameDelegate')) != null) {
			//set list of existing delegates
			if (show) {
				var i;
				for (i = ddl.length; i > 0; i--) {
					//clear list except first
					ddl.remove(i);
				}

				if (sdprev != null) {
					//copy over previous
					for (i = 1; i < sdprev.length; i++) {
						ddl.add(new Option(sdprev.options[i].text, sdprev.options[i].value));
					}
				}
			}
			else {
				//add this delegate to the list
				var b, n;
				
				if ((o = findObj(id + '_tbFirstName')) != null) {
					if ((o = findObj(id + '_tbFirstName')) != null) {
						n = o.value;
					}
					if ((o = findObj(id + '_tbLastName')) != null) {
						if (n != '')
							n += ' ';
						n += o.value;
					}
				}

				b = true;
				for (i = 1; i < ddl.length; i++) {
					//check doesnt exist already
					if (n == ddl.options[i].text) {
						b = false;
						break;
					}
				}
				if (b && n != '')
					ddl.add(new Option(n, id));
				sdprev = ddl;
			}
		}
	}
}

function setCompanyValidators(enable) {
	var o;
	if ((o = findObj('rfvCompName')) != null)
		o.enabled = enable;
	if ((o = findObj('rfvAddress1')) != null)
		o.enabled = enable;
	if ((o = findObj('rfvAddress2')) != null)
		o.enabled = enable;
	if ((o = findObj('rfvTown')) != null)
		o.enabled = enable;
	if ((o = findObj('rfvCounty')) != null)
		o.enabled = enable;
	if ((o = findObj('rfvPostcode')) != null)
		o.enabled = enable;
	if ((o = findObj('rfvCountry')) != null)
		o.enabled = enable;
	if ((o = findObj('rfvTelephone')) != null)
		o.enabled = enable;
	if ((o = findObj('rfvFax')) != null)
		o.enabled = enable;

	setCustomFieldValidators('', enable, '');
	cval = enable;
}

function setDelegateValidators(id, enable) {
	var cb, o;
	var disable = '';

	if ((cb = findObj(id + '_cbAutoFill')) != null) {
		enable = enable && !cb.checked;
		if (cb.checked)
			disable = 'disabled';
	}
	if ((o = findObj(id + '_ddlTitle')) != null) {
		//delegates
		if (cb != null) {
			o.disabled = disable;
			if ((o = findObj(id + '_tbFirstName')) != null)
				o.disabled = disable;
			if ((o = findObj(id + '_tbLastName')) != null)
				o.disabled = disable;
			if ((o = findObj(id + '_tbCompany')) != null)
				o.disabled = disable;
			if ((o = findObj(id + '_tbJobTitle')) != null)
				o.disabled = disable;
			if ((o = findObj(id + '_tbTelephone')) != null)
				o.disabled = disable;
			if ((o = findObj(id + '_tbEmail')) != null)
				o.disabled = disable;
			if ((o = findObj(id + '_tbSpecialReqs')) != null)
				o.disabled = disable;
		}

		if ((o = findObj(id + '_rfvTitle')) != null)
			o.enabled = enable;
		if ((o = findObj(id + '_rfvFirstName')) != null)
			o.enabled = enable;
		if ((o = findObj(id + '_rfvLastName')) != null)
			o.enabled = enable;
		if ((o = findObj(id + '_rfvCompany')) != null)
			o.enabled = enable;
		if ((o = findObj(id + '_rfvJobTitle')) != null)
			o.enabled = enable;
		if ((o = findObj(id + '_rfvTelephone')) != null)
			o.enabled = enable;
		if ((o = findObj(id + '_rfvEmail')) != null)
			o.enabled = enable;

		setCustomFieldValidators(id + '_', enable, disable);
	}

	if ((o = findObj(id + '_ddlGuestTitle')) != null) {
		//guests
		if ((o = findObj(id + '_rfvGuestTitle')) != null)
			o.enabled = enable;
		if ((o = findObj(id + '_rfvGuestFirstName')) != null)
			o.enabled = enable;
		if ((o = findObj(id + '_rfvGuestLastName')) != null)
			o.enabled = enable;
		if ((o = findObj(id + '_rfvGuestCompany')) != null)
			o.enabled = enable;
		if ((o = findObj(id + '_rfvGuestJobTitle')) != null)
			o.enabled = enable;
	}
}

function setCustomFieldValidators(prefix, enable, disable) {
	//custom fields
	var i, j, o;
	for (i = 0; i < 99; i++) {
		var ft;
		if ((ft = findObj(prefix + 'rptCustomField__ctl' + i + '_lblCustomFieldType')) != null) {
			ft = parseInt(ft.innerHTML);
			if ((o = findObj(prefix + 'rptCustomField__ctl' + i + '_rfaCustom')) != null) {
				if ((o = findObj(prefix + 'rptCustomField__ctl' + i + '_rfvCustom')) != null)
					o.enabled = enable;
			}
			switch(ft) {
				case 0, 1:	//tb
					if ((o = findObj(prefix + 'rptCustomField__ctl' + i + '_tbAnswer')) != null)
						o.disabled = disable;
					break;
				case 2:	//cb
					if ((o = findObj(prefix + 'rptCustomField__ctl' + i + '_cbAnswer')) != null) {
						o.disabled = disable;
						o = o.parentNode;	//span tag
						o.disabled = disable;
					}
					break;
				case 6:	//cbl
					if ((o = findObj(prefix + 'rptCustomField__ctl' + i + '_cblAnswer')) != null) {
						o.disabled = disable;
						for (j = 0; j < 99; j++) {
							if ((o = findObj(prefix + 'rptCustomField__ctl' + i + '_cblAnswer_' + j)) != null) {
								o.disabled = disable;
								o = o.parentNode;	//span tag
								o.disabled = disable;
							}
							else
								break;
						}
					}
					break;
				case 3:	//rbl
					if ((o = findObj(prefix + 'rptCustomField__ctl' + i + '_rblAnswer')) != null) {
						o.disabled = disable;
						for (j = 0; j < 99; j++) {
							if ((o = findObj(prefix + 'rptCustomField__ctl' + i + '_rblAnswer_' + j)) != null) {
								o.disabled = disable;
								o = o.parentNode;	//span tag
								o.disabled = disable;
							}
							else
								break;
						}
					}
					break;
				case 4:	//ddl
					if ((o = findObj(prefix + 'rptCustomField__ctl' + i + '_ddlAnswer')) != null)
						o.disabled = disable;
					break;
				case 5:	//dt
					if ((o = findObj(prefix + 'rptCustomField__ctl' + i + '_tbDate')) != null)
						o.disabled = disable;
					break;
			}
		}
		else
			break;
	}
}

function copySameDelegate(id, ddl) {
	var id2, o, o2;
	id = id + '_';
	id2 = ddl.options[ddl.selectedIndex].value + '_';

	if ((o = findObj(id + 'tbFirstName')) != null) {
		//copy values from selected delegate
		if ((o = findObj(id + 'ddlTitle')) != null && (o2 = findObj(id2 + 'ddlTitle')) != null)
			o.selectedIndex = o2.selectedIndex;
		if ((o = findObj(id + 'tbFirstName')) != null && (o2 = findObj(id2 + 'tbFirstName')) != null)
			o.value = o2.value;
		if ((o = findObj(id + 'tbLastName')) != null && (o2 = findObj(id2 + 'tbLastName')) != null)
			o.value = o2.value;
		if ((o = findObj(id + 'tbCompany')) != null && (o2 = findObj(id2 + 'tbCompany')) != null)
			o.value = o2.value;
		if ((o = findObj(id + 'tbJobTitle')) != null && (o2 = findObj(id2 + 'tbJobTitle')) != null)
			o.value = o2.value;
		if ((o = findObj(id + 'tbTelephone')) != null && (o2 = findObj(id2 + 'tbTelephone')) != null)
			o.value = o2.value;
		if ((o = findObj(id + 'tbEmail')) != null && (o2 = findObj(id2 + 'tbEmail')) != null)
			o.value = o2.value;
		if ((o = findObj(id + 'tbSpecialReqs')) != null && (o2 = findObj(id2 + 'tbSpecialReqs')) != null)
			o.value = o2.value;
	}
	
//copy custom field values
	var i, j;
	for (i = 0; i < 99; i++) {
		var ft, ref;
		if ((ft = findObj(id + 'rptCustomField__ctl' + i + '_lblCustomFieldType')) != null && (ref = findObj(id + 'rptCustomField__ctl' + i + '_lblClientRef')) != null) {
			ft = parseInt(ft.innerHTML);
			ref = ref.innerHTML;
			if (ref != '') {
				switch(ft) {
					case 0, 1:	//tb
						if ((o = findObj(id + 'rptCustomField__ctl' + i + '_tbAnswer')) != null && (o2 = getSameCustomField(id2, '_tbAnswer', ref)) != null)
							o.value = o2.value;
						break;
					case 2:	//cb
						if ((o = findObj(id + 'rptCustomField__ctl' + i + '_cbAnswer')) != null && (o2 = getSameCustomField(id2, '_cbAnswer', ref)) != null)
							o.checked = o2.checked;
						break;
					case 6:	//cbl
						if ((o = findObj(id + 'rptCustomField__ctl' + i + '_cblAnswer')) != null && (o2 = getSameCustomField(id2, '_cblAnswer', ref)) != null) {
							for (j = 0; j < 99; j++) {
								if ((o = findObj(id + 'rptCustomField__ctl' + i + '_cblAnswer_' + j)) != null && (o3 = findObj(o2.id + '_' + j)) != null)
									o.checked = o3.checked;
								else
									break;
							}
						}
						break;
					case 3:	//rbl
						if ((o = findObj(id + 'rptCustomField__ctl' + i + '_rblAnswer')) != null && (o2 = getSameCustomField(id2, '_rblAnswer', ref)) != null) {
							for (j = 0; j < 99; j++) {
								if ((o = findObj(id + 'rptCustomField__ctl' + i + '_rblAnswer_' + j)) != null && (o3 = findObj(o2.id + '_' + j)) != null)
									o.checked = o3.checked;
								else
									break;
							}
						}
						break;
					case 4:	//ddl
						if ((o = findObj(id + 'rptCustomField__ctl' + i + '_ddlAnswer')) != null && (o2 = getSameCustomField(id2, '_ddlAnswer', ref)) != null)
							o.selectedIndex = o2.selectedIndex;
						break;
					case 5:	//dt
						if ((o = findObj(id + 'rptCustomField__ctl' + i + '_tbDate')) != null && (o2 = getSameCustomField(id2, '_tbDate', ref)) != null)
							o.value = o2.value;
						break;
				}
			}
		}
		else
			break;
	}
	
	ddl.selectedIndex = 0;
}

function getSameCustomField(id, ft, ref) {
	var i, o;
	for (i = 0; i < 99; i++) {
		if ((o = findObj(id + 'rptCustomField__ctl' + i + '_lblClientRef')) != null) {
			if (o.innerHTML == ref && (o = findObj(id + 'rptCustomField__ctl' + i + ft)) != null)
				return o;	//found matching custom field
		}
		else
			break;
	}
	
	return null;
}

function validateCheckbox(obj, rfvd) {
	var obj2 = document.getElementById(rfvd);
	if (obj2) {
		if (obj.checked)
			obj2.value = 'true';
		else
			obj2.value = '';
	}
}

function validateCheckboxList(obj, rfv) {
	var b, i, o;
	b = false;
	for (i = 0; i < 99; i++) {
		if ((o = document.getElementById(obj.id + '_' + i)) != null) {
			if (o.checked)
				b = true;
		}
		else
			break;
	}
	if (b)
		document.getElementById(rfv).value = 'true';
	else
		document.getElementById(rfv).value = '';
}

function addPromoCode(multi) {
	var pc = findObj('tbCode');
	var lbl = findObj('lblPromoCodes');
	var tb = findObj('tbPromoCodes');

	pc.value = pc.value.replace(/[^\w\d-\+\$&*%]/g, '');

	if (multi) {
		// multiple codes allowed
		if (tb.value.split(',').length >= 5)
			alert('Only a maximum of 5 ' + txtPromoCode + 's can be entered.');
		else if (pc.value != '') {
			if (tb.value != '')
				tb.value += ',';
			tb.value += pc.value.toUpperCase();
			listPromoCodes();
		}
		else
			alert('Please enter a valid ' + txtPromoCode + '.');
		pc.value = '';
	}
	else {
		if (pc.value != '')
			alert('Thank you, your code has been applied and the discount will appear on the next page.');
		else
			alert('Please enter a valid ' + txtPromoCode + '.');
	}
	return false;
}

function listPromoCodes(pcToRemove) {
	var lbl = findObj('lblPromoCodes');
	var tb = findObj('tbPromoCodes');

	if (tb != null && lbl != null) {
		var i, s, apc;
		s = '';
		if (tb.value != '') {
			apc = tb.value.split(',');
			tb.value = '';
			for (i = 0; i < apc.length; i++) {
				if (pcToRemove == null || apc[i] != pcToRemove) {
					if (tb.value != '')
						tb.value += ',';
					tb.value += apc[i];

					if (s != '')
						s += ', ';
					s += apc[i];
					s += '<img src="include/' + 'images/delete.gif" width="10" height="10" alt="" onclick="removePromoCode(\'' + apc[i] + '\')" style="cursor: pointer">';
				}
			}
		}

		if (s != '')
			lbl.innerHTML = s;
		else
			lbl.innerHTML = '--';
	}
}

function removeItem() {
	return confirm('Are you sure you wish to remove this item and reduce the quantity you are booking?');
}

function removePromoCode(pc) {
	if (confirm('Are you sure you wish to remove this ' + txtPromoCode + '?')) {
		listPromoCodes(pc)
	}
}

function continueBooking() {
	setCompanyValidators(true);

	if (typeof(Page_ClientValidate) == 'function' && !Page_ClientValidate()) {
		alert('Please complete all required fields before continuing.');
		return false;
	}
	return true;
}

function valBooker() {
	var rb, o, enable, disable, display;
	if ((rb = findObj('rbThisBooker')) != null) {
		if (rb.checked) {
			enable = false;
			disable = 'disabled';
			display = 'none';
		}
		else {
			enable = true;
			disable = '';
			display = '';
		}
		
		if ((o = findObj('ddlThisBooker')) != null) {
			if (rb.checked)
				o.disabled = '';
			else
				o.disabled = 'disabled';
		}
		if ((o = findObj('rowTitle')) != null)
			o.style.display = display;
		if ((o = findObj('rowFirstName')) != null)
			o.style.display = display;
		if ((o = findObj('rowLastName')) != null)
			o.style.display = display;
		if ((o = findObj('rowTelephone')) != null)
			o.style.display = display;
		if ((o = findObj('rowEmail')) != null)
			o.style.display = display;
		if ((o = findObj('rowEmailConfirm')) != null)
			o.style.display = display;
		if ((o = findObj('rfvThisBooker')) != null)
			o.enabled = !enable;
		if ((o = findObj('rfvTitle')) != null)
			o.enabled = enable;
		if ((o = findObj('rfvFirstName')) != null)
			o.enabled = enable;
		if ((o = findObj('rfvLastName')) != null)
			o.enabled = enable;
		if ((o = findObj('rfvTelephone')) != null)
			o.enabled = enable;
		if ((o = findObj('rfvEmail')) != null)
			o.enabled = enable;
		if ((o = findObj('rfvEmailConfirm')) != null)
			o.enabled = enable;
	}
}

function valInvoice() {
	var rb, o, enable, disable, display;
	if ((rb = findObj('rbThisInvoice')) != null) {
		if (rb.checked) {
			enable = false;
			disable = 'disabled';
			display = 'none';
		}
		else {
			enable = true;
			disable = '';
			display = '';
		}
		
		if ((o = findObj('rowInvoiceCompany')) != null)
			o.style.display = display;
		if ((o = findObj('rowInvoiceAddress1')) != null)
			o.style.display = display;
		if ((o = findObj('rowInvoiceAddress2')) != null)
			o.style.display = display;
		if ((o = findObj('rowInvoiceAddress3')) != null)
			o.style.display = display;
		if ((o = findObj('rowInvoiceTown')) != null)
			o.style.display = display;
		if ((o = findObj('rowInvoiceCounty')) != null)
			o.style.display = display;
		if ((o = findObj('rowInvoicePostcode')) != null)
			o.style.display = display;
		if ((o = findObj('rowInvoiceCountry')) != null)
			o.style.display = display;
		if ((o = findObj('rfvInvoiceCompany')) != null)
			o.enabled = enable;
		if ((o = findObj('rfvInvoiceAddress1')) != null)
			o.enabled = enable;
		if ((o = findObj('rfvInvoiceTown')) != null)
			o.enabled = enable;
		if ((o = findObj('rfvInvoicePostcode')) != null)
			o.enabled = enable;
		if ((o = findObj('rfvInvoiceCountry')) != null)
			o.enabled = enable;
	}
}

function submitBooking(accept) {
	if (typeof(Page_ClientValidate) == 'function' && !Page_ClientValidate()) {
		alert('Please complete all required fields before submitting.');
		return false;
	}
	var cb;
	if (accept && (cb = findObj('cbTermsAccept')) != null && !cb.checked) {
		alert('You must tick the box to accept the Terms and Conditions.');
		return false;
	}
	return true;
}