/* MAPLINK.JS
   CIS_Web map link utilities.

   Version Information
    20011025a = Initial Release.
	20020528a = Removed database name array...when showing location, full database name must now be passed.
	20020709a = Added menubar=no code to map window open functions to align with debug versions.
	20021122a = Modified winLeft and winTop to support new winSize options.
	20021213a = Added drawmap function.
	20030102a = Added support for legend items in drawmap functions.
	20030108a = Fixed line info write in drawMap.
	20030304a = Added radial search data to showLocMap.
	20030305a = Modified search codes in showLocMap.
	20030403a = Completed fixSearchData function for CC map search form.

Copyright 2001, Land Information Access Association
*/

/* Initialize Global Variables */
var mapLink;                      //Map URL
var mapWin;                       //Map window
var appPath = "maps/"             //URL of CIS_Web map page application

/* Map Link Functions */
function showMap(isFrame, fromRoot, whichMap) {
	var winLeft, winTop;
	winSize(isFrame);
	var mapLink = "";
	for (var i = 0; i < fromRoot; i++) {
		mapLink += "../";
	}
	mapLink += appPath + "cismappg.asp?" + whichMap + "&x=" + winX + "&y=" + winY;
	if (mapWin) {
		if (!mapWin.closed) mapWin.close();
	}
	if (isFrame == 3) {
		winLeft = winTop = 0;
	} else {
		winLeft = ((screen.availWidth - winX - 10) * .5);
		winTop = ((screen.availHeight - winY - 30) * .5);
	}
	mapWin = window.open(mapLink,'mWindow','menubar=no,toolbar=no,location=no,scrollbars=yes,top=' + winTop + ',left=' + winLeft + ',width=' + winX + ',height=' + winY + ',resizable=yes');
}

function showLocMap(isFrame, fromRoot, whichMap, locID, dbName, sField, sDist, sFile) {
	var winLeft, winTop, sCode;
	winSize(isFrame);
	if (mapWin) {
		if (!mapWin.closed) mapWin.close();
	}
	var mapLink = "";
	for (var i = 0; i < fromRoot; i++) {
		mapLink += "../";
	}
	mapLink += appPath + "cisin.asp?a=startmap&" + whichMap + "&x=" + winX + "&y=" + winY;
	if (isFrame == 3) {
		winLeft = winTop = 0;
	} else {
		winLeft = ((screen.availWidth - winX - 10) * .5);
		winTop = ((screen.availHeight - winY - 30) * .5);
	}
	sCode = (sDist && sFile) ? 3 : 1;
	mapWin = window.open('','mWindow','menubar=no,toolbar=no,location=no,scrollbars=yes,top=' + winTop + ',left=' + winLeft + ',width=' + winX + ',height=' + winY + ',resizable=yes');
	mapWin.document.writeln('<html>\n<head>\n<title></title>\n</head>\n<body bgcolor="#FFFFFF">');
	mapWin.document.writeln('<form name="requestinfo" method="post" action="' + mapLink + '">');
	mapWin.document.writeln('<input type="hidden" name="SrchCode" value="' + sCode + '">');
	mapWin.document.writeln('<input type="hidden" name="SrchLDB" value="' + dbName + '">');
	mapWin.document.writeln('<input type="hidden" name="SrchField" value="' + sField + '">');
	mapWin.document.writeln('<input type="hidden" name="SrchValue" value="' + locID + '">');
	if (sCode == 3) {
		mapWin.document.writeln('<input type="hidden" name="SrchRadius" value="' + sDist + '">');
		mapWin.document.writeln('<input type="hidden" name="SrchTarget" value="' + sFile + '.ldb">');
		mapWin.document.writeln('<input type="hidden" name="SrchStyle" value="' + sFile + '.ini">');
	}
	mapWin.document.writeln('</form>');
	mapWin.document.writeln('</body>\n</html>');
	mapWin.document.close();
	mapWin.document.requestinfo.submit();
}
function drawPointObj(coords, label, clickact, width, linecol, fillcol, icon) {
	this.type = 'point';
	this.opcode = 0;
	this.coords = coords;
	this.label = label;
	this.clickact = clickact;
	this.width = width;
	this.linecol = linecol;
	this.fillcol = fillcol;
	this.icon = icon;
}

function drawLineObj(label, clickact, width, linecol, fillcol, icon) {
	this.type = 'line';
	this.opcode = 0;
	this.label = label;
	this.clickact = clickact;
	this.width = width;
	this.linecol = linecol;
	this.fillcol = fillcol;
	this.icon = icon;
	this.points = new Array();
}

function drawLegendObj(label, icon, clickact, clicktype) {
	this.type = 'legend';
	this.label = label;
	this.icon = icon;
	this.clickact = clickact;
	this.clicktype = clicktype;
}

function drawMap(isFrame, fromRoot, whichMap, objArray) {
	var winLeft, winTop;
	var opText, opCode;
	var strDelimit = '`';
	winSize(isFrame);
	if (mapWin) {
		if (!mapWin.closed) mapWin.close();
	}
	var mapLink = "";
	for (var i = 0; i < fromRoot; i++) {
		mapLink += "../";
	}
	mapLink += appPath + "cisin.asp?a=startmap&" + whichMap + "&x=" + winX + "&y=" + winY;
	if (isFrame == 3) {
		winLeft = winTop = 0;
	} else {
		winLeft = ((screen.availWidth - winX - 10) * .5);
		winTop = ((screen.availHeight - winY - 30) * .5);
	}
	mapWin = window.open('','mWindow','menubar=no,toolbar=no,location=no,scrollbars=yes,top=' + winTop + ',left=' + winLeft + ',width=' + winX + ',height=' + winY + ',resizable=yes');
	mapWin.document.writeln('<html>\n<head>\n<title></title>\n</head>\n<body bgcolor="#FFFFFF">');
	mapWin.document.writeln('<form name="requestinfo" method="post" action="' + mapLink + '">');
	mapWin.document.writeln('<input type="hidden" name="SrchCode" value="8">');
	var pointCounter = 0;
	var lineCounter = 0;
	var legendCounter = 0;
	for (i = 1; i < objArray.length; i++) {
		opText = '';
		opCode = 0;
		if (objArray[i].label) {
			opCode += 1;
			opText += strDelimit + objArray[i].label + strDelimit;
		}
		if (objArray[i].clickact) {
			opCode += 2;
			opText += (opText == '' ? '' : ',') + strDelimit + objArray[i].clickact + strDelimit;
		}
		if (objArray[i].width) {
			opCode += 4;
			opText += (opText == '' ? '' : ',') + objArray[i].width;
		}
		if (objArray[i].linecol) {
			opCode += 8;
			opText += (opText == '' ? '' : ',') + objArray[i].linecol;
		}
		if (objArray[i].fillcol) {
			opCode += 16;
			opText += (opText == '' ? '' : ',') + objArray[i].fillcol;
		}
		if (objArray[i].icon) {
			opCode += 32;
			opText += (opText == '' ? '' : ',') + strDelimit + objArray[i].icon + strDelimit;
		}
		switch(objArray[i].type) {
			case 'point':
				pointCounter++;
				mapWin.document.writeln('<input type="hidden" name="PCoord' + pointCounter + '" value="' + objArray[i].coords + '">');
				mapWin.document.writeln('<input type="hidden" name="PCode' + pointCounter + '" value="' + opCode + '">');
				mapWin.document.writeln('<input type="hidden" name="POpts' + pointCounter + '" value="' + opText + '">');
			break
			case 'line':
			case 'poly':
				lineCounter++;
				lineCoords = '';
				for (x = 0; x < objArray[i].points.length; x++) {
					if (x != 0) lineCoords += ',';
					lineCoords += objArray[i].points[x];
				}
				mapWin.document.writeln('<input type="hidden" name="LType' + lineCounter + '" value="' + objArray[i].type + '">');
				mapWin.document.writeln('<input type="hidden" name="LCoord' + lineCounter + '" value="' + lineCoords + '">');
				mapWin.document.writeln('<input type="hidden" name="LCount' + lineCounter + '" value="' + objArray[i].points.length + '">');
				mapWin.document.writeln('<input type="hidden" name="LCode' + lineCounter + '" value="' + opCode + '">');
				mapWin.document.writeln('<input type="hidden" name="LOpts' + lineCounter + '" value="' + opText + '">');
			break
			case 'legend':
				legendCounter++;
				mapWin.document.writeln('<input type="hidden" name="LegLabel' + legendCounter + '" value="' + objArray[i].label + '">');
				mapWin.document.writeln('<input type="hidden" name="LegIcon' + legendCounter + '" value="' + objArray[i].icon + '">');
				mapWin.document.writeln('<input type="hidden" name="LegClickAct' + legendCounter + '" value="' + objArray[i].clickact + '">');
				mapWin.document.writeln('<input type="hidden" name="LegClickType' + legendCounter + '" value="' + objArray[i].clicktype + '">');
			break
		}
	}
	mapWin.document.writeln('<input type="hidden" name="PointCount" value="' + pointCounter + '">');
	mapWin.document.writeln('<input type="hidden" name="LineCount" value="' + lineCounter + '">');
	mapWin.document.writeln('<input type="hidden" name="LegendCount" value="' + legendCounter + '">');
	mapWin.document.writeln('</form>');
	mapWin.document.writeln('</body>\n</html>');
	mapWin.document.close();
	mapWin.document.requestinfo.submit();
}

function fixSearchData(whatControl, andHow, dataPrompt) {
	var i, thisChar;
	var retVal = '';
	var allDigits = '0123456789';
	var searchData = whatControl.value;
	if (searchData != '') {
		switch(andHow) {
			case 'UC':
				whatControl.value = searchData.toUpperCase();
				break;
			case 'LC':
				whatControl.value = searchData.toLowerCase();
				break;
			case 'NUM':
				for (i = 0; i < searchData.length; i++) {
					thisChar = searchData.substr(i, 1);
					retVal += allDigits.lastIndexOf(thisChar) > -1 ? thisChar : '';
				}
				if (retVal != '') {
					whatControl.value = retVal;
				} else {
					alert('You must provide a number for ' + dataPrompt + '.');
					return false;
				}
				break;
		}
		return true;
	} else {
		alert('Please provide a value for ' + dataPrompt + '.');
		return false;
	}
}

