﻿/*
* Script: cmn.js
* Author: UMG
* Descrpition: Common utilities script.
*/

//set the domain name so that it can talk to map viewer
hostDomain = document.domain;
//document.domain = hostDomain.substring(hostDomain.indexOf(".")+1);

// local variables
var pIsIE  = (navigator.appVersion.indexOf("MSIE")>=0);
var pIsNS  = (navigator.appName.indexOf("Netscape")>=0);
var pIsWin = (navigator.userAgent.indexOf("Win")>=0)
var pDomainPfx = ""; //pDomainPfx = "http://www.geographynetwork.com";
var ArcExplorerWin = null;
var initMinx = 67.2314;
var initMiny = 8.0057;
var initMaxx = 94.4215;
var initMaxy = 34.5346;

//the custom id, must in the url for every request call
var origid = "ESRI_World";

function openMarketplace(){
  var oFrm =  document.forms.fSubmit;
  if (oFrm != null) {
    oFrm.action = jsWepAppContext+"/DiscoveryServlet";
    cmnCmd('marketplace');
  }
}

// execute a command
function cmnCmd(sCmd) {
	if ((sCmd != "viewmetadata") && window.opener && !window.opener.closed && window.opener.cmnCmd) {
		var oFrm = window.opener.document.forms.fSubmit;
		if (oFrm != null) {
			oFrm.action = document.forms.fSubmit.action;
		}
		window.opener.cmnCmd(sCmd);
	} else {
	
		if (sCmd.toLowerCase().indexOf("help") == -1) {
			var oFrm = document.forms.fSubmit;
			
			if (oFrm && oFrm.command) {
				self.focus();
				oFrm.command.value = sCmd;
				oFrm.target = "_top";
				//oFrm.method = "post";
				//oFrm.action = jsWepAppContext+"/";
				oFrm.submit();
			}
		} else {
			var s2 = "left=10,top=10,width=770,height=450";
			s2 += ",toolbar=0,location=0,directories=0,status=0,resizable=yes,scrollbars=yes";
			var winHelp = window.open(sCmd,"PTK_Help",s2);
			winHelp.focus();
		}
	}
}

//helper function to call cmnCmd without pop up window
/*function cmnCmd(sCmd) {
 doCmnCmd(sCmd,"_top");
}*/

// execute a command
function doCmnCmd(sCmd,target) {
  if ((sCmd != "viewmetadata") && window.opener && !window.opener.closed && window.opener.cmnCmd) {
    window.opener.cmnCmd(sCmd);
  } else {
    if (sCmd.toLowerCase().indexOf("help") == -1) {
      var oFrm = document.forms.fSubmit;
      if (oFrm && oFrm.command) {
        self.focus();
        oFrm.command.value = sCmd;
        oFrm.target = target;
        oFrm.submit();
      }
    } else {
      var s2 = "left=10,top=10,width=770,height=450";
      s2 += ",toolbar=0,location=0,directories=0,status=0,resizable=yes,scrollbars=yes";
      var winHelp = window.open(sCmd,"PTK_Help",s2);
      winHelp.focus();
    }
  }
}

// get search area for viewer
function cmnGetSearchArea() {
  var winMap;
  var sName = "ArcExplorerWeb";
  var sInfo = "toolbar=no,width=800,height=564";
  if (pIsWin)
    sInfo = "toolbar=no,width=800,height=564,left=5,top=5";
  else if (!pIsWin && pIsNS)
    sInfo = "toolbar=no,width=800,height=564,screenX=5,screenY=5";
  else if (!pIsWin && pIsIE)
    sInfo = "toolbar=no,width=784,height=548,left=5,top=5";
  winMap = window.open('',sName,sInfo);
  if (!winMap.closed && winMap.Map && winMap.Hidden4.setMapExtentStatus) {
    winMap.opener = self;
    //winMap.Map.setMapExtentStatus(true);
	winMap.Hidden4.setMapExtentStatus(true);
  } else {
    winMap.close(); alert("Preglednik karte trenutno nije otvoren.");
  }
}

// view details
function cmnViewDetails(sUuid) {
	var oFrm = document.forms.fSubmit;
	if (oFrm && oFrm.command && oFrm.uuid) {
		oFrm.action = jsWepAppContext+"/DiscoveryServlet";
		oFrm.command.value = "viewdetails";
		oFrm.uuid.value = sUuid;
		oFrm.target = "_top";
		oFrm.submit();
	}
}

// view a map
function cmnViewMap(sServer, sService, sServiceType, isDefault, sUUID, sArgs)
{
    return addToArcExplorerWeb(sServer, sService, sServiceType, isDefault, sUUID, sArgs);

	/*
	return addToArcExplorerWeb(
		'http://vladp2:8181/wfsconnector/com.esri.wfs.Esrimap/SearchMap_wfs?wfslayers=WorldCountries-0',
		null,
		'wfs',
		false,
		sUUID,
		sArgs
	);

	return addToArcExplorerWeb(
	'http://vladp2:8181/wmsconnector/com.esri.wsit.WMSServlet/SearchMap?wmslayers=WorldCountries',
	null,
	'wms',
	false,
	sUUID,
	sArgs
	);
	*/
}

//add the map service to map viewer
function addToArcExplorerWeb(sServer, sService, sServiceType, isDefault, sUUID, sArgs)
{
    // do not change the window name!
    var windowName = "ArcExplorerWeb";
    var ArcExplorerWin = null;
    var sExtent = "";

    while(sServer.indexOf('amp;') > 0) sServer = sServer.replace('amp;', '&');

    var sLayers = '';
	var sLayersName = sServiceType + 'layers='; // name of http request layers attribute

	var iStart = sServer.indexOf(sLayersName);  // start index

    if (iStart > 0)
    {
        var iStop = sServer.indexOf('&', iStart + 1);  // stop index

        if(iStop == -1) {
            sLayers = sServer.substr(iStart + sLayersName.length);
            sServer = sServer.substring(0, iStart);
        }
        else {
            sLayers = sServer.substring(iStart + sLayersName.length, iStop);
            sServer = sServer.substring(0, iStart) + sServer.substr(iStop);
        }
    }

	//alert('Server=' + sServer + '\nService=' + sService + '\nType=' + sServiceType + '\nDefault=' + isDefault + '\nUUID=' + sUUID + '\nArgs=' + sArgs);

    if (pIsWin)
        ArcExplorerWin = window.open('', windowName, "toolbar=no,resizable=yes,width=800,height=564,left=5,top=5");
    else if (!pIsWin && pIsNS)
        ArcExplorerWin = window.open('', windowName, "toolbar=no,resizable=yes,width=800,height=564,screenX=5,screenY=5");
    else if (!pIsWin && pIsIE)
        ArcExplorerWin = window.open('', windowName, "toolbar=no,resizable=yes,width=784,height=548,left=5,top=5");
    else
        ArcExplorerWin = window.open('', windowName, "toolbar=no,resizable=yes,width=800,height=564");

    //---------------------------------------------
	//         If MapViewer window is opened
    // --------------------------------------------

	if(!ArcExplorerWin.closed && ArcExplorerWin.Map)
    {
        ArcExplorerWin.focus();
        ArcExplorerWin.opener = self;

        if(!isDefault)
        {
            if(sServiceType == "feature" || sServiceType == "image")
                ArcExplorerWin.Map.addServiceRemote(sServer, sService, sExtent);
            else if(sServiceType == "wms")
				ArcExplorerWin.Map.addWMSService(sServer, sLayers);
		    else if(sServiceType == "wfs")
				ArcExplorerWin.Map.addWFSService(sServer, sLayers);
		    else if(sServiceType == "wcs")
				ArcExplorerWin.Map.addWCSService(sServer, sLayers);
        }
	} else {

	//---------------------------------------------
	//         If MapViewer window is closed
    // --------------------------------------------

		var theUrl = pDomainPfx + "/arcexplorer/arcexplorer.html?origid=" + origid;

        if(sServiceType == "feature" || sServiceType == "image")
            theUrl += "&server=" + escape(sServer) + "&service=" + sService;
        else if(sServiceType == "wms" || sServiceType == "wfs" || sServiceType == "wcs")
            theUrl += "&" + sServiceType + "server=" + escape(sServer) + "&" + sServiceType + "layers=" + sLayers;

        theUrl += "&usesession=true";

        if (pIsWin)
			ArcExplorerWin = window.open(theUrl, windowName, "toolbar=no,resizable=yes,width=800,height=564,left=5,top=5");
        else if (!pIsWin && pIsNS)
        {
            ArcExplorerWin = window.open("", windowName, "toolbar=no,resizable=yes,width=800,height=564,screenX=5,screenY=5");
            ArcExplorerWin.location = theUrl;
        } else if (!pIsWin && pIsIE)
            ArcExplorerWin = window.open(theUrl, windowName, "toolbar=no,resizable=yes,width=784,height=548,left=5,top=5");
        else
            ArcExplorerWin = window.open(theUrl, windowName, "toolbar=no,resizable=yes,width=800,height=564");

        ArcExplorerWin.focus();
    }
}


// view a map in HTRS coordinate system
function cmnViewMapHTRS(sServer, sService, sServiceType, isDefault, sUUID, sArgs) {
    return addToArcExplorerHTRSWeb(sServer, sService, sServiceType, isDefault, sUUID, sArgs);

    /*
    return addToArcExplorerWeb(
    'http://vladp2:8181/wfsconnector/com.esri.wfs.Esrimap/SearchMap_wfs?wfslayers=WorldCountries-0',
    null,
    'wfs',
    false,
    sUUID,
    sArgs
    );

    return addToArcExplorerWeb(
    'http://vladp2:8181/wmsconnector/com.esri.wsit.WMSServlet/SearchMap?wmslayers=WorldCountries',
    null,
    'wms',
    false,
    sUUID,
    sArgs
    );
    */
}

// open ArcExplorer for viewing maps in HTRS coordinate system
function addToArcExplorerHTRSWeb(sServer, sService, sServiceType, isDefault, sUUID, sArgs) {
    // do not change the window name!
    var windowName = "ArcExplorerWebHTRS";
    var ArcExplorerWin = null;
    var sExtent = "";

    while (sServer.indexOf('amp;') > 0) sServer = sServer.replace('amp;', '&');

    var sLayers = '';
    var sLayersName = sServiceType + 'layers='; // name of http request layers attribute

    var iStart = sServer.indexOf(sLayersName);  // start index

    if (iStart > 0) {
        var iStop = sServer.indexOf('&', iStart + 1);  // stop index

        if (iStop == -1) {
            sLayers = sServer.substr(iStart + sLayersName.length);
            sServer = sServer.substring(0, iStart);
        }
        else {
            sLayers = sServer.substring(iStart + sLayersName.length, iStop);
            sServer = sServer.substring(0, iStart) + sServer.substr(iStop);
        }
    }

    //alert('Server=' + sServer + '\nService=' + sService + '\nType=' + sServiceType + '\nDefault=' + isDefault + '\nUUID=' + sUUID + '\nArgs=' + sArgs);

    if (pIsWin)
        ArcExplorerWin = window.open('', windowName, "toolbar=no,resizable=yes,width=800,height=564,left=5,top=5");
    else if (!pIsWin && pIsNS)
        ArcExplorerWin = window.open('', windowName, "toolbar=no,resizable=yes,width=800,height=564,screenX=5,screenY=5");
    else if (!pIsWin && pIsIE)
        ArcExplorerWin = window.open('', windowName, "toolbar=no,resizable=yes,width=784,height=548,left=5,top=5");
    else
        ArcExplorerWin = window.open('', windowName, "toolbar=no,resizable=yes,width=800,height=564");

    //---------------------------------------------
    //         If MapViewer window is opened
    // --------------------------------------------

    if (!ArcExplorerWin.closed && ArcExplorerWin.Map) {
        ArcExplorerWin.focus();
        ArcExplorerWin.opener = self;

        if (!isDefault) {
            if (sServiceType == "feature" || sServiceType == "image")
                ArcExplorerWin.Map.addServiceRemote(sServer, sService, sExtent);
            else if (sServiceType == "wms")
                ArcExplorerWin.Map.addWMSService(sServer, sLayers);
            else if (sServiceType == "wfs")
                ArcExplorerWin.Map.addWFSService(sServer, sLayers);
            else if (sServiceType == "wcs")
                ArcExplorerWin.Map.addWCSService(sServer, sLayers);
        }
    } else {

        //---------------------------------------------
        //         If MapViewer window is closed
        // --------------------------------------------

        var theUrl = pDomainPfx + "/arcexplorerDOF2011/arcexplorer.html?origid=" + origid;

        if (sServiceType == "feature" || sServiceType == "image")
            theUrl += "&server=" + escape(sServer) + "&service=" + sService;
        else if (sServiceType == "wms" || sServiceType == "wfs" || sServiceType == "wcs")
            theUrl += "&" + sServiceType + "server=" + escape(sServer) + "&" + sServiceType + "layers=" + sLayers;

        theUrl += "&usesession=true";

        if (pIsWin)
            ArcExplorerWin = window.open(theUrl, windowName, "toolbar=no,resizable=yes,width=800,height=564,left=5,top=5");
        else if (!pIsWin && pIsNS) {
            ArcExplorerWin = window.open("", windowName, "toolbar=no,resizable=yes,width=800,height=564,screenX=5,screenY=5");
            ArcExplorerWin.location = theUrl;
        } else if (!pIsWin && pIsIE)
            ArcExplorerWin = window.open(theUrl, windowName, "toolbar=no,resizable=yes,width=784,height=548,left=5,top=5");
        else
            ArcExplorerWin = window.open(theUrl, windowName, "toolbar=no,resizable=yes,width=800,height=564");

        ArcExplorerWin.focus();
    }
}


function openSavedMap(linkId)
{
   var winMap;
   var sName = "ArcExplorerWeb";
   var ArcExplorerWin;
   var sInfo = "toolbar=no,width=800,height=564";

   if (getCookie("PortalToolKitUserID") == null) {
     alert("Korisnik je isključen ...");
     return;
   }

   if (pIsWin)
     sInfo = "toolbar=no,width=800,height=564,left=5,top=5,resizable=yes";
   else if (!pIsWin && pIsNS)
     sInfo = "toolbar=no,width=800,height=564,screenX=5,screenY=5,resizable=yes";
   else if (!pIsWin && pIsIE)
     sInfo = "toolbar=no,width=784,height=548,left=5,top=5,resizable=yes";

   var theUrl = pDomainPfx + "/arcexplorer/arcexplorer.html?origid=" + origid + "&link=" + linkId;
   winMap =  window.open(theUrl,sName,sInfo);
}

function getCookie(name){
   var cname = name + "=";
   var dc = document.cookie;
   if (dc.length > 0) {
        begin = dc.indexOf(cname);
        if (begin != -1) {
            begin += cname.length;
            end = dc.indexOf(";", begin);
            if (end == -1) end = dc.length;
            return unescape(dc.substring(begin, end));
         }
   }
   return null;
}

function setCookie(name, value) {
    var now = new Date();
    var then = "-1"
    document.cookie = name + "=" + escape(value) + "; expires=-1; path=/";
}

function deleteSessionCookies() {
  var expiration_date = new Date();
   expiration_date.setTime(expiration_date.getTime());
   document.cookie = "PortalToolKitUserID=deleted" + "; expires=" + expiration_date.toGMTString() + ";path=/";
   document.cookie = "PortalToolKitUserRole=deleted" + "; expires=" + expiration_date.toGMTString() + ";path=/";
   document.cookie = "PortalToolKitChnMgtRole=deleted" + "; expires=" + expiration_date.toGMTString() + ";path=/";
   document.cookie = "EsriPortalToolKit2004=deleted" + "; expires=" + expiration_date.toGMTString() + ";path=/";
}

