(function($){
	var className="DefaultText";
	function DefaultText(target){
		if(target) {
			this.init(target);
			if(!this.target.data("init")) 
				this.target.data("init",[]);
			this.target.data("init").push(function(e){
				new DefaultText(e);
			});
		}
	}
	DefaultText.prototype.init=function(target){
		this.className=className;
		this.target=$(target).data(className,this);
		this.defaultText=""+this.target.attr("rel");
		if(this.isDefault()) 
			this.setDefault();
		else 
			this.setNormal();
		this.seed=Math.round(Math.random()*10000);
		this.target.addClass(className+this.seed)
		.closest("form").submit(function(){
			$("input:text",this).each(function(){
				if(typeof $(this).data(className)!="undefined" 
				&& $(this).data(className).isDefault()) 
					$(this).data(className)
					.clear();
			})
		})
	}
	DefaultText.prototype.setDefault=function(){
		this.target
		.css({color:"#AAA"})
		.val(this.defaultText);
	};
	DefaultText.prototype.setNormal=function(){
		this.target
		.css({color:"#000"});
	};
	DefaultText.prototype.clear=function(){
		if(this.target.attr("value")==this.target.attr("rel"))
			this.target
			.css({color:"#000"})
			.attr("value","")
	};
	DefaultText.prototype.isDefault=function(){
		return (this.target.attr("value")==this.target.attr("rel") 
			|| this.target.attr("value")=="")
	};
	DefaultText.prototype.live=function(){
		if (!this.goLive){
			$("."+className+this.seed)
			.live("click",function(){
				if(!$(this).data(className)) new DefaultText(this);
				$(this)
				.unbind("blur")
				.blur(function(){
					if($(this).data(className).isDefault()) 
						$(this).data(className)
						.setDefault();
					else 
						$(this).data(className)
						.setNormal();
				})
				.data(className)
				.clear();
			}).live("keydown",function(){
				if(!$(this).data(className)) new DefaultText(this);
				$(this)
				.unbind("blur")
				.blur(function(){
					if($(this).data(className).isDefault()) 
						$(this).data(className)
						.setDefault();
					else 
						$(this).data(className)
						.setNormal();
				})
				.data(className)
				.clear();
			});
			this.goLive=true;
		}
		return this
	};
	$.fn.addDefaultText=function(){
		this.each(function(){
			new DefaultText(this).live();
		});
		return this
	};
})(jQuery);

/* 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(page) {

	/*	# %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();
	if(page) {
		var url = "/" + page + "/results.php?" + searchParams;
	} else {
		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.Order = "price";		// 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
	this.Street = "";			// Street Name
	this.Suburb = "";	// Suburb

	// 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('lastSearch');
	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('lastSearch', this.toURLString(), 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)+ "&Street=" + encodeURIComponent(this.Street) + "&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 + 
					"&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) + "&Street=" + encodeURIComponent(this.Street) + "&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 + 
					"&Fur=" + this.Fur + "&ExSold=" + this.ExSold + "&ExAuct=" + this.ExAuct + 
					"&OFI=" + this.OFI;
                    
    urlString += "&PrL=" + this.PrL + "&PrH=" + this.PrH;
	
	urlString += "&Sub=";
	
	if(this.Suburb!="") {
		urlString += this.Suburb;
	} else {
		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 'Order':
					this.Order = decodeURIComponent(value);
					break;
					
				case 'Street':
					this.Street = 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;
}

var type="";
function checkSuburb(val) {
	type = val;
	$.ajax({ type: "GET", cache: "false", url: "/getsuburbs.php", data: "type="+type, success: function(content){ $("#qs-suburb").html(content); } });
	$.ajax({ type: "GET", cache: "false", url: "/price.php", data: "type="+type, success: function(content){ $("#qs-price").html(content); } });
	$.ajax({ type: "GET", cache: "false", url: "/getpt.php", data: "type="+type, success: function(content){ $("#qs-proptype").html(content); } });
};

function setValues(val) {
	$("#Con").val(val);
	if(val == "S") {
		$("#page").val("buying");
		$("#order").val("price");
	} else {
		$("#page").val("renting");
		$("#order").val("rent");
	}
	checkSuburb(val);
};

function checkPT() {
var d = document.sForm
var i
	if (d.PT.selectedIndex==0) {						// All selected
		for (i=1; i<d.PT.length; i++) {
			d.PT.options[i].selected = true
		}
		d.PT.options[0].selected = false
	}
}

function checkOID() {
	check = $("#OID").val();
	if(check == "Street Name or Property ID") {
		check = "";	
	}
	if(isNaN(parseInt(check))) {
		$("#OID").val("");
		$("#streetname").val(check);
		PortalApp.saveSearch('searchAdvForm');
		PortalApp.obtainSearchResults();
	} else {
		$("#streetname").val("");
		var url = "/view/?OID=" + check;
		window.location = url;
	}
}

function sortFetchSearchResults() {
		PortalApp.savedSettings.Order = $('#sortOrder').val();
		PortalApp.savedSettings.saveSettings();
		PortalApp.obtainSearchResults();
		return false;
}

$(document).ready(function(){
    PortalApp = new PortalApplication();
	PortalApp.updateSortControl('#sortOrder');
	$(".useDefault").addDefaultText();
	$("#streetname").val("");
	$("#qs-buy").attr("checked", true);
	$("#qs-rent").attr("checked", false); 
	setValues('S');
	$("ul.sf-menu").supersubs({ 
            minWidth:    13,
            maxWidth:    30, 
            extraWidth:  1 
        }).superfish({animation:{opacity:'show',height:'show'},autoArrows:false,speed: 'fast'});
	
	var htmlStr = $('#replace-name').html();
	if(htmlStr) {
	var htmlStrArray = htmlStr.split(" ",1);
	$('#replace-name').html(htmlStrArray[0]);
	}
	$("#qs-buy-link").click(function () { 
			$('#qs-buy-link').addClass('active'); 
			$('#qs-rent-link').removeClass('active');
			return false;
	});
	$("#qs-rent-link").click(function () { 
			$('#qs-rent-link').addClass('active'); 
			$('#qs-buy-link').removeClass('active');
			return false;
	});
	
	$("#appraisalForm").validate({
		rules: {
			firstname: "required",
			surname: "required",
			property_address: {
				required: true
			},
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			firstname: "Please enter your firstname",
			surname: "Please enter your lastname",
			property_address: {
				required: "Please enter the property address"
				},
			email: "Please enter a valid email address"
		}
	});
	$("#contactForm").validate({
		rules: {
			name: "required",
			comments: "required",
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			name: "Please enter your name",
			comments: "Please enter a comment",
			email: "Please enter a valid email address"
		}
	});
	$("#repairForm").validate({
		rules: {
			fullname: "required",
			property_address: "required",
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			fullname: "Please enter your name",
			property_address: "Please enter the address",
			email: "Please enter a valid email address"
		}
	});
});						   