/* ------------------------------------------------------------------------------------------------
 * This is the DOQQ98 IR Mosaic Javascript for Business Code.
 * This File contains all the Functions for the DOQQMosaic web page and subpages to separate Content from 
 * Code.  Loaded before this file is the DOQQM_Data.js file which contains the data that these functions use.

 * Calls to the functions are made from the Visual aspect of the Web Page.
 * 
 * This Javascript is called by the GISDOQQMosaic.html file.
 ----------------------------------------------------------------------------------------------- */ 

	// * Function used to open up page 
	// * Called before makeTitle()
	function beginFrame()
	{
		document.writeln('');
		document.writeln('<TABLE border="0" cellSpacing="0" cellPadding="3" width="745" bgColor="#ffffff" border="0">');
			document.writeln('<TR>');
		document.writeln('<!-- Header Text -->');
				document.writeln('<TD colspan="3" align="center" vAlign="center" height="50" width="740">');
	}

	// * Non-GIS Frame Title
	// * THe GIS Frame is the Frame work provided by the IT dept. (look and feel)
	// * This is simply a String for which we want to express the first letter.
	// * Since it is called after beginFrame() it must be followed by a simple document.writeln('</td></tr>') statement.
	function makeTitle(gTitle)
	{
		firstLetter = gTitle.substr(0,1);
		remainder = gTitle.substr(1);

		lType = checkURL();
			switch(lType[0][1])
			{
				case 'City':
					remainder = remainder + '<br>' + 'by City Name';
					break;
				case 'County':
					remainder = remainder + '<br>' + 'by County Name';
					break;
				case 'Division':
					remainder = remainder + '<br>' + 'by NCDOT Division';
					break;
				case 'Map':
					remainder = remainder + '<br>' + 'by Interactive Map';
					break;
				default:
					break;
			}

		document.write('<span class=Title1>',firstLetter,'</span><span class=Title1>',remainder,'</span>');

	}

	// * Paragraph group writing function
	function paraWrite(pArray)
	{
		for(i=0;i<pArray.length;i++)
		{
			document.writeln('<span class="',pArray[i][0],'">',pArray[i][1],'</span><br><br>');
		}
	}

	// * Links to associated Pages
	function jumpLinks(lArray)
	{
		document.writeln('<TABLE BORDER="0" CELLSPACING="1" CELLPADDING="4" WIDTH="720">');
		for(i=0;i<lArray.length;i++)
		{
			document.writeln('<TR class="text2b"><td style="padding-left:35px;">',lArray[i][1].link(lArray[i][0]),'</td></tr>');
			if(lArray[i][2] != '')
				document.writeln('<TR class="text2"><td style="padding-left:55px;">',lArray[i][2],'</td></tr>');
		}
		document.writeln('</table>');
	}

	// * Grabs any variables from the URL
	// * THis function currently does not support more than 1 variable.
	function checkURL()
	{
		strURL = new String(window.location);
		startOfVars = strURL.indexOf('?');
		strVars = strURL.substr(startOfVars+1);

		varSep = strVars.indexOf('=');
		tVar = strVars.substr(0,varSep);
		tValue = strVars.substr(varSep+1);
		
		listType = new Array();
		listType[0] = [tVar, tValue];

		return listType;
	}

	// * orgList receives the Value of 'by' and outputs the appropriate organzied List.
	function orgList(by)
	{
		var d = document;
		switch(by)
		{
			case 'DOQQ':
				// * This option takes over a minute to display the data.  It is turned off.
				//document.writeln(by);
				//tArray = dMc; columns = 4;
				break;
			case 'County':
				//document.writeln(by);
				tArray = dMd; columns = 4;
				break;
			case 'City':
				//document.writeln(by);
				tArray = dMe; columns = 4;
				break;
			case 'Division':
				//document.writeln(by);
				tArray = dMf; columns = 8;
				break;
			default:
				tArray = new Array();
				break;
		}

		d.writeln('<table');
		for(tElem in tArray)
		{
			if(!(tElem=='trimArray' || tElem=='inArray' || tElem=='indexOf'))
			{
				d.writeln('<tr><td colspan="',columns+1,'">');
					d.writeln('<table><tr><th style="border-bottom:thick solid #273b7c; text-align:left;">');
				if(by=='Division') d.write('Division ');
				d.writeln(tElem);
					d.writeln('</th></tr></table>');
				d.writeln('</td></tr>');
	
				count = 0;
				d.writeln('<tr><td width="40px">&nbsp;</td>');
				for(tTile in tArray[tElem])
				{
					if(!(tTile=='trimArray' || tTile=='inArray' || tTile=='indexOf'))
					{
						if(count==columns) { d.writeln('</tr><tr>'); d.writeln('<tr><td>&nbsp;</td>'); count=0; }
						d.writeln('<td width="50px">');
						d.writeln(tArray[tElem][tTile].link('GISUTMIMosaic_Info.html?tile='+tArray[tElem][tTile]));
						d.writeln('</td>');
						count++;
					}
				}
				d.writeln('</tr>');
				d.writeln('<tr><td colspan="',(columns+1),'"><hr></td></tr>');
			}
		}
		d.writeln('</table>');

		if(by=='Map') {
			d.writeln('<hr>');
			d.writeln('<center><span class="title1" style="color:#000000;">North Carolina Color Infrared Images Grid</span> ');
			d.writeln('<br> <span class="title1" style="color:#000000; font-size:14pt;">Click on a grid tile to download the image</spsn></center>');
			d.writeln('<img src="/it/gis/graphics/ColorIRGridB.jpg" alt="Map" usemap="#IMap" border="0">'); 
			d.writeln('<table name="info" id="info" style="position:relative; left:60px; top:-120px; visibility:hidden; border-color:#000000;" border="1" bgcolor="#ffffbb" cellspacing="0" cellpadding="3"><tr><td name="infotd" id="infotd">Information Box</td></tr></table>'); 
			}
	}

	// * InfoList receives the Value of 'Tile' and outputs the appropriate Information.
	function InfoList(tile)
	{
		var d = document;
		var FTPPath = "http://dotw-xfer01.dot.state.nc.us/gisdot/"
		DataPath= FTPPath+"DOTUSGSTopo/";
		if(tile<10) filename = 'USGSTopoTile00' + tile + '.zip';
		if(tile>9 && tile<100) filename = 'USGSTopoTile0' + tile + '.zip';
		if(tile>99) filename = 'USGSTopoTile' + tile + '.zip';
		filename = DataPath + filename;

		// The only legitimate tiles are 1 through 178
		if(tile<1 || tile>178) return;
		dArray = filterByTile(tile);

		//gridArray = ['GRID_ID', 'CITYNAME', 'COUNTYNAME', 'COUNTYLNAME', 'DOT_DISTRICT', 'DOT_DIVISION', 'QUAD_TILE', 'QUAD_NAME'];
		d.writeln('<table');
		d.writeln('<tr><th style="border-bottom:thick solid #273b7c; text-align:left;">');
		d.write('Mosaic Grid tile #:  ');
		d.writeln(tile);
		d.writeln('</th></tr>');
		d.writeln('<tr><td class="text3" style="font-weight:bold; font-size:12pt; padding-left:40px;">');
		d.writeln('Download'.link(filename));
		d.writeln('</td></tr>');
		d.writeln('</table><br><br>');

		endText = 'fully or partially in this grid tile';
		d.writeln('<table');
			d.writeln('<tr><th style="border-bottom:thick solid #273b7c; text-align:left;">');
			d.write('Counties ',endText);
			d.writeln('</th></tr>');
			d.writeln('<tr><td class="text3">');
			cntyArray = uniqueItems(dArray, 2, true);
			//nDisplay(cntyArray);
			d.writeln('</td></tr>');
			d.writeln('<tr><td colspan="2"><hr></td></tr>');

			d.writeln('<tr><th style="border-bottom:thick solid #273b7c; text-align:left;">');
			d.write('Cities ',endText);
			d.writeln('</th></tr>');
			d.writeln('<tr><td class="text3">');
			cityArray = uniqueItems(dArray, 1, true);
			//nDisplay(cityArray);
			d.writeln('</td></tr>');
			d.writeln('<tr><td colspan="2"><hr></td></tr>');

			d.writeln('<tr><th style="border-bottom:thick solid #273b7c; text-align:left;">');
			d.write('NCDOT Divisions ',endText);
			d.writeln('</th></tr>');
			d.writeln('<tr><td class="text3">');
			divArray = uniqueItems(dArray, 5, true);
			//nDisplay(divArray);
			d.writeln('</td></tr>');
			d.writeln('<tr><td colspan="2"><hr></td></tr>');

			d.writeln('<tr><th style="border-bottom:thick solid #273b7c; text-align:left;">');
			d.write('Quarter Quadrangles ',endText);
			d.writeln('</th></tr>');
			d.writeln('<tr><td class="text3">');
			divArray = uniqueItems(dArray, 7, true);
			//nDisplay(divArray);
			d.writeln('</td></tr>');
			//d.writeln('<tr><td colspan="2"><hr></td></tr>');

		d.writeln('</table>');
		//for(fRow in dArray)
		//{
		//	d.writeln('<br>',dArray[fRow][7]);
		//}

	}

	// * This is called by InfoList and returns an Array of only those 
	// * array elements that are for the designated tile number. (GRID_ID)
	// * Calls on the DOQQM_Data_Full.js dqmc table.
	function filterByTile(tile)
	{
		xArr = new Array(); n=0;
		for(tArr in dqmc)
		{
			if(dqmc[tArr][0]==tile)
				xArr[n++] = dqmc[tArr];
		}
		return xArr;
	}

	// * This is called by InfoList and returns an Array of only those 
	// * unique array elements that are for the designated tile number. (GRID_ID)
	// * Calls on filterByTile subset culled in InfoList from the DOQQM_Data_Full.js dqmc table.
	function uniqueItems(dArray, field, bDisplay)
	{
		xArr = new Array(); n=0; exists=0;
		for(tArr in dArray)
		{
			if(dArray[tArr][field] != '')
			{
				for(noDup in xArr)
				{
					if(dArray[tArr][field] == xArr[noDup])
						exists = 1;
				}
				if(exists != 1) {strUndefined = "|" + dArray[tArr][field] + "|";}
				if(exists != 1 && strUndefined!="|undefined|") {xArr[n++] = dArray[tArr][field];}
				exists=0;
			}
		}

		// * return string?
		xArr.sort();
		if(bDisplay) nDisplay(xArr, field, dArray);
		return xArr;
	}

	// * This is called by InfoList and returns an Array of only those 
	// * unique array elements that are for the designated tile number. (GRID_ID)
	// * Calls on filterByTile subset culled in InfoList from the DOQQM_Data_Full.js dqmc table.
	function uniqueItems_citySub(dArray, field, qq)
	{
		xArr = new Array(); n=0; exists=0;
		for(tArr=0;tArr<dArray.length;tArr++)
		//for(tArr in dArray)
		{
			if(dArray[tArr][field] != '' && dArray[tArr][field+1]==qq)
			{
				for(noDup in xArr)
				{
					if(dArray[tArr][field] == xArr[noDup])
						exists = 1;
				}
				if(exists != 1) xArr[n++] = dArray[tArr][field];
				exists=0;
			}
		}

		// * return string?
		xArr.sort();
		nDisplaySub(xArr, field);
		return xArr;
	}

	// * This is called by InfoList and displays an Array of Unqiue items.
	function nDisplay(pArr, field, dArray)
	{
		if(pArr.length==0) document.writeln('none');
		for(i=0;i<pArr.length;i++)
		{
			switch(field)
			{
				case 1: // * Cities
					document.write(pArr[i]);
					if(pArr.length>0 && i-1+2 != pArr.length) document.writeln('<br>');
					break;
				case 2: // * Counties
					//document.write(pArr[item].substr(0,1),pArr[item].substr(1).toLowerCase()); // * Stop to think about New Hanover
					document.write(pArr[i]);
					if(pArr.length>0 && i-1+2 != pArr.length) document.writeln('<br>');
					break;
				case 5: // * Divisions
					document.write(pArr[i]);
					if(pArr.length>0 && i-1+2 != pArr.length) document.writeln(', ');
					break;
				//case 6: // * DOQQs
				//	document.write(pArr[i]);
				//	if(pArr.length>0 && i-1+2 != pArr.length) document.writeln(', ');
				//	break;
				case 7: // * DOQQs
					document.writeln('<b>',pArr[i], '</b><br>');
					document.write('<span style="font-size:10pt; font-weight:normal; margin-left:25px;">[ ');
					uniqueItems_citySub(dArray, 6, pArr[i]);
					document.write(' ]</span>');
					if(pArr.length>0 && i-1+2 != pArr.length) document.writeln('<br>');
					break;
			}
			//document.write(pArr[item]);
			//if(pArr.length>0 && item != pArr.length) document.writeln('<br>');*/
		}
	}

	function nDisplaySub(pArr, field, dArray)
	{
		if(pArr.length==0) document.writeln('none');
		for(j=0;j<pArr.length;j++)
		{
			document.write(pArr[j]);
			if(pArr.length>0 && j+1 != pArr.length) document.writeln(', ');
		}
	}

	// * A pair of functions for the pop up display on the Listing Page for the Image Map.
	// * Include statement to modify innerHTML value.
	function myClick2() {
		if (document.all) { // * IE
			document.all('info').style.visibility = 'hidden';
			document.all('infotd').innerHTML = '';
		}
		else if (document.getElementById) { // * NS
			document.getElementById('info').style.visibility = 'hidden';
			document.getElementById('infotd').innerHTML = '';
		}
	}
	function myoClick2(tile) {
		if (document.all) { // * IE
			document.all('info').style.visibility = 'visible';
			tArray = dMd;
			ihtml = '<b>Counties fully or partially in grid tile ' + tile + '</b><br>';
			for(tElem in tArray)
			{
				thisDOQQ = false;
				for(j=0;j<tArray[tElem].length;j++)
					if(tArray[tElem][j]==tile) thisDOQQ = true;
				if(thisDOQQ)
					ihtml += tElem + '<br>';
			}
			document.all('infotd').innerHTML = ihtml;
		}
		else if (document.getElementById) { // * NS
			document.getElementById('info').style.visibility = 'visible';
			tArray = dMd;
			ihtml = '<b>Counties fully or partially in grid tile ' + tile + '</b><br>';
			for(tElem in tArray)
			{
				thisDOQQ = false;
				for(j=0;j<tArray[tElem].length;j++)
					if(tArray[tElem][j]==tile) thisDOQQ = true;
				if(thisDOQQ)
					ihtml += tElem + '<br>';
			}
			document.getElementById('infotd').innerHTML = ihtml;
		}
	}

	// * Create the map tag and area data for an image map.
	function makeImageMap(name)
	{
		fileInfo = 'GISUTMIMosaic_Info.html?tile=';
		
		document.writeln('<map name="',name,'">');
		for(i=0;i<jCoords.length;i++)
		{
			tFile = fileInfo + jCoords[i][2]; // * target File
			document.writeln('<area shape="',jCoords[i][0],'" coords="',jCoords[i][1],'" href="',tFile,'" alt="',jCoords[i][2],'" onMouseOver="myoClick2(',jCoords[i][2],');" onMouseOut="myClick2()">');
		}
		document.writeln('</map>');
	}
	
	// * Create the Warning about Downloading Large Files	
	function largeFileWarning() 
	{
		warningText = 'The files available for download are quite large.  Downloads can take up to 30 minutes using slow internet connections (e.g. modem).  Please be patient.  Thank You.';

		document.writeln('<TABLE BORDER="0" CELLSPACING="1" CELLPADDING="7" WIDTH="720">');
		document.writeln('<TR class="text2b">');
		document.writeln('<td><font color="Red">',warningText,'</font></td>');
		document.writeln('</TR>');
		document.writeln('</TABLE>');
	}

	// * Create the small link to the Disclaimer if necessary	
	function readDisclaimer() { deepDisclaimer(); }

