//-------------------------------------------------------------
// CLIENT-SIDE CALLER SCRIPTS
//
// These scripts presume the existence of the following DOM objects.
//
// The first form is used to store inputs which the test driver asp app
// uses to invoke the geographic application. To use these scripts 
// straight up, the CALLING application must have this form. Otherwise
// these scripts need to be revised for appropriate access of data inputs.
//
// (1) INPUTS -- form named "forms[0]"
// 		Including the following input objects (select tag menus)...
//			ranges,towns,sections,muni,commdist,zip
//
// The second form is required in the CALLING application, the one 
// that invokes the geographic application. If the "reference mode"
// of the application changes, then this form must also change. The
// geographic application must know the names of this form and its
// component fields.
//
// (2) OUTPUTS -- form named "frmRefmode"
// 		Including the following input objects (text tag fields)...
//			xaddr,yaddr,rng,twn,sec,muni,dist,fema
//
// tested with IE only.
//-------------------------------------------------------------


//-------------------------------------------------------------
//  VARIABLES USED FOR LINKING APPLICATIONS (caller and geographic apps)
//
//  used by makeMapBaseURL()
//-------------------------------------------------------------

var HOST_NAME = "cwgisweb.co.palm-beach.fl.us";
var WEB_SITE_NAME = "Addrmgr";

//-------------------------------------------------------------
// SCRIPTS THAT INVOKE THE GEOGRAPHIC APPLICATION
//
// include this as client-side scripts in driver asp page
//-------------------------------------------------------------

//-------------------------------------------------------------
// browseRTS()
// opens the geographic application to extent of PLSS section
// in BROWSE mode.
//-------------------------------------------------------------
function browseTRS() {
	document.forms[0].usrmode.value = "0";
	var sURL;
	var trs="";
	trs = makeTRS();
	sURL = makeMapBaseURL();
	sURL += "?Mode=BROWSE&Cmd=INIT";
	sURL += "&Layer=TRS&Key="+trs+"&Label="+trs;
	PopOutForBrowse(sURL);
}
//-------------------------------------------------------------
// updateTRS()
// opens the geographic application to extent of PLSS section
// in UPDATE mode.
//-------------------------------------------------------------
function updateRTS() {
	document.forms[0].usrmode.value = "0";
	var sURL;
	var trs="";
	var filename;	
	var filename=document.forms[0].filename.value;
	trs = makeRTS();
	sURL = makeMapBaseURL();
	sURL += "?Mode=UPDATE&Cmd=INIT";
	sURL += "&Layer=TRS&Key="+trs+"&Label="+trs;
	sURL += "&filename="+filename;
	//alert(sURL)
	PopOutForUpdate(sURL);
}

//-------------------------------------------------------------
// Supporting functions
// with references to form variables isolated here
//-------------------------------------------------------------

//-------------------------------------------------------------
// makeRTS()
// retrieves range, town, section selections
//-------------------------------------------------------------
function makeRTS() {
	var i,j,k,range,town,section;
	i = document.forms[0].ranges.selectedIndex;
	j = document.forms[0].towns.selectedIndex;
	k = document.forms[0].sections.selectedIndex;
	range = document.forms[0].ranges.options[i].text;
	town = document.forms[0].towns.options[j].text;
	section = document.forms[0].sections.options[k].text;
	return (range+town+section);
}
//-------------------------------------------------------------
// makeMapBaseURL()
// constructs URL to geographic application
//-------------------------------------------------------------
function makeMapBaseURL() {
	return "http://"+HOST_NAME+"/"+WEB_SITE_NAME+"/presentation/map.asp";
}
//-------------------------------------------------------------
// Window Functions
//-------------------------------------------------------------
function PopOutForBrowse(sURL) {
	PopOut(sURL,"_blank",540,690,"yes","yes");	//600,720
}
function PopOutForUpdate(sURL) {
	PopOut(sURL,"_blank",600,690,"yes","yes");
}
function PopOut(sURL,sTitle,iHeight,iWidth,ynScroll,ynResize) {
	var objWin;
	var features = "height="+iHeight+",width="+iWidth+",scrollbars="+ynScroll+",resizeable="+ynResize
	features += ",left=165,top=75";	//IE only, locates top-left corner of window
	objWin = window.open(sURL, sTitle, features);
	
	if (parseInt(navigator.appVersion) >=4) objWin.window.focus();
}

//-------------------------------------------------------------
// Client-side Selection Menu Functionality
//-------------------------------------------------------------

//-------------------------------------------------------------
// PLSS range-town-section structures and functions 
// for dymanic menu update
//-------------------------------------------------------------
//domains
//all ranges
var allRngs = ["35","36","37","38","39","40","41","42","43"];
//all towns
var allTwns = ["40","41","42","43","44","45","46","47","48"];
var allRngTwn = ["3543","3544","3545","3546","3547","3548","3642","3643","3644","3645",
"3646","3647","3648","3740","3741","3742","3743","3744","3745","3746","3747","3840","3841",
"3842","3843","3844","3845","3846","3847","3940","3941","3942","3943","3944","3945","3946",
"3947","4040","4041","4042","4043","4044","4045","4046","4047","4140","4141","4142","4143",
"4144","4145","4146","4147","4240","4241","4242","4243","4244","4245","4246","4247","4340",
"4341","4342","4343","4344","4345","4346","4347"];
//all Towns per Range, parallel to allRngs
var rngTwns = [
["43","44","45","46","47","48"],
["42","43","44","45","46","47","48"],
["40","41","42","43","44","45","46","47"],
["40","41","42","43","44","45","46","47"],
["40","41","42","43","44","45","46","47"],
["40","41","42","43","44","45","46","47"],
["40","41","42","43","44","45","46","47"],
["40","41","42","43","44","45","46","47"],
["40","41","42","43","44","45","46","47"]];
//all Ranges per Towns, parallel to allTwns
var twnRngs = [
["37","38","39","40","41","42","43"],
["37","38","39","40","41","42","43"],
["36","37","38","39","40","41","42","43"],
["35","36","37","38","39","40","41","42","43"],
["35","36","37","38","39","40","41","42","43"],
["35","36","37","38","39","40","41","42","43"],
["35","36","37","38","39","40","41","42","43"],
["35","36","37","38","39","40","41","42","43"],
["35","36"]];
//all sections per range-town, parallel to allRngTwn
var rngtwnSecs = [
["17","18","19","20","21","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","L1","L2","L3","L4","L5","L6"],
["01","02","03","04","05","06"],
["13","24","25","36"],
["01","12","13","14","23","24","25","26","27","29","30","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","L1","L2","L3","L4","L5","L6"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","L1","L2","L3","L4","L5","L6"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","L1"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","L1","L2","L3","L4","L5","L6"],
["01","02","03","04","05","06"],
["35","36"],
["01","02","11","12","13","14","22","23","24","25","26","27","33","34","35","36"],
["01","02","03","04","05","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","L1","L2","L3","L4","L5","L6"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","L1","L2","L3","L4","L5","L6"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","L1","L2","L3","L4"],
["31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","L1","L2","L3","L4","L5","L6"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","L1","L2","L3","L4","L5","L6"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"],
["31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","L1","L2","L3","L4","L5","L6"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","36","L1","L2","L3","L4","L5","L6"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"],
["31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","L1","L2","L3","L4","L5","L6"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["25","26","27","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"],
["30","31","32"],
["05","06","07","08","09","16","17","18","19","20","21","28","29","30","31","32","33","34"],
["03","04","05","06","07","08","09","10","14","15","16","17","18","19","20","21","22","23","27","28","29","30","31","32","33","34","35"],
["02","03","04","05","06","07","08","09","10","11","14","15","16","17","18","19","20","21","22","23","26","27","28","29","30","31","32","33","34","35"],
["02","03","04","05","06","07","08","09","10","11","14","15","16","17","18","19","20","21","22","23","26","27","28","29","30","31","32","33","34","35","41","42"],
["02","03","04","05","06","07","08","09","10","11","14","15","16","17","18","19","20","21","22","23","26","27","28","29","30","31","32","33","34","35"],
["03","04","05","06","07","08","09","10","15","16","17","18","19","20","21","22","27","28","29","30","31","32","33","34"],
["04","05","06","07","08","09","16","17","18","19","20","21","28","29","30","31","32","33"]];

//update other menus when choose range
function rngChoice() {
	var i = document.forms[0].ranges.selectedIndex;
	var rng = document.forms[0].ranges.options[i].text;
	updateTwns(rng);
	var j = document.forms[0].towns.selectedIndex;
	var twn = document.forms[0].towns.options[j].text;
	document.forms[0].towns.focus();
	//document.forms[0].ranges.focus();
	updateSecs(rng,twn);
}
//update other menus when choose town
//always giving a full list for range
function twnChoice() {
	var j = document.forms[0].towns.selectedIndex;
	var twn = document.forms[0].towns.options[j].text;
	//updateRngs(twn);
	var i = document.forms[0].ranges.selectedIndex;
	var rng = document.forms[0].ranges.options[i].text;
	updateSecs(rng,twn);
	//alert()
}
function updateRngs(twn) {
	var k = document.forms[0].ranges.selectedIndex;
	var selRng = document.forms[0].ranges.options[k].text;
	var i,j;
	i = -1;
	for (j=0;j<allTwns.length;j++) {
		if (allTwns[j] == twn) {
			i = j;
			break;
		}
	}
	if (i == -1) {
		alert("Unable to find town "+twn);
		return;
	}
	var oldIndex = -1;
	var newElt;
	while (document.forms[0].ranges.options.length > 0)	//clear menu
		document.forms[0].ranges.options.remove(0);
	for (j=0;j<twnRngs[i].length;j++) {//write rngTwns[i][j] to menu
		if (selRng == twnRngs[i][j]) oldIndex = j;
		newElt = document.createElement("OPTION");
		newElt.text = twnRngs[i][j];
		newElt.value = twnRngs[i][j];
		document.forms[0].ranges.options.add(newElt);
	}
	if (oldIndex > -1) {
		document.forms[0].ranges.options[oldIndex].selected = true;		//select oldindex
	}
}
function updateTwns(rng) {
	var k = document.forms[0].towns.selectedIndex;
	var selTwn = document.forms[0].towns.options[k].text;
	var i,j;
	i = -1;
	for (j=0;j<allRngs.length;j++) {
		if (allRngs[j] == rng) {
			i = j;
			break;
		}
	}
	if (i == -1) {
		alert("Unable to find range "+rng);
		return;
	}
	var oldIndex = -1;
	var newElt;
	while (document.forms[0].towns.options.length > 0)	//clear menu
		document.forms[0].towns.options.remove(0);
	for (j=0;j<rngTwns[i].length;j++) {		//write rngTwns[i][j] to menu
		if (selTwn == rngTwns[i][j]) oldIndex = j;
		newElt = document.createElement("OPTION");
		newElt.text = rngTwns[i][j];
		newElt.value = rngTwns[i][j];
		document.forms[0].towns.options.add(newElt);
	}
	if (oldIndex > -1) {
		document.forms[0].towns.options[oldIndex].selected = true;		//select oldindex
	}
}
function updateSecs(rng,twn) {
	var k = document.forms[0].sections.selectedIndex;
	var selSec = document.forms[0].sections.options[k].text;
	var rngtwn = rng+twn;
	var i,j;
	i = -1;
	for (j=0;j<allRngTwn.length;j++) {
		if (allRngTwn[j] == rngtwn) {
			i = j;
			break;
		}
	}
	if (i == -1) {
		alert("Unable to find range and town "+rngtwn);
		return;
	}
	var oldIndex = -1;
	var newElt;
	while (document.forms[0].sections.options.length > 0)	//clear menu
		document.forms[0].sections.options.remove(0);
	for (j=0;j<rngtwnSecs[i].length;j++) {		//write rngtwnSecs[i][j] to menu
		if (selSec == rngtwnSecs[i][j]) oldIndex = j;
		newElt = document.createElement("OPTION");
		newElt.text = rngtwnSecs[i][j];
		newElt.value = rngtwnSecs[i][j];
		document.forms[0].sections.options.add(newElt);
	}
	if (oldIndex > -1) {
		document.forms[0].sections.options[oldIndex].selected = true;		//select oldindex
	}
}

function displayBaseMapURL() {
	document.urldisplay.baseMapURL.value = makeMapBaseURL();
}
function nop() {}