// Search Page Scripts

function PopupOtherStateILS ( field ) 
{
	if ( ! field ) return; 
	if ( field.value == "" ) return;
	newWindow = window.open( field.value, 'OtherStateWindow', 'scrollbars=yes,resizable=yes');
	newWindow.focus();
	field.value = ""
}

function saveSearchPopup( form_id, display_group, owner, email, user_id, user_type, currentID )
// Popup window to allow user to save the current search
// form_id	- the id of the form in the opening window
// display_group - The type of search being saved
// owner - The user id of logged in user
// email - Is Email Alert to be defaulted on/off (Yes/No)
// user_id the realtor_no or public_user_id of logged in user
// user_type is this a member or public user
{
	var form = document.getElementById(form_id);
	// Build the list of form inputs names & values
	var inputName = '';
	var inputValue = '';
	for ( i=0; i<form.length; i++ )
	{
		if ( (form.elements[i].type == 'checkbox' && form.elements[i].checked) || (form.elements[i].type != 'checkbox' && form.elements[i].value != '') )
		{
			// strip commas from value
			if ( form.elements[i].name == 'min_price' || form.elements[i].name == 'max_price' )
			{
				form.elements[i].value = form.elements[i].value.replace(/,/g,"");
			}
			
			if (i == 0)
			{
				inputName += form.elements[i].name;
				inputValue += form.elements[i].value;
			}
			else
			{
				inputName += "," + form.elements[i].name;
				inputValue += "," + form.elements[i].value;
			}
		}
	}
	// Build the URL string
	UrlForPopUp = '/lst/lst-popup-savesearch.cfm?display_group=' + escape ( display_group );
	UrlForPopUp += '&email=' + escape ( email );
	UrlForPopUp += '&owner=' + escape ( owner );
	UrlForPopUp += '&user_id=' + escape ( user_id );
	UrlForPopUp += '&user_type=' + escape ( user_type );
	UrlForPopUp += '&inputs=' + escape ( inputName );
	UrlForPopUp += '&values=' + escape ( inputValue );
	UrlForPopUp += '&currentID=' + escape ( currentID );
	saveSearchWindow = window.open(UrlForPopUp, 'saveSearchWindow', 'height=330,width=560,scrollbars=no');
	saveSearchWindow.focus();
}

function runSearch ( form_id, id, search_type )
{ 
	var form = document.getElementById(form_id);
	form.action = "/lst/lst-search-query.cfm";
	form.saved_search.value= id;
	form.search_type.value= search_type;
	form.submit();
}
function updateSearch ( form_id )
{ 
	var form = document.getElementById(form_id);
	form.action = "/lst/lst-savesearch-query.cfm";
	form.search_type.value= "Update";
	form.submit();
}
function deleteSearch ( form_id )
{ 
	var form = document.getElementById(form_id);
	form.action = "/lst/lst-savesearch-query.cfm";
	form.search_type.value= "Delete";
	form.submit();
}

function resetSavedSearch ( searchName, searchID )
{
	//Get the search select element
	searchSelect = document.getElementById("saved_search")
	
	// make sure it exists to stop any errors
	if (searchSelect) 
	{
		// Add this option to the end of the select box
		posn = searchSelect.options.length;
		searchSelect.options.length=posn+1;
		searchSelect.options[posn].value = searchID;
		searchSelect.options[posn].text = searchName;			
		// Ensure that the search select box is visible
		searchSelect.style.display='';
	} 
} 
//function ValidateThisForm (form)
		//{
			// Add validation here
			//if ( form.Min_Price.value != "" && form.Max_Price.value != "" )
			//{ 
				// Both Min and max price is specified - ensure that min is less than or equal max
				// if ( form.Min_ListPr.value > form.Max_ListPr.value )
				//if ( IsLessThan ( form.Max_Price, form.Min_Price.value ) )
				//{
					//AlertValidationFailed ( form.Min_Price, "Min Price", "Cannot be greater than the selected Max Price " );
					//return false;
				//}
			//}
			//return true;
		//}
// Shows current value as text
function displayValues(vals,maxval,id) {
	var lower = "";
	var upper = "";
	lower = getDisplayPrice(vals[0],0,maxval);
	upper = getDisplayPrice(vals[1],1,maxval);
	$('min_'+id).value = lower;
	$('max_'+id).value = upper;
	if (lower == 'Show All') {
		$('min'+id).value = "";
	}
	else {
		$('min'+id).value = vals[0];
	}
	if (upper == 'Show All') {
		$('max'+id).value = "";
	}
	else {
		$('max'+id).value = vals[1];
	}
}
// Returns the price to display. [val]: Price, [index]: 0 = min, 1 = max, [upper]: the uppermost price in this slider.
function getDisplayPrice(val,index,upper) {
	if (val == upper && index == 1) {
		return 'Show All';
	}
	else if (val == upper && index == 0) {
		return 'Show All';
	}
	else if (val == 0) {
		return 'Show All';
	}
	else {
		return addCommas(val);
	}
}
// Returns a number formatted with commas
function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}