// Written By David Fitzpatrick
// Map interface Java Script Package


var version = 1.11;
function Autover() {
	parent.frames.contents.document.info.ver.value=version;
}

function WarnEt(){

	if(parent.frames.main.document.retrive.kc.checked){
		alert("Please Note: Starting date must be the \ndate of planting for your crop, \nor accumulated heat units can be \nsubstuted in the base offset for GDD box.");
	}

	PlantSelect();
}


function PlantSelect(){
	var plant_data;
	var checked;

	//Find which button is selected then determin which temp type to use
	//Default is Fahrenheit, check to see if we need to convert to Cels
	for(var i=0; i<parent.frames.main.document.retrive.Format.length; i++){
		if(parent.frames.main.document.retrive.Format[i].checked){
			checked = i;
			break;
		}
	}

	//Find which plant type was selected and get values from html
	for(var i=0; i<parent.frames.main.document.retrive.plant.length; i++){
		if(parent.frames.main.document.retrive.plant[i].selected){
			plant_data = parent.frames.main.document.retrive.plant[i].value;
			break;
		}
	}

	//Set the values for GDD by plant type
	if(plant_data.length > 5){ 	//Test to see if the data is correct, at least 3 nums + 2 delimeters
		var max_cut, min_cut, base, intercept, c1, c2, c3;
		var delim;

		delim = plant_data.indexOf(',');
		max_cut = plant_data.substring(0,delim);

		plant_data = plant_data.substring(delim+1,plant_data.length);
		delim = plant_data.indexOf(',');
		min_cut = plant_data.substring(0,delim);

		plant_data = plant_data.substring(delim+1,plant_data.length);
		delim = plant_data.indexOf(':');
		base = plant_data.substring(0,delim);

		plant_data = plant_data.substring(delim+1,plant_data.length);
		delim = plant_data.indexOf(',');
		intercept = plant_data.substring(0,delim);

		plant_data = plant_data.substring(delim+1,plant_data.length);
		delim = plant_data.indexOf(',');
		c1 = plant_data.substring(0,delim);

		plant_data = plant_data.substring(delim+1,plant_data.length);
		delim = plant_data.indexOf(',');
		c2 = plant_data.substring(0,delim);

		plant_data = plant_data.substring(delim+1,plant_data.length);
		c3 = plant_data;

		// Convert Temps to metric if selected type is in metric
		if(checked == 2 || checked == 7){
			max_cut = 5/9*(max_cut-32);
			min_cut = 5/9*(min_cut-32);
			base	  = 5/9*(base-32);
			c1      = (9/5)*c1;
			c2      = (9/5)*(9/5)*c2;
			c3      = (9/5)*(9/5)*(9/5)*c3;
		}

		parent.frames.main.document.retrive.HUmax.value = max_cut;
		parent.frames.main.document.retrive.HUmin.value = min_cut;
		parent.frames.main.document.retrive.HUbas.value = base;
		parent.frames.main.document.retrive.intercept.value = intercept;
		parent.frames.main.document.retrive.c1.value = c1;
		parent.frames.main.document.retrive.c2.value = c2;
		parent.frames.main.document.retrive.c3.value = c3;

	}
	else{ alert("Plant Type Configuration Error: Incorrect format: " + plant_data);}

	//Check to see KC is wanted
	if(checked > 0 && checked < 7){
		if(parent.frames.main.document.retrive.kc.checked){
			parent.frames.main.document.retrive.kc.click();
			alert("Crop Coeff for ET is not avaliable for selected convert type.\nMust choose convert type English or Metric");
		}
	}

}

function AutoDate() {

	today = new Date();
	var temp = today;

	if((today.getMonth()+ 1) < 10){
		TheMonth = "0" + (today.getMonth()+ 1);
		TheMonth2= TheMonth - 1;
		TheMonth2= "0" + TheMonth2;
	}
	else{
		TheMonth = (today.getMonth()+ 1);
		TheMonth2= TheMonth - 1;
	}

	//TheYear = today.getYear();
	TheYear = today.getFullYear();

	if(today.getDate() < 10){
		TheDate = "0" + today.getDate();
	}
	else{
		TheDate = today.getDate();
	}

	parent.frames.main.document.retrive.begmm.value="01";
	parent.frames.main.document.retrive.begdd.value="01";
	parent.frames.main.document.retrive.begyy.value=TheYear;

	parent.frames.main.document.retrive.endmm.value=TheMonth;
	parent.frames.main.document.retrive.enddd.value=TheDate;
	parent.frames.main.document.retrive.endyy.value=TheYear;
}


function Get_station_name(id){
	var name;

	switch(id)
	{
		case  1  : {name = 'Airport, Albuquerque ';break;} 
		case  2  : {name = 'Ag Science Center: Alcalde ';break;} 
		case  3  : {name = 'Ag Science Center: Artesia ';break;} 
		case  4  : {name = 'Carlsbad - CR10 #3 ';break;} 
		case  5  : {name = 'Carlsbad Gossym Comax ';break;} 
		case  6  : {name = 'Clayton Livestock Research Center ';break;} 
		case  7  : {name = 'Ag Science Center: Clovis ';break;} 
		case  8  : {name = 'Corona Ranch ';break;} 
		case  9  : {name = 'Cottonwood - CR10 #1 ';break;} 
		case  10 : {name = 'Cottonwood Gossym Comax ';break;} 
		case  11 : {name = 'Deming Vineyard Experimental Station ';break;} 
		case  12 : {name = 'Ag Science Center: Farmington ';break;} 
		case  13 : {name = 'NAPI ';break;} 
		case  14 : {name = 'Gila ';break;} 
		case  15 : {name = 'Grants ';break;} 
		case  16 : {name = 'Horticulture Experimental Station ';break;} 
		case  17 : {name = 'Jornada Range ';break;} 
		case  18 : {name = 'Knox Hall - New Gossym-Comax CR10 ';break;} 
		case  19 : {name = 'Knox Hall Experimental Station ';break;} 
		case  20 : {name = 'Plant Science Research Center - Las Cruces ';break;} 
		case  21 : {name = 'Los Altos, Alb ';break;} 
		case  22 : {name = 'Ag Science Center: Los Lunas ';break;} 
		case  23 : {name = 'Loving - CR10 #2 ';break;}
		case  24 : {name = 'Mora Research Center ';break;} 
		case  25 : {name = 'NMSU East Mesa, Golf Course';break;} 
		case  26 : {name = 'National Weather Service ';break;} 
		case  27 : {name = 'Albuquerque Pino ';break;} 
		case  28 : {name = 'Rio Grande Nursey-Alb ';break;} 
		case  29 : {name = 'Albuquerque Sierra ';break;} 
		case  30 : {name = 'Fort Stanton Experimental Ranch ';break;} 
		case  31 : {name = 'Ag Science Center: Tucumcari ';break;} 
		case  32 : {name = 'Berino Station - Berino ';break;} 
		case  33 : {name = 'Derry ';break;} 
		case  34 : {name = 'La Union';break;} 
		case  35 : {name = 'Boys Ranch ';break;} 
		case  36 : {name = 'Pena Blanca ';break;}
		case  37 : {name = 'Albuquerque Golf Course ';break;}
		case  38 : {name = 'Truth Or Consequences Elephant Butte ';break;}
		case  39 : {name = 'Albuquerque Bosque ';break;}
		case  40 : {name = 'N Bosque del Apache - San Antonio ';break;}
		case  41 : {name = 'S Bosque del Apache - San Antonio ';break;}
		case  42 : {name = 'Costilla ';break;}
		case  43 : {name = 'Angostura ';break;}
		case  44 : {name = 'Jarales ';break;}
		case  45 : {name = 'Luis Lopez ';break;}
		default: {name = 'Sorry This Station Does Not Have A Discription Yet';break;}
	} 
return name;
}



function Update_op(op_id){
	if(op_id){
		parent.frames.main.document.location.href="/map/retrieve.htm";
	}
	else {
		parent.frames.main.document.location.href="/map/graph.htm";
	}
}

function Remote_Select(id){
	document.selected.id.value=id;
	document.selected.current.value=Get_station_name(id);
}

function Remote_Set(){
	//Now we write to opening document so the viewer can see
	if(window.opener.document.retrive){
		window.opener.document.retrive.location.value=document.selected.id.value;
		window.opener.document.retrive.name.value=document.selected.current.value;
	}
	else if(window.opener.document.graph) {
		window.opener.document.graph.location.value=document.selected.id.value;
		window.opener.document.graph.name.value=document.selected.current.value;
	}
	else {
		if(confirm("Unable to write to main window.\nDo you wish to reset the windows and try again?")){
		window.opener.parent.location.href="/map/map.htm";
		}
	}
	self.close();
}

function Close(){
	self.close();
}

function SetStation(id){
	var name;
	name = Get_station_name(id);

	//Now we write to opening document so the viewer can see
	if(parent.frames.main.document.retrive){
		parent.frames.main.document.retrive.location.value=id;
		parent.frames.main.document.retrive.name.value=name;
	}
	else if(parent.frames.main.document.graph) {
		parent.frames.main.document.graph.location.value=id;
		parent.frames.main.document.graph.name.value=name;
	}
	else{
		if(confirm("Unable to write to main window.\nDo you wish to reset the windows and try again?")){
			parent.document.location.href="/map/map.htm";
		}
	}
}

//Global variable for following function
var cycle=0;

function open_map(map) {
  browsername=navigator.appName;
  browserversion=parseInt(navigator.appVersion);


	if(browsername == "Netscape"){
		if (map == 1){ remote = window.open("/map/nm_map_a.htm", "remote", "height=535,width=430,scrollbars=yes");   }
		if (map == 2){ remote = window.open("/map/nm_map_b.htm", "remote", "height=535,width=430,scrollbars=yes");   }
		if (map == 3){ remote = window.open("/map/nm_map_c.htm", "remote", "height=535,width=430,scrollbars=yes");   }
		if (map == 4){ remote = window.open("/map/nm_map_d.htm", "remote", "height=535,width=430,scrollbars=yes");   }
		if (map == 5){ remote = window.open("/map/nm_map_1.htm", "remote", "height=535,width=430,scrollbars=yes");   }
		if (map == 6){ remote = window.open("/map/nm_map_2.htm", "remote", "height=535,width=430,scrollbars=yes");   }
		remote.focus();	
	}
	else{
		//Handle IE's inabality to not focus
		if(cycle){ remote.close(); }
		if (map == 1){ remote = window.open("/map/nm_map_a.htm", "remote", "height=575,width=440,scrollbars=yes");   }
		if (map == 2){ remote = window.open("/map/nm_map_b.htm", "remote", "height=575,width=440,scrollbars=yes");   }
		if (map == 3){ remote = window.open("/map/nm_map_c.htm", "remote", "height=575,width=440,scrollbars=yes");   }
		if (map == 4){ remote = window.open("/map/nm_map_d.htm", "remote", "height=575,width=440,scrollbars=yes");   }
		if (map == 5){ remote = window.open("/map/nm_map_1.htm", "remote", "height=575,width=440,scrollbars=yes");   }
		if (map == 6){ remote = window.open("/map/nm_map_2.htm", "remote", "height=575,width=440,scrollbars=yes");   }

		// Total kludge but it works...
		if(map==-123211){var nop=0;}
		else{ cycle=1;
			if (map == 1){ remote = window.open("/map/nm_map_a.htm", "remote", "height=550,width=440,scrollbars=yes");   }
			if (map == 2){ remote = window.open("/map/nm_map_b.htm", "remote", "height=550,width=440,scrollbars=yes");   }
			if (map == 3){ remote = window.open("/map/nm_map_c.htm", "remote", "height=550,width=440,scrollbars=yes");   }
			if (map == 4){ remote = window.open("/map/nm_map_d.htm", "remote", "height=550,width=440,scrollbars=yes");   }
			if (map == 5){ remote = window.open("/map/nm_map_1.htm", "remote", "height=550,width=440,scrollbars=yes");   }
			if (map == 6){ remote = window.open("/map/nm_map_2.htm", "remote", "height=550,width=440,scrollbars=yes");   }
		}
	}

} 

