
// Required fields: used for checkForm()
requiredFields = new Array('0001_0002', '1001_0001');

// Default tooltip width
defaultTooltipWidth = 300;

// Maximum length of a textbox
maxLength = 150;


// General functions
function $(id) {
	return document.getElementById(id);
}

// First execution
function initialize() {
	// Initialize lists
	$('field_0013_1001').innerHTML = dropdown('0013_1001', trainings);
	$('field_0013_2001').innerHTML = dropdown('0013_2001', trainings);
	$('field_0013_3001').innerHTML = dropdown('0013_3001', trainings);
	
	$('list_1003_20011').innerHTML = fillDropdown('1003_20011', crops);
	$('list_1003_20021').innerHTML = fillDropdown('1003_20021', crops);
	$('list_1003_20031').innerHTML = fillDropdown('1003_20031', crops);
	$('list_1003_20041').innerHTML = fillDropdown('1003_20041', crops);
	$('list_1003_20051').innerHTML = fillDropdown('1003_20051', crops);
	$('list_1003_20061').innerHTML = fillDropdown('1003_20061', crops);
	$('list_1003_20071').innerHTML = fillDropdown('1003_20071', crops);
	$('list_1003_20081').innerHTML = fillDropdown('1003_20081', crops);
	$('list_1003_20091').innerHTML = fillDropdown('1003_20091', crops);
	$('list_1003_20101').innerHTML = fillDropdown('1003_20101', crops);
	
	$('field_1003_20014').innerHTML = dropdown('1003_20014', months);
	$('field_1003_20024').innerHTML = dropdown('1003_20024', months);
	$('field_1003_20034').innerHTML = dropdown('1003_20034', months);
	$('field_1003_20044').innerHTML = dropdown('1003_20044', months);
	$('field_1003_20054').innerHTML = dropdown('1003_20054', months);
	$('field_1003_20064').innerHTML = dropdown('1003_20064', months);
	$('field_1003_20074').innerHTML = dropdown('1003_20074', months);
	$('field_1003_20084').innerHTML = dropdown('1003_20084', months);
	$('field_1003_20094').innerHTML = dropdown('1003_20094', months);
	$('field_1003_20104').innerHTML = dropdown('1003_20104', months);
	
	$('field_1003_20016').innerHTML = dropdown('1003_20016', months);
	$('field_1003_20026').innerHTML = dropdown('1003_20026', months);
	$('field_1003_20036').innerHTML = dropdown('1003_20036', months);
	$('field_1003_20046').innerHTML = dropdown('1003_20046', months);
	$('field_1003_20056').innerHTML = dropdown('1003_20056', months);
	$('field_1003_20066').innerHTML = dropdown('1003_20066', months);
	$('field_1003_20076').innerHTML = dropdown('1003_20076', months);
	$('field_1003_20086').innerHTML = dropdown('1003_20086', months);
	$('field_1003_20096').innerHTML = dropdown('1003_20096', months);
	$('field_1003_20106').innerHTML = dropdown('1003_20106', months);
	
	$('field_2001_1011').innerHTML = fillDropdown('2001_1011', fertilisers);
	$('field_2001_1021').innerHTML = fillDropdown('2001_1021', fertilisers);
	$('field_2001_1031').innerHTML = fillDropdown('2001_1031', fertilisers);
	$('field_2001_1041').innerHTML = fillDropdown('2001_1041', fertilisers);
	$('field_2001_1051').innerHTML = fillDropdown('2001_1051', fertilisers);
	$('field_2001_1061').innerHTML = fillDropdown('2001_1061', fertilisers);
	$('field_2001_1071').innerHTML = fillDropdown('2001_1071', fertilisers);
	$('field_2001_1081').innerHTML = fillDropdown('2001_1081', fertilisers);
	
	$('field_2001_1012').innerHTML = dropdown('2001_1012', fertiliser_apps);
	$('field_2001_1022').innerHTML = dropdown('2001_1022', fertiliser_apps);
	$('field_2001_1032').innerHTML = dropdown('2001_1032', fertiliser_apps);
	$('field_2001_1042').innerHTML = dropdown('2001_1042', fertiliser_apps);
	$('field_2001_1052').innerHTML = dropdown('2001_1052', fertiliser_apps);
	$('field_2001_1062').innerHTML = dropdown('2001_1062', fertiliser_apps);
	$('field_2001_1072').innerHTML = dropdown('2001_1072', fertiliser_apps);
	$('field_2001_1082').innerHTML = dropdown('2001_1082', fertiliser_apps);
	
	$('field_2001_2011').innerHTML = fillDropdown('2001_2011', fertilisers);
	$('field_2001_2021').innerHTML = fillDropdown('2001_2021', fertilisers);
	
	$('field_2001_2012').innerHTML = dropdown('2001_2012', fertiliser_apps);
	$('field_2001_2022').innerHTML = dropdown('2001_2022', fertiliser_apps);
	
	$('field_2007_1002').innerHTML = dropdown('2007_1002', frequencies);
	$('field_2007_2002').innerHTML = dropdown('2007_2002', frequencies);
	
	$('field_2003_4102').innerHTML = dropdown('2003_4102', soil_types);
	$('field_2003_4202').innerHTML = dropdown('2003_4202', soil_types);
	$('field_2003_4302').innerHTML = dropdown('2003_4302', soil_types);
	
	$('field_2005_1001').innerHTML = fillDropdown('2005_1001', soil_constraints);
	$('field_2005_2001').innerHTML = fillDropdown('2005_2001', soil_constraints);
	$('field_2005_3001').innerHTML = fillDropdown('2005_3001', soil_constraints);
	$('field_2005_4001').innerHTML = fillDropdown('2005_4001', soil_constraints);
	$('field_2005_5001').innerHTML = fillDropdown('2005_5001', soil_constraints);
	
	$('field_2006_1001').innerHTML = fillDropdown('2006_1001', chemicals);
	$('field_2006_2001').innerHTML = fillDropdown('2006_2001', chemicals);
	$('field_2006_3001').innerHTML = fillDropdown('2006_3001', chemicals);
	$('field_2006_4001').innerHTML = fillDropdown('2006_4001', chemicals);
	$('field_2006_5001').innerHTML = fillDropdown('2006_5001', chemicals);

//-- FEB 09 Reinstated 
	$('field_4002_3101').innerHTML = fillDropdown('4002_3101', pasture_types, 40);
	$('field_4002_3201').innerHTML = fillDropdown('4002_3201', pasture_types, 40);
	$('field_4002_3301').innerHTML = fillDropdown('4002_3301', pasture_types, 40);
	$('field_4002_3401').innerHTML = fillDropdown('4002_3401', pasture_types, 40);
	
	$('field_4002_5101').innerHTML = dropdown('4002_5101', pasture_grazing_types);
	$('field_4002_5201').innerHTML = dropdown('4002_5201', pasture_grazing_types);
	$('field_4002_5301').innerHTML = dropdown('4002_5301', pasture_grazing_types);
	$('field_4002_5401').innerHTML = dropdown('4002_5401', pasture_grazing_types);
	
	// Initialize help texts in the order they appear in the document
	// First retrieve the actual "help" images
	var _imgs = document.getElementsByTagName('img');
	var helpimgs = new Array();
	for(var i=0; i<_imgs.length; i++)
		if(_imgs[i].className.indexOf("help") > -1)
			helpimgs.push(_imgs[i]);
	
	// Stick a name for the help identifier
	for(var i=0; i<helpimgs.length; i++) {
		helpimgs[i].name = 'h' + i;
		helpimgs[i].onmouseover = showTooltipHandler;
		helpimgs[i].onmouseout = hideTooltipHandler;
	}
	
	// Final initialization
	showform($('sec_1a'));
	changestate($('0001_0009').value);
	updateAreas(); // In case of a soft refresh
}


var currentSection = -1;
var sections = new Array('sec_1a','sec_1b', 'sec_2a', 'sec_2b', 'sec_2c', 'sec_3', 'sec_4', 'sec_5', 'sec_6', 'sec_7', 'sec_8', 'sec_9', 'sec_10', 'sec_11', 'sec_12', 'sec_13', 'sec_14', 'sec_15', 'sec_16');

// Switch form view to the specified section
function showform(linkObj) {
	// Reset scroll position
	window.location.href = linkObj.href;
	
	var _id = linkObj.id;
	// Hide every section until no more form has the id, showing the specified one.
	for(var i=0; i<sections.length; i++) {
		var sectionLink = $(sections[i]);
		var section = $(sections[i]+"_f");
		if(!sectionLink || !section) continue;
		if(sections[i] == _id) {
			show(section);
			currentSection = i;
			sectionLink.removeAttribute('href');
			sectionLink.style.color = "black";
			sectionLink.onclick = null;
			$('sec_title').innerHTML = sectionLink.innerHTML;
		} else {
			hide(section);
			sectionLink.href = '#topofform';
			sectionLink.onclick = function(event) { showform(this) };
			sectionLink.style.color = "";
		}
	}
	
	var prevbutton = $('but_prevform');
	var nextbutton = $('but_nextform');
	
	if(currentSection == 0) {
		// No previous form
		prevbutton.removeAttribute('href');
		prevbutton.onclick = null;
		prevbutton.style.color = "gray";
	} else {
		prevbutton.href = '#';
		prevbutton.onclick = function(event) { return prevform(); };
		prevbutton.style.color = "black";
	}
	
	if(currentSection == sections.length - 1) {
		// No next form
		nextbutton.removeAttribute('href');
		nextbutton.onclick = null;
		nextbutton.style.color = "gray";
	} else {
		nextbutton.href = '#';
		nextbutton.onclick = function(event) { return nextform(); };
		nextbutton.style.color = "black";
	}
}

// User clicked on previous form
function prevform() {
	showform( $(sections[currentSection-1]) );
}

// User clicked on next form
function nextform() {
	showform( $(sections[currentSection+1]) );
}

// The state the user is located has been changed
function changestate(state) {
	
	// Only the first section requires some more complex logic, the rest can be
	// classified into QLD & NNSW, WA, and others.
	
	// Switch 1
	// NGR/CBR
	
//	switch(state) {
//	case "QLD":
//	case "Northern NSW":
//	case "Southern NSW":
//	case "VIC":
//	case "SA":
		// NGR
//		$('txt_0000_1002').innerHTML = "NGR No";
//		break;
	
//	default:
		// CBR
//		$('txt_0000_1002').innerHTML = "CBR No";
//	}
	
	
	// Switch 2
	// NRM Body
	
	switch(state) {
	case "QLD":
		// Regional Group
		$('txt_0002_0004').innerHTML = "Regional Group";
		$('txt_0002_0007').innerHTML = "RG";
		break;
	
	case "WA":
		// Catchment Council
		$('txt_0002_0004').innerHTML = "Catchment Council";
		$('txt_0002_0007').innerHTML = "CC";
		break;
	
	case "VIC":
	case "TAS":
	case "Southern NSW":
	case "Northern NSW":
	case "ACT":
		// Catchment Management Authority
		$('txt_0002_0004').innerHTML = "Catchment Management Authority";
		$('txt_0002_0007').innerHTML = "CMA";
		break;
	
	case "SA":
	case "NT":
		// Catchment Board
		$('txt_0002_0004').innerHTML = "Catchment Board";
		$('txt_0002_0007').innerHTML = "CB";
		break;
	}
	
	// Populate NRM body and shire name list - use NSW in case of either of the two NSW choices
	var _state = state;
	if(_state == "Southern NSW" || _state == "Northern NSW") _state = "NSW";
	
	var nrmlist = nrm_names[_state];
	$('field_0002_1004').innerHTML = dropdown('0002_1004', nrmlist);
	$('field_0002_2004').innerHTML = dropdown('0002_2004', nrmlist);
	$('field_0002_3004').innerHTML = dropdown('0002_3004', nrmlist);
	
	var shirelist = shire_names[_state];
	$('field_0002_1001').innerHTML = dropdown('0002_1001', shirelist);
	$('field_0002_2001').innerHTML = dropdown('0002_2001', shirelist);
	$('field_0002_3001').innerHTML = dropdown('0002_3001', shirelist);
	
	if(state == "QLD" || state == "Northern NSW") {
		// Land Areas
		hide($('field_1001_1016'));
		hide($('field_1001_1021'));
		
		// Tillage
		hide($('field_1004_1003'));
		
		// Stubble
		$('txt_1005_1001').innerHTML = "Not grazed";
		$('txt_1005_1002').innerHTML = "Grazed";
		hide($('field_1005_1003'));
		hide($('field_1005_1004'));
		hide($('field_1005_1005'));
		$('txt_1005_1006').innerHTML = "Baled, mulched etc";
		hide($('field_1005_1008'));
		
		// Soil conditioners
		hide($('field_2002_1001'));
		hide($('field_2002_2001'));
		
		// Chemicals title
		$('txt_2006').innerHTML = "Selected Soil Residual Chemicals Used";
		
	} else {
		if(state == "WA") {
			// Land Areas
			show($('field_1001_1016'));
			show($('field_1001_1021'));
			
			// Stubble
			$('txt_1005_1001').innerHTML = "Intact at Planting - standing, no grazing";
			$('txt_1005_1002').innerHTML = "Not Standing (eg Grazed, Slashed / Mulched / Incorporated)";
			show($('field_1005_1003'));
			$('txt_1005_1003').innerHTML = "Cool Burn";
			show($('field_1005_1004'));
			show($('field_1005_1005'));
			$('txt_1005_1005').innerHTML = "Stubble Raking and Windrow Burning - whole paddock";
			$('txt_1005_1006').innerHTML = "Stubble Baled";
			show($('field_1005_1008'));
			
		} else {
			// Land Areas
			show($('field_1001_1016'));
			hide($('field_1001_1021'));
			
			// Stubble
			$('txt_1005_1001').innerHTML = "Intact at Planting";
			$('txt_1005_1002').innerHTML = "Not Standing (eg Slashed / Mulched / Incorporated)";
			show($('field_1005_1003'));
			$('txt_1005_1003').innerHTML = "Late Burn";
			show($('field_1005_1004'));
			show($('field_1005_1005'));
			$('txt_1005_1005').innerHTML = "Stubble Raking and Windrow Burning";
			$('txt_1005_1006').innerHTML = "Stubble Baled";
			show($('field_1005_1008'));
			
		}
		
		// Tillage
		show($('field_1004_1003'));
		
		// Soil conditioners
		show($('field_2002_1001'));
		show($('field_2002_2001'));
		
		// Chemicals title
		$('txt_2006').innerHTML = "Selected Chemicals Used";
	}
}

// Recalculate the areas and propagate the changes throughout the document

function updateAreas() {
	
	// Farm
	
	var _item1 = parseFloat($('1001_1001').value);
	if(isNaN(_item1)) _item1 = 0;
	
	var _group1 = document.getElementsByName('txt_1001_1001');
	for(var i=0;i<_group1.length;i++)
		_group1[i].innerHTML = _item1;
	
	// Crop
	// [Update] Fodder crop
	
	var _item21 = parseFloat($('1001_1012').value);
	var _item22 = parseFloat($('1001_1013').value);
	if(isNaN(_item21)) _item21 = 0;
	if(isNaN(_item22)) _item22 = 0;
	_item2 = _item21 + _item22;
	
	var _group2 = document.getElementsByName('txt_1001_1012_3');
	for(var i=0;i<_group2.length;i++)
		_group2[i].innerHTML = _item2;
	
	// Pasture
	
	var _item3 = parseFloat($('1001_1014').value);
	if(isNaN(_item3)) _item3 = 0;
	
	var _group3 = document.getElementsByName('txt_1001_1014');
	for(var i=0;i<_group3.length;i++)
		_group3[i].innerHTML = _item3;
	
	// [Update]
	// NAT VEG
	
	var _item4 = parseFloat($('1001_1015').value);
	if(isNaN(_item4)) _item4 = 0;
	
	var _group4 = document.getElementsByName('txt_1001_1015');
	for(var i=0;i<_group4.length;i++)
		_group4[i].innerHTML = _item4;
	
	// NAT VEG
	
	var _item5 = parseFloat($('1001_1022').value);
	if(isNaN(_item5)) _item5 = 0;
	
	var _group5 = document.getElementsByName('txt_1001_1022');
	for(var i=0;i<_group5.length;i++)
		_group5[i].innerHTML = _item5;
	
	// Fodder
	
	var _item6 = parseFloat($('1001_1022').value);
	if(isNaN(_item6)) _item6 = 0;
	
	var _group6 = document.getElementsByName('txt_1001_1022');
	for(var i=0;i<_group6.length;i++)
		_group6[i].innerHTML = _item6;
	
}


// HTML/CSS functions

function fillDropdown(id, values, fillboxSize) {
	if(fillboxSize == undefined) fillboxSize = 20;
	
	var list = '';
	list += '<select id="'+id+'" size="1" name="'+id+'" onchange="doFill(this)">';
	list += fillList(values);
	list += "</select>";
	list += '<br />';
	list += '<input type="text" id="'+id+'_" size="'+fillboxSize+'" name="'+id+'_" maxlength="'+maxLength+'" class="hide">';
	
	return list;
}

function dropdown(id, values) {
	var list = '';
	list += '<select id="'+id+'" size="1" name="'+id+'">';
	list += fillList(values);
	list += "</select>";
	return list;
}

function fillList(values) {
	var list = "";
	for(var i=0;i<values.length;i++)
		list += '<option value="'+(values[i].substr(0,2) == '..' ? '' : values[i])+'">'+values[i]+'</option>';
	return list;
}

function doFill(list) {
	var pri = list.id;
	var sec = pri + '_';
	var txtBox = $(sec);
	if(list.value == "Other") {
		list.name = sec;
		txtBox.name = pri;
		show(txtBox);
	} else {
		list.name = pri;
		txtBox.name = sec;
		hide(txtBox);
	}
}

function show(htmlObj) {
	htmlObj.style.display = 'block';
}

function hide(htmlObj) {
	htmlObj.style.display = 'none';
}

function checkForm() {
	// Check required fields
	for(var i=0; i<requiredFields.length; i++) {
		if( $(requiredFields[i]).value == '' ) {
			alert('Please complete all required fields before continuing');
			return false;
		}
	}
	
	// Ask user to confirm
	return confirm("Are you sure you want to submit?");
}

function calcLength(obj) {
	$('8001_01_length').innerHTML = obj.value.length;
}

<!-- NEW FEB 09 -->
<!-- Additional edit field length calculation -->
function calcLength2(obj) {
	$('8001_0002_length').innerHTML = obj.value.length;
}

function calcLength3(obj) {
	$('8001_0003_length').innerHTML = obj.value.length;
}
function calcLength4(obj) {
	$('8001_0004_length').innerHTML = obj.value.length;
}

function showTooltip(pos, txt, width) {
	// Default width
	if(width == undefined) width = defaultTooltipWidth;
	
	// Get scroll positions
	var scrollX = window.pageXOffset ? window.pageXOffset : document[(document.compatMode == 'CSS1Compat') ? 'documentElement' : 'body'].scrollLeft;
	var scrollY = window.pageYOffset ? window.pageYOffset : document[(document.compatMode == 'CSS1Compat') ? 'documentElement' : 'body'].scrollTop;
	
	var tooltip = $('tooltip');
	tooltip.innerHTML = txt;
	tooltip.style.width = width + "px";
	tooltip.style.left = scrollX + pos.x + "px";
	tooltip.style.top = scrollY + pos.y + "px";
	
	show(tooltip);
}

// Wrapper for executing showTooltip(...) as event handler in IMG tag
function showTooltipHandler(e) {
	if(!e) e = event; // IE Compatibility
	
	var id = parseInt(this.name.substr(1));  // Starts with 'h'
	// Ignore if somehow the help id is messed up
	if(isNaN(id)) return;
	
	var txt = form_help[id];
	
	var pos = {
		'x': e.clientX + document.body.scrollLeft,
		'y': e.clientY + 15 + document.body.scrollTop
	};
	
	// See if any width is specified
	var _class = this.className;
	_class = _class.replace('help', '').replace(/ /, '');  // remove 'help' and clean
	
	var width = parseInt(width);
	
	// No width is specified
	if(isNaN(width))
		showTooltip(pos, txt);
	else
	// Use specified width
		showTooltip(pos, txt, width);
}

function hideTooltip() {
	hide($('tooltip'));
}

// NOT IMPLEMENTED: handler for hiding
hideTooltipHandler = hideTooltip;

