var currentpos = 0;
var LOIColour = '#CCC';
var LOIName = 'state';
var mapIsLoaded = false;

function selectLOI(n) {
	//alert("selectLOI(" + n + ")");
	if (checkMapIsLoaded()) {
		document.loiForm.loi[n].checked = true;
		currentpos = n;
		changeColour();
		changeLOILinks();
		setLOIName();
		changeSearchForm();

		if (window.frames['mapFrame'].document.mapForm!=null)
			window.frames['mapFrame'].document.mapForm.loi.value = LOIName;
	}
}

function changeColour() {
	setLOIName();
	setLOIColour();
	changeText();
	resetLOILinks();
	var linkEl = document.getElementById('loi' + currentpos).style;
	linkEl.color = LOIColour;
	linkEl.textDecoration = 'none';
	linkEl.fontWeight = 'bold';
	setLOIIdentify();
}

function changeText() {
	var reportHeadText = document.getElementById('reportHeadText').firstChild;
	if (LOIName=="state") reportHeadText.innerHTML = "Search";
	if (LOIName=="region") reportHeadText.innerHTML = "Region search";
	if (LOIName=="council") reportHeadText.innerHTML = "Council search";
	if (LOIName=="city_town") reportHeadText.innerHTML = "City/town search";
	if (LOIName=="suburb") reportHeadText.innerHTML = "Suburb search";
	if (LOIName=="property") reportHeadText.innerHTML = "Property search";

	document.getElementById('reportLine').style.backgroundColor = LOIColour;
}

function setLOIIdentify() {
	var source = "" + document.getElementById("identify").src;
	document.getElementById("identify").src = source.replace(/\loi..gif/g, "loi" + currentpos + ".gif");
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("msie 6.0")!=-1)
		window.frames['mapFrame'].changecss('.mapidentify','cursor','url(../images/tool_identify_loi' + currentpos + '.cur)');
	source = "" + document.getElementById("legend").src;
	document.getElementById("legend").src = source.replace(/\loi..gif/g, "loi" + currentpos + ".gif");

}

function setLOIColour() {

	switch (currentpos) {
		case 0:
			LOIColour = '#339900';
			break;
		case 1:
			LOIColour = '#E9CB00';
			break;
		case 2:
			LOIColour = '#FF6600';
			break;
		case 3:
			LOIColour = '#CC0000';
			break;
		case 4:
			LOIColour = '#660099';
			break;
		case 5:
			LOIColour = '#0066CC';
			break;
		default:
			LOIColour = '#000';
			break;
	}
}

function resetLOILinks() {
	
	for (var i = 0; i <= 5; i++) {
		var linkEl = document.getElementById('loi' + i).style;
		linkEl.color = '#003399';
		linkEl.textDecoration = 'underline';
		linkEl.fontWeight = 'normal';		
	}
}

function setLOIName() {

	switch (currentpos) {
		case 0:
			LOIName = 'state';
			break;
		case 1:
			LOIName = 'region';
			break;
		case 2:
			LOIName = 'council';
			break;
		case 3:
			LOIName = 'city_town';
			break;
		case 4:
			LOIName = 'suburb';
			break;
		case 5:
			LOIName = 'property';
			break;
		default:
			LOIName = 'state';
			break;
	}
}

function changeLOILinks() {
	setLOIName();
	window.frames['linksFrame'].location.href = '/iqed/map/links.jsp?loi=' + LOIName;
}

function changeSearchForm() {
	//alert(currentpos);

	resetSearchForms();	
	var loiselect = document.mapForm.loiselect;
	var loi = document.mapForm.loi;
	var cmd = document.mapForm.cmd;

	switch (currentpos) {
	case 0:
		loiselect.value = 'stateForm';
		loi.value = 'state';
		document.getElementById("stateForm").style.display = 'block';
		break;
	case 1:
		loiselect.value = 'regionForm';
		loi.value = 'region';
		document.getElementById("regionForm").style.display = 'block';
		document.getElementById("searchButtons").style.display = 'block';
		break;
	case 2:
		loiselect.value = 'councilForm';
		loi.value = 'council';
		document.getElementById("councilForm").style.display = 'block';
		document.getElementById("searchButtons").style.display = 'block';
		break;
	case 3:
		loiselect.value = 'settlementForm';
		loi.value = 'city_town';
		document.getElementById("settlementForm").style.display = 'block';
		document.getElementById("searchButtons").style.display = 'block';
		break;
	case 4:
		loiselect.value = 'suburbForm';
		loi.value = 'suburb';
		cmd.value = 'geocode';
		document.getElementById("suburbForm").style.display = 'block';
		document.getElementById("searchButtons").style.display = 'block';
		break;
	case 5:
		loiselect.value = 'propertyForm';
		loi.value = 'property';
		document.getElementById("propertyForm").style.display = 'block';
		document.getElementById("searchButtons").style.display = 'block';
		break;
	default:
		loiselect.value = 'propertyForm';
		loi.value = 'property';
		document.getElementById("propertyForm").style.display = 'block';
		document.getElementById("searchButtons").style.display = 'block';
		break;
	}

	showSearchForms();
}

function resetSearchForms() {

	document.getElementById("propertyForm").style.display = 'none';
	document.getElementById("stateForm").style.display = 'none';
	document.getElementById("regionForm").style.display = 'none';
	document.getElementById("councilForm").style.display = 'none';
	document.getElementById("settlementForm").style.display = 'none';
	document.getElementById("suburbForm").style.display = 'none';
	document.getElementById("searchButtons").style.display = 'none';
}

function checkMapIsLoaded() {
	if (mapIsLoaded){
		return true;
	} else {
		alert("Sorry, the map is still loading.");
		return false;
	}
}

function mapLoaded() {
	mapIsLoaded = true;
}