// extended javascript template for ArcIMS HTML Viewer
// aimsTDLCensus.js


function TDLWriteCensusXML()	//Renderer: census table field
{	
	var theString="";

	theString += '<LAYERDEF id="LSB" name="LSB" type="polygon" visible="true" >\n';

	if ((CensusField == "POP") || (CensusField == "WORKPOP") || (CensusField == "HH"))	//0
	{
		theString += '<GROUPRENDERER>\n';
		theString += '<SIMPLERENDERER>\n';
	    theString += '<SIMPLEPOLYGONSYMBOL fillinterval="6" filltransparency="0.0" fillcolor="0,200,0" filltype="solid" boundarytype="solid" boundarywidth="2" boundarycaptype="round" boundaryjointype="round" boundarycolor="0,0,204" />\n';
		theString += '</SIMPLERENDERER>\n';
		theString += '<SIMPLELABELRENDERER howmanylabels="One_label_per_shape" field="' + CensusField + '">\n';
	    //theString += '<TEXTSYMBOL fontcolor="255,0,0" font="Arial" style="Bold" size="12" background="false" glowing="255,255,255" transparency="1.0" antialiasing="false" threed="false" />\n';
	    theString += '<TEXTSYMBOL font="Arial Negreta" fontstyle="bold" fontsize="12" fontcolor="255,0,0" glowing="255,255,255" />\n';
		theString += '</SIMPLELABELRENDERER>\n';
		theString += '</GROUPRENDERER>\n';
	}
	else if ((CensusField == "C_AGEM") || (CensusField == "C_AGEF") || (CensusField == "C_1PERSON") || (CensusField == "C_UNEXT") || (CensusField == "C_HHINCOME") || (CensusField == "C_HHRENT") || (CensusField == "C_OWNER"))	//2
	{
		theString += '<VALUEMAPRENDERER lookupfield="' + CensusField + '">\n';
		theString += '<EXACT value="H" label="H">\n';
	    theString += '<SIMPLEPOLYGONSYMBOL filltransparency="1.0" filltype="bdiagonal" fillcolor="255,153,153" fillinterval="4" boundarytype="solid" boundarywidth="2" boundarycaptype="butt" boundaryjointype="round" boundarycolor="0,0,204" />\n';
		theString += '</EXACT>\n';
		theString += '<EXACT value="M" label="M">\n';
	    theString += '<SIMPLEPOLYGONSYMBOL filltransparency="1.0" filltype="bdiagonal" fillcolor="51,204,51" fillinterval="4" boundarytype="solid" boundarywidth="2" boundarycaptype="butt" boundaryjointype="round" boundarycolor="0,0,204" />\n';
		theString += '</EXACT>\n';
		theString += '<EXACT value="L" label="L">\n';
		theString += '<SIMPLEPOLYGONSYMBOL filltransparency="1.0" filltype="bdiagonal" fillcolor="153,204,255" fillinterval="4" boundarytype="solid" boundarywidth="2" boundarycaptype="butt" boundaryjointype="round" boundarycolor="0,0,204" />\n';
		theString += '</EXACT>\n';
		theString += '</VALUEMAPRENDERER>\n';
	}
	else if ((CensusField == "M_HHSIZE") || (CensusField == "SINGLEM") || (CensusField == "SINGLEF") || (CensusField == "SEC") || (CensusField == "TERTIARY"))	//5
	{
		theString += '<GROUPRENDERER>\n';
		theString += '<SIMPLELABELRENDERER howmanylabels="One_label_per_shape" field="' + CensusField + '">\n';
		//theString += '<TEXTSYMBOL fontcolor="255,0,0" font="Arial" glowing="255,255,255" style="Bold" size="12" />\n';
		theString += '<TEXTSYMBOL font="Arial Negreta" fontstyle="bold" fontsize="12" fontcolor="255,0,0" glowing="255,255,255" />\n';
		theString += '</SIMPLELABELRENDERER>\n';
		theString += '<SIMPLERENDERER>\n';
		theString += '<SIMPLEPOLYGONSYMBOL fillinterval="6" filltransparency="0.0" fillcolor="0,200,0" filltype="solid" boundarytype="solid" boundarywidth="2" boundarycaptype="round" boundaryjointype="round" boundarycolor="0,0,204" />\n';
		theString += '</SIMPLERENDERER>\n';
		theString += '</GROUPRENDERER>\n';
	}
	else
	{
		theString += '<GROUPRENDERER>\n';
		theString += '<SIMPLERENDERER>\n';
	    theString += '<SIMPLEPOLYGONSYMBOL fillinterval="6" filltransparency="0.0" fillcolor="0,200,0" filltype="solid" boundarytype="solid" boundarywidth="2" boundarycaptype="round" boundaryjointype="round" boundarycolor="0,0,204" />\n';
		theString += '</SIMPLERENDERER>\n';
		theString += '<SIMPLELABELRENDERER howmanylabels="One_label_per_shape" field="POP">\n';
	    //theString += '<TEXTSYMBOL fontcolor="255,0,0" font="Arial" style="Bold" size="12" background="false" glowing="255,255,255" transparency="1.0" antialiasing="false" threed="false" />\n';
	    theString += '<TEXTSYMBOL font="Arial Negreta" fontstyle="bold" fontsize="12" fontcolor="255,0,0" glowing="255,255,255" />\n';
		theString += '</SIMPLELABELRENDERER>\n';
		theString += '</GROUPRENDERER>\n';
	}
	
	theString += '</LAYERDEF>\n';
	CensusString=theString;	//Save the render string to CensusString

	//alert(theString);
	return theString;
	
	//sendToServer(imsURL,theString,1);
}

//copy from writeXML and changed some...
function TDLWriteXML() {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_IMAGE>\n<PROPERTIES>\n<ENVELOPE minx="' + forceComma(eLeft) + '" miny="' + forceComma(eBottom) + '" maxx="' + forceComma(eRight) + '" maxy="' + forceComma(eTop) + '" />\n';
	theString += '<IMAGESIZE height="' + iHeight + '" width="' + iWidth + '" />\n';
	var visString = "";
	if (aimsLayersPresent) {
		// tell the server which layers are to be visible
		if (toggleVisible) {
			theString += '<LAYERLIST >\n';
			
			// add by hkm 2001-06-19, get census data
			if (NowLevel>4) {
				if (CensusField=="")	
				{
					theString += CensusString;	//get the old render string	
				} else
				{
					theString += TDLWriteCensusXML();	//creare a new render string
				}
			}
			// end add by hkm
			
			for (var i=0;i<layerCount;i++) {
				if (LayerVisible[i]==1) {
					theString += '<LAYERDEF id="' + LayerID[i] + '" visible="true" ';
					if (aimsClassRenderPresent) {
						theString += addSpecialRenderToMap(i);
					} else {
						theString += '/>\n';
					}
					
				}
				else {
					theString += '<LAYERDEF id="' + LayerID[i] + '" visible="false" />\n';
				}
				
			}
			theString += '</LAYERLIST>\n';
		}
	}
	
	// map background color
	if (mapTransparent) {
		theString += '<BACKGROUND color="255,255,254" transcolor="255,255,254" />\n';
	} else {
		if (mapBackColor!="") {
			theString += '<BACKGROUND color="' + mapBackColor + '" />\n\n';
		}
	}
	if (aimsLegendPresent) {
		// create a legend image
		if (legendVisible) theString += addLegendToMap();
	}
	
	theString += '</PROPERTIES>\n';
	
	// buffer
	if (aimsBufferPresent) {
		if (showBuffer) theString += addBufferToMap();
	}
	
	// select
	if (aimsSelectPresent) {
		theString += addSelectToMap();
	}	
	
	// any custom stuff to fit here
	if (aimsCustomPresent) theString += addCustomToMap1();
	
	
	if (aimsClickPresent){
		// clickpoints 
		if (clickCount>0) {
			// draw click points and lines between them on map
			var clickColor = selectColor;
			if (clickType==1) clickColor = clickMarkerColor;
			theString += '<LAYER type="acetate" name="allTheClicks">\n';
			if (clickCount>1) {
				theString += '<OBJECT units="database">\n<LINE coords="' + forceComma(clickPointX[0]) + coordsDelimiter + forceComma(clickPointY[0]);
				for (var i=1;i<clickCount;i++) {
					theString += pairsDelimiter  + forceComma(clickPointX[i]) + coordsDelimiter + forceComma(clickPointY[i]); 
				}
				theString += '" >\n';
				theString += '<SIMPLELINESYMBOL type="solid" color="' + clickMarkerColor;
				theString += '" width="3" />\n</LINE>\n</OBJECT>\n'; 
				theString += '<OBJECT units="database">\n<LINE coords="' + forceComma(clickPointX[0]) + coordsDelimiter + forceComma(clickPointY[0]);
				for (var i=1;i<clickCount;i++) {
					theString += pairsDelimiter  + forceComma(clickPointX[i]) + coordsDelimiter + forceComma(clickPointY[i]); 
				}
				theString += '" >\n';
				theString += '<SIMPLELINESYMBOL type="solid" color="255,255,255" width="1" />\n</LINE>\n</OBJECT>\n'; 
	 		}
			for (var i=0;i<clickCount;i++) {
				theString += '<OBJECT units="acetate">\n<POINT coords="' + forceComma(clickPointX[i]) + coordsDelimiter + forceComma(clickPointY[i]) + '">\n';
				theString += '<SIMPLEMARKERSYMBOL  type="' + clickMarkerType + '"';
				theString += ' color="' + clickMarkerColor + '" width="' + clickMarkerSize + '" />\n</POINT>\n</OBJECT>\n';
			}
			theString += '</LAYER>\n';
		}
	}
	
	// geocoding or point with label
	if (showGeocode) {
		// draw the point . . . also used to display any point with a label on map
		theString += '<LAYER type="acetate" name="GeoCode1">\n';
		theString += '<OBJECT units="database">\n<POINT coords="' + forceComma(geocodeX) + coordsDelimiter + forceComma(geocodeY) + '">\n';
		theString += '<SIMPLEMARKERSYMBOL  type="circle"  color="' + geocodePointColor + '" width="' + geocodePointSize +'" overlap="false" />\n</POINT></OBJECT>\n';
		if (geocodeLabel!="") {
			theString += '<OBJECT units="database">\n<TEXT coords="' + forceComma(geocodeX) + coordsDelimiter + forceComma(geocodeY) + '" label="' + geocodeLabel + '">\n';
			theString += '<TEXTMARKERSYMBOL fontcolor="' + geocodePointColor + '" fontsize="12" shadow="64,64,64" glowing="255,255,0" halignment="right" valignment="top" overlap="false" /></TEXT></OBJECT>\n';
		}
		theString += '</LAYER>\n';
		
	}
	
	// any custom stuff to fit here
	//if (aimsCustomPresent) theString += addCustomToMap2();

	if (drawNorthArrow) {
		// draw a north arrow
		theString += '<LAYER type="acetate" name="theNorthArrow">\n';
		theString += '<OBJECT units="pixel">\n<NORTHARROW type="' + NorthArrowType + '" size="' + NorthArrowSize + '" coords="' + NorthArrowCoords + '" shadow="32,32,32" ';
		theString += 'angle="' + NorthArrowAngle + '" antialiasing="True" overlap="False" />\n</OBJECT>\n';
		theString += '</LAYER>\n';
	}
	if (drawScaleBar) {
		// draw a scale bar
		//ScaleBarPrecision = numDecimals;
		theString += '<LAYER type="acetate" name="theScaleBar">\n';
		xDistance = eRight - eLeft;
		//yDistance = eTop - eBottom;
		//theString += '<OBJECT units="database">\n';
		//theString += '<SCALEBAR coord="' + ((xDistance * 0.60)+eLeft) + coordsDelimiter + (((yDistance/iHeight)*2)+eBottom) + '" outline="' + ScaleBarBackColor + '" ';
		theString += '<OBJECT units="pixel">\n';
		theString += '<SCALEBAR coords="' + parseInt(iWidth * (7/10)) + coordsDelimiter + '2" ';
		if (MapUnits=="DEGREES") theString += 'mode="geodesic" ';
		///*
		theString += 'outline="' + ScaleBarBackColor + '" ';
		theString += 'font="' + ScaleBarFont + '" fontcolor="' + ScaleBarFontColor + '" style="' + ScaleBarStyle + '" barcolor="' + ScaleBarColor + '" ';
		if (MapUnits!="DEGREES") {
			theString += 'mapunits="' + MapUnits.toLowerCase() + '" ';
		}
		theString += 'scaleunits="' + ScaleBarUnits.toLowerCase() + '" antialiasing="True" ';
		var sDistance = getScaleBarDistance();
		if (MapUnits=="DEGREES") {
			if (xDistance<1/10){
				theString += 'precision="' + ScaleBarPrecision + '" ';
			}
		} else if (sDistance<5) {
			theString += 'precision="' + ScaleBarPrecision + '" ';
		}
		theString += 'screenlength="' + parseInt(iWidth * (2/10)) + '" ';
		//theString += 'distance="' + sDistance + '" ';
		theString += 'fontsize="' + ScaleBarSize + '" barwidth="' + ScaleBarWidth + '" overlap="False"  ';
		//*/
		theString += '/>\n</OBJECT>\n';
		theString += '</LAYER>\n';	
		
	}
		theString += addCustomToMap2();
	
	// any custom stuff to fit here
	if (aimsCustomPresent) theString += addCustomToMap3();

	if (drawModeOnMap) {
		// draw the current mode on the map
		theString += '<LAYER type="acetate" name="theMode">\n';
		theString += '<OBJECT units="pixel">\n<TEXT coords="5' + coordsDelimiter + (iHeight-10) + '" label="' + modeBlurb + '">\n';
		theString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		theString += 'threed="TRUE" glowing="' + modeMapGlow + '" overlap="false" />\n</TEXT>\n</OBJECT>';
	
	}
	
	// any custom stuff to be drawn on top of everything
	if (aimsCustomPresent) theString += addCustomToMap4();

	theString += '</GET_IMAGE>\n</REQUEST>\n</ARCXML>';
	//alert(theString);
	return theString;
}



//use for show census data--identify
//called by aimsXML.ProcessXML()--identify(XMLMode=7)
function TDLCensusparseFields(theReply)
{
	var fList="";
	var i=0;
	var pos = theReply.indexOf("<FEATURECOUNT");
	var startpos = pos + 21;
	var endpos = theReply.indexOf(dQuote,startpos);
	var tempString = theReply.substring(startpos,endpos);
	var featureCount = parseInt(tempString);
    //alert(featureCount);

    if (featureCount>0)
    {            
      endpos = 1;
      for (i=0;i<featureCount;i++)
      {
		startpos = theReply.indexOf("<FIELDS ",endpos);
		startpos = startpos + 8;
		endpos =theReply.indexOf(" />",startpos);
		tempString = theReply.substring(startpos,endpos);
		if (featureCount>1) tempString = "|" + tempString;
		fList = fList + tempString;
      }
      //alert(fList);	// + '1'+ ActiveLayer);
     }
     
     var QueryStr="(";	//use LSB.JNAME as Query Where string
     if (featureCount>0)
     {	
		startpos=1;
		for (i=0;i<featureCount;i++)
		{
			pos = fList.indexOf("LSB.JNAME=",startpos);
			startpos = pos + 11;
			endpos = fList.indexOf(dQuote,startpos);
			if (QueryStr=="(")
				{ QueryStr = QueryStr + "'" + fList.substring(startpos,endpos) + "'"; }
			else
				{ QueryStr = QueryStr + "," + "'" + fList.substring(startpos,endpos) + "'"; }
			startpos=endpos;
		}
		QueryStr=QueryStr + ")"
		
		var TableName="LSB";
		var FieldName="JNAME";
		var FieldValue=QueryStr;
		
		TDLGetCensusData(TableName,FieldName,FieldValue);
		hideRetrieveData();		
	}    
    
    if (featureCount==0)
    {
		hideRetrieveData();
		alert("沒有地圖資料!");
    }
    
    //alert(QueryStr);
}


//open a new window for show census data--- without any toolbar...
function TDLGetCensusData(TableName,FieldName,FieldValue)
{
	var href=document.location.href;
	var href1 = href.toUpperCase();	
	var pos = href1.indexOf("MAPFRAME.HTM",0);
	var url = href.substring(0,pos)+"CensusMap/GetCensusData.asp?TableName=" +TableName + "&FieldName=" + FieldName + "&FieldValue=" + FieldValue;	
	//alert(url);
	var newwin=window.open(url,"Data",'width=320,height=380,status=no,directories=no,scrollbars=yes,resizable=yes,location=no,menubar=no,toolbar=no,alwaysLowered=yes');
	newwin.focus();
}


function TDLGetCensusName(indexName)
{
	var searchName;
	if (indexName == "POP") { searchName="Total Population(總人口)" }
	if (indexName == "WORKPOP") { searchName="Working Population(工作人口)" }
	if (indexName == "C_AGEM") { searchName="Median Age (Male)(年齡中位數﹝男性﹞)" }
	if (indexName == "C_AGEF") { searchName="Median Age (Famale)(年齡中位數﹝女性﹞)" }
	if (indexName == "HH") { searchName="Number of Domestic Households(家庭住戶數目)" }
	if (indexName == "M_HHSIZE") { searchName="Average Domestic Household Size(每戶平均人數)" }
	if (indexName == "C_1PERSON") { searchName="1-Person Domestic Household(單人住戶)" }
	if (indexName == "C_UNEXt") { searchName="Unextended Nuclear Family(未擴展單核心家庭住戶)" }
	if (indexName == "SINGLEM") { searchName="Aged 15+ and Never Married (Male)(十五歲以上從未結婚人士﹝男性﹞)" }
	if (indexName == "SINGLEF") { searchName="Aged 15+ and Never Married (Famale)(十五歲以上從未結婚人士﹝女性﹞)" }
	if (indexName == "C_HHINCOME") { searchName="Median Household Income(住戶收入中位數)" }
	if (indexName == "C_HHRENT") { searchName="Median Household Rent(住戶租金中位數)" }
	if (indexName == "C_OWNER") { searchName="Proportion of Owner-Occupiers(住戶居於自置居住單位比例)" }
	if (indexName == "SEC") { searchName="Sixth Form and Below (Aged 15+)(十五歲以上人士具預科或以下程度)" }
	if (indexName == "TERTIARY") { searchName="Tertiary and Above (Aged 15+)(十五歲以上人士具大專或以下程度)" }
	
	return searchName;
}
