/*
* 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";


//IH change bob;

//var pDomainPfx = "http://mndmdev";
//IH;

var initMinx = 67.2314;
var initMiny = 8.0057;
var initMaxx = 94.4215;
var initMaxy = 34.5346;

var theWhereClause="";
var theLayerID="";

//the custom id, must in the url for every request call
//var origid = "ESRI_World";

var origid = "";  // jting

// execute a command
function cmnCmd(sCmd) {
  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 = ""; 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,"GOS_Help",s2);
      winHelp.focus();
    }
  }
}

// get search area for viewer
function cmnGetSearchArea() {
  var winMap;
  var sName = "GOSArcExplorerWeb";
  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("The Map Viewer is not currently open.");
  }
}

// view details
function cmnViewDetails(sUuid) {
  var oFrm = document.forms.fSubmit;
  if (oFrm && oFrm.command && oFrm.uuid) {
    oFrm.command.value = "viewdetails"; oFrm.uuid.value = sUuid;
    oFrm.target = "_blank"; oFrm.submit();
  }
}

// jting: view a map with a selection
function cmnViewMapWithSelection(sServer,sService,isWMS,isDefault,minx,miny,maxx,maxy, sLayerID, sWhereClause) {
  theWhereClause=sWhereClause;
  theLayerID=sLayerID;

  return addToArcExplorerWeb(sServer,sService,isWMS,minx,miny,maxx,maxy);
}

// view a map
function cmnViewMap(sServer,sService,isWMS,isDefault,minx,miny,maxx,maxy) {
  theWhereClause="";  // jting
  theLayerID=""; // jting

	return addToArcExplorerWeb(sServer,sService,isWMS,minx,miny,maxx,maxy);
}

// jting- view a certain extent
function cmnViewExtentMap(server, service, minx,miny,maxx,maxy) {
	// do not change the window name!
	var windowName = "GOSArcExplorerWeb";

	// first open the window
	var ArcExplorerWin;
	if (pIsWin) {
		ArcExplorerWin = window.open('',windowName,"toolbar=no,width=800,height=564,left=5,top=5");
	} else if (!pIsWin && pIsNS){
		ArcExplorerWin = window.open("",windowName,"toolbar=no,width=800,height=564,screenX=5,screenY=5");
	} else if (!pIsWin && pIsIE){
		ArcExplorerWin = window.open('',windowName,"toolbar=no,width=784,height=548,left=5,top=5");
	} else {
		ArcExplorerWin = window.open('',windowName,"toolbar=no,width=800,height=564");
	}

	var theUrl = pDomainPfx+"/arcexplorer/arcexplorer.html?origid="+origid;

        theUrl+= "&server=" + escape(server) + "&service=" + escape(service) + "&minx="+minx+"&miny="+miny+"&maxx="+maxx+"&maxy="+maxy;

        // jting: remove after testing
        // alert("jting: this is the URL: " + theUrl);

	 //--if the window is opened, use session
	if (!ArcExplorerWin.closed && ArcExplorerWin.Map) {
		theUrl += "&usesession=true";
	}
       // alert("theUrl="+theUrl);
	// do not change the window size!
	if (pIsWin) {
		ArcExplorerWin = window.open(theUrl,windowName,"toolbar=no,width=800,height=564,left=5,top=5");
	} else if (!pIsWin && pIsNS){
		ArcExplorerWin = window.open("",windowName,"toolbar=no,width=800,height=564,screenX=5,screenY=5");
		ArcExplorerWin.location = theUrl;
	} else if (!pIsWin && pIsIE){
		ArcExplorerWin = window.open(theUrl,windowName,"toolbar=no,width=784,height=548,left=5,top=5");
	} else {
		ArcExplorerWin = window.open(theUrl,windowName,"toolbar=no,width=800,height=564");
	}
	ArcExplorerWin.focus();
}

//add the map service to map viewer
function addToArcExplorerWeb(server, service, isWMS, minx,miny,maxx,maxy) {
	// do not change the window name!
	var windowName = "GOSArcExplorerWeb";

//alert(server + " " + service);

	// first open the window
	var ArcExplorerWin;
	if (pIsWin) {
		ArcExplorerWin = window.open('',windowName,"toolbar=no,width=800,height=564,left=5,top=5");
	} else if (!pIsWin && pIsNS){
		ArcExplorerWin = window.open("",windowName,"toolbar=no,width=800,height=564,screenX=5,screenY=5");
	} else if (!pIsWin && pIsIE){
		ArcExplorerWin = window.open('',windowName,"toolbar=no,width=784,height=548,left=5,top=5");
	} else {
		ArcExplorerWin = window.open('',windowName,"toolbar=no,width=800,height=564");
	}

// if remote then +"&addremote=true"
	var theUrl = pDomainPfx+"/arcexplorer/arcexplorer.html?origid="+origid;

	if (!isWMS)
		theUrl += "&server=" + escape(server) + "&service=" + escape(service);
	else
		theUrl += "&wmsserver=" + escape(server);

	if (minx && miny && maxx && maxy)
			theUrl += "&minx="+minx+"&miny="+miny+"&maxx="+maxx+"&maxy="+maxy;
	else
		theUrl += "&minx="+initMinx+"&miny="+initMiny+"&maxx="+initMaxx+"&maxy="+initMaxy;

        if (theWhereClause != "") {
          theUrl += "&whereClause=" + theWhereClause;
        }

        if (theLayerID != "") {
          theUrl += "&layerid=" + theLayerID;
        }

        // jting: remove after testing
        ///alert("jting: this is the URL: " + theUrl);

	 //--if the window is opened, use session
	if (!ArcExplorerWin.closed && ArcExplorerWin.Map) {
		theUrl += "&usesession=true";
	}
       // alert("theUrl="+theUrl);
	// do not change the window size!
	if (pIsWin) {
		ArcExplorerWin = window.open(theUrl,windowName,"toolbar=no,width=800,height=564,left=5,top=5");
	} else if (!pIsWin && pIsNS){
		ArcExplorerWin = window.open("",windowName,"toolbar=no,width=800,height=564,screenX=5,screenY=5");
		ArcExplorerWin.location = theUrl;
	} else if (!pIsWin && pIsIE){
		ArcExplorerWin = window.open(theUrl,windowName,"toolbar=no,width=784,height=548,left=5,top=5");
	} else {
		ArcExplorerWin = window.open(theUrl,windowName,"toolbar=no,width=800,height=564");
	}
	ArcExplorerWin.focus();
}


// view details
function cmnViewMndmDetails(sUuid,aCommand) {
  var oFrm = document.forms.fSubmit;
  if (oFrm && oFrm.command && oFrm.uuid) {
    oFrm.command.value = aCommand; oFrm.uuid.value = sUuid;
    oFrm.target = "_blank"; oFrm.submit();
  }
}

// jt
function openWin(select, querytype, textboxname, tab)
{ 
  for (var i = 0; i < select.options.length; i++) {
    if (select.options[i].selected) {
      if (select.options[i].text == "Pick List (or)") {
        //alert("Pick List (or) picked");
        // open a new window 
        var lookup= '&lookup=' + querytype;
        var textn= '&textboxname=' + textboxname;
        var win = window.open('/gosportal/gos?command=mndmsearchlookup:' + tab + lookup + textn, 'lookup', 'width=290, height=400');
      }
    }
  }
}

// jt
function updateField(s, formInput)
{    
        var str= "";
	for (x=0; x< s.length; x++) {
          str+= "\"" + s[x] + "\"";
          if (x != s.length - 1) {
            str+= ", "
          }
        }	

	//document.forms[0].township.value = str;
	var temp = "document.forms[0]." + formInput;
        var Source = eval(temp);
       
        Source.value= str;
}
