//Get Price List
var priceStore;
var ticketPackageType = false;
dojo.xhrGet({
	url: "js/prices.json",
	handleAs: "json",
	load: function(data) {
		if(data)
			priceStore = data;
	}
});
//Keep the session so the order id doesn't blow up
function retainSession() {
	dojo.xhrGet({
		url: "session.php"
	});
}
setTimeout(retainSession,10000);
//Displays a dialog with a specified title and message
function showDialog(strTitle, strMessage) {
	var dlgInfo = dijit.byId("dlgInfo");
	dlgInfo.titleNode.innerHTML = strTitle;
	dojo.byId("dlgMessage").innerHTML = strMessage;
	dlgInfo.show();
}
//Displays dialogs for the footer buttons
function showCancellation() {
	showDialog("Cancellation Policy","Cancellations received after April 17, 2011 are non-refundable. Delegate substitution is permitted. Badges and event tickets will be distributed on site at the Conference.");
}
function showPrivacy() {
	showDialog("Privacy Policy","The information requested is collected and retained only for the purpose of registration, delegate list, vendor list, and archives for the AHLA Annual Convention &amp; Trade Show.");
}
function showConfirmation() {
	showDialog("Confirmations","You will be registered once the AHLA has received and processed your payment. An automatic acknowledgement will be provided for online registration.  Confirmations will be sent with your invoice and receipt.");
}
//Change pages in the accordion container
function nextStep() {
	dijit.byId("wizardPane").forward();
}
function previousStep() {
	dijit.byId("wizardPane").back();
}
function showStep1() {
	dijit.byId("wizardPane").selectChild(dijit.byId("step1"));
}
function confirmTicketType(newType) {
	if(!ticketPackageType) {
		ticketPackageType = true;
		return true;
	}
	var packageType;
	if(dojo.byId("ticketFull").checked && newType=="full") {
		packageType = "Individual Ticket";
	}
	if(dojo.byId("ticketIndividual").checked && newType=="individual") {
		packageType = "Full Package";
	}
	if(packageType) {
		if(confirm("Do you wish to switch from " + packageType + " registration?")) {
			return true;
		} else {
			if(newType == "full") {
				dijit.byId('ticketIndividual').setChecked(true);
			} else {
				dijit.byId('ticketFull').setChecked(true);
			}
			return false;
		}
	}
	return true;
}
//Shows appropriate ticket package
function showFullPackage() {
	resetTicketType();
	dojo.fx.wipeIn({node: 'fullPackage', duration:1000}).play();
	dojo.fx.wipeOut({node: 'individualTickets', duration:1000}).play();
}
function showIndividualPackage() {
	resetTicketType();
	dojo.fx.wipeOut({node: 'fullPackage', duration:1000}).play();
	dojo.fx.wipeIn({node: 'individualTickets', duration:1000}).play();
}
function showNeitherPackage() {
	dojo.fx.wipeOut({node: 'fullPackage', duration:1000}).play();
	dojo.fx.wipeOut({node: 'individualTickets', duration:1000}).play();
}
function showSessionPeriod(sender,showDiv) {
	if(sender.checked) {
		dojo.fx.wipeIn({node: showDiv, duration:1000}).play();
	} else {
		dojo.fx.wipeOut({node: showDiv, duration:1000}).play();
	}
}
function showSessionLength(one,two,hideAll) {
	dojo.fx.wipeOut({node: two, duration:1000}).play();
	if(hideAll) {
		dojo.fx.wipeOut({node: one, duration:1000}).play();
	} else {
		dojo.fx.wipeIn({node: one, duration:1000}).play();
	}
}
//Updates the prices based on member type
function changeCategory() {
	var HousekeepingAwardsGala = false;
	var CORAwardsBreakfast = false;
	var GoldKeyLuncheon = false;
	dojo.query(".Award").forEach(function(node) {
		if(node.checked) {
			switch(node.id) {
				case 'catHousekeeping':
					HousekeepingAwardsGala = true;
					break;
				case 'catGoldKey':
				case 'catEmployerOfChoice':
					GoldKeyLuncheon = true;
					break;
				case 'catCOR':
				case 'catMenu':
					CORAwardsBreakfast = true;
					break;
			}
		}
	});
	var prices = priceStore.MemberPrices.Default;
	dojo.query(".price").forEach(function(node){
		var value=prices[node.id];
		if(typeof(value) == 'undefined') {
			var value = prices.IndividualTickets[node.id];
		}
		if(typeof(value) != 'undefined') {
			node.innerHTML = "$" + value.Before;
		}
	});
	dojo.query(".price2").forEach(function(node){
		var value=prices[node.id.substr(0,node.id.length-5)];
		if(typeof(value) == 'undefined') {
			var value = prices.IndividualTickets[node.id.substr(0,node.id.length-5)];
		}
		if(typeof(value) != 'undefined') {
			node.innerHTML = "$" + value.After;
		} else {
			alert(node.id.substr(0,node.id.length-5));
		}
	});
	if(HousekeepingAwardsGala) {
		dojo.byId('HousekeepingAwardsGala').innerHTML = "$" + priceStore.MemberPrices.Award.IndividualTickets.HousekeepingAwardsGala.Before;
		dojo.byId('HousekeepingAwardsGalaAfter').innerHTML = "$" + priceStore.MemberPrices.Award.IndividualTickets.HousekeepingAwardsGala.After;
	}
	if(CORAwardsBreakfast) {
		dojo.byId('CORAwardsBreakfast').innerHTML = "$" + priceStore.MemberPrices.Award.IndividualTickets.CORAwardsBreakfast.Before;
		dojo.byId('CORAwardsBreakfastAfter').innerHTML = "$" + priceStore.MemberPrices.Award.IndividualTickets.CORAwardsBreakfast.After;
	}
	if(GoldKeyLuncheon) {
		dojo.byId('GoldKeyLuncheon').innerHTML = "$" + priceStore.MemberPrices.Award.IndividualTickets.GoldKeyLuncheon.Before;
		dojo.byId('GoldKeyLuncheonAfter').innerHTML = "$" + priceStore.MemberPrices.Award.IndividualTickets.GoldKeyLuncheon.After;
	}
}
function boldPrice(sender) {
	if(sender.checked) {
		changeClass("add",sender.parentNode.parentNode,"selectedTicket");
	} else {
		changeClass("remove",sender.parentNode.parentNode,"selectedTicket");
	}
}
function boldPriceTitle(sender) {
	if(sender.checked) {
		changeClass("add",sender.parentNode.parentNode.parentNode,"selectedTicket");
	} else {
		changeClass("remove",sender.parentNode.parentNode.parentNode,"selectedTicket");
	}
}
//Confirm no collisions of events
function checkMondayDinner(sender, conflict) {
	if(sender.checked && conflict.checked) {
		showDialog("Error","This meal coincides with the " + conflict.id.replace(/_/g," ") + ".  Please unselect the other dinner before selecting this one");
		sender.checked = false;
	}
}
function checkTransportation(sender, conflict) {
	if(sender.checked && conflict.checked) {
		showDialog("Error","This selection coincides with the " + conflict.id.replace(/_/g," ") + ".  Please unselect the other choice before selecting this one");
		sender.checked = false;
	}
}
function checkStep1() {
	if(!dijit.byId('registerForm').isValid()) {
		dijit.byId('registerForm').validate();
		showDialog("Error","Required field is incomplete");
		return false;
	}
	return true;
}
function checkStep2Category() {
	return true;
	var valid = false;
	dojo.query(".NonAward").forEach(function(node) {
		if(node.checked) {
			valid = true;
		}
	});
	dojo.query(".Award").forEach(function(node) {
		if(node.checked) {
			valid = true;
		}
	});
	return valid;
}
function checkStep2() {
	if(!checkStep2Category() || !dijit.byId("registerForm").getValues().TicketType) {
		if(!checkStep2Category()) {
			changeClass("add",dojo.byId("regcat").parentNode,"invalid");
		}
		if(!dijit.byId("registerForm").getValues().TicketType) {
			changeClass("add",dojo.byId("tblFullPackage").parentNode,"invalid");
			changeClass("add",dojo.byId("tblIndividual").parentNode,"invalid");
		}
		showDialog("Error","Required field is incomplete");
		return false;
	}
	return true;
}
function checkForm() {
	return (dijit.byId('registerForm').isValid() && checkStep2Category() && dijit.byId("registerForm").getValues().TicketType)
}
function resetTicketType() {
	changeClass("remove",dojo.byId("tblFullPackage").parentNode,"invalid");
	changeClass("remove",dojo.byId("tblIndividual").parentNode,"invalid");
}
//For editing previous entries
function editPreviousConfirm(id,step) {
	var btn1 = new dijit.form.Button({
		label: "Yes",
		onClick: function() {
			editPrevious(id,step,true);
		}
	});
	var btn2 = new dijit.form.Button({
		label: "No",
		onClick: function() {
			editPrevious(id,step,false);
		}
	});
	showDialog("Save?","Would you like to save your current entry before continuing?<br />");
	btn1.placeAt("dlgMessage");
	btn2.placeAt("dlgMessage");
}
function editPrevious(id,step,save) {
	if(save) {
		submitForm("edit" + step + "" + id);
	} else {
		location = "index.php?mode=edit" + step + "&id=" + id;
	}
}
//Should be pretty obvious
function submitForm(type) {
	if(type=='cancel' || checkForm()) {
		dojo.byId('registerForm').submitType.value = type;
		dojo.byId('registerForm').submit();
	} else {
		if(!dijit.byId('registerForm').isValid()) {
			showStep1();
			dijit.byId('registerForm').validate();
		} else {
			previousStep();
		}
		if(!dijit.byId("registerForm").getValues().Category) {
			changeClass("add",dojo.byId("regcat").parentNode,"invalid");
		}
		if(!dijit.byId("registerForm").getValues().TicketType) {
				changeClass("add",dojo.byId("tblFullPackage").parentNode,"invalid");
				changeClass("add",dojo.byId("tblIndividual").parentNode,"invalid");
		}
		showDialog("Error","Please fill in all required fields");
	}
}
//Show dialog at end of step 2 asking if another registration is needed
function addRegistration() {
	var btn1 = new dijit.form.Button({
		label: "Yes",
		onClick: function() {
			submitForm('add');
		}
	});
	var btn2 = new dijit.form.Button({
		label: "No",
		onClick: function() {
			dijit.byId("dlgInfo").hide();
			nextStep();
		}
	});
	showDialog("Add Another?","Would you like to register another delegate?<br />");
	btn1.placeAt("dlgMessage");
	btn2.placeAt("dlgMessage");
}
//Update the current data for the summary view
function updateStep3() {
	dojo.byId("summaryName").innerHTML = dojo.byId("registerForm").FirstName.value + " " + dojo.byId("registerForm").LastName.value;
	dojo.byId("summaryCompany").innerHTML = dojo.byId("registerForm").Company.value;
	dojo.byId("summaryPosition").innerHTML = dojo.byId("registerForm").Position.value;
	dojo.byId("summaryAddress").innerHTML = dojo.byId("registerForm").Address.value + "<br />" + dojo.byId("registerForm").City.value + ", " + dijit.byId("ProvinceSelect").getValue() + " " + dojo.byId("registerForm").PostalCode.value;
	dojo.byId("summaryPhone").innerHTML = dojo.byId("registerForm").Phone.value;
	dojo.byId("summaryEmail").innerHTML = dojo.byId("registerForm").EmailAddress.value;
	dojo.byId("summaryNeeds").innerHTML = dojo.byId("registerForm").SpecialNeeds.value;
	dojo.byId("summaryAttend").innerHTML = dojo.byId("registerForm").FirstAttend.checked?"Yes":"No";
	var registerType = "Default", memberType = "";
	var HousekeepingAwardsGala = false;
	var CORAwardsBreakfast = false;
	var GoldKeyLuncheon = false;
	dojo.query(".Award").forEach(function(node) {
		if(node.checked) {
			switch(node.id) {
				case 'catHousekeeping':
					HousekeepingAwardsGala = true;
					memberType += (memberType.length?", ":"") + "Housekeeping Award Recipient";
					break;
				case 'catGoldKey':
					GoldKeyLuncheon = true;
					memberType += (memberType.length?", ":"") + "Gold Key Recipient";
					break;
				case 'catEmployerOfChoice':
					GoldKeyLuncheon = true;
					memberType += (memberType.length?", ":"") + "Employer Of Choice Recipient";
					break;
				case 'catCOR':
					CORAwardsBreakfast = true;
					memberType += (memberType.length?", ":"") + "COR Award Recipient";
					break;
				case 'catMenu':
					CORAwardsBreakfast = true;
					memberType += (memberType.length?", ":"") + "Menu Award Recipient";
					break;
			}
		}
	});
	dojo.byId("summaryMember").innerHTML = memberType?memberType:"None";
	var ticketInfo = "";
	var attend;
	var price = 0;
	switch(dijit.byId("registerForm").getValues().TicketType) {
		case "FullPackage":
			price = priceStore.MemberPrices[registerType].FullPackage[priceTime];
			ticketInfo = "Full Package<ul>";
			attend = dojo.byId("registerForm").MondayBusinessLengthFirst.checked;
			if(attend) {
				ticketInfo += "<li>Monday Afternoon - First Business Session: ";
				if(dijit.byId("registerForm").getValues().MondayBusinessSession1) {
					ticketInfo += dijit.byId("registerForm").getValues().MondayBusinessSession1;
				} else {
					ticketInfo += "Session Not Yet Selected";
				}
				ticketInfo += "</li>";
			}
			attend = dojo.byId("registerForm").MondayBusinessLengthSecond.checked;
			if(attend) {
				ticketInfo += "<li>Monday Afternoon - Second Business Session: ";
				if(dijit.byId("registerForm").getValues().MondayBusinessSession2) {
					ticketInfo += dijit.byId("registerForm").getValues().MondayBusinessSession2;
				} else {
					ticketInfo += "Session Not Yet Selected";
				}
				ticketInfo += "</li>";
			}
			attend = dijit.byId("registerForm").getValues().MondayDinner;
			ticketInfo += "<li>Monday Dinner Session: " + (attend?attend:"Not Attending") + "</li>";
			attend = false;
			switch(dijit.byId("registerForm").getValues().TuesdayBusinessLength) {
				case "2Hour":
					attend = dijit.byId("registerForm").getValues().TuesdayBusinessAM;
					break;
				case "1Hour":
					//attend = dojo.byId("registerForm").TuesdayBusinessSessionC.checked?dojo.byId("registerForm").TuesdayBusinessSessionC.value:"";
					//attend += dojo.byId("registerForm").TuesdayBusinessSessionD.checked?(attend?", ":"") + dojo.byId("registerForm").TuesdayBusinessSessionD.value:"";
					attend = dijit.byId("registerForm").getValues().TuesdayBusinessSessionC;
					attend += dijit.byId("registerForm").getValues().TuesdayBusinessSessionD?(attend?", ":"") + dijit.byId("registerForm").getValues().TuesdayBusinessSessionD:"";
					break;
			}
			ticketInfo += "<li>Tuesday Morning Business Session: " + (attend?attend:"Not Attending") + "</li>";
			attend = dijit.byId("registerForm").getValues().TuesdayBusinessPM;
			ticketInfo += "<li>Tuesday Afternoon Business Session: " + (attend?attend:"Not Attending") + "</li>";
			attend = dijit.byId("registerForm").getValues().TuesdayDinner;
			ticketInfo += "<li>Tuesday Dinner Session: " + (attend?attend:"Not Attending") + "</li></ul>";
			break;
		case "Individual":
			ticketInfo = "Individual Tickets<ul>";
			attend = dojo.byId("registerForm").TradeShowReception.checked;
			if(attend) {
				price += priceStore.MemberPrices[registerType].IndividualTickets.TradeShowReception[priceTime];
				ticketInfo += "<li>Trade Show Reception</li>";
			}
			attend = dojo.byId("registerForm").OpeningBreakfast.checked;
			if(attend) {
				price += priceStore.MemberPrices[registerType].IndividualTickets.OpeningBreakfast[priceTime];
				ticketInfo += "<li>Opening Breakfast</li>";
			}
			attend = dojo.byId("registerForm").TradeShowLuncheon.checked;
			if(attend) {
				price += priceStore.MemberPrices[registerType].IndividualTickets.TradeShowLuncheon[priceTime];
				ticketInfo += "<li>Trade Show Luncheon</li>";
			}
			attend = dojo.byId("registerForm").IndividualMondayBusinessLengthFirst.checked;
			if(attend) {
				ticketInfo += "<li>Monday Afternoon First Business Session: ";
				if(dijit.byId("registerForm").getValues().IndividualMondayBusinessSession1) {
					ticketInfo += dijit.byId("registerForm").getValues().IndividualMondayBusinessSession1;
				} else {
					ticketInfo += "Session Not Yet Selected";
				}
				ticketInfo += "</li>";
			}
			attend = dojo.byId("registerForm").IndividualMondayBusinessLengthSecond.checked;
			if(attend) {
				ticketInfo += "<li>Monday Afternoon Second Business Session: ";
				if(dijit.byId("registerForm").getValues().IndividualMondayBusinessSession2) {
					ticketInfo += dijit.byId("registerForm").getValues().IndividualMondayBusinessSession2;
				} else {
					ticketInfo += "Session Not Yet Selected";
				}
				ticketInfo += "</li>";
			}
			//ticketInfo += attend?"<li>Monday Business Session: " + attend + "</li>":"";
			attend = dojo.byId("registerForm").CampgroundMemberDinner.checked;
			if(attend) {
				price += priceStore.MemberPrices[registerType].IndividualTickets.CampgroundMemberDinner[priceTime];
				ticketInfo += "<li>Campground Member Dinner</li>";
			}
			attend = dojo.byId("registerForm").HousekeepingAwardsGala.checked;
			if(attend) {
				ticketInfo += "<li>Housekeeping Awards Gala</li>";
				if(HousekeepingAwardsGala) {
					price += priceStore.MemberPrices.Award.IndividualTickets.HousekeepingAwardsGala[priceTime];
				} else {
					price += priceStore.MemberPrices.Default.IndividualTickets.HousekeepingAwardsGala[priceTime];
				}
			}
			attend = dojo.byId("registerForm").CORAwardsBreakfast.checked;
			if(attend) {
				ticketInfo += "<li>COR Awards Breakfast</li>";
				if(CORAwardsBreakfast) {
					price += priceStore.MemberPrices.Award.IndividualTickets.CORAwardsBreakfast[priceTime];
				} else {
					price += priceStore.MemberPrices.Default.IndividualTickets.CORAwardsBreakfast[priceTime];
				}
			}
			attend = false;
			switch(dijit.byId("registerForm").getValues().IndividualTuesdayBusinessLength) {
				case "2Hour":
					attend = dijit.byId("registerForm").getValues().IndividualTuesdayBusinessAM;
					break;
				case "1Hour":
					//attend = dojo.byId("registerForm").IndividualTuesdayBusinessAMSessionC.checked?dojo.byId("registerForm").IndividualTuesdayBusinessAMSessionC.value:"";
					//attend += dojo.byId("registerForm").IndividualTuesdayBusinessAMSessionD.checked?(attend?", ":"") + dojo.byId("registerForm").IndividualTuesdayBusinessAMSessionD.value:"";
					attend = dijit.byId("registerForm").getValues().IndividualTuesdayBusinessAMSessionC;
					attend += dijit.byId("registerForm").getValues().IndividualTuesdayBusinessAMSessionD?(attend?", ":"") + dijit.byId("registerForm").getValues().IndividualTuesdayBusinessAMSessionD:"";
					break;
			}
			ticketInfo += attend?"<li>Tuesday Morning Business Session: " + attend + "</li>":"";
			attend = dojo.byId("registerForm").GoldKeyLuncheon.checked;
			if(attend) {
				ticketInfo += "<li>Gold Key Luncheon</li>";
				if(GoldKeyLuncheon) {
					price += priceStore.MemberPrices.Award.IndividualTickets.GoldKeyLuncheon[priceTime];
				} else {
					price += priceStore.MemberPrices.Default.IndividualTickets.GoldKeyLuncheon[priceTime];
				}
			}
			attend = dojo.byId("registerForm").IndividualTuesdayBusinessPM.checked;
			if(attend) {
				price += priceStore.MemberPrices[registerType].IndividualTickets.TuesdayBusinessPM[priceTime];
				ticketInfo += "<li>" + dojo.byId("registerForm").IndividualTuesdayBusinessPM.value + "</li>";
			}
			attend = dojo.byId("registerForm").ChairmansDinner.checked;
			if(attend) {
				price += priceStore.MemberPrices[registerType].IndividualTickets.ChairmansDinner[priceTime];
				ticketInfo += "<li>Chairmans dinner</li>";
			}
			ticketInfo += "</ul>";
			break;
	}
	/*attend = dojo.byId("registerForm").TransportationFromEdmonton.checked;
	if(attend) {
		price += priceStore.MemberPrices[registerType].EdmontonRide[priceTime];
		ticketInfo += "Transportation From Edmonton<br />";
	}
	attend = dojo.byId("registerForm").TransportationFromCalgary.checked;
	if(attend) {
		price += priceStore.MemberPrices[registerType].CalgaryRide[priceTime];
		ticketInfo += "Transportation From Calgary<br />";
	}
	attend = dojo.byId("registerForm").SleighRide.checked;
	if(attend) {
		price += priceStore.MemberPrices[registerType].SleighRide[priceTime];
		ticketInfo += "Sleigh Ride<br />";
	}*/
	dojo.byId("summaryTicket").innerHTML = ticketInfo;
	dojo.byId("ticketPrice").innerHTML = "$" + price;
}
function deleteRecord(id) {
	dojo.xhrGet({
		url: "delete.php?id=" + id,
		load: function(data) {
			dijit.byId("step3").refresh();
		}
	});
}
function formatPhone(sender) {
	var phone = dojo.byId(sender.id).value;
	if(!phone.match(/\(\d{3}\) \d{3}-\d{4}/)) {
		phone.replace(/\D/g,"");
		if(phone.match(/\d{10}/)) {
			phone = "(" + phone.substr(0,3) + ") " + phone.substr(3,3) + "-" + phone.substr(6);
		}
	}
	dijit.byId(sender.id).setValue(phone);
}
function formatPostalCode(sender) {
	var pc = dojo.byId(sender.id).value;
	if(!pc.match(/\w\d\w \d\w\d/)) {
		pc = pc.substr(0,3) + " " + pc.substr(3);
	}
	dijit.byId(sender.id).setValue(pc);
}
function validateTextArea(sender) {
	if(sender.value.length) {
		changeClass("remove",sender.domNode,"invalid");
	} else {
		changeClass("add",sender.domNode,"invalid");
	}
}
