	function openFaceboxWithReplacements(divId, replaceClassIdNameString) {
		var elementObj = $('#'+divId);
		var textToPopup = "";
		if (elementObj.length > 0) {
			textToPopup = elementObj[0].innerHTML;
		}
		// now do the replacement
		var reId = RegExp('id="'+replaceClassIdNameString, 'g');
		textToPopup = textToPopup.replace(reId, 'id="');
		var reClass = RegExp('class="'+replaceClassIdNameString, 'g');
		textToPopup = textToPopup.replace(reClass, 'class="');
		var reName = RegExp('name="'+replaceClassIdNameString, 'g');
		textToPopup = textToPopup.replace(reName, 'name="');
		// IE next (doesn't copy quotes? wth is that???
		var reId = RegExp('id='+replaceClassIdNameString, 'g');
		textToPopup = textToPopup.replace(reId, 'id=');
		var reClass = RegExp('class='+replaceClassIdNameString, 'g');
		textToPopup = textToPopup.replace(reClass, 'class=');
		var reName = RegExp('name='+replaceClassIdNameString, 'g');
		textToPopup = textToPopup.replace(reName, 'name=');
		// then use that!
//		alert(textToPopup);
		jQuery.facebox(textToPopup);
		$(document).bind('close.facebox', function() {
//			holdWindowOpenWide.show("fast");
//			alert("You're closing it! OMG!");
			$(".buttons").show("fast");
			$(".errorBox").show("fast");
			$(".quitFormContainer").show("fast");
			$(".quitFormConfirmation").hide("fast");
		});
		hideAndShowHiddenQuestions();
		BEXTracker.CalendarTracker_ShowQuitForm();
	}

	// opens questions 1a and 2a based on answers to 1 and 2
	function hideAndShowHiddenQuestions() {
		var triedToQuitBeforeVal = $('input[name=triedToQuitBefore]:checked').val();
		if (triedToQuitBeforeVal == "Y") {
			$(".howManyTimes").show('fast');
		} else {
			$(".howManyTimes").hide('fast');
		}
		var didUseMedicationVal = $('input[name=didUseMedication]:checked').val();
		if (didUseMedicationVal == "Y") {
			$(".whichMedication").show('fast');
		} else {
			$(".whichMedication").hide('fast');
		}
	}

	function goBackToCalendarInTracking(response,userData) {
		jQuery(document).trigger('close.facebox');
		if (document.calendarHelper.setNavAbilityDayWeek) {
			document.calendarHelper.setNavAbilityDayWeek(true);
		}
		
		var quitDate = userData;
		if (quitDate != undefined) {
			// new quit date has been set
			BEXTracker.trackOmnitureEvent('event13');
			var u = new util();			
			document.calendarHelper.monthlyCalendar.quitDate = new Date(u.makeDateFromAPI(quitDate));
		}
		document.calendarHelper.monthlyCalendar.setURLsForCells();	
	}
  
	function setRecommendedDateClick() {
		BEXTracker.CalendarTracker_SetRecommendedQuitDate();
		BEXAPI.SetQuitDate($("#recommendedQuitDate").val(), true, function(response,userData){goBackToCalendarInTracking(response,userData)}, $("#recommendedQuitDate").val());
	}

	function setPickedDateClick() {
		BEXTracker.CalendarTracker_SetSelectedQuitDate();
		BEXAPI.SetQuitDate($("#selectedQuitDate").val(), false, function(response,userData){goBackToCalendarInTracking(response,userData)}, $("#selectedQuitDate").val());
	}


	// @param recommendedDate [YYYYMMDD]
	function setRecommendedDate(recommendedDate) {
		// stick it in the form
		var dateObj = new Date(recommendedDate.substring(0,4), recommendedDate.substring(4,6) - 1, recommendedDate.substring(6,8));
		$("#quitForm").recommendedQuitDate = dateObj;
		$("#recommendedQuitDate").val(recommendedDate);
		// also insert visually
		visualDateSet(dateObj, "recommendedDatePlaceholder");
	}

	// calendar should invoke this function
	function calendarSetPickedDate(dateObj) {
		setPickedDate(dateObj);
		// make sure the form is properly set
		$(".buttons").show("fast");
		$(".errorBox").show("fast");
		$(".quitFormContainer").show("fast");
		$(".quitFormConfirmation").hide("fast");
		// then show the entire form in a facebox
		openFaceboxWithReplacements("overlayQuitform", "BOXREMOVE");
	}

	function setPickedDate(dateObj) {
		// stick it where it belongs
		$("#quitForm").selectedQuitDate = dateObj;
		var mon = dateObj.getMonth()+1;
		if (mon < 10) {
			mon = "0"+mon;
		}
		var day = dateObj.getDate();
		if (day < 10) {
			day = "0"+day;
		}
		var dateString = dateObj.getFullYear() + "" + mon + "" + day;
		$("#BOXREMOVEselectedQuitDate").val(dateString);
		// also insert visually
		visualDateSet(dateObj, "desiredDatePlaceholder");
	}

	function visualDateSet(dateObj, spanClassName) {
		var languageCodedDate = getTranslatedDate(dateObj);
		$("."+spanClassName).each(function(index) {
			$(this).text(languageCodedDate);
		});
	}
	
	function submitQuitForm() {
		BEXAPI.WriteQuitForm( function(response,userData){submitQuitFormCallback(response,userData)} );
	}

	function submitQuitFormCallback(response, userData) {
		if (response.IsSuccess) {
			if (response.ResponseData.IsQuitDateTooEarly) {
				BEXTracker.CalendarTracker_SubmitQuitFormTooEarly();
				// too Early?
				setRecommendedDate(response.ResponseData.RecommendedDate);
				//$(".buttons").hide(); // why was this failing?
				$(".buttons").css('display','none');
				$(".errorBox").hide("fast");
				$(".quitFormContainer").hide("fast");
				$(".quitFormConfirmation").show("fast");
				if (response.ResponseData.ReasonTooSoon) {
					$("#reasonTooSoon").show("fast");
				} else {
					$("#reasonTooSoon").hide("fast");
				}
				if (response.ResponseData.ReasonHaveMajorEvent) {
					$("#reasonMajorEvent").show("fast");
				} else {
					$("#reasonMajorEvent").hide("fast");
				}
				if (!response.ResponseData.ReasonHaveStartedTracking) {
					$("#reasonNotStarted").show("fast");
				} else {
					$("#reasonNotStarted").hide("fast");
				}
			} else { // not too early, just set it
				BEXTracker.CalendarTracker_SubmitQuitForm();
				BEXAPI.SetQuitDate($("#selectedQuitDate").val(), true, function(response,userData){goBackToCalendarInTracking(response,userData)},$("#selectedQuitDate").val());
				jQuery.facebox.close();
			}
		} else {
			BEXTracker.CalendarTracker_FailQuitForm();
			// failure, undo hides?
			$(".errorBox").show("fast");
			markErrorFields(response.ResponseData.errorFields);
		}
	}

	function markErrorFields(errorFieldArr) {
		var errorFieldStr = errorFieldArr.join(",");
		var allFields = $(".formField").each(function(index) {
			var fieldId = $(this).attr("class").split(" ")[0];
			if (errorFieldStr.indexOf(fieldId) >= 0) {
				// this is an error field, mark as such
				$(this).addClass("error"); 
			} else {
				// no error on this field, remove error class if present
				$(this).removeClass("error");
			}
		});
	}
	
	function cancelQuitForm() {
		BEXTracker.CalendarTracker_CancelQuitForm();
		jQuery(document).trigger('close.facebox');
	}

	function turnErrorOn(classId) {
		if (!$("."+classId).hasClass("error")) {
			$("."+classId).addClass("error"); 
		}
	}

	function turnErrorOff(classId) {
		if ($("."+classId).hasClass("error")) {
			$("."+classId).removeClass("error"); 
		}
	}

	function getTranslatedDate(dateObj) {
		return(document.languageTranslations[dateObj.getMonth() + 1] + " " + dateObj.getDate());
	}
	// onload...
	$(document).ready( function(){
		hideAndShowHiddenQuestions();
	});
