$(document).ready(function () {
	
	// set default date to 2 days from today
	
	var currentDate = new Date()
	var day = currentDate.getDay()
	
    var dateNeededInput = $("span.product_options_name:contains('Delivery Date')").parent().next().children().eq(0);
    var dateNeededInputLabel = $("span.product_options_name:contains('Delivery Date')").parent("td");
	var saturday_surcharge_select = $("span.product_options_name:contains('Delivery Surcharge')").parent().next().children("select").eq(0);
	$("span.product_options_name:contains('Delivery Surcharge')").parent().parent().hide();
	var the_product_form = $("form[name^='add2cart_form']");
	
	// variable to store the min num of business days before delivery can be scheduled
	var x_business_days = 2;
	
	if (day == 0) {
		// if Sunday
		x_business_days = 2;
		
	} else if(day == 1){
		// if Monday
		x_business_days = 2;
		
	} else if(day == 2){
		// if Tuesday
		x_business_days = 2;
		
	} else if(day == 3){
		// if Wed
		x_business_days = 2;
		
	} else if(day == 4){
		// if Thurs
		x_business_days = 2;
		
	} else if(day == 5){
		// if Fri
		x_business_days = 4;
		
	} else if(day == 6){
		// if Sat
		x_business_days = 3;
	}
	
	// start the JQuery UI Date Picker
	
	$(dateNeededInput).datepicker({
		dateFormat : 'DD mm/dd/yy',
		// set the soonest available delivery date (in days from today)
		minDate: x_business_days,
		// set the latest available delivery date  (in days from today)
		maxDate: 180,
		changeMonth: true,
		changeYear: true,
		showOn: 'button',
		buttonImage: 'images_templ/fr_date_pick_btn.jpg',
		buttonImageOnly: true,
		beforeShowDay: fr_date_filter, // don't allow Sun, Mon and charge $15.00 for specific dates
		onSelect: fr_surcharge
	});
	
	function add_saturday_surcharge() {
		// alert("There is a $15.00 Surcharge for Saturday Delivery");
		// if the checkbox is not checked
		//alert("add sat - saturday checked: " + saturday_surcharge_select.attr("checked"));
		if(saturday_surcharge_select.attr("selectedIndex") != 1){
			$(".surcharge_message").show();
			saturday_surcharge_select.attr("selectedIndex","1");
			saturday_surcharge_select.trigger('change');
			
			//recalculate_product_price_total_2(the_product_form, '');option_toggle(the_product_form);
		} // if
	} // function
	
	function remove_saturday_surcharge() {
		//alert("remove sat - saturday checked: " + saturday_surcharge_select.attr("checked"));
		if(saturday_surcharge_select.attr("selectedIndex") == 1){
			$(".surcharge_message").hide();
			saturday_surcharge_select.attr("selectedIndex","0");
			saturday_surcharge_select.trigger('change');
			//recalculate_product_price_total_2(the_product_form, '');option_toggle(the_product_form);
		} // if
	} // function
	
	// set the date on page load to be x_business_days from today
	$(dateNeededInput).datepicker( "setDate" , x_business_days );
	
	// by default saturday_surcharge_select should be false
	// alert("selected index: " + saturday_surcharge_select.attr("selectedIndex") + "day: " + day);
	// if (saturday_surcharge_select.attr("selectedIndex") == 0 && day == 4){
	 if (day == 4){
		$(".surcharge_message").show();
		saturday_surcharge_select.attr("selectedIndex","1");
		saturday_surcharge_select.trigger('change');
	} else {
		$(".surcharge_message").hide();
		saturday_surcharge_select.attr("selectedIndex","0");
		saturday_surcharge_select.trigger('change');
	}

	
	// if today is Thrus, add surcharge because default date will be Sat
	//if(day == 4){
	//	add_saturday_surcharge();
	//}
	
	function fr_date_filter(date) {
		// do not allow Sunday and Monday to be selected
		if (date.getDay() == 0 || date.getDay() == 1) {
	        return [false, 'fr_no_delivery'];
	      }
		// if the customer chooses a Saturday delivery, then charge $15 more
		if (date.getDay() == 6) {
			return [true, 'fr_sat_delivery','A $15 Surcharge is Added For Saturday Delivery'];
	      }
	  return [true, ''];
	} // function
	
	function fr_surcharge(dateText) {
		dateText_arr = dateText.split(" ");
		day_of_week = dateText_arr[0];
		if(day_of_week == "Saturday"){
			add_saturday_surcharge();
		} else {
			remove_saturday_surcharge();
		}
	}
   
	
	// remove border from the text input
	$(dateNeededInput).attr("border","0");
    $(dateNeededInputLabel).attr("align","");
	$(dateNeededInputLabel).parent("tr").parents("table").attr("cellspacing","0");
	$(dateNeededInputLabel).parent("tr").prevAll(".product_options_title").remove();
	$(dateNeededInputLabel).parent("tr").after('<tr><td valign="top" colspan="3" class="fr_delivery_details"><p><strong>Delivery Details</strong>: Please select your preferred delivery date.  We suggest you receive your order approximately two or three days before your event. To ensure your order arrives in the best possible condition, we ship all flowers via overnight carrier.  Overnight delivery is available Tuesday, Wednesday, Thursday, Friday and Saturday. (Saturday surcharge applies) If you select a delivery date that directly follows a holiday, it will arrive on the next possible delivery day.</p><p><strong>Special note to Customers</strong>: The $19.99 delivery charges are automatically waived if you purchase three separate products with the same delivery date.  Thank you for ordering from Fresh Petal.</p><p class="surcharge_message" style="display: none;">A $15.00 Surcharge has been added to the price shown below for Saturday Delivery.</p></td></tr>');
	
    // add class to datepicker's parent td
	$(dateNeededInput).parent("td").addClass("date_pic_td");
	 
	// add class to td that wraps the datepicker label
	 $(dateNeededInputLabel).addClass("date_pic_label_td");
	
	// add border to table
	// $(dateNeededInputLabel).parent("tr").parents("table").first().css("border","3px solid #e4ebc2");
	$(dateNeededInputLabel).parent("tr").parents("table").first().css("margin","15px 0 15px 0");

	// add padding to top of box
	// css("padding","10px 0 3px 0")
	$(dateNeededInputLabel).parent("tr").children("td").addClass("date_pick_row_td");
	
	// catch the Saturday
	if (day == 4){
		$(".surcharge_message").show();
	}

}); // ready