<!-- author, publisher, copyright ARiS websitewerk - www.websitewerk.nl-->
function checkchar(mychar) {
if ((mychar=="N") || (mychar=="E") || (mychar=="n") || (mychar=="e") || (mychar=="O") || (mychar=="o")) return "signpositive";
if ((mychar=="S") || (mychar=="W") || (mychar=="s") || (mychar=="w") || (mychar=="Z") || (mychar=="z") || (mychar=="-") || (mychar=="_")) return "signnegative";
if ((mychar=="0") || (mychar=="1") || (mychar=="2") || (mychar=="3") || (mychar=="4") || (mychar=="5")) return "value";
if ((mychar=="6") || (mychar=="7") || (mychar=="8") || (mychar=="9")) return "value";
if (mychar==" ") return "space";
if (mychar==".") return "point";
if (mychar==",") return "comma";

}

function latlongauto(latitudelongitude) {
lat1first = 0; lat1last = 0;
lat2first = 0; lat2last = 0;
lat3first = 0; lat3last = 0;
long1first = 0; long1last = 0;
long2first = 0; long2last = 0;
long3first = 0; long3last = 0;
state = "start";
lattype = "unknown";
longtype = "unknown";
latfactor = 1.0;
longfactor = 1.0; index=0;
while ((index < latitudelongitude.length) && (index < 51) && (state!="longready"))
{
	result = checkchar (latitudelongitude.substr(index, 1));
		//alert (state);
	if (state=="start"){
		if (result=="signnegative") {latfactor=-1.0;}
		if (result=="value") {state="lat1found"; lat1first = index;}
	} else if (state=="lat1found") {
		if (result=="point")  {lattype="D";}
		if ((result!="value")&&(result!="point"))  {// comma or others
			if ((lattype=="D")||(result=="comma")) {
				state="latready"; lat1last = index-1;
			} else {
				state="lat1ready"; lat1last = index-1;
			}
		}
	} else if (state=="lat1ready") {
		if (result=="value")  {state="lat2found"; lat2first = index;}
	} else if (state=="lat2found") {
		if (result=="point")  {lattype="M";}
		if ((result!="value")&&(result!="point"))  {// comma or others
			if ((lattype=="M")||(result=="comma")) {
				state="latready"; lat2last = index-1;
			} else {
				state="lat2ready"; lat2last = index-1;
			}
		}
	} else if (state=="lat2ready") {
		if (result=="value")  {state="lat3found"; lat3first = index;}
	} else if (state=="lat3found") {
		if (result=="point")  {lattype="S";}
		if ((result!="value")&&(result!="point"))  {// comma or others
			state="latready"; lat3last = index-1;
		}
	} else if (state=="latready") {
		if (result=="signnegative") {longfactor=-1.0;}
		if (result=="value") {
			if (index == latitudelongitude.length-1) {
				state="longready"; long1first = index; long1last = index;
			} else {
				state="long1found"; long1first = index;
			}
		}
	} else if (state=="long1found") {
		if (result=="point")  {longtype="D";}
		if ((result!="value")&&(result!="point"))  {// comma or others
			if (longtype=="D") {
				state="longready"; long1last = index-1;
			} else {
				state="long1ready"; long1last = index-1;
			}
		} else if (index == latitudelongitude.length-1) {
			state="longready"; long1last = index;
		}
	} else if (state=="long1ready") {
		if (result=="value")  {state="long2found"; long2first = index;}
	} else if (state=="long2found") {
		if (result=="point")  {longtype="M";}
		if ((result!="value")&&(result!="point"))  {// comma or others
			if (longtype=="M") {
				state="longready"; long2last = index-1;
			} else {
				state="long2ready"; long2last = index-1;
			}
		} else if (index == latitudelongitude.length-1) {
			state="longready"; long2last = index;
		}
	} else if (state=="long2ready") {
		if (result=="value")  {state="long3found"; long3first = index;}
	} 
	if (state=="long3found") {// one digit at the end is possible
		if ((result!="value")&&(result!="point")) {
			state="longready"; long3last = index-1;
		} else if (index == latitudelongitude.length-1) {
			state="longready"; long3last = index;
		}		
	}
	index++;
}
if (state=="longready"){
	newlatitude = parseFloat (latitudelongitude.substr(lat1first, lat1last - lat1first + 1));
	if ((lat2first!=0) && (lat2last!=0)) {
		minutes = parseFloat (latitudelongitude.substr(lat2first, lat2last - lat2first + 1));
		if (minutes>=60.0) {
			state="error";
		} else {
			newlatitude = newlatitude + (minutes / 60.0);
		}
	}
	if ((lat3first!=0) && (lat3last!=0)) {
		seconds = parseFloat (latitudelongitude.substr(lat3first, lat3last - lat3first + 1));
		if (seconds>=60.0) {
			state="error";
		} else {
			newlatitude = newlatitude + (seconds / 3600.0);
		}
	}
	newlongitude = parseFloat (latitudelongitude.substr(long1first, long1last - long1first + 1));
	if ((long2first!=0) && (long2last!=0)) {
		minutes = parseFloat (latitudelongitude.substr(long2first, long2last - long2first + 1));
		if (minutes>=60.0) {
			state="error";
		} else {
			newlongitude = newlongitude + (minutes / 60.0);
		}
	}
	if ((long3first!=0) && (long3last!=0)) {
		seconds = parseFloat (latitudelongitude.substr(long3first, long3last - long3first + 1));
		if (seconds>=60.0) {
			state="error";
		} else {
			newlongitude = newlongitude + (seconds/ 3600.0);
		}
	}
}
if (state=="error"){
	document.getElementById('latlong').value = "ongeldige invoer";
} else if (state!="longready"){
	document.getElementById('latlong').value = "onbekend formaat";
} else {
	latitude = newlatitude * latfactor;
	longitude = newlongitude * longfactor;
}
}





