// GET MILES
var thisFar="";
function getMiles(howFar) {
	if(thisFar!="" || thisFar=="0") {
		if(parseFloat(howFar)>parseFloat(thisFar)) { netDistance=returnPrecision1Float((howFar-thisFar));alert(netDistance+" Miles");}
			else { netDistance=returnPrecision1Float((thisFar-howFar));alert(netDistance+" Miles");}
		thisFar="";
		howFar=0;
		} else { thisFar=parseFloat(howFar);}
	}
function returnPrecision1Float(theFloat) {
	revisedFloat="";
	theFloat=theFloat+"";
	if(theFloat.indexOf(".")<0) {
		return theFloat+".0";
		} else {
			return (Math.round(parseFloat(theFloat)*10)/10);
			}
	}


/* POPUP: (function: PopTool) */
function pop_tool(a) {
   var argv=pop_tool.arguments;
   var rstamp = new Date();
   var params="width=" + ((argv[1])?argv[1]:600);
       params+=",height="+((argv[2])?argv[2]:450);
       params+=",scrollbars=1,resizable=1";
   var x_target = 'new'+rstamp.getTime();
   var x_url = a;
   x_target=((x_target)?x_target:"_blank");
   var popWindow=window.open(x_url,x_target,params);	
   popWindow.focus();
   return false;
   }


// CALCULATE DISTANCE

thisFar="";
sort=0;
titles=new Array("NORTHBOUND","SOUTHBOUND");
relinks=new Array("<a class=green href=shelters-auto.html?sort=0>NORTHBOUND<\/a>","<a class=green href=shelters-auto.html?sort=1>SOUTHBOUND<\/a>");




/* PARSE_FORM (begin all) */

/* PARSE_FORM (init values array) */
var formdata=new Array();
/* PARSE_FORM (end init) */

/* PARSE_FORM (begin parse_form) */
function parse_form(){
    if (document.location.search.indexOf('=')>-1) { 
        keyvalues=unescape(document.location.search.substring('1').replace(/\+/g,' ')).split("&");
        for(p=0;p<keyvalues.length;p++) {
            pairs=keyvalues[p].split("=");
            formdata[pairs[0]]=pairs[1];
            }            
        }
    }
/* PARSE_FORM (end parse_form) */

/* PARSE_FORM (end all) */




// PARSE FORM

function ParseForm() {
	if(document.location.search.indexOf('=')>-1) {
		formvalsues=document.location.search.substring('1').split('&');
		for(v=0;v<formvalsues.length;v++) {
			tempEq=formvalsues[v].replace(/=/,"='");
			eval(tempEq+"'");
			}
		}
	}

ParseForm();
var thisTitle=titles[sort];
var thisRelink=((sort==1)?0:1);
	thisRelink=relinks[thisRelink];



// PRINT SECTION

function printSection(y) {
	document.writeln("<table cellpadding=3 cellspacing=3 width=100%>");
	printSectionHeading(y);
	document.writeln("<\/table>");
	}

// PRINT SECTION TOWNS

function printSectionTowns(y) {
	document.writeln("<p><table cellpadding=3 cellspacing=3 width=90%>");
	thisSection=atSections[y];
	thisSectionId=thisSection.sectionId;
	for(i=0;i<93;i++) {
		thisFeature=featureRecords[i];
		if(thisFeature.featureSection==thisSectionId) {
			document.write(writeFeatureRow(thisFeature.featureName,thisFeature.featureAtMile,thisFeature.featureOffTrail));
			}
		}
	document.writeln("<\/table>");
	}

// PRINT SECTION TOWNS & SHELTERS

function printSectionSheltersTowns(y) {
	document.writeln("<table cellpadding=3 cellspacing=3 width=90%>");
	printSectionMiles(y);		
	document.writeln("<\/table>");
	}

// PRINT SECTION TOWNS & SHELTERS: HOT

function printSectionSheltersTownsHot(y) {
	document.writeln("<table cellpadding=3 cellspacing=3 width=90%>");
	printSectionMilesHot(y);		
	document.writeln("<\/table>");
	}


// PRINT SECTION FEATURES

function printSectionFeatures(y) {
	document.writeln("<p><table cellpadding=3 cellspacing=3 width=90%>");
	printSectionFeatureRecords(y);		
	document.writeln("<\/table>");	
	}







// GET MILES

function getMiles(howFar) {
	if(thisFar!="" || thisFar=="0") {
		if(parseFloat(howFar)>parseFloat(thisFar)) { netDistance=returnPrecision1Float((howFar-thisFar));alert(netDistance);}
			else { netDistance=returnPrecision1Float((thisFar-howFar));alert(netDistance);}
		thisFar="";
		howFar=0;
		} else { thisFar=parseFloat(howFar);}
	}

// PRINT AT RECORD TABLE: ALL SHELTERS

function printRecordRows() {
	titleRow="";
	titleRow+="<tr><td bgcolor=#ffffff width=300 class=SansBlack10>";
	titleRow+="SHELTER/TOWN";
	titleRow+="<\/td><td bgcolor=#ffffff width=100 class=SansBlack10>";
	titleRow+="AT MILE";
	titleRow+="<\/td><\/tr>";
	document.write(titleRow);
	if(sort==0) {
		for(i=0;i<385;i++) {
			if(trailMileRecords[i].atRecordType==2) {
				document.write(makeShelterRowHot(i));
				}
			if(trailMileRecords[i].atRecordType==1) {
				document.write(makeTownRowHot(i));
				}
			}
		} else {
			for(i=384;i>0;i--) {
				if(trailMileRecords[i].atRecordType==2) {
					document.write(makeShelterRowHot(i));
					}
				if(trailMileRecords[i].atRecordType==1) {
					document.write(makeTownRowHot(i));
					}
				}
			}
	}

// MAKE AT RECORD TABLE: SHELTER ROW

function makeShelterRow(r) {
	shelterRow="";
	shelterRow+="<tr><td bgcolor=#ffffff width=220 class=SansBlack11>";
	shelterRow+=trailMileRecords[r].atShelterTown;
	shelterRow+="<\/td><td bgcolor=#ffffff width=80 class=SansBlack11 align=right>";
	shelterRow+=trailMileRecords[r].atRecordMile;
	shelterRow+="<\/td><\/tr>";
	return shelterRow;
	}
	
// MAKE AT RECORD TABLE: TOWN ROW

function makeTownRow(r) {
	townRow="";
	townRow+="<tr><td bgcolor=#eeeeee width=220 class=SansBlack11>";
	townRow+=trailMileRecords[r].atShelterTown.toUpperCase();
	townRow+="<\/td><td bgcolor=#eeeeee width=80 class=SansBlack11 align=right>";
	townRow+=trailMileRecords[r].atRecordMile;
	townRow+="<\/td><\/tr>";
	return townRow;
	}


// MAKE AT RECORD TABLE: SHELTER ROW: HOT

function makeShelterRowHot(r) {
	shelterRow="";
	shelterRow+="<tr><td bgcolor=#eeeeee width=220 class=SansBlack11>";
	shelterRow+="<a class=green href=javascript:getMiles("+trailMileRecords[r].atRecordMile+") style=text-decoration:none;>"+trailMileRecords[r].atShelterTown+"<\/a>";
	shelterRow+="<\/td><td bgcolor=#eeeeee width=80 class=SansBlack11 align=right>";
	shelterRow+=trailMileRecords[r].atRecordMile;
	shelterRow+="<\/td><\/tr>";
	return shelterRow;
	}
	
// MAKE AT RECORD TABLE: TOWN ROW: HOT

function makeTownRowHot(r) {
	townRow="";
	townRow+="<tr><td bgcolor=#ddffee width=220 class=SansBlack11>";
	townRow+="<a class=green href=javascript:getMiles("+trailMileRecords[r].atRecordMile+") style=text-decoration:none;>"+trailMileRecords[r].atShelterTown.toUpperCase()+"<\/a><\/b>";
	townRow+="<\/td><td bgcolor=#ddffee width=80 class=SansBlack11 align=right>";
	townRow+=trailMileRecords[r].atRecordMile;
	townRow+="<\/td><\/tr>";
	return townRow;
	}


// RETURN PRECISION FLOAT
function returnPrecision1Float(theFloat) {
	revisedFloat="";
	theFloat=theFloat+"";
	if(theFloat.indexOf(".")<0) {
		return theFloat+".0";
		} else {
			return (Math.round(parseFloat(theFloat)*10)/10);
			}
	}

// SET SHELTER FORM	

function setShelterForm(f)	{
	theShelter=	shelterRecords[f];	
	document.forms[0].atMilesInput.value=parseFloat(theShelter.shelterAtMile)+" mi";
	document.forms[0].atNorthInput.value=returnMilesNorth(f);
	document.forms[0].atSouthInput.value=returnMilesSouth(f);
	}

// RETURN MILES NORTH
function returnMilesNorth(mn) {
	thisAtMile=shelterRecords[mn].shelterAtMile;
	if(thisAtMile<2164) {
		atMilesNorth=returnPrecision1Float(parseFloat(shelterRecords[mn+1].shelterAtMile-thisAtMile));
		atMilesNorth+=" mi";
		} else 	{ 
			atMilesNorth="";
			}
	return 	atMilesNorth;			

	}
// RETURN MILES SOUTH
function returnMilesSouth(ms) {
	thisAtMile=shelterRecords[ms].shelterAtMile;
	if(thisAtMile>1) {
		atMilesSouth=returnPrecision1Float(parseFloat(thisAtMile-shelterRecords[ms-1].shelterAtMile));
		atMilesSouth+=" mi";
		} else 	{ 
			atMilesSouth="";
			}
	return 	atMilesSouth;			
	}
// MAP IMAGES

sectionMap=new Array(14)
sectionMap[0]="nc-ga-map.gif";
sectionMap[1]="tn-nc-map.gif";
sectionMap[2]="swva-map.gif";
sectionMap[3]="cent-va-at.gif";
sectionMap[4]="shen-at-map.gif";
sectionMap[5]="wv-at-map.jpg";
sectionMap[6]="pa-at-map.jpg";
sectionMap[7]="nj-at-map.jpg";
sectionMap[8]="ny-at-map.jpg";
sectionMap[9]="ct-at-map.gif";
sectionMap[10]="ma-at-map.gif";
sectionMap[11]="vt-at-map.gif";
sectionMap[12]="nh-at-map.gif";
sectionMap[13]="me-at-map.gif";

// SET MAP IMAGE

function setImage(p) {
	theImage="";
	imageName=sectionMap[p];
	theImage="<img src=sections/"+imageName+">";
	return theImage;
	}


// GO TO SECTION

function goToSection(h) {
	sectionHref="";
	sectionHref+="sections-auto.html?section="+h;
	document.location.href=sectionHref;
	}

// GO TO SECTION SHELTERS

function goToSectionShelters(h) {
	sectionHref="";
	sectionHref+="sections-shelters-auto.html?section="+h;
	document.location.href=sectionHref;
	}

// RETURN SECTION ID

function returnSectionId() {
	theSectionId=document.location.search.substring('1');
	if(theSectionId.indexOf("section=")>-1) {
		theSectionId=theSectionId.replace("section=","");
		} else {
			theSectionId=0;
			}
	return theSectionId;
	} 




// MAKE SECTION HEADING

function printSectionHeading(s) {
	thisSection=atSections[s];
	thisSectionId=thisSection.sectionId;
	thisSectionName=thisSection.sectionName;
	thisSectionAtMile=thisSection.sectionAtMile;
	thisSectionStartMile=thisSection.sectionStartMile;
	thisSectionEndMile=thisSection.sectionEndMile;
	document.write(writeSectionRow(thisSectionName,thisSectionAtMile,thisSectionStartMile,thisSectionEndMile));	
	}

// MAKE SECTION SHELTER - TOWN TABLE
// PRINT SECTION MILES

function printSectionMiles(s) {
	thisSection=atSections[s];
	thisSectionStart=parseFloat(thisSection.sectionStartMile);
	thisSectionEnd=parseFloat(thisSection.sectionEndMile);
	for(i=0;i<385;i++) {
		thisFeature=trailMileRecords[i];
		if(parseFloat(thisFeature.atRecordMile) >= thisSectionStart &&
			parseFloat(thisFeature.atRecordMile) <= thisSectionEnd) {
			if(trailMileRecords[i].atRecordType==2) {
				document.write(makeShelterRow(i));
				}
			if(trailMileRecords[i].atRecordType==1) {
				document.write(makeTownRow(i));
				}
			}
		}
	
	}

function printSectionMilesHot(s) {
	thisSection=atSections[s];
	thisSectionStart=parseFloat(thisSection.sectionStartMile);
	thisSectionEnd=parseFloat(thisSection.sectionEndMile);
	for(i=0;i<385;i++) {
		thisFeature=trailMileRecords[i];
		if(parseFloat(thisFeature.atRecordMile) >= thisSectionStart &&
			parseFloat(thisFeature.atRecordMile) <= thisSectionEnd) {
			if(trailMileRecords[i].atRecordType==2) {
				document.write(makeShelterRowHot(i));
				}
			if(trailMileRecords[i].atRecordType==1) {
				document.write(makeTownRowHot(i));
				}
			}
		}
	
	}

	
// PRINT SECTION FEATURES RECORD
function printSectionFeatureRecords(f) {
	for(c=0;c<featureMax;c++) {
		if(trailFeatures[c].sectionId==f) {
			document.write(returnSectionFeatureRecordsRow(c));
			}
		}
	}
// PRINT SECTION FEATURES ROW
function returnSectionFeatureRecordsRow(t) {
	sectionFeatureRow="";
	sectionFeatureRow+="<tr><td width=180 bgcolor=#ffffff class=SansBlack11>"+trailFeatures[t].featureName+"<\/td>";	
	sectionFeatureRow+="<td width=70 bgcolor=#ffffff class=SansBlack11 align=right>"+trailFeatures[t].featureElevation+"<\/td><\/tr>";
	return sectionFeatureRow;
	}
	
// WRITE SECTION ROW

function writeSectionRow(thisSectionName,thisSectionAtMile,thisSectionStartMile,thisSectionEndMile) {
	sectionRow="";
	sectionRow+="<tr><td align=center class=SansBlack14>"+thisSectionName.toUpperCase()+"<br>";
	sectionRow+="<span class=SansBlack11>Total Miles: "+thisSectionAtMile+"<br>";
	sectionRow+="AT Miles: "+thisSectionStartMile;
	sectionRow+=" - "+thisSectionEndMile+"<\/td><\/tr>";
	return sectionRow;
	}

// WRITE FEATURE ROW

function writeFeatureRow(thisFeatureName,thisFeatureAtMile,thisFeatureOffTrail) {
	featureRow="";
	featureRow+="<tr><td width=180 bgcolor=#ffffff class=SansBlack11>"+thisFeatureName+"<\/td>";
	featureRow+="<td width=70 bgcolor=#ffffff class=SansBlack11 align=right>"+thisFeatureAtMile+"<\/td><\/tr>";
	return featureRow;
	}
	
/* DOCUEMENT.WRITE ALIASES */

function print(str) {
	document.write(str);
	}

function echo(str) {
	document.write(str);
	}




/* POPUP: ALIAS */

function PopTool (a) {
   var argv=PopTool.arguments;
	return pop_tool(a,argv[1],argv[2]);
	}

/* POPUP: (end PopTool) */


/* POPUP IMAGE: (function: pop_img) */
function pop_img(a) {
   var argv=pop_img.arguments;
   var params="width=" + ((argv[1])?argv[1]:600);
       params+=",height="+((argv[2])?argv[2]:450);
       params+=",scrollbars=1,resizable=1";
   var x_target = 'new';
   var x_url = "pop.html?photo="+a;
   x_target=((x_target)?x_target:"_blank");
   var popWindow=window.open(x_url,x_target,params);	
   popWindow.focus();
   return false;
   }
/* POP_IMG: (end PopTool) */

function PopImage(whereIsIt) {
	args=PopImage.arguments;
	pitchurUrl="http://www.sophiaknows.com/testbed/imv/preview.html?imgsrc="+whereIsIt;
	params="width="+(((args[1])?args[1]:500))+",height="+(((args[2])?args[2]:450))+",scrollbars=yes,resizable=yes"
	pitchurWindow=window.open(pitchurUrl,"",params);
	return false;
	}


/*   TYPEDOWN (begin main) */

/*   TYPEDOWN (begin define objects) */

function optionprops() {                                  // select props storage object
	this.o_value=new Array();
	this.o_text=new Array();
	}

var options=new Array();                                  // list of select objects

/*   TYPEDOWN (end define objects) */

/*   TYPEDOWN (begin initialize objects) */

function InitSelectVals(s) {
	options[s.name]=new optionprops();
	for(i=0;i<s.length;i++) {
		options[s.name].o_value[i]=s.options[i].value;
		options[s.name].o_text[i]=s.options[i].text;
		}
	}

/*   TYPEDOWN (end initialize objects) */

/*   TYPEDOWN (begin typedown) */

function TypeDown(s,t) {
   var y=0;                                                 // init match counter
   var suggestions=new optionprops();                       // init suggestion object
   for(m=0;m<options[s.name].o_text.length;m++) {           // if matches
      if(options[s.name].o_text[m].toLowerCase().indexOf(t.value.toLowerCase())>-1) {
         suggestions.o_text[y]=options[s.name].o_text[m];      // store text
         suggestions.o_value[y]=options[s.name].o_value[m];    // store value
         y++;                                                  // increment counter
         }
      }
   s.length=suggestions.o_text.length;                      // reset select length
   for(i=0;i<suggestions.o_text.length;i++) {               // populate suggestions
      s.options[i].text=suggestions.o_text[i];
      s.options[i].value=suggestions.o_value[i];      
      }
   s.selectedIndex=0;                                       // select first option
   }

/*   TYPEDOWN (end typedown) */

/*   TYPEDOWN (global init selects) */

function InitSelects() {
	var elements="";
	for(f=0;f<document.forms.length;f++) {
		for(e=0;e<document.forms[f].length;e++) {
			if(document.forms[f].elements[e].name
			   && document.forms[f].elements[e].type.indexOf('select')>-1) {
				InitSelectVals(document.forms[f].elements[e]);
				}
			}
		}
	return elements;
	}

/*   TYPEDOWN (end main) */

/* --------------------------------- */
/*          WEBBUG v. 3.0            */
/* --------------------------------- */
/*        JAVASCRIPT LIBRARY         */
/* --------------------------------- */
/*  Created:  2001-11-01             */
/*  Issued:   2001-11-01             */
/*  Modified: 2006-02-22             */
/*  Component: JS LIBRARY            */
/*  Copyright (c) 2001-2006          */
/*  A.R. Tony Pisarra, SophiaKnows   */
/* --------------------------------- */
/* --------------------------------- */

var turl="http://www.sophiaknows.com/cgi-bin/sKspacer.pl";

// COUNTER: INIT COUNTER
function counter_image(pagename) {
	counter=new Counter(pagename);
	if(document.location.href.indexOf('http')>-1) {
		document.write(counter.bugtag());
		}
	}

// COUNTER: CONSTRUCTOR
function Counter(pagename) {
	this.pagename=pagename;
	this.referrer=escape(document.referrer);
	}

//	COUNTER: BUGTAG
Counter.prototype.bugtag = function() {
	var bugimg="<img src=\""+turl+"?";
	bugimg+="pagename="+this.pagename;
	bugimg+="&browser="+this.get_browser();
	bugimg+="&version="+this.get_version();
	bugimg+="&platform="+this.get_platform();
	bugimg+="&resolution="+this.get_resolution();
	bugimg+=((this.referrer)?"&referrer="+this.referrer:"");
	bugimg+="\" height=1 width=1 border=0 />";
	return bugimg;
	}

// COUNTER: GET BROWSER
Counter.prototype.get_browser=function() {
	var Browser;
	Browser=((navigator.appName == "Microsoft Internet Explorer")?"MSIE":Browser);
	Browser=((navigator.appName == "Netscape")?"Netscape":Browser);
	Browser=((navigator.userAgent.indexOf("Firefox") > -1)?"Firefox":Browser);
	Browser=((navigator.userAgent.indexOf("Camino") > -1)?"Camino":Browser);
	Browser=((navigator.userAgent.indexOf("Safari") > -1)?"Safari":Browser);
	Browser=((navigator.userAgent.indexOf("Opera") > -1)?"Opera":Browser);
	return Browser=((Browser)?Browser:"Other");
	}

// COUNTER: GET VERSION
Counter.prototype.get_version = function() {
	var v=navigator.appVersion;
	return ((v.indexOf("MSIE") > 0) ? v.substring(v.indexOf("MSIE")+5,v.indexOf("MSIE")+6) : parseFloat(v));
	}

// COUNTER: GET PLATFORM
Counter.prototype.get_platform = function() {
	var Platform;
	Platform=((navigator.userAgent.indexOf("Win") > -1)?"Win":Platform);
	Platform=((navigator.userAgent.indexOf("Mac") > -1)?"Mac":Platform);
	return ((Platform)?Platform:"Other");
	}

// COUNTER: GET RESOLUTION
Counter.prototype.get_resolution = function() {
	return ((this.get_version()>3)?screen.width+"x"+screen.height:"n/a");
	}


