/*	-------------------------------------------------
	CONSTANTS/LANGUAGE STRINGS
	-------------------------------------------------	*/
	var STR_STANDARD_ERROR			= "Some errors were found while attempting to submit this form.\n\n";	

/*	-------------------------------------------------
	FUNCTIONS
	-------------------------------------------------	*/
	function saveProperty(strMLSID) {
		jQuery.facebox(function() { 
			jQuery.post(	"/search/_save-property.php",
							{
								id: strMLSID
							}, 
							function(txtData) {
								jQuery.facebox(txtData)
							})
		});
	}

	function focusLoginForm() {
		$.facebox.close();
		window.scrollTo(0, 0);
	}

	function hidePopup() {
		jQuery.facebox.close();
	}

	function processHeaderLogin() {
		var boolErrorFound			= false;
		var strErrorText			= "";
		
		if (($("#strHeaderEmail").val().length == 0) || ($("#strHeaderEmail").val() == STR_HEADER_LOGIN_USERNAME)) {
			boolErrorFound			= true;
			strErrorText			+= "- Please enter your e-mail address.\n";
		} else if (($("#strHeaderEmail").val().indexOf("@") == -1) || ($("#strHeaderEmail").val().indexOf(".") == -1)) {
			boolErrorFound			= true;
			strErrorText			+= "- Please enter a valid e-mail address.\n";
		}
		if ($("#strHeaderPassword").val().length < 6) {
			boolErrorFound			= true;
			strErrorText			+= "- Please enter your password.\n";
		}
		
		if (boolErrorFound) {
			alert(STR_STANDARD_ERROR + strErrorText);
			return false;
		} else {
			return true;
		}
	}
	
	function processMinimalHeaderLogin() {
		var boolErrorFound			= false;
		var strErrorText			= "";
		
		if (($("#strHeaderEmail").val().length == 0) || ($("#strHeaderEmail").val() == STR_HEADER_LOGIN_USERNAME)) {
			boolErrorFound			= true;
			strErrorText			+= "- Please enter your e-mail address.\n";
		} else if (($("#strHeaderEmail").val().indexOf("@") == -1) || ($("#strHeaderEmail").val().indexOf(".") == -1)) {
			boolErrorFound			= true;
			strErrorText			+= "- Please enter a valid e-mail address.\n";
		}
		if ($("#strHeaderPassword").val().length < 6) {
			boolErrorFound			= true;
			strErrorText			+= "- Please enter your password.\n";
		}
		
		if (boolErrorFound) {
			alert(STR_STANDARD_ERROR + strErrorText);
			return false;
		} else {
			if (window.opener) {
				if (window.opener.document.forms.frmHeaderLogin) {
					window.opener.document.forms.frmHeaderLogin.strHeaderEmail.value	= $("#strHeaderEmail").val();
					window.opener.document.forms.frmHeaderLogin.strHeaderPassword.value	= $("#strHeaderPassword").val();
					window.opener.document.forms.frmHeaderLogin.submit();
					window.opener.focus();
					window.close();
					
					return false;
				} else {
					return true;
				}
			} else {
				return true;
			}
		}
	}
	
	function processMinimalSearch() {
		if (($("#strSearchQuery").val() != STR_HEADER_SEARCH_DEFAULT) && ($("#strSearchQuery").val() != "")) {
			if (window.opener) {
				if (window.opener.document.forms.frmHeaderSearch) {
					window.opener.document.forms.frmHeaderSearch.strSearchQuery.value	= $("#strSearchQuery").val();
					window.opener.loadMarkers();
					window.opener.focus();
					window.close();
					
					return false;
				} else {
					return true;
				}
			} else {
				return true;
			}
		}
	}

	function setCheckbox(strCheckboxID, strValue) {
		if (parseInt(strValue) == 1) {
			// enabled
			$("#" + strCheckboxID).val("1");
			$("#" + strCheckboxID + "Wrapper").addClass("checkbox_wrapper_selected");
		} else {
			// disabled
			$("#" + strCheckboxID).val("0");
			$("#" + strCheckboxID + "Wrapper").removeClass("checkbox_wrapper_selected");
		}
	}
	
	function setSearchPassthroughOptions() {
		if ($("#strSearchQuery") == STR_HEADER_SEARCH_DEFAULT) {
			$("#strSearchQuery").val("");
		}
		
		// gather custom options here
		$("#txtOptionsJSON").val(buildCustomOptions());
		
		// let's go!
		return true;
	}
	
	function setupCustomOptions() {
		$(".checkbox_wrapper").each(function() {
			$(this).click(function() {
				var intCurValue		= parseInt($(this).children("input").val());
				
				if (intCurValue) {
					$(this).children("input").val("0");
					$(this).removeClass("checkbox_wrapper_selected");
				} else {
					$(this).children("input").val("1");
					$(this).addClass("checkbox_wrapper_selected");
				}
			});
		});
		$(".checkbox_label").each(function() {
			$(this).click(function() {
				var strCurCheckbox	= $(this).attr("for");
				
				if (strCurCheckbox) {
					$("#" + strCurCheckbox).trigger("click");
				}
			});
		});
	}

	function buildCustomOptions() {
		var txtJSON			= 	"{";
		
		txtJSON				+=	"q:\"" + (($("#strSearchQuery").val() == STR_HEADER_SEARCH_DEFAULT) ? "" : $("#strSearchQuery").val()) + "\",";
		txtJSON				+=	"type_h:" + $("#chkTypeHouse").val() + ",";
		txtJSON				+=	"type_c:" + $("#chkTypeCondo").val() + ",";
		txtJSON				+=	"type_t:" + $("#chkTypeTownhouse").val() + ",";
		txtJSON				+=	"type_m:" + $("#chkTypeMulti").val() + ",";
		txtJSON				+=	"type_l:" + $("#chkTypeLand").val() + ",";
		txtJSON				+=	"type_o:" + $("#chkTypeOther").val() + ",";
		txtJSON				+=	"inc_mls:" + $("#chkIncludeMLS").val() + ",";
		txtJSON				+=	"inc_mlsf:" + $("#chkIncludeMLSFore").val() + ",";
		txtJSON				+=	"inc_s:" + $("#chkIncludeSale").val() + ",";
		txtJSON				+=	"price_m:" + $("#intPriceMin").val() + ",";
		txtJSON				+=	"price_x:" + $("#intPriceMax").val() + ",";
		txtJSON				+=	"beds_m:" + $("#intBedsMin").val() + ",";
		txtJSON				+=	"beds_x:" + $("#intBedsMax").val() + ",";
		txtJSON				+=	"baths:" + $("#intBaths").val() + ",";
		txtJSON				+=	"sqft_m:" + $("#intSqftMin").val() + ",";
		txtJSON				+=	"sqft_x:" + $("#intSqftMax").val() + ",";
		txtJSON				+=	"ls_m:" + $("#intLotMin").val() + ",";
		txtJSON				+=	"ls_x:" + $("#intLotMax").val() + ",";
		txtJSON				+=	"pkg:" + $("#intParking").val() + ",";
		txtJSON				+=	"pkg_g:" + $("#chkParkingGarage").val() + ",";
		txtJSON				+=	"yb_m:" + $("#intYearMin").val() + ",";
		txtJSON				+=	"yb_x:" + $("#intYearMax").val() + ",";
		txtJSON				+=	"newcon:" + $("#chkNewConst").val() + ",";
		txtJSON				+=	"view:" + $("#chkHasView").val() + ",";
		txtJSON				+=	"fixer:" + $("#chkFixerUpper").val() + ",";
		txtJSON				+=	"nl:" + $("#chkNewListings").val() + ",";
		txtJSON				+=	"pr:" + $("#chkPriceReduced").val() + ",";
		txtJSON				+=	"pr_v:" + $("#intPriceReduced").val() + ",";
		txtJSON				+=	"excl_c:" + $("#chkExcludeContract").val() + ",";
		txtJSON				+=	"excl_ss:" + $("#chkExcludeShortSales").val() + ",";
		txtJSON				+=	"rmrk:\"" + $("#txtRemarks").val() + "\"";
		
		txtJSON				+=	"}";
		
		return txtJSON;
	}
	
	function restoreCustomOptions() {
		
	}
	
	function showCustomWindow() {
		$("#custom_search_wrapper").show();	
	}
	