// JavaScript Document

function SearchHomes(min_sqft, min_num_beds, min_num_baths, fp_type, update_this) {
	
	url = "ajax/search.php?timevar=" + (new Date()).getTime();
	
	var params = "&min_sqft=" + min_sqft +
				  "&min_num_beds=" + min_num_beds +
				  "&min_num_baths=" + min_num_baths +
					"&type=" + fp_type;
				  
	//alert(params);
				  
	new Ajax.Updater(update_this, url, {
					 asynchronous: true,
					 method: "get",
					 parameters: params,
					 onLoading: function(request) { Element.hide(update_this); Element.show("search_progress"); Form.disable('search_form'); },
					 onComplete: function(request) { Element.show(update_this); Element.hide("search_progress"); Form.enable('search_form'); initLightbox(); },
					 onFailure: function(request) { assignError(request.responseText); }
					 });
}

function SearchHomesAndSites(search_type, price_range_from, price_range_to, number_of_bedrooms, number_of_bathrooms, update_this) {
	
	url = "ajax/search.php?timevar=" + (new Date()).getTime();
	
	var params = "&search_type=" + search_type +
				  "&price_range_from=" + price_range_from +
				  "&price_range_to=" + price_range_to +
				  "&number_of_bedrooms=" + number_of_bedrooms +
				  "&number_of_bathrooms=" + number_of_bathrooms;
				  
	//alert(params);
				  
	new Ajax.Updater(update_this, url, {
					 asynchronous: true,
					 method: "get",
					 parameters: params,
					 onLoading: function(request) { Element.hide(update_this); Element.show("search_progress"); Form.disable('search_form'); },
					 onComplete: function(request) { Element.show(update_this); Element.hide("search_progress"); Form.enable('search_form'); initLightbox(); },
					 onFailure: function(request) { assignError(request.responseText); }
					 });
}

function SendToFriend(friend_name, friend_email, your_name, your_email, property, update_this) {
	
	url = "ajax/sendtofriend.php";
	
	//var params = $('sendtofriend_form').serialize();
	var params = "friend_name=" + friend_name +
				  "&friend_email=" + friend_email +
				  "&your_name=" + your_name +
				  "&your_email=" + your_email +
				  "&property=" + property;
				  
	//alert(params);
				  
	new Ajax.Updater(update_this, url, {
					 asynchronous: true,
					 method: "get",
					 parameters: params,
					 onLoading: function(request) { Element.hide(update_this); Element.show("sendtofriend_progress"); Form.disable('sendtofriend_form'); },
					 onComplete: function(request) { Element.show(update_this); Element.hide("sendtofriend_progress"); Form.enable('sendtofriend_form'); $('friend_name').clear(); $('friend_email').clear(); FrominitLightbox(); },
					 onFailure: function(request) { assignError(request.responseText); }
					 });
}

function checkPriceOptions(select_start, select_end, low_price, high_price, price_increment) {
	selected_start = select_start.options[select_start.selectedIndex].value;
	selected_end = select_end.options[select_end.selectedIndex].value;
	
	select_start.options.length = 0;
	select_end.options.length = 0;
	
	//for(x = 0; x < 10; x++) {
	x = 0;
	
	while( (low_price + (x * price_increment)) < selected_end ) {
		//alert(x);
		
		if( (low_price + (x * price_increment)) < selected_end) {
			
			if( (low_price + (x * price_increment)) == selected_start ) {
				selected_option = true;
			} else {
				selected_option = false;
			}
			
			option_text = new NumberFormat(low_price + (x * price_increment));
			option_text.setPlaces(0);
			option_text_formatted = option_text.toFormatted();
			
			select_start.options[x] = new Option( option_text_formatted, low_price + (x * price_increment), selected_option, selected_option)
			//addOption(select_start, low_price + (x * price_increment), low_price + (x * price_increment));
			//alert(select_start.options[x].text);
		} 
		
		x++;
	}
	
	//for(x = 0; x < 10; x++) {
	x = 0;
	
	while( (high_price - (x * price_increment)) > selected_start ) {
		
		if( (high_price - (x * price_increment)) > selected_start) {
			
			if( (high_price - (x * price_increment)) == selected_end ) {
				selected_option = true;
			} else {
				selected_option = false;
			}
			
			option_text = new NumberFormat(high_price - (x * price_increment));
			option_text.setPlaces(0);
			option_text_formatted = option_text.toFormatted();
			
			select_end.options[x] = new Option(option_text_formatted, high_price - (x * price_increment), selected_option, selected_option)
			//addOption(select_end, high_price - (x * price_increment), high_price - (x * price_increment));
			//alert(select_end.options[x].text);
		} 
		
		x++;
	}
		
}

function ShowHideBedBath(option_text) {
	if(option_text != 'Commercial' && option_text != 'Homesites') {
		//alert("Show");
		Element.show("bedrooms");
		Element.show("bathrooms");
	} else {
		//alert("Hide");
		Element.hide("bedrooms");
		Element.hide("bathrooms");
	}
}


/*
function doEasyValidation(form_name) {
	
		//alert('Validating...');
		
		valid = new Validation(form_name, {onSubmit:false});
		result = valid.validate();
		
		//if(result) {
			//Element.hide('submit');
			//Element.show('progress');
		//}
		
		//alert(result);
		
		return result;
	
}
*/

function ShowPopUp(_name) {
	var arrayPageSize = getPageSize();
	Element.setHeight('overlay', arrayPageSize[1]);
	new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.8 } );
	
	// calculate top offset for the lightbox and display 
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);

	Element.setTop(_name, lightboxTop);
	Element.show(_name);
	
	//alert( $('property').value );
}

function HidePopUp(_name) {
	Element.hide(_name);
	Element.hide('popover_results');
	new Effect.Fade('overlay', { duration: 0.2 });
}

function EmailToFriend(property_id) {
	$('sendtofriend_form').reset();
	
	//valid = new Validation(form_name, {onSubmit:false});
	if(window.valid) {
		valid.reset();
	}
	
	$('property').value = property_id;
	
	ShowPopUp('sendtofriend');
}