/*
** This file was created by Cuesta Systems Inc, August 2004
** It contains common functions for handling dynamic layers

/************************************************************************
** This function creates the dynamic string sent to Mapserver
** to render the dynamic layers
*/
function csy_loadDynamicData(boolSend)
{
	showRetrieveMap();
	var theForm = top.PostFrame.document.forms[0];

	var str ="";
	for (var i=0;i<LayerID.length; i++)
		if (LayerisDynamic[i] == 1)
		{
			//str += shpCustStringArray[i] + "&";
			str += csy_createDynamicLayersString(i) + "&";
		}

	str = str.substring(0,str.length-1);
	dynamicDataString = str;
	theForm.CMD.value = "DYNAMICDATA";
	theForm.redraw.value = "Y";
	theForm.dynamicDataString.value = str;
	
	if (boolSend)
		theForm.submit();
	else
		hideRetrieveMap();
}
/************************************************************************
** This function creates the dynamic string sent to Mapserver
** to render the dynamic layers
*/
function csy_createDynamicLayersString(currNr)
{
	var customString = "";
	var strTemp = "";
	 //id = name
	 customString += LayerID[currNr] +"#";
	 //data
	 customString += LayerDataPath[currNr] + "\\" + LayerDataSet[currNr] +"#";
	 //type
	 customString += LayerType[currNr] +"#";
	 //status
	 customString += LayerVisible[currNr] +"#";

	if (LayerType[currNr].toLowerCase() == "point" || LayerType[currNr].toLowerCase() == "line" || LayerType[currNr].toLowerCase() == "polygon")
	{	//send the default customization
		strTemp = KTDefaultLayerString[currNr] + "|" + KTDefaultLabelString[currNr];			
		customString += strTemp;
		return customString;
	} else { //it is an image
		customString += "Null";	
		return customString;
	}
	
}
/************************************************************************
** This function populates the layers arrays with the dynamic layers
** based on an input string
*/
function csy_populateDynamicArrays(str)
{
	arrDynGeneral = str.split("&");
	for (w=0; w<arrDynGeneral.length; w++)
	{//for each dynamic layer
		arrDyn = arrDynGeneral[w].split("#");       	

		var currNr = LayerName.length;
		LayerisDynamic[currNr] = 1;
		LayerID[currNr] = arrDyn[0];
		LayerDataSet[currNr] = arrDyn[0];
		LayerName[currNr] = arrDyn[0];
		LayerType[currNr] = arrDyn[2];
		var tmp = arrDyn[1];
		var cc = tmp.lastIndexOf("\\",tmp.length-1);
		if (cc != -1)
			tmp = tmp.substr(0,cc);
		LayerDataPath[currNr] = tmp;
	
		if (LayerType[currNr].toLowerCase() == "point" || LayerType[currNr].toLowerCase() == "line" || LayerType[currNr].toLowerCase() == "polygon")
			LayerIsFeature[currNr] = 1;
		else
			LayerIsFeature[currNr] = 0;
		
		LayerOrderInitialPHP[currNr] = LayerID[currNr];
		LayerOrderCurrentPHP[currNr] = LayerID[currNr];
		LayerVisible[currNr] = 1;
		loadedLayers[currNr] = 1;

		LayerLegendArray[currNr] = "";
		LayerLegendNameArray[currNr] = "";

		LayerMaxScale[currNr] = 1.7976931348623157E308;
		LayerMinScale[currNr] = -1;
		LayerClassMaxScale[currNr] = 1.7976931348623157E308;
		LayerClassMinScale[currNr] = -1;
		LayerClassStatus[currNr] = 1;
		LayerExtent[currNr] = "";
		LayerFieldList[currNr]="";
		LayerFieldAliasList[currNr]="";

		//default symbology
		var symb = "";
		switch (LayerType[currNr])
		{
			case "point":
				symb = "star|8|0~76~115";
				break
			case "line":
				symb = "circle|2|0~76~115";
				break
			case "polygon":
				symb = "screen|100|0~76~115|none|1|-1~-1~-1";
				break
		}
		KTType[currNr] = "simple";
		KTTypeDef[currNr] = "simple";
		KTCustomType[currNr] = "os";
		KTDefaultType[currNr] = "os";
		KTDefaultLayerString[currNr] = symb;
		KTCustomLayerString[currNr] = symb;
		KTDefaultLabelString[currNr] = "NOLABELS";
		KTCustomLabelString[currNr] = "NOLABELS";
		
		//MS Oct 19, 2004
		LayerJNameList[currNr]="";
		LayerJTabList[currNr]=""; //AD Nov 19, 2004
		LayerJConnectionList[currNr]="";
		LayerJSourceFieldList[currNr]="";
		LayerJFieldList[currNr]="";
		LayerJFieldAliasList[currNr]="";
		LayerJClauseList[currNr]="";
		LayerJTargetFieldTypeList[currNr]="";
	}
}
//*************************************************************************
/*
** This function creates the dynamic string sent to Mapserver
** to render the persisted layers
*/
function csy_loadPersistLayer(boolSend)
{
	showRetrieveMap();
	var theForm = top.PostFrame.document.forms[0];

	var str ="";
	for (var i=0;i<LayerID.length; i++)
	{	if (LayerisPersist[i] == 1 )
		{
			str += csy_createPersistLayersString(i) + "&";
		}
	}
	if (str.length>0)str = str.substring(0,str.length-1);
	persistDataString = str;
	theForm.CMD.value = "PERSISTLAYER";
	theForm.redraw.value = "Y";
	theForm.persistDataString.value = str;
	if (boolSend)
		theForm.submit();
	else
		hideRetrieveMap();
}
//*************************************************************************

function csy_createPersistLayersString(currNr)
{
	var strPersist = "";
	var strTemp = "";
	 //id = name
	 strPersist += LayerID[currNr] +"`";
	 //data
	 strPersist += LayerDataPath[currNr] + "\\" + LayerDataSet[currNr] +"`";
	 //type
	 strPersist += LayerType[currNr] +"`";
	 //status
	 strPersist += LayerVisible[currNr] +"`";

	strTemp = KTDefaultLayerString[currNr] + "|" + KTDefaultLabelString[currNr];	
	
	strPersist += strTemp;
	strPersist += "`" + t.PersistLayerSource[currNr]; //SP May 2006
	
	return strPersist;
	
		
}


//*************************************************************************
/*
** This function creates the dynamic string sent to Mapserver
** to render the raster layers
*/
function csy_loadRasterLayer(boolSend)
{
	showRetrieveMap();
	var theForm = top.PostFrame.document.forms[0];

	var str ="";
	for (var i=0;i<LayerID.length; i++)
	{	if (LayerisRaster[i] == 1 )
		{
			str += csy_createRasterLayersString(i) + "&";
		}
	}
	if (str.length>0)str = str.substring(0,str.length-1);
	RasterDataString = str;
	//alert(str);
	theForm.CMD.value = "ADDRASTER";
	theForm.redraw.value = "Y";
	theForm.rasterDataString.value = str;
	if (boolSend)
		theForm.submit();
	else
		hideRetrieveMap();
}
//*************************************************************************

function csy_createRasterLayersString(currNr)
{
	var strRaster = "";
	
	 //id = name
	 strRaster += LayerID[currNr] +"#";
	 //data
	 strRaster += LayerDataPath[currNr]+"#";
	 //status
	strRaster += LayerVisible[currNr]+"#";
	// Extent - not determinable for raster layers in mapscript
	strRaster += LayerExtent[currNr];
	
	return strRaster;
}
//*************************************************************************

function csy_updateArrayRasterLayers(currNr)
{
     	     	
     	 
	LayerisPersist[currNr]= "";		
	PersistLayerDataPath[currNr]= "" ;		
	PersistLayerSource[currNr]= "";		
	PersistLayerType[currNr]= "";
	PersistLayerWidth[currNr]= "";
	PersistLayerQry[currNr]= "";
	LayerJSourceFieldTypeWidth[currNr]= "";
	LayerID[currNr]= "";	
	LayerDataSet[currNr]= "";

	LayerIsFeature[currNr]= "";
	loadedLayers[currNr]= "";
	LayerLegendArray[currNr]= "";
	LayerLegendNameArray[currNr]= "";
	LayerMaxScale[currNr]= "";
	LayerMinScale[currNr]= "";
	LayerClassMaxScale[currNr]= "";
	LayerClassMinScale[currNr]= "";
	LayerClassStatus[currNr]= "";
	LayerExtent[currNr]= "";
	LayerisDynamic[currNr]= "";
	LayerFieldList[currNr]= "";

	LayerFieldTypeList[currNr]= "";
	LayerFieldWidthList[currNr]= "";
	LayerIdentifyFields[currNr]= "";
	LayerOnLegend[currNr]= ""; //AD June 2007
	LayerFieldVisibleList[currNr]= ""; //AD June 2007

	DistributableLayers[currNr]= "";

	LayerJNameList[currNr]= "";
	LayerJTabList[currNr]= "" ;
	LayerJConnectionList[currNr]= "";
	LayerJFieldList[currNr]= "";
	LayerJFieldAliasList[currNr]= "";
	LayerJClauseList[currNr]= "";
	LayerJTargetFieldTypeList[currNr]= "";
	KTType[currNr]= "";
	KTTypeDef[currNr]= "";
	KTCustomType[currNr]= "";
	KTDefaultType[currNr]= "";
	KTDefaultLayerString[currNr]= ""
	KTCustomLayerString[currNr]= "";
	KTDefaultLabelString[currNr]= "";
	KTCustomLabelString[currNr]= "";
	KTSymbologyCustomized[currNr]= "";
	LayerFieldTypeList[currNr]= "";
	LayerConnectionName[currNr]= "";
	LayerConnectionTable[currNr]= "";
	LayerJTargetTableList[currNr]= "";
	LayerJTargetFieldList[currNr]= "";
	LayerJFieldTypeList[currNr]= "";

	LayerHyperLinkField[currNr]= "";
	LayerHyperLinkPrefix[currNr]= "";
	LayerHyperLinkSuffix[currNr]= "";
	LayerJHyperLinkField[currNr]= "";
	LayerJHyperLinkPrefix[currNr]= "";
	LayerJHyperLinkSuffix[currNr]= "";
	
	LayerSJFieldList[currNr]= "";
	LayerSJFieldTypeList[currNr]= "";
	LayerSJFieldAliasList[currNr]= "";
	LayerSJHyperLinkField[currNr]= "";
	LayerSJHyperLinkPrefix[currNr]= "";
	LayerSJHyperLinkSuffix[currNr]= "" ;
	LayerJFieldList_FINAL[currNr]= "" ;
	LayerJFieldAliasList_FINAL[currNr]= "";
	LayerJFieldTypeList_FINAL[currNr]= "";
	LayerJHyperLinkField_FINAL[currNr]= "";
	LayerJHyperLinkPrefix_FINAL[currNr]= "" ;
	LayerJHyperLinkSuffix_FINAL[currNr]= "";
	
	LayerJFieldVisibleList[currNr]= ""; //AD June 2007
	LayerSJFieldVisibleList[currNr]= ""; //AD June 2007
	
   }
