var estiHandler = function() {
	// private			
	var ef = null; // the estimation form
	var efelem = null;
	var customOpt = null;
	var that = {
		// public
		init : function(customOpter) {
			customOpt = customOpter;
			ef = document["customQuoteForm"];
			efelem = ef.elements;
			// chooses single or multi mode
			that.setMode();
			that.setNoteControl();
			that.setNoteBehave();
			that.setPagesControl();
			that.setPaperControl();
			that.setFoldVisual();
			that.setDrillEyeletInteraction();
			that.setShrinkControl();
			that.setMailControl();
			that.setEnvControl();
			that.setQuantityControl();
		},
		finalize : function() {
			$(".noteField").focus(); // wipe out default text
			$(efelem).removeAttr("disabled"); // re-enabled					
			var missing = "";
			if (efelem.phone.value.length < 10) {
				missing = (missing + "\nphone number");
			}
			if (efelem.jobname.value.length < 1) {
				missing = (missing + "\njobname");
			}

			if (missing.length >= 1) {
				alert("The following fields are missing or too short:\n"
						+ missing);
			} else {
				ef.submit();
			}
		},
		setQuantityControl : function() {
			var quantField = $(efelem["quantity"]);
			quantField.keypress(function (e) {
				this.value = this.value.replace(/\D/, '');
			});
		},
		setPagesControl : function() {
			var showPagesBlurb = function(val, bindVal, container) {
				var valNormal = isNaN(val) ? 0 : +val,
					blurb;
				if (valNormal < 2 || valNormal % 2 !== 0) {
					blurb = "(invalid page count)";
				} else {
					if (bindVal === "saddle") {
						if (valNormal % 4 !== 0) {
							blurb = "(invalid, needs to be multiple of 4)";
						} else {
							blurb = " Inside Pages + Cover = " + (valNormal + 4) + " Pages";
						}
					} else {
						blurb = " Inside Pages + Cover = " + (valNormal + 4) + " Pages";
					}
				}
				container.html(blurb);
			}
			var pagesBlurb = $("#pagesBlurb");
			if (pagesBlurb) {
				var pagesEl = efelem["pages"];
				var bindEl = efelem["bind"];
				pagesEl.onkeyup = function() {
					showPagesBlurb(this.value, bindEl.value, pagesBlurb);
				};
				bindEl.onchange = function() {
					showPagesBlurb(pagesEl.value, this.value, pagesBlurb);
				};
				bindEl.onchange();
			}
		},
		setDrillEyeletInteraction : function() {
			var drillJQ = $(efelem["drill"]);
			var eyeletJQ = $(efelem["eyelet"]);
			var drillLoc = $("#drillLoc");
			drillJQ.change(function() {
				if (this.value === customOpt || this.value === "no") {
					drillLoc.hide();
				} else {
					drillLoc.show();
				}
			}).change();
			eyeletJQ.change(function() {
				if (this.value === customOpt) {
					drillJQ.val(customOpt);
					drillJQ.attr("disabled", "disabled");
					drillJQ.change();
				} else if (this.value !== "no") {
					drillJQ.val("cal_18");
					drillJQ.attr("disabled", "disabled");
					drillJQ.change();
				} else {
					drillJQ.removeAttr("disabled");
				}
			}).change();
		},
		setFoldVisual : function() {
			var foldVisuals = [
					'',
					'<img name="bind_visimage" src="/grp/images/fold_half.gif" alt="visual aid image" border="0">',
					'<img name="bind_visimage" src="/grp/images/fold_tri.gif" alt="visual aid image" border="0">',
					'<img name="bind_visimage" src="/grp/images/fold_z.gif" alt="visual aid image" border="0">',
					'<img name="bind_visimage" src="/grp/images/fold_accordian.gif" alt="visual aid image" border="0">',
					'<img name="bind_visimage" src="/grp/images/fold_roll.gif" alt="visual aid image" border="0">',
					'<img name="bind_visimage" src="/grp/images/fold_gate.gif" alt="visual aid image" border="0">',
					'<img name="bind_visimage" src="/grp/images/fold_doublegate.gif" alt="visual aid image" border="0">',
					'<img name="bind_visimage" src="/grp/images/fold_dblparallel.gif" alt="visual aid image" border="0">',
					'<img name="bind_visimage" src="/grp/images/fold_sig.gif" alt="visual aid image" border="0">',
					'' ];
			efelem["fold"].onchange = function() {
				$("#foldVisual").html(foldVisuals[this.selectedIndex]);
			};
		},
		setNoteControl : function() {
			$(".hasNoteField").each(function() {
				$(this).change(function(e) {
					var fieldValue = this.value;
					var noteField = efelem[this.name + "Notes"];
					if (noteField) {
						var noteShow = $(noteField).closest("tr");
						if (fieldValue === customOpt) {
							noteShow.show();
						} else {
							noteShow.hide();
						}
					}
				}).change();
			});
		},
		setNoteBehave : function() {
			$(".noteField").each(function() {
				var firstWipe = true;
				$(this).focus(function(e) {
					if (firstWipe) {
						this.value = "";
						this.style.fontStyle = "normal";
						this.style.color = "#000000";
						firstWipe = false;
					}
				});
			});
		},
		setPaperControl : function() {
			var paperJQ = $(efelem["paper"]);
			var coverPaperJQ = $(efelem["coverPaper"]);
		},
		setShrinkControl : function() {
			var shrinkJQ = $(efelem["shrink"]);
			var shrinkExp = $("#shrinkExpand");
			shrinkJQ.change(function() {
				if (this.value !== "no") {
					shrinkExp.show();
				} else {
					shrinkExp.hide();
				}
			}).change();
		},
		setMailControl : function() {
			var mailJQ = $(efelem["mailing"]);
			var mailExp = $(".mailExpand");
			mailJQ.change(function() {
				if (this.value !== "no") {
					mailExp.show();
				} else {
					mailExp.hide();
				}
			}).change();
		},
		setEnvControl : function() {
			var envJQ = $(efelem["envelopes"]);
			var envExp = $(".envExpand");
			var envColJQ = $(efelem["stationeryEnvColors"]);
			var envExpNone = $(".envExpandNone");
			envColJQ.change(function() {
				if (this.value === "") {
					envExpNone.hide();
				} else {
					envExpNone.show();
				}
			}).change();
			envJQ.change(function() {
				if (this.value !== "no") {
					envExp.show();
					envExp.find(".hasNoteField").change();
				} else {
					envExp.hide();
				}
			}).change();
		},
		setMode : function() {
			var multi = efelem["multiPage"];
			var multiRows = $("tr.multiRow");
			var folderRows = $("tr.folderRow");

			// this function controls the triggering behavior
			var projectTrigger = function(projectType) {
				switch (projectType) {
				case "multi":
					multi.value = "true";
					multiRows.show();
					multiRows.find(":input.hasNoteField").change();
					folderRows.hide();
					break;
				case "folder":
					multi.value = "false";
					multiRows.hide();
					folderRows.show();
					folderRows.find(":input.hasNoteField").change();
					break;
				default:
					multi.value = "false";
					multiRows.hide();
					folderRows.hide();
					break;
				}
			};
			// now add handler to the trigger
			$(efelem["projectType"]).change(function() {
				projectTrigger(this.value);
			}).change();
		}
	};
	return that;
}();
