
/* Call to avoid jQuery $ conflicts. */
jQuery.noConflict();

/* Set up our namespaces. */
var HolidayLets;
HolidayLets = {};
HolidayLets.GetInTouch = {};
HolidayLets.IncomeEstimate = {};
HolidayLets.PropertyForSale = {};
HolidayLets.Utils = {};

/* Globals. */
HolidayLets.pendingEvent = null;
HolidayLets.http = xmlHttpRequestObject(); // From classic.js.

/* Holds an x/y coordinate. */
HolidayLets.Coordinate = function (x, y) {
    this.x = x;
    this.y = y;
};

/* QuotePicker class. */
HolidayLets.QuotePicker = function () {
    this.FadeInTime = 2500;
    this.QuoteDisplayed = 0;
    this.Positions = [ new HolidayLets.Coordinate(30, 50),
					   new HolidayLets.Coordinate(320, 200),
					   new HolidayLets.Coordinate(25, 180),
					   new HolidayLets.Coordinate(325, 15),
					   new HolidayLets.Coordinate(60, 50),
					   new HolidayLets.Coordinate(180, 140),
					   new HolidayLets.Coordinate(50, 195),
					   new HolidayLets.Coordinate(350, 200),
					 ];
    this.MiniQuotes = jQuery('#QuotePicker > p');
    this.FullQuotes = jQuery('#QuotePane > div');
    this.run();
};

/* Add functionality to the QuotePicker object. */
HolidayLets.QuotePicker.prototype = {
    _this: "",

    /* Start fading quotes. */
    run: function () {
        _this = this;
        var i = 0;
        /* Set the mini-quote positions and click events. */
        for (i = 0; i < 8; i++) {
            jQuery(_this.MiniQuotes[i]).css('left', _this.Positions[i].x);
            jQuery(_this.MiniQuotes[i]).css('top', _this.Positions[i].y);
            jQuery(_this.MiniQuotes[i]).click((function (i) {
                return function (event) {
                    _this.show_quote(i);
                    event.preventDefault();
                };
            })(i));
        }
        /* Initial fade in of quotes. */ 
        i = 0;
        for (i = 0; i < 4; i++) {
            jQuery(_this.MiniQuotes[i]).fadeIn(_this.FadeInTime, function () { });
        }
        /* Begin fading after four seconds. */
        setTimeout(function () { _this.interval_fade() }, 4000);
    },

    /* Fade the quotes that are on display, one every three
     * seconds. */
    interval_fade: function () {
        _this = this
        var i = 0;
        for (i = 0; i < 4; i++) {
            var MiniQuote = _this.MiniQuotes[i];
            MiniQuote.i = i;
            var Closure = (function (MiniQuote) {
                return function () {
                    _this.fade(MiniQuote);
                };
            })(MiniQuote);
            setTimeout(Closure, i * 3000);
        }
    },

    /* Fade an individual quote. */
    fade: function (MiniQuote) {
        _this = this
        jQuery(MiniQuote).fadeOut('slow', function () { });
        jQuery(_this.MiniQuotes[MiniQuote.i + 4]).fadeIn(_this.FadeInTime, function () { });
        /* After we have faded all our quotes start at the beggining
         * again. */
        if (MiniQuote.i + 4 === 7) {
            setTimeout(function () { _this.hide_all(); _this.run(); }, 5000);
        }
    },

    /* Fade out all quotes that are on display. */
    hide_all: function () {
        for (i = 0; i < 8; i++) {
            jQuery(_this.MiniQuotes[i]).fadeOut(_this.FadeInTime, function () { });
        }
    },

    /* Show the full quote (called after a fading quote has been
     * clicked). */
    show_quote: function (QuoteId) {
        jQuery(_this.FullQuotes[_this.QuoteDisplayed]).fadeOut('slow', function () {
            _this.QuoteDisplayed = QuoteId;
            jQuery(_this.FullQuotes[QuoteId]).fadeIn('slow', function () { });
        });
    },

    /* Show the next quote from our array. */
    next_quote: function () {
        jQuery(_this.FullQuotes[_this.QuoteDisplayed]).fadeOut('slow', function () {
            if (_this.QuoteDisplayed === _this.FullQuotes.length - 2) {
                _this.QuoteDisplayed = 0;
            } else {
                _this.QuoteDisplayed = _this.QuoteDisplayed + 1;
            }
            jQuery(_this.FullQuotes[_this.QuoteDisplayed]).fadeIn('slow', function () { });
        });
    }
};

/* This class takes in some basic property information and calculates
 * an estimate of Owner income. This is provided in the following
 * format: E.g. 12,000 to 17,000. Where the property information is
 * incomplete defaults are substituted.
 */
HolidayLets.IncomeEstimate = function ( Accomodates, 
										BrochureArea,
										Bedrooms,
										Bathrooms,
										StyleOfProperty,
										SeaView )  {
	/* These two values are calculated based on the property
	 * parameters we have been given.  Combined they are used to
	 * return the income estimate from the this.RentBandIncomes
	 * nested array. E.g.  this.RentBandIncomes[10][7] for 27
	 * weeks at rent band G. */
	this.RentBand = 0;
	this.Occupancy = 0;
	
	/* Used to convert a rent band number to a letter. */
	this.AllRentBands = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	
	/* Hold property information. */
	this.Accomodates  = 0;
	this.BrochureArea  = 0;
	this.Bedrooms  = 0;
	this.Bathrooms = 0;
	this.StyleOfProperty = 0;
	this.SeaView = 0;
	
	/* Hold rent band values for each parameter provided (to be
	 * summed later). */
	this.AccomodatesRentBand  = 0;
	this.BrochureAreaRentBand  = 0;
	this.BedroomsRentBand  = 0;
	this.BathroomsRentBand = 0;
	this.StyleOfPropertyRentBand = 0;
	this.SeaViewRentBand = 0;
	
	/* Hold occupancy levels for each parameter provided (to be
	 * summed later). */
	this.AccomodatesOccupancy  = 0;
	this.BrochureAreaOccupancy  = 0;
	this.BedroomsOccupancy  = 0;
	this.BathroomsOccupancy = 0;
	this.StyleOfPropertyOccupancy = 0;
	this.SeaViewOccupancy = 0;
	
	/* Each property parameter has multiple options for example
	 * Sea View can either be Unselected, Yes or No .  Each option
	 * is converted into a rent band value used in calculating a
	 * rent band (in conjunction with the parameter weighting). */
	this.AccomodatesRentValues		  = [0, 0, -6, -4, -2, 1, 3, 7, 9, 11, 12, 15, 15];
	this.BrochureAreaRentValues		  = [0, -1, 0, 2, 1, 0, 0, 0, 0, 1, 0];
	this.BedroomsRentValues			  = [10, -6, -2, 2, 8, 13, 15, 15, 16];
	this.BathroomsRentValues		  = [0, -2, 0, 6, 8];  
	this.StyleOfPropertyRentValues	  = [0, 2, 0, 1, -1];
	this.SeaViewRentValues			  = [0, 0, 3];
	
	/* The xOccupancyValues are used in the same way as the
	 * xRentValues but represent average occupancy values rather
	 * than rent bands. */
	this.AccomodatesOccupancyValues		= [0, 0, 0.034007909, 0.009339295, -0.001175738, -0.012132352, -0.066244874, -0.023594225, -0.080134766, -0.013407346, -0.052154964, 0.008608722, -0.205984539];
	this.BrochureAreaOccupancyValues	= [0, 0.004685774, 0.022936247, 0.020724364, -0.120656563, -0.017354764, -0.123876243, -0.127010568, 0.009414055, 0.011310456, 0.030449207];
	this.BedroomsOccupancyValues		= [0, 0.033549803, -0.000286264, -0.057974114, -0.051351688, -0.022226412, -0.216573037, -0.138856489, -0.149967119];
	this.BathroomsOccupancyValues		= [0, 0.004642094, -0.023001189, -0.088458572, -0.238684942, 0];
	this.StyleOfPropertyOccupancyValues = [0, 0.026155862, -0.024536812, -0.04112734, 0.01281683];
	this.SeaViewOccupancyValues			= [0, 0, 0.051178789];
	
	/* Weightings used to calculate rent band values and occupancy
	 * levels for each property parameter. For example we can take
	 * this.AccomodatesRentValues and multiply it by
	 * this.AccomodatesWeightings[i] to calculate our rent band
	 * based on the this.Accomodates parameter. */
	this.AccomodatesWeightings		= [0.7, 0.9, 0.8, 2, 2];
	this.BrochureAreaWeightings		= [1, 1, 2, 1, 1];
	this.BedroomsWeightings			= [1, 1, 1, 1, 1];
	this.BathroomsWeightings		= [1, 2, 3, 2,1];
	this.StyleOfPropertyWeightings	= [2, 2, 5, 2, 2];
	this.SeaViewWeightings			= [2, 1.2, 1.5, 2, 2];
		
	/* X-axis represents rent bands [a-z], y-axis weeks. A
	 * combination of rent band and weeks will therefore give you a
	 * number (multiply by 1000 to give you actual income). */
	this.RentBandIncomes =  [																						/* Rent Bands A-Z */ 
								[5,	6,	6,	7,	7,	7,	8,	8,	9,	10,	10,	11,	12,	12,	13,	14,	14,	15,	16,	17,	19,	21,	23,	26,	33,	41], /* W */
								[5,	6,	6,	7,	7,	8,	8,	9,	9,	10,	11,	11,	12,	13,	14,	14,	15,	16,	17,	18,	20,	22,	23,	26,	34,	42], /* E */
								[6,	6,	7,	7,	8,	8,	9,	9,	10,	10,	11,	12,	13,	13,	14,	15,	15,	16,	17,	19,	21,	22,	24,	27,	35,	44], /* E */
								[6,	6,	7,	7,	8,	8,	9,	10,	10,	11,	12,	12,	13,	14,	15,	15,	16,	17,	18,	19,	21,	23,	25,	28,	36,	45], /* K */
								[6,	7,	7,	8,	8,	9,	9,	10,	11,	11,	12,	13,	13,	14,	15,	16,	17,	17,	18,	20,	22,	24,	26,	29,	37,	47], /* S */
								[6,	7,	7,	8,	9,	9,	10,	10,	11,	12,	12,	13,	14,	15,	15,	16,	17,	18,	19,	20,	23,	25,	27,	30,	38,	48], /* 1 */
								[7,	7,	8,	8,	9,	9,	10,	11,	11,	12,	13,	14,	14,	15,	16,	17,	18,	18,	20,	21,	23,	25,	27,	31,	39,	49], /* 7 */
								[7,	7,	8,	9,	9,	10,	10,	11,	12,	12,	13,	14,	15,	16,	16,	17,	18,	19,	20,	22,	24,	26,	28,	32,	40,	51], /* T */
								[7,	8,	8,	9,	9,	10,	10,	11,	12,	13,	13,	14,	15,	16,	17,	18,	19,	19,	21,	22,	24,	27,	29,	32,	41,	52], /* O */
								[7,	8,	8,	9,	10,	10,	11,	11,	12,	13,	14,	15,	15,	16,	17,	18,	19,	20,	21,	23,	25,	27,	29,	33,	42,	53], /* 4 */
								[7,	8,	9,	9,	10,	10,	11,	12,	12,	13,	14,	15,	16,	17,	18,	19,	19,	20,	22,	23,	26,	28,	30,	34,	43,	54], /* 5 */
								[8,	8,	9,	10,	10,	11,	11,	12,	13,	14,	14,	15,	16,	17,	18,	19,	20,	21,	22,	24,	26,	28,	31,	35,	44,	55],
								[8,	9,	9,	10,	10,	11,	12,	12,	13,	14,	15,	16,	17,	18,	18,	19,	20,	21,	23,	24,	27,	29,	31,	35,	45,	57],
								[8,	9,	9,	10,	11,	11,	12,	13,	13,	14,	15,	16,	17,	18,	19,	20,	21,	22,	23,	25,	27,	30,	32,	36,	46,	58],
								[8,	9,	10,	10,	11,	11,	12,	13,	14,	15,	16,	16,	17,	18,	19,	20,	21,	22,	24,	25,	28,	30,	33,	37,	47,	59],
								[8,	9,	10,	10,	11,	12,	12,	13,	14,	15,	16,	17,	18,	19,	20,	21,	22,	23,	24,	26,	28,	31,	33,	38,	48,	60],
								[9,	9,	10,	11,	11,	12,	13,	13,	14,	15,	16,	17,	18,	19,	20,	21,	22,	23,	24,	26,	29,	32,	34,	38,	49,	61],
								[9,	10,	10,	11,	12,	12,	13,	14,	15,	15,	16,	17,	18,	19,	20,	21,	22,	24,	25,	27,	30,	32,	35,	39,	50,	62],
								[9,	10,	10,	11,	12,	12,	13,	14,	15,	16,	17,	18,	19,	20,	21,	22,	23,	24,	25,	27,	30,	33,	35,	40,	51,	63],
								[9,	10,	11,	11,	12,	13,	13,	14,	15,	16,	17,	18,	19,	20,	21,	22,	23,	24,	26,	28,	31,	33,	36,	40,	51,	64],
								[9,	10,	11,	12,	12,	13,	14,	14,	15,	16,	17,	18,	19,	20,	22,	23,	24,	25,	26,	28,	31,	34,	36,	41,	52,	65],
								[10,10,	11,	12,	12,	13,	14,	15,	16,	17,	18,	19,	20,	21,	22,	23,	24,	25,	27,	29,	32,	34,	37,	42,	53,	66],
								[10,	11,	11,	12,	13,	13,	14,	15,	16,	17,	18,	19,	20,	21,	22,	23,	25,	26,	27,	29,	32,	35,	38,	43,	54,	68],
								[10,	11,	11,	12,	13,	14,	14,	15,	16,	17,	18,	19,	20,	22,	23,	24,	25,	26,	28,	30,	33,	35,	38,	43,	55,	69],
								[10,	11,	12,	12,	13,	14,	15,	16,	16,	18,	19,	20,	21,	22,	23,	24,	25,	27,	28,	30,	33,	36,	39,	44,	56,	70],
								[10,	11,	12,	13,	13,	14,	15,	16,	17,	18,	19,	20,	21,	22,	23,	25,	26,	27,	28,	30,	34,	37,	39,	44,	56,	71],
								[10,	11,	12,	13,	14,	14,	15,	16,	17,	18,	19,	20,	21,	23,	24,	25,	26,	27,	29,	31,	34,	37,	40,	45,	57,	72],
								[11,	11,	12,	13,	14,	14,	15,	16,	17,	18,	19,	21,	22,	23,	24,	25,	26,	28,	29,	31,	35,	38,	41,	46,	58,	73],
								[11,	12,	12,	13,	14,	15,	16,	17,	18,	19,	20,	21,	22,	23,	24,	26,	27,	28,	30,	32,	35,	38,	41,	46,	59,	74]
							];
	
	/* Fetch constructor parameters */						
	this.Accomodates  = Accomodates;
	this.BrochureArea  = BrochureArea;
	this.Bedrooms  = Bedrooms;
	this.Bathrooms = Bathrooms;
	this.StyleOfProperty = StyleOfProperty;
	this.SeaView = SeaView;
};

HolidayLets.IncomeEstimate.prototype = {
	
	/* Used to calculate which value to select from our weightings
	 * tables. */
	weighting: function ( ) {
		if ( this.Accomodates < 4) {
			return 0;
		} else if (this.Accomodates < 9) {
			if ( this.Bathrooms === 0 || this.Bathrooms === 1 ) {
					return 3;					
				}			
			return 1;
		} else {
			if ( this.Bathrooms === 0 || this.Bathrooms === 1 ) {
				return 4;
			}
			return 2;				
		}
	},
	
	/* Calculates rent band and occupancy. */
	calculate: function () {
		this.calculate_rent_band();
		this.calculate_occupancy();		
	},
	
	calculate_rent_band: function() {
		/* Fetch actual values for each parameter apply
		 * weightings. */
		this.AccomodatesRentBand  = this.AccomodatesRentValues[this.Accomodates] / this.AccomodatesWeightings[this.weighting( this.Accomodates )];
		this.BrochureAreaRentBand  = this.BrochureAreaRentValues[this.BrochureArea] / this.BrochureAreaWeightings[this.weighting( this.Accomodates )];
		this.BedroomsRentBand  = this.BedroomsRentValues[this.Bedrooms] / this.BedroomsWeightings[this.weighting( this.Accomodates )];
		this.BathroomsRentBand = this.BathroomsRentValues[this.Bathrooms] / this.BathroomsWeightings[this.weighting( this.Accomodates )];
		this.StyleOfPropertyRentBand = this.StyleOfPropertyRentValues[this.StyleOfProperty] / this.StyleOfPropertyWeightings[this.weighting( this.Accomodates )];
		this.SeaViewRentBand = this.SeaViewRentValues[this.SeaView] / this.SeaViewWeightings[this.weighting( this.Accomodates )];
		
		/* Take average of accommodates, bathrooms,
		 * bedrooms. */
		var AccomodatesBathroomsBedroomsRentAverage = Math.ceil((this.AccomodatesRentBand + this.BedroomsRentBand 
																					  + this.BathroomsRentBand) / 3);
		
		/* Sum each parameters average rent band to produce a
		 * final rent band. Incorporating average rent band
		 * accross all properties (10). */
		var FinalRentBand = Math.round(AccomodatesBathroomsBedroomsRentAverage
										+ this.BrochureAreaRentBand
										+ this.StyleOfPropertyRentBand
										+ this.SeaViewRentBand) + 10;
		
		/* Check rent band hasn't exceeded the rent band
		 * bounds. */
		if (FinalRentBand > 25) {
			FinalRentBand = 25;
		}		
		/* Set object level variable. */
		this.RentBand = FinalRentBand;
	},
	
	calculate_occupancy: function() {
		/* Fetch actual values for each parameter. */
		this.AccomodatesOccupancy  = this.AccomodatesOccupancyValues[this.Accomodates] * 100;
		this.BrochureAreaOccupancy  = this.BrochureAreaOccupancyValues[this.BrochureArea] * 100;
		this.BedroomsOccupancy  = this.BedroomsOccupancyValues[this.Bedrooms] * 100;
		this.BathroomsOccupancy = this.BathroomsOccupancyValues[this.Bathrooms] * 100;
		this.StyleOfPropertyOccupancy = this.StyleOfPropertyOccupancyValues[this.StyleOfProperty] * 100;
		this.SeaViewOccupancy = this.SeaViewOccupancyValues[this.SeaView] * 100;
		
		/* Take average of accommodates, bathrooms,
		 * bedrooms. */
		var AccomodatesBathroomsBedroomsOccupancyAverage = (this.AccomodatesOccupancy + this.BedroomsOccupancy) / 2;
		/* Average of parameters gives us a final
		 * occupancy. Based on the avaerage property being
		 * available for 45 weeks per year and an average
		 * occupancy accross all properties being 55.1%. */
		var FinalOccupancy = Math.ceil(((AccomodatesBathroomsBedroomsOccupancyAverage
										+ this.BrochureAreaOccupancy
										+ this.StyleOfPropertyOccupancy
										+ this.SeaViewOccupancy
										+ 55.1) /100) * 45);	
												
		/* Align to the this.RentBandIncomes array which
		 * starts at week 17. */
		FinalOccupancy = FinalOccupancy - 17;
		
		/* Ensure occupancy hasn't exceeded the bounds of the
		 * this.RentBandIncomes array. */
		if (FinalOccupancy < 4) {
			FinalOccupancy = 3;
		} 	
		if (FinalOccupancy > 28) {
			FinalOccupancy = 28;
		}
		this.Occupancy = FinalOccupancy;
	},
	
	/* Render our income estimate as html. */
	to_html: function () {
		this.calculate();
		
		var LowerIncomeRange = "&#163;" + this.RentBandIncomes[this.Occupancy -3 ][this.RentBand -2] + ",000";
		var HigherIncomeRange = "&#163;" + this.RentBandIncomes[this.Occupancy +3][this.RentBand] + ",000";
		
		var rent_band_html = [];
		rent_band_html.push('<p style="padding-top:0px;margin-top:0px;" class="bigText">');
		rent_band_html.push('You can expect income in the region of <b>');		
		rent_band_html.push(HolidayLets.Utils.format('{0} to {1} ', LowerIncomeRange, HigherIncomeRange)); 		
		rent_band_html.push(HolidayLets.Utils.format('</b> Net<sup>*</sup> per year.</p>', 
													 this.Occupancy + 17, this.AllRentBands.charAt(this.RentBand)));
		return rent_band_html.join(""); 
	}	
};

/* Page level functions. */

/* Income estimate page. */

/* Holds the names of the hover sections for the income estimate
 * map. For example 'cofw.png'. */
HolidayLets.IncomeEstimate.RegionsExtensions = ['none', 'cofw', 'cosw', 'conc', 'cobm', 'cosc', 'deno', 'deda', 'deso', 'so', 'do'	];

/* Load a region hover section onto the income estimate map. */
HolidayLets.IncomeEstimate.LoadRegionHover =  function ( Region )  {	
	jQuery('#HoverMap').attr('src', HolidayLets.Utils.format('/media/letting/holiday-letting-income/{0}.png', 
															 HolidayLets.IncomeEstimate.RegionsExtensions[Region]));
}; 

/* The user has clicked the map so load the region they selected. */
HolidayLets.IncomeEstimate.LoadRegion =  function ( Region )  {	
	jQuery('#HoverMapStatic').attr('src', HolidayLets.Utils.format('/media/letting/holiday-letting-income/{0}.png', 
															 HolidayLets.IncomeEstimate.RegionsExtensions[Region]));
	jQuery('#ddlRegion').val(Region)
	HolidayLets.IncomeEstimate.ControlChangeEvent();												 
}; 

/* Fetch similar properties based on number accommodates, brochure
 * area and style of property. */
HolidayLets.IncomeEstimate.FetchSimilarProperties =  function ()  {	
	jQuery('#SimilarProperties').html("");
	jQuery.ajax({
		type: "GET",
		url: "/holiday-lets/searchresults.aspx",
		data: ({Accomodates : parseInt(jQuery('#ddlNumberAccomodates option:selected').text()),
				BrochureArea: jQuery('#ddlRegion option:selected').text(),
				StyleOfProperty: jQuery('#ddlStyleOfProperty option:selected').text()
				}),
		success: function(msg){
			if (msg !== "") {
				msg = msg + HolidayLets.IncomeEstimate.SimilarPropertiesLink();
				jQuery('#SimilarProperties').html(msg);	
			}					
		}
	});	
}; 

/* Link to Cottage results page with a prepopulated search based on
 * the parameters that the user has entered. */
HolidayLets.IncomeEstimate.SimilarPropertiesLink =  function ()  {	
	var Html = [];
	var Region = HolidayLets.IncomeEstimate.RegionsExtensions[jQuery('#ddlRegion option:selected').val()];
	var Accomodates = jQuery('#ddlNumberAccomodates option:selected').val();
	var Bedrooms = jQuery('#ddlBedrooms option:selected').val();
	var Bathrooms = jQuery('#ddlBathrooms option:selected').val();
	var PropertyType = jQuery('#ddlStyleOfProperty option:selected').val();
	var SeaViews = jQuery('#ddlSeaViews option:selected').val();
	/* Now do some ugly clean up to match the classic.co.uk url formats. */
	if (Region === "none") {
		Region = "";
	}
	if (Accomodates === "1") {
		Accomodates = "";
	}
	if (Bedrooms === "0") {
		Accomodates = "";
	}
	if (Bathrooms === "0") {
		Bathrooms = "";
	}
	if (PropertyType === "0") {
		PropertyType = "";
	}
	if (SeaViews > 1) {
		SeaViews = "true"
	} else {
		SeaViews = ""
	}
	var url = HolidayLets.Utils.format('/results.aspx?search=a&minA={0}&ptyp={1}&rgn={2}&bed={3}&bath={4}&svw={5}', 
		Accomodates, 
		PropertyType, 
		Region,
		Bedrooms,
		Bathrooms,
		SeaViews);
	Html.push(HolidayLets.Utils.format('<p class="bigText"><a href="{0}">View more similiar Properties ></a></p>', url));
	return Html.join("");	
}; 

/* Builds a query string based on the parameters the user has selected
 * on the income estimate form. Then redirects the user to the 'Get in
 * Touch' page with the previously built query string. */
HolidayLets.IncomeEstimate.SubmitProperty =  function ()  {	
	var Location = jQuery('#ddlRegion option:selected').text();
	var Type = jQuery('#ddlStyleOfProperty option:selected').val();
	var Bedrooms = jQuery('#ddlBedrooms option:selected').val();
	var Bathrooms = jQuery('#ddlBathrooms option:selected').val();
	/* Build url + query string. */
	var url = HolidayLets.Utils.format('/holiday-lets/about-your-holiday-home.html?location={0}&type={1}&bedrooms={2}&bathrooms={3}', 
										Location, 
										Type,
										Bedrooms,
										Bathrooms);
	/* Redirect user */
	window.location = url;
}; 

/* Handles drop down list changed events for the income estimate
 * form. */
HolidayLets.IncomeEstimate.ControlChangeEvent =  function () {
	/* Display a loading icon. */			
	jQuery('#RentBandTable').html( '<img src="/media/letting/ajax-loader.gif" />' ); 
	
	/* Get the user input from the form controls. */
	var AccomodatesValue  = parseInt( jQuery('#ddlNumberAccomodates').val() );
	var BrochureAreaValue  = jQuery('#ddlRegion').val();
	var BedroomsValue  = jQuery('#ddlBedrooms').val();
	var BathroomsValue = jQuery('#ddlBathrooms').val();
	var StyleOfPropertyValue = jQuery('#ddlStyleOfProperty').val();
	var SeaViewValue = jQuery('#ddlSeaViews').val();
	
	/* Maintain map consistency. */
	jQuery('#HoverMapStatic').attr('src', HolidayLets.Utils.format('/media/letting/holiday-letting-income/{0}.png', 
															 HolidayLets.IncomeEstimate.RegionsExtensions[jQuery('#ddlRegion').val()]));
	
	/* If the user has not changed any property information do not
	 * show an estimate. */
	if ((AccomodatesValue === 1) || (BedroomsValue === "0")) {
			jQuery('#RentBandTable').html( "Please enter your property information to receive an 'Estimate of Income'." );
			return;	
	}
	
	/* Create a new calculator object with our form input. */
	var Calculator = new HolidayLets.IncomeEstimate( AccomodatesValue, 
													 BrochureAreaValue,
													 BedroomsValue,
													 BathroomsValue,
													 StyleOfPropertyValue,
													 SeaViewValue);
													 
	/* Delay updating the interface and display the loading icon
	 * further for 1 second. */
	return setTimeout(function () {	jQuery('#RentBandTable').html( Calculator.to_html()).slideDown("slow"); 
									HolidayLets.IncomeEstimate.FetchSimilarProperties();  
								  }, 1000); 
};

/* Tell us about your property page. */ 

/* If a text box value is blank alert an error and set the text box
 * background color. */
HolidayLets.GetInTouch.ValidateInput = function (controlName, message) {
    var control = document.getElementById(controlName);
    if (control.value.length === 0) {
        control.style.backgroundColor = '#FFDBF0';
        alert(message);
        return true;
    } else {
        control.style.backgroundColor = '#FFFFFF';
        return false;
    }
};

/* Check that required fields have been completed and that we have a
 * valid email. */
HolidayLets.GetInTouch.ValidateProspectRequest = function () {
    if (HolidayLets.GetInTouch.ValidateInput('txtName', 'Please complete your name.')) return false;
    if (HolidayLets.GetInTouch.ValidateInput('txtEmail', 'Please complete your email address.')) return false;
    if (validateEmail(document.getElementById('txtEmail').value, 'from') != true) {
        document.getElementById('txtEmail').style.backgroundColor = '#FFDBF0';
        return false;
    }
    if (HolidayLets.GetInTouch.ValidateInput('txtPhone', 'Please complete your telephone number.')) return false;
};

/* Shows an additional question when the user anwsers 'No' to 'Is this
 * property ready to let right now?' */
HolidayLets.GetInTouch.AnswerRentNow = function () {
	if (jQuery('#ddlRentNow').val() === '2') {
		jQuery('#divWhenReady').show();
	} else {
		jQuery('#divWhenReady').hide();
	}
}; 

/* Property for Sale page. */

/* Reveals a hidden text description. */
HolidayLets.PropertyForSale.ExpandDescription = function() {
	var Dots = jQuery(this).find(".Dots");
	var Text = jQuery(this).find(".Text");
	jQuery(this).find("a").click( function (event) {	
		jQuery(this).hide();
		Dots.hide();			
		Text.show();
	});			
};

/* Utility code */

/* Replicates String.Format in Vb.Net. Maybe add this functionality to
 * the Array prototype under appendFormat()? */
HolidayLets.Utils.format = function() {
  var s = arguments[0];
  for (var i = 0; i < arguments.length - 1; i++) {       
    var reg = new RegExp("\\{" + i + "\\}", "gm");             
    s = s.replace(reg, arguments[i + 1]);
  }
  return s;
};

/* Run application. */

/* On document ready (all /holiday-lets/ pages). */
makeRounded();

/* Always show the 'Get in Touch' div, used on all pages. */
var ScrollingDiv = jQuery("#alwaysVisible");
jQuery(window).scroll(function () {
    if (jQuery(window).scrollTop() > 410) {
		ScrollingDiv.css("top", "0" );
        ScrollingDiv.css("position", "fixed" );
    }
    if (jQuery(window).scrollTop() <= 410) {
		ScrollingDiv.css("top", "" );
        ScrollingDiv.css("position", "" );
        ScrollingDiv.css("margin-top", "20px" );
    }
});

/* What People Say About Us. */
if (jQuery('#QuotePicker > p').length !== 0) {
    var MyQuotePicker = new HolidayLets.QuotePicker();
    jQuery("#QuoteNext").click(function (event) {
        event.preventDefault();
        MyQuotePicker.next_quote();
    });
}

/* How Much Could I Earn. */
if (jQuery('#FinalQuote > p').length !== 0) {	
	/* Add the ValueChangedEvent to all of the select inputs. */
	jQuery('#QuoteParameters :input').change(HolidayLets.IncomeEstimate.ControlChangeEvent);
	/* Add a click event to the 'Submit your property' link. */
	jQuery("#SubmitProperty > *").click(function (event) {
		HolidayLets.IncomeEstimate.SubmitProperty();
	});
	// Required in case the browser back button is pressed.
	HolidayLets.IncomeEstimate.ControlChangeEvent();
	/* Because we need the maps to be absolutely
	 * positioned we must refresh the MapContainer after a
	 * resize event. */
	jQuery(window).resize(function() {
		jQuery('#MapContainer').html( jQuery('#MapContainer').html() );
	});
}

/* Property for Sale. */
if (jQuery('.MoreLink').length !== 0) {	
	/* Add 'show more text' events to 'show more' links. */
	jQuery.each( jQuery('.MoreLink'), HolidayLets.PropertyForSale.ExpandDescription );	
	/* Make sure absolutely positioned elements stay in place. */
	jQuery(window).resize(function() {
		jQuery('.cottageList').html( jQuery('.cottageList').html() );
		/* And rebind our events. */
		jQuery.each( jQuery('.MoreLink'), HolidayLets.PropertyForSale.ExpandDescription );
	});
}



