var LANG_JS_LOADCATEGORYTREE = "Wait%2C Loading Category Tree...";
var LANG_JS_LOADING = "Loading...";
var LANG_JS_FAVORITEADD = "This item was added to your Local Favourites.";
var LANG_JS_FAVORITEDEL = "This item was removed from your Local Favourites.";
var LANG_JS_LOADCATEGORYTREE = "Wait%2C Loading Category Tree...";
var LANG_JS_GOOGLEMAPS_NOTAVAILABLE_ADDRESS = "Google Map is not available for the current address.";
var LANG_JS_LOADING = "Loading...";
jQuery(document).ready(function($) {
	$wheres = $("#wheres");
	$wheres.val("Suburb or Postcode..");
	$wheres.focus(function() {
		if ($(this).val() == "Suburb or Postcode..") {
			$(this).val("");
			$(this).attr("class", "si_quick");
		}
	});
	$wheres.blur(function() {
		if ($(this).val() == "") {
			$(this).val("Suburb or Postcode..");
			$(this).attr("class", "si_key");
		}
	});
	$keyword = $("#keyword");
	$keyword.val("Keyword or Key-Phrase");
	$keyword.focus(function() {
		if ($(this).val() == "Keyword or Key-Phrase") {
			$(this).val("");
			$(this).attr("class", "si_quick");
		}
	});
	$keyword.blur(function() {
		if ($(this).val() == "") {
			$(this).val("Keyword or Key-Phrase");
			$(this).attr("class", "si_sub");
		}
	});
});

function check_empty(sub) {
	if (document.getElementById('keyword').value == 'Keyword or Key-Phrase') {
		document.getElementById('keyword').value = '';
	}
	if (document.getElementById('wheres').value == 'Suburb or Postcode..') {
		document.getElementById('wheres').value = '';
	}
	if (sub) {
		document.getElementById('search_form').submit();
	} else {
		return true;
	}
}
var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

function noCache(uri) {
	return uri.concat(/\?/.test(uri) ? "&" : "?", "noCache=", (new Date).getTime(), ".", Math.random() * 1234567)
};

function changeClass(what, to) {
	what.className = to;
}
function hoverFav(ref, oo) {
	if (oo == 'over') {
		ref.parentNode.parentNode.style.backgroundColor = '#dff6c0';
		document.getElementById("local_favs_cbox").style.backgroundColor = '#dff6c0';
	} else {
		ref.parentNode.parentNode.style.backgroundColor = '';
		document.getElementById("local_favs_cbox").style.backgroundColor = '#e2e9ed';
	}
}
function countSpaces(obj) {
	var iLength = obj.value.length;
	var strSpaces = obj.value.match(new RegExp("( )", "g"));
	var countSpaces = strSpaces ? strSpaces.length : 0;
	return countSpaces;
}
function countLineBreaks(obj) {
	var iLength = obj.value.length;
	var strLineBreaks = obj.value.match(new RegExp("(\\n)", "g"));
	var countLineBreaks = strLineBreaks ? strLineBreaks.length : 0;
	return countLineBreaks;
}
function textCounter(field, counter_field, maxlimit) {
	var lineBreaks = countLineBreaks(field);
	var adjust = isInternetExplorer ? 1 : 0;
	if (field.value.length - lineBreaks * adjust > maxlimit) {
		field.value = field.value.substring(0, maxlimit + lineBreaks * adjust);
		field.focus();
	} else {
		counter_field.value = maxlimit - field.value.length + lineBreaks * adjust;
	}
}
function textCounter_nb(field, counter_field, maxlimit) {
	field.value = field.value.replace(/\n/g, ' ');
	var lineBreaks = countLineBreaks(field);
	var adjust = isInternetExplorer ? 1 : 0;
	if (field.value.length - lineBreaks * adjust > maxlimit) {
		field.value = field.value.substring(0, maxlimit + lineBreaks * adjust);
		field.focus();
	} else {
		document.getElementById(counter_field).innerHTML = maxlimit - field.value.length + lineBreaks * adjust;
	}
}
function backToSection(backToURL, forceBackToURL) {
	if (forceBackToURL == null) forceBackToURL = false;
	if (history.length > 1 && !forceBackToURL) history.back();
	else window.location.href = backToURL;
}
function hideStatus() {
	window.defaultStatus = '';
	window.status = '';
	return true;
}
function searchReset() {
	tot = document.search_form.elements.length;
	for (i = 0; i < tot; i++) {
		if (document.search_form.elements[i].type == 'text') {
			document.search_form.elements[i].value = "";
		} else if (document.search_form.elements[i].type == 'checkbox' || document.search_form.elements[i].type == 'radio') {
			document.search_form.elements[i].checked = false;
		} else if (document.search_form.elements[i].type == 'select-one') {
			document.search_form.elements[i].selectedIndex = 0;
		}
	}
	if ((document.search_form.country_id) || (document.search_form.state_id) || (document.search_form.region_id) || (document.search_form.city_id) || (document.search_form.area_id)) {
		searchLocationReset();
	}
}
function easyFriendlyUrl(name2friendlyurl, target, validchars, separator) {
	var str = "";
	var i;
	var exp_reg = new RegExp("[" + validchars + separator + "]");
	var exp_reg_space = new RegExp("[ ]");
	name2friendlyurl.toString();
	for (i = 0; i < name2friendlyurl.length; i++) {
		if (exp_reg.test(name2friendlyurl.charAt(i))) {
			str = str + name2friendlyurl.charAt(i);
		} else {
			if (exp_reg_space.test(name2friendlyurl.charAt(i))) {
				if (str.charAt(str.length - 1) != separator) {
					str = str + separator;
				}
			}
		}
	}
	if (str.charAt(str.length - 1) == separator) str = str.substr(0, str.length - 1);
	document.getElementById(target).value = str.toLowerCase();
}
function subdomainUpdate(input, target, targetb) {
	document.getElementById(target).innerHTML = "http://" + input.value + ".seeklocal.biz";
	document.getElementById(targetb).innerHTML = "http://seeklocal.biz/listing/" + input.value;
}
function searchLocationReset() {
	if (document.search_form.country_id) {
		if (document.search_form.state_id) {
			while (document.search_form.state_id.options.length > 1) {
				deleteIndex = document.search_form.state_id.options.length - 1;
				document.search_form.state_id.options[deleteIndex] = null;
			}
		}
		if (document.search_form.region_id) {
			while (document.search_form.region_id.options.length > 1) {
				deleteIndex = document.search_form.region_id.options.length - 1;
				document.search_form.region_id.options[deleteIndex] = null;
			}
		}
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length > 1) {
				deleteIndex = document.search_form.city_id.options.length - 1;
				document.search_form.city_id.options[deleteIndex] = null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length > 1) {
				deleteIndex = document.search_form.area_id.options.length - 1;
				document.search_form.area_id.options[deleteIndex] = null;
			}
		}
	} else if (document.search_form.state_id) {
		if (document.search_form.region_id) {
			while (document.search_form.region_id.options.length > 1) {
				deleteIndex = document.search_form.region_id.options.length - 1;
				document.search_form.region_id.options[deleteIndex] = null;
			}
		}
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length > 1) {
				deleteIndex = document.search_form.city_id.options.length - 1;
				document.search_form.city_id.options[deleteIndex] = null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length > 1) {
				deleteIndex = document.search_form.area_id.options.length - 1;
				document.search_form.area_id.options[deleteIndex] = null;
			}
		}
	} else if (document.search_form.region_id) {
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length > 1) {
				deleteIndex = document.search_form.city_id.options.length - 1;
				document.search_form.city_id.options[deleteIndex] = null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length > 1) {
				deleteIndex = document.search_form.area_id.options.length - 1;
				document.search_form.area_id.options[deleteIndex] = null;
			}
		}
	} else if (document.search_form.city_id) {
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length > 1) {
				deleteIndex = document.search_form.area_id.options.length - 1;
				document.search_form.area_id.options[deleteIndex] = null;
			}
		}
	}
}
function showText(text) {
	return unescape(text);
}
po_switch = "";

function togglePO(checkboxID, toggleID) {
	var checkbox = document.getElementById(checkboxID);
	var toggle = document.getElementById(toggleID);
	if (checkbox.checked) {
		po_switch = toggle.value;
		toggle.value = "PO Box";
		toggle.disabled = false;
		document.getElementById('sname').style.display = "none";
		document.getElementById('snum').innerHTML = "Box Number:";
	} else {
		toggle.disabled = false;
		if (po_switch == "PO Box") {
			toggle.value = "";
		} else {
			toggle.value = po_switch;
		}
		document.getElementById('sname').style.display = '';
		document.getElementById('snum').innerHTML = "Street No:";
	}
}
function displayMap(auto_marker) {
	if ($.cookie('showMap') == 1) {
		document.getElementById('resultsMap').className = 'resultsMap isShow';
		document.getElementById('map_hide').className = 'ui_menu_bar isShow';
		document.getElementById('map_show').className = 'isHidden';
		$.cookie('showMap', '0', {
			expires: 7,
			path: '/'
		});
		initialize(auto_marker);
	} else {
		document.getElementById('resultsMap').className = 'resultsMap isHidden';
		document.getElementById('map_hide').className = 'isHidden';
		document.getElementById('map_show').className = 'ui_button_strip gb isShow';
		$.cookie('showMap', '1', {
			expires: 7,
			path: '/'
		});
	}
}
var obj;
var field_0;
var field_1;
var field_2;
var field_loading;

function fillSelect(host, obj, fk_value, form, autos, autosb) {
	this.obj = obj;
	this.field_0 = form.country_id;
	this.field_1 = form.state_id;
	this.field_2 = form.region_id;
	this.rhost = host;
	this.rname = obj.name;
	this.rval = fk_value;
	this.rform = form;
	resetSelect(obj);
	if (typeof obj != 'object') return false;
	if (autos > 0) {
		window.autosel = autos;
	} else {
		window.autosel = 0;
	}
	if (autosb > 0) {
		window.autoselb = autosb;
	} else {
		window.autoselb = 0;
	}
	if (fk_value > 0) {
		form.country_id.disabled = true;
		form.state_id.disabled = true;
		form.region_id.disabled = true;
	}
	if (obj.name == "state_id") {
		resetSelect(form.region_id);
		if (fk_value > 0) {
			form.state_id.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			form.state_id.options[1].selected = true;
			this.field_loading = form.state_id;
		}
		url = host + '/location.php?country_id=' + fk_value;
		if (fk_value > 0) loadResult(url, '');
	}
	if (obj.name == "region_id") {
		if (fk_value > 0) {
			form.region_id.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			form.region_id.options[1].selected = true;
			this.field_loading = form.region_id;
		}
		url = host + '/location.php?state_id=' + fk_value;
		if (fk_value > 0) {
			loadResult(url, '');
		}
	}
}
function fillSelectC(host, obj, fk_value, form, sect) {
	resetSelect(obj);
	if (fk_value > 0) {
		form.search_sub_category.options[1] = new Option(showText(LANG_JS_LOADING), -1);
		form.search_sub_category.options[1].selected = true;
		this.field_loading = form.search_sub_category;
	}
	url = host + '/' + sect + '/cats.php?cat_id=' + fk_value;
	if (fk_value > 0) {
		loadResult(url, '');
	}
}
function setSearchCat(val) {
	document.getElementById('hidden_category_id').value = val;
}
function fillPcode(host, fk_value, field) {
	if (fk_value == "Select a Suburb") {
		field.value = "";
	} else {
		put = field;
		try {
			xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
			field.value = "loading...";
		} catch (e) {}
		xmlhttp.onreadystatechange = triggered;
		url = host + '/get_postcode.php?state=vic&suburb=' + fk_value;
		req = "state=vic&suburb=" + fk_value;
		xmlhttp.open("GET", url);
		xmlhttp.send(null);
	}
}
function triggered() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		put.value = xmlhttp.responseText;
	}
}
function fillSelectSearchPage(host, obj, fk_value, form) {
	this.obj = obj;
	this.field_0 = form.search_country;
	this.field_1 = form.search_state;
	this.field_2 = form.search_region;
	resetSelect(obj);
	if (typeof obj != 'object') return false;
	if (fk_value > 0) {
		form.search_country.disabled = true;
		form.search_state.disabled = true;
		form.search_region.disabled = true;
	}
	if (obj.name == "search_state") {
		resetSelect(form.search_region);
		if (fk_value > 0) {
			form.search_state.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			form.search_state.options[1].selected = true;
			this.field_loading = form.search_state;
		}
		url = host + '/location.php?country_id=' + fk_value + '&searchpage=1';
		if (fk_value > 0) loadResult(url, '');
	}
	if (obj.name == "search_region") {
		if (fk_value > 0) {
			form.search_region.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			form.search_region.options[1].selected = true;
			this.field_loading = form.search_region;
		}
		url = host + '/location.php?state_id=' + fk_value + '&searchpage=1';
		if (fk_value > 0) loadResult(url, '');
	}
}
function resetSelect(obj) {
	while (obj.options.length > 1) {
		deleteIndex = obj.options.length - 1;
		obj.options[deleteIndex] = null;
	}
}
function loadXMLDoc(url) {
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();
		}
	}
}
function processReqChange() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			response = req.responseXML.documentElement;
			if (response) {
				var result = new Array();
				for (i = 0; i < response.getElementsByTagName('id').length; i++) {
					result[i] = {
						'id': response.getElementsByTagName('id')[i].firstChild.data,
						'name': response.getElementsByTagName('name')[i].firstChild.data,
						'obj_name': response.getElementsByTagName('obj_name')[i].firstChild.data
					};
				}
				loadResult('', result);
				unlockLocation();
			}
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}
function loadResult(url, result) {
	if (result != '') {
		for (i = 0; i < result.length; i++) {
			document.getElementById(result[i].obj_name).options[document.getElementById(result[i].obj_name).options.length] = new Option(result[i].name, result[i].id);
			if (window.autosel > 0) {
				if (window.autosel == result[i].id) {
					auto_id = (i + 1);
				}
			} else {
				auto_id = 0;
			}
		}
	} else if (url != '') {
		return (loadXMLDoc(url));
	}
}
function selectOptionByValue(selObj, val) {
	var A = selObj.options,
		L = A.length;
	while (L) {
		if (A[--L].value == val) {
			selObj.selectedIndex = L;
			L = 0;
		}
	}
}
function clear_asearch() {
	document.getElementById('wheres').value = "";
}
function clear_pcode() {
	document.getElementById('zip_code').value = "";
}
function unlockLocation() {
	if (this.field_loading) {
		this.field_loading.options[1] = null;
		this.field_loading.options[auto_id].selected = true;
	}
	this.field_0.disabled = false;
	this.field_1.disabled = false;
	this.field_2.disabled = false;
	if (this.obj.name == "state_id" && window.autosel > 0) {
		fillSelect(this.rhost, this.rform.region_id, this.rform.state_id.value, this.rform, window.autoselb);
	} else if ("region_id" && window.autosel > 0) {
		fillPcode(this.rhost, this.rform.region_id.options[this.rform.region_id.selectedIndex].innerHTML, this.rform.zip_code);
	}
}
function moveUpDown(status, obj) {
	if (status == "SHOW") {
		document.getElementById(obj).className = "isShown";
	}
	if (status == "HIDE") {
		document.getElementById(obj).className = "isHidden";
	}
}
function moveDownAll() {
	for (i = 1; i <= 4; i++) {
		var str;
		str = "tab" + i;
		if (document.getElementById(str)) document.getElementById(str).className = "isHidden";
	}
}
function hideTabs() {
	var str;
	for (i = 1; i < 5; i++) {
		str = "li-" + i;
		if (document.getElementById(str)) document.getElementById(str).className = "tab-inactive";
	}
}
function tabToActive(obj) {
	hideTabs();
	document.getElementById(obj).className = "tab-active";
}
function showPhone(listingid, default_url) {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (exc) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ex) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = false;
			}
		}
	}
	if (xmlhttp) {
		xmlhttp.open("GET", default_url + '/countphoneclick.php?listing_id=' + listingid, true);
		xmlhttp.send(null);
	}
	document.getElementById("phoneLink" + listingid).className = "controlPhoneHide";
	document.getElementById("phoneNumber" + listingid).className = "controlPhoneShow";
}
function showWeb(listingid, default_url) {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (exc) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ex) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = false;
			}
		}
	}
	if (xmlhttp) {
		xmlhttp.open("GET", default_url + '/countwebclick.php?listing_id=' + listingid, true);
		xmlhttp.send(null);
	}
	document.getElementById("webLink" + listingid).className = "controlWebHide";
	document.getElementById("webAddress" + listingid).className = "controlWebShow";
}
function showEmail(listingid, default_url) {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (exc) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ex) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = false;
			}
		}
	}
	if (xmlhttp) {
		xmlhttp.open("GET", default_url + '/countemailclick.php?listing_id=' + listingid, true);
		xmlhttp.send(null);
	}
	document.getElementById("emailLink" + listingid).className = "controlEmailHide";
	document.getElementById("emailAddress" + listingid).className = "controlEmailShow";
}
function showFax(listingid, default_url) {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (exc) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ex) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = false;
			}
		}
	}
	if (xmlhttp) {
		xmlhttp.open("GET", default_url + '/countfaxclick.php?listing_id=' + listingid, true);
		xmlhttp.send(null);
	}
	document.getElementById("faxLink" + listingid).className = "controlFaxHide";
	document.getElementById("faxNumber" + listingid).className = "controlFaxShow";
}
function readCookie(name) {
	var eq = name + "=";
	var ca = document.cookie.split(';');
	if (!ca.length) return null;
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(eq) == 0) return unescape(c.substring(eq.length, c.length));
	}
	return null;
}
function includeInCookie(property_id, site_path, type) {
	if (!isNaN(property_id)) {
		var name = "bookmark" + type;
		var d = new Date();
		if (!site_path) site_path = "/";
		d.setTime(d.getTime() + (15 * 24 * 60 * 60 * 1000));
		var expires = '; expires=' + d.toGMTString();
		var bookmark = readCookie("bookmark" + type);
		if (!bookmark) bookmark = "'" + property_id + "'";
		else {
			if (bookmark.indexOf("'" + property_id + "'") == -1) {
				bookmark = bookmark + "," + "'" + property_id + "'";
			}
		}
		document.cookie = name + '=' + escape(bookmark) + expires + '; path=' + site_path;
		alert(showText(LANG_JS_FAVORITEADD));
		load_favs();
	}
}
function removeFromCookie(property_id, site_path, type, box) {
	var name = "bookmark" + type;
	var d = new Date();
	if (!site_path) site_path = "/";
	d.setTime(d.getTime() + (15 * 24 * 60 * 60 * 1000));
	var expires = '; expires=' + d.toGMTString();
	if (isNaN(property_id)) {
		if (property_id == "all") {
			var bookmark = "";
			document.cookie = name + '=' + escape(bookmark) + expires + '; path=' + site_path;
		}
	} else {
		var bookmark = readCookie("bookmark" + type);
		if (bookmark.length > 0) {
			if (bookmark.indexOf("'" + property_id + "'") > -1) {
				finalvar = bookmark.indexOf("'" + property_id + "'") + property_id.length + 3;
				var aux = "";
				aux = bookmark.substr(0, bookmark.indexOf("'" + property_id + "'"));
				aux += bookmark.substr(finalvar);
				bookmark = aux;
			}
		}
		len = bookmark.length;
		len--;
		if (bookmark.lastIndexOf(",") == len) {
			bookmark = bookmark.substr(0, len);
		}
		document.cookie = name + '=' + escape(bookmark) + expires + '; path=' + site_path;
		alert(showText(LANG_JS_FAVORITEDEL));
	}
	if (box) {
		load_favs();
	} else {
		window.location.reload();
	}
}

