var aAttendees = [];
var a = -1;
var gsFirstAttendeeContact = '';
var gsEventId = 'event-1678';

$(function()
{

	$.ajaxSetup(
	{
		cache: false
	});

	//if (msOnDemandContextId == 'test')
	//{
	//	gsEventId = 'event-1669';
	//}
	
	GetEventContact();
	
})

window.onbeforeunload = function() 
{
	
}

function GetEventContact(sFirstAttendeeContact)
{
	if (gsFirstAttendeeContact == '')
	{
		if (sFirstAttendeeContact == undefined)
		{
			$.ajax(
			{
				type: 'GET',
				url: '/directory/ondemand/site.asp?method=SITE_GET_SESSION_VARIABLE&rf=TEXT&variable=eventFirstAttendeeContact',
				dataType: 'text',
				async: false,
				success: GetEventContact
			});
		}
		else
		{
			gsFirstAttendeeContact = sFirstAttendeeContact.replace('OK|', '')
		}
	}
}

function ClearEvent()
{

	$('#txtFirstName').val("");
	$('#txtSurname').val("");
	$('#txtPhone').val("");
	$('#txtMobile').val("");
	$('#txtEmail').val("");
	$('#txtNote').val("");
	$('#txtMailingAddress1').val("");
	$('#txtMailingSuburb').val("");
	$('#txtMailingState').val("");
	$('#txtMailingPostCode').val("");
	$('#txtMailingCountry').val("");
	$("#selectRotarianType").val("1");
	$("#selectClub").val("1");
	$("#txtOtherClub").val("");
	$("#txtOtherDistrict").val("");
	$('#txtDietary').val("");
	$('#txtPhysical').val("");
	$("#selectBreakfast").val("1");
	$("#selectCocktailParty").val("1");
	$("#selectDay1").val("1");
	$("#selectDinner").val("1");
	$("#selectDay2").val("1");
	$("#txtSalutation").val("");
	$("#selectTitle").val("");
	
}

function EventAddAnotherAttendee()
{
	
	if (EventAttendeeValidate())
	{
		AddAttendeeToEvent(gsEventId, 1);
		$("#divContactDetails").hide();
		$("#divAccomodationDetails").hide();
		document.body.setAttribute("additionalattendee", "1");
		$("#inputEventFinishNoAdd").show();
		ShowEventAttendees();
		ClearEvent();
		$("#txtFirstName").focus();
		window.alert("Attendee added.  Please enter details for next attendee.");
		
	}
	else
	{
		window.alert('The attendee can not be added, as the following information is missing or incorrect:\r\n' + sOnDemandValidateMessage);
	}	

}

function EventFinish()
{
	if (EventAttendeeValidate())
	{
		AddAttendeeToEvent(gsEventId, 1);
		$("#divContactDetails").hide();
		$("#divAccomodationDetails").hide();
		$("#divControl").hide();
		$("#divPersonalDetails").hide();
		$("#divConferenceOptions").hide();
		$("#divOrderDetails").show(1000);
		$("#divPaymentDetails").show(1000);
		ShowEventOrder();
	}
	else
	{
		window.alert('The attendee can not be added, as the following information is missing or incorrect:\r\n' + sOnDemandValidateMessage);
	}	
	
}

function EventFinishNoAdd()
{

	if (document.body.getAttribute("additionalattendee") != "1")
	{
		window.alert("Need at least one attendee.");
	}
	else
	{
		$("#divControl").hide();
		$("#divPersonalDetails").hide();
		$("#divConferenceOptions").hide();
		$("#divOrderDetails").show(1000);
		$("#divPaymentDetails").show(1000);
		ShowEventOrder();
	}
}

function EventPay()
{

	SubmitEvent(gsEventId);

}

function AddAttendeeToEvent(sEvent, lQuantity)
{

	var aEvent = sEvent.split('-');
	var sReturn;
	var sParam;
	var aNote = [];
	var n = -1;
	
	$("#divStatus").html("Updating...");
			
	document.body.setAttribute("eventid", aEvent[1])	
	
	if ($('#cboSurvey5842 :selected').text() != '')
	{
		aNote[++n] = 'Club: ' + $('#cboSurvey5842 :selected').text()
	}	
	
	if ($('#txtOtherClub').val() != '')
	{
		aNote[++n] = 'Other Club: ' + $('#txtOtherClub').val()
	}	
	
	if ($('#txtOtherDistrict').val() != '')
	{
		aNote[++n] = 'Other District: ' + $('#txtOtherDistrict').val()
	}	
		
	aNote[++n] = 'Welcome Party: ' + $("#selectCocktailParty :selected").text()
	aNote[++n] = 'Day 1: ' + $("#selectDay1 :selected").text()
	aNote[++n] = 'Day 2: ' + $("#selectDay2 :selected").text()
	aNote[++n] = 'Sat. Dinner: ' + $("#selectDinner :selected").text()
	aNote[++n] = 'Breakfast: ' + $("#selectBreakfast :selected").text()
	
	if ($('#txtDietary').val() != '')
	{
		aNote[++n] = 'Dietary: ' + $('#txtDietary').val()
	}	
	
	if ($('#txtPhysical').val() != '')
	{
		aNote[++n] = 'Physical: ' + $('#txtPhysical').val()
	}	
		
	if ($('#txtNote').val() != '')
	{
		aNote[++n] = 'Comment: ' + $('#txtNote').val()
	}	
		
	sParam = 'method=SITE_EVENT_ADD_ATTENDEE&site=1164&select=' + aEvent[1] +
				'&firstname=' + encodeURIComponent($('#txtFirstName').val()) +
				'&surname=' + encodeURIComponent($('#txtSurname').val()) +
				'&phone=' + encodeURIComponent($('#txtPhone').val()) +
				'&mobile=' + encodeURIComponent($('#txtMobile').val()) +
				'&email=' + encodeURIComponent($('#txtEmail').val()) +
				'&fax=' + encodeURIComponent($('#txtFax').val()) +
				'&homephone=' + encodeURIComponent($('#txtHomePhone').val()) +
				'&mailingaddress1=' + encodeURIComponent($('#txtMailingAddress1').val()) +
				'&mailingsuburb=' + encodeURIComponent($('#txtMailingSuburb').val()) +
				'&mailingstate=' + encodeURIComponent($('#txtMailingState').val()) +
				'&mailingpostcode=' + encodeURIComponent($('#txtMailingPostCode').val()) +
				'&mailingcountry=' + encodeURIComponent($('#txtMailingCountry').val()) +
				'&notes=' + encodeURIComponent(aNote.join('<br />')) +
				'&sq5939=' + encodeURIComponent($("#selectRotarianType :selected").text()) +
				'&sq6305=' + encodeURIComponent($("#selectClub :selected").text()) +
				'&sq5947=' + encodeURIComponent($("#txtOtherClub").val()) +
				'&sq5948=' + encodeURIComponent($("#txtOtherDistrict").val()) +
				'&sq5940=' + encodeURIComponent($('#txtDietary').val()) +
				'&sq5941=' + encodeURIComponent($('#txtPhysical').val()) +
				'&sq5942=' + encodeURIComponent($("#selectBreakfast :selected").text()) +
				'&sq6306=' + encodeURIComponent($("#selectCocktailParty :selected").text()) +
				'&sq6307=' + encodeURIComponent($("#selectDay1 :selected").text()) +
				'&sq6308=' + encodeURIComponent($("#selectDinner :selected").text()) +
				'&sq6309=' + encodeURIComponent($("#selectDay2 :selected").text()) +
				'&sq6444=' + encodeURIComponent($('#txtSalutation').val())
						
	if (lQuantity != undefined)
	{
		sParam = sParam + '&quantity=' + lQuantity
	}
		
	if ($("#selectBreakfast").val() == '3')	
	{
		sParam = sParam + '&productcount=1&product1=12225&invoicedescription1=' +
								encodeURIComponent($('#txtFirstName').val() + ' ' + $('#txtSurname').val() + ' Breakfast');
	}
		
	//window.alert(sParam);	
		
	sReturn = onDemandSite(sParam);
	
	//window.alert(sReturn);	
	
	var aValues = sReturn.split('|');
	
	if (aValues[1] == 'ER')
	{
		window.alert('You are missing information.  Please correct and try again.');
	}
	else
	{
		if (gsFirstAttendeeContact == '')
		{
			var aValues = sReturn.split('|');
			
			var aJSON = [];
			var j = -1;
			
			aJSON[++j] = '"id" : "' + aValues[2] + '"';
			aJSON[++j] = '"email" : "' + $("#txtEmail").val() + '"';
			aJSON[++j] = '"homephone" : "' + $("#txtHomePhone").val() + '"';
			aJSON[++j] = '"fax" : "' + $("#txtFax").val() + '"';
			aJSON[++j] = '"mobile" : "' + $("#txtMobile").val() + '"';
			aJSON[++j] = '"phone" : "' + $("#txtPhone").val() + '"';
			aJSON[++j] = '"mailingaddress1" : "' + $("#txtMailingAddress1").val() + '"';
			aJSON[++j] = '"mailingaddress2" : "' + $("#txtMailingAddress2").val() + '"';
			aJSON[++j] = '"mailingsuburb" : "' + $("#txtMailingSuburb").val() + '"';
			aJSON[++j] = '"mailingstate" : "' + $("#txtMailingState").val() + '"';
			aJSON[++j] = '"mailingpostcode" : "' + $("#txtMailingPostCode").val() + '"';
			aJSON[++j] = '"mailingcountry" : "' + $("#txtMailingCountry").val() + '"';
			aJSON[++j] = '"checkindate" : "' + $("#txtCheckInDate").val() + '"';
			aJSON[++j] = '"checkoutdate" : "' + $("#txtCheckOutDate").val() + '"';
			aJSON[++j] = '"accommodationcomments" : "' + $("#txtAccommodationComments").val() + '"';

			gsFirstAttendeeContact = '{' + aJSON.join(',') + '}';
			
			$.ajax(
			{
				type: 'POST',
				url: '/directory/ondemand/site.asp?method=SITE_SET_SESSION_VARIABLE&rf=TEXT&variable=eventFirstAttendeeContact',
				dataType: 'text',
				data: 'value=' + gsFirstAttendeeContact,
				async: true,
				cache: false
			});
			
		}
	}	

	$("#divStatus").html("");	
}

function SubmitEvent(sEvent)
{

	var sReturn;
	var sParam;
	var aEvent = sEvent.split('-');
	var sInvoiceDocumentId = '29363';
		
	$("#divStatus").html("Submitting...");	
		
	if (EventValidate())
	{	
	
		if (gsFirstAttendeeContact == '')
		{
			GetEventContact();
		}	
		
	    var oFirstAttendeeContact = eval('(' + gsFirstAttendeeContact + ')');
	
		sParam = 'method=SITE_EVENT_SUBMIT&site=1164' +
			'&contactperson=' + oFirstAttendeeContact.id +
			'&email=' + oFirstAttendeeContact.email +
			'&collectpaymentaccount=70' +
			'&collectpaymenttestamount=1.00' +
			'&ccname=' + $('#txtCreditCardName').val() +
			'&ccnumber=' + $('#txtCreditCardNumber').val() +
			'&ccexpirymonth=' + $('#selectCreditCardExpiryMonth').val() +
			'&ccexpiryyear=' + $('#selectCreditCardExpiryYear').val() +
			'&autoinvoice=1' + 
			'&autoreceipt=1' + 
			'&invoicedocument=' + sInvoiceDocumentId +
			'&[EMAIL]=' + encodeURIComponent(oFirstAttendeeContact.email) +
			'&[MOBILE]=' + encodeURIComponent(oFirstAttendeeContact.mobile) +
			'&[FAX]=' + encodeURIComponent(oFirstAttendeeContact.fax) +
			'&[HOMEPHONE]=' + encodeURIComponent(oFirstAttendeeContact.homephone) +
			'&[PHONE]=' + encodeURIComponent(oFirstAttendeeContact.phone) +
			'&[MAILINGADDRESS1]=' + encodeURIComponent(oFirstAttendeeContact.mailingaddress1) +
			'&[MAILINGADDRESS2]=' + encodeURIComponent(oFirstAttendeeContact.mailingaddress2) +
			'&[MAILINGSUBURB]=' + encodeURIComponent(oFirstAttendeeContact.mailingsuburb) +
			'&[MAILINGSTATE]=' + encodeURIComponent(oFirstAttendeeContact.mailingstate) +
			'&[MAILINGPOSTCODE]=' + encodeURIComponent(oFirstAttendeeContact.mailingpostcode) +
			'&[MAILINGCOUNTRY]=' + encodeURIComponent(oFirstAttendeeContact.mailingcountry) +
			'&nosystemtemplate=1';
								
		//window.alert(sParam);		
		
		sReturn = onDemandSite(sParam);
				
		//window.alert(sReturn);		
				
		var aValues = sReturn.split('|');
		
		if (aValues[0] == "ER")
		{
			$("#divStatus").html("Please check your credit card information and then re-submit.");
			window.alert('An error has occured submitting your event.  Please check your credit card information and re-submit.');
		}
		else
		{
		
			//Send email to SA Travel
			//var sEmail = 'stuart.mcarthur@nihon2000.com'
			var sEmail = 'satcaccommodation@tourism.sa.com;webmaster@rotarydistrict9800.com.au'
 				
			if ($("#selectAccomodation").val() != "1" || $("#selectAccommodation2").val() != "1")
			{
				sParam = 'method=SITE_SEND_EMAIL&site=1164&email=' + sEmail + '&document=29337' +
							'&[ACCOMODATION]=' + $("#selectAccomodation :selected").text() +
							'&[ACCOMMODATION2]=' + $("#selectAccommodation2 :selected").text() +
							'&[CHECKINDATE]=' + encodeURIComponent(oFirstAttendeeContact.checkindate) +
							'&[CHECKOUTDATE]=' + encodeURIComponent(oFirstAttendeeContact.checkoutdate) +
							'&[ACCOMMODATIONCOMMENTS]=' + encodeURIComponent(oFirstAttendeeContact.taccommodationcomments) +
							'&linktype=32&linkid=' + encodeURIComponent(oFirstAttendeeContact.id) +
							'&[HOMEPHONE]=' + encodeURIComponent(oFirstAttendeeContact.homephone) +
							'&[MOBILE]=' + encodeURIComponent(oFirstAttendeeContact.mobile) + 
							'&[FAX]=' + encodeURIComponent(oFirstAttendeeContact.fax) +
							'&[MAILINGADDRESS1]=' + encodeURIComponent(oFirstAttendeeContact.mailingaddress1) +
							'&[MAILINGADDRESS2]=' + encodeURIComponent(oFirstAttendeeContact.mailingaddress2) +
							'&[MAILINGSUBURB]=' + encodeURIComponent(oFirstAttendeeContact.mailingsuburb) +
							'&[MAILINGSTATE]=' + encodeURIComponent(oFirstAttendeeContact.mailingstate) +
							'&[MAILINGPOSTCODE]=' + encodeURIComponent(oFirstAttendeeContact.mailingpostcode) +
							'&[MAILINGCOUNTRY]=' + encodeURIComponent(oFirstAttendeeContact.mailingcountry);
										
				sReturn = onDemandSite(sParam);			
			}
		
			window.location.href = 'http://www.rotarydistrict9800.org.au/event_confirmation';
		}	

	}
	else
	{
		window.alert('You can not submit the event as:' + sOnDemandValidateMessage);
	}
	
	$("#divStatus").html("");	
}

function EventValidate() 
{

	sOnDemandValidateMessage = '';

	if ($('#txtCreditCardName').val() == '' || $('#txtCreditCardNumber').val() == '' || $('#selectCreditCardExpiryMonth').val() == '' || $('#selectCreditCardExpiryYear').val() == '')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # You are missing some payment details.';
	}
	
	if (sOnDemandValidateMessage != '')
	{
		return (false);
	}
	else
	{
		return (true);
	}

}

function EventAttendeeValidate() 
{

	sOnDemandValidateMessage = '';

	if ($('#txtFirstName').val() == '')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # First Name.';
	}	
	
	if ($('#txtSurname').val() == '')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Surname.';
	}	
	
	if ($('#txtEmail').val() == '')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Email.';
	}	
	
	if ($('#selectRotarianType').val() == '1')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Relationship to Rotary.';
	}	
		
	if ($('#selectClub').val() == '27640')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Club Name (D9800).';
	}	
		
	if ($('#selectCocktailParty').val() == '1')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Welcome Drinks at Adelaide Town Hall - Thursday Night.';
	}		
	
	if ($('#selectDay1').val() == '1')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Sessions on Friday.';
	}		
	
	if ($('#selectDay2').val() == '1')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Sessions on Saturday.';
	}		
	
	if ($('#selectDinner').val() == '1')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Gala Dinner on Saturday.';
	}		
	
	if ($('#selectBreakfast').val() == '1')
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Conference Breakfast on Saturday morning.';
	}		
	 
	if (document.body.getAttribute("additionalattendee") != "1")
	{
	
		if ($('#txtMailingAddress1').val() == '')
		{	
			sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Mailing Address.';
		}			
	
		if ($('#txtMailingSuburb').val() == '')
		{	
			sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Mailing Suburb.';
		}			
	
		if ($('#txtMailingState').val() == '')
		{	
			sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Mailing State.';
		}			
		
		if ($('#txtMailingPostCode').val() == '')
		{	
			sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Mailing Post Code.';
		}			
		
	}
	 	
	if (!onDemandIsEmail($('#txtEmail').val()))
	{	
		sOnDemandValidateMessage = sOnDemandValidateMessage + '\r\n # Not a valid email address.';
	}
	
	if (sOnDemandValidateMessage != '')
	{
		return (false);
	}
	else
	{
		return (true);
	}
}

function ShowEventOrder()
{
	
	var iProductCartType;
	var sParam;
	var oXML;
	var oRoot;
	var oRow;
	var aHTML = [];
	var h = -1;
	var cTotalPrice = 0;
	var cOtherTotalPrice = 0;

	aHTML[++h] = '<table border="0" cellspacing="0"  width="100%">';
	aHTML[++h] = '<tbody>'
	aHTML[++h] = '<tr class="pro_details_head">';
	aHTML[++h] = '<td align="left" style="width: 400px; font-size:14px;"><strong>&nbsp;Event Details</strong></td>';
	aHTML[++h] = '<td>&nbsp;</td><td align="right" style="width: 100px; font-size:14px;"><strong>Total</strong></td></tr>';
	
	sParam = 'method=SITE_ORDER_PRODUCT_SEARCH&cart=1&includeproducts=1&site=1164&shoporderonly=1&rf=XML'
	
	$.ajax(
	{
		type: 'GET',
		url: '/directory/ondemand/site.asp?' + sParam,
		dataType: 'xml',
		async: false,
		success: function(data, textStatus, XMLHttpRequest) 
		{

			oXML = data;
			oRoot = oXML.getElementsByTagName("ondemand").item(0);

			if (oRoot.childNodes.length == 0)
			{
				aHTML[++h] = '<tr><td valign="top" style="padding: 4px;">There are no attendees.</td></tr>';
				aHTML[++h] = '</tbody></table>';
				$('#divOrderDetails').html(aHTML.join(''));
			}
			else
			{

				aHTML[++h] = '<tr class="pro_details_head">';
				aHTML[++h] = '<td class="underline" align="left" valign="top">&nbsp;</td>';
				aHTML[++h] = '<td class="underline" align="right" valign="top">&nbsp;</td>';
				aHTML[++h] = '<td class="underline" align="right" valign="top">&nbsp;</td>';
				aHTML[++h] = '</tr>';

				for (var iRow = 0; iRow < oRoot.childNodes.length; iRow++) 
				{
					var oRow = oRoot.childNodes.item(iRow);
					
					if (onDemandXMLGetData(oRow, "totalprice") != '')
					{
						cTotalPrice = cTotalPrice + parseFloat(onDemandXMLGetData(oRow, "totalprice"));
					}

					aHTML[++h] = '<tr class="pro_checkout_details">';
					if (onDemandXMLGetData(oRow, "invoicedescription") != '')
					{
						aHTML[++h] = '<td align="left" valign="top" style="padding: 4px;">' + onDemandXMLGetData(oRow, "invoicedescription") + '</td>';
					}	
					else
					{
						aHTML[++h] = '<td align="left" valign="top" style="padding: 4px;">' + onDemandXMLGetData(oRow, "title") + '</td>';
					}
					aHTML[++h] = '<td align="left" valign="top" style="padding: 4px;">' + onDemandXMLGetData(oRow, "notes").replace() + '</td>';
					aHTML[++h] = '<td align="right" valign="top" style="padding: 4px;" id="productPrice-' + onDemandXMLGetData(oRow, "product") + '">$' + 
									onDemandXMLGetData(oRow, "totalprice") + '</td>';
					aHTML[++h] = '</tr>'
						
					
				}

				aHTML[++h] = '<tr class="pro_checkout_details">';
				aHTML[++h] = '<td align="left" class="pro_lines" valign="top">&nbsp;</td>';
				aHTML[++h] = '<td align="right" class="topline" style="color: #000000; font-size: 14px;"><strong>Total Amount:</strong></span></td>';
				aHTML[++h] = '<td align="right" class="topline" id="orderTotalPrice" style="color: #000000; font-size: 14px; width=100px;"><strong>A$' + cTotalPrice.toFixed(2) + '</strong></span></td>';
				aHTML[++h] = '</tr>';
				aHTML[++h] = '</tbody></table>';

				$('#divOrderDetails').html(aHTML.join(''));
				
			}
		}	
	})		
		
}

function ShowEventAttendees()
{
	
	var iProductCartType;
	var sParam;
	var oXML;
	var oRoot;
	var oRow;
	var aHTML = [];
	var h = -1;
	var cTotalPrice = 0;
	var cOtherTotalPrice = 0;

	aAttendees[++a] = '<tr><td style="width: 100px">' + $('#txtFirstName').val() + '&nbsp;' + $('#txtSurname').val() + '</td><td>&nbsp;</td></tr>'
		
	aHTML[++h] = '<table border="0" cellspacing="0" width="100%">';
	aHTML[++h] = '<tbody>'
	aHTML[++h] = '<tr class="pro_details_head">';
	aHTML[++h] = '<td align="left" style="width: 400px; font-size:14px;"><strong>Current Attendees</strong></td><td>&nbsp;</td></tr>';
	
	for (var iRow = 0; iRow < aAttendees.length; iRow++) 
	{
		aHTML[++h] = aAttendees[iRow];
	}

	aHTML[++h] = '</tbody></table>';

	$('#divOrderDetails').html(aHTML.join(''));
	
}


