/*------------------------------------------------
	1st Stage
16 Jan 2009 Joshua
	add the validation for credit card number
7 Dec 2008 Joshua
	add function checkDayLimit for stage 2
29 Oct 2008 Joshua
	fix bug in chgFormCheck. now, firefox can invoke the function well
29 Oct 2008 Joshua
	add function chgFormCheck
-------------------------------------------------*/
<!--- Disable back button during booking process --->
<!--
	window.history.forward(1);
//-->


function chgFormCheck (checktype, targetFieldId, targetFiledCheckerId, title)
{
	var target  = document.getElementById(targetFieldId);
	var checker = document.getElementById(targetFiledCheckerId);
	if (target == null || checker != null)
		return;
	var cid = targetFieldId + "_js_checker";
	checker = document.getElementById(cid);
	var star = document.getElementById(targetFiledCheckerId+"_star");

	if (checker != null)
	{
		checker.parentNode.removeChild(checker);
		if (star != null)
			star.style.display = "none";
	}
	else
	{
		checker = document.createElement("input");
		checker.setAttribute('type', 'hidden');
		checker.setAttribute('id', cid);
		checker.setAttribute('name', checktype);
		checker.setAttribute('value', target.name);
		checker.setAttribute('title', title);
		target.parentNode.appendChild(checker);
		if (star != null)
		{
			star.style.display = "inline";
			star.style.color = "#10DD32";
		}
	}
}

function displayAccommDetails(itemnum) {
	var divRequired = document.getElementById('accommRequired');
	var divRequest = document.getElementById('accommRequest');
	var divDetails = document.getElementById('accommDetails');
	

	switch (parseInt(itemnum)) {
		case -1: // No Accomm
			if (divRequired) divRequired.style.display = 'none';
			if (divRequest) divRequest.style.display = 'none';
			break;

		case -2: // Accomm Request
			if (divRequired) divRequired.style.display = 'none';
			if (divRequest) divRequest.style.display = '';
			break;

		default:
			if (divRequired) divRequired.style.display = '';
			if (divRequest) divRequest.style.display = 'none';
	}

	// If no Accomm is selected, ensure any existing specific Accomm selection is unselected
	if (parseInt(itemnum) < 0) {
		var confObj = document.getElementsByName('conferenceaccommodationid');
		for (i=0; i < confObj.length; i++) {
			var numId = confObj[i].id.split('_')[1];
			var numObject = document.getElementById('accommrooms_' + numId);
			numObject.selectedIndex = 0; 
			if (document.getElementById('accommDetails')) {
				var numObject = document.getElementById('accommnums_' + numId);
				numObject.selectedIndex = 0; 
				document.getElementById('hiddennums_' + numId).value=0;
				document.getElementById('hiddenaccomm_' + numId).value='';
				document.getElementById('hiddenrooms_' + numId).value=0;
				var hiddenTable = document.getElementById('accomm_' + numId + '_occupants');
				hiddenTable.style.display ='none';
			} else	
				selectRooms(numObject, numId);
		}
	}
}

// Check if the new specified dates for the accomm are within the Block Accomm Dates
// Highlight as Request using CSS if not.
function accommChangeDates(accommId) {
	
	var dateArray = document.getElementById('accommArrive_' + accommId).value.split('-');
	var arriveDate = new Date(dateArray[1] + ' ' + dateArray[0] + ', ' + dateArray[2]);
	
	dateArray = document.getElementById('accommDepart_' + accommId).value.split('-');
	var departDate = new Date(dateArray[1] + ' ' + dateArray[0] + ', ' + dateArray[2]);
	
	var blockStart = new Date(document.getElementById('blockStartDate_' + accommId).value);
	var blockEnd = new Date(document.getElementById('blockEndDate_' + accommId).value);
	
	var numSelected = document.getElementById('accommrooms_' + accommId).selectedIndex;
	var accommWrapper = document.getElementById('accommSpecific_' + accommId);
	var accommWarning = document.getElementById('requestWarning_' + accommId);
	var warningText = '<h3>On Request</h3><div>The dates you have selected are outside the confirmed accommodation blocked dates.<br />You will be contacted when your requested dates are confirmed, at which time payment will be required.<br />Prices and availability may vary for room nights outside the accommodation block dates.<br />You will be contacted for payment if applicable.<br />You do not need to supply any Occupant Names for rooms On Request.</div>';

	if (numSelected > 0 && (arriveDate < blockStart || departDate > blockEnd)) {
		accommWrapper.className = 'accommSpecificRequest';
		accommWarning.innerHTML = warningText;
	}
	else {
		accommWrapper.className = 'accommSpecific';
		accommWarning.innerHTML = '';
	}
}


// When a room value is changed, need to update the conferenceaccommodation hidden fields
function selectRooms(numObj, roomid) {
	var numSelected = parseInt(numObj.options[numObj.selectedIndex].value);
	var hiddenObj = document.getElementById('hiddenrooms_' + roomid);
	var detailsObj = document.getElementById('accommDetails_' + roomid);

if (numSelected > 0) {
		hiddenObj.value = roomid;
		detailsObj.style.display = '';
		accommChangeDates(roomid);
	}
	else {
		hiddenObj.value = '';
		detailsObj.style.display = 'none';
		accommChangeDates(roomid);
	}
}

function selectRooms2(numObj, roomid) {
	var numSelected = parseInt(numObj.options[numObj.selectedIndex].value);
	var hiddenObj = document.getElementById('hiddenrooms_' + roomid);
	var detailsObj = document.getElementById('accommDetails_' + roomid);
	var hiddenDels = document.getElementById('regs_selected'); //number of delegates chosen
	if (numSelected > 0) {
		
		hiddenObj.value = roomid;
		detailsObj.style.display = '';
		accommChangeDates(roomid);
		//Check for the existence of fields up to maxrooms

	for (i = 1; i < 11 ; i++) {								  
				if (document.getElementById('div_accomm_' + roomid + '_room_' + i)) {
					hideObj1 = document.getElementById('div_accomm_' + roomid + '_room_' + i);
					//if room is greater than delegates then don't display
					//if the number of delegates is 1 then hide the second and subsequent name fields
					//ADD MESSAGES to alert....
					if (i > numSelected) 
						hideObj1.style.display ='none';
					else 
						hideObj1.style.display ='';	
				}
		}
	}
	else {
		hiddenObj.value = '';
		detailsObj.style.display = 'none';
		accommChangeDates(roomid);
	}
}

function selectRooms3(numObj, roomid) {
	var numSelected = parseInt(numObj.options[numObj.selectedIndex].value);
	var confObj = document.getElementById('hiddenaccomm_'+roomid);
	confObj.value=roomid;
	
	if (numObj.id.substring(0,11) == "accommrooms") {
		hiddenObj = document.getElementById('hiddenrooms_' + roomid);
		hiddenPeopleObj = document.getElementById('hiddennums_' + roomid);
		numPeople = document.getElementById('hiddennums_' + roomid).value;
		numRooms = numSelected;

		if (numPeople < 1) {
			hiddenPeopleObj.selectedIndex = "1";
			numPeople = 1;
		}
	} else {
		hiddenObj = document.getElementById('hiddennums_' + roomid);
		numPeople = numSelected;
		numRooms = document.getElementById('hiddenrooms_' + roomid).value;
		maxpersons=document.getElementById('maxPeople_'+ roomid).value;
 		surTitle =  document.getElementById('sur_' + roomid);
		sur1Value =  document.getElementById('sur1_' + roomid);
		sur2Value =  document.getElementById('sur2_' + roomid);
		
 		if (numPeople > 2) {
			surTitle.style.display = 'block';
			sur1Value.style.display = 'block';
			sur2Value.style.display = 'none';
			if (numPeople > 3) 
				sur2Value.style.display = '';
		} else {
			surTitle.style.display = 'none';
			sur1Value.style.display = 'none';
			sur2Value.style.display = 'none';
		}
	}
	if (numSelected > 0 ) hiddenObj.value = numSelected;
	hiddenNumsMax = document.getElementById('maxPeople_' + roomid);
	maxnums = hiddenNumsMax.value;
	hiddenRoomsMax = document.getElementById('hiddenmaxroom_' + roomid);
	maxRooms = hiddenRoomsMax.value;
	var hiddenTable = document.getElementById('accomm_' + roomid + '_occupants');
	if ((numRooms == 0) || (numPeople == 0)) {
		hiddenTable.style.display ='none';
		//may need more
	}
	
	if ((numRooms > 0) && (numPeople >0)) {
		confObj.value=roomid;
		hiddenTable.style.display ='';
		hiddenTableCol2 = document.getElementById('accomm_' + roomid + '_occupants2');
		hiddenTableCol3 = document.getElementById('accomm_' + roomid + '_occupants3');
		if (numPeople > 1) {
			hiddenTableCol2.style.display = '';
			hiddenTableCol3.style.display = 'none';	
			if (numPeople > 2)
				hiddenTableCol3.style.display = '';	
		}
		for (i = 1; i <= maxRooms ; i++) 	{							  
					hiddenRowLabel = document.getElementById('accomm_' + roomid + '_occupants_room_' + i + '_label');					
					hiddenRow = document.getElementById('accomm_' + roomid + '_occupants_room_' + i);
					hiddenRow2 =  document.getElementById('accomm_' + roomid + '_room_' + i +'_occupants2');
					hiddenRow3 =  document.getElementById('accomm_' + roomid + '_room_' + i +'_occupants3');
					if (i <= numRooms) {
						hiddenRowLabel.style.display ='block'; 
						hiddenRow.style.display ='block'; 
						if (numPeople > 1) {
							hiddenRow2.style.display = 'block';	
							hiddenRow3.style.display = 'none';
							if (numPeople > 2)
								hiddenRow3.style.display = 'block';
						} else {	
							hiddenTableCol2.style.display = 'none';
							hiddenRow2.style.display = 'none';	
							hiddenRow3.style.display = 'none';	
							hiddenTableCol3.style.display = 'none';	
						}
					} else {
						hiddenRow.style.display ='none';
						hiddenRowLabel.style.display ='none'; 
						hiddenRow2.style.display = 'none';	
						hiddenRow3.style.display = 'none';
					}
		}
	} else confObj.value="";
}


function updateCosts(roomrate, accommid, roomnightid) {
	roomrate=parseInt(roomrate);
	
	//first make sure they've selected the number of rooms and the number of people
	if (document.getElementById('accommrooms_' + accommid).selectedIndex == 0) { 
			alert('Please select the number of rooms you require first.');
			document.getElementById('accommrooms_' + accommid).focus();
			return false;
				} 
				else
				{
					if (document.getElementById('accommnums_' + accommid).selectedIndex == 0) {
						alert('Please select the people per room.');
						document.getElementById('accommnums_' + accommid).focus();
						return false;
					}
					
				}
				
				
				
	
	switch(roomrate) { //SWITCH
		case 2:
			if (document.getElementById('accommday_' + accommid + '_droomrate_' + roomnightid).checked) {	//1
			roomsavailable = Number(document.getElementById('accommday_' + accommid + '_roomsavailable_' + roomnightid).value);
			roomsselected = Number(document.getElementById('accommrooms_'+ accommid).value);
				if (roomsavailable < roomsselected) { 
			alert('There are not enough rooms available on this date to meet your request.\nPlease select less rooms or choose other nights.');
			return false;
				} 
				if (document.getElementById('accommday_' + accommid + '_sroomrate_' + roomnightid) && 
						document.getElementById('accommday_' + accommid + '_sroomrate_' + roomnightid).checked) { //2
							alert('You have both Room Rates checked for this day. Please choose ONE only.');
							document.getElementById('accommday_' + accommid + '_sroomrate_' + roomnightid).checked = 0;
							document.getElementById('accommday_' + accommid + '_droomrate_' + roomnightid).checked = 0;
							document.getElementById('accommday_' + accommid + '_cost_' + roomnightid).value = '';
							document.getElementById('accommday_' + accommid + '_costgstinc_' + roomnightid).value = '';
							document.getElementById('accommday_' + accommid + '_roomtype_' + roomnightid).value = '';
					} else {		
						document.getElementById('accommday_' + accommid + '_cost_' + roomnightid).value = 
								document.getElementById('accommday_' + accommid + '_double_' + roomnightid).value;
						document.getElementById('accommday_' + accommid + '_costgstinc_' + roomnightid).value = 
								document.getElementById('accommday_' + accommid + '_doublegstinc_' + roomnightid).value;
						document.getElementById('accommday_' + accommid + '_roomtype_' + roomnightid).value = 'double';	
					} //2	
			} else {
				document.getElementById('accommday_' + accommid + '_cost_' + roomnightid).value = '';
				document.getElementById('accommday_' + accommid + '_costgstinc_' + roomnightid).value = '';
				document.getElementById('accommday_' + accommid + '_roomtype_' + roomnightid).value = '';
			} //1 
			break; 
		case 1: 
			if (document.getElementById('accommday_' + accommid + '_sroomrate_' + roomnightid).checked) {//1
			roomsavailable = Number(document.getElementById('accommday_' + accommid + '_roomsavailable_' + roomnightid).value);
			roomsselected = Number(document.getElementById('accommrooms_'+ accommid).value);
				if (roomsavailable < roomsselected) { 
					alert('There are not enough rooms available on this date to meet your request.\nPlease select less rooms or choose other nights.');
				return false;
				} 
				if (document.getElementById('accommday_' + accommid + '_droomrate_' + roomnightid) && 
								document.getElementById('accommday_' + accommid + '_droomrate_' + roomnightid).checked) { //2
							alert('You have both Room Rates checked for this day. Please choose ONE only.');
							document.getElementById('accommday_' + accommid + '_sroomrate_' + roomnightid).checked = 0;
							document.getElementById('accommday_' + accommid + '_droomrate_' + roomnightid).checked = 0;
							document.getElementById('accommday_' + accommid + '_cost_' + roomnightid).value = '';
							document.getElementById('accommday_' + accommid + '_costgstinc_' + roomnightid).value = '';
							document.getElementById('accommday_' + accommid + '_roomtype_' + roomnightid).value = '';
						} else {
							document.getElementById('accommday_' + accommid + '_cost_' + roomnightid).value = 
								document.getElementById('accommday_' + accommid + '_single_' + roomnightid).value;
							document.getElementById('accommday_' + accommid + '_costgstinc_' + roomnightid).value = 
								document.getElementById('accommday_' + accommid + '_singlegstinc_' + roomnightid).value;
							document.getElementById('accommday_' + accommid + '_roomtype_' + roomnightid).value = 'single';
						} //2
			} else {
				document.getElementById('accommday_' + accommid + '_cost_' + roomnightid).value = '';
				document.getElementById('accommday_' + accommid + '_costgstinc_' + roomnightid).value = '';
				document.getElementById('accommday_' + accommid + '_roomtype_' + roomnightid).value = '';
			} //1
			break;
		} //SWITCH
} //END


function postform_1(frm, accommcount) {
	//Not Required as Booker is not used - 12/12/2006 - Peter Stewart
	//if(!ValidateField(frm.name, "text", "Please enter your name, as the contact for this booking."))
	//	return false;
	if (frm.title && frm.title.options[frm.title.selectedIndex].value == '') {
		if (frm.titleother.value == '') {
			alert('Please enter a title for Attendee #1.');
			frm.titleother.focus();
			return false;
		}
	}

	if (!ValidateField(frm.password, "text", "Please enter a password."))
		return false;
	if (!ValidateField(frm.password_confirm, "text", "Please confirm your password."))
		return false;
	if (frm.password.value != frm.password_confirm.value) {
		alert("The passwords you have entered do not match. Please enter your password again.");
		frm.password.value = '';
		frm.password_confirm.value = '';
		frm.password.focus();
		return false;
	}
	if (frm.password.value.length < 5) {
		alert("The password you have entered is less than 5 characters. Please enter your password again.");
		frm.password.value = '';
		frm.password_confirm.value = '';
		frm.password.focus();
		return false;
	}
	
	if (validateCustomFields() == false)
		return false;


	// If accomm is selected, ensure there are enough rooms for the number of attendees
	var totalAccommSpace = 0;
	var accSel = document.getElementsByName('conferenceaccommodationid');// all the hotels 
	var selConfid = parseInt(document.getElementById('confid').value);

	for (i=0; i < accSel.length; i++) { //Loop through all the hotels

		venueOK = true;

		if (accSel[i].value != '' ) { 
			var thisPpl = document.getElementById('maxPeople_' + accSel[i].value).value;
			var thisRooms = document.getElementById('accommrooms_' + accSel[i].value).selectedIndex;
			var thisGuests = document.getElementById('accommnums_' + accSel[i].value).selectedIndex;
			var thisVenue = document.getElementById('hiddenvenuename_' + accSel[i].value).value;

			totalAccommSpace += thisPpl * thisRooms;

				for (ii=1 ; ii < thisRooms+1; ii=ii+1) {
						room_name=document.getElementById('accomm_'+accSel[i].value+'_room_'+ii+'_occ_1').value;

						if (room_name.length == 0) {
							alert('You need to supply a name for each room chosen.'); 							
						room_name.focus;
						return false; 
							}
					}

			if ((thisRooms > 0) && (thisGuests >0)) {
				nights=document.getElementById('accommNights_' + accSel[i].value).value;

				nightselected = false;
				nightindex1 = 0;
				nightindex2 = 0;

				for  (jj=1 ; jj <= nights; jj=jj+1) { //1 

						if (document.getElementById('accommday_' + accSel[i].value + '_cost_' + jj).value != '') { //3
							nightselected = true;
							}
							else {
							nightindex1 = jj;
						} //3
				} //1
				if (!nightselected) { //2
						alert ('You have not chosen any particular dates for your stay.\n Please select at least one night.');
						return false;
				} else { //2
				
// not working - check logic above, should loop through checkboxes and only alert if one is not checked
					if ((nightindex1) > 0) {//3
						if (confirm ('Are you sure you have chosen all your nights for ' + thisVenue + '.\nIf yes click OK to continue or click on Cancel to check your accommodation bookings.') && venueOK) venueOK=true;
						else return false;
					}//3
				} //2
				//return false;
			}
		}
	}
	
	
	if (document.getElementById('numberofdelegates1')) {
		numDel = document.getElementById('numberofdelegates1');
		numDelegates = numDel.value; }
	else {
		numDel = document.getElementById('numberofdelegates');
		numDelegates = numDel.options[numDel.selectedIndex].value;
	}
	if (numDelegates > totalAccommSpace && totalAccommSpace > 0) {
		alert('You have chosen more Attendees than can be accommodated in your current Accommodation selection.\nPlease select more rooms or reduce your number of Attendees.');
		//return false;
	}
	// Check Continuity over dates
	// Check for Continuity of Rates over dates selected
	return true;
}

function selectGender(obj, genderobjname, titleotherfld) {
	genderobj = document.getElementsByName(genderobjname)[0];
	document.getElementById(titleotherfld).style.display = 'none';
	switch(obj.options[obj.selectedIndex].value) {
		case 'Mrs': case 'Ms': case 'Miss':case 'Lady':
			genderobj.selectedIndex = 1;
			break;
		case '':
			document.getElementById(titleotherfld).style.display = '';
			break;
		default:
			genderobj.selectedIndex = 0;
	}
}

function selectFlightCity(obj, titleotherfld) {
	document.getElementById(titleotherfld).style.display = 'none';
	switch(obj.options[obj.selectedIndex].value) {
		case '':
			document.getElementById(titleotherfld).style.display = '';
			break;
	}
}

function validateCustomFields() {
	// Retrieves all required fields in the form, and ensures data has been entered.
	// Uses the title attribute to personalise the error msg.
	objlist = document.getElementsByName('customfield_isrequired');
	if (objlist.length) {
		for (i=0; i < objlist.length; i++) {
			obj = document.getElementsByName(objlist[i].value)[0];
			if (obj.value == '') {
				alert('Please enter a ' + objlist[i].title + ' before proceeding.');
				obj.focus();
				return false;
			}		
		}
	}	
	
	// ensure that any customfield of the date type are validated
	// these fields will be listed in the customfield_formatdate hidden field
	objlist = document.getElementsByName('customfield_formatdate');
	if (objlist.length) {
		for (i=0; i < objlist.length; i++) {
			obj = document.getElementsByName(objlist[i].value)[0];
			
			if (obj.value != "" && obj.value.replace(/^[0-9]{1,2}(\-)['Jan'|'Feb'|'Mar'|'Apr'|'May'|'Jun'|'Jul'|'Aug'|'Sep'|'Oct'|'Nov'|'Dec']{3}(\-)[0-9]{4}$/, "") != "") {
				alert('Please enter a valid format (dd-mmm-yyyy) for ' + objlist[i].title + ' before proceeding.');
				obj.focus();
				return false;	
			}
		}
	}
	
	
	// ensure that any customfield of the date type are validated
	// these fields will be listed in the customfield_formatdate hidden field
	objlist = document.getElementsByName('customfield_formatnumeric');
	if (objlist.length) {
		for (i=0; i < objlist.length; i++) {
			obj = document.getElementsByName(objlist[i].value)[0];
			
			if (obj.value != "" && obj.value.replace(/^[0-9]+$/, "") != "") {
				alert('Please enter a valid format (numeric) for ' + objlist[i].title + ' before proceeding.');
				obj.focus();
				return false;	
			}
		}
	}
	
		// ensure that any customfield of the date type are validated
	// these fields will be listed in the customfield_formatdate hidden field
	objlist = document.getElementsByName('customfield_formatemail');
	if (objlist.length) {
		for (i=0; i < objlist.length; i++) {
			obj = document.getElementsByName(objlist[i].value)[0];
			
			if (obj.value != "" && (obj.value.length < 6 || obj.value.indexOf("@") < 1 || obj.value.indexOf("@") > (obj.value.length - 5) || obj.value.indexOf("@") != obj.value.lastIndexOf("@") || obj.value.lastIndexOf(".") > (obj.value.length - 3))) {
				alert('Please enter a valid format (name@example.com) for ' + objlist[i].title + ' before proceeding.');
				obj.focus();
				return false;
			}
		}
	}
	
	return true;
}



/*------------------------------------------------
	2nd Stage - Delegate Details
-------------------------------------------------*/

currentDelegateSummary = null;

function toggleDelegateSummary(objid) {
	obj = document.getElementById(objid);
	
	if (currentDelegateSummary != null && currentDelegateSummary != obj)
		currentDelegateSummary.style.display = 'none';

	if (obj.style.display == 'none') {
		obj.style.display = '';
		currentDelegateSummary = obj;
	}
	else
		obj.style.display = 'none';
	
}

function checkDayLimit (frm)
{
	if (frm._needCheckDayLimit.value == "0")
		return true;
	var cnt = 0;
	var firstObj = null;
	var daynum = parseInt(frm.FreeChoiceDayNumber.value);
	var alldaynum = parseInt(frm.FreeChoiceAllDayNumber.value);
	for (var i=0; i< alldaynum; i++)
	{
		var obj = document.getElementById("day"+i);
		if (firstObj == null)
			firstObj = obj
		if (obj.checked)
			cnt ++;
	}
	if (firstObj != null)
	{
		if (cnt<=0 || cnt>daynum)
		{
			firstObj.focus();
			return false;
		}
	}	
	return true;
}
function postform_2(frm, direction, hasaddons, hastravelinsurance, numdelegates, simpleformat, relroot) {
	redirectpage = frm.redirectpage.value;
	urltoken = frm.urltoken.value;
	
	// determine which page to go to next after processing
	
	if (direction == "prev") // go back to first stage of booking
		redirobj = 'conference';
	else if (hasaddons == 0) // no addons - go to booking summary
		redirobj = 'conference_4';
	else // if there are addons for this conference, go to the addons page next
		redirobj = 'conference_addons';
	
	frm.action = frm.relroot.value + "booking_conference_pro.cfm?redirectobject=" + redirobj + "&" + urltoken;
	
	// only validate form if moving to the next step - if going backwards then don't validate
	if (direction == "submit") {
		if (!checkDayLimit(frm))
		{
			alert("Please select days (1 ~ "+frm.FreeChoiceDayNumber.value+") for the booking.");
			return false;
		}
		for (i=1; i <= numdelegates; i++) {
			titleobj = eval('frm.delegate' + i + 'title');
			titleotherobj = eval('frm.delegate' + i + 'titleother');
			if (titleobj && titleobj.options[titleobj.selectedIndex].value == '') {
				if (titleotherobj.value == '') {
					alert('Please enter a title.');
					titleotherobj.focus();
					return false;
				}
			}
			if(!ValidateField(eval('frm.delegate' + i + 'firstname'), "text", "Please enter Attendee #" + i + "'s first name."))
				return false;
			if(!ValidateField(eval('frm.delegate' + i + 'surname'), "text", "Please enter Attendee #" + i + "'s surname."))
				return false;

			if (hastravelinsurance && !simpleformat) {
				if (eval('frm.delegate' + i + 'insurance') && eval('frm.delegate' + i + 'insurance').checked == true) {
					dayval = parseInt(eval('frm.delegate' + i + 'dob_1').options[eval('frm.delegate' + i + 'dob_1').selectedIndex].value);
					monthval = parseInt(eval('frm.delegate' + i + 'dob_2').options[eval('frm.delegate' + i + 'dob_2').selectedIndex].value);
					yearval = parseInt(eval('frm.delegate' + i + 'dob_3').options[eval('frm.delegate' + i + 'dob_3').selectedIndex].value);
					invalid = false;
					switch(monthval) {
						case 4: case 6: case 9: case 11:
							if (dayval == 31)
								invalid = true;
							break;
						case 2:
							maxdays = 28;
							if (yearval % 4 == 0)
								maxdays = 29;
							if (dayval > maxdays)
								invalid = true;
							break;
					}
					if (invalid == true) {
						alert('Please enter a valid birthdate for Attendee #' + i);
						eval('frm.delegate' + i + 'dob_1').focus();
						return false;
					}
	
					if (eval('frm.delegate' + i + 'resident').checked == false) {
						if(!ValidateField(eval('frm.delegate' + i + 'nationality'), "text", "Insurance - Attendee #" + i + "\n\nIf you're not a national resident, you must enter your nationality & passport number."))
							return false;
						if(!ValidateField(eval('frm.delegate' + i + 'passport'), "text", "Insurance - Attendee #" + i + "\n\nIf you're not a national resident, you must enter your nationality & passport number."))
							return false;
					}
				}
			}
		}
		if (validateCustomFields() == false)
			return false;
	}

	frm.submit();
	return false;
}


function changeResident(obj, delnum) {
	if (obj.checked == false)
		displaytype = '';
	else
		displaytype = 'none';

	document.getElementById('ins_' + delnum + '_nationality').style.display = displaytype;
	document.getElementById('ins_' + delnum + '_passport').style.display = displaytype;

	//return true;//obj.checked = !obj.checked;
}


// accepts parameters for a flight request and sets Request Flight fields to those values
function requestflight(num, departcity, returncity, departclass, departday, departmonth, departyear, returnday, returnmonth, returnyear, isreturn, returnflightcount) {				
	if (departcity != '') {
		document.getElementsByName('delegate' + num + 'Departflightrequestcity')[0].value = departcity;
		settrue(document.getElementsByName('delegate' + num + 'Departflightid'), -2);
		settrue(document.getElementsByName('delegate' + num + 'Departflightrequestflightclassid'), departclass);
		ctrl = document.getElementsByName('delegate' + num + 'Departflightrequestdate');
		ctrl[0].value = departday + '-' + departmonth + '-' + departyear;
		ctrl[1].value = 12;
		ctrl[2].value = 00;
	}		
	if (returncity != "" && returnflightcount > 0) {
			document.getElementsByName('delegate' + num + 'Returnflightrequestcity')[0].value = returncity;	
			settrue(document.getElementsByName('delegate' + num + 'Returnflightid'), -2);
			settrue(document.getElementsByName('delegate' + num + 'Returnflightrequestflightclassid'), departclass);
			ctrl = document.getElementsByName('delegate' + num + 'Returnflightrequestdate');
			ctrl[0].value = returnday + '-' + returnmonth + '-' + returnyear;
			ctrl[1].value = 12;
			ctrl[2].value = 00;
	}
	alert('The flight you selected has been placed on Request.');
}

// accepts an input control name and sets that control to the specified value
function settrue(control, value) {
	if (control.length) {
		for (var i=0; i < control.length; i++) {
			if (control[i].value == value) {
				control[i].selected = true;
				control[i].checked = true;
			}
		}
	} 
	else if (control.value == value) {
		control.selected = true;
		control.checked = true;
	}
}


function clickRegOption(num, typeid) {
	regtypeobj = document.getElementsByName('delegate' + num + 'bookingtypeid')[0];
	alldaysobj = document.getElementsByName('delegate' + num + 'bookingalldayspricingid');
	singledaysobj = document.getElementsByName('delegate' + num + 'bookingsingledayspricingids');

	switch (typeid) {
		case 1:
			//deselectchecks(singledaysobj);
			for (i=0; i < singledaysobj.length; i++) {
				singledaysobj[i].checked = false;
				singledaysobj[i].disabled = true;
			}
			for (i=0; i < alldaysobj.length; i++)
				alldaysobj[i].disabled = false;
				
			alldaysobj[0].checked = true;
			break;
		case 2:
			for (i=0; i < singledaysobj.length; i++)
				singledaysobj[i].disabled = false;
			for (i=0; i < alldaysobj.length; i++) {
				alldaysobj[i].checked = false;
				alldaysobj[i].disabled = true;
			}
			break;
		default:
			for (i=0; i < singledaysobj.length; i++) {
				singledaysobj[i].checked = false;
				singledaysobj[i].disabled = true;
			}
			for (i=0; i < alldaysobj.length; i++) {
				alldaysobj[i].checked = false;
				alldaysobj[i].disabled = true;
			}
			break;
	}
}

function deselectchecks(checks) {
	if (checks.length == 1) {
		if (checks.checked != false)
			checks.checked = false;
	} 
	else {
		for (var i=0 ; i < checks.length ; i++) {
			if (checks[i].checked != false)
				checks.checked = false;
		}
	}
}

function setchecksstatus(checks, val) {
	if (checks.length == 1) {
		checks.disabled = val;
	} 
	else {
		for (var i=0 ; i < checks.length ; i++)
			checks[i].disabled = val;
	}
}

function changeGender(num) {
	// change the gender based on title selection
	titleSelect = eval('document.bookingconference.delegate' + num + 'titleid');
	genderSelect = eval('document.bookingconference.delegate' + num + 'genderid');

	titleid = titleSelect.options[titleSelect.selectedIndex].value;
	if (titleid == 1 || titleid == 4) {
		genderSelect.value = 1;
	} else if (titleid == 2 || titleid == 3) {
		genderSelect.value = 2;
	}
}

function updateWkshpNumbers(name) {
	objNumber = document.getElementById('ccwt_'+name.value+'_used');
	if (name.checked) {	
		if (objNumber.value == 0) {
			alert('The registrants on this booking have used the last places in this workshop.\n You cannot book any more registrants into it');
			name.checked = 0;
			return false;
		} else {
			objNumber.value = 1*objNumber.value -1;
		}
	} else {
		 if (objNumber.value == 0) {
			 	numdelegates = document.getElementById('num_delegates').value;
				// alert(numdelegates);
				for (ii=1; ii <= numdelegates; ii++) {
					x=document.getElementById('fully_booked_' + name.value + '_' + ii);
					y=document.getElementById('book_' + name.value + '_' + ii);
					x.style.display="none";
					y.style.display="";
				}
		}
		objNumber.value = 1*objNumber.value + 1;
	}
	if (objNumber.value == 0)
		alert('You have booked the last place in this workshop.\nYou will not be able to book any subsequent registrants on this form into it');	
}

/*------------------------------------------------
	3rd Stage - Addons
-------------------------------------------------*/

function postform_3(frm, direction) {
	if (direction == 'prev')
		frm.action = frm.relroot.value + "booking_conference_pro.cfm?redirectobject=conference_2&" + frm.urltoken.value;
	else
		frm.action = frm.relroot.value + "booking_conference_pro.cfm?redirectobject=conference_4&" + frm.urltoken.value;
	frm.submit();
	return false;
}



/*------------------------------------------------
	4th Stage - Payment Summary
-------------------------------------------------*/

submitclicked = false;
		
function postform_4(frm, direction, addoncount) {				
	if (direction == "prev") {				
		if (addoncount > 0)
			redirobject = "conference_addons";
		else
			redirobject = "conference_2";
		frm.action = frm.relroot.value + frm.redirectpage.value + '?page=booking&object=' + redirobject + '&redirectpage=' + frm.redirectpage.value + '&' + frm.urltoken.value;
		frm.submit();
		return false;
	}
	else {
		
		var totalowing = document.getElementsByName('totalowing')[0].value;
		var hoteldeferredcharge = document.getElementsByName('hoteldeferredcharge')[0].value;
		var hoteldeferredcharge_CCprovided = document.getElementsByName('hoteldeferredcharge_ccprovided')[0].value;
		
		if (submitclicked) {
			alert("You have already clicked the submit button once,\nyou can not submit multiple times as that may result\nin multiple payments.");
			return false;
		}
		
		frm.action = frm.relroot.value + 'booking_conference_pro.cfm?' + frm.urltoken.value;
		
		// First check whether the hotel requires payment and if so enforce credit card detail requirement
		if (hoteldeferredcharge == 'true') {
			
			if (!frm.termsandconditions.checked) {
				alert("Please confirm that you have read, understood & agree to the 'Terms & Conditions' by checking the box.");
				return false;
			}
			
			// Perform CC validation check here
			if (hoteldeferredcharge_CCprovided != 'true')
				if (! validateCC(frm))
				{
					return false;
				}
		}
		
		else if (totalowing > 0) {
			
			if (!frm.termsandconditions.checked) {
				alert("Please confirm that you have read, understood & agree to the 'Terms & Conditions' by checking the box.");
				return false;
			}

			// CC payment has been selected
			if (frm.paymentmethodid.length > 1) creditobj = frm.paymentmethodid[0];
			else creditobj = frm.paymentmethodid;
			
			if (creditobj.value == 1 && creditobj.checked) {
				if (! validateCC(frm))
				{
					return false;
				}
			}
		}
		else {
			if (!frm.termsandconditions.checked) {
				alert("Please confirm that you have read, understood & agree to the 'Terms & Conditions' by checking the box.");
				return false;
			}
		}
			
		
		if (confirm("Submit booking?\n\nYour booking/registration may take a few seconds to process, please do not press the Back arrow or any other keys until the system has finished processing.")) {
			submitclicked = true;						
			frm.cmdback.disabled = true;
			frm.cmdsubmit.disabled = true;
			frm.submit();
			return false;
		}
		
	}
}

function validateCC(frm) 
{
	//return true;
	// CC payment is required
	// alert('In...' + document.getElementsByName('paymentcardtypeid')[0].value);
	if (document.getElementsByName('paymentcardtypeid')[0].value == "0") {
			alert('Please choose ONE of the accepted Card Types...');
			document.getElementsByName('paymentcardtypeid').focus;
			return false;
	}
	if (!ValidateField(frm.paymentcardnumber, "text", "Your credit card details are required to proceed with this booking.\nPlease enter your card number."))
		return false;
	
	//validate cc number
	if (typeof(checkCreditCard) == "function")
	{
		if (!checkCreditCard(frm.paymentcardnumber.value, frm.paymentcardtypeid.value))
		{
			alert ("Invalid credit card number.");
			frm.paymentcardnumber.focus();
			return false;
		}
	}

	if (!ValidateField(frm.paymentnameoncard, "text", "Your credit card details are required to proceed with this booking.\nPlease enter your name as it appears on your credit card."))
		return false;	
		
		
	
	return true;	
}

function testCreditCard() {
  myCardNo = document.getElementsByName('paymentcardnumber').value;
  myCardType = document.getElementsByName('paymentcardtypeid').value;
	alert (myCardNo & ' - ' & myCardType);
  if (checkCreditCard (myCardNo,myCardType)) {
    alert ("Credit card has a valid format")
  } 
  else {alert (ccErrors[ccErrorNo])};
}


function currencyconverter() {
	frm = document.getElementById('currencyForm');
	bookingfrm = document.getElementById('bookingconference');
	wnd = window.open('http://www.xe.com/ucc/convert.cgi?template=pca-xetrade&Amount=' + bookingfrm.totalowing.value + '&From=' + frm.From.value + '&To=' + frm.To.value, 'CurrencyWindow', 'top=0,left=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,height=150,width=575');
	wnd.focus();
}



