/* PortalApplication Object / Class */

function PortalApplication(change_setting_for) {

	/* 	# %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('competition');
		# :*			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;

	this.bookmarkedProperties = new Object();
	this.bookmarkCount = 0;

	this.viewedProperties = new Object();
	this.viewedCount = 0;

	this.loadBookmarkedProperties();
	this.loadViewedProperties();

 	this.ButtonSettings = {
			tl: {radius: 5},
			tr: {radius: 5},
			bl: {radius: 5},
			br: {radius: 5},
			antiAlias: true,
			autoPad: false
		};

	this.roundButtonCorners();
	
	// lspeel cruft load it if the javascript is available otherwise sliently fail
	try {
		esel.putObjects();

	} catch(e) {

		// sliently fail
	}

}

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 = "/portal/search?rm=search&" + searchParams;
	window.location = url;
}

PortalApplication.prototype.roundButtonCorners = function() {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.roundButtonCorners
		# DESCR:*		Find all of the <button></button> elements on the page that
		# :*			are contained within a <div class="button"></div> or
		# :*			<div class="buttonSmall"></div> or <div class="buttonLarge"></div> html
		# :*			element and round the corners of the button.
		# USAGE:*		Portalapp.roundButtonCorners();
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	var ButtonCornersObj = new curvyCorners(this.ButtonSettings, "button");
	var ButtonSmallCornersObj = new curvyCorners(this.ButtonSettings, "buttonSmall");
	var ButtonLargeCornersObj = new curvyCorners(this.ButtonSettings, "buttonLarge");
	ButtonCornersObj.applyCornersToAll();
	ButtonSmallCornersObj.applyCornersToAll();
	ButtonLargeCornersObj.applyCornersToAll();
}

PortalApplication.prototype.suburbAutocomplete = function(textControlID) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.suburbAutocomplete
		# DESCR:*		Creates new AutoComplete objects for each of the textControlIDs
		# :*			passed.			
		# USAGE:*		Portalapp.suburbAutocomplete('ressub');
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

		var AutoCompleteOptions = { script: '/portal/search?rm=suburbautocomplete&',
									valueSep: ',',
									varname: 'sub',
									maxentries: 40,
									shownoresults: false,
									minchars: 2};

		switch (textControlID) {
			case 'advsearchsub':
				AutoCompleteOptions = { script: function(subcontent) {
											var scriptname = '/portal/search?rm=suburbautocomplete';
											return scriptname + '&cs=' + $F('advcs') + '&sub=' + subcontent;
										},
										valueSep: ',',
										varname: 'sub',
										maxentries: 40,
										shownoresults: false,
										minchars: 2};
				var advSearchAutocomplete = new AutoComplete(textControlID, AutoCompleteOptions);
					advSearchAutocomplete.submit = function() { return true; };
				break;
				
			case 'reportsubone':
				AutoCompleteOptions = { script: function(subcontent) {
											var scriptname = '/portal/search?rm=suburbautocomplete';
											return scriptname + '&cs=' + $F('state') + '&sub=' + subcontent;
										},
										valueSep: ',',
										varname: 'reportsubone',
										maxentries: 40,
										shownoresults: false,
										minchars: 2};
				var reportSubOneAutocomplete = new AutoComplete(textControlID, AutoCompleteOptions);
					reportSubOneAutocomplete.submit = function() { return true; };
				break;
			
			case 'reportsubtwo':
				AutoCompleteOptions = { script: function(subcontent) {
											var scriptname = '/portal/search?rm=suburbautocomplete';
											return scriptname + '&cs=' + $F('state') + '&sub=' + subcontent;
										},
										valueSep: ',',
										varname: 'reportsubtwo',
										maxentries: 40,
										shownoresults: false,
										minchars: 2};
				var reportSubTwoAutocomplete = new AutoComplete(textControlID, AutoCompleteOptions);
					reportSubTwoAutocomplete.submit = function() { return true; };
				break;			

			case 'searchsub':
				var searchAutocomplete = new AutoComplete(textControlID, AutoCompleteOptions);
					searchAutocomplete.submit = function() { return true; };
				break;
				
			case 'auctionsearchsub1':
				var searchAutocomplete = new AutoComplete(textControlID, AutoCompleteOptions);
					searchAutocomplete.submit = function() { return true; };
				break;
			
			case 'auctionsearchsub2':
				var searchAutocomplete = new AutoComplete(textControlID, AutoCompleteOptions);
					searchAutocomplete.submit = function() { return true; };
				break;		

			case 'refinesub':
				var refineAutocomplete = new AutoComplete(textControlID, AutoCompleteOptions);
					refineAutocomplete.submit = function() { return true; };
				break;

			case 'csrefinesub':
				AutoCompleteOptions = { script: function(subcontent) {
											var scriptname = '/portal/search?rm=suburbautocomplete';
											return scriptname + '&cs=' + $F('csrefine') + '&sub=' + subcontent;
										},
										valueSep: ',',
										varname: 'sub',
										maxentries: 40,
										shownoresults: false,
										minchars: 2};

				var csRefineAutocomplete = new AutoComplete(textControlID, AutoCompleteOptions);
					csRefineAutocomplete.submit = function() { return true; };
				break;

			case 'agentsub':
				var agentAutocomplete = new AutoComplete(textControlID, AutoCompleteOptions);
					agentAutocomplete.submit = function() { return true; };
				break;

			default:
				break;

		}
}

PortalApplication.prototype.propertyDataSuburbAutocomplete = function(textControlID) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.propertyDataSuburbAutocomplete
		# DESCR:*		Creates new AutoComplete object for each of the textControlIDs
		# :*			passed.	The Property Data Auto complete behaves differently to the
		# :*			normal Portal autocomplete, so it has it's own ajax runmode	
		# USAGE:*		Portalapp.propertyDataSuburbAutocomplete('propertydatasub');
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

		var AutoCompleteOptions = { script: function() {
										var scriptname= '/portal/propertydata?rm=suburbautocomplete';
										return scriptname + '&cs=' + $F('propertydatastate') + '&sub=' + $F('propertydatasub');
									},
									//valueSep: ',',
									varname: 'sub',
									maxentries: 100,
									shownoresults: false,
									minchars: 2};

		switch (textControlID) {
			case 'propertydatasub':
				var propertyDataAutocomplete = new AutoComplete(textControlID, AutoCompleteOptions);
					propertyDataAutocomplete.submit = function() { return true; };
				break;

			default:
				break;

		}
}

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 != "rm" && name != "P" && name != "portalsection" && name != "portalview" && name != "con" && name != "ptr") {
							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['sor'];

		if (typeof(value) == 'object') {
			value = value[0];
		}
		if ($(elementID) != null){				
			for(var j = 0; j < $(elementID).length; j++) {
				if ($(elementID).options[j].value ==  value) {
					//found match so select it
					$(elementID).options[j].selected = true;
				}
			}
		}	
}

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);
		}
}

PortalApplication.prototype.updateRSSLink = function(id) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.updateRSSLink
		# DESCR:*		Given an ID of an <a href=""></a> pertaining to an RSS link,
		# :*			update the href="" to point to the parameters in the 'lastSearch'
		# :*			cookie.
		# USAGE:*		PortalApp = new PortalApplication(); PortalApp.updateRSSLink('RSSLink');
		# RETURNS:*		Returns True if update successeful, False if not. 
		# %ENDPUBLIC% */

	if (id) {
		var searchParams = this.savedSettings.toURLString();
		$(id).href = '/portal/rss?rss=1&' + searchParams;
		//alert($(id).href);
		return true;
	}

	return false;
}

PortalApplication.prototype.prettyURL = function() {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.prettyURL
		# DESCR:*		Pretty the URL's for printing. Will allow all urls without http://
		# :*			part to have a http:// part.
		# USAGE:*		PortalApp = new PortalApplication(); PortalApp.prettyURL();
		# :*			Used in the body onload="PortalApp = new PortalApplication(); PortalApp.prettyURL();"
		# :*			stage of the Portal page wrap.
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	var xurl = document.getElementsByTagName("a");

	for (i = 0; i < xurl.length; i++) {
		xurl[i].href = xurl[i].href;
	}
}

PortalApplication.prototype.loadBookmarkedProperties = function() {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.loadBookmarkedProperties
		# DESCR:*		Loads the bookmark cookie and selects the approprate property checkboxes
		# USAGE:*		Internal Function: Do Not Use.
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	// Read the bookMarked cookie
 	var savedString = this.savedSettings.readCookie('bookMarked');
 	
 	if(savedString != null) {
 		// If there is a cookie 
 		var BookmarkedArray = savedString.split(',');//split cookie string in to array
 		for(var i = 0; i < BookmarkedArray.length; i++) {
 			if(BookmarkedArray[i]!="") {
 				// if a value, create a new bookmark
 				this.bookmarkedProperties[BookmarkedArray[i]] = new BookmarkedProperties(BookmarkedArray[i]);
				// Increment count
 				this.bookmarkCount++;
 			}
 		}
		// Select the approprite checkboxes if they exist on the page.
		this.selectBookmarkedProperties();
	}

 	if ($('bookmarkCount') != undefined) {
		if (this.bookmarkCount) {
	 		$('bookmarkCount').innerHTML = this.bookmarkCount;
		} else {
	 		$('bookmarkCount').innerHTML = "0";
		}
 	}
}

PortalApplication.prototype.selectBookmarkedProperties = function() {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.selectBookmarkedProperties
		# DESCR:*		Selects the appropriate property checkboxes in the bookmarkedProperties object.
		# USAGE:*		Internal Function: Do Not Use.
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	// Select the approprite checkboxes if they exist on the page.
	for(bookmark in this.bookmarkedProperties) {
		if(document.forms['propertyResults']) {
			// if the properties are listed update selections
			// Debug
			//alert(bookmark);
			if ($('OID_' + this.bookmarkedProperties[bookmark])) {
				$('OID_' + this.bookmarkedProperties[bookmark]).checked = true;
			}
		}
	}
}

PortalApplication.prototype.bookmarkProperty = function(e, orderid) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.bookmarkProperty
		# DESCR:*		Update the bookmarkCount, Display, highlight popup and right column tool
		# :*			and add the OrderID to the bookmarked cookie.
		# USAGE:*		PortalApp = new PortalApplication(); PortalApp.bookmarkProperty(e, orderid);
		# :*			Used in the Search Results display and the corresponding check box for each
		# :*			property displayed.
		# :*			e: event
		# :*			orderID: OrderID of property to bookmark
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */


	var displayString = "";
	var targ;
	if (!e) {
		var e = window.event;
	}

	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}

	// add or removed if checked or not
	if(targ.checked) {
		this.bookmarkedProperties[orderid] = new BookmarkedProperties(orderid);
		this.bookmarkCount++;
     
        if (orderid) {		
            var ts = '&ts=' + new Date().getTime();
            new Ajax.Request('/portal/shortlisthits?OID='+orderid+ts, {
    			method: 'get',
  	    		onSuccess: function(transport) {
      		   	}
		    });
	    }
	} else {
		this.bookmarkedProperties[orderid] = null;
		this.bookmarkCount--;
		if (this.bookmarkCount < 0) {
			this.bookmarkCount = 0;
		}
	}

	var saveString = this.bookmarkToString();
	this.savedSettings.createCookie('bookMarked', saveString);
	
	displayString = this.bookmarkCount + '<br>Shortlisted Properties' ;

	infoBox(displayString, 'oid_' + orderid + '_' + orderid);

	$('bookmarkCount').innerHTML = this.bookmarkCount;
	new Effect.Highlight('bookmarkHighlight',{startcolor:'#333333', queue: {scope: 'highlight', position: 'end'}});
}

PortalApplication.prototype.bookmarkToString = function(url) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.bookmarkToString
		# DESCR:*		Turns bookmarks in to a string.
		# :*			Pass true for a url parameter string
		# :*			else return a comma seperated string
		# USAGE:*		Internal Function. Do Not Used.
		# RETURNS:*		String containg bookmarked properties (OrderID).
		# %ENDPUBLIC% */

	var BookmarkString = "";
 	for(bookmark in this.bookmarkedProperties) {
		if (this.bookmarkedProperties[bookmark] != null) {
			if (url) {
				// Create a url parameter string.
				BookmarkString += "&OID=" + this.bookmarkedProperties[bookmark].toString();
			} else {
				// Create a comma seperated string.
				BookmarkString += this.bookmarkedProperties[bookmark].toString() + ",";
			}
		}
	}

	return BookmarkString;
}

PortalApplication.prototype.loadViewedProperties = function() {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.loadViewedProperties
		# DESCR:*		Loads the contents of the "viewedProperties"
		# :*			cookie and sets the display count of view properties
		# USAGE:*		Internal Function. Do Not Used.
		# RETURNS:*		Nothing
		# %ENDPUBLIC% */

	// Read the users "viewedProperties" cookie
	var savedString = this.savedSettings.readCookie('viewedProperties');
	
	if(savedString != null) { 
		// If there is a cookie, split the cookie string in to array
		var viewedPropertiesArray = savedString.split(',');
		for(var i = 0; i < viewedPropertiesArray.length; i++) {
			if (viewedPropertiesArray[i] != "") {
				// If a value, create a new bookmark
				this.viewedProperties[viewedPropertiesArray[i]] = new BookmarkedProperties(viewedPropertiesArray[i]);
				// Increment count
				this.viewedCount++;
			}
		}

		if($('viewedCount') != undefined) {
		 	$('viewedCount').innerHTML = this.viewedCount;
		}
	}
}

PortalApplication.prototype.viewProperty = function(orderid) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.viewProperty
		# DESCR:*		Adds / removes a property to our viewedProperties cookie
		# :*			orderid: OrderID of the property being viewed.
		# USAGE:*		PortalApp = new PortalApplication(); PortalApp.viewProperty(orderid);
		# RETURNS:*		Nothing.
		# %ENDPUBLIC% */

	this.viewedProperties[orderid] = new BookmarkedProperties(orderid);

	var viewedString = this.viewedToString();

	// Save our viewed properties to the cookie.
	this.savedSettings.createCookie('viewedProperties', viewedString);
}

PortalApplication.prototype.viewedToString = function(url) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.viewedToString
		# DESCR:*		Turns viewed properties in to a string.
		# :*			Pass true for a url parameter string
		# :*			else return a comma seperated string.
		# USAGE:*		Internal Function. Do Not Use.
		# RETURNS:*		Nothing.
		# %ENDPUBLIC% */

	var viewedString = "";
	for(bookmark in this.viewedProperties) {
		if (this.viewedProperties[bookmark]!= null) {
			if (url) {
				// Create a url parameter string.
				viewedString += "&OID=" + this.viewedProperties[bookmark].toString();
			} else {
				// Create a comma seperated string.
				viewedString += this.viewedProperties[bookmark].toString() + ",";
			}
		}
	}
	
	return viewedString;
}

PortalApplication.prototype.updateOIDcookie = function(e, cookieName, orderid) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.updateOIDcookie
		# DESCR:*		Removes a property OrderID from the cookie name passed
		# :*			and displays an information overlay box.
		# USAGE:*		PortalApp = new PortalApplication(); PortalApp.updateOIDcookie(e, 'bookMarked', orderid);
		# :*			e: event
		# :*			cookieName: Name of cookie to update. Valid cookie names - 'viewedProperties', 'BookMarked'
		# :*			orderid: OrderID to remove
		# RETURNS:*		Nothing.
		# %ENDPUBLIC% */

	var displayString = "";
	if (cookieName && orderid) {

		displayString = 'Property removed from Shortlist';
		var saveString;

		switch (cookieName) {
			case 'viewedProperties':
				this.viewedProperties[orderid] = null;
				this.viewedCount--;
				saveString = this.viewedToString();
				this.savedSettings.createCookie('viewedProperties', saveString);
				break;

			case 'bookMarked':
		 		this.bookmarkedProperties[orderid] = null;
				this.bookmarkCount--;
				saveString = this.bookmarkToString();
				this.savedSettings.createCookie('bookMarked', saveString);
				break;

			default:
				displayString = 'Invalid cookie name passed';
				break;
		}
	}

	infoBox(displayString);
}

PortalApplication.prototype.clearHistory = function(cookieName) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.clearHistory
		# DESCR:*		Removes the cookie contents form the cookie name passed and updates the PortalApp
		# :*			object to set the Count associated with the cookie name to zero.
		# USAGE:*		PortalApp = new PortalApplication(); PortalApp.clearHistory(e, 'bookMarked');
		# :*			cookieName: Name of cookie to update. Valid cookie names - 'viewedProperties', 'BookMarked'
		# RETURNS:*		Nothing.
		# %ENDPUBLIC% */

	var displayString = "Invalid cookie name passed";
	if (cookieName) {

		switch (cookieName) {
			case 'viewedProperties':
				this.viewedProperties = new Object();;
				this.viewedCount = 0;
				saveString = this.viewedToString();
				this.savedSettings.eraseCookie('viewedProperties');
				displayString = 'My Last Viewed History cleared';
				if($('viewedCount') != undefined) {
		 			$('viewedCount').innerHTML = this.viewedCount;
				}
				break;

			case 'bookMarked':

				// De-Select the approprite checkboxes if they exist on the page.
				for(bookmark in this.bookmarkedProperties) {
					if(document.forms['propertyResults']) {
						// debug
						//alert(bookmark);
						// if the properties are listed and checked, update selections
						if ($('OID_' + bookmark)) {
							if($('OID_' + bookmark).checked == true) {
							// if this property is on the page, uncheck it
								$('OID_' + bookmark).checked = false;
							}
						}
					}
				}

		 		this.bookmarkedProperties = new Object();;
				this.bookmarkCount = 0;
				saveString = this.bookmarkToString();
				this.savedSettings.eraseCookie('bookMarked');
				displayString = 'Shortlisted Property history cleared';
				if ($('bookmarkCount') != undefined) {
					$('bookmarkCount').innerHTML = this.bookmarkCount;
				}
				break;

			default:
				displayString = 'Invalid cookie name passed';
				break;
		}
	}

	infoBox(displayString);
}

/* 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.kw = ""; 				// Keyword
	this.pt = new Array();		// Property Type
	this.cs = new Array();		// CityState location
	this.rg = new Array();		// Area regions
	this.sub = "";				// Suburb Name / Post Code / ID
	this.mun = 0;				// Municipality
	this.street = "";			// Street name
	this.sur = "";				// surrounding suburbs
	this.bel = 0;				// Bedroom Low
	this.beh = 0;				// Bedroom High
	this.bal = 0;				// Bathroom Low
	this.bah = 0;				// Bathroom High
	this.prl = 0;				// Price Low
	this.prh = 0;				// Price High
	this.pal = 0;				// Parking Low
	this.pah = 0;				// Parking High
	this.rprl = 0;				// Rent Price Low
	this.rprh = 0;				// Rent Price High
	this.sor = "";				// Sorting
	this.CID = 0;				// ClientID for Agent
	this.GID = 0;				// GroupID for Agent
	this.ybl = 0;				// Year Built Low
	this.ybh = 0;				// Year Built 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.con = "";				// Contract Type
	this.ptr = "r";				// Propert Type range
	this.rgr = "";				// Region Range
	this.portalview = "residential";	// Portal View
	this.portalsection = "buy";			// Portal Section.
	this.cat = 0; 				// Business Category
	this.subcat = new Array();			// Business Sub Category 
    
    //for quick search
    this.quicksearch = 0;
    
	// Options that appear on the advanced search pages.
	this.fur = 0;				// Furnished listings
	this.exsold = 0;			// Sold listings
	this.exauct = 0;			// Auction Listings
	this.exCID = 0;				// Exclude CID's
	this.ofi = 0;				// Listings without OFI times
	this.ls = 0;				// Life Style
	this.grl = 0;				// Golf and Resort Living
	this.ac = 0;				// Aged care

	// Options that appear on the Rental advanced search page
	this.stl = 0;				// Properties with Short Term Lease

	// Green options that appear on the advanced search pages.
	this.green = 0;			// green search
	this.OF70 = 0;			// Passive Climate Design
	this.OF71 = 0;			// Solar Power
	this.OF72 = 0;			// Double Glazing
	this.OF73 = 0;			// Eco Materials
	this.OF74 = 0;			// Wind Power
	this.OF75 = 0;			// Grey Water
	this.OF76 = 0;			// Water Tanks
	this.OF77 = 0;			// Solar Hot Water
	this.OF78 = 0;			// Energy Rated Appliances
	this.OF79 = 0;			// Alternative Energy
	this.OF80 = 0;			// Organic Building Materials
	this.OF81 = 0;			// High Insulation Rating
	
	// 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.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 = "kw=" + encodeURIComponent(this.kw) + "&sub=" + encodeURIComponent(this.sub) +
 					"&street=" + encodeURIComponent(this.street) + "&sur=" + encodeURIComponent(this.sur) +
					"&mun=" + this.mun + "&bel=" + this.bel + "&beh=" + this.beh + "&bal=" + this.bal + "&cat=" + this.cat +
					"&bah=" + this.bah + "&sor=" + encodeURIComponent(this.sor) + "&CID=" + this.CID +
					"&GID=" + this.GID + "&con=" + encodeURIComponent(this.con) + "&ybl=" + this.ybl + "&ybh=" + this.ybh + 
					"&lal=" + this.lal + "&lah=" + this.lah + "&fal=" + this.fal + "&fah=" + this.fah + "&ptr=" + this.ptr +
					"&pal=" + this.pal + "&pah=" + this.pah + "&rgr=" + this.rgr + 
					"&portalview=" + this.portalview + "&portalsection=" + this.portalsection +
					"&fur=" + this.fur + "&exsold=" + this.exsold + "&exauct=" + this.exauct + "&exCID=" + this.exCID + 
					"&ofi=" + this.ofi + "&ls=" + this.ls + "&grl=" + this.grl + "&ac=" + this.ac + "&stl=" + this.stl + 
					"&green=" + this.green + "&OF70=" + this.OF70 + "&OF71=" + this.OF71 + "&OF72=" + this.OF72 + 
					"&OF73=" + this.OF73 + "&OF74=" + this.OF74 + "&OF75=" + this.OF75 + "&OF76=" + this.OF76 + 
					"&OF77=" + this.OF77 + "&OF78=" + this.OF78 + "&OF79=" + this.OF79 + "&OF80=" + this.OF80 + 
					"&OF81=" + this.OF81;
                    
    if (this.quicksearch !=0) {
        urlString += "&quicksearch=1";
        urlString += "&bs=" + 50;
    }

	if (this.con == 'S') {
		urlString += "&prl=" + this.prl + "&prh=" + this.prh;
	} else {
		urlString += "&rprl=" + this.rprl + "&rprh=" + this.rprh;
	}

	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];
	}

	for(var i = 0; i < this.rg.length; i++) {
 		urlString += "&rg=" + this.rg[i];
	}
	
	for(var i = 0; i < this.subcat.length; i++) {
 		urlString += "&subcat=" + this.subcat[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 'kw':
					this.kw = decodeURIComponent(value);
					break;
                
                case 'quicksearch':
                    this.quicksearch = decodeURIComponent(value);
                    break;
                    
                case 'street':
					this.street = decodeURIComponent(value);
					break;

				case 'sub':
					this.sub = decodeURIComponent(value);
					break;

				case 'sur':
					this.sur = decodeURIComponent(value);
					break;

				case 'rgr':
					this.rgr = value;
					break;

				case 'mun':
					this.mun = value;
					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 'rprl':
					this.rprl = value;
					break;

				case 'rprh':
					this.rprh = value;
					break;

				case 'sor':
					this.sor = decodeURIComponent(value);
					break;

				case 'CID':
					this.CID = value;
					break;

				case 'GID':
					this.GID = value;
					break;
					
				case 'ybl':
					this.ybl = value;
					break;

				case 'ybh':
					this.ybh = 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 'rg':
					this.rg.push(value);
					break;

				case 'con':
					this.con = decodeURIComponent(value);
					break;

				case 'portalview':
					this.portalview = value;
					break;

				case 'portalsection':
					this.portalsection = value;
					break;

				case 'fur':
					this.fur = value;
					break;

				case 'exsold':
					this.exsold = value;
					break;

				case 'exauct':
					this.exauct = value;
					break;
					
				case 'exCID':
					this.exCID = value;
					break;
					
				case 'ofi':
					this.ofi = value;
					break;

				case 'ls':
					this.ls = value;
					break;

				case 'grl':
					this.grl = value;
					break;

				case 'ac':
					this.ac = value;
					break;

				case 'stl':
					this.stl = value;
					break;

				case 'green':
					this.green = value;
					break;

				case 'OF70':
					this.OF70 = value;
					break;

				case 'OF71':
					this.OF71 = value;
					break;

				case 'OF72':
					this.OF72 = value;
					break;

				case 'OF73':
					this.OF73 = value;
					break;

				case 'OF74':
					this.OF74 = value;
					break;

				case 'OF75':
					this.OF75 = value;
					break;

				case 'OF76':
					this.OF76 = value;
					break;

				case 'OF77':
					this.OF77 = value;
					break;

				case 'OF78':
					this.OF78 = value;
					break;

				case 'OF79':
					this.OF79 = value;
					break;

				case 'OF80':
					this.OF80 = value;
					break;

				case 'OF81':
					this.OF81 = value;
					break;
					
				case 'cat':
					this.cat = value;
					break;
					
				case 'subcat':
					this.subcat.push(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);
}


/* BookmarkedProperties Object / Class */

function BookmarkedProperties(orderid) {

	/* 	# %PUBLIC%
		# NAME:*		BookmarkedProperties
		# DESCR:*		Class object for storing a bookmarked property (OrderID) 
		# USAGE:*		Internal Function. Do Not Used.
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	this.PropertyID = orderid;
}

BookmarkedProperties.prototype.toString = function() {

	/* 	# %PUBLIC%
		# NAME:*		BookmarkedProperties.prototype.toString
		# DESCR:*		Returns saved bookemarked property (OrderID).
		# USAGE:*		Internal Function. Do Not Used.
		# RETURNS:*		OrderID or Array of OrderIDs. 
		# %ENDPUBLIC% */

	return this.PropertyID;
}

BookmarkedProperties.prototype.fromString = function(theString) {

	/* 	# %PUBLIC%
		# NAME:*		BookmarkedProperties.prototype.fromString
		# DESCR:*		Stores a string of saved bookemarked properties (OrderID).
		# USAGE:*		Internal Function. Do Not Used.
		# RETURNS:*		Nothing
		# %ENDPUBLIC% */

	this.PropertyID = theString;	
}


/* General non Class / object Functions */

function showHideSelectControls(hide) {

	/* 	# %PUBLIC%
		# NAME:*		showHideSelectControls()
		# DESCR:*		Make the select controls on the Page visible / invisible.
		# :*			IE 6 select controls sit above every other z-index.
		# :*			Sometimes we need to hide them so other things show on top.
		# :*			This function will show by default. If hide is true, then
		# :*			make the selects invisible.
		# USAGE:*		showHideSelectControls(hide);
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	var selectControls = document.getElementsByTagName("select");

	if (hide == true) {
		// Hide the select controls.
		for (i = 0; i != selectControls.length; i++) {
			selectControls[i].style.visibility = "hidden";
		}

	} else {
		// Show the select controls.
		for (i = 0; i != selectControls.length; i++) {
			selectControls[i].style.visibility = "visible";
		}
	}
}

function obtainPageSize() {
	
	/* 	# %PUBLIC%
		# NAME:*		obtainPageSize()
		# DESCR:*		Return an array containg the page and window size 
		# :*			of the borwser.
		# USAGE:*		var arrayPageSize = obtainPageSize();
		# RETURNS:*		Array containing the page and window sizes.
		# :*			arrayPageSize[0] = pageWidth
		# :*			arrayPageSize[1] = pageHeight
		# :*			arrayPageSize[2] = windowWidth
		# :*			arrayPageSize[3] = windowHeight
		# %ENDPUBLIC% */


	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		// all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;

	if (self.innerHeight) {
		// all except Explorer
		if (document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		// other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// For small pages with total height less than the height of the viewport
	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// For small pages with total width less than the width of the viewport
	if (xScroll < windowWidth) {	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
	return arrayPageSize;
}

function obtainDocumentScrollTop() {

	/* 	# %PUBLIC%
		# NAME:*		obtainDocumentScrollTop
		# DESCR:*		Obtain the document scroll top offset.
		# USAGE:*		my Offset = obtainDocumentScrollTop();
		# RETURNS:*		Integer for the Document Scoll Top offset. 
		# %ENDPUBLIC% */

	var scrollY = 0;

	if (document.documentElement && document.documentElement.scrollTop) {
		scrollY = document.documentElement.scrollTop;
	} else if (document.body && document.body.scrollTop) {
		scrollY = document.body.scrollTop;
	} else if (window.pageYOffset) {
		scrollY = window.pageYOffset;
	} else if (window.scrollY) {
		scrollY = window.scrollY;
	}

	return scrollY;
}

function infoBox(displayString, OrderIDElement) {

	/* 	# %PUBLIC%
		# NAME:*		infoBox
		# DESCR:*		Display an overlayed information box, centred on the screen
		# :*			with the message passed. Used with History / bookmarking tools.
		# USAGE:*		infoBox("Some Message");
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	var infoBoxElement = $('infoBox');
	var parent;

	if (infoBoxElement != undefined) {
		infoBoxElement.parentNode.removeChild(infoBoxElement);
	}

	//create div and set attributes
	infoBoxElement = document.createElement('div');
	infoBoxElement.setAttribute('id','infoBox');

	// If we have a string to display, display the info box
	// else do nothing. Useless to display a blank box.
	if (displayString != "" || displayString != null) {
		infoBoxElement.innerHTML = "<div><div class='infoBoxContents'>" + displayString + "</div></div>";

		if (OrderIDElement != undefined) {
			parent = $(OrderIDElement);
			parent.insertBefore(infoBoxElement, parent.firstChild);
			infoBoxElement.style.left = "30%";
		} else {
			parent = document.getElementsByTagName('body').item(0);
			parent.insertBefore(infoBoxElement, parent.firstChild);
			new Effect.Centre('infoBox');
		}

		new Effect.Appear('infoBox', {queue: 'front'});
		new Effect.Fade('infoBox', {duration: 1.0, queue: 'end'});
	}
}

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 testEmailAddress(emailAddress) {
	/* 	# %PUBLIC%
		# NAME:*		testEmailAddress
		# DESCR:*		Given an email address, test to see if it is valid.			
		# USAGE:*		var ValidEmailAddress = testEmailAddress(emailAddress);
		# RETURNS:*		True for valid email address or False for invalid email address. 
		# %ENDPUBLIC% */

		var filter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

		if (!filter.test(emailAddress)) {
			return false;
		}

		return true
}

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 callInProgress(xmlhttp) {

	/*	# %PRIVATE%
		# NAME:*		callInProgress
		# DESCR:*		Determine if an AJAX call is in progress or not. 
		# USAGE:*		Internal Function. Do not use.
		# RETURNS:*		Returns True if an AJAX call is in progress, False if not.
		# %ENDPRIVATE% */

	if (xmlhttp == null) {
		return false;
	}

	switch (xmlhttp.readyState) {
		case 1: case 2: case 3:
			return true;
			break;

		// Case 4 and 0
		default:
			return false;
			break;
	}
}

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;
}

function alertBox(errorHeaderString, errorString) {	

	/* 	# %PUBLIC%
		# NAME:*		alertBox
		# DESCR:*		Display a 21st century javascript alert box,
		# :*			with the paramaters passed.
		# USAGE:*		alertBox("Some Header", "Some Message");
		# RETURNS:*		false. 
		# %ENDPUBLIC% */


	var buttonHtml = '<br><div class="largebutton"><button type="submit" onclick="_ToggleAlertBox();return false;">Close</button></div>';

	if (errorHeaderString == "") {
		errorHeaderString = "An Error has occured!";
	}

	if (errorString == "") {
		errorString = "There has been an unknown error.<br>";
	}
	$('alertboxdialog').innerHTML = "<h4>" + errorHeaderString + "</h4>" + errorString + "<br>" + buttonHtml;

	var arrayPageSize = obtainPageSize();
	$('alertbox').style.width = arrayPageSize[2] + "px";
	$('alertbox').style.height = arrayPageSize[3] + "px";

	_ToggleAlertBox();

	return false;
}

function _ToggleAlertBox() {

	/* 	# %PRIVATE%
		# NAME:*		_ToggleAlertBox
		# DESCR:*		Internal Subroutine used by the alertBox() function
		# USAGE:*		Do Not Use
		# RETURNS:*		Nothing
		# %ENDPRIVATE% */

	var AlertBoxen = $("alertbox");

	if (AlertBoxen.style.display == "block") {
		// Hide the AlertBox
		AlertBoxen.style.display = "none";
		showHideSelectControls();
	} else {
		// Show the AlertBox
		showHideSelectControls(true);
		AlertBoxen.style.display = "block";
	}
}

function showContactNumbers(oid) {

	/* 	# %PUBLIC%
		# NAME:*		showContactNumbers
		# DESCR:*		Given an OID of a listing, displays all hidden phone numbers
		# :*			and increments the PTH hit counter.
		# USAGE:*		showContactNumbers('1234567');
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	if (oid) {		
		var ts = '&ts=' + new Date().getTime();
		new Ajax.Request('/portal/phonehits?OID='+oid+ts, {
			method: 'get',
  			onSuccess: function(transport) {
      		// Hide links and show contact numbers
				$$('.shownumbers').invoke('hide');
				$$('.contactnumbers').invoke('show');
				Effect.multiple($$('.highlight'),Effect.Highlight,{ speed: 0 });
  			}
		});
	}
}


function returnBusinessSubCategory(	update_field_number, category_id, sub_category_id,
									select_name, include_all, multiple, size, width, 
									disabled ) {

	/* 	# %PUBLIC%
		# NAME:*		returnBusinessSubCategory
		# DESCR:*		Generate and return a business subcategories select control 
		# :*			based on the given category id. 
		# USAGE:*		updateBusinessSubCategory(1, 11);
		# :*			updateBusinessSubCategory(1, 11, 101); # where sub category id 101
		# :*												   # will be selected
		# RETURNS:*		Nothing but updates HTML element named 
		# :*			'SubCategory'+update_field_number with HTML code 
		# :*			containing a select control.
		# %ENDPUBLIC% */
		
	if (category_id && update_field_number) {		
		var params = 'select_id='+ update_field_number +'&bus_cat=' + category_id + 
					 '&bus_sub_cat=' + sub_category_id + 
					 
					 // select control attributes
					 '&include_all=' + include_all + 
					 '&multiple=' + multiple + '&size=' + size + '&width=' + width + 
					 '&disabled=' + disabled;
		if (select_name) 
			params += '&select_name=' + select_name;	

		new Ajax.Request('/portal/returnbusinesssubcategories?' + params, {
			method: 'get',
  			onSuccess: function(transport) {
  				var UpdatingHTMLName = 'SubCategory' + update_field_number;
  				$(UpdatingHTMLName).update(transport.responseText);
  			}
		});
	}
}

PortalApplication.prototype.loadBookmarkedPropertiesAgency = function(agency) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.loadBookmarkedProperties
		# DESCR:*		Loads the bookmark cookie and selects the approprate property checkboxes
		# USAGE:*		Internal Function: Do Not Use.
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	// Read the bookMarked cookie
 	var savedString = this.savedSettings.readCookie('bookMarkedAgency');
     	
 	if(savedString != null) {
 		// If there is a cookie 
        
        this.bookmarkedProperties = new Object();
	    this.bookmarkCount = 0;
        var BookmarkedArray = savedString.split(',');//split cookie string in to array
        
 		for(var i = 0; i < BookmarkedArray.length; i++) {
            
 			if(BookmarkedArray[i]!="" && BookmarkedArray[i].endsWith('_' + agency)) {
                //alert('ss= ' + BookmarkedArray[i]);
 				// if a value, create a new bookmark
 				this.bookmarkedProperties[BookmarkedArray[i]] = new BookmarkedProperties(BookmarkedArray[i]);
				// Increment count
 				this.bookmarkCount++;
 			}
 		}
		// Select the approprite checkboxes if they exist on the page.
		this.selectBookmarkedPropertiesAgency(agency);
	}
 	
}

PortalApplication.prototype.selectBookmarkedPropertiesAgency = function(agency) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.selectBookmarkedProperties
		# DESCR:*		Selects the appropriate property checkboxes in the bookmarkedProperties object.
		# USAGE:*		Internal Function: Do Not Use.
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	// Select the approprite checkboxes if they exist on the page.
	for(bookmark in this.bookmarkedProperties) {
        
	//	alert(bookmark);
		if ($('OID_' + this.bookmarkedProperties[bookmark])) {
           	$('OID_' + this.bookmarkedProperties[bookmark]).checked = true;
		}
	}
}

PortalApplication.prototype.numberBookMarkedPropertiesAgency = function(agency) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.selectBookmarkedProperties
		# DESCR:*		Selects the appropriate property checkboxes in the bookmarkedProperties object.
		# USAGE:*		Internal Function: Do Not Use.
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */

	// Select the approprite checkboxes if they exist on the page.
    var count = 0;
	for(bookmark in this.bookmarkedProperties) {
        count++;	
	}
    return count;
}

PortalApplication.prototype.bookmarkPropertyAgency = function(e, orderid, agency) {

	/* 	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.bookmarkProperty
		# DESCR:*		Update the bookmarkCount, Display, highlight popup and right column tool
		# :*			and add the OrderID to the bookmarked cookie.
		# USAGE:*		PortalApp = new PortalApplication(); PortalApp.bookmarkProperty(e, orderid);
		# :*			Used in the Search Results display and the corresponding check box for each
		# :*			property displayed.
		# :*			e: event
		# :*			orderID: OrderID of property to bookmark
		# RETURNS:*		Nothing. 
		# %ENDPUBLIC% */


	var displayString = "";
	var targ;
	if (!e) {
		var e = window.event;
	}

	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
    
	// add or removed if checked or not
	if(targ.checked) {
		this.bookmarkedProperties[orderid + '_' + agency] = new BookmarkedProperties(orderid + '_' + agency);
		this.bookmarkCount++;
	} else {
        //alert('orderid = ' + orderid);
		this.bookmarkedProperties[orderid + '_' + agency] = null;
		this.bookmarkCount--;
		if (this.bookmarkCount < 0) {
			this.bookmarkCount = 0;
		}
	}

	var saveString = this.bookmarkToString();
    this.savedSettings.createCookie('bookMarkedAgency', saveString);
}

PortalApplication.prototype.competition_setting = function() {

	/*	# %PUBLIC%
		# NAME:*		PortalApplication.prototype.competition_setting
		# DESCR:*		Public funtion used to change the setting for the competition.
		# USAGE:*		PortalApp.autofitBg();
		# RETURNS:*		Returns nothing.
		# %ENDPUBLIC% */

	var contentHeight = $("contentContainer").offsetHeight;
	var primaryExists = $("competitionleft");
	if(primaryExists) {	
		var primaryHeight = $("competitionleft").offsetHeight + 195;
	}
	var secondaryExists = $("competitionright");
	if(secondaryExists) {	
		var secondaryHeight = $("competitionright").offsetHeight + 195;
	}
	var contentHeightTemp = contentHeight;
	
	if(primaryExists) {	
		if (contentHeightTemp < primaryHeight) {
			contentHeightTemp = primaryHeight;
		}
	}
	
	if(secondaryExists) {	
		if (contentHeightTemp < secondaryHeight) {
			contentHeightTemp = secondaryHeight;
		}
	}
	if (contentHeightTemp > contentHeight) {
		$("contentContainer").style.height = contentHeightTemp + "px";
	}
	
}

// Transforms all labels with class "inside", so they will appear inside the
// input field which they're for.
function transformInsideLabels() {
  $$('label.inside').each(function(label) {
    // get the element which this label is for
    var input = $(label.readAttribute('for'));
    if (input && ((input.tagName.toLowerCase() == 'input' && input.readAttribute('type').toLowerCase() == 'text') || input.tagName.toLowerCase() == 'textarea')) {
      
      if (input.labelValue == undefined) {
        input.labelValue = label.textContent || label.innerHTML; // textContent as of DOM 3
        input.styleInsideLabel = styleInsideLabel;
        input.styleInsideLabelWithFocus = styleInsideLabelWithFocus;
        
		input.styleInsideLabel();
        label.hide();

        input.observe('focus', input.styleInsideLabelWithFocus);
        input.observe('blur', input.styleInsideLabel);
      }
    }
  });

  $$('form').each(function(form) {
    if (!form.clearInsideLabelsTemporarly) {
      form.clearInsideLabelsTemporarly = clearInsideLabelsTemporarly;
      form.observe('submit', form.clearInsideLabelsTemporarly);

      // Change this form's serialize method to the one we define below
      form.serialize = Form.serialize.methodize()
    }
  });
}

// Assigns the correct css class and value to an input field with inside label
function styleInsideLabel() {
  this.removeClassName('focused');
  if (this.value) {
    this.addClassName('active');
    this.removeClassName('inactive');
  } else {
    this.addClassName('inactive');
    this.removeClassName('active');
    this.value = this.labelValue;
  }
}

// Assigns the correct css class and value to an input field with inside label which is focused
function styleInsideLabelWithFocus() {
  // Apply the correct css class
  this.addClassName('focused');
  this.removeClassName('active');
  this.removeClassName('inactive');
  if (this.value == this.labelValue) this.value = "";
}

// Clears all input field with inside labels, so they do not have the wrong value on submit
function clearInsideLabelsTemporarly() {
  this.descendants().each(function(input) {
    if (input.labelValue) {
      // This is an input field with inside label
      if (input.value == input.labelValue) {
        input.value = "";
        // reset value after a short while
        setTimeout(function() {
          input.styleInsideLabel();
        }, 10);
      }
    }
  });
}

// Serializes a form, but clears all inside labels before doing so
Form.Methods.serializeWithInsideLabelsRemoved = function(form, options) {
  if (form.clearInsideLabelsTemporarly) form.clearInsideLabelsTemporarly();
  return Form.serializeWithoutInsideLabelsRemoved(form, options);
};

// Alias method chain for the above
Form.Methods.serializeWithoutInsideLabelsRemoved = Form.Methods.serialize;
Form.Methods.serialize = Form.Methods.serializeWithInsideLabelsRemoved;
Object.extend(Form, Form.Methods);

