// JavaScript Document
var NumAttendees = 0;
var theTotalDue = 0;
var theFormData;
var HasGolfError = false;
var HasSCError = false;
var HasRWError = false;
var HasEYBError = false;
var HasLunchError = false;
$(document).ready(function() {
   	
	// do stuff when DOM is ready
	$.preLoadImages("circlecheck.png", "circlex.png", "noerror.png");
	$('.ErrorMsg').hide();
	$('div[id$=error]').hide();
	$('.TableTitle').hide();
	$('#GolfTable td:nth-child(3)').hide();
	$('#SCTable td:nth-child(3)').hide();
	$('#EYBTable td:nth-child(4)').hide();
	$('#AddAttendeeBtn').click(function () {
		AddAttendee();
	});
	$('#contact_attend').click(function() {
		if ($('#contact_attend').attr('checked')) {
			
			AddAttendeetoAll(0);
			$('#AttendeeName-0').val($('#contact_name').val()); 
			$('tr[id$=Row_0] .NameCell').html($('#contact_name').val());
		} else {
			$('tr[id$=Row-0]').remove();
			$('tr[id$=Row_0]').remove();
			if ($('#BreakfastTable tr').length == 3) {
				$('.NoAttendees').show();
				$('.TableTitle').hide();
			}
		}
	});
	$('#golf_sponsor').click(function() {
		if (!($('#golf_sponsor').attr('checked'))) { $('#GolfTable td:nth-child(3) input:radio:checked').parents('tr').find('td:last input:radio').attr('checked',true); }
		UpdateTotal();
	});
	$('#sclays_sponsor').click(function() {
		if (!($('#sclays_sponsor').attr('checked'))) { $('#SCTable td:nth-child(3) input:radio:checked').parents('tr').find('td:last input:radio').attr('checked',true); }
		UpdateTotal();
	});
	$('#corp_table_sponsor').click(function() {
		if (!($('#corp_table_sponsor').attr('checked'))) { $('#EYBTable td:nth-child(4) input:radio:checked').parents('tr').find('td:last input:radio').attr('checked',true); }
		UpdateTotal();
	});
	$('#benchmark_sponsor, #foundation_sponsor, #steel_sponsor').click(function() {
		UpdateTotal();
	});
	$('#contact_name').blur(function() {
		if ($("#AttendeeNameRow-0").length)  { 
			$('#AttendeeName-0').val($('#contact_name').val()); 
			var changerow = $('tr[id$=Row_0]');
			$('td:first', changerow).html($('#contact_name').val());
		}
		
		if ($(this).val() == '') {
			$(this).next('img').attr("src", "circlex.png");
			$(this).addClass('AttendeeError');
		} else {
			$(this).next('img').attr("src", "circlecheck.png");
			$(this).removeClass('AttendeeError');
		}
		CheckForErrors();
	});
	
	$('#contact_phone').blur(function() {
		var tmpNumber = $(this).val();
		tmpNumber = tmpNumber.replace(/[^0-9]/g, '');
		if ((tmpNumber.length == 7) || (tmpNumber.length == 10)) {
			$(this).next('img').attr("src", "circlecheck.png");
			$(this).removeClass('AttendeeError');
			if (tmpNumber.length == 7) {
				tmpNumber = tmpNumber.substring(0,3) + '-' + tmpNumber.substring(3);
			} else {
				tmpNumber = "(" + tmpNumber.substring(0,3) + ') ' + tmpNumber.substring(3,6) + '-' + tmpNumber.substring(6);
			}
			$(this).val(tmpNumber);
		} else {
			$(this).next('img').attr("src", "circlex.png");
			$(this).addClass('AttendeeError');
		}
		CheckForErrors();
	});
	$('#contact_email').blur(function() {
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
		if (emailPattern.test($(this).val())) {
			$(this).next('img').attr("src", "circlecheck.png");
			$(this).removeClass('AttendeeError');
		} else {
			$(this).next('img').attr("src", "circlex.png");
			$(this).addClass('AttendeeError');
		}
		CheckForErrors();
	});
		
	$('input:radio').live("click", function() {
		UpdateTotal();
	});
	
	$('#verify_registration').click(function (e) {
		$('input:text[id^=contact_]').blur();
		if ((($('#totaldue').html() == 0) && (!$('#gift_sponsor').is(':checked'))) || ($('div[id$=error]:visible').length > 0)) {
			e.preventDefault();
			$('#basic-modal-content ul').empty();
			if (($('#totaldue').html() == 0) && (!$('#gift_sponsor').is(':checked'))) { $('#basic-modal-content ul').append('<li>You must register for at least one event.</li>'); }
			if ($('#contacterror:visible').length > 0) { $('#basic-modal-content ul').append('<li>Contact information is either missing or contains errors.</li>'); }
			if ($('#attendeeserror:visible').length > 0) { $('#basic-modal-content ul').append('<li>Names must be provided for all attendees.</li>'); }
			if (HasLunchError) { $('#basic-modal-content ul').append('<li>You must select a choice for lunch.</li>'); }	
			if (HasGolfError) { $('#basic-modal-content ul').append('<li>There is a problem with the number of golf players selected.</li>'); }
			if (HasSCError) { $('#basic-modal-content ul').append('<li>There is a problem with the number of sporting clays players selected.</li>'); }
			if (HasRWError) { $('#basic-modal-content ul').append('<li>A t-shirt size is needed for all participants in the Family 5k Run/Walk.</li>'); }
			if (HasEYBError) { $('#basic-modal-content ul').append('<li>There is a problem with the number of attendees selected for the Engineer of the Year Banquet.</li>'); }
			$('#simplemodal-container').css('height', $('#basic-modal-content div').height());
			$('#basic-modal-content').modal();
			$('#basic-modal-content').height($('#basic-modal-content').height() + 5 + 'px');
			$('#simplemodal-container').css('height', $('#basic-modal-content div').outerHeight() + 24 +'px');
		} else {
			getFormData();
			var dataToPost = JSON.stringify(theFormData);
			$.post('updatecookie.php',{ "formData": dataToPost });
			$.post('eweekregverify.php',{ "formData": dataToPost }, function (theData) { 
				//$('<div id="formContent"></div>').insertAfter('#mainContent');
				$('#mainContent').attr('id','formContent').hide();
				$('<div id="returnContent"></div>').insertBefore('#formContent').hide();
				$('#returnContent').append(theData); 
				var theVerifyForm = $('#returnContent #mainContent').html();
				$('#returnContent').empty().html(theVerifyForm).attr('id', 'mainContent').show();
				$('#make_changes').click(function () {
					$('#mainContent').remove();
					$('#formContent').attr('id','mainContent').show();
				} );
				$('#pay_by_paypal').click(function () {
					theFormData.PaymentType = 'PayPal';
					theFormData.PaymentAmount =0;
					var dataToPost = JSON.stringify(theFormData);
					$.post('updatecookie.php',{ "formData": dataToPost }, function (theData) {
						//theFormData.ConfirmNo = theData;
						//$('#ConfirmNo').val(theFormData.ConfirmNo);
						$('#contact_form').submit();
					});
				} );
				$('#pay_by_mail').click(function () {
					theFormData.PaymentType = 'Mail';
					theFormData.PaymentAmount =0;
					var dataToPost = JSON.stringify(theFormData);
					$.post('updatecookie.php',{ "formData": dataToPost }, function (theData) {
						window.location = 'registrationend.php';
					});
				} );
			} ); 
		}
		
	});
	
	UpdateTotal();
	$('#selectionerror').css('visibility', 'visible');
	$('#golferror').css('visibility','visible');
	$('#luncherror').css('visibility','visible');
	$('#scerror').css('visibility','visible');
	$('#attendeeserror').css('visibility','visible');
	$('#contacterror').css('visibility','visible');
	$('#rwerror').css('visibility','visible');
	$('#eyberror').css('visibility','visible');
	
	
	$("[id$='target-1']").ezpz_tooltip();
	
	$.getJSON("testforexistingdata.php", function(jsondata) {
  		$('input:text').val('');
		$('input:radio[id$=na]').attr('checked',true);
		$('input:checkbox').attr('checked', false);
		if (jsondata.dataavailable) {
			setFormData(jsondata.theJSONData);
		}
	});
});

function AddAttendee() {
	
	//  This function is used to add an Attendee row when the Add button is clicked.
	NumAttendees++;
	var myrow = $('<tr valign="middle" id="AttendeeNameRow-' + NumAttendees + '"></tr>').insertBefore('#AttendeeTable tr:last');
	myrow.hide();
	myrow.append('<td width="60"><p><label for="attendee"> Name:</label></p></td>');
	myrow.append('<td width="320" ><p><input style="float:left" name="attendee" type="text" id="AttendeeName-' + NumAttendees + '" size="30" />&nbsp;&nbsp;<img src="noerror.png" width="20" height="20" /></p></td>');
	myrow.append('<td width="60"><p  class="EWButton" align="center"><a id="RemoveBtn-' + NumAttendees + '" HREF="javascript:void(0)">Remove</a></p></td>');
	myrow.fadeIn();
	$('#RemoveBtn-' + NumAttendees).click(function () {
		var RowNum = $(this).attr("id").split("-")[1];
		if ($('#AttendeeName-' + RowNum).val() != '') {
			var answer = confirm ("Are you sure you want to delete " + $('#AttendeeName-' + RowNum).val() + "?");
			if (answer) {
				$('tr[id$=Row-' + RowNum + ']').remove();
				$('tr[id$=Row_' + RowNum + ']').remove();
				if ($('#BreakfastTable tr').length == 3) {
					$('.NoAttendees').show();
					$('.TableTitle').hide();
				}
			} 
		} else {
			$('tr[id$=Row-' + RowNum + ']').remove();
			$('tr[id$=Row_' + RowNum + ']').remove();
			if ($('#BreakfastTable tr').length == 3) {
				$('.NoAttendees').show();
				$('.TableTitle').hide();
			}
		}
		CheckForErrors();
		UpdateTotal();
	});
	$('#AttendeeName-' + NumAttendees).blur(function () {
		var RowNum = $(this).attr("id").split("-")[1];
		$('tr[id$=Row_' + RowNum + '] .NameCell').html($(this).val());
		if ($('#AttendeeName-' + RowNum).val() == '') {
			$('#AttendeeNameRow-' + RowNum + ' img').attr("src", "circlex.png");
			$('#AttendeeNameRow-' + RowNum).addClass('AttendeeError');
		} else {
			$('#AttendeeNameRow-' + RowNum + ' img').attr("src", "circlecheck.png");
			$('#AttendeeNameRow-' + RowNum).removeClass('AttendeeError');
		}
		CheckForErrors();
	});

	
	AddAttendeetoAll(NumAttendees);
	$('#AttendeeName-' + NumAttendees).focus();
}
function AddAttendeetoAll(RowNum) {
	// Ensure table titles are showing.
	var myrow;
	$('.NoAttendees').hide();
	$('.TableTitle').show();
	
	if (RowNum==0) {
		myrow = $('<tr id="AttendeeNameRow-0"></tr>').insertBefore('#AttendeeTable tr:first');
		myrow.hide();
		myrow.append('<td width="60"><p><label for="attendee"> Name:</label></p></td>');
		myrow.append('<td width="320"><p><input readonly name="attendee" type="text" id="AttendeeName-0" size="30" value="' + $('#contact_name').val() + '"  /></p></td>');
		myrow.append('<td width="60"><p>&nbsp;</p></td>');
		myrow.fadeIn();
	}
	// Add attendee to Proclamation breakfast table.
	if (RowNum==0) {
		myrow = $('<tr id="ProcBreakfastRow_' + RowNum +'"></tr>').insertAfter('#BreakfastTable .TableTitle');
	} else {
		myrow = $('<tr id="ProcBreakfastRow_' + RowNum +'"></tr>').insertBefore('#BreakfastTable tr:last');
	}
	myrow.append('<td class="NameCell">&nbsp;</td>');
	myrow.append('<td><div align="center"><input name="pb-' + RowNum +'" type="radio" id="pb_attendee" value="pb_attendee" /></div></td>');
	myrow.append('<td><div align="center"><input name="pb-' + RowNum +'" type="radio" id="pb_student" value="pb_student" /></div></td>');
	myrow.append('<td><div align="center"><input name="pb-' + RowNum +'" type="radio" id="pb_na" value="pb_na" checked="CHECKED" /></div></td>');
	
	// Add attendee to Past Presidents Lunch table.
	if (RowNum==0) {
		myrow = $('<tr id="PastPresidentsRow_' + RowNum +'"></tr>').insertAfter('#PastPresidentsTable .TableTitle');	
	} else {
		myrow = $('<tr id="PastPresidentsRow_' + RowNum +'"></tr>').insertBefore('#PastPresidentsTable tr:last');
	}
	myrow.append('<td class="NameCell">&nbsp;</td>');
	myrow.append('<td><div align="center"><input name="ppl-' + RowNum +'" type="radio" id="ppl_attendee" value="ppl_attendee" /></div></td>');
	myrow.append('<td><div style="position:relative;text-align:center"><select  name="lunch_choice-' + RowNum + '" id="lunch_choice-' + RowNum + '"><option value="NoLunch" selected="selected">None</option><option value="Ravioli">Four Cheese Ravioli</option><option value="Chicken Parmigiana">Chicken Parmigiana</option><option value="Shrimp">Three Piece Shrimp Paesano</option></select>&nbsp;&nbsp;<img style="position:absolute;top:0" src="noerror.png" width="20" height="20" /></div></td>');
	myrow.append('<td><div align="center"><input name="ppl-' + RowNum +'" type="radio" id="ppl_na" value="ppl_na" checked="CHECKED" /></div></td>');
	$('#lunch_choice-' + RowNum).change(function() { UpdateTotal(); });
	// Add attendee to Golf table.
	if (RowNum==0) {
		myrow = $('<tr id="GolfRow_' + RowNum +'"></tr>').insertAfter('#GolfTable .TableTitle');	
	} else {
		myrow = $('<tr id="GolfRow_' + RowNum +'"></tr>').insertBefore('#GolfTable tr:last');
	}
	myrow.append('<td class="NameCell">&nbsp;</td>');
	myrow.append('<td><div align="center"><input name="gt-' + RowNum +'" type="radio" id="gt_attendee" value="gt_attendee" /></div></td>');
	myrow.append('<td><div align="center"><input name="gt-' + RowNum +'" type="radio" id="gt_cs" value="gt_cs" /></div></td>');
	myrow.append('<td><div align="center"><input name="handicap-' + RowNum + '" type="text" id="handicap" size="4" /></div></td>');
	myrow.append('<td><div align="center"><input name="gt-' + RowNum +'" type="radio" id="gt_na" value="gt_na" checked="CHECKED" /></div></td>');
	
	
	// Add attendee to Sporting Clays table.
	if (RowNum==0) {
		myrow = $('<tr id="SCRow_' + RowNum +'"></tr>').insertAfter('#SCTable .TableTitle');	
	} else {
		myrow = $('<tr id="SCRow_' + RowNum +'"></tr>').insertBefore('#SCTable tr:last');
	}
	myrow.append('<td class="NameCell">&nbsp;</td>');
	myrow.append('<td><div align="center"><input name="sc-' + RowNum +'" type="radio" id="sc_attendee" value="sc_attendee" /></div></td>');
	myrow.append('<td><div align="center"><input name="sc-' + RowNum +'" type="radio" id="sc_cs" value="sc_cs" /></div></td>');
	myrow.append('<td><div align="center"><input name="sc-' + RowNum +'" type="radio" id="sc_na" value="sc_na" checked="CHECKED" /></div></td>');
	
	
	// Add attendee to Run/Walk table.
	if (RowNum==0) {
		myrow = $('<tr id="RWRow_' + RowNum +'" valign="middle" ></tr>').insertAfter('#RWTable .TableTitle');	
	} else {
		myrow = $('<tr id="RWRow_' + RowNum +'" valign="middle" ></tr>').insertBefore('#RWTable tr:last');
	}
	myrow.append('<td class="NameCell">&nbsp;</td>');
	myrow.append('<td><div align="center"><input name="f5k-' + RowNum +'" type="radio" id="f5k_member" value="f5k_member" /></div></td>');
	myrow.append('<td><div align="center"><input name="f5k-' + RowNum +'" type="radio" id="f5k_nonmember" value="f5k_nonmember" /></div></td>');
	myrow.append('<td><div align="center"><input name="f5k-' + RowNum +'" type="radio" id="f5k_child" value="f5k_child" /></div></td>');
	myrow.append('<td valign="middle"><div style="position:relative" align="center"><select name="tshirt_size- ' + RowNum +'" id="tshirt_size_' + RowNum + '"><option value="None" selected="selected">None</option><option value="S">Small</option><option value="M">Medium</option><option value="L">Large</option><option value="XL">X-Large</option><option value="XXL">XX-Large</option></select>&nbsp;&nbsp;<img style="position:absolute;top:0" src="noerror.png" width="20" height="20" /></div></td>');
	myrow.append('<td><div align="center"><input name="f5k-' + RowNum +'" type="radio" id="f5k_na" value="f5k_na" checked="CHECKED" /></div></td>');
	$('#tshirt_size_' + RowNum).change(function() { UpdateTotal(); });
	// Add attendee to EYB Table.
	if (RowNum==0) {
		myrow = $('<tr id="EYBRow_' + RowNum +'"></tr>').insertAfter('#EYBTable .TableTitle');	
	} else {
		myrow = $('<tr id="EYBRow_' + RowNum +'"></tr>').insertBefore('#EYBTable tr:last');
	}
	myrow.append('<td class="NameCell">&nbsp;</td>');
	myrow.append('<td><div align="center"><input name="eoy-' + RowNum +'" type="radio" id="eoy_attendee" value="eoy_attendee" /></div></td>');
	myrow.append('<td><div align="center"><input name="eoy-' + RowNum +'" type="radio" id="eoy_under30" value="eoy_under30" /></div></td>');
	myrow.append('<td><div align="center"><input name="eoy-' + RowNum +'" type="radio" id="eoy_cs" value="eoy_cs" /></div></td>');
	myrow.append('<td><div align="center"><input type="checkbox" name="veg_meal-' + RowNum + '" id="veg_meal-' + RowNum + '" disabled="true" /></div></td>');
	myrow.append('<td><div align="center"><input name="eoy-' + RowNum +'" type="radio" id="eoy_na" value="eoy_na" checked="CHECKED" /></div></td>');
	
	UpdateTotal();
	
	
}
function UpdateTotal() {
	var tempTotal = ($('input:radio[value=pb_attendee][checked]').length) * 30;
	tempTotal += ($('input:radio[value=pb_student][checked]').length) * 20;
	tempTotal += ($('input:radio[value=ppl_attendee][checked]').length) * 35;
	tempTotal += ($('input:radio[value=gt_attendee][checked]').length) * 110;
	tempTotal += ($('input:radio[value=sc_attendee][checked]').length) * 40;
	tempTotal += ($('input:radio[value=f5k_member][checked]').length) * 25;
	tempTotal += ($('input:radio[value=f5k_nonmember][checked]').length) * 30;
	tempTotal += ($('input:radio[value=f5k_child][checked]').length) * 10;
	tempTotal += ($('input:radio[value=eoy_attendee][checked]').length) * 40;
	tempTotal += ($('input:radio[value=eoy_under30][checked]').length) * 30;
	tempTotal += $('#golf_sponsor').attr('checked') * 500;
	tempTotal += $('#sclays_sponsor').attr('checked') * 425;
	tempTotal += $('#corp_table_sponsor').attr('checked') * 500;
	tempTotal += $('#benchmark_sponsor').attr('checked') * 750;
	tempTotal += $('#foundation_sponsor').attr('checked') * 500;
	tempTotal += $('#steel_sponsor').attr('checked') * 250;
	theTotalDue = tempTotal;
	$('#totaldue').html(tempTotal);
	
	HasGolfError = ($('#golf_sponsor').attr('checked')) && ($('input:radio[value=gt_cs][checked]').length != 4);
	$('#golferror').toggle(HasGolfError); 
	$('#Error-Select4Golf').toggle(($('#golf_sponsor').attr('checked')) && ($('input:radio[value=gt_cs][checked]').length < 4)); 
	$('#Error-SelectOnly4Golf').toggle(($('#golf_sponsor').attr('checked')) && ($('input:radio[value=gt_cs][checked]').length > 4)); 
	
	HasSCError = ($('#sclays_sponsor').attr('checked')) && ($('input:radio[value=sc_cs][checked]').length != 4);
	$('#scerror').toggle(HasSCError);
	$('#Error-Select4SC').toggle(($('#sclays_sponsor').attr('checked')) && ($('input:radio[value=sc_cs][checked]').length < 4)); 
	$('#Error-SelectOnly4SC').toggle(($('#sclays_sponsor').attr('checked')) && ($('input:radio[value=sc_cs][checked]').length > 4)); 
	
	HasEYBError = ($('#corp_table_sponsor').attr('checked')) && ($('input:radio[value=eoy_cs][checked]').length != 8);
	$('#eyberror').toggle(HasEYBError); 
	$('#Error-Select10Table').toggle(($('#corp_table_sponsor').attr('checked')) && ($('input:radio[value=eoy_cs][checked]').length < 8)); 
	$('#Error-SelectOnly10Table').toggle(($('#corp_table_sponsor').attr('checked')) && ($('input:radio[value=eoy_cs][checked]').length > 8)); 
	
	$('#GolfTable td:nth-child(3)').toggle($('#golf_sponsor').attr('checked'));
	$('#SCTable td:nth-child(3)').toggle($('#sclays_sponsor').attr('checked'));
	$('#EYBTable td:nth-child(4)').toggle ($('#corp_table_sponsor').attr('checked'));
	
	$('input:radio[value$=_na]:checked').parents('tr').find('td:nth-child(4) input:text').attr('disabled',true).val('');
	$('input:radio[value$=_na]:not(:checked)').parents('tr').find('td:nth-child(4) input:text').attr('disabled',false);
	$('input:radio[value$=_na]:checked').parents('tr').find('td:nth-child(5) select').attr('disabled',true).val('None');
	$('input:radio[value$=_na]:not(:checked)').parents('tr').find('td:nth-child(5) select').attr('disabled',false);
	$('input:radio[value$=_na]:checked').parents('tr').find('td:nth-child(3) select').attr('disabled',true).val('None');
	$('input:radio[value$=_na]:not(:checked)').parents('tr').find('td:nth-child(3) select').attr('disabled',false);
	$('input:radio[value$=_na]:checked').parents('tr').find('td:nth-child(5) input:checkbox').attr('disabled',true).attr('checked',false);
	$('input:radio[value$=_na]:not(:checked)').parents('tr').find('td:nth-child(5) input:checkbox').attr('disabled',false);
	
	$('select:[value="NoLunch"]:enabled').next('img').attr("src", "circlex.png").addClass('LunchError');
	$('select:[value!="NoLunch"]:enabled, select:disabled').next('img').attr("src","noerror.png").removeClass('LunchError');
	HasLunchError = ($('#PastPresidentsTable tr td .LunchError').length > 0)
	$('#luncherror').toggle(HasLunchError);
	
	$('select[id^="tshirt"]:[value="None"]:enabled').next('img').attr("src", "circlex.png").addClass('ShirtError');
	$('select[id^="tshirt"]:[value!="None"]:enabled, select:disabled').next('img').attr("src","noerror.png").removeClass('ShirtError');
	HasRWError = ($('#RWTable tr td .ShirtError').length > 0)
	$('#rwerror').toggle(HasRWError);
	
	$('#selectionerror').toggle(HasGolfError || HasSCError || HasRWError || HasEYBError || HasLunchError);
}


function CheckForErrors() {
	$('#attendeeserror').toggle($('#AttendeeTable .AttendeeError').length > 0); 
	$('#contacterror').toggle($('#ContactInfoTable .AttendeeError').length > 0); 
}

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

function getFormData () {
	theFormData = new Object;
	var theAttendeeData = [];
	theFormData.Contact_Name = $('#contact_name').val();
	theFormData.Company_Name = $('#company_name').val();
	theFormData.Contact_Phone = $('#contact_phone').val();
	theFormData.Contact_Email = $('#contact_email').val();
	theFormData.Golf_Sponsor = $('#golf_sponsor').is(':checked');
	theFormData.SClays_Sponsor = $('#sclays_sponsor').is(':checked');
	theFormData.CorpTable_Sponsor = $('#corp_table_sponsor').is(':checked');
	theFormData.Benchmark_Sponsor = $('#benchmark_sponsor').is(':checked');
	theFormData.Foundation_Sponsor = $('#foundation_sponsor').is(':checked');
	theFormData.Steel_Sponsor = $('#steel_sponsor').is(':checked');
	theFormData.Gift_Sponsor = $('#gift_sponsor').is(':checked');
	theFormData.AmountDue = theTotalDue;
	theFormData.ConfirmNo = -1;
	$('#AttendeeTable tr:not(:last)').each(function (i) {
		var theRowIndex = $(this).attr('id').split('-')[1];
		var theAttendeeName = $('input:text', $(this)).val();
		var theAttendeeInfo = new Object;
		theAttendeeInfo.AttendeeName = theAttendeeName;
		theAttendeeInfo.ProcBreakfast = $('#ProcBreakfastRow_' + theRowIndex + ' input:checked').val();
		theAttendeeInfo.PastPresidentLunch = $('#PastPresidentsRow_' + theRowIndex + ' input:checked').val();
		theAttendeeInfo.Lunch_Choice = $('#lunch_choice-' + theRowIndex).val();
		theAttendeeInfo.Golf = $('#GolfRow_' + theRowIndex + ' input:checked').val();
		theAttendeeInfo.GolfHandicap = $('#GolfRow_' + theRowIndex + ' input:text').val();
		theAttendeeInfo.SportingClays = $('#SCRow_' + theRowIndex + ' input:checked').val();
		theAttendeeInfo.RunWalk = $('#RWRow_' + theRowIndex + ' input:checked').val();
		theAttendeeInfo.RunWalkShirtSize = $('#tshirt_size_' + theRowIndex).val();
		theAttendeeInfo.EYB = $('#EYBRow_' + theRowIndex + ' input:checked').val();
		theAttendeeInfo.EYBVegMeal = $('#veg_meal-' + theRowIndex).is(':checked');
		
		theAttendeeData.push(theAttendeeInfo);
	});
	theFormData.AttendeeData = theAttendeeData;
}

function setFormData (theJSONString) {
	theFormData = JSON.parse(theJSONString);
	
	var theAttendeeData = [];
	$('#contact_name').val(theFormData.Contact_Name);
	$('#company_name').val(theFormData.Company_Name);
	$('#contact_phone').val(theFormData.Contact_Phone);
	$('#contact_email').val(theFormData.Contact_Email);
	$('#golf_sponsor').attr('checked', theFormData.Golf_Sponsor);
	$('#sclays_sponsor').attr('checked', theFormData.SClays_Sponsor);
	$('#corp_table_sponsor').attr('checked', theFormData.CorpTable_Sponsor);
	$('#benchmark_sponsor').attr('checked', theFormData.Benchmark_Sponsor);
	$('#foundation_sponsor').attr('checked', theFormData.Foundation_Sponsor);
	$('#steel_sponsor').attr('checked', theFormData.Steel_Sponsor);
	$('#gift_sponsor').attr('checked', theFormData.Gift_Sponsor);
	theTotalDue = theFormData.AmountDue;
	if (theFormData.AttendeeData.length > 0) {
		var theRowIndex = 1;
		if (theFormData.AttendeeData[0].AttendeeName == theFormData.Contact_Name) {
			$('#contact_attend').attr('checked',true);
			AddAttendeetoAll(0);
			$('#AttendeeName-0').val($('#contact_name').val()); 
			$('tr[id$=Row_0] .NameCell').html($('#contact_name').val());
			theRowIndex = 0;
		}
		
		for ( var i in theFormData.AttendeeData )	 {
			if (theRowIndex != 0) { 
				AddAttendee(); 
				$('#AttendeeName-' + theRowIndex).val(theFormData.AttendeeData[i].AttendeeName);
				$('#AttendeeName-' + theRowIndex).blur();
			}
			$('#ProcBreakfastRow_' + theRowIndex + ' input[value=' + theFormData.AttendeeData[i].ProcBreakfast + ']').attr('checked', true);
			$('#PastPresidentsRow_' + theRowIndex + ' input[value=' + theFormData.AttendeeData[i].PastPresidentLunch + ']').attr('checked', true);
			$('#lunch_choice-' + theRowIndex).val(theFormData.AttendeeData[i].Lunch_Choice);
			$('#GolfRow_' + theRowIndex + ' input[value=' + theFormData.AttendeeData[i].Golf + ']').attr('checked', true);
			$('#GolfRow_' + theRowIndex + ' input:text').val(theFormData.AttendeeData[i].GolfHandicap);
			$('#SCRow_' + theRowIndex + ' input[value=' + theFormData.AttendeeData[i].SportingClays + ']').attr('checked', true);
			$('#RWRow_' + theRowIndex + ' input[value=' + theFormData.AttendeeData[i].RunWalk + ']').attr('checked', true);
			$('#tshirt_size_' + theRowIndex).val(theFormData.AttendeeData[i].RunWalkShirtSize);
			$('#EYBRow_' + theRowIndex + ' input[value=' + theFormData.AttendeeData[i].EYB + ']').attr('checked', true);
			$('#veg_meal-' + theRowIndex).attr('checked', theFormData.AttendeeData[i].EYBVegMeal);
			theRowIndex++;
		}
	}
	$('input:text[id^=contact]').blur();
	CheckForErrors();
	UpdateTotal();
	
	if ($.getUrlVar('PayPalCancel')) {
			$('#basic-modal-content ul').empty();
			$('#basic-modal-content ul').append('<p><strong>Your regstration is not complete!<strong></p><p>You have canceled your PayPal payment.  You may either try PayPal again or select Pay by Mail.</p>'); 
			$('#simplemodal-container').css('height', $('#basic-modal-content div').height());
			$('#basic-modal-content').modal();
			$('#basic-modal-content').height($('#basic-modal-content').height() + 5 + 'px');
			$('#simplemodal-container').css('height', $('#basic-modal-content div').outerHeight() + 24 +'px');
	}
}


$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});