meals = {
	showHide: function() {
	   var dateSelected = $("input[name=marathon-date]:checked").val();
	   
// 	   var performanceMonthDay = new Array();
// 	   performanceMonthDay = dateSelected.split("/");
// 	   var pMonth=performanceMonthDay[0];
// 	   var pDay=performanceMonthDay[1];
// 	   
	   var now = new Date();
	   var nowMonth = now.getMonth()+1;
	   var nowDay = now.getDate();
// 	   
// 	   var pDayAdj = 31*(Number(pMonth)-1) + Number(pDay);
// 	   var nowDayAdj = 31*(Number(nowMonth)-1) + Number(nowDay);
// 	   
// 	   var dayDiff = Number(pDayAdj) - Number(nowDayAdj);
	   
//	   if (dayDiff <= 3) {

// Cutoff date for ordering meals is February 4		
//		if (nowDay > 4) {

		var mealsAvailable = false;

//		} else {
//			mealsAvailable = true;
//		}

     if (mealsAvailable==true) {
     	$(".meals-available").show();
     	$(".meals-not-available").hide();
     } else {
     	$(".meals-available").hide();
     	$(".meals-not-available").show();
     	
     	//If meals are hidden, reset to 0
     	$("option:contains(0)").attr("selected",true);
     }
	}
};

tickets = {

 cartInitialized: false,

 part1 : {
  id : '3D1BDC58-94AF-48F1-96F7-3266ECEDD2C7',
//   id : '137A419F-616E-4421-A7BB-CB0A302195A7',
  '1/7' : '7CD40502-F112-4F24-BE05-82C58C43ECC0',
  '1/8' : '4134EFC9-ED89-4520-A5B4-BD19BF069802',
  '1/9' : '9F6FC5E7-B3A7-45B9-AB06-15D398A0AA89',
  '1/10' : '8507ADF2-86A5-4F5C-880D-C451568E0D93',
  '1/12' : '759660C0-E879-4546-9B51-67D959060328',
  '1/15' : 'F2D77EC4-95C7-4B1F-802A-9A1610089620',
  '1/16' : 'E384FF3F-B002-4DE5-8BCA-0258F7FEB3AE',
  '1/17' : '7C208158-5808-48C1-B731-C6B567979DB7',
  '1/18' : '14D5B05D-F9F5-4310-BC8F-55E6F2599319',
  '1/21' : '33B4D70F-DEA2-4D41-819F-F8084196A329',
  '1/23' : '15699F99-D3F9-4944-B1CF-C7857A565EB3',
  '1/24' : 'C9EB6A3B-BFF3-4272-8073-C2D10130F101',
  '1/27' : '6D5BF241-E6D8-4F3A-8A3B-B7DB3D9EE2B6',
  '1/29' : '2AF9D3C7-2044-4CE3-931F-FD1543BDFE18',
  '1/30' : '5236F255-DEFA-42BB-8391-36C63D7872F4',
  '1/31' : '619BD96A-BA36-40B2-983B-4C52D579E1BB',
  '2/2' : '0FA7112A-BAE0-4948-98FA-E1A67C971DFB',
  '2/5' : '6AB92E8E-FF91-4537-AF92-071083ED4C71',
  '2/6' : '31B45796-CDD2-426B-87A9-437E50D1F28B',
  '2/7' : '1AE95A27-4B18-4741-93D8-EC714B471482',
  },

 part2 : {
  id : '550BA7CB-9084-4451-9A1A-58D9FFCBB4A7',
//   id : 'C68D5C2F-A18F-40F9-9020-C4297D91247F',
  '1/9' : 'DC6A7249-CD41-4FB1-B928-5E3C67848588',
  '1/10' : '5E57A695-551B-438F-8B53-D5D20FF8FAEB',
  '1/15' : 'FC9ABB69-8005-4414-8277-0A37823BDD15',
  '1/16' : '68C00532-A99E-4692-9B3D-DE3D05F5D116',
  '1/17' : '32A32024-FCDD-4FDB-8E73-76110EEC9106',
  '1/18' : 'C4D44F3A-F1FA-4946-8D2A-B22D71300CFB',
  '1/22' : '041F25A2-D9CB-41A3-8EA7-EB366C022EB9',
  '1/23' : '7EA44490-2C40-4D57-85C1-0A6D279C7693',
  '1/24' : 'DFEB5B5F-2457-43B5-BFD6-3E75EF72B546',
  '1/28' : '50AE32C5-5873-46B5-A571-E35FA933754D',
  '1/29' : 'A9931445-BF38-4723-A2B4-7A4CC5F06FC4',
  '1/30' : '8C18DBDD-9696-417E-AB74-6AC5BEC5F2CC',
  '1/31' : 'C1F8F7AF-3D67-44A4-92E2-DAFD474179B4',
  '2/3' : '0783FF5D-332D-41BE-AE7B-06A6FA0D54FA',
  '2/5' : 'EDA3A71C-BE72-4D94-97BF-717A5D976818',
  '2/6' : 'AFA98D63-1F85-4C9D-8350-8253BB29286E',
  '2/7' : '20552B36-E65D-40EC-AE4D-243111BD6D01',
 },

 'meals' : {
   '1/9' : {
     'fish' : '07D0B081-041A-4F78-8F82-EFEE3444DE05',
     'non-veggie' : 'D8E34BAE-E54B-47DC-8956-29A11F87B462',
     'veggie' : '2D75AE80-31EB-40E8-99D9-FA5C9EABB602'
   },
   '1/10' : {
     'fish' : '51B523D2-6DB5-45FC-8355-899D7408ED33',
     'non-veggie' : 'E82515AF-0572-48C9-A423-19C6D5EA826D',
     'veggie' : '8716C0CE-3098-4340-A36C-B64712450241'
   },
   '1/15' : {
     'fish' : 'F3ACEF45-9390-48A7-AA86-5276E4C71000',
     'non-veggie' : 'B820987E-0766-4DBB-ABF7-512B4999178E',
     'veggie' : 'EBF7F445-157D-4D08-9D37-A7D0149B634E'
   },
   '1/16' : {
     'fish' : 'EFDCC928-C407-4DF6-A40F-3D17E1C0CBC0',
     'non-veggie' : 'ABF6B278-07B9-4D96-A1FE-A03D57A3CF13',
     'veggie' : 'D36840E3-C3AF-47F1-8DED-AA5486C1ACE7'
   },
   '1/17' : {
     'fish' : '04D11ABA-83B3-469E-A041-32CB99CF2305',
     'non-veggie' : '92895450-2DB5-4CE6-97EC-1C146E155E36',
     'veggie' : 'EEE53882-C29D-4ED7-A514-653567BBAD61'
   },
   '1/18' : {
     'fish' : 'CE766B4E-DB87-4AF3-820D-17182800E27E',
     'non-veggie' : 'DC62B8E4-3578-4658-BF76-A4C911D8E550',
     'veggie' : '0FC3D06D-C85C-42B0-8B99-3BA66D04666C'
   },
   '1/23' : {
     'fish' : 'C8E1C60F-2327-4EB3-AB22-D75FBD875595',
     'non-veggie' : '7AD764B7-AF68-4AD0-9413-F4E2026BB77D',
     'veggie' : 'BD6CF9AD-4F45-4890-9747-562B3BE96326'
   },
   '1/24' : {
     'fish' : '6EED191D-07FD-4C89-8B75-69332F9B0980',
     'non-veggie' : 'EF8E59FC-3921-4807-AC5C-B0E25DFD8A78',
     'veggie' : '6953A699-1CBB-4FA7-9828-7B6D1BFCE228'
   },
   '1/29' : {
     'fish' : '983FCB5B-D408-4F41-AB61-D01F1B94281B',
     'non-veggie' : 'F1280143-7A52-4D05-84D2-6FCADCD81E70',
     'veggie' : '945E6248-4409-43BD-B225-C06B574368FE'
   },
   '1/30' : {
     'fish' : 'B120D43D-9C41-4363-AFED-C660B7B0F33E',
     'non-veggie' : '80673C23-1F14-4656-9F98-75BD581E32F8',
     'veggie' : 'A7F0C923-0A37-4CF4-B525-0B2864715271'
   },
   '1/31' : {
     'fish' : 'FAF33EDC-A4CC-41FC-B5C5-A3C3969C8F25',
     'non-veggie' : 'BC1A7089-C0FB-4A27-B054-D75AE4D029B7',
     'veggie' : 'EE3D10EF-3FA0-48AE-87C4-E4B8D4362A5E'
   },
   '2/5' : {
     'fish' : 'F7CA05F0-51E8-437D-9409-052149229F5C',
     'non-veggie' : 'E4734F44-C5CD-413A-856A-91641347C92A',
     'veggie' : '94EE8A44-8ACF-4BB5-A65E-A51A17B53213'
   },
   '2/6' : {
     'fish' : '289AF3D3-1EA2-4ABF-9A69-603E5FB9D0B3',
     'non-veggie' : '04F7E70E-5742-4B42-8611-E6C965C7808F',
     'veggie' : 'A5947A25-CF39-4D66-A4C4-7C2C59028438'
   },
   '2/7' : {
     'fish' : '23F5341E-07ED-44B6-B0AB-59B0363BC3E2',
     'non-veggie' : '70CD6E65-DA58-40AF-9538-FB59B97B0D53',
     'veggie' : '2E7A306D-175D-4096-A7BA-9B45F3428303'
   }

 },

 addFormField: function(form, field, value) {
   var tag = '<input type="hidden" name="<name>" value="<value>">';
   tag = tag.replace("<name>", field).replace("<value>", value);
   $("form[name=" + form + "]").append(tag);
 },

 addSeats: function(id, value) {
   var fieldName = "AudienceViewSession::Performances::" + id;
   this.addFormField("get-seats", fieldName, value);
 },

 addMeal: function(id, qty) {
   var fieldName = "BOparam::WSorder::manageItems::addItemID::" + id;
   this.addFormField("get-meals", fieldName, qty);
 },

 submitForm: function() {

   // hide the button
   $("#submit-button").hide();

   // start the process: 
   // step 1: clear the shopping cart
   $("#status").text("Starting order...");
   $("iframe[name=clear]")
     .attr('src', "https://tickets.americanrepertorytheater.org/Online/newOrder.asp")
     .load(function() { tickets.initCart() });
   return false;
 },

 initCart: function() {
   // step 2: initialize the bundle
   $("iframe[name=init]")
     .attr('src',	"https://tickets.americanrepertorytheater.org/Online/bundleSelect.asp?doWork::WSbundleadmissions::load=Load&WSbundleadmissions::admission::bundle_id=C50A59F6-E9AA-4A72-9E82-7B0A315ECBB2")
     .load(function() { tickets.getSeats() });
 },

 getMeals: function() {

   // step 3: request the meals

   // only do this for marathons
   var marathon = $("input[name=parts]:checked").val() == "marathon";
   if (!marathon) {
     this.goToCart();
     return;
   }

   // are there any meals requested?
   var any = false;
   var marathonDate = $("input[name=marathon-date]:checked").val();
   var meals = this.meals[marathonDate];
   for (var mealType in meals) {
     id = meals[mealType];
     qty = $("select[name=" + mealType + "]").val();
     if (qty > 0) {
       this.addMeal(id, qty);
       any = true;
     }
   }
   
   // bail if no meals requested
   if (!any) {
     this.goToCart();
     return;
   }

   // otherwise, submit the meals form
   $("#status").text("Requesting meals...");
   $("form[name=get-meals]").submit();
   $("iframe[name=cart]").load(function() { tickets.goToCart() });

 },

 getSeats: function() {

   // step 4: request the seats
  
   $("#status").text("Requesting seats...");

   // get dates from calendar input
   var part1Date = $("input[name=part1-date]:checked").val();
   var part2Date = $("input[name=part2-date]:checked").val();
   var marathonDate = $("input[name=marathon-date]:checked").val();

   // get parts requested
   var parts = $("input[name=parts]:checked").val();

   // use marathon date if marathon requested
   if (parts == "marathon") {
		 part1Date = marathonDate;
		 part2Date = marathonDate;
   }

// 
//    // add the get seats fields
//    if (parts != "part2-only") {
//      this.addSeats(this.part1['id'], this.part1[part1Date]);
//    }
//    if (parts != "part1-only") {
//      this.addSeats(this.part2['id'], this.part2[part2Date]);
//    }

	// Above modified by Tim Kinnel, since part1-only and part2-only variables
	// are not used	
	if (this.part1[part1Date] != undefined) {
		this.addSeats(this.part1['id'], this.part1[part1Date]);
	}
	if (this.part2[part2Date] != undefined) {
		this.addSeats(this.part2['id'], this.part2[part2Date]);
	}


   // add the number of tickets
   numTickets = $("select[name=num-tickets]").val();
   fieldName = "reqNum::F398AD7C-EC0C-4267-9845-621EE31A6853";
   this.addFormField("get-seats", fieldName, numTickets);
   fieldName += "::select";
   this.addFormField("get-seats", fieldName, numTickets);
   
   // add the price zone
   var zone = $("select[name=price-zone]").val();
   this.addFormField("get-seats", "BundlePriceZone", zone);
   

   // submit the get-seats form
   $("form[name=get-seats]").submit();
   $("iframe[name=seats]").load(function() { tickets.getMeals() });

   return false;
 },

 goToCart: function() {
   // clear the iframes so they don't reload when the back button is pressed
   $("iframe")
     .unbind('load')
     .load(function() { tickets.goToCartWhenAllFramesHaveCleared() } )
	 .attr('src',"blank.html");
 },

 goToCartWhenAllFramesHaveCleared: function() {
   if (this.framesCleared < 4) this.framesCleared++;
   else location.href = "https://tickets.americanrepertorytheater.org/Online/shoppingCart.asp";
 },

 showHide: function() {
   var parts = $("input[name=parts]:checked").val();

   if (parts == "marathon") {
     $(".marathon-only").show();
     $(".not-marathon").hide();
   } else {
     $(".marathon-only").hide();
     $(".not-marathon").show();
   }
   
   if (parts == "part2-only") {
     $(".part1-calendar").hide();
   } else {
     $(".part1-calendar").show();
   }

   if (parts == "part1-only") {
     $(".part2-calendar").hide();
   } else {
     $(".part2-calendar").show();
   }
 }

};


// initialization
$(document).ready(function() {

	
	meals.showHide();

    tickets.showHide();

    // set onchange handler on the "how do you want to see it?" control
    $("input[name=parts]").change(function() { tickets.showHide(); });


    // set onchange handler for the meals
    $("input[name=marathon-date]").change(function() { meals.showHide(); });

    // set the submit handler on the ui form
    $("form[name=ui]").submit(function() { 
        tickets.submitForm();
        return false;
      });
  });

