/*--------------------------------------------------------------------
' File:		MapClient.js
' Purpose:	Handle Map specific client side issues
' Updated:	030901
' Version:	040502
' Edited:	TNY
' Status:	Release
' Copyright © Centroid Software Ab Oy 2000-2003, Finland
'				All rights reserved. 
'--------------------------------------------------------------------*/

var sActiveThemes;
var lMinEasting;
var lMaxEasting;
var lMinNorthing;
var lMaxNorthing;
var dResolution;
var lWidth;
var lHeight;
var sMapMode;
var nThemeGroups;
var bw=new BrowserCheck();

/*--------------------------------------------------------------------
*  Function:RedirectList
*  Purpose:	General redirection function for GeoMedia sites
*-------------------------------------------------------------------*/
function RedirectList(sParm) {
	var sUrl;
	sUrl = "../sa/list.asp?"+sParm;
	parent.parent.frames[2].location.href=sUrl;
}

/*--------------------------------------------------------------------
*  Function:RedirectPopup
*  Purpose:	General redirection function for GeoMedia sites
*-------------------------------------------------------------------*/	
function RedirectPopup(sParm) {
	// do nothing, present for future use
}
	
/*--------------------------------------------------------------------
*  Function:RedirectListData
*  Purpose:	General redirection function for GeoMedia sites
*-------------------------------------------------------------------*/	
function RedirectListData(sParm) {
	var sUrl;
	sUrl = sParm;
	parent.parent.frames[2].location.href=sUrl;
}
	
/*--------------------------------------------------------------------
*  Function:compassOn
*  Purpose:	set the Compass to On Mode
*-------------------------------------------------------------------*/	
function compassOn(sLayout,e)
{
	var imgSrc;

	if(ns6){	
		imgSrc = "../_Layouts/"+sLayout+"/Images/compass/"+e.currentTarget.id+"On.gif"		
		e.currentTarget.src = imgSrc;
	}
	else{
		imgSrc = "../_Layouts/"+sLayout+"/Images/compass/"+window.event.srcElement.id+"On.gif"
		window.event.srcElement.src = imgSrc;
	}
}
	
/*--------------------------------------------------------------------
*  Function:compassOff
*  Purpose:	set the Compass to Off Mode
*-------------------------------------------------------------------*/	
function compassOff(sLayout,e)
{
	var imgSrc;
	
	if(ns6){
		imgSrc = "../_Layouts/"+sLayout+"/Images/compass/"+e.currentTarget.id+".gif"
		e.currentTarget.src = imgSrc;
	}
	else{
		imgSrc = "../_Layouts/"+sLayout+"/Images/compass/"+window.event.srcElement.id+".gif"
		window.event.srcElement.src = imgSrc;
	}
}
	
/*--------------------------------------------------------------------
*  Function:toolOn
*  Purpose:	set the Tool to On Mode
*-------------------------------------------------------------------*/	
function toolOn(sLayout,e)
{
	var imgSrc;
	
	if(ns6){
		imgSrc = "../_Layouts/"+sLayout+"/Images/tools/"+e.currentTarget.id+"On.gif"
		e.currentTarget.src = imgSrc;
	}
	else{
		imgSrc = "../_Layouts/"+sLayout+"/Images/tools/"+window.event.srcElement.id+"On.gif"
		window.event.srcElement.src = imgSrc;
	}
}

/*--------------------------------------------------------------------
*  Function:toolOff
*  Purpose:	set the Tool to Off Mode
*-------------------------------------------------------------------*/	
function toolOff(sLayout,e)
{
	var imgSrc;
	
	
	if(ns6){
		imgSrc = "../_Layouts/"+sLayout+"/Images/tools/"+e.currentTarget.id+".gif"
		e.currentTarget.src = imgSrc;
	}
	else{
		imgSrc = "../_Layouts/"+sLayout+"/Images/tools/"+window.event.srcElement.id+".gif"
		window.event.srcElement.src = imgSrc;
	}
}
	
/*--------------------------------------------------------------------
*  Function:AlligatorWindow
*  Purpose:	Opens a new window in a given mode
*-------------------------------------------------------------------*/
function AlligatorWindow(sPage,lMapId,lMCE,lMCN,dRes,sCoordSys,sTheme,sObjLabel,nPrintMode)
{
	var href;
	var setParameters;
	var myWin;
	var width;
	var height;
	var trgName;
	
	width = 600;
	height = 700;

	// handle the open parameters
	if (nPrintMode == 0)
		setParameters = "scrollbars=1,status=1,resizable=1,toolbar=0,top=50,left=5,width=" +width+ ",height=" +height;
	else
		setParameters = "scrollbars=0,status=1,resizable=1,toolbar=1,top=50,left=5,width=" +width+ ",height=" +height;
	
	trgName = "win"+lMCE+lMCN+lMapId;

	//sTheme = "";
	if (nPrintMode == 2)
		href = sPage + "?MCEasting="+lMCE+"&MCNorthing="+lMCN+"&MapId="+lMapId+"&Theme="+sTheme+"&CoordSys="+sCoordSys+"&Resolution="+dRes+"&ObjectLabel="+sObjLabel+"&measure=1";
	else
	 	href = sPage + "?MCEasting="+lMCE+"&MCNorthing="+lMCN+"&MapId="+lMapId+"&Theme="+sTheme+"&CoordSys="+sCoordSys+"&Resolution="+dRes+"&ObjectLabel="+sObjLabel+"&measure=0";
	
    myWin = window.open(href, trgName, setParameters);		
	myWin.focus(true);
}
/*--------------------------------------------------------------------
*  Function: ListWindow
*  Purpose:	Opens a new page in the list window
*-------------------------------------------------------------------*/
function ListWindow(sPage,lMapId,lMCE,lMCN,dRes,sCoordSys,sIndexGroup,sTheme,sObjLabel,nPrintMode)
{
	var href;
	var myWin;
	var target;
	
	if(sTheme==null)sTheme="";
	
	
	href = sPage + "?MCEasting="+lMCE+"&MCNorthing="+lMCN+"&MapId="+lMapId+"&Theme="+sTheme+"&CoordSys="+sCoordSys+"&Resolution="+dRes/1000+"&ObjectLabel="+sObjLabel+"&Group="+sIndexGroup
	
	target = window.parent.frames[2];
	target.location.href = href;
}
/*--------------------------------------------------------------------
*  Function:getThemes
*  Purpose:	Handle the theme request
*-------------------------------------------------------------------*/
function getThemes(theForm, sBaseURL, sObjectName, ThemeCount)
{
	var i;
	var ctrl;
	var sName;
	var sThemes = "";
	var sThemesStatus = "";
	var sURL;
	var sDiv;
	var sValue;
	var reg;
	var tstr="";
	var sTCookie;
	
	nThemeGroups=ThemeCount;
	if (sBaseURL == "POWERMAP")
		sMapMode = "POWERMAP"
	else
		sMapMode = "BASIC"
	
	for (i=0;i<theForm.elements.length;i++)
	{
		ctrl = theForm.elements[i];
		sName = ctrl.id;
		if (sName != "")
		{
			if(ctrl.value==""){
			//opera6 trick...
					sValue=ctrl.id;
					sValue=parseInt(sValue.substring(3,sValue.length)); 
			}
			else{
				sValue=ctrl.value;
			}
			sThemes = sThemes + sValue + ",";
			// construct the theme tag
			if (ctrl.checked){
				sThemesStatus = sThemesStatus + sValue + ","; 
			}
			else
			{
				// there might be several themes present in the value (grouping)
				// so we have to do a special thing
				reg = /,/g;
				sValue = "-" + sValue;
				sValue = sValue.replace(reg,",-");
				sThemesStatus = sThemesStatus + sValue + ","; 
			}
		}
	}
	
	//update cookie values
	sTCookie=readCookie(siteId+'themeMem');
	if(sTCookie==null)sTCookie="";
	var arr=sTCookie.split(',');
	for(i=0;i<arr.length;i++){
		if((arr[i]!="0" && arr[i].charAt(0)=="-" && sThemesStatus.search(arr[i].substring(1,arr[i].length))==-1) || (arr[i]!="0" && arr[i].charAt(0)!="-" && sThemesStatus.search(arr[i])==-1))
			sThemesStatus+=arr[i]+",";
	}
	tstr = sThemesStatus + "0";
	saveCookie(siteId+'themeMem',tstr,1);	
	saveCookie(siteId+'newMap',0,1);
	
	// set the theme tags and URL
	sThemes = "[" + sThemes + "0]";
	sThemesStatus = sThemesStatus+"0";	
	saveCookie(siteId+"actTHM",sThemesStatus,1);
	sURL = "setThemes.asp?Themes=" + sThemes + "&ThemeStatus=" + sThemesStatus + "&LoadedThemes=" + sActiveThemes + "&MinE=" + lMinEasting + "&MaxE=" + lMaxEasting + "&MinN=" + lMinNorthing + "&MaxN=" + lMaxNorthing + "&Width=" + lWidth + "&Height=" + lHeight + "&Resolution=" + dResolution + "&MapMode=" + sMapMode;
	parent.parent.frames[4].location.href=sURL;
	
	// hide the menu
	if(bw.ns6)
		sDiv = document.getElementById('popThemes');
	else
		sDiv = document.all['popThemes'];
	sDiv.style.visibility = "hidden";
}

/*--------------------------------------------------------------------
*  Function:activateTheme
*  Purpose:	Activates loaded items
*-------------------------------------------------------------------*/
function activateTheme(lThemeId)
{
	var gObjMapView;
	var sName,i;
	var tstr=readCookie(siteId+'themeMem');
	var str="";
	var arr=tstr.split(',');
	
	//if we come from themeForm we update theme-cookie
	if(readCookie(siteId+'newMap')==0){
		
		for(i=0;i<arr.length;i++){
			if(arr[i]=="-"+lThemeId){
				arr[i]=lThemeId;
			}
		}
		for(i=0;i<arr.length-1;i++){
			str=str+arr[i]+","; 
		}
		str=str+"0";
		saveCookie(siteId+'themeMem',str,1);
	}
	else{
		//else we come from some of the maps
		for(i=0;i<arr.length;i++){
			if(arr[i]=="-"+lThemeId){
				deactivateTheme(lThemeId);			
				return;
			}
		}
	}
	
	// go on
	sName = "THM" + lThemeId;
	
	if (sMapMode == "POWERMAP")
	{
		// PowerMap Mode
		gObjMapView = document.applets["JMapViewBigMap"];
		gObjMapView.setLayerEnabled(sName, true);
		gObjMapView.setLayerEnabled(sName+"_HS", true);
		gObjMapView.setLayerVisible( sName, true );
		gObjMapView.setLayerVisible( sName+"_HS", true );  
	}
	else
	{
		// BASIC MODE
		for(i=0;i<maxItems;i++){
			if(document.getElementById(sName+"_"+i)!=null) {
				document.getElementById(sName+"_"+i).style.visibility="visible";
			}
		}
	}	
}

/*--------------------------------------------------------------------
*  Function:deactivateTheme
*  Purpose:	Deactivates loaded items
*-------------------------------------------------------------------*/
function deactivateTheme(lThemeId)
{
	var gObjMapView;
	var sName, i;
	var tstr=readCookie(siteId+'themeMem');
	var str="";
	var arr=tstr.split(',');
	
	
	
	//if we come from themeForm we update theme-cookie
	if(readCookie(siteId+'newMap')==0){	 
		for(i=0;i<nThemeGroups;i++){
			if(arr[i]==lThemeId){
				arr[i]="-"+arr[i];
			}
		}
		for(i=0;i<arr.length-1;i++){
			str=str+arr[i]+","; 
		}
		str=str+"0";
		saveCookie(siteId+'themeMem',str,1);
	}
	
	// go on
	sName = "THM" + lThemeId;
	
	if (sMapMode == "POWERMAP")
	{
		// PowerMap Mode
		gObjMapView = document.applets["JMapViewBigMap"];
		gObjMapView.setLayerVisible( sName, false );
		gObjMapView.setLayerVisible( sName+"_HS", false );  
		gObjMapView.setLayerEnabled(sName, false);
		gObjMapView.setLayerEnabled(sName+"_HS", false);
	}
	else
	{
		// BASIC MODE
		for(i=0;i<maxItems;i++){
			if(document.getElementById(sName+"_"+i)!=null){
				document.getElementById(sName+"_"+i).style.visibility="hidden";
			}
		}
	}
}

/*--------------------------------------------------------------------
*  Function:setActiveThemes
*  Purpose:	Sets active themes
*-------------------------------------------------------------------*/
function setActiveThemes(sThemes)
{
	sActiveThemes = sThemes;
}

/*--------------------------------------------------------------------
*  Function:createThemeItem
*  Purpose:	Creates themeitems on-the-fly
*-------------------------------------------------------------------*/
function createThemeItem(lThemeId, j, sLabel, sURL, sTarget, sIcon, nIconWidth, nIconHeight, nWindowWidth, nWindowHeight, nPopupMode, bShowLabel, nLeft, nTop, nWidth, nHeight, nPixE, nPixN, sMapMode, sGroup)
{	
	var content = "";
	var sImg;
	var nIdLeft;
	var nIdTop;
	var sIdName;
	var nZ = j;
	var nMaxLength = m_nStrMaxlength;
	var sPopupLabel = sLabel;
	var vis='hidden';
	var i;
	var bFlag=0;
	var sTid;
	var sGid;
	
	//Is this theme visible with this map
	for(i=0;i<document.forms['themeForm'].elements.length;i++){
		sTid="THM"+lThemeId;
		sGid="THM"+sGroup;
		if(document.forms['themeForm'].elements[i].id==sTid || document.forms['themeForm'].elements[i].id==sGid)
			bFlag=1;
	}
	if(bFlag==0)return;
	
	this.sMapMode=sMapMode;

	if(sCg=="NO"){
		sCg=0;
	}
	else{
		sCg=1;
	}
	
	sIdName = "THM"+lThemeId+"_"+j;
	
	nIdLeft = nLeft+nPixE-(nIconWidth/2);
	nIdTop = nTop+nPixN-(nIconHeight/2);	
	
	// add 3 chars to accomodate for the ellipsis
	if(sPopupLabel.length > nMaxLength){
		sPopupLabel= sPopupLabel.substring(0,nMaxLength-4);
		sPopupLabel = sPopupLabel + "...";
	}

	if(sMapMode == "PRINT"){
		sURL = "";
	}

	if(nPixE > 0 && nPixE < nWidth && nPixN > 0 && nPixN < nHeight){
		
		if(multiPopMode == "YES"){
			content+="<img src='" + sIcon + "' border=0 width=" + nIconWidth + " height=" + nIconHeight;
		}	
		else{
			content+="<img src='" + sIcon + "' border=0 width=" + nIconWidth + " height=" + nIconHeight + " alt='" + sLabel + "'";
		}
			
		if(sMapMode == "PRINT"){
			content+=" class='themeItem'";
		}
		
		else{		
			if(sURL != ""){
				if(multiPopMode == "YES"){
					content+=" class='themeItem' onclick=clicked=1;javascript:OpenNewWindow('" + sURL + "'," + nWindowWidth + "," + nWindowHeight + ",'" + sTarget + "'," + nPopupMode + ");SetLocation(" + nIdLeft + "," + nIdTop + ");";
					content+=" onmouseover=multiblinkOnImg(" + lThemeId + "," + j + "," + nThemeGroups + ",'" + sTarget + "'," + nMaxLength + "," + sCg + ",'" + siteLayout + "','" + waitText + "'); onmouseout=multiblinkOffImg('" + sTarget + "');";
				}
				else{
					content+=" class='themeItem' onclick=javascript:OpenNewWindow('" + sURL + "'," + nWindowWidth + "," + nWindowHeight + ",'" + sTarget + "'," + nPopupMode + ");SetLocation(" + nIdLeft + "," + nIdTop + ");";
				}
			}
			else{
				if(multiPopMode == "YES"){
					content+=" class='themeItem' onmouseover=multiblinkOnImg(" + lThemeId + "," + j + "," + nThemeGroups + ",'" + sTarget + "'," + nMaxLength + "," + sCg + ",'" + siteLayout + "','" + waitText + "'); onmouseout=multiblinkOffImg('" + sTarget + "');";
				}
				else{
					content+= " class='themeItem'";
				}
			}
		}
		content+= ">";				
		
		createSuperDiv(sIdName,nIdLeft,nIdTop,98,nIconWidth,nIconHeight,vis,sIcon,sURL,sTarget,nMaxLength,sPopupLabel,content);
		
		//handle the label tag
		if(bShowLabel=='TRUE'){
			content= "<table><tr><td class='themeLabel'>" + sLabel + "</td></tr></table>";
			createSuperDiv('label',nIdLeft+20,nIdTop,98,200,200,vis,'','','',nMaxLength,sPopupLabel,content);	
		}
	}
}

/*--------------------------------------------------------------------
*  Function: createSuperDiv
*  Purpose:	Creates on-the-fly divs
*-------------------------------------------------------------------*/
function createSuperDiv(sIdName,left,top,zIndex,width,height,visibility,sIcon,sURL,sTarget,nMaxLength,popuplabel,content){
	var str="";
	var path = "<img src=" + sIcon + " width=12 height=12>";
	var parentlayerobj=document.body;
	
	if(sIdName=='label'){
	
		if (bw.ie5 || bw.ie6) {
			str = '\n<DIV id='+sIdName+' style="position:absolute; left:'+left+'; top:'+top+'; width:'+width;
			str += '; height:'+height;
			str += '; clip:rect(0,'+width+','+height+',0)';
			str += '; z-index:'+zIndex;
			str += '; visibility:'+visibility;
			str += ';">'+((content)?content:'')+'</DIV>';	
			parentlayerobj.insertAdjacentHTML("BeforeEnd",str);
			return (document.all) ? document.all[sIdName] : document.getElementById(sIdName);
		}
		else if(bw.ns6){
			var tempLayer = document.createElement('div');
      		tempLayer.setAttribute('id',sIdName);
      		tempLayer.setAttribute('style','position:absolute; left:'+left+'; top:'+top+'; width:'+width+';visibility:'+visibility+';');
      		tempLayer.innerHTML=content;
      		parentlayerobj.appendChild(tempLayer);
      		return document.getElementById(sIdName);
		}
	}
	else{
	
	if (bw.ie5 || bw.ie6) {
			str += "\n<DIV id='"+sIdName+"' style='position:absolute; left:"+left+"; top:"+top+"; width:"+width;
			str += "; height:"+height;
			str += "; clip:rect(0,"+width+","+height+",0)";
			str += "; z-index:"+zIndex;
			str += "; visibility:"+visibility+";'";
			str += " name='" + sIdName + "'";
			str += " left=" + left + " top=" + top + " src='" + path + "'";
			str += " link='" + sURL + "' label='" + popuplabel + "'";
			str += " target='" + sTarget + "' length=" + nMaxLength; 
			str += ">"+((content)?content:'')+"</DIV>";	
			parentlayerobj.insertAdjacentHTML("BeforeEnd",str);
			return (document.all) ? document.all[sIdName] : document.getElementById(sIdName);
	
			
	}
		else if(bw.ns6){
			var tempLayer = document.createElement('div');
      		tempLayer.setAttribute('id',sIdName);
      		tempLayer.setAttribute('style','position:absolute; left:'+left+'; top:'+top+'; width:'+width+';visibility:'+visibility+';');
      		tempLayer.setAttribute('name',sIdName);
      		tempLayer.setAttribute('id',sIdName);
      		tempLayer.setAttribute('left',left);
      		tempLayer.setAttribute('top',top);
      		tempLayer.setAttribute('label','"'+popuplabel+'"');
      		tempLayer.setAttribute('length',nMaxLength);
      		tempLayer.innerHTML=content;
			parentlayerobj.appendChild(tempLayer);
      		return document.getElementById(sIdName);
		}
	}
}

/*--------------------------------------------------------------------
*  Function: BrowserCheck
*  Purpose:	 BrowserCheck
*-------------------------------------------------------------------*/
function BrowserCheck(){  
	this.ver=navigator.appVersion 
	this.agent=navigator.userAgent 
	this.dom=document.getElementById?1:0 
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie=this.ie4||this.ie5||this.ie6 
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.bw=(this.ie6 || this.ie5 || this.ns6 ) 
	return this 
} 

/*--------------------------------------------------------------------
*  Function: createGeoMediaLayer
*  Purpose:	 Creates GeoMedia layers
*-------------------------------------------------------------------*/
function createGeoMediaLayer(lThemeId, sCGMFile, lPriority)
{
	var gObjMapView;
	var sName;

	if (sMapMode == "POWERMAP")
	{
		sMapMode = "POWERMAP";
		sName = "THM" + lThemeId;
		gObjMapView = document.applets['JMapViewBigMap'];
		gObjMapView.loadFile (sCGMFile, sName, lPriority);
	}
	
}



/*--------------------------------------------------------------------
*  Function: moveLocation
*  Purpose:	 Create a URL that pans without forgetting the active themes
*-------------------------------------------------------------------*/
function moveLocation(sURL)
{
	sURL = sURL + "&Theme=" + readCookie(siteId+'themeMem');
	parent.parent.frames[3].location.href=sURL;
}

/*--------------------------------------------------------------------
*  Function:allThemesOn
*  Purpose:	Selects all checkboxes
*-------------------------------------------------------------------*/
function allThemesOn(theForm)
{
	var i;
	var ctrl;
	
	for (i=0;i<theForm.elements.length;i++)
	{
		ctrl = theForm.elements[i];
		ctrl.checked=true;
	}	
}

/*--------------------------------------------------------------------
*  Function:allThemesOff
*  Purpose:	Deselects all checkboxes
*-------------------------------------------------------------------*/
function allThemesOff(theForm)
{
	var i;
	var ctrl;
	
	for (i=0;i<theForm.elements.length;i++)
	{
		ctrl = theForm.elements[i];
		ctrl.checked=false;
	}	
}