/* PortalApplication Object / Class */

function PortalApplication() {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication
		# DESCR:*		Portal Application constructor.
		# :*			Sets up the Javascript Portal Object,
		# :*			loads saved settings, booked marked properties
		# :*			and viewed Properties.
		# USAGE:*		PortalApp = new PortalApplication();
		# :*			Used in the body onload="PortalApp = new PortalApplication();"
		# :*			stage of the Portal page wrap.
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	this.savedSettings = new SavedSettings();
	this.propertyRequest = null;
	this.ajaxWait = null;
}

PortalApplication.prototype.obtainSearchResults = function() {

	/*	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.obtainSearchResults
		# DESCR:*		Public funtion to grab the saved search settings from the cookie and
		# :*			load the search page. This replaces the PortalSearch.obtainSearchResults
		# :*			function as Realestateview.com.au will not use ajax searches.
		# USAGE:*		<button class="buttonSmall" type="submit" onClick="PortalApp.saveSearch('refinesearch');PortalApp.obtainSearchResults();return false;">Search</button>
		# RETURNS:*		Returns nothing. The browser is directed to load a new URL link.
		# :*			results.
		# %ENDPUBLIC% */

	var searchParams = PortalApp.savedSettings.toURLString();

	var url = "/results.php?" + searchParams;
	window.location = url;
}

PortalApplication.prototype.getCheckBoxInputArray = function(formName, formInputName) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.getCheckBoxInputArray
		# DESCR:*		Obtain the values of the check boxes within a form.
		# :*			if they are checked.
		# USAGE:*		var CheckBoxArrayValue = this.getCheckBoxInputArray(formName, name);
		# :*			fromName: Name of the form to look for
		# :*			formInputName: name of the checkboxes to get values from
		# RETURNS:*		Returns array of forms input values
		# :*			if they are checked. 
		# %ENDPUBLIC% */

	var CheckBoxen = document.forms[formName][formInputName];
	var CheckBoxValues = new Array();
	for(var i = 0;i < CheckBoxen.length; i++) {
		if (CheckBoxen[i].checked)
			CheckBoxValues.push(CheckBoxen[i].value);
	}

	return CheckBoxValues;
}

PortalApplication.prototype.saveSearch = function(formName) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.saveSearch
		# DESCR:*		Saves the form search settings to the savedSettings object.
		# :*			Uses the forms input names as class member accessors
		# USAGE:*		PortalApp = new PortalApplication(); PortalApp.saveSearch(nameOfForm);
		# :*			nameOfForm: Name of the form to save values from.
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	// Obtain all form elements
	var el = document.forms[formName].elements;

	// for each form element found
	for(var i = 0; i < el.length; i++) {
		var value = null;
		var name = el[i].name;

		// input type
		switch (el[i].type) {

			case 'checkbox': //currently all check boxes are array of same names
				//if(el[i].checked)
					//gets all the values of same named checkboxes in an array
					value = this.getCheckBoxInputArray(formName, name);
				break;

			case 'text':
					value = el[i].value;
				break;

			case 'search':
					value = el[i].value;
				break;

			case 'select-one':

				if (el[i].selectedIndex!=-1) {
					if (el[i].options[el[i].selectedIndex].value != "") {
						value = new Array(el[i].options[el[i].selectedIndex].value);
					}
				}

			case 'select-multiple':
				value = new Array();
				for(var j = 0;j < el[i].length;j++) {
					if (el[i].options[j].selected) {
						value.push(el[i].options[j].value);
					}
				}
				break;

			case 'hidden':
				value = el[i].value;
				break;

			default:
				break;
		}

		if (value != null) {
			// save the value in the object, use it's name to reference it
			this.savedSettings[name] = value;
			
		}
	}

	// tell object to write cookies
	this.savedSettings.saveSettings();
}

PortalApplication.prototype.updateForm = function(formName, elementName) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.updateForm
		# DESCR:*		Sets forms input variables with saved settings in savedSettings Object
		# :*			originally read from cookie.
		# USAGE:*		PortalApp = new PortalApplication(); PortalApp.updateForm(nameOfForm, elementName);
		# :*			nameOfForm: the form to update
		# :*			elementName: the element to update
		# :*			if elementName is blank, all elements are update
		# :*			else only elements matching that name are updated
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */


	// Obtain all form elements
	var el;
	try {
		el = document.forms[formName].elements;
	} catch(e) {
		// do nothing. We want el to be defined.
	}
	// for each form element
	if (el) {
		for(var i = 0;i < el.length; i++) {
		
			var name = el[i].name;
		
			if ((elementName == '') || (name == elementName)) {
				// input type
				switch (el[i].type) {
					case 'checkbox':
						var selected = false;
						var arr = this.savedSettings[name];

						if (arr != null) {
							// loop through stored values to find ones we need to select
							for(var j = 0; j < arr.length; j++) {
								if (arr[j] == el[i].value) {
									// selected value found so select it
									selected = true;
								}
							}

							// if array is empty then we should select all in the group for business rules
							if (arr.length == 0) {
								selected = true;
							}
					
							el[i].checked = selected;
						}
						break;

					case 'text':
						el[i].value = this.savedSettings[name];
						break;
	
					case 'search':
						el[i].value = this.savedSettings[name];
						break;

					case 'select-one':
					  	var value = this.savedSettings[name];

						if (typeof(value) == 'object') {
							value = value[0];
						}
					
						for(var j = 0; j < el[i].length; j++) {
							if (el[i].options[j].value ==  value) {
								//found match so select it
								el[i].options[j].selected = true;
							}
						}
						break;

					case 'select-multiple':
					  	var value = this.savedSettings[name];
						for(var j = 0; j < el[i].length; j++) {
							for(var l = 0; l < value.length; l++) {
								if (el[i].options[j].value ==  value[l]) {
									//found match so select it
									el[i].options[j].selected = true;
								}
							}
						}
						break;

					case 'hidden':
						// Skip updating "rm", "P", "portalsection, "portalview",  "con" or "ptr" if they are in the form.
						if (name != "P") {
							el[i].value = this.savedSettings[name];
							// Debug
							// alert(name + "=" + value);
						}
						break;

					default:
						break;
				}
			}
		}
	}
}

PortalApplication.prototype.updateSortControl = function(elementID) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.updateSortControl
		# DESCR:*		Sets Sort Control with saved settings in savedSettings Object
		# :*			originally read from cookie.
		# USAGE:*		PortalApp = new PortalApplication(); PortalApp.updateSortControl(elementName);
		# :*			elementName: the sort Select control id to update
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

		var value = this.savedSettings['Order'];

		if (typeof(value) == 'object') {
			value = value[0];
		}
		if ($(elementID) != null){				
			$(elementID).val(value).attr("selected", "selected");
		}
}

PortalApplication.prototype.selectAllCheckboxes = function(formName, CheckboxArrayName) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.selectAllCheckboxes
		# DESCR:*		Given a name of a form and a checkbox Array Name, Check all of the 
		# :*			checkbox Array contents.
		# USAGE:*		PortalApp = new PortalApplication(); PortalApp.selectAllCheckboxes(formName, CheckboxArrayName);
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

		var CheckBoxen = document.forms[formName][CheckboxArrayName];

		// Make all of the checkboxes in the CheckboxArrayName checked = true.
		for (i = 0; i < CheckBoxen.length; i++) {
			if (CheckBoxen[i].type == 'checkbox') {
				CheckBoxen[i].checked = true;
			}
		}
}

PortalApplication.prototype.checkIfAllCheckboxesEmpty = function(formName, CheckboxArrayName) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.checkIfAllCheckboxesEmpty
		# DESCR:*		Given a name of a form and a checkbox Array Name, verify if all of the 
		# :*			checkbox Array contents are unchecked. If they are all unchecked, make them all
		# :*			checked.
		# USAGE:*		PortalApp = new PortalApplication(); PortalApp.checkIfAllCheckboxesEmpty(formName, CheckboxArrayName);
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

		var CheckBoxen = document.forms[formName][CheckboxArrayName];
		// Assume form Checkbox Array is all unchecked
		var uncheckedArrayOfCheckboxes = true;

		for (i = 0; i < CheckBoxen.length; i++) {
			if (CheckBoxen[i].type == 'checkbox') {
				if (CheckBoxen[i].checked == true) {
					uncheckedArrayOfCheckboxes = false;
				}
			}
		}

		if (uncheckedArrayOfCheckboxes == true) {
			// If our array is all unckecked, check them all.
			this.selectAllCheckboxes(formName, CheckboxArrayName);
		}
}

/* SavedSettings Object / Class */

function SavedSettings() {

	/* 	# %PUBLIC%
		# NAME:*		SavedSettings
		# DESCR:*		SavedSettings Object constructor.
		# :*			Loads Portal users settings from cookies.
		# :*			Sets up storage for Portal users settings.
		# USAGE:*		this.savedSettings = new SavedSettings();
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	// last search storage
	this.IKW = ""; 				// Keyword
	this.PT = new Array();		// Property Type
	this.CS = new Array();		// CityState location
	this.Sub = new Array();		// Suburb Name / Post Code / ID
	this.BeL = 0;				// Bedroom Low
	this.BeH = 9999;			// Bedroom High
	this.BaL = 0;				// Bathroom Low
	this.BaH = 9999;			// Bathroom High
	this.PrL = 0;				// Price Low
	this.PrH = 99999;			// Price High
	this.PaL = 0;				// Parking Low
	this.PaH = 99999;			// Parking High
	this.LaL = 0;				// Land Area Low
	this.LaH = 9999;			// Land Area High
	this.FaL = 0;				// Floor Area Low
	this.FaH = 999999;				// Floor Area High
	this.Order = "";			// Sorting
	this.CID = 0;				// ClientID for Agent
	this.GID = 0;				// GroupID for Agent
	this.LaL = 0;				// Land Area Low
	this.LaH = 9999;			// Land Area High
	this.FaL = 0;				// Floor Area Low
	this.FaH = 999999;			// Floor Area High
	this.Con = "S";				// Contract Type
	this.PTr = "";				// Propert Type range
	this.Fur = 0;				// Furnished listings
	this.ExSold = 0;			// Sold listings
	this.ExAuct = 0;			// Auction Listings
	this.OFI = 0;				// Listings without OFI times

	// Grab Cookie Settings
	this.loadSettings();
}

SavedSettings.prototype.loadSettings = function() {

	/* 	# %PUBLIC%
		# NAME:*		SavedSettings.prototype.loadSettings
		# DESCR:*		load our settings from thier respective cookies
		# USAGE:*		Internal Function. Do Not Use.
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	// Load the last search the user did
	var str = this.readCookie('lastCaineSearch');
	if (str != null) {
		this.fromURLString(str);
	}

}

SavedSettings.prototype.saveSettings = function() {

	/* 	# %PUBLIC%
		# NAME:*		SavedSettings.prototype.saveSettings
		# DESCR:*		Save our settings to thier respective cookies
		# USAGE:*		Internal Function. Do Not Use.
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	this.createCookie('lastCaineSearch', this.toURLStringCookie(), 365);
}

SavedSettings.prototype.toURLStringCookie = function() {

	/* 	# %PUBLIC%
		# NAME:*		SavedSettings.prototype.toURLString
		# DESCR:*		returns a URL string of search parameters
		# USAGE:*		Internal Function. Do Not Use.
		# RETURNS:*		String containing the Search URL Parameters. 
		# %ENDPUBLIC% */

 	var urlString = "IKW=" + encodeURIComponent(this.IKW) + "&BeL=" + this.BeL + "&BeH=" + this.BeH + "&BaL=" + this.BaL +
					"&BaH=" + this.BaH + "&Order=" + encodeURIComponent(this.Order) + "&CID=" + this.CID +
					"&GID=" + this.GID + "&Con=" + encodeURIComponent(this.Con) + 
					"&LaL=" + this.LaL + "&LaH=" + this.LaH + "&FaL=" + this.FaL + "&FaH=" + this.FaH + "&PTr=" + this.PTr +
					"&PaL=" + this.PaL + "&PaH=" + this.PaH + "&LaL=" + this.LaL + "&LaH=" + this.LaH + "&FaL=" + this.FaL + "&FaH=" + this.FaH + 
					"&Fur=" + this.Fur + "&ExSold=" + this.ExSold + "&ExAuct=" + this.ExAuct + 
					"&OFI=" + this.OFI;
                    
    urlString += "&PrL=" + this.PrL + "&PrH=" + this.PrH;
	
	for(var i = 0; i < this.Sub.length; i++) {
 		urlString += "&Sub=" + this.Sub[i];
	}

	
	for(var i = 0; i < this.PT.length; i++) {
 		urlString += "&PT=" + this.PT[i];
	}

	for(var i = 0; i < this.CS.length; i++) {
 		urlString += "&CS=" + this.CS[i];
	}

	return urlString;
}

SavedSettings.prototype.toURLString = function() {

	/* 	# %PUBLIC%
		# NAME:*		SavedSettings.prototype.toURLString
		# DESCR:*		returns a URL string of search parameters
		# USAGE:*		Internal Function. Do Not Use.
		# RETURNS:*		String containing the Search URL Parameters. 
		# %ENDPUBLIC% */

 	var urlString = "IKW=" + encodeURIComponent(this.IKW) + "&BeL=" + this.BeL + "&BeH=" + this.BeH + "&BaL=" + this.BaL +
					"&BaH=" + this.BaH + "&Order=" + encodeURIComponent(this.Order) + "&CID=" + this.CID +
					"&GID=" + this.GID + "&Con=" + encodeURIComponent(this.Con) + 
					"&LaL=" + this.LaL + "&LaH=" + this.LaH + "&FaL=" + this.FaL + "&FaH=" + this.FaH + "&PTr=" + this.PTr +
					"&PaL=" + this.PaL + "&PaH=" + this.PaH + "&LaL=" + this.LaL + "&LaH=" + this.LaH + "&FaL=" + this.FaL + "&FaH=" + this.FaH +  
					"&Fur=" + this.Fur + "&ExSold=" + this.ExSold + "&ExAuct=" + this.ExAuct + 
					"&OFI=" + this.OFI;
                    
    urlString += "&PrL=" + this.PrL + "&PrH=" + this.PrH;
	
	urlString += "&Sub=";
	
	for(var i = 0; i < this.Sub.length; i++) {
 		urlString += this.Sub[i]+",";
	}
	urlString = urlString.substring(0,urlString.length-1);
	
	for(var i = 0; i < this.PT.length; i++) {
 		urlString += "&PT=" + this.PT[i];
	}

	for(var i = 0; i < this.CS.length; i++) {
 		urlString += "&CS=" + this.CS[i];
	}

	return urlString;
}

SavedSettings.prototype.fromURLString = function(URLString) {

	/* 	# %PUBLIC%
		# NAME:*		SavedSettings.prototype.fromURLString
		# DESCR:*		Parses a URL search string and sets the
		# :*			SavedSettings Object Constants with the values
		# :*			passed. Used for decoding the 'lastSearch'
		# :*			cookie string.
		# USAGE:*		Internal Function. Do Not Use.
		# RETURNS:*		String containing the Search URL Parameters. 
		# %ENDPUBLIC% */

	var params = URLString.split('&');
	for(var i = 0; i < params.length; i++) {
		var str = params[i];
		var name = str.substring(0, str.indexOf('='));
		var value = str.substring(str.indexOf('=') + 1, str.length);
		if (value != '') {
        
			switch(name) {

				case 'IKW':
					this.IKW = decodeURIComponent(value);
					break;

				case 'Sub':
					SubArray = new Array();
					SubArray = value.split(",");
					this.Sub.push(SubArray);
					break;

				case 'BeL':
					this.BeL = value;
					break;

				case 'BeH':
					this.BeH = value;
					break;

				case 'BaL':
					this.BaL = value;
					break;

				case 'BaH':
					this.BaH = value;
					break;

				case 'PrL':
					this.PrL = value;
					break;

				case 'PrH':
					this.PrH = value;
					break;

				case 'PaL':
					this.PaL = value;
					break;

				case 'PaH':
					this.PaH = value;
					break;

				case 'LaL':
					this.LaL = value;
					break;

				case 'LaH':
					this.LaH = value;
					break;

				case 'FaL':
					this.FaL = value;
					break;

				case 'FaH':
					this.FaH = value;
					break;

				case 'Order':
					this.Order = decodeURIComponent(value);
					break;

				case 'CID':
					this.CID = value;
					break;

				case 'GID':
					this.GID = value;
					break;
					
				case 'LaL':
					this.LaL = value;
					break;

				case 'LaH':
					this.LaH = value;
					break;

				case 'FaL':
					this.FaL = value;
					break;

				case 'FaH':
					this.FaH = value;
					break;

				case 'PTr':
					this.PTr = value;
					break;

				case 'PT':
					this.PT.push(value);
					break;

				case 'CS':
					this.CS.push(value);
					break;

				case 'Con':
					this.Con = decodeURIComponent(value);
					break;

				case 'Fur':
					this.Fur = value;
					break;

				case 'ExSold':
					this.ExSold = value;
					break;

				case 'ExAuct':
					this.ExAuct = value;
					break;
					
				case 'OFI':
					this.OFI = value;
					break;

				default:
					break;
			}
		}
	}
}

/* SavedSettings cookie helpers */

SavedSettings.prototype.createCookie = function(name, value, expiredays) {

	/* 	# %PUBLIC%
		# NAME:*		SavedSettings.prototype.createCookie
		# DESCR:*		Create a Cookie with the values supplied.
		# USAGE:*		this.savedSettings = new SavedSettings();
		# :*			this.createCookie('lastSearch', urlstring, ExpireDays);
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	if (expiredays) {
		var date = new Date();
		date.setTime(date.getTime() + (expiredays * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name + "=" + escape(value) + expires + "; path=/";
}

SavedSettings.prototype.readCookie = function(name) {

	/* 	# %PUBLIC%
		# NAME:*		SavedSettings.prototype.readCookie
		# DESCR:*		Read the contents of the named cookie
		# USAGE:*		this.savedSettings = new SavedSettings();
		# :*			var somestring = this.readCookie('lastSearch');
		# RETURNS:*		null or contents of the Cookie. 
		# %ENDPUBLIC% */

	var nameEQ = name + "=";
	var CookieArray = document.cookie.split(';');

	for(var i=0; i < CookieArray.length; i++) {
		var c = CookieArray[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) { 
			return unescape(c.substring(nameEQ.length, c.length));
		}
	}

	return null;
}

SavedSettings.prototype.eraseCookie = function(name) {

	/* 	# %PUBLIC%
		# NAME:*		SavedSettings.prototype.eraseCookie
		# DESCR:*		Erase A Cookie. 
		# :*			name - name of the cookie to erase.
		# USAGE:*		this.savedSettings = new SavedSettings();
		# :*			this.eraseCookie('lastSearch');
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	this.createCookie(name, "", -1);
}

/* General non Class / object Functions */

function setRadioGroupValue(formName, radioGroupName, newValue) {

	/* 	# %PUBLIC%
		# NAME:*		setRadioGroupValue
		# DESCR:*		Given a form name, radio button group name
		# :*			and a radio button group value, Set the
		# :*			with the paramaters passed.
		# USAGE:*		setRadioGroupValue('nameOfForm', 'nameOfRadioGroup', 'someValue');
		# RETURNS:*		True if the value was found and checked in the radio button group,
		# :*			false if the value is not in the radio buttton group. 
		# %ENDPUBLIC% */

	if (!radioGroupName && !formName && !newValue) {
		return false;
	}

	var radioGroup = document.forms[formName].elements[radioGroupName];
	var radioOptionFound = false;

	for(var i = 0; i < radioGroup.length; i++) {
		radioGroup[i].checked = false;
		if (radioGroup[i].value == newValue.toString()) {
			radioGroup[i].checked = true;
			radioOptionFound = true;
		}
	}

	return radioOptionFound;
}

function obtainRadioGroupValue(formName, radioGroupName) {

	/* 	# %PUBLIC%
		# NAME:*		obtainRadioGroupValue
		# DESCR:*		Given a form name, radio button group name, obtain the
		# :*			value of the selected Radio button.			
		# USAGE:*		obtainRadioGroupValue('nameOfForm', 'nameOfRadioGroup');
		# RETURNS:*		zero(0) no form name and radio group name is passed or
		# :*			the value of the selected button in radio buttton group is returned. 
		# %ENDPUBLIC% */

	if (!radioGroupName && !formName) {
		return 0;
	}

	var radioGroup = document.forms[formName].elements[radioGroupName];

	var radioGroupValue = "";
	for(var i = 0; i < radioGroup.length; i++) {

		if (radioGroup[i].checked) {
			radioGroupValue = radioGroup[i].value
		}
	}

	return radioGroupValue;
}

function removeTextFromTextField(FieldID) {
	/* 	# %PUBLIC%
		# NAME:*		removeTextFromTextField
		# DESCR:*		Given an ID of a text input field, makes it value blank.			
		# USAGE:*		removeTextFromTextField(FieldID);
		# RETURNS:*		Nothing. If the FieldID is not null / blank it's value will become "". 
		# %ENDPUBLIC% */

	if (FieldID) {
		$(FieldID).value = "";
	}

}

function findNodeById(id, nodeArray){

	/*	# %PUBLIC%
		# NAME:*		findNodeById
		# DESCR:*		search in an array of nodes for the one with the passed id 
		# USAGE:*		var results = findNodeById('results', nodes);
		# :*			id : string, the id of the node to find
		# :*			nodeArray : array of nodes
		# RETURNS:*		Returns node if found or false if not
		# %ENDPUBLIC% */

	for(var i = 0; i < nodeArray.length; i++) {
		
			if (nodeArray[i].id == id) {
				return nodeArray[i];
			}
	}

	return false;
}

//##################### STAMP DUTY ##########################

function stampCalcSD(frm) {
	var num = frm.num.value;
	if (num == "") {window.alert("You need to enter a dollar value!"); num=0; }
	// Now do Victoria
    frm.vicSD.value = 0;
	if (num <= 25000) {var CalcSD = eval(((num)/100)*1.4);}
	else if ((num > 25000) && (num <= 130000)) {var CalcSD = eval((((num - 25000)/100)*2.4) + 350);}
	else if ((num > 130000) && (num <= 440000)) {var CalcSD = eval((((num - 130000)/100)*5) + 2870);}
	else if ((num > 440000) && (num <= 550000)) {var CalcSD = eval((((num - 440000)/100)*6) + 18370);}
	else if ((num > 550000) && (num <= 960000)) {var CalcSD = eval((((num - 550000)/100)*6) + 28070);}
	else {var CalcSD = eval(((num)/100)*5.5);}
    frm.vicSD.value = CalcSD;
    frm.vicSD.value = addc(cents(frm.vicSD.value));
}

function addc(i)
{
  if (i.length >= 10 && i.length <= 12) {	i = (i.substring(0,i.length-9) + "," + i.substring(i.length-9,i.length-6) + "," + i.substring(i.length-6,i.length)); }
  else if (i.length >= 7 && i.length <= 9) { i = (i.substring(0,i.length-6) + "," + (i.substring(i.length-6,i.length))); }
  return i;
}

function cents(i)
{
var d = Math.floor(i);
	var tot = Math.round(i*100).toString();
	return (tot.substring(0,tot.length-2) + "." + tot.substring(tot.length-2,tot.length))
}

//##################### MORTGAGE ##########################

function floor(number)
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function dosum(f)
{
  var mi = f.IR.value / 1200;
  var base = 1;
  var mbase = 1 + mi;
  for (i=0; i<f.YR.value * 12; i++)
  {
    base = base * mbase
  }
  var mp = floor(f.LA.value * mi / ( 1 - (1/base)))
  var zer="";
  if ((mp*100) % 10 == 0) { zer="0"; }
  if ((mp*10) % 10 == 0) { zer=".00"; }
  f.MP.value = "$" + mp + zer

}

function newPage(val)
{
	document.NextForm.p.value=val;
	document.NextForm.action="results.php";
	document.NextForm.submit();
}

function newSoldPage(val)
{
	document.NextForm.p.value=val;
	document.NextForm.action="results-sold.php";
	document.NextForm.submit();
}

function formatSuburbs() {
var d = document.sForm
var i
var temp			// Boolean for a region selected
	if(d.Suburb.options[0].selected) {
		d.Sub.value=""
	}
	else
	{
		temp="";
		d.Sub.value=""
		for (i=1; i<=(d.Suburb.length-1); i++) {
  			if(d.Suburb.options[i].selected) {
					temp=d.Sub.value;
					if (temp!="") {d.Sub.value=d.Suburb.options[i].text+", "+temp}
					else {d.Sub.value=d.Suburb.options[i].text}
			}
		}
	}

	return true;
}

function checkSuburb() {
var d = document.sForm
var i
	if(d.Suburb.options[0].selected) {
		for (i=1; i<=(d.Suburb.length-1); i++) {
			d.Suburb.options[i].selected = false
		}
		d.Suburb.options[0].selected = true;
	}
}

/*!
 * Simplest jQuery Slideshow Plugin – http://github.com/mathiasbynens/Simplest-jQuery-Slideshow
 * Script by Jonathan Snook – http://snook.ca/archives/javascript/simplest-jquery-slideshow
 * Pluginified by Mathias Bynens – http://mathiasbynens.be/
 */
(function(a){a.fn.slideshow=function(b){a.extend({timeout:3e3,speed:400},b);return this.each(function(){var c=a(this);c.children().eq(0).appendTo(c).show();setInterval(function(){c.children().eq(0).hide().appendTo(c).fadeIn(b.speed)},b.timeout)})}})(jQuery);
