var pack_select = null;
var seating = null;
var isstudent = false;
var flexnum = 0;

seriesPrice = new Array();
seriesPrice["preview"] = new Array();
seriesPrice["preview"]["adult"] = new Array();
seriesPrice["preview"]["senior"] = new Array();
seriesPrice["preview"]["student"] = new Array();
seriesPrice["preview"]["adult"]["a"] = 130;
seriesPrice["preview"]["senior"]["a"] = 130;
seriesPrice["preview"]["student"]["a"] = 120;
seriesPrice["preview"]["adult"]["b"] = 130;
seriesPrice["preview"]["senior"]["b"] = 130;
seriesPrice["preview"]["student"]["b"] = 120;

seriesPrice["matinee"] = new Array();
seriesPrice["matinee"]["adult"] = new Array();
seriesPrice["matinee"]["senior"] = new Array();
seriesPrice["matinee"]["student"] = new Array();
seriesPrice["matinee"]["adult"]["a"] = 168;
seriesPrice["matinee"]["senior"]["a"] = 153;
seriesPrice["matinee"]["student"]["a"] = 120;
seriesPrice["matinee"]["adult"]["b"] = 148;
seriesPrice["matinee"]["senior"]["b"] = 133;
seriesPrice["matinee"]["student"]["b"] = 120;

seriesPrice["weekday-evening"] = new Array();
seriesPrice["weekday-evening"]["adult"] = new Array();
seriesPrice["weekday-evening"]["senior"] = new Array();
seriesPrice["weekday-evening"]["student"] = new Array();
seriesPrice["weekday-evening"]["adult"]["a"] = 198;
seriesPrice["weekday-evening"]["senior"]["a"] = 168;
seriesPrice["weekday-evening"]["student"]["a"] = 120;
seriesPrice["weekday-evening"]["adult"]["b"] = 178;
seriesPrice["weekday-evening"]["senior"]["b"] = 148;
seriesPrice["weekday-evening"]["student"]["b"] = 120;

seriesPrice["weekend-evening"] = new Array();
seriesPrice["weekend-evening"]["adult"] = new Array();
seriesPrice["weekend-evening"]["senior"] = new Array();
seriesPrice["weekend-evening"]["student"] = new Array();
seriesPrice["weekend-evening"]["adult"]["a"] = 218;
seriesPrice["weekend-evening"]["senior"]["a"] = 198;
seriesPrice["weekend-evening"]["student"]["a"] = 120;
seriesPrice["weekend-evening"]["adult"]["b"] = 203;
seriesPrice["weekend-evening"]["senior"]["b"] = 183;
seriesPrice["weekend-evening"]["student"]["b"] = 120;

seriesPrice["opening"] = new Array();
seriesPrice["opening"]["adult"] = new Array();
seriesPrice["opening"]["senior"] = new Array();
seriesPrice["opening"]["student"] = new Array();
seriesPrice["opening"]["adult"]["a"] = 218;
seriesPrice["opening"]["senior"]["a"] = 218;
seriesPrice["opening"]["student"]["a"] = 218;
seriesPrice["opening"]["adult"]["b"] = 218;
seriesPrice["opening"]["senior"]["b"] = 218;
seriesPrice["opening"]["student"]["b"] = 218;




series4PlayPrice = new Array();
series4PlayPrice["preview"] = new Array();
series4PlayPrice["preview"]["adult"] = new Array();
series4PlayPrice["preview"]["senior"] = new Array();
series4PlayPrice["preview"]["student"] = new Array();
series4PlayPrice["preview"]["adult"]["a"] = 104;
series4PlayPrice["preview"]["senior"]["a"] = 104;
series4PlayPrice["preview"]["student"]["a"] = 96;
series4PlayPrice["preview"]["adult"]["b"] = 104;
series4PlayPrice["preview"]["senior"]["b"] = 104;
series4PlayPrice["preview"]["student"]["b"] = 96;

series4PlayPrice["matinee"] = new Array();
series4PlayPrice["matinee"]["adult"] = new Array();
series4PlayPrice["matinee"]["senior"] = new Array();
series4PlayPrice["matinee"]["student"] = new Array();
series4PlayPrice["matinee"]["adult"]["a"] = 135;
series4PlayPrice["matinee"]["senior"]["a"] = 123;
series4PlayPrice["matinee"]["student"]["a"] = 96;
series4PlayPrice["matinee"]["adult"]["b"] = 119;
series4PlayPrice["matinee"]["senior"]["b"] = 111;
series4PlayPrice["matinee"]["student"]["b"] = 96;

series4PlayPrice["weekday-evening"] = new Array();
series4PlayPrice["weekday-evening"]["adult"] = new Array();
series4PlayPrice["weekday-evening"]["senior"] = new Array();
series4PlayPrice["weekday-evening"]["student"] = new Array();
series4PlayPrice["weekday-evening"]["adult"]["a"] = 159;
series4PlayPrice["weekday-evening"]["senior"]["a"] = 135;
series4PlayPrice["weekday-evening"]["student"]["a"] = 96;
series4PlayPrice["weekday-evening"]["adult"]["b"] = 135;
series4PlayPrice["weekday-evening"]["senior"]["b"] = 123;
series4PlayPrice["weekday-evening"]["student"]["b"] = 96;

series4PlayPrice["weekend-evening"] = new Array();
series4PlayPrice["weekend-evening"]["adult"] = new Array();
series4PlayPrice["weekend-evening"]["senior"] = new Array();
series4PlayPrice["weekend-evening"]["student"] = new Array();
series4PlayPrice["weekend-evening"]["adult"]["a"] = 175;
series4PlayPrice["weekend-evening"]["senior"]["a"] = 159;
series4PlayPrice["weekend-evening"]["student"]["a"] = 96;
series4PlayPrice["weekend-evening"]["adult"]["b"] = 163;
series4PlayPrice["weekend-evening"]["senior"]["b"] = 147;
series4PlayPrice["weekend-evening"]["student"]["b"] = 96;

series4PlayPrice["opening"] = new Array();
series4PlayPrice["opening"]["adult"] = new Array();
series4PlayPrice["opening"]["senior"] = new Array();
series4PlayPrice["opening"]["student"] = new Array();
series4PlayPrice["opening"]["adult"]["a"] = 210;
series4PlayPrice["opening"]["senior"]["a"] = 194;
series4PlayPrice["opening"]["student"]["a"] = 96;
series4PlayPrice["opening"]["adult"]["b"] = 178;
series4PlayPrice["opening"]["senior"]["b"] = 162;
series4PlayPrice["opening"]["student"]["b"] = 96;


samplerPrice = new Array();
samplerPrice["preview"] = new Array();
samplerPrice["preview"]["adult"] = new Array();
samplerPrice["preview"]["senior"] = new Array();
samplerPrice["preview"]["adult"]["a"] = 92;
samplerPrice["preview"]["senior"]["a"] = 92;
samplerPrice["preview"]["adult"]["b"] = 92;
samplerPrice["preview"]["senior"]["b"] = 92;

samplerPrice["matinee"] = new Array();
samplerPrice["matinee"]["adult"] = new Array();
samplerPrice["matinee"]["senior"] = new Array();
samplerPrice["matinee"]["adult"]["a"] = 116;
samplerPrice["matinee"]["senior"]["a"] = 116;
samplerPrice["matinee"]["adult"]["b"] = 116;
samplerPrice["matinee"]["senior"]["b"] = 116;

samplerPrice["weekday-evening"] = new Array();
samplerPrice["weekday-evening"]["adult"] = new Array();
samplerPrice["weekday-evening"]["senior"] = new Array();
samplerPrice["weekday-evening"]["adult"]["a"] = 140;
samplerPrice["weekday-evening"]["senior"]["a"] = 140;
samplerPrice["weekday-evening"]["adult"]["b"] = 140;
samplerPrice["weekday-evening"]["senior"]["b"] = 140;

samplerPrice["weekend-evening"] = new Array();
samplerPrice["weekend-evening"]["adult"] = new Array();
samplerPrice["weekend-evening"]["senior"] = new Array();
samplerPrice["weekend-evening"]["adult"]["a"] = 164;
samplerPrice["weekend-evening"]["senior"]["a"] = 149;
samplerPrice["weekend-evening"]["adult"]["b"] = 164;
samplerPrice["weekend-evening"]["senior"]["b"] = 149;


flexPrice = new Array();
flexPrice["4"] = new Array();
flexPrice["4"]["adult"] = 190;
flexPrice["4"]["senior"] = 170;
flexPrice["6"] = new Array();
flexPrice["6"]["adult"] = 285;
flexPrice["6"]["senior"] = 255;

function setPack(){
	pack_select = $('#sPackage').val();

	var t = pack_select.toUpperCase().split("-");
	flexnum = (t[0] == "FLEX") ? t[1] : 0;
   return flexnum;
}

function getSeating(){
	return $('#sSeating').val();
}

function getSeatingCode(cat){
	if(cat == "Zone A"){
		return "a";
	}else if(cat == "Zone B"){
		return "b";
	}else{
		//assume wheelchair is a
		return "a";
	}
}

function getCode(cat){
	if(cat != null && cat != ""){
		var tc = cat.split(',');
		tc = jQuery.trim(tc[0].replace(' ','-')).toLowerCase();
		return tc;
	}else{
		return null;
	}
}

function resetBox(boxname){
	$("input[@name="+boxname+"]").each(function (i) {
					var sel = $(this);
					sel.attr("checked", "")
				});
}

function getBoxValue(boxname){
	var selb = null;
	$("input[@name="+boxname+"]").each(function (i) {
					var sel = $(this);
					if(sel.attr("checked")){

						selb = sel.val();
					}

				});

		return selb;
}

function getQuantities(){

	var arr = new Array();

	arr["adult"] = $('#iQuantityAdult').val() == "" ? 0 : $('#iQuantityAdult').val();
	arr["senior"] = $('#iQuantitySenior').val() == "" ? 0 : $('#iQuantitySenior').val();
	arr["student"] = $('#iQuantityStudent').val() == "" ? 0 : $('#iQuantityStudent').val();

	//alert("adult: "+ arr["adult"] + " - Senior: " + arr["senior"] + " - Student: " + arr["student"]);

	return arr;
}

function checkQuantities(){
	var arr = getQuantities();
	var valid = true;
}

function verifyPackage(thefocus){
	if(sPackage == ""){
		//alert("Please Select a Package");
		//$("#"+thefocus).focus();
		return false;
	}else{
		return true;
	}
}

function verifyRadios(thefocus){
	var valid = false;
   if (sSeries != "") {
   	valid = true;
   } else {
   	if(flexnum <= 0){
   		$("input[@name=sSeries]").each(function (i) {
   					var sel = $(this);
   					if(sel.attr("checked")){
   						valid = true;
   					}
   				});
   	}else{
   		valid = true;
   	}
   }
	return valid;
}

function verifyRadiosGlobal(thefocus){
	var valid = false;
   $("input[@name="+thefocus+"]").each(function (i) {
      var sel = $(this);
      if(sel.attr("checked")){
         valid = true;
      }
   });
	return valid;
}

//index-1-5-pick
function check3(obj) {
   var count = $("input[@name='aShows[]']:checked").size();
   if (!obj.checked) {
      if(count >= 3){
         alert("You can only choose 3 plays.");
         obj.blur();
         return false;
      } else {
         return true;
      }
   }
}
function max3(obj) {
   var count = $("input[@name='aShows[]']:checked").size();
   var series = getBoxValue('sSeries');
   if(count > 3){
      alert("You can only choose 3 plays.");
      obj.checked = "";
   } else if (count == 3 && series != null) {
      //showdates
      updateDates();
   }
}
function showDates() {
   var count = $("input[@name='aShows[]']:checked").size();
   var series = getBoxValue('sSeries');
   if(count == 3){
      //showdates
      updateDates();
   } else {
      alert("Please select your 3 shows above.");
   }
}
function updateDates() {
   //hide shows
   $("input[@name='aShows[]']").each(function (i) {
      var sel = $(this);
      switch (sel.val()) {
         case "The Miracle Worker":
            theclass="show1";
            break;
         case "Dirty Rotten Scoundrels":
            theclass="show2";
            break;
         case "Beyond Eden":
            theclass="show3";
            break;
         case "The Love List":
            theclass="show4";
            break;
         case "Dangerous Corner":
            theclass="show5";
            break;
      }
      if(sel.attr("checked")){
         //show
         $('.'+theclass).show();
         $('.'+theclass).attr("width",90);
      } else {
         //hide
         $('.'+theclass).hide();
      }
   });
   //reset radios
   resetDateBoxes();
   //show radios
   $('.dateradio').show();
   //adjust colspan
   $('.colspan').attr("colspan",4);
   //hide series
   var series = getBoxValue('sSeries');
   switch(series) {
      case "Preview":
         $('.series-preview').show();
         $('.series-matinee').hide();
         $('.series-midweek').hide();
         $('.series-weekend').hide();
         break;
      case "Matinee":
         $('.series-preview').hide();
         $('.series-matinee').show();
         $('.series-midweek').hide();
         $('.series-weekend').hide();
         break;
      case "Midweek":
         $('.series-preview').hide();
         $('.series-matinee').hide();
         $('.series-midweek').show();
         $('.series-weekend').hide();
         break;
      case "weekend-evening":
         $('.series-preview').hide();
         $('.series-matinee').hide();
         $('.series-midweek').hide();
         $('.series-weekend').show();
         break;
   }
}
function resetDateBoxes() {
   resetBox("show1date");
   resetBox("show2date");
   resetBox("show3date");
   resetBox("show4date");
   resetBox("show5date");
}
function verifyDates() {
   var ret = true;
   $("input[@name='aShows[]']:checked").each(function (i) {
      var sel = $(this);
      switch (sel.val()) {
         case "The Miracle Worker":
            radio="show1date";
            break;
         case "Dirty Rotten Scoundrels":
            radio="show2date";
            break;
         case "Beyond Eden":
            radio="show3date";
            break;
         case "The Love List":
            radio="show4date";
            break;
         case "Dangerous Corner":
            radio="show5date";
            break;
      }
      ret = verifyRadiosGlobal(radio);
   })
   return ret;
}
//index-1-5-pick

function verifyQty(){
	var arr = getQuantities();
	//alert("adult: "+ arr["adult"] + " - Senior: " + arr["senior"] + " - Student: " + arr["student"]);
	if(arr["adult"] <= 0 && arr["senior"] <= 0 && arr["student"] <= 0)
		return false;
	else
		return true;

}

function verifySeating(){
   	if(getSeating() == "")
   		return false;
   	else
   		return true;
}

function allFieldsValid(showmess){

	if(showmess === undefined)
		showmess = false;

	var err = "Error: \n";
	var valid = true;

	if(!verifyPackage("sPackage")){
		err += "- Please select a Package\n";
		valid = false;
	}

	if(!verifyRadios("first-series")){
		err += "- Please select a show Series\n";
		valid = false;
	}

	if(!verifyQty()){
		err += "- Please the number of subscribers\n";
		valid = false;
	}

	if(flexnum <= 0){
		if(!verifySeating()){
			err += "- Please choose your seating\n";
			valid = false;
		}
	}

	if(showmess && !valid)
		alert(err);

	return valid;
}

function updateTotals(obj) {
	//
}

function showTotal3(){
	getTotalText();
}

function getTotalText(){
	$('#sTotal').val("$" + formatCurrency(calcTotal3()));
   $('#items-total').empty();
   $('#items-total').append("TOTAL: "+$('#sTotal').val()+" (all-inclusive)<br />");
}

function calcTotal3(){
	var arr = getQuantities();
	var bonus = getCode(sBonus);
	var donation = $('#sDonation').val() == "" ? 0 : parseFloat($('#sDonation').val());
	var total = 0;
	var totaladult = parseInt(arr["adult"]) + parseInt(arr["senior"]);
	var totalstudent = parseInt(arr["student"]) != "" ? parseInt(arr["student"]) : 0;

	var bonusprice;
	if(bonus == null)
		bonusprice = 0;
	else
		bonusprice = (bonus == "preview") ? 20 : 30;

	if(flexnum > 0){
		//calc flex package stuff
		total += flexPrice[flexnum]["adult"] * arr["adult"];
		total += flexPrice[flexnum]["senior"] * arr["senior"];

	}else if (sPackage == "5-Play") {
		//calc 5-play package stuff
		var series = getCode(sSeries);
		var seat = getSeatingCode(getSeating());

		total += seriesPrice[series]["adult"][seat] * arr["adult"];
		if (series != "opening") {
			total += seriesPrice[series]["senior"][seat] * arr["senior"];
			total += seriesPrice[series]["student"][seat] * arr["student"];
		} else {
			//if opening series add in 1000
			donation = donation < 1000 ? 1000 : donation;
		}

	}else if (sPackage == "4-Play") {
		//calc 4-play package stuff
		var series = getCode(sSeries);
		var seat = getSeatingCode(getSeating());

		total += series4PlayPrice[series]["adult"][seat] * arr["adult"];
		total += series4PlayPrice[series]["senior"][seat] * arr["senior"];
		total += series4PlayPrice[series]["student"][seat] * arr["student"];
	}else if (sPackage == "Pick-3") {
		//calc pick-3 package stuff
		var series = getCode(sSeries);
		var seat = getSeatingCode(getSeating());

		total += samplerPrice[series]["adult"][seat] * arr["adult"];
		total += samplerPrice[series]["senior"][seat] * arr["senior"];
	}
	if (bonusprice > 0) {
		total += bonusprice * totaladult;
	}
	//total += 20 * totalstudent;

	if(!isNaN(donation)){
		if(donation > 0)
			total += donation;
	}

   //alert(total);
	return total;
}

function formatCurrency(num) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num))
    num = "0";
   sign = (num == (num = Math.abs(num)));
   num = Math.floor(num*100+0.50000000001);
   cents = num%100;
   num = Math.floor(num/100).toString();
   if(cents<10)
     cents = "0" + cents;
   for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
     num = num.substring(0,num.length-(4*i+3))+''+
           num.substring(num.length-(4*i+3));
   return (((sign)?'':'-') + num + '.' + cents);
   //return (((sign)?'':'-') + num);
}
function validate(obj) {
	if (obj.sFirstName.value == "") {
		alert("Please Input Your First Name.");
		obj.sFirstName.focus();
		return false;
	} else if (obj.sLastName.value == "") {
		alert("Please Input Your Last Name.");
		obj.sLastName.focus();
		return false;
	} else if (obj.sEmail.value == "" || obj.sEmail.value.indexOf('@') == -1 || obj.sEmail.value.indexOf('.') == -1) {
     alert("Please Input Valid Email Address.");
     obj.sEmail.focus();
     return false;
	} else if (obj.sAddress.value == "") {
		alert("Please Input Your Address.");
		obj.sAddress.focus();
		return false;
	} else if (obj.sCity.value == "") {
		alert("Please Input Your City.");
		obj.sCity.focus();
		return false;
	} else if (obj.sProvince.value == "") {
		alert("Please Input Your sProvince.");
		obj.sProvince.focus();
		return false;
	} else if (obj.sPostal.value == "") {
		alert("Please Input Your Postal Code.");
		obj.sPostal.focus();
		return false;
	} else if (obj.sPhone.value == "") {
		alert("Please Input Your Phone Number.");
		obj.sPhone.focus();
		return false;
	} else {
      return true;
	}
}