// ----------------------------------------------------------------------------------------------
// This file contains functions for browser interaction like parsing query string and getting
// viewport width and height

function getComponentUrl()
{			
	return document.location.toString().substring(0,document.location.toString().lastIndexOf("/")+1);						
}

//-----------------------------------------------------------------------------
// QueryString
//-----------------------------------------------------------------------------
function QueryString(key)
{
var value = null;
for (var i=0;i<QueryString.keys.length;i++)
{
	if (QueryString.keys[i]==key)
	{
		value = QueryString.values[i];
		break;
	}
}
return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

//-----------------------------------------------------------------------------
// ParseQueryString
//-----------------------------------------------------------------------------
function ParseQueryString()
{
var query = window.location.search.substring(1);
var pairs = query.split("&");

for (var i=0;i<pairs.length;i++)
{
	var pos = pairs[i].indexOf('=');
	if (pos >= 0)
	{
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		QueryString.keys[QueryString.keys.length] = argname;
		QueryString.values[QueryString.values.length] = value;		
	}
}

}
ParseQueryString();


//-----------------------------------------------------------------------------
// Returns the height of the viewport.
//-----------------------------------------------------------------------------
function getViewportHeight() 
{
  var height = null;
    
  // supported in Mozilla, Opera, and Safari
  if (window.innerHeight)
    height = window.innerHeight;

	// supported in standards mode of IE, but not in any other mode
  if (height == null && window.document.documentElement.clientHeight)
    height = document.documentElement.clientHeight;
	
	// supported in quirks mode, older versions of IE, and mac IE (anything else).
  if (height == null && window.document.body.clientHeight)
    height = window.document.body.clientHeight;
    
  if (height == null || height == "" || height == "0")
  {
    height = "400";
  }
  
  return height;
}	

//-----------------------------------------------------------------------------
// Returns the width of the viewport.
//-----------------------------------------------------------------------------
function getViewportWidth() 
{
  var width = null;
  
  // supported in Mozilla, Opera, and Safari
  if (window.innerWidth)
    width = window.innerWidth;

  // supported in standards mode of IE, but not in any other mode
  if (width == null && window.document.documentElement.clientWidth)
    width = document.documentElement.clientWidth;
	
  // supported in quirks mode, older versions of IE, and mac IE (anything else).
  if (width == null && window.document.body.clientWidth)
    width = window.document.body.clientWidth;
    
  if (width == null || width == "" || width == "0")
  {
    width = "400";
  }
  
  return width;
}	

//-----------------------------------------------------------------------------
// expandForPrint
//-----------------------------------------------------------------------------
function expandForPrint(elementId)
{
  var element = getObject(elementId);
  if (element)
  {
    if (element.style)
    {
			element.style.overflow = "visible";
      element.style.display  = "inline";
      element.style.width    = "auto";
      element.style.height   = "auto";
    }
  }
}

//-----------------------------------------------------------------------------
// expandForPrint
//-----------------------------------------------------------------------------
function collapseAfterPrint(elementId)
{
  var element = getObject(elementId);
  if (element)
  {
    if (element.style)
    {
      element.style.display  = "block";
			element.style.overflow = "hidden";
    }
  }
}
//-----------------------------------------------------------------------------
// Adjusts the height of infoArea according to available space
// Used by FrameInformation and all FrameSearch...
//-----------------------------------------------------------------------------
function setInformationAreaWidth(infoAreaId, width)
{
  var infoArea = getObject(infoAreaId);
  if (infoArea)
  {
    if (infoArea.style && !isNaN(width))
    {
      infoArea.style.width =  width + "px";
    }
  }
}
//-----------------------------------------------------------------------------
// Adjusts the height of infoArea according to available space
// Used by FrameInformation and all FrameSearch...
//-----------------------------------------------------------------------------
function setInformationAreaHeight(infoAreaId, containerId, containerOffset, minContainerHeight, parentHeight)
{
	setInformationArea(infoAreaId, containerId, containerOffset, minContainerHeight, parentHeight);
}
//-----------------------------------------------------------------------------
// Adjusts the height of infoArea according to available space
// Used by FrameInformation and all FrameSearch...
//-----------------------------------------------------------------------------
function setInformationArea(infoAreaId, containerId, containerOffset, minContainerHeight, parentHeight)
{
  var infoArea = getObject(infoAreaId);
  if (infoArea)
  {
    if (infoArea.style)
    {
      infoArea.style.display = "block";
      infoArea.style.overflow = "visible";
    }
  }
  var container = getObject(containerId);
  if (container)
      {
    if (container.style)
    {
      container.style.display = "block";
      container.style.overflow = "auto";
      var vpHeight = getViewportHeight();
      var height = vpHeight - containerOffset;
      if (parentHeight && parentHeight != "undefined") height = parentHeight - containerOffset;
      if (height < minContainerHeight) height = minContainerHeight;
      if (!isNaN(height))
      {
				container.style.height =  height + "px";
			}
    }
  }
}

//-----------------------------------------------------------------------------
function resizeObject(name, width, height) 
{ 
	if (!isNaN(height) && !isNaN(width))
	{
		if(document.getElementById && !(document.all)) 
		{ 
			//alert(document.getElementById(name).style.height);
			document.getElementById(name).style.width = width + "px";	
			document.getElementById(name).style.height = height + "px"; 
			//alert(document.getElementById(name).style.height);
		} 
		else if(document.all) 
		{ 		
			document.all(name).style.height = height; 
			document.all(name).style.width = width; 
			
			if( document.all(name).height )
				document.all(name).height = height;
			
		}
	} 		
}

/// <summary>
/// Look up the common name for a SRS given the EPSG code
/// </summary>
/// <param name="srs">EPSG code for SRS</param>
/// <returns>Common name of SRS</returns>
function lookupSRS(srs)
{
	switch (srs)
	{
		case "EPSG:23031": return "ED50 Sone 31";
		case "EPSG:23032": return "ED50 Sone 32";
		case "EPSG:23033": return "ED50 Sone 33";
		case "EPSG:23034": return "ED50 Sone 34";
		case "EPSG:23035": return "ED50 Sone 35";
		case "EPSG:23036": return "ED50 Sone 36";
	  
		case "EPSG:27391": return "NGO1948 Akse 1";
		case "EPSG:27392": return "NGO1948 Akse 2";
		case "EPSG:27393": return "NGO1948 Akse 3";
		case "EPSG:27394": return "NGO1948 Akse 4";
		case "EPSG:27395": return "NGO1948 Akse 5";
		case "EPSG:27396": return "NGO1948 Akse 6";
		case "EPSG:27397": return "NGO1948 Akse 7";
		case "EPSG:27398": return "NGO1948 Akse 8";

		case "EPSG:32631": return "EUREF89 Sone 31"; 
		case "EPSG:32632": return "EUREF89 Sone 32"; 
		case "EPSG:32633": return "EUREF89 Sone 33"; 
		case "EPSG:32634": return "EUREF89 Sone 34"; 
		case "EPSG:32635": return "EUREF89 Sone 35"; 
		case "EPSG:32636": return "EUREF89 Sone 36"; 

		case "NORSRS:1"  : 
		case "NORSRS:01"  : return "Oslo lokal";
		case "NORSRS:2"  : 
		case "NORSRS:02"  : return "Bærum lokal";
		case "NORSRS:3"  : 
		case "NORSRS:03"  : return "Drammen lokal";
		case "NORSRS:4"  : 
		case "NORSRS:04"  : return "Bergen/Askøy lokal";
		case "NORSRS:5"  : 
		case "NORSRS:05"  : return "Trondheim lokal";
		case "NORSRS:6"  :
		case "NORSRS:06"  : return "Tromsø lokal";
		case "NORSRS:7"  : 
		case "NORSRS:07"  : return "Dale fabrikker lokal lokal";
		case "NORSRS:8"  :
		case "NORSRS:08"  : return "NGO1948 kartesiske koordinater";
		case "NORSRS:9"  : 
		case "NORSRS:09"  : return "NGO1948 geodetiske koordinater";
		case "NORSRS:10" : return "NGO1948 plane koordinater i sentralmer";
		case "NORSRS:11" : return "Gardermoen lokal";
		case "NORSRS:12" : return "Trondheim lokal";
		case "NORSRS:13" : return "Bergen lokal";
		case "NORSRS:14" : return "Bærum lokal";

		default: return srs;
	}
}

//-----------------------------------------------------------------------------
function mapEpsgToSosi(srs)
{
	switch (srs)
	{
		case "EPSG:23031":	return 31;
		case "EPSG:23032":	return 32;
		case "EPSG:23033":	return 33;
		case "EPSG:23034":	return 34;
		case "EPSG:23035":	return 35;
		case "EPSG:23036":	return 36;
    
		case "EPSG:27391":	return 1;
		case "EPSG:27392":	return 2;
		case "EPSG:27393":	return 3;
		case "EPSG:27394":	return 4;
		case "EPSG:27395":	return 5;
		case "EPSG:27396":	return 6;
		case "EPSG:27397":	return 7;
		case "EPSG:27398":	return 8;

		case "EPSG:32631":	return 21; 
		case "EPSG:32632":	return 22; 
		case "EPSG:32633":	return 23; 
		case "EPSG:32634":	return 24; 
		case "EPSG:32635":	return 25; 
		case "EPSG:32636":	return 26; 

		case  "NORSRS:1":
		case  "NORSRS:01":	return 101;
		case  "NORSRS:2": 
		case  "NORSRS:02":	return 102;
		case  "NORSRS:3": 
		case  "NORSRS:03":	return 105;
		case  "NORSRS:4": 
		case  "NORSRS:04":	return 106;
		case  "NORSRS:5": 
		case  "NORSRS:05":	return 107;
		case  "NORSRS:6": 
		case  "NORSRS:06":	return 120;
		case  "NORSRS:7": 
		case  "NORSRS:07":	return 130;
		case  "NORSRS:8": 
		case  "NORSRS:08":	return 1000;
		case  "NORSRS:9": 
		case  "NORSRS:09":	return 1001;
		case  "NORSRS:10":	return 1010;
		case  "NORSRS:11":	return 1011;
		case  "NORSRS:12":	return 1012;
		case  "NORSRS:13":	return 1013;
		case  "NORSRS:14":	return 1014;
	}
	return -1;
}
//-----------------------------------------------------------------------------
// Sets a buttons selected state by adding or removing the "selected" class
// Used by state aware frames
// Depends on DOM.js
//-----------------------------------------------------------------------------
function setButtonSelectedState(buttonId,selectedState)
{
  var b = getObject(buttonId);
  if (b)
  {
    var c = b.className;
    if (c)
    {
			if (selectedState)
			{
				if (c.indexOf(" selected") < 0)
				{
					c = c + " selected";
				}
			}
			else
			{
				c = c.replace(" selected","");
			}
			b.className = c;
    }
	}
}

//-----------------------------------------------------------------------------
function trim(string)
{
	if(string != null)
	{
		return string.replace(/^\s+|\s+$/, '');
	}
	return null;
}

//-----------------------------------------------------------------------------

function getStyle(el,styleProp) 
{ 
	var y = null;
	if (el.currentStyle) 
		y = el.currentStyle[styleProp]; 
	else if (window.getComputedStyle) 
		y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp); 
	else if (document.defaultView && document.defaultView.getComputedStyle) {
		// external stylesheet for Mozilla and Safari 1.3+
		
		styleProp = styleProp.replace(/([A-Z])/g, "-$1");
		styleProp = styleProp.toLowerCase();
		y = document.defaultView.getComputedStyle(el,"").getPropertyValue(styleProp);
	} 
	
	
	return y; 
}


//-----------------------------------------------------------------------------
function replace(s, t, u) 
{			
	var i = s.indexOf(t);
	var r = "";
	if (i == -1) return s;
	r += s.substring(0,i) + u;
	if ( i + t.length < s.length)
		r += replace(s.substring(i + t.length, s.length), t, u);
	return r;
}

function createXmlDocument()
{
	var xmlDoc = null;
  if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);	
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		//xmlDoc.onreadystatechange = function () {
	//    if (xmlDoc.readyState == 4) createTable()
		//};
 	}
 	
 	return xmlDoc;
}

// ----------------------------------------------------------------------------------------------
var m_busy = false;
var m_busyTimer = null;
var m_busyTimeoutValue = 30000;

// ----------------------------------------------------------------------------------------------
function setBusy(value,busyTextValue)
{				
	m_busy = value;
	
	if( m_busy == true )
	{
		//showInformation("");
		var busyText = getObject("busyText");
		if (busyText != null && busyTextValue != null)
		{
			busyText.innerHTML = busyTextValue;
		}
		setCursor("body","progress");
		changeObjectDisplay("loading", "inline");					
		if (m_busyTimer) clearInterval(m_busyTimer);
		m_busyTimer = setInterval("onBusyTimeout()", m_busyTimeoutValue);
	}
	else
	{
		if (m_busyTimer) clearInterval(m_busyTimer);
		changeObjectDisplay("loading", "none");					
		setCursor("body","default");
	}
}
//-----------------------------------------------------------------------------
// Prevent 'spinning earth of death' by timout
//-----------------------------------------------------------------------------
function onBusyTimeout()
{
	setBusy(false);
}

// The following function gives TONS of javascript warnings please rewrite!
var Utilities = {
	getWindowSize : function()
	{
		var x,y;
		if (self.innerHeight) // all except Explorer
		{
			x = self.innerWidth;
			y = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		
		return {width:x, height:y};
	},
	stopBubble : function(evt)
	{
		var e = (window.event) ? window.event : evt;
		e.cancelBubble=true;
		if (e.stopPropagation) e.stopPropagation();
	}
}



