// aimsClassRender.js
/*
*  JavaScript template file for ArcIMS HTML Viewer Interactive Class Rendering
*		dependent on aimsXML.js, aimsDHTML.js, aimsCommon.js, and ArcIMSparam.js, 
*
*	Functions to change elements in the LayerRenderString array, whose elements are
*		strings containing replacement renderer arcxml tags for a specific layer for 
* 		current map request
* 	The element index is the Layer's index, starting with the topmost layer as zero.
*		The string must contain the complete renderer information for the layer 
*		(including any labelling or scaling) and follow arcxml renderer requirements.
*/

aimsClassRenderPresent=true;
// color ramps
var ClassRenderStartColor = new Array();
var ClassRenderEndColor = new Array();
var cr_SceltaColori = 0;
var ClassRenderPersonalColor = new Array();
// marker/line sizes
var ClassRenderStartSize = new Array();
var ClassRenderEndSize = new Array();
// color of polygon outlines. . . "" if none
var ClassRenderOutlineColor = new Array();
// rendering information preceeding classification
var ClassRenderStringPrefix = new Array();
// rendering information following classification
var ClassRenderStringSuffix = new Array();
// marker style
var ClassRenderStyle = new Array();
// Legend title
var cr_Title = ClassRenderList[0];
var ClassRenderLista_valori = new Array();


// current active ClassRenderLayer
var ClassRenderLayerIndex=0;
// Name of layer
var ClassRenderLayerName = ClassRenderLayer[0];
// current field for classification
var ClassRenderLayerField = "";
// number of classes to use in rendering
var cr_Classes = 0;
// method of classification
//		0=equal interval; 1=quantile;
var cr_Method = 0;


var cr_Range = 0;
var cr_BreakIncrement = 0;
var cr_RecordIncrement = 0;
var cr_BreakMin = new Array();
var cr_BreakMax = new Array();
var cr_BreakColor = new Array();
var cr_BreakSize = new Array();

var cr_Min=0;
var cr_Max=9999999999;
var cr_Count=1;
var cr_Sum=0;



/*
***************************************************************************************

Interactive Class Rendering functions 

***************************************************************************************
*/

// write interactive ValueRenderer strings in Map XML request
function cr_addSpecialRenderToMap(i) {
	var renderString = "";
	var j=-1;
	for (var k=0;k<ClassRenderLayer.length;k++) {
		if (ClassRenderLayer[k]==LayerName[i]) j=k;
	}
	if (j!=-1) {
		if (ClassRenderString[j]!="") {
			// add custom rendering for this request
			renderString += 'type="' + LayerType[i] + '">\n' + ClassRenderString[j] + '\n</LAYERDEF>\n';
		} else {
			renderString += '/>\n';
		}
	} else {
		renderString += '/>\n';
	}
	return renderString;
}

// set up the interactive classification
function cr_classifyLayer() {
	var j = -1;
	for (var i=0;i<LayerName.length;i++) {
	   if (ClassRenderLayer[ClassRenderLayerIndex] == LayerName[i]) j = i;
	}
	if (j>-1) {
		//if (useTextFrame) parent.TextFrame.document.location = appDir + "text.htm";
		setLayerFields(j);
		fieldIndex=0;
		selectCount=0;
		showBuffer=false;
		ActiveLayer=LayerID[j];
		ActiveLayerType=LayerType[j];
		ActiveLayerIndex=j;
		cr_writeClassifyForm(ClassRenderLayerIndex);
	} else {
		alert(ClassRenderList[1]);
	}
}

function cr_scrivi_layer(){
   if (useTextFrame) {
		var Win1 = parent.TextFrame;
		Win1.document.open();
		var t = "parent.MapFrame";		
	} else {
		var Win1 = open(appDir + "text.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
		var t = "opener";
		if (parent.MapFrame!=null) t = "opener.parent.MapFrame";
	}
	
	
}



// form for interactive classifying
function cr_writeClassifyForm(theIndex) {
	var crIndex = theIndex;
	if (useTextFrame) {
		var Win1 = parent.TextFrame;
		Win1.document.open();
		var t = "parent.MapFrame";
		
	} else {
		var Win1 = open(appDir + "text.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
		var t = "opener";
		if (parent.MapFrame!=null) t = "opener.parent.MapFrame";
	}
	
	Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><HEAD>');
	Win1.document.writeln('<link rel="stylesheet" href="'+pagStile+'" type="text/css">');
	Win1.document.writeln('<script language="javascript">');
	Win1.document.writeln('		var t = ' + t);
	
	Win1.document.writeln('		function setStyle() {');
	Win1.document.writeln('			var f = document.forms[0];');
	Win1.document.writeln('			var qField = f.QueryField.options[f.QueryField.selectedIndex].text;');
	Win1.document.writeln('			var qMethod = f.classMethod.options[f.classMethod.selectedIndex].value;');	
	Win1.document.writeln('			var theColor = f.tipiColori.options[f.tipiColori.selectedIndex].value;');
	Win1.document.writeln('			if (qMethod==2) {');
	Win1.document.writeln('          t.cr_contaValori(qField,theColor)');
	Win1.document.writeln('	      } else {');
	Win1.document.writeln('			   var theIndex = f.numClasses.options[f.numClasses.selectedIndex].value;');
	Win1.document.writeln('			   if (theIndex==0) {');
	Win1.document.writeln('				   t.cr_clearAllClasses(' + theIndex + ');');
	Win1.document.writeln('			   } else {');
	Win1.document.writeln('				   var qClasses = f.numClasses.options[f.numClasses.selectedIndex].value;');
	Win1.document.writeln('				   if (f.theTitle.value!="") t.cr_Title = f.theTitle.value;');
	Win1.document.writeln('				   t.cr_setupClasses(qField, qClasses, qMethod,theColor);');
	Win1.document.writeln('			   }');
	Win1.document.writeln('			}');
	Win1.document.writeln('		}');
	Win1.document.writeln('		function selectLayer() {');
	Win1.document.writeln('			var f = document.forms[0];');
	Win1.document.writeln('			var theIndex = f.theLayer.selectedIndex;');
	Win1.document.writeln('			t.ClassRenderLayerIndex = theIndex;');
	Win1.document.writeln('			t.cr_classifyLayer();');
	Win1.document.writeln('		}');
	Win1.document.writeln('		function cr_clearClasses() {');
	Win1.document.writeln('			t.ClassRenderLayerIndex = 0;');
	Win1.document.writeln('			t.cr_clearClasses(' + theIndex + ');');
	Win1.document.writeln('			t.sendMapXML();');
	Win1.document.writeln('		}');
	Win1.document.writeln('    function change_items() {');
   Win1.document.writeln('       var f = document.forms[0];');
   Win1.document.writeln('       var metodo = f.classMethod.selectedIndex;');
	Win1.document.writeln('       var ciclo = f.numClasses.length;');
	Win1.document.writeln('  	   for (var i=0;i<=ciclo;i++){');
   Win1.document.writeln('		      f.numClasses.options[ciclo-i]=null;');
   Win1.document.writeln('		   }');
	Win1.document.writeln(' 	   if (metodo != 2){');	
	Win1.document.writeln(' 	      var nessuno = new Option("Nessuno",0);');
	Win1.document.writeln(' 	      f.numClasses.options[0]=nessuno;');		      
	Win1.document.writeln('          var opzioni = new Array (3,4,5,6,7,8,9,10);');
	Win1.document.writeln('          for (var i=1;i<opzioni.length+1;i++) {');
	Win1.document.writeln('              var op = new Option(opzioni[i-1],opzioni[i-1]);');
	Win1.document.writeln('              f.numClasses.options[i]=op;');
	Win1.document.writeln('          }');
   Win1.document.writeln('       }');
   Win1.document.writeln('     }');
   
   Win1.document.writeln('    function change_campo() {');
   Win1.document.writeln('       var f = document.forms[0];');     	
   Win1.document.writeln('       var tipoCampo = f.QueryField.options[f.QueryField.selectedIndex].value;');
   Win1.document.writeln('       if ((tipoCampo<=1) || (tipoCampo>=12))  {  ');
	//E' stata scelto un campo letterale
	Win1.document.writeln('       	var ciclo = f.classMethod.length;');
	Win1.document.writeln('  	      for (var i=0;i<=ciclo;i++){');
   Win1.document.writeln('		         f.classMethod.options[ciclo-i]=null;');
   Win1.document.writeln('		      }');
   Win1.document.writeln(' 	      var valoreUnico = new Option("' + ClassRenderList[28] + '",2);');
   Win1.document.writeln(' 	      f.classMethod.options[0]=valoreUnico;');		      
   Win1.document.writeln('       	var ciclo = f.numClasses.length;');
	Win1.document.writeln('  	      for (var i=0;i<=ciclo;i++){');
   Win1.document.writeln('		         f.numClasses.options[ciclo-i]=null;');
   Win1.document.writeln('		      }');
   Win1.document.writeln('       }');	
	Win1.document.writeln('       else {');
	//E' stato scelto un campo NUMERICO \');');	   
	Win1.document.writeln(' 	      var valoreIntervalli = new Option("' + ClassRenderList[8] + '",0);');
	Win1.document.writeln(' 	      var valoreQuantili = new Option("' + ClassRenderList[9] + '",1);');
	Win1.document.writeln(' 	      var valoreUnico = new Option("' + ClassRenderList[28] + '",2);');
   Win1.document.writeln(' 	      f.classMethod.options[0]=valoreIntervalli;');		      
   Win1.document.writeln(' 	      f.classMethod.options[1]=valoreQuantili;');		      
   Win1.document.writeln(' 	      f.classMethod.options[2]=valoreUnico;');		      
	Win1.document.writeln(' 	      change_items();'); //Per risettare la SELECT delle Classi	
	Win1.document.writeln('       }');	
   Win1.document.writeln('    }');
	
	Win1.document.writeln('</script>');
	Win1.document.writeln('</head>');

	Win1.document.writeln('<body bgcolor="White" topmargin=0 leftmargin=0 rightmargin=0 onload="window.focus(); change_campo();">');
	Win1.document.writeln('<div align="center">');
	Win1.document.writeln('<font size="-2"><b>');
	Win1.document.writeln('<form action="" name="theClassifyForm" onsubmit="setStyle(); return false;">');
	Win1.document.writeln('<table border=0 cellspacing="2" nowrap width=80%>');
	Win1.document.writeln('<tr><th colspan="6" align="CENTER">');
	Win1.document.writeln('<center><font size="-1"><b>' + ClassRenderList[2] + '</b><br></font>');
	Win1.document.writeln('<font  size="-2"><b>' + ClassRenderList[3] + '</b></font></center>');
   Win1.document.writeln('</th>');	
	Win1.document.writeln('</tr>');
	Win1.document.writeln('<tr><th colspan="6" >&nbsp;');
   Win1.document.writeln('</th></tr>');
	Win1.document.writeln('<tr>');
	Win1.document.writeln('<td align="Left">');
	Win1.document.writeln('<font size="-1"> ' + ClassRenderList[5] + '<br>');
	Win1.document.writeln('<select name="theLayer" onchange="selectLayer()">')
	for (var i=0;i<ClassRenderLayer.length;i++) {
		Win1.document.write('	<option value=' + i);
		if (i==crIndex) Win1.document.write(' selected');
		Win1.document.writeln('>' + ClassRenderLayer[i]);
	
	}
	Win1.document.writeln('</select>');
	Win1.document.writeln('</font></td>');
	Win1.document.writeln('<td><font  size="-1">' + ClassRenderList[6] + '<br>');
	Win1.document.writeln('			<select name="QueryField" onchange="change_campo()">');
	for (var i=0;i<LayerFieldCount;i++) {
		var typeNum = parseInt(LayerFieldType[i]);
		if ((typeNum>=1) && (typeNum<=12)) {
			Win1.document.write('		<option value="' + typeNum + '"');
			Win1.document.writeln('>' + LayerFields[i]);
		}
	}
	Win1.document.writeln('			</select>');
	Win1.document.writeln('</font>');
	Win1.document.writeln('</td>');
	
	Win1.document.writeln('<td ><font  size="-1">' + ClassRenderList[7] + '<br>');
	Win1.document.writeln('			<select name="classMethod" onchange="change_items()">');
	Win1.document.writeln('				<option value="0">' + ClassRenderList[8]);	
	Win1.document.writeln('				<option value="1">' + ClassRenderList[9]);
	Win1.document.writeln('				<option value="2">' + ClassRenderList[28]);		
	Win1.document.writeln('			</select>');
	Win1.document.writeln('</font>');
	Win1.document.writeln('</td>');  
	Win1.document.writeln('<td><font size="-1">' + ClassRenderList[10] + '<br>');	
	Win1.document.writeln('			<select name="numClasses">');
	Win1.document.writeln('			<option value="0">' + ClassRenderList[11]);
	for (var i=3;i<11;i++) {
	   Win1.document.write('			<option value="' + i + '"');
		if (cr_Classes == i) Win1.document.write(' selected');
		   Win1.document.writeln('>' + i);
   }
	Win1.document.writeln('			</select>');	
	Win1.document.writeln('</font>');
	Win1.document.writeln('</td>');
	
	
	Win1.document.writeln('<td><font size="-1">' + ClassRenderList[29] + '<br>');
	Win1.document.writeln('			<select name="tipiColori">');
   for (var i=0;i<10;i++) 
      Win1.document.write('			<option value="' + i + '">'+ClassRenderList[i+30]);
   Win1.document.writeln('			</select>');
   Win1.document.writeln('</font>');
	Win1.document.writeln('</td>');
	
	Win1.document.writeln('<td align="CENTER" valign="BOTTOM">');
	Win1.document.writeln('<font  size="-1">');
	Win1.document.writeln(ClassRenderList[4] + '<br>');
	Win1.document.writeln('<INPUT TYPE="Text" NAME="theTitle" VALUE="' + cr_Title + '" SIZE="15"><br><br>');
	Win1.document.writeln('</font>');		
	Win1.document.writeln('<input type="button" class=background name="setDU" value="' + ClassRenderList[12] + '" onclick="setStyle()">');
	Win1.document.writeln('&nbsp;<input type="button" class=background name="annullaDU" value="Annulla" onclick="cr_clearClasses()">');
	Win1.document.writeln('</td></tr></table></form>');
	Win1.document.writeln('</b></font></div>');
	Win1.document.writeln('</body></html>');
	Win1.document.close();
	Win1=null;
	//alert("Finished with page.");
}

// clear out all custom render strings and draw map
function cr_clearAllClasses() {
	cr_clearClassesAll()
	sendMapXML();
}

// clear out all custom render strings
function cr_clearClassesAll() {
	for (var i=0;i<ClassRenderLayer.length;i++) {
		cr_clearClasses(i);
		legTitle = defaultLegTitle;
	}
}
// clear out custom renderer for one of the ClassRenderLayers
function cr_clearClasses(theIndex) {
	ClassRenderString[theIndex] = "";
}

// send XML request for field values to setup new custom renderer for the current ClassRenderLayer
function cr_setupClasses(theField, numClasses, methodIndex, color) {
	cr_clearClassesAll();
	ClassRenderLayerField = theField;
	cr_Method = methodIndex;
	cr_Classes = numClasses;
	cr_SceltaColori = color;
	cr_BreakMin.length=1;
	cr_BreakMax.length=1;
	cr_BreakColor.length=1;
	cr_BreakSize.length=1;
		// turn off all ClassRenderLayers
	for (var m=0;m<ClassRenderLayer.length;m++) {
		for (var n=0;n<layerCount;n++) {
			if (ClassRenderLayer[m]==LayerName[n]) LayerVisible[n]=0;
		}
	}
	
	var theLayer = ClassRenderLayer[ClassRenderLayerIndex];
		// get LayerName index
	var j=-1;
	for (var k=0;k<LayerName.length;k++) {
		if (LayerName[k]==theLayer) j=k;
	}
		// get shape type
	var shapeType = LayerType[j];
	var theLayerID = LayerID[j]
		// turn on this layer
	LayerVisible[j]=1;
	
	if (numClasses>0) {
		showRetrieveData();
		var theString = cr_writeGetAllValues(theLayerID, shapeType, theField);
		cr_sendGetValuesRequest(theString, 2001);
	} else {
		cr_clearAllClasses();
	}
	
}

function cr_writeGetAllValues(theLayerID, shapeType, theField) {
	var theString = "";
	theString = '<ARCXML VERSION="1.1">\n';
	theString += '<REQUEST>\n';
	theString += '	<GET_FEATURES outputmode="xml" geometry="false" envelope="false" compact="true">\n';
	theString += '		<LAYER id="' + theLayerID + '" ftype="' + shapeType + '" />\n';
	//theString += '		<QUERY subfields="' + theField + '" where="#ID# > -1" />\n';
	theString += '		<QUERY subfields="' + theField + '" />\n';
	theString += '	</GET_FEATURES>\n';
	theString += '</REQUEST>\n';
	theString += '</ARCXML>\n';
	
	return theString;

}

function cr_sendGetValuesRequest(XMLRequest, theType) {
	var theFunction = "parent.MapFrame.cr_processClassRenderXML";
	//alert(XMLRequest);
	sendCustomToServer(XMLRequest,theFunction, theType)
}

function cr_processClassRenderXML(theReplyIn) {   
   
	theReplyIn = replacePlus(theReplyIn);
	var theReply = unescape(theReplyIn);
	//alert(theReply);
	okToSend = true;
	if (debugOn>2) alert(msgList[13] + theReply);
	if (XMLMode == 2001) {
		//alert("XMLMode=2001");
		cr_setClassRenderBreaks(theReply);	
	}
	if (XMLMode == 2002) {
		//alert("XMLMode=2002");
		cr_calccr_fieldStats(theReply,ClassRenderLayerName,ClassRenderLayerField);	
	}
	if (XMLMode == 2003) {
	   cr_tematizzaSe(theReply,ClassRenderLayerName,ClassRenderLayerField);
	}
	
}

function cr_tematizzaSe(theReply,ClassRenderLayerName,ClassRenderLayerField){
   cr_Count = justGetFeatureCount(theReply);			
	var theValues = getAllFieldValuesString(theReply,ClassRenderLayerField,cr_Count);
	
	//alert(theValues);
	theValues.sort();		
   ClassRenderLista_valori=new Array;
	ClassRenderLista_valori[0]=theValues[0]
	var j=0;
	for (var i=1;i<theValues.length;i++) {
		 if(theValues[i]!=theValues[i-1]){		   
		   j++;
		   ClassRenderLista_valori[j]=theValues[i];
	   }
	}
	//alert(ClassRenderLista_valori);
	if (ClassRenderLista_valori.length>20){
		alert("sono troppi i valori per cui tematizzare");
		hideRetrieveData();		
	} else {
	   //alert("tematizzo per "+ClassRenderLista_valori.length+" valori"); 
	   cr_Classes=ClassRenderLista_valori.length;
	   cr_creaTematizzazione();
   }	   
   
}
	

function cr_contaValori(theField,theColor){
	cr_clearClassesAll();
	ClassRenderLayerField = theField;	
	cr_SceltaColori = theColor;
	cr_Method = 2;
	
	// turn off all ClassRenderLayers
	for (var m=0;m<ClassRenderLayer.length;m++) {
		for (var n=0;n<layerCount;n++) {
			if (ClassRenderLayer[m]==LayerName[n]) LayerVisible[n]=0;
		}
	}
	
	var theLayer = ClassRenderLayer[ClassRenderLayerIndex];
		// get LayerName index
	var j=-1;
	for (var k=0;k<LayerName.length;k++) {
		if (LayerName[k]==theLayer) j=k;
	}
		// get shape type
	var shapeType = LayerType[j];
	var theLayerID = LayerID[j]
		// turn on this layer
	LayerVisible[j]=1;
	
	showRetrieveData();	
	var theString = cr_writeGetAllValues(theLayerID, shapeType, theField);
	cr_sendGetValuesRequest(theString, 2003);	
}


function cr_setClassRenderBreaks(theReply) {
	cr_Count = justGetFeatureCount(theReply);	
	//alert(cr_Count + " Features");
	if (cr_Count>0) {
		if (cr_Count<cr_Classes) cr_Classes = cr_Count;
		var theValues = getAllFieldValues(theReply,ClassRenderLayerField,cr_Count);
		//alert("Values extracted");		
		theValues.sort(numberorder);		   		
		//alert("Values sorted");
		cr_Min = theValues[0];
		cr_Max = theValues[cr_Count-1];
		cr_Range = cr_Max - cr_Min;
		cr_BreakIncrement = cr_Range / cr_Classes;
		cr_RecordIncrement = Math.round(cr_Count / cr_Classes);
		//alert("Min:" + cr_Min + "\nMax:" + cr_Max);
		var msg = ClassRenderList[13] + "\n";
		if (cr_Method==0) {
			// equal interval
			var u = Math.pow(10,numDecimals);
			var theIncre = cr_Min;
			var theIncre2 = theIncre;
			if (theIncre!=0) theIncre2 = parseInt(theIncre * u + (5/10)) / u;
			for (var i=0;i<cr_Classes;i++) {
				cr_BreakMin[i] = theIncre2;
				theIncre = theIncre + cr_BreakIncrement;
				if (theIncre!=0) {
					theIncre2 = parseInt(theIncre * u + (5/10)) / u;
				} else {
					theIncre2 = theIncre;
				}
				cr_BreakMax[i] = theIncre2;
				msg +=theIncre + "\n";
			}
		} else {
			// quantile
			var theRecord = 0;
			for (var i=0;i<cr_Classes;i++) {
				cr_BreakMin[i] = theValues[theRecord];
				theRecord = theRecord + cr_RecordIncrement;
				if (theRecord>=cr_Count) theRecord = cr_Count-1;
				cr_BreakMax[i] = theValues[theRecord];
				msg +=theValues[theRecord] + "\n";
			}
		}
		cr_BreakMax[cr_Classes-1] = cr_Max;
		//alert(msg);
		if(cr_SceltaColori!=9){
         cr_setBreakColors(cr_SceltaColori);
      } else {
         //alert("temi vari");
         cr_setClassRenderPersonalColor();
		}
		cr_setBreakSizes();
		ClassRenderString[ClassRenderLayerIndex] = ClassRenderStringPrefix[ClassRenderLayerIndex] + cr_setRenderString() + ClassRenderStringSuffix[ClassRenderLayerIndex];
		legTitle = cr_Title;
		theValues=null;
	}
	hideRetrieveData();
	if (aimsLayersPresent) {
		if ((hasTOC) && (!legendVisible)) parent.TOCFrame.document.location = appDir + "toc.htm";
		if (LayerListOpen) writeLayerListForm();
	}
	sendMapXML();
}

function cr_setClassRenderPersonalColor(){   
   if (cr_Classes>10){
      for (var i=0;i<(cr_Classes);i++){
		   cr_BreakColor[i] = ClassRenderPersonalColor[i];     
     }
  } else {
     for (var i=0;i<(cr_Classes);i++) {
        var j = i+i;
		  cr_BreakColor[i] = ClassRenderPersonalColor[j];     
	  }
  }
}
			
			   

function cr_creaTematizzazione(){
   cr_setColori();
   ClassRenderString[ClassRenderLayerIndex] = ClassRenderStringPrefix[ClassRenderLayerIndex] + cr_cr_setRenderStringVU() + ClassRenderStringSuffix[ClassRenderLayerIndex];
	//alert(ClassRenderString[ClassRenderLayerIndex]);
	legTitle = cr_Title;
	theValues=null;

	hideRetrieveData();
	if (aimsLayersPresent) {
		if ((hasTOC) && (!legendVisible)) parent.TOCFrame.document.location = appDir + "toc.htm";
		if (LayerListOpen) writeLayerListForm();
	}
	hideRetrieveData();
	sendMapXML();
}

function cr_setColori(){
   if(cr_SceltaColori!=9){
      cr_setBreakColors(cr_SceltaColori);
   } else {
      cr_setClassRenderPersonalColor();
   }
}



function cr_cr_setRenderStringVU() {
	var rendString = '<VALUEMAPRENDERER lookupfield="' + ClassRenderLayerField + '">\n';
	for (var z=0;z<cr_Classes;z++) {
		rendString += '<EXACT value="' + ClassRenderLista_valori[z] +'" >\n';
		if (ClassRenderShape[ClassRenderLayerIndex]=="point") {
			rendString += '<SIMPLEMARKERSYMBOL color="' + cr_BreakColor[z] + '" type="' + ClassRenderStyle[ClassRenderLayerIndex] + '" width="10" />\n';
		} else if (ClassRenderShape[ClassRenderLayerIndex]=="polygon") {
			rendString += '<SIMPLEPOLYGONSYMBOL fillcolor="' + cr_BreakColor[z] + '" filltype="' + ClassRenderStyle[ClassRenderLayerIndex] + '" ';
			
			if (ClassRenderOutlineColor[ClassRenderLayerIndex]!="") {
				rendString += 'boundarycolor="' + ClassRenderOutlineColor[ClassRenderLayerIndex] + '" boundarywidth="' + cr_BreakSize[z] + '" ';
			} else {
				rendString += 'boundary="false" ';
			}
			
			rendString += '/>\n';
		} else {
			rendString += '<SIMPLELINESYMBOL type="' + ClassRenderStyle[ClassRenderLayerIndex] + '" color="' + cr_BreakColor[z] + '" width="3" />\n';
		}
		rendString += '</EXACT>\n';
	}	
	rendString += '</VALUEMAPRENDERER>';
	
	return rendString
}




function cr_setRenderString() {
	var rendString = '<VALUEMAPRENDERER lookupfield="' + ClassRenderLayerField + '">\n';
	for (var z=0;z<cr_Classes;z++) {
		rendString += '<RANGE lower="' + cr_BreakMin[z] + '" upper="' + cr_BreakMax[z] + '">\n';
		if (ClassRenderShape[ClassRenderLayerIndex]=="point") {
			rendString += '<SIMPLEMARKERSYMBOL color="' + cr_BreakColor[z] + '" type="' + ClassRenderStyle[ClassRenderLayerIndex] + '" width="10" />\n';
		} else if (ClassRenderShape[ClassRenderLayerIndex]=="polygon") {
			rendString += '<SIMPLEPOLYGONSYMBOL fillcolor="' + cr_BreakColor[z] + '" filltype="' + ClassRenderStyle[ClassRenderLayerIndex] + '" ';
			
			if (ClassRenderOutlineColor[ClassRenderLayerIndex]!="") {
				rendString += 'boundarycolor="' + ClassRenderOutlineColor[ClassRenderLayerIndex] + '" boundarywidth="' + cr_BreakSize[z] + '" ';
			} else {
				rendString += 'boundary="false" ';
			}
			
			rendString += '/>\n';
		} else {
			rendString += '<SIMPLELINESYMBOL type="' + ClassRenderStyle[ClassRenderLayerIndex] + '" color="' + cr_BreakColor[z] + '" width="3" />\n';
		}
		rendString += '</RANGE>\n';
	}
	
	
	rendString += '</VALUEMAPRENDERER>';
	
	return rendString
}

// ramp out colors
function cr_setBreakColors(colore) {
   //alert("colore " +colore);
	if (ClassRenderStartColor[colore]==ClassRenderEndColor[colore]) {
		// starting and ending colors are the same
		for (var i=0;i<cr_Classes;i++) {
			cr_BreakColor[i] = ClassRenderStartColor[colore];
		}
	} else {
		var sColors = ClassRenderStartColor[colore].split(",");
		var eColors = ClassRenderEndColor[colore].split(",");
		var sRed = parseInt(sColors[0]);
		var sGreen = parseInt(sColors[1]);
		var sBlue = parseInt(sColors[2]);
		var eRed = parseInt(eColors[0]);
		var eGreen = parseInt(eColors[1]);
		var eBlue = parseInt(eColors[2]);
		var rRange = eRed - sRed;
		var gRange = eGreen - sGreen;
		var bRange = eBlue - sBlue;
		var rIncre = parseInt(rRange / cr_Classes);
		var gIncre = parseInt(gRange / cr_Classes);
		var bIncre = parseInt(bRange / cr_Classes);
		var theRed = sRed + rIncre;
		var theGreen = sGreen + gIncre;
		var theBlue = sBlue + bIncre;
		cr_BreakColor[0] = ClassRenderStartColor[colore];
		cr_BreakColor[cr_Classes-1] = ClassRenderEndColor[colore];
		for (var i=1;i<(cr_Classes-1);i++) {
			cr_BreakColor[i] = theRed + "," + theGreen + "," + theBlue;
			theRed = theRed + rIncre;
			theGreen = theGreen + gIncre;
			theBlue = theBlue + bIncre;
		}
		sColors=null;
		eColors=null;
		
	}
}

// ramp out sizes
function cr_setBreakSizes() {
	if (ClassRenderStartSize[ClassRenderLayerIndex]==ClassRenderEndSize[ClassRenderLayerIndex]) {
		// starting and ending sizes are the same
		for (var i=0;i<cr_Classes;i++) {
			cr_BreakSize[i] = ClassRenderStartSize[ClassRenderLayerIndex];
		}
	} else {
		cr_BreakSize[0] = ClassRenderStartSize[ClassRenderLayerIndex];
		cr_BreakSize[cr_Classes-1] = ClassRenderEndSize[ClassRenderLayerIndex];
		var sRange = ClassRenderEndSize[ClassRenderLayerIndex] - ClassRenderStartSize[ClassRenderLayerIndex];
		var sIncre = parseInt(sRange * 10 + (5/10)) / 10;
		var theSize = ClassRenderStartSize[ClassRenderLayerIndex] + sIncre;
		for (var i=1;i<(cr_Classes-1);i++) {
			cr_BreakSize[i] = theSize;
			theSize = theSize + sIncre;
		}

	}

}

function cr_calccr_fieldStats(theReply,theLayer,theField) {
   //alert(theReply);
	inCount = justGetFeatureCount(theReply);
	hideRetrieveData();
	var theValues = getAllFieldValues(theReply,theField,inCount);
	var theCount = theValues.length;
	if (theCount>0) {
		
		//alert("Values extracted");
		theValues.sort(numberorder);
		//alert("Values sorted");
		
		var theMin = theValues[0];
		var theMax = theValues[theCount-1];
		var theRange = theMax - theMin;
		var theVarInt = 0;
		var theSum = 0;
		for (var i=0;i<theCount;i++) {
			theSum = theSum + theValues[i];
			theVarInt = theVarInt + Math.pow(theValues[i],2);
		}
		var u = Math.pow(10,cr_numStatDecimals);
		var theAverage = theSum / theCount;
		var theVariance = (theVarInt - theCount * Math.pow(theAverage,2)) / (theCount-1);
		var theStdDev = Math.sqrt(theVariance);
		if (theAverage!=0) theAverage = parseInt(theAverage * u + 0.5) / u;
		if (theStdDev!=0) theStdDev = parseInt(theStdDev * u + 0.5) / u;
		if (theSum!=0) theSum = parseInt(theSum * u + 0.5) / u;
		
		var msg = ClassRenderList[14] + theField ;
		msg +="\n" + ClassRenderList[15] + theLayer;
		msg +="\n\n" + ClassRenderList[16] + theCount + "\n" + ClassRenderList[17] + theMin;
		msg +="\n" + ClassRenderList[18] + theMax + "\n" + ClassRenderList[19] + theSum;
		msg +="\n" + ClassRenderList[20] + theAverage + "\n" + ClassRenderList[21] + theRange;
		msg +="\n" + ClassRenderList[22] + theVariance;
		msg +="\n" + ClassRenderList[23] + theStdDev;
		
		
		var Winstat = open("","StatisticWindow","width=300,height=250,scrollbars=yes,resizable=yes");
		Winstat.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head><title>' + titleList[3] + '</title>');
		Winstat.document.writeln('<link rel="stylesheet" href="'+pagStile+'" type="text/css">');
		Winstat.document.writeln('</head><body>');
		Winstat.document.writeln('<div align=center><font size="-1"><b>'+ClassRenderList[15] + theLayer+'<br><br>');		
		Winstat.document.writeln(ClassRenderList[14] + theField+'<br><br></b></font>');
		Winstat.document.writeln('<table width=80%><tr><td>');
		Winstat.document.writeln(ClassRenderList[16] +'</td><td>'+ theCount+'</td></tr><tr><td>');
		Winstat.document.writeln(ClassRenderList[17] + '</td><td>'+theMin+'</td></tr><tr><td>');
		Winstat.document.writeln(ClassRenderList[18] + '</td><td>'+theMax+'</td></tr><tr><td>');
		Winstat.document.writeln(ClassRenderList[19] + '</td><td>'+theSum+'</td></tr><tr><td>');
		Winstat.document.writeln(ClassRenderList[20] + '</td><td>'+theAverage+'</td></tr><tr><td>');
		Winstat.document.writeln(ClassRenderList[21] +'</td><td>'+ theRange+'</td></tr><tr><td>');
		Winstat.document.writeln(ClassRenderList[22] +'</td><td>'+ theVariance+'</td></tr><tr><td>');
		Winstat.document.writeln(ClassRenderList[23] + '</td><td>'+theStdDev+'</td></tr>');
		
		Winstat.document.writeln('</table></div></body></html>');
		Winstat.document.close();
		Winstat.focus();
		
		
		//alert(msg);
	
	} else {
		alert(ClassRenderList[24]);
	}
	theValues=null;
}

// form for interactive classifying
function cr_writecr_fieldStatsForm(theIndex) {
	var crIndex = theIndex;
	if (useTextFrame) {
		var Win1 = parent.TextFrame;
		Win1.document.open();
		var t = "parent.MapFrame";
		
	} else {
		var Win1 = open(appDir + "text.htm","RenderWindow","width=575,height=150,scrollbars=yes,resizable=yes");
		var t = "opener";
		if (parent.MapFrame!=null) t = "opener.parent.MapFrame";
	}
	
	Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><HEAD>');
	Win1.document.writeln('<link rel="stylesheet" href="'+pagStile+'" type="text/css"><script language="javascript">');
	Win1.document.writeln('		var t = ' + t);
	
	Win1.document.writeln('		function setStyle() {');
	Win1.document.writeln('			var f = document.forms[0];');
	Win1.document.writeln('			var qField = f.QueryField.options[f.QueryField.selectedIndex].value;');
	Win1.document.writeln('			t.cr_setupcr_fieldStats(qField);');
	Win1.document.writeln('		}');
	Win1.document.writeln('		function selectLayer() {');
	Win1.document.writeln('			var f = document.forms[0];');
	Win1.document.writeln('			var theIndex = f.theLayer.selectedIndex;');
	Win1.document.writeln('			t.ClassRenderLayerIndex = theIndex;');
	Win1.document.writeln('			t.cr_fieldStats();');
	Win1.document.writeln('		}');
	Win1.document.writeln('</script>');
	Win1.document.writeln('</head>');

	Win1.document.writeln('<body bgcolor="White" topmargin=0 leftmargin=0 rightmargin=0 onload="window.focus()">');
	Win1.document.writeln('<div align="center">');
	Win1.document.writeln('<font  size="-2"><b><br>');
	Win1.document.writeln('<form action="" name="theStatsForm" onsubmit="setStyle(); return false;">');
	Win1.document.writeln('<table border=0 cellspacing="2" nowrap  width=60%>');
	Win1.document.writeln('<tr><th colspan="3" align="CENTER">');
	Win1.document.writeln('<font  size="-1"><b>' + ClassRenderList[25] + '</b><br></font>');
	Win1.document.writeln('<font size="-2"><b>' + ClassRenderList[26] +'</b></font>');
	Win1.document.writeln('</th>');
	Win1.document.writeln('</tr>');
	Win1.document.writeln('<tr>');
	Win1.document.writeln('<td align="Left">');
	Win1.document.writeln('<font  size="-1"> ' + ClassRenderList[5] + '<br>');
	Win1.document.writeln('<select name="theLayer" onchange="selectLayer()">')
	
	for (var i=0;i<ClassRenderLayer.length;i++) {
		Win1.document.write('	<option value=' + i);
		if (i==crIndex) Win1.document.write(' selected');
		Win1.document.writeln('>' + ClassRenderLayer[i]);	
	}
	Win1.document.writeln('</select>');
	Win1.document.writeln('</font></td>');
	Win1.document.writeln('<td><font size="-1">' + ClassRenderList[6] + '<br>');
	Win1.document.writeln('			<select name="QueryField">');
	setLayerFields(ActiveLayerIndex);
	
	for (var i=0;i<LayerFieldCount;i++) {
	   //alert(LayerFields[i]);
	   //alert(LayerFieldType[i]);
		var typeNum = Math.abs(parseInt(LayerFieldType[i]));
		if ((typeNum>=4) && (typeNum<=9)) {
			Win1.document.write('		<option value="' + LayerFields[i] + '"');
			Win1.document.writeln('>' + LayerFields[i]);
		}
	}
	Win1.document.writeln('			</select>');
	Win1.document.writeln('</font>');
	Win1.document.writeln('</td>');
	
	Win1.document.writeln('<td align="CENTER" valign="BOTTOM">');
	Win1.document.writeln('<input type="button" class=background name="setDU" value="' + ClassRenderList[12] + '" onclick="setStyle()">  <br>');
	
	Win1.document.writeln('</td></tr></table></form>');
	Win1.document.writeln('</b></font></div>');
	Win1.document.writeln('</body></html>');
	Win1.document.close();
	Win1=null;
}

// send XML request for field values to setup new custom renderer for the current ClassRenderLayer
function cr_setupcr_fieldStats(theField) {
	ClassRenderLayerField = theField;
	var theLayer = ClassRenderLayer[ClassRenderLayerIndex];
		// get LayerName index
	var j=-1;
	for (var k=0;k<LayerName.length;k++) {
		if (LayerName[k]==theLayer) j=k;
	}
		// get shape type
	var shapeType = LayerType[j];
	var theLayerID = LayerID[j]
	showRetrieveData();
	var theString = cr_writeGetAllValues(theLayerID, shapeType, theField);
	//alert(theString);
	ClassRenderLayerName = theLayer;
	cr_sendGetValuesRequest(theString, 2002);
	
}

// set up the interactive classification
function cr_fieldStats() {
	var j = -1;
	for (var i=0;i<LayerName.length;i++) {	  	   
		if (ClassRenderLayer[ClassRenderLayerIndex] == LayerName[i]) j = i;
	
	}
	if (j>-1) {
		//if (useTextFrame) parent.TextFrame.document.location = appDir + "text.htm";
		setLayerFields(j);
		fieldIndex=0;
		selectCount=0;
		showBuffer=false;
		ActiveLayer=LayerID[j];
		ActiveLayerType=LayerType[j];
		ActiveLayerIndex=j;
		cr_writecr_fieldStatsForm(ClassRenderLayerIndex);
	} else {
		alert(ClassRenderList[27]);
	}
}

