var CommonUtil = {

	popup_emailToAgent: function(link, name) {
		var height = 445;
		var width = 670;
		if (!window.focus) return true;
		var href;
		if (typeof(link) == 'string') {
		   href = link;
		} else {
		   href = link.href;
		}
		var win = window.open(href, name, 'width=' + width + ', height=' + height + ', scrollbars=1, resizable=1');
		win.moveTo((window.screen.availWidth - width) / 2, (window.screen.availHeight - height) / 2);
		return false;
	},

	popup_emailToFriend: function(link, name) {
		if (!window.focus) return true;
		var href;
		if (typeof(link) == 'string') {
		   href = link;
		} else {
		   href = link.href;
		}
		window.open(href, name, 'width=670, height=445, scrollbars=auto');
		return false;
	},

	changeReadonlyStyle: function(id) {
		var elementArray = (id == null) ? document.getElementsByTagName("INPUT") : document.getElementById(id).getElementsByTagName("INPUT");
		for (var i = 0; i < elementArray.length; i++) {
			if (elementArray[i].readOnly) {
				elementArray[i].style.backgroundColor = "#EEEEEE";
			}
		}
	},

	changeImageStyle: function(id) {
		var elementArray = (id == null) ? document.getElementsByTagName("IMG") : document.getElementById(id).getElementsByTagName("IMG");
		for (var i = 0; i < elementArray.length; i++) {
		}
	},

	changeAnchorStyle: function(id) {
		var elementArray = (id == null) ? document.getElementsByTagName("A") : document.getElementById(id).getElementsByTagName("A");
		for (var i = 0; i < elementArray.length; i++) {
		}
	},

	globalInitialize: function() {
		this.changeReadonlyStyle();
		//this.changeImageStyle("td_centerContent");
		//this.changeAnchorStyle("td_centerContent");
	},

	caution: false,

	// name - name of the cookie
	// value - value of the cookie
	// [expires] - expiration date of the cookie
	// (defaults to end of current session)
	// [path] - path for which the cookie is valid
	// (defaults to path of calling document)
	// [domain] - domain for which the cookie is valid
	// (defaults to domain of calling document)
	// [secure] - Boolean value indicating if
	// the cookie transmission requires a secure transmission
	// * an argument defaults when it is assigned null as a placeholder
	// * a null placeholder is not required for trailing omitted arguments
	setCookie: function(name, value, expires, path, domain, secure) {
		var curCookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
		if (!this.caution || (name + "=" + escape(value)).length <= 4000) {
			document.cookie = curCookie;
		} else {
			if (confirm("Cookie exceeds 4KB and will be cut!")) {
				document.cookie = curCookie;
			}
		}
	},

	// name - name of the cookie
	// * return string containing value
	// of specified cookie or null if cookie
	// does not exist
	getCookie: function(name) {
		var prefix = name + "=";
		var cookieStartIndex = document.cookie.indexOf(prefix);
		if (cookieStartIndex == -1) {
			return null;
		}
		var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
		if (cookieEndIndex == -1) {
			cookieEndIndex = document.cookie.length
		}
		return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
	},

	// name - name of the cookie
	// [path] - path of the cookie
	// (must be same as path used to create cookie)
	// [domain] - domain of the cookie
	// (must be same as domain used to create cookie)
	// * path and domain default if assigned
	// null or omitted if no explicit argument proceeds
	deleteCookie: function(name, path, domain) {
		if (getCookie(name)) {
			document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") + 
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	},

	// date - any instance of the Date object
	// * you should hand all instances of the
	// Date object to this function for "repairs"
	// * this function is taken from
	// Chapter 14, "Time and Date in JavaScript", in
	// "Learn Advanced JavaScript Programming"
	fixDate: function(date) {
		var base = new Date(0);
		var skew = base.getTime();
		if (skew > 0) {
			date.setTime(date.getTime() - skew);
		}
	},

	autoLogin: function() {
		var username = this.getCookie("username");
		var password = this.getCookie("password");
		if (username != null && password != null) {
			document.location = "login.action?username=" + username + "&password=" + password;
		}
	},

	deleteLoginInfo: function() {
		this.deleteCookie("username");
		this.deleteCookie("password");
	},

	printLastSearchCriteria: function(isDirSearch) {
		var lastSearch_categoryId = this.getCookie("searchCriteria_categoryId");
		var lastSearch_categoryName = this.getCookie("searchCriteria_categoryName");
		var hasCategory = lastSearch_categoryId != null && lastSearch_categoryId !== "" && lastSearch_categoryName != null && lastSearch_categoryName !== "";

		var lastSearch_subcategoryId = this.getCookie("searchCriteria_subcategoryId");
		var lastSearch_subcategoryName = this.getCookie("searchCriteria_subcategoryName");
		var hasSubcategory = lastSearch_subcategoryId != null && lastSearch_subcategoryId !== "" && lastSearch_subcategoryName != null && lastSearch_subcategoryName !== "";

		var lastSearch_keyword = this.getCookie("searchCriteria_keyword");
		var hasKeyword = lastSearch_keyword != null && lastSearch_keyword !== "";

		var lastSearch_suburb = this.getCookie("searchCriteria_suburb");
		var hasSuburb = lastSearch_suburb != null && lastSearch_suburb !== "";

		var lastSearch_state = this.getCookie("searchCriteria_state");
		var hasState = lastSearch_state != null && lastSearch_state !== "";

		var lastSearch_postcode = this.getCookie("searchCriteria_postcode");
		var hasPostcode = lastSearch_postcode != null && lastSearch_postcode !== "";
		
		var message = "";

		if (hasCategory || hasSubcategory || hasKeyword || hasSuburb || hasState || hasPostcode) {

			var url = (isDirSearch ? "searchDir" : "search") + ".action?";
			var parameterArray = [];

			if (hasCategory) {
				message += "Category Name: " + lastSearch_categoryName + "<br/>";
				parameterArray.push("searchParameters.categoryid=" + lastSearch_categoryId);
			}
			if (hasSubcategory) {
				message += "Subcategory Name: " + lastSearch_subcategoryName + "<br/>";
				parameterArray.push("searchParameters.subcategoryid=" + lastSearch_subcategoryId);
			}
			if (hasKeyword) {
				message += "Keyword: " + lastSearch_keyword + "<br/>";
				parameterArray.push("searchParameters.keyword=" + lastSearch_keyword);
			}
			if (hasSuburb) {
				message += "Suburb: " + lastSearch_suburb + "<br/>";
				parameterArray.push("searchParameters.suburb=" + lastSearch_suburb);
			}
			if (hasState) {
				message += "State: " + lastSearch_state + "<br/>";
				parameterArray.push("searchParameters.state=" + lastSearch_state);
			}
			if (hasPostcode) {
				message += "Postcode: " + lastSearch_postcode + "<br/>";
				parameterArray.push("searchParameters.postcode=" + lastSearch_postcode);
			}
			parameterArray.push("searchParameters.adtype=" + (isDirSearch ? "2" : "8"));

			url += parameterArray.join("&");

			message += '<input type="button" value="View Result" onclick="document.location=\'' + url + '\';"/>';

		} else {
			message = "Your most recent search will be displayed here, so you can search again quickly.";
		}
		document.write(message);
	},

	capitalizeFirstCharAndUncapitalizeTheRestForAllWords: function(str) {
		if (str == null) {
			return str;
		}
		var stringArray = str.split(" ");
		for (var i = 0; i < stringArray.length; i++) {
			stringArray[i] = stringArray[i].toLowerCase();
			stringArray[i] = stringArray[i].charAt(0).toUpperCase() + stringArray[i].substr(1);
		}
		return stringArray.join(" ");
	}
};
