/*
** This file was created by Cuesta Systems Inc, Aug 2004
** It contains functions used for Custom Query
** Created By: Aida Dodo, Cuesta Systems
** On: August 24, 2004
*/


/****************************************************************************
** This function sets the initialize the SelectValues selection pull down for custom query, 
** Created By: Aida Dodo, August 23 2004
** Modified By: Aida Dodo, Oct 13, 2004 for browser compatibility
** - Used getElementById to get field.
** - Browser check implemented for using StreetName2_OnChange()/StreetName2_OnChangeNET() function
*/
function csy_processSelectValues(rsp) {

	//AD June 22, 2005 - check for if MAX RECORDS is reached
	if (rsp[rsp.length-1].substring(0,12)=="MAXREACHEDOF") 
	{
		var maxLimit = rsp[rsp.length-1].substring(12,rsp[rsp.length-1].length);
		alert("Too many records found to be retrieved, \nonly the first "+maxLimit+" records will be displayed!");
		rsp.splice(rsp.length-1,1);	
	}
	//AD June 22, 2005
	
	//var txtForm = top.TextFrame.document.all;
	var txtForm = top.TextFrame.document;
	res = "";
	//var values = rsp.sort();//AD Mar 21, 2005 - already order in sql result
	var values = rsp;
	res += "<Select id='SelectValue' name='SelectValue' size=7 style='FONT: 8pt Times Roman;' onchange='SelectValue_OnChange()'>";
	res += "<option selected value=''>";
		lastVal = "";
		for (var i=0;i<values.length;i++)
		{
			if (values[i].length>0){
				if (values[i] != lastVal)
				{
					res += "<option value='" + values[i] + "'>" + values[i] + "</option>";
				}
				lastVal = values[i];
			}
		}
	res += "</SELECT><br>";
	//txtForm.SelectValue.innerHTML = res;
	//AD Oct 3, 2004
	//txtForm.Instruct1.innerHTML = "";
	//AD Oct 13, 2004
	txtForm.getElementById("SelectValue").innerHTML = res;
	txtForm.getElementById("Instruct1").innerHTML = "";
}
//***************************************************************************


/****************************************************************************
** This function returns the field alias name, from the layerList.xml file.
** used in Custom Query. Returns field name is no alias name defined.
** Created By: Aida Dodo, August 23 2004
** Modified By: 
*/
function csy_getfieldAliasName(layerIndex, fieldName) {
var str = fieldName;
var tempVal = LayerFieldAliasList[layerIndex].split(",");
	if (tempVal.length > 0) {
		for (var i=0;i<tempVal.length;i++) 	{			
			var tempPair = tempVal[i].split(":");
			if (Trim(tempPair[0]) == fieldName) {
				str = tempPair[1];
				i = tempVal.length;
			}//end if
		}//end for
	}//end if
	return str;
}
//***************************************************************************

/****************************************************************************
** This function returns the Join field alias name, from the layerList.xml file.
** used in Custom Query. Returns field name is no alias name defined.
** Created By: Aida Dodo, August 2 2005
** Parameters:	- layerIndex: layer index
** 		- joinIndex: join order number layer list, starting from 0.
** Modified By: 
*/
function csy_getJfieldAliasName(layerIndex, joinIndex, fieldName) {
var str = fieldName;
var tempVal = LayerJFieldAliasList[layerIndex].split(",");
	if (tempVal.length > 0) {
		for (var i=0;i<tempVal.length;i++) 	{			
			var tempPair = tempVal[i].split(":");
			if (Trim(tempPair[0]) == fieldName) {
				str = tempPair[1];
				i = tempVal.length;
			}//end if
		}//end for
	}//end if
	return str;
}
//***************************************************************************

/****************************************************************************
** This function returns the field alias name, from the layerList.xml file.
** used in Custom Query. Returns field name is no alias name defined.
** get alias field name for report heading fields.
** parameters: strValue = string containing field aliases
** Created By: Aida Dodo,Nov 26 2004
** Modified By: 
*/
function csy_getfieldAliasName2(strValue, fieldName) {
	var str = fieldName;
	var tempVal = strValue.split(",");
		if (tempVal.length > 0) {
			for (var i=0;i<tempVal.length;i++) 	{			
				var tempPair = tempVal[i].split(":");
				//if (Trim(tempPair[0]) == fieldName) {
				//LM May 25
				if(MyEqualsIngoreCase(Trim(tempPair[0]).toUpperCase(), fieldName.toUpperCase())){
					str = tempPair[1];
					i = tempVal.length;
				}//end if
			}//end for
		}//end if
		return str;
}

//***************************************************************************
//LM May 25
function MyEqualsIngoreCase(value1, value2)
{
	if(value1 == value2)
		return true;
	else
		return false;

}

/****************************************************************************
** This function returns the field type, from the shape file.
** used in Custom Query. Returns 'N' for number, 'F' for float or 'C' for string.
** Created By: Aida Dodo, August 24 2004
** Modified By: Aida Dodo, Mar 14, 2005:
**	- replaced LayerFieldType array, with LayerFieldTypeList array, for MS GeoPortal4.0.
**	to work for all layers with <layerConnection> defined in layerList.xml
*/
function csy_getfieldType(layerIndex, fieldName) {
var str = "";
//var tempVal = LayerFieldType[layerIndex].split("|");
var tempVal = LayerFieldTypeList[layerIndex].split(",");
	if (tempVal.length > 0) {
		for (var i=0;i<tempVal.length;i++) 	{			
			var tempPair = tempVal[i].split(":");
			//AD Jan 3, 2005, made case sensitive comparison
			//if (Trim(tempPair[0]) == fieldName) {
			if (Trim(tempPair[0].toUpperCase()) == fieldName.toUpperCase()) {
				str = tempPair[1];
				i = tempVal.length;
			}//end if
		}//end for
	}//end if
	return str;
}
//***************************************************************************

/****************************************************************************
** This function return the field visibility, from a string value in selFieldsVisible XML tag.
** Returns '1' for visible, '0' otherwise.
** Created By: Aida Dodo, May 2007
** Modified By: 
*/
function csy_getfieldVisible(layerIndex, fieldName) 
{//strValue is comma separated
	var str = "1";//set as visible by default
	strValue = LayerFieldVisibleList[layerIndex];

	if (strValue.length>0)
	{
		var tempVal = strValue.split(",");
		if (tempVal.length>0) {
			for (var i=0;i<tempVal.length;i++)
			{			
				var tempPair = tempVal[i].split(":");
				if (Trim(tempPair[0].toUpperCase()) == fieldName.toUpperCase()) 
				{
					str = tempPair[1];
					i = tempVal.length;
				}//end if
			}//end for
		}//end if
	}
	return str;
}
//***************************************************************************

/****************************************************************************
** This function return the field visibility, from a string value in joinselFieldsVisible XML tag.
** Returns '1' for visible, '0' otherwise.
** Created By: Aida Dodo, May 2007
** Modified By: 
*/
function csy_getJfieldVisible(layerIndex, fieldName) 
{//strValue is comma separated
	var str = "1";//set as visible by default
	strValue = LayerJFieldVisibleList[layerIndex];
	if (strValue.length>0)
	{
		var tempVal = strValue.split(",");
		if (tempVal.length>0) {
			for (var i=0;i<tempVal.length;i++)
			{			
				var tempPair = tempVal[i].split(":");
				if (Trim(tempPair[0].toUpperCase()) == fieldName.toUpperCase()) 
				{
					str = tempPair[1];
					i = tempVal.length;
				}//end if
			}//end for
		}//end if
	}
	return str;
}
//***************************************************************************

/****************************************************************************
** This function returns the Join field type, from the shape file.
** used in Custom Query. Returns 'N' for number, 'F' for float or 'C' for string.
** Created By: Aida Dodo, August 3 2005
** Parameters:	- layerIndex: layer index
** 		- joinIndex: join order number layer list, starting from 0.
** Modified By:
*/
function csy_getJfieldType(layerIndex, joinIndex, fieldName) {
var str = "";
var tempVal0 = LayerJFieldTypeList[layerIndex].split("~");
var tempVal = tempVal0[joinIndex].split(",");
	if (tempVal.length > 0) 
	{
		for (var i=0;i<tempVal.length;i++) 	
		{			
			var tempPair = tempVal[i].split(":");
			if (Trim(tempPair[0].toUpperCase()) == fieldName.toUpperCase()) 
			{
				str = tempPair[1];
				i = tempVal.length;
			}//end if
		}//end for
	}//end if
	return str;
}
//***************************************************************************

/****************************************************************************
** This function returns the list of field names, from the layerList.xml file.
** used in Custom Query, if notation "#all#" is used in fieldList. 
** Created By: Aida Dodo, August 23 2004
** Modified By: Aida Dodo, Mar 14, 2005:
**	- replaced LayerFieldType array, with LayerFieldTypeList array, for MS GeoPortal4.0.
**	to work for all layers with <layerConnection> defined in layerList.xml
*/
//function csy_getallFields(LayerIndex) 
function csy_getallFields(layerIndex) //AD Aug 2, 2005 - corrected parameter case, to match code
{
var str = "";
//var tempVal = LayerFieldType[LayerIndex].split("|");
var tempVal = LayerFieldTypeList[layerIndex].split(",");
	if (tempVal.length > 0) {
		for (var i=0;i<tempVal.length;i++) {			
			var tempPair = tempVal[i].split(":");
			str = str + Trim(tempPair[0]) + ",";
		}//end for
		str = str.substr(0,(str.length)-1);
	}//end if
	return str;
}
//***************************************************************************

/****************************************************************************
** This function inverts the case of the first letter of a string, and return the new string.
** for example invCaseFirstLetter(0,'abc') returns 'Abc', and invCaseFirstLetter(0,'Abc') returns 'abc'.
** Created By: Aida Dodo, August 25 2004
** Modified By: 
*/
function invCaseFirstLetter(idx,str)
{
var chr = str.charAt(idx);
	if ( str.charCodeAt(idx) >=97 && str.charCodeAt(idx) <=122 ) //currently lower case
		{tmp = chr.toUpperCase() + str.substr(1,str.length-1);}
	else if ( str.charCodeAt(idx) >=65 && str.charCodeAt(idx) <=90 ) //currently upper case
		{tmp = chr.toLowerCase() + str.substr(1,str.length-1);}	
	else
		{tmp = str;}
	return tmp;
}
//***************************************************************************

/****************************************************************************
** This function return a string that is used by mapserver expression or filter 
** to test for case sensitive expressions in  mapscript.
** Used in Custom Query, Quick Find Owner.
** for example getStringExpression('aBc') returns: "(a|A)(B|b)(c|C)".
** Created By: Aida Dodo, Sept 16 2004
** Modified By: 
*/
function getStringExpression(str)
{
	var chr = "";
	var invCasechr = "";
	var res = "";
	
	for (var i=0;i<str.length;i++)
	{	chr = str.charAt(i);
		invCasechr = invCaseFirstLetter(0,chr);
		res = res + "(" + chr + "|" + invCasechr + ")";
		
	}
	//alert(res);
	return res;
}
//***************************************************************************





function showHyperlink(theLinkURL)
{
	var linkString = "width=" + hyperlinkWindowWidth + ",height=" + hyperlinkWindowHeight + ",scrollbars=yes,resizable=yes,menubar";//,location=yes
	hyperWnd = open(theLinkURL,"HyperLinkWindow",linkString);
}

//***************************************************************************
//* 	getHyperlink function
//* 	used to open the hyperlink window
//*
//*	Created by Aida Dodo May 27, 2005, Cuesta Systems, 2005
//*	Copyright Cuesta Systems, Ontario, Canada, 2005
//** Parameters: 
//**	- callType: "QUERY" or "REPORT".
//**	- layer: layer ID in  LayerID array.
//**	- field: layer field name.
//**	- id: field value.
//**	- fieldOrigin: "SPATIAL" or "JOIN".
//**	- joinIndex: join Index, used if fieldOrigin="JOIN".
//***************************************************************************
function getHyperlink(callType,layer,field,id,fieldOrigin,joinIndex)
{
	var theLinkURL="";
	if (id.length>0)
	{
		var lyrIndex=-1;
		for (k=0;k<LayerID.length;k++)
		{
			if (callType=="QUERY") 
			{
				if (LayerID[k]==layer)
				{
					lyrIndex=k;
					k=LayerID.length;
					var res=csy_getfieldHyperData(lyrIndex,field,fieldOrigin,joinIndex);
					if (Trim(res[0]).length>0)
						theLinkURL = res[0] + id + res[1];
					//alert(theLinkURL);
				}
			} else {
				var res=csy_getfieldHyperDataREPORT(field,fieldOrigin,joinIndex);
				if (Trim(res[0]).length>0)
					theLinkURL = res[0] + id + res[1];
					//alert(field+"--"+theLinkURL);			
			}
		}	
	}
	//else 
		//alert("This feature does not have hyperlinked data");
		
	return theLinkURL;
}

//***************************************************************************

/****************************************************************************
** This function return the field Hyper Link Prefix/Suffix, from the LayerHyperLinkPrefix/Suffix arrays.
** used to show layer HyperLink in QUERY attributes window. Returns complete link as a string.
** Created By: Aida Dodo, May 27, 2005.
** Parameters: 
**	- layerIndex: layer index i  LayerID array.
**	- fieldName: layer field name.
**	- fieldOrigin: "SPATIAL" or "JOIN" source from XML definition.
** Modified: AD June 7, 2005
**	- replaced LayerJHyperLinkPrefix,LayerJHyperLinkSuffix with new arrays: LayerJHyperLinkPrefix_FINAL,LayerJHyperLinkSuffix_FINAL.
*/
function csy_getfieldHyperData(layerIndex, fieldName, fieldOrigin,joinIndex) {
	var resArray = new Array();
	var tempValP = new Array();
	var tempValS = new Array();
	var str = "";
	if (fieldOrigin=="JOIN") {
		if (joinIndex==-1) joinIndex=getJoinIndex(layerIndex, fieldName);
		if (joinIndex!=-1)
		{
			if (LayerJHyperLinkPrefix_FINAL[layerIndex].length>0)
				{var tempValP_tmp = LayerJHyperLinkPrefix_FINAL[layerIndex].split("~");
				tempValP = tempValP_tmp[joinIndex].split(",");}
			if (LayerJHyperLinkSuffix_FINAL[layerIndex].length>0)
				{var tempValS_tmp = LayerJHyperLinkSuffix_FINAL[layerIndex].split("~");
				tempValS = tempValS_tmp[joinIndex].split(",");}
			if (tempValP.length > 0) {
				for (var i=0;i<tempValP.length;i++) {			
					var tempPair = tempValP[i].split("|");
					if (Trim(tempPair[0].toUpperCase())==fieldName.toUpperCase()) {
						str = tempPair[1];
						i = tempValP.length;
					}//end if
				}//end for
			}//end if
		}
		resArray[0]=str;
		str = "";
		if (tempValS.length > 0) {
			for (var i=0;i<tempValS.length;i++) {			
				var tempPair = tempValS[i].split("|");
				if (Trim(tempPair[0].toUpperCase())==fieldName.toUpperCase()) {
					str = tempPair[1];
					i = tempValS.length;
				}//end if
			}//end for
		}//end if
		resArray[1]=str;
	}
	else {//spatial fields
		if (LayerHyperLinkPrefix[layerIndex].length>0)
			tempValP = LayerHyperLinkPrefix[layerIndex].split(",");
		if (LayerHyperLinkSuffix[layerIndex].length>0)
			tempValS = LayerHyperLinkSuffix[layerIndex].split(",");
		if (tempValP.length > 0) {
			for (var i=0;i<tempValP.length;i++) {			
				var tempPair = tempValP[i].split("|");
				if (Trim(tempPair[0].toUpperCase())==fieldName.toUpperCase()) {
					str = tempPair[1];
					i = tempValP.length;
				}//end if
			}//end for
		}//end if
		resArray[0]=str;
		str = "";
		if (tempValS.length > 0) {
			for (var i=0;i<tempValS.length;i++) {			
				var tempPair = tempValS[i].split("|");
				if (Trim(tempPair[0].toUpperCase())==fieldName.toUpperCase()) {
					str = tempPair[1];
					i = tempValS.length;
				}//end if
			}//end for
		}//end if
		resArray[1]=str;
	}
	return resArray;
}
//***************************************************************************

//***************************************************************************
//* 	getJoinIndex function
//* 	used to open the hyperlink window, for fields in default tab in QUERY attributes window.
//*	Returns join index from xml, for layer, for a given join field name (returns first occurence in field name found).
//*
//*	Created by Aida Dodo May 30, 2005, Cuesta Systems, 2005
//*	Copyright Cuesta Systems, Ontario, Canada, 2005
//** Parameters:
//**	- layerIndex: layer index i  LayerID array.
//**	- fieldName: layer field name.
//** Modified: AD June 7, 2005
//**	- replaced LayerJFieldList with new arrays: LayerJFieldList_FINAL.
//***************************************************************************
function getJoinIndex(layerIndex, fieldName)
{
	var idx=-1;
	if (LayerJNameList[layerIndex].length>0){

		var tmp1 = LayerJTabList[layerIndex].split("~");
		for (var k=0;k<tmp1.length;k++)
		{
			if (Trim(tmp1[k]).length==0)
			{//default tab
				var tmp = LayerJFieldList_FINAL[layerIndex].split("~");
				var tempVal = tmp[k].split(",");
				if (tempVal.length > 0) {
					for (var i=0;i<tempVal.length;i++) {
						if (Trim(tempVal[i])==fieldName) {
							idx=k;
							i = tempVal.length;
							k = tmp1.length;
						}//end if
					}//end for
				}//end if			
			} 
		}
	}
	return idx;
}

/****************************************************************************
** This function return the field Hyper Link Prefix/Suffix, from the LayerHyperLinkPrefix/Suffix arrays.
** used to show layer HyperLink in REPORT attributes window. Returns complete link as a string.
** Created By: Aida Dodo, May 31, 2005.
** Parameters: 
**	- fieldName: layer field name.
**	- fieldOrigin: "SPATIAL" or "JOIN" source from XML definition.
** Modified: AD June 7, 2005
**	- replaced stdnReportJHyperLinkPrefix,stdnReportJHyperLinkSuffix with new arrays: stdnReportJHyperLinkPrefix_FINAL,stdnReportJHyperLinkSuffix_FINAL.
*/
function csy_getfieldHyperDataREPORT(fieldName, fieldOrigin,joinIndex) {
	var resArray = new Array();
	var tempValP = new Array();
	var tempValS = new Array();
	var str = "";
	if (fieldOrigin=="JOIN") {
		if (joinIndex==-1) joinIndex=getJoinIndexREPORT(fieldName);
		if (joinIndex!=-1)
		{
			if (stdnReportJHyperLinkPrefix_FINAL.length>0)
				{var tempValP_tmp = stdnReportJHyperLinkPrefix_FINAL.split("~");
				tempValP = tempValP_tmp[joinIndex].split(",");}
			if (stdnReportJHyperLinkSuffix_FINAL.length>0)
				{var tempValS_tmp = stdnReportJHyperLinkSuffix_FINAL.split("~");
				tempValS = tempValS_tmp[joinIndex].split(",");}
			if (tempValP.length > 0) {
				for (var i=0;i<tempValP.length;i++) {			
					var tempPair = tempValP[i].split("|");
					if (Trim(tempPair[0].toUpperCase())==fieldName.toUpperCase()) {
						str = tempPair[1];
						i = tempValP.length;
					}//end if
				}//end for
			}//end if
		}
		resArray[0]=str;
		str = "";
		if (tempValS.length > 0) {
			for (var i=0;i<tempValS.length;i++) {			
				var tempPair = tempValS[i].split("|");
				if (Trim(tempPair[0].toUpperCase())==fieldName.toUpperCase()) {
					str = tempPair[1];
					i = tempValS.length;
				}//end if
			}//end for
		}//end if
		resArray[1]=str;
	}
	else {//spatial fields
		if (stdnReportHyperLinkPrefix.length>0)
			tempValP = stdnReportHyperLinkPrefix.split(",");
		if (stdnReportHyperLinkSuffix.length>0)
			tempValS = stdnReportHyperLinkSuffix.split(",");
		if (tempValP.length > 0) {
			for (var i=0;i<tempValP.length;i++) {			
				var tempPair = tempValP[i].split("|");
				if (Trim(tempPair[0].toUpperCase())==fieldName.toUpperCase()) {
					str = tempPair[1];
					i = tempValP.length;
				}//end if
			}//end for
		}//end if
		resArray[0]=str;
		str = "";
		if (tempValS.length > 0) {
			for (var i=0;i<tempValS.length;i++) {			
				var tempPair = tempValS[i].split("|");
				if (Trim(tempPair[0].toUpperCase())==fieldName.toUpperCase()) {
					str = tempPair[1];
					i = tempValS.length;
				}//end if
			}//end for
		}//end if
		resArray[1]=str;
	}
	return resArray;
}
//***************************************************************************

//***************************************************************************
//* 	getJoinIndex function
//* 	used to open the hyperlink window, for fields in default tab in REPORT attributes window.
//*	Returns join index from xml, for layer, for a given join field name (returns first occurence in field name found).
//*
//*	Created by Aida Dodo May 31, 2005, Cuesta Systems, 2005
//*	Copyright Cuesta Systems, Ontario, Canada, 2005
//** Parameters:
//**	- fieldName: layer field name.
//** Modified: AD June 8, 2005
//**	- replaced LayerJFieldList with new arrays: stdnReportJFieldList_FINAL.
//***************************************************************************
function getJoinIndexREPORT(fieldName)
{
	var idx=-1;
	if (stdnReportJNameList.length>0){

		var tmp1 = stdnReportJSectionList.split("~");
		for (var k=0;k<tmp1.length;k++)
		{
			if (Trim(tmp1[k]).length==0)
			{//default tab
				var tmp = stdnReportJFieldList_FINAL.split("~");
				var tempVal = tmp[k].split(",");
				if (tempVal.length > 0) {
					for (var i=0;i<tempVal.length;i++) {
						if (Trim(tempVal[i])==fieldName) {
							idx=k;
							i = tempVal.length;
							k = tmp1.length;
						}//end if
					}//end for
				}//end if			
			} 
		}
	}
	return idx;
}

/****************************************************************************
** This function used to populate all option fields in an OPTION SELECT object
** Parameters: constraintType="ALL": for all SPATIAL fields only,
** 		or constraintType="SPATIALFIELDS": for spatial fields only,
** 		or constraintType="ALLFIELDS": for all spatial and all fields.
** Used for Query, to get Join fields as well.
** Created By: Aida Dodo, Aug 2 2005
** Modified By: 
*/
function csy_setAllFieldsOptionsStr_Query(constraintType)
{	
	var res = csy_setAllFieldsOptionsStr("ALL_VISIBLE");
	if (constraintType=="SPATIALFIELDS")
	{
		return res;
	}
	else if (constraintType=="ALLFIELDS")
	{//add all join fields to spatial fields
		//get layer index in LayerName array
		var layerIdx = -1;
		for (var k=0;k<LayerID.length;k++) 
		{
			if (LayerID[k]==ActiveLayerID) 
			{
				layerIdx = k; 
				k=LayerID.length;
			}
		}
		//if (layerIdx>-1 && LayerJFieldList[layerIdx].length>0)
		{
			//get all join field list
			var fieldList0 = LayerJFieldList[layerIdx].split("~");
			for (var j=0;j<fieldList0.length;j++)
			{//for each join
				var fieldList = fieldList0[j];
				if (fieldList != "") 
				{
				  var item = fieldList.split(",");
				  for (var i=0;i<item.length;i++) 
				  {
					var itemJAlias = csy_getJfieldAliasName(layerIdx,j,item[i]);
					var itemJName0 = LayerJNameList[layerIdx].split("~");
					var itemJName = itemJName0[j];
					var itemJFieldType = csy_getJfieldType(layerIdx,j,item[i]);
					res += "<option value='JFIELD|"+itemJName+"|"+itemJFieldType+"|"+ item[i] + "'>" + itemJAlias;
				  }//for
				} //end fieldList
								
			}		
		}//end layerIdx
	}
	return res;
}