  var isNetscape;
  var Parishes;
  var firstParish = "";
  var changingText = 0;
  var	MarkName, MarkNumber, MarkStamp;
  var SearchRadius, MarkStatus, AccuracyH, AccuracyV, MarkDatum, TrigsOnly;
  var DirectoryRef;
	var f;
	var error_handler;

	 
function checkBrowser ()
{
  isNetscape = (navigator.appName == "Netscape");
}

function checkParameters(searchType)
{
	
//alert ("lcs checkParameters for " + searchType);
	
		f = document.forms[0];

//	get values from form depending on search type
		
		var result = false;

		if (searchType == "SurveyMark") result = getSurveyMark ();
		if (searchType == "Directory") result = getDirectory ();
		if (searchType == "Coordinates") result = getCoordinates ();
		if (searchType == "Plan") result = getLotPlan ();
		if (searchType == "Address") result = getAddress ();
		if (searchType == "Crown") result = getCrownDesc ();
		if (searchType == "SurveyLabel") result = getSurveyLabel ();
		if (searchType == "Feature") result = getFeature ();
	
		if (searchType == "ReserveMarks") result = getReserveMarks ();
		if (searchType == "ClearReserved") result = true;


		if (! result) return;

//	get the common criteria if a survey mark search

		if (f.searchOrigin.value == "SMES")
		{
			if (searchType != "ReserveMarks" &&
					searchType != "ClearReserved") 
			{
				result = getCriteria ();
				if (! result) return;
			}
		}
		f.searchType.value = searchType;

		if (searchType == "Directory")
		{
			f.PAGE.value = trim(f.PAGE.value);
			f.REFERENCE.value = trim(f.REFERENCE.value);
		}
		
		if (searchType == "Address") { submitForm("stageOneAddress"); return; }
		if (searchType == "Coordinates") { submitForm("stageOneCoordinates"); return; }
		if (searchType == "Crown") { submitForm("stageOneCrown"); return; }
		if (searchType == "Directory") { submitForm("stageOneDirectory"); return; }
		if (searchType == "Plan") { submitForm("stageOneLotPlan"); return; }

	  	f.submit ();

}

function submitSMESForm(searchType)
{
		f = document.forms[0];

		f.searchType.value = searchType;
  	f.submit ();

}

function getCriteria ()
{

		var haveRadius = 0;
		var haveUserRadius = 0;
		for (i = 0; i < f.elements.length; i ++)
		{
			if (f.elements[i].name == "radius" && f.elements[i].type == "select-one") haveRadius = 1;
			if (f.elements[i].name == "userradius") haveUserRadius = 1;
		}
		
 		SearchRadius = 500;
		if (haveRadius) SearchRadius = parseInt(f.radius.options[f.radius.selectedIndex].value);
		if (haveUserRadius)
		{
			if (SearchRadius == -1) SearchRadius = parseInt(f.userradius.value);
			if (isNaN(SearchRadius)) SearchRadius = 0;
			f.userradius.value = SearchRadius;
		}
		if (SearchRadius > 10000)
		{
			alert ("Please enter a Radial Search distance less than 10000 m");
			return false;
		}
  	if (f.markStatus[0].checked) MarkStatus = "All";
  	if (f.markStatus[1].checked) MarkStatus = "Available";
  	AccuracyH = f.accuracyH.options[f.accuracyH.selectedIndex].value;
  	AccuracyV = f.accuracyV.options[f.accuracyV.selectedIndex].value;
  	if (f.markDatum[0].checked) MarkDatum = "AGD66";
  	if (f.markDatum[1].checked) MarkDatum = "GDA94";
  	TrigsOnly = f.trigsOnly.checked;
  	return true;
}	

function setUserRadius()
{
		f = document.forms[0];
		var haveRadius = 0;
		for (i = 0; i < f.elements.length; i ++)
		{
			if (f.elements[i].name == "radius" && f.elements[i].type == "select-one") haveRadius = 1;
		}
		
 		SearchRadius = 0;
		if (haveRadius) SearchRadius = parseInt(f.radius.options[f.radius.selectedIndex].value);
		if (SearchRadius >= 0) f.userradius.value = SearchRadius;
}

function getSurveyMark ()
{

	  MarkName = trim(f.markName.value);
	  var MarkNum = trim(f.markNumber.value);
  	MarkNumber = parseInt(MarkNum);
  	MarkStamp = trim(f.markStamp.value);

    if (MarkName.length == 0 && MarkNum.length == 0 && MarkStamp.length == 0) 
    {
    	alert ("You must enter something to search for");
    	return false;
    }
    
//	Mark name must have at least 3 chars

  	if (MarkName.length > 0)
  	{
  		if (MarkName.length < 3)
	  	{
		  	alert ("You must enter at least 3 non-blank characters of the mark name");
			  f.markName.focus();
  			return false;
	  	}
  	}

//	Mark number must have at least 4 digits and no alpha chars

  	if (! isNum(MarkNum))
  	{
	  	alert ("9 digit number must be numeric characters only");
  		f.markNumber.focus();
	  	return false;
  	}
  	if (! isNaN(MarkNumber) && MarkNumber > 0)
	  {
		  result = String(MarkNumber);
  		if (result.length < 4)
	  	{
		  	alert ("You must enter at least 4 characters of the 9 digit number");
			  f.markNumber.focus();
  			return false;
	  	}
		  f.markNumber.value = Number(MarkNumber);
  	}
  	
  	return true;
}

function getDirectory ()
{
//  check directory reference 
//  must have page number, letter, number
//  page number must be reasonable


		var page = trim(f.PAGE.value);
		var ref = trim(f.REFERENCE.value);
		
    DirectoryRef = page + ref;

    if (DirectoryRef == "") return false;
    var dirtype;

    if (f.DIRECTORY[0].checked) dirtype = "M";
    if (f.DIRECTORY[1].checked) dirtype = "V";
    
    var page = "";
    var across = "";
    var down = "";
    var i, istart = 0;
    var c;
    
    for (i = istart; i < DirectoryRef.length; i ++)
    {
        c = DirectoryRef.charAt(i);
        if (c < '0' || c > '9') break;
        page += c;
    }
    istart = i;
    for (i = istart; i < DirectoryRef.length; i ++)
    {
        c = DirectoryRef.charAt(i);
        if (c == ' ') continue;
        if (c >= '0' && c <= '9') break;
        across += c;
    }
    istart = i;
    for (i = istart; i < DirectoryRef.length; i ++)
    {
        c = DirectoryRef.charAt(i);
        if (c == ' ') continue;
        if (c < '0' || c > '9') break;
        down += c;
    }
//alert ("DirectoryRef = " + DirectoryRef + " -> " + page + "," + across + "," + down);    
    if (page != "" && across != "" && down != "")
    {
        i = parseInt (page);
        if (dirtype == 'M' && i > 999 ||
            dirtype == 'V' && i > 999 ) 
        {
          alert ("Page number for directory reference is invalid");
          return false;
        }
        DirectoryRef = dirtype + DirectoryRef;
        
        return true;
    }
    
    alert ("Directory reference must have page number, letter, number - eg. 29E4 or 91A F 5");
    return false;
           
}

function getCoordinates()
{


	var coordType = f.coordType.options[f.coordType.selectedIndex].value;
	var centerX= parseInt(f.xcoord.value);
	var centerY= parseInt(f.ycoord.value);

	var easting = trim(f.xcoord.value);
	var northing = trim(f.ycoord.value);

	var msg = "";

	if (easting == "" && northing == "") return false;

	if (coordType != "LL66" && coordType != "LL94")
	{
		if (! isNum(easting) || (easting != "" && isNaN (centerX)))
			msg += "Easting value is invalid\n";
	
		if (! isNum(northing) || (northing != "" && isNaN (centerY)))
			msg += "Northing value is invalid\n";
		
		if (msg != "") 
		{
			alert (msg);
			return false;
		}
	}

	if (isNaN(centerX) && isNaN(centerY)) return false;

//  do quick check on coordinate values

	if (coordType == "VG66")
	{
    		if (centerX > 2050000 && centerX < 3050000 && centerY > 4000000 && centerY < 5000000) return true;
  	}

	if (coordType == "VG94")
	{
    		if (centerX > 2050000 && centerX < 3050000 && centerY > 2000000 && centerY < 3000000) return true;
  	}

	if (coordType == "AMG54" || coordType == "MGA54")
  	{
    		if (centerX > 400000 && centerX < 1000000 && centerY > 5500000 & centerY < 6350000) return true;
  	}

	if (coordType == "AMG55" || coordType == "MGA55")
  	{
    		if (centerX > 100000 && centerX < 1000000 && centerY > 5500000 & centerY < 6350000) return true;
  	}

//	latitude/longitude must allow for different formats
//	spaces can separate deg/min/sec - eg.  37 48 08
//	space can separate deg/min - eg.  37 48  or  37 48.5
//	number can be decimal degrees - eg.  37.6

	if (coordType == "LL66" || coordType == "LL94")
	{
		if (!isNumSpace (easting)) msg += "Latitude value is invalid\n";
		if (!isNumSpace (northing)) msg += "Longitude value is invalid\n";
		if (msg != "") 
		{
			alert (msg);
			return false;
		}
		
//	split on spaces to check for multiple fields

		fields = f.xcoord.value.split(" ");

		d = m = s = 0;
				
		d = parseFloat(fields[0]);
		if (fields.length >= 2) m = parseFloat(fields[1]);
		if (fields.length >= 3) s = parseFloat(fields[2]);

		if (isNaN(d) || isNaN(m) || isNaN(s))
			msg += "The latitude value is invalid\n";
		
		d = Math.abs(d);
		m = Math.abs(m);
		s = Math.abs(s);
		if (! ((d >= 30 && d <= 40) || (d >= 140 && d <= 151)) || m < 0 || m > 60 || s < 0 || s > 60)
			msg += "The latitude value is not in range\n";

		centerX = d + m / 60 + s / 3600;

		fields = f.ycoord.value.split(" ");
		d = m = s = 0;
		d = parseFloat(fields[0]);
		if (fields.length >= 2) m = parseFloat(fields[1]);
		if (fields.length >= 3) s = parseFloat(fields[2]);
		if (isNaN(d) || isNaN(m) || isNaN(s))
			msg += "The longitude value is invalid\n";
		d = Math.abs(d);
		m = Math.abs(m);
		s = Math.abs(s);
		if (! ((d >= 30 && d <= 40) || (d >= 140 && d <= 151)) || m < 0 || m > 60 ||s < 0 || s > 60)
			msg += "The longitude value is not in range\n";

		if (msg != "") 
		{
			alert (msg);
			return false;
		}
		
		centerY = d + m / 60 + s / 3600;

//	swap lat/long if needed - we want X to be latitude, Y to be longitude

		if (centerX > centerY)
		{
			d = centerX;
			centerX = centerY;
			centerY = d;
		}
 		if (centerX > 33 && centerX < 40 && centerY > 140 && centerY < 151) return true;
	}


//	to get here, point is not in Victoria

	alert ("The coordinates are invalid or this point is not in Victoria.\nPlease correct the coordinates and try again");
	return false;

}
function getLotPlan ()
{
		var theLot= trim(f.LOT.value);
		var theType =f.PLAN_TYPE.options[f.PLAN_TYPE.selectedIndex].value;
		var thePlanNo=trim(f.PLAN.value);

	  if(thePlanNo=="")
	  {
		  alert("You must enter at least the Plan Number");
		  return false;
		}
		return true;		
}

function getAddress()
{
		var theStreetName=trim(f.STREET_NAME.value);
		var theStreetType=f.STREET_TYPE.options[f.STREET_TYPE.selectedIndex].value;
		var theLocality=trim(f.LOCALITY.value);
		if (theStreetName == "" && theLocality == "")
		{
				alert ("You must enter at least one of street name and locality");
				return false;
		}
		return true;
}

function getCrownDesc()
{
		var theAllot=trim(f.ALLOTMENT.value);
		var theSection=trim(f.SECTION.value);
		var theSubdivision=trim(f.SUBDIVISION.value);
		var thePortion=trim(f.PORTION.value);
		var theBlock=trim(f.BLOCK.value);
		var theParish=trim(f.parishName.value);
		
  	if (theParish.length > 0 && theParish.charAt(0) >= "A")
  	{

  		var ParishU = theParish.toUpperCase();
  		var ListU = "";
  		if (f.parishList.selectedIndex >= 0) ListU = f.parishList.options[f.parishList.selectedIndex].value.toUpperCase();
  		if (ListU == "") // (ParishU != ListU) 
	  	{
		  	alert ("You must select a parish/township name from the list");
			  f.parishName.focus();
  			return false;
	  	}
	  	f.parishName.value = f.parishList.options[f.parishList.selectedIndex].value;
  	}
		
		if (f.name != "IMFCrownSearch")
		{
			if (theAllot == "" && theSection == "" && 
					theSubdivision == "" && thePortion == "" 
					&& theBlock == "")
			{
				alert ("You must enter at least one field other than Parish/Township\nEnter NO SEC as the section if there is no section");
				return false;
			}
		}
		if (theParish.length == 0)
		{
				alert ("You must select a parish/township name from the list");
				return false;
		}
		

		return true;
}

function getSurveyLabel()
{
		var theLabel=trim(f.surveyLabel.value);
		if (theLabel == "")
		{
				alert ("You must enter something to search for");
				return false;
		}
		f.surveyLabel.value = theLabel.toUpperCase();
		return true;
}

function getFeature()
{
		var theSuburb=trim(f.suburbName.value);
		if (theSuburb == "")
		{
				alert ("You must enter something to search for");
				return false;
		}
		f.suburbName.value = theSuburb.toUpperCase();
		return true;
}


function trim (oldString)
{
//	trim leading and trailing spaces
	return oldString.replace(/^\s+|\s+$/g, '') ;	
}



function isNum (value)
{
	var OK = true;
	var c;
	var points = 0;
	
	for (var i = 0; i < value.length; i ++)
	{
		c = value.charAt(i);
		if (c != '.' && (c < '0' || c > '9')) OK = false;
		if (c == '.') points ++;
	}
	if (points > 1) OK = false;
	return OK;
}		

function isNumSpace (value)
{
	var OK = true;
	var c;
	
	for (var i = 0; i < value.length; i ++)
	{
		c = value.charAt(i);
		if (c != ' ' && c != '.' && (c < '0' || c > '9')) OK = false;
	}
	return OK;
}		

function upperCase (textObject)
{
	text = textObject.value.toUpperCase ();
	textObject.value = text;
}

function clearAll (searchType)
{
  var f = document.forms[0];

	if (searchType == "ReserveMarks")
	{
		f.markName.value = "";
		makePList (f.markName, "P");
		f.markCount.selectedIndex = 0;
		return;
	}

//	SMES searches

	if (searchType == "Coordinates")
	{
  	f.coordType.selectedIndex = 7;  
  	f.xcoord.value = "";
  	f.ycoord.value = ""
  	
		if (typeof(f.mapRadius) != "undefined") f.mapRadius.selectedIndex = 1;
  }
  if (searchType == "SurveyMark")
  {
  	f.markName.value = "";
  	f.markNumber.value = "";
  	f.markStamp.value = "";
		makePList (f.markName, "P");
	}
  if (searchType == "Directory")
  {
		f.PAGE.value = "";
		f.REFERENCE.value = "";
		f.DIRECTORY[0].checked = true;
	}
	if (searchType == "Plan")
	{
	  f.LOT.value="";
    f.PLAN.value="";
	}
	if (searchType == "Address")
	{
    f.UNIT_NUMBER.value="";
    f.STREET_NUMBER.value="";
    f.STREET_NAME.value="";
    f.STREET_TYPE.selectedIndex=0; 
    f.LOCALITY.value="";
    f.POST_CODE.value="";
	}
	
	if (searchType == "Crown")
	{ 
    if (typeof(f.ALLOTMENT) != "undefined") f.ALLOTMENT.value="";
    if (typeof(f.SECTION) != "undefined") f.SECTION.value="";
    if (typeof(f.SUBDIVISION) != "undefined") f.SUBDIVISION.value="";
    if (typeof(f.PORTION) != "undefined") f.PORTION.value="";
    if (typeof(f.BLOCK) != "undefined") f.BLOCK.value="";
    if (typeof(f.allotment) != "undefined") f.allotment.value="";
    if (typeof(f.section) != "undefined") f.section.value="";
    f.parishName.value="";
		makePList (f.parishName, "PT");
	}

	if (searchType == "SurveyLabel")
	{
    f.surveyLabel.value="";
	}

	if (searchType == "Feature")
	{
    f.suburbName.value="";
	}


}

function clearCriteria ()
{
//	mark name does not have radius to clear	

  var f = document.forms[0];
	var userradius = "";
	
	for (var i = 0; i < f.elements.length; i ++)
	{
		if (f.elements[i].name == "radius" && f.elements[i].type == "select-one") 
		{
			f.radius.selectedIndex = 1;
			userradius = f.radius.options[f.radius.selectedIndex].value;
		}
	}

  f.markDatum[0].checked = true;  // GDA
  f.markStatus[0].checked = true;  // Available
  f.accuracyH.selectedIndex = 0;
  f.accuracyV.selectedIndex = 0;
  f.trigsOnly.checked = false;
  if (typeof(f.userradius) != "undefined") f.userradius.value = userradius;

}

function clearField(fieldName)
{
//  clear markName/markNumber when markNumber/markName changes

		f = document.forms[0];
		for (i = 0; i < f.elements.length; i ++)
		{
			if (f.elements[i].name == fieldName) f.elements[i].value = "";
		}
}

function setSelectedParish ()
{
		var f=document.forms[0];
		var theParish=trim(f.parishName.value);
		
  	if (theParish.length > 0)
  	{
			if (theParish.charAt(0) >= "A")
			{
	  		var ListU = "";
	  		if (f.parishList.selectedIndex >= 0) ListU = f.parishList.options[f.parishList.selectedIndex].value;
		  	f.parishName.value = ListU;
		  }
  	}
}
