
var thecgi = 'http://madison.apl.wisc.edu/cgi/';
var dropOrganizer = new dropOrg('none');
var csvCreate = false
function getDetails(lat, longi){
	StartAjaxRequest("openProfileAjax")
	ajaxRequest.open("GET", thecgi + "mad_geo_func.py?lat=" + lat + "&longi=" + longi + "&thegeo=" + document.getElementById("thegeotype").options[document.getElementById("thegeotype").selectedIndex].value, true);
	ajaxRequest.send(null); 
}
function saveResultsasCSV(){
	csvCreate = true
	updateDataTableRequest('y')
}
function openProfile(theid){	
	go("documents/" + theid + ".pdf", 'Profile')
}
function openProfileAjax(theid){	
	if(ajaxRequest.readyState == 4){
		if (ajaxRequest.responseText != ''){
			openProfile(ajaxRequest.responseText)
		}else {window.alert("Sorry no results")}
	}
}
function Left(str, n)
{
   if (n <= 0)
         return "";
   else if (n > String(str).length)
         return str;
   else
         return String(str).substring(0,n);
}
function go(where, name){
  if (Left(where,7) == 'mapping'){
	if (document.getElementById('geoNH')){
		if (where.indexOf('vari') < 0){where += "?vari=ACRES"}
		if (document.getElementById('geoNH').selectedIndex > 1) {		
			where += "&geosel=NH_assoc&key=" + document.getElementById('geoNH').options[document.getElementById('geoNH').selectedIndex].value
		}else{
			where += "&geosel=Plan_Dist&key=" + document.getElementById('geoPL').options[document.getElementById('geoPL').selectedIndex].value
		}
	}
  } 
	    var ah = screen.availHeight - 100;
    var aw = screen.availWidth - 100;			 
  var str = "resizeable=1,height=" + ah + ",width=" + aw + ",toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1";

if (name.substring(0,7)!= 'Profile'){
	name = 'Map'
}
   theWindow = window.open(where, name, str);
   theWindow.focus()
}
function gonovar(where, name){
  var ah = screen.availHeight - 100;
  var aw = screen.availWidth - 100;			 
  var str = "resizeable=1,height=" + ah + ",width=" + aw + ",toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1";

if (name.substring(0,7)!= 'Profile'){
	name = 'Map'
}
   theWindow = window.open(where, name, str);
   theWindow.focus()
}
function winShowDef(){
if(ajaxRequest.readyState == 4){
	document.getElementById('thecontent').innerHTML = ajaxRequest.responseText
}
}
function startLookup(myfield){
	StartAjaxRequest("winShowDef")
	ajaxRequest.open("GET", thecgi + "madison_getfield_data.py?myfield=" + myfield, true);
	ajaxRequest.send(null);
}
function showdef(myfield){
  var sURL = "Info.html?myfield=" + myfield			 
  var str = "resizeable=1,height=200,width= 700";
  if (window.screen) {
    var ah = 100;
    var aw = screen.availWidth - 10;

    var xc = (aw - 500) / 2;
    var yc = 30;
    str += ",left=" + xc + ",screenX=" + xc;
    str += ",top=" + yc + ",screenY=" + yc + ",toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1";
  }
   theWindow = window.open(sURL, 'Info', str);
   theWindow.focus()
}
function updateDataTableRequest(thetype, which){
	var geoids = ''
	var updatediv = ''
	var geod
	var geod2
	var geod3
	dropOrganizer.clear()

if (thetype == 'y'){
	geod = document.getElementById('geoSelection').options[document.getElementById('geoSelection').selectedIndex].value
	geod2 = document.getElementById('geoSelection2').options[document.getElementById('geoSelection2').selectedIndex].value
	geod3 = document.getElementById('geoSelection3').options[document.getElementById('geoSelection3').selectedIndex].value
	if (parseInt(geod) > 0 ){
		geoids = geod
		dropOrganizer.setIndex(geod, document.getElementById('geoSelection').selectedIndex)
		updatediv = '1'
	}
	if (parseInt(geod2) > 0 ){
		if (geod2 != geod){
			if (geoids != '') {
				geoids = geoids.toString() + '~' + geod2.toString()
				updatediv = updatediv.toString() + '$' + '2'
				dropOrganizer.setIndex(geod2, document.getElementById('geoSelection2').selectedIndex)
			}
			else {
				geoids = geod2.toString()
				updatediv = '2'
			}
		}
	}
	if (parseInt(geod3) > 0 ){
		if (geod3 != geod2){
			if  (geod3 != geod){
				if (geoids != '') {
					geoids = geoids.toString() + '~' + geod3.toString()
					updatediv = updatediv.toString() + '$' + '3'
					dropOrganizer.setIndex(geod3, document.getElementById('geoSelection3').selectedIndex)
				}
				else {
					geoids = geod3.toString()
					updatediv =+'3'
				}
			}	
		}
	}
		document.getElementById('geoSelection').selectedIndex = dropOrganizer.returnindexone()
		document.getElementById('geoSelection2').selectedIndex = dropOrganizer.returnindexsecond()
		document.getElementById('geoSelection3').selectedIndex = dropOrganizer.returnindexthird()
}
else {
	if (which = 1)	{
		var selectedgeo = ''
		var geotype1 = document.getElementById('geoNH').options[document.getElementById('geoNH').selectedIndex].value
		var geotype2 = document.getElementById('geoPL').options[document.getElementById('geoPL').selectedIndex].value
		if (geotype1 !='none'){selectedgeo=geotype1}
		if (geotype2 !='none'){selectedgeo=geotype2}
		geoids = selectedgeo
	}
	updatediv = '1'
}
	if (geoids != '' ){
		if (csvCreate == true){
			csvCreate = false
			window.open(thecgi + "mad_tables.py?which=datadown&geo=" + geoids + "&tabletype=" + thetype + "&thediv=" + updatediv)
		}
		else{
			StartAjaxRequest("updateDataTable")
			ajaxRequest.open("GET", thecgi + "mad_tables.py?which=data&geo=" + geoids + "&tabletype=" + thetype + "&thediv=" + updatediv, true);
			ajaxRequest.send(null); 
		}
	}
	else {
		if (geod){geod.innerHTML = ''}
	}
}
function morePageStuff(){
if(ajaxRequest.readyState == 4){
	document.getElementById('geoselectionDrops').innerHTML = ajaxRequest.responseText
	fillInPageData('n')
}	
}
function morePageStuffPDF(){
if(ajaxRequest.readyState == 4){
	document.getElementById('divpdfdrops').innerHTML = ajaxRequest.responseText
}	
}
function morePageStuffPro(){
if(ajaxRequest.readyState == 4){
	var myString = ajaxRequest.responseText;
	var mySplitResult = myString.split("~");
	document.getElementById("sel").innerHTML = mySplitResult[0]
	document.getElementById("sel2").innerHTML = mySplitResult[1]
	document.getElementById("sel3").innerHTML = mySplitResult[2]
	fillInPageData('y')
}
}
function fillInPageData(thetype){
		updateDataTableRequest(thetype)
}
function updateDataDrops(){
if(ajaxRequest.readyState == 4){
	datadrop = document.getElementById("myDrops")
	datadrop.innerHTML = ajaxRequest.responseText;
	updateDataTableRequest(document.getElementById('geoSelection').options[document.getElementById('geoSelection').selectedIndex].value)
	
}
}
function GeoChange(id){
if (id == 'geoNH'){
	document.getElementById('geoPL').selectedIndex = 0
}
else{
	document.getElementById('geoNH').selectedIndex = 0
}
	updateDataTableRequest('n', 1)
}
function updateDataTable(){
if(ajaxRequest.readyState == 4){
	neidesc =  document.getElementById("nei_desc")
	var imagelocation = ''
	var imagepre = ''
	var myString = ajaxRequest.responseText;
	var mySplitResult = myString.split("~");
	//document.getElementById("error").innerHTML = ajaxRequest.responseText
	var myimagepathstemp = mySplitResult[1]
	var myimagespaths = myimagepathstemp.split("$")
	var myimageids = mySplitResult[2]
	var numofgeos = mySplitResult[4]
	var temp = myimageids.split("$")
	var imagetype = mySplitResult[3]
	if (imagetype == "y"){
		imagelocation = "compare_maps/"
		imagepre = "2.gif"
	}
	else {
		if (parseInt(mySplitResult[5]) != 1){document.getElementById("linktoexternal").innerHTML = "<br /><a target='_blank' href='" +  mySplitResult[5] + "'>" +   mySplitResult[5]  + "</a>" ;}
		else {document.getElementById("linktoexternal").innerHTML = "";}
		if (parseInt(mySplitResult[6]) != 0){document.getElementById("linktoexternal").innerHTML = document.getElementById("linktoexternal").innerHTML + "<br /><a target='_blank' href='" +  mySplitResult[6] + "'>" +   mySplitResult[6]  + "</a>" ;}
		imagepre = ".gif"
		imagelocation = "web_profile_maps/"}
	//var dropindex = droptempindex.split("$")
	var theimage;
	var theimagediv;
	datadrop = document.getElementById("thedata1")
	for (var i=0; i < parseInt(numofgeos); i++) {
		if (i == 0) {
			theimage = document.getElementById("neiImage")
			theimagediv = document.getElementById("myimage")
			//document.getElementById("geoSelection").selectedIndex = dropindex[i]
		}
		else if (i == 1) {
			theimage = document.getElementById("neiImage2")
			theimagediv = document.getElementById("myimage2")
			//document.getElementById("geoSelection2").selectedIndex = dropindex[i]
		}
		else if (i == 2) {
			theimage = document.getElementById("neiImage3")
			theimagediv = document.getElementById("myimage3")
			//document.getElementById("geoSelection3").selectedIndex = dropindex[i] 
		}
		else if (temp[i] == 4) {
			theimage = document.getElementById("neiImage4")
			theimagediv = document.getElementById("myimage4")
		}
		else if (temp[i] == 5) {
			theimage = document.getElementById("neiImage5")
			theimagediv = document.getElementById("myimage5")
		}	
		theimage.src = imagelocation + myimagespaths[i] + imagepre;
		theimagediv.style.display = "block"
	}
	//datadrop.innerHTML = mySplitResult[1];
	if (neidesc) {
		//neidesc.innerHTML = mySplitResult[0]
	}
	datadrop.innerHTML = mySplitResult[0]
	csvResults = mySplitResult[0]
}
}