/* 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.
	20040715a = Added popup window test.
	20040730a = Added showAMap function, to use an absolute path to the map (also automatically shows full screen).
	            Use this function when you need a map link that will work in several contexts.
	20040831a = Added check for in=y in showAMap map string.
	20041006a = Added support for empty location list for drawMap.
	20041019a = Added support for icon=NONE in map draw requests.
	20041028a = Updated drawMap to send proper request when objArray is empty.
	20041130a = Added a check for the file extension to the sFile parameter on showLocMap.
	20041208a = Added a check for sFile when performing above check.
	20060426a = Modified all map launch functions to size map window without 1/20 of vertical space
	            to avoid task bar if isFrame = 3 (full size window).

Copyright © 2006, 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

/* Popup Window Test */
function popOK() {
	var testWin;
	var testObj = new RegExp('object','gi');
	try {
		testWin = window.open('','testwin','width=50,height=50,status=no,resizable=yes'); 
		testWin.close();
	}
	catch(e) { }
	if(!testObj.test(String(testWin))) {
		alert('NOTE: Maps are displayed in a popup window.  It appears that you have popup blocking software active in your browser.  Please disable popup blocking for this site to view maps.');
		return false;
	} else {
		return true;
	}
}

/* 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;
		winY = winY - parseInt(winY * 0.05)
	} else {
		winLeft = ((screen.availWidth - winX - 10) * .5);
		winTop = ((screen.availHeight - winY - 30) * .5);
	}
	if (popOK()) {
		mapWin = window.open(mapLink,'mWindow','menubar=no,toolbar=no,location=no,scrollbars=yes,top=' + winTop + ',left=' + winLeft + ',width=' + winX + ',height=' + winY + ',resizable=yes');
	}
}

function showAMap(rootPath, whichMap) {
	var winLeft, winTop;
	winSize(3);
	//subtract space for task bar from vertical axis
	winY = winY - parseInt(winY * 0.05)
	var mapLink = "";
	if (whichMap.indexOf('&in=y') < 0) whichMap += '&in=y';
	mapLink += rootPath + appPath + "cismappg.asp?" + whichMap + "&x=" + winX + "&y=" + winY;
	if (mapWin) {
		if (!mapWin.closed) mapWin.close();
	}
	winLeft = winTop = 0;
	if (popOK()) {
		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;
		winY = winY - parseInt(winY * 0.05)
	} else {
		winLeft = ((screen.availWidth - winX - 10) * .5);
		winTop = ((screen.availHeight - winY - 30) * .5);
	}
	if (sFile) {if (sFile.charAt(sFile.length - 4) == '.') sFile = sFile.substr(0, sFile.length - 4)};
	sCode = (sDist && sFile) ? 3 : 1;
	if (popOK()) {
		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;
		winY = winY - parseInt(winY * 0.05)
	} else {
		winLeft = ((screen.availWidth - winX - 10) * .5);
		winTop = ((screen.availHeight - winY - 30) * .5);
	}
	if (objArray && objArray.length > 0) {
		if (popOK()) {
			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 && objArray[i].icon != 'NONE') {
					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();
		}
	} else {
		showMap(isFrame, fromRoot, whichMap);
	}
}

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;
	}
}

function mvOp(whatOp, mvIndex) {
	var mvForm = eval('document.mapview' + mvIndex);
	var mvControl = eval('mvForm.viewop' + mvIndex);
	mvControl.value = whatOp;
	mvForm.submit();
}
