function HashMap()
{
   this.list = new Array();
   this.keys = new Array();
   this.values = new Array();

   this.init = function()
   {
      this.list = new Array();
      this.keys = new Array();
      this.values = new Array();
   }

   this.get = function(name)
   {
      for (var i = 0; i < this.list.length; i++)
      {
         var param = this.list[i];
         if (param.name == name)
         {
            return param.value;
         }
      }
      return null;
   }

   this.getIgnoreCase = function(name)
   {
      var upperName = name.toUpperCase();
      for (var i = 0; i < this.list.length; i++)
      {
         var param = this.list[i];
         var upperParamName = param.name.toUpperCase();
         if (upperParamName == upperName)
         {
            return param.value;
         }
      }
      return null;
   }

   this.getIndex = function(name)
   {
      for (var i = 0; i < this.list.length; i++)
      {
         var param = this.list[i];
         if (param.name == name)
         {
            return i;
         }
      }
      return -1;
   }

   this.put = function(name, value)
   {
      var idx = this.getIndex(name);
      if (idx == -1) idx = this.list.length;

      this.list[idx] = new nameValuePair(name, value);
      this.keys[idx] = name;
      this.values[idx] = value;
   }

   this.remove = function(name)
   {
      var idx = this.getIndex(name);
      if (idx == -1) return false;

      var tmpList = this.list;
      this.init();
      for (var i = 0; i < tmpList.length; i++)
      {
         if (i != idx)
         {
            this.put(tmpList[i].name, tmpList[i].value);
         }
      }
   }

   this.addValue = function(name, value, ignoreCase)
   {
      var oldValue = ignoreCase ? this.getIgnoreCase(name) : this.get(name);
      var newValue = (oldValue && oldValue != "") ? oldValue + "," + value : value;
      this.put(name, newValue);
   }

   this.getKeys = function()
   {
      return this.keys;
   }

   this.getValues = function()
   {
      return this.values;
   }

   this.getValuesAsCSVString = function()
   {
      if (this.values == null || this.values.length == 0) return "";
      var str = "";
      for (var i = 0; i < this.values.length; i++)
      {
         if (str.length > 0) str += ",";
         str += this.values[i];
      }
      return str;
   }

   this.length = function()
   {
      return this.list.length;
   }

   this.makeQueryString = function(keyStr)
   {
      if (!this.list || this.list.length == 0) return "";
      var keyArr = null;
      if (keyStr && keyStr.length > 0)
      {
         keyArr = keyStr.split(",");
      }

      var qStr = "";
      for (var i = 0; i < this.list.length; i++)
      {
         if (keyArr == null || !arrayContaines(keyArr, this.list[i].name))
         {
            qStr += "&" + this.list[i].name + "=";
            qStr += this.list[i].value;
         }
      }
      return qStr;
   }

}

function nameValuePair(name, value)
{
   this.name = name;
   this.value = value;
}

function WmsSource(id, name, url, srs, version, format, bbox, layers, dates, elevation, styles, supportString, colorAtts, defaultLayers)
{
   this.id = id;
   this.name = name;
   this.url = url;
   if (url.indexOf('http://') == -1)
   {
      this.url = 'http://' + url;
   }
   this.srs = srs;
   this.version = version;
   this.format = format;
   this.bbox = bbox;
   this.layers = layers;
   this.dates = dates;
   this.elevation = elevation;
   this.styles = styles;
   this.defaultLayers = defaultLayers ? defaultLayers : "";
   this.colorAtts = colorAtts;
   this.supportString = supportString;
   this.userCredentials = "";
   this.transparent = true;
   this.dynamic = true;
}

function WmsSourceLayer(id, name, styles, dates, elevation, elevationUnit)
{
   this.id = id;
   this.name = name;
   this.styles = styles;
   this.dates = dates;
   this.elevation = elevation;
   this.elevationUnit = elevationUnit;
}

/* WmsSource[], WmsSource.id -> WmsSource
*/
function searchWmsSource(sourceList, id)
{
   if (!sourceList || !id || id == "") return null;
   for (var i = 0; i < sourceList.length; i++)
   {
      if (sourceList[i].id == id)
      {
         return sourceList[i];
      }
   }
   return null;
}

/* WmsSource[], WmsSource.id, property -> value
*/
//function getFromSource(sourceList, id, property)   NO NEED - REPLACED BY  searchWmsSource(sourceList, id). torc
//{
//   var wmsSource = searchWmsSource(sourceList, id);
//   if (!wmsSource) return null;
//   switch (property.toLowerCase())
//   {
//      case "id" : return wmsSource.id;
//      case "name" : return wmsSource.name;
//      case "url" : return wmsSource.url;
//      case "srs" : return wmsSource.srs;
//      case "version" :return wmsSource.version;
//      case "format" :return wmsSource.format;
//      case "layers" :return wmsSource.layers;
//      case "defaultlayers" :return wmsSource.defaultLayers;
//      case "styles" :return wmsSource.styles;
//      case "coloratts" :return wmsSource.colorAtts;
//      case "dates" :return wmsSource.dates;
//      case "elevation" :return wmsSource.elevation;
//      case "supportstring" :return wmsSource.supportString;
//      case "bbox" :return wmsSource.bbox;
//      case "transparent" :return wmsSource.transparent;
//      case "dynamic" :return wmsSource.dynamic;
//      case "usercredentials" :return wmsSource.userCredentials;
//      default: return null;
//   }
//   return null;
//}

function arrayContaines(arr, value)
{
   if (!arr || !value) return false;

   if (!arr.length) return (arr == value);

   for (var i = 0; i < arr.length; i++)
   {
      if (arr[i] == value) return true;
   }
   return false;
}

function selectOptionValue(selectField, value)
{
   if (!value || value == "") return;
   if (!selectField) return;

   var idx = 0;
   for (var i = 0; i < selectField.options.length; i++)
   {
      if (selectField.options[i].value == value)
      {
         idx = i;
         break;
      }
   }

   selectField.selectedIndex = idx;
}

// ************* getLayer *************
// Return a reference to the named layer.  Null if the layer cannot be found.
function getById(name)
{
   if (!name) return null;
   var l = document.getElementById ?
      document.getElementById(name) :
      document.all ?
      document.all[name] :
      document.layers ?
      //document.layers[name] :
      findLayer(name, document) :
      null;
   return l;
}

function getSelectedOptionValue(selectField)
{
   if (!selectField) return null;
   if (selectField.selectedIndex == -1) return null;

   var val = selectField.options[selectField.selectedIndex].value;
   return val;
}

function selectCheckboxes(vcheck, select)
{
   if (select == null || select == 'undefined') return;
   if (vcheck)
   {
      if (vcheck[0])
      {
         for (i = 0; i < vcheck.length; i++)
         {
            vcheck[i].checked = select;
         }
      }
      else
      {
         vcheck.checked = select;
      }
   }
}

function getSelectedCheckboxValuesString(vcheck, sep)
{
   var valueStr = "" ;

   if (vcheck)
   {
      if (vcheck[0])
      {

         for (i = 0; i < vcheck.length; i++)
         {
            if (vcheck[i].checked)
            {
               if (valueStr != "") valueStr += sep;
               valueStr = valueStr + vcheck[i].value;
            }
         }
      }
      else
      {
         if (vcheck.checked)
         {
            valueStr = vcheck.value;
         }
      }
   }

   return valueStr;
}

function getFieldCSV(field, delim, replaceDelim)
{
   if (!field) return "";
   var resStr = "" ;

   //select has length function
   if (field.length && !field.options)
   {
      for (i = 0; i < field.length; i++)
      {
         if (resStr != "" || i > 0) resStr += delim;           // Modified aug 06 VW in order to handle empty value in first item
         var v = getFieldVaue(field[i]);
         if (replaceDelim)
         {
            v = replaceDelimInValue(delim, v);
         }
         resStr += v;
      }
   }
   else
   {
      var v = getFieldVaue(field);
      if (replaceDelim)
      {
         v = replaceDelimInValue(delim, v);
      }
      resStr = v;
   }

   return resStr;
}

function replaceDelimInValue(delim, value)
{
   // replace only ,
   var newValue = value;
   switch (delim)
         {
      case ",":
         while (newValue.indexOf(delim) != -1)
         {
            newValue = newValue.replace(delim, ";");
         }
         break;
   }
   return newValue;
}

function getFieldVaue(field)
{
   if (!field) return "";
   if (!field.type) return field.value ? field.value : "";

   switch (field.type)
         {
      case "checkbox":
      case "radio":
         return field.checked ? field.value : "";
         break;

      case "select-multiple":
      case "select-one":
         if (!field.options) return "";
         return field.selectedIndex != -1 ? field.options[field.selectedIndex].value : "";
         break;

      case "text":
      case "textarea":
         return field.value;
         break;

      default:
         return "";
   }
}

function addUniqueValue(arr, value)
{
   if (!value) return;
   if (!arr) arr = new Array();

   for (var i = 0; i < arr.length; i++)
   {
      if (arr[i] == value) return;
   }

   // finnes ikke, legg til
   arr[arr.length] = value;
   return;
}

function initCheckboxes(vcheck, checkValueStr, sep)
{
   if (checkValueStr != null && checkValueStr != "")
   {
      var s = checkValueStr.split(sep);
      for (i = 0; i < s.length; i++)
      {
         for (j = 0; j < vcheck.length; j++)
         {
            if (vcheck[j].value == s[i])
            {
               vcheck[j].checked = true;
               break;
            }
         }
      }
   }
}

function getLocalServerName(wmsServers)
{
   var layerStr = "";
   var names = wmsServers.getKeys();
   if (!names) return "";

   for (var i = 0; i < names.length; i++)
   {
      var serverName = names[i];
      var wmsSourceList = wmsServers.get(serverName);
      if (wmsSourceList && wmsSourceList.length > 0)
      {
         var url = wmsSourceList[0].url;
         var domain = getDomain(url);
         if (document.location.host == domain)
         {
            var context = getContext(url);
            var localPath = document.location.pathname;
            if (localPath.charAt(0) == '/') localPath = localPath.substring(1);
            if (localPath.indexOf(context) == 0) // localPath startsWidth
            {
               return serverName;
            }
         }
      }
   }
   return null;
}

function ascendingParameter(a, b)
{
   if (a.value && b.value) return a.value - b.value;
   else return a - b;
}

function descendingParameter(a, b)
{
   if (a.value && b.value) return b.value - a.value;
   else return b - a;
}

function compareNum(a, b)
{
   return a - b;
}

function getDomainAndContext(url)
{
   if (!url) return "";
   var endidx = url.lastIndexOf("/");
   var context = url.substring(0, endidx != -1 ? endidx : tmp.length);
   return context;
}

function getDomain(url)
{
   if (!url) return "";
   var idx = url.indexOf("//");
   var tmp = idx != -1 ? url.substring(idx + 2) : url;
   var endidx = tmp.indexOf("/");
   var domain = tmp.substring(0, endidx != -1 ? endidx : tmp.length);
   return domain;
}

function getContext(url)
{
   if (!url) return "";
   var domain = getDomain(url);
   var idx = url.indexOf(domain);
   var domainPartLength = idx + domain.length + 1;
   if (url.length < (domainPartLength)) return "";

   var tmp = idx != -1 ? url.substring(domainPartLength) : url;
   var endidx = tmp.indexOf("/");
   var context = tmp.substring(0, endidx != -1 ? endidx : tmp.length);
   return context;
}

function getUrlString(urlStr, requestStr, oldPath, newPath)
{
   if (urlStr != null && urlStr != "") return urlStr;

   var newUrl = "";
   var req = requestStr;
   req.replace('http://', '');

   var idx = req.indexOf(oldPath);
   if (idx > 0)
   {
      newUrl += req.substring(0, idx);
   }
   else
   {
      newUrl = req;
   }
   newUrl += newPath;
   return newUrl;
}

function chooseDefault(chooseVal, defaultStr, altStr)
{
   if (altStr == null || altStr == "") return defaultStr;

   if (chooseVal == "" || chooseVal == "true")
   {
      return defaultStr;
   }
   else
   {
      return altStr;
   }
}

function getUTCDateTimeString(millies)
{
   var dateTime = new Date(millies);
   var tmpStr = getDayName(dateTime.getUTCDay()) + ", ";
   tmpStr += dateTime.getUTCDate() + " ";
   tmpStr += getMonthName(dateTime.getUTCMonth()) + " ";
   tmpStr += dateTime.getUTCFullYear() + " ";
   var s = dateTime.getUTCHours();
   tmpStr += (s < 10 ? "0" + s : s) + ":";
   s = dateTime.getUTCMinutes();
   tmpStr += (s < 10 ? "0" + s : s) + ":";
   s = dateTime.getUTCSeconds();
   tmpStr += (s < 10 ? "0" + s : s) + " UTC";
   return tmpStr;
}

function getDayName(dayNum)
{
   switch (dayNum)
         {
      case 0:  return "Sunday";
      case 1:  return "Monday";
      case 2:  return "Tuesday";
      case 3:  return "Wednesday";
      case 4:  return "Thursday";
      case 5:  return "Friday";
      case 6:  return "Saturday";
   }
}

function getMonthName(monthNum)
{
   switch (monthNum)
         {
      case 0:  return "Jan";
      case 1:  return "Feb";
      case 2:  return "Mar";
      case 3:  return "Apr";
      case 4:  return "May";
      case 5:  return "Jun";
      case 6:  return "Jul";
      case 7:  return "Aug";
      case 8:  return "Sep";
      case 9:  return "Oct";
      case 10:  return "Nov";
      case 11:  return "Dec";
   }
}


function Ellipsoid()
{
   this.A = 6378137;
   this.earthCircum = Math.PI * 2.0 * this.A;

   //  Create the ERM constants.
   this.F = 1 / 298.257223563;
   this.Eps2 = (this.F) * (2.0 - this.F);

   //this.K1 = (this.A * (1.0 - this.Eps2)) * (Math.PI/180);
   //this.K2 = this.A * (Math.PI/180);

   this.K1 = toRadians(this.A * (1.0 - this.Eps2));
   this.K2 = toRadians(this.A);

   this.calcDistance = function(long1, lat1, long2, lat2, srs)
   {
      var centLat = (lat1 + lat2) * 0.5;
      var east = long2 - long1;
      var north = lat2 - lat1;

      if (srs == "EPSG:4326") // long lat srs
      {
         east *= this.longToM(centLat);
         north *= this.latToM(centLat);
      }
      var dist = Math.sqrt(east * east + north * north);

      return dist;
   }

   this.latToM = function(latitude)
   {
      var div0 = this.getDiv0(latitude);
      var div1 = Math.sqrt(div0 * div0 * div0);
      return (this.K1 / div1);
   }

   this.longToM = function(latitude)
   {
      var div2 = Math.sqrt(this.getDiv0(latitude));
      return (Math.cos(toRadians(latitude)) * this.K2) / div2;
   }

   this.getDiv0 = function(latitude)
   {
      var rad = toRadians(latitude);
      var div0 = Math.sin(rad);
      return 1.0 - this.Eps2 * div0 * div0;
   }
}

function toRadians(degrees)
{
   return degrees * (Math.PI / 180);
}


// Text Field Validation Functions
// copyright Stephen Chapman, 26th Dec 2004
// you may copy this function but please keep the copyright notice with it
function stripBlanks(fld)
{
   var result = "";
   var c = 0;
   for (i = 0; i < fld.length; i++)
   {
      if (fld.charAt(i) != " " || c > 0)
      {
         result += fld.charAt(i);
         if (fld.charAt(i) != " ") c = result.length;
      }
   }
   return result.substr(0, c);
}

var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function isValid(parm, val)
{
   if (parm == "") return true;
   for (i = 0; i < parm.length; i++)
   {
      if (val.indexOf(parm.charAt(i), 0) == -1) return false;
   }
   return true;
}

function isNum(parm)
{
   return isValid(parm, numb);
}
function isLower(parm)
{
   return isValid(parm, lwr);
}
function isUpper(parm)
{
   return isValid(parm, upr);
}
function isAlpha(parm)
{
   return isValid(parm, lwr + upr);
}
function isAlphanum(parm)
{
   return isValid(parm, lwr + upr + numb);
}
function isAlphanumExt(parm)
{
   return isValid(parm, lwr + upr + numb + " _-");
}
function isDecimal(parm)
{
   return isValid(parm, numb + '.');
}

function oneOnly(parm, chr, must)
{
   var atPos = parm.indexOf(chr, 0);
   if (atPos == -1)
   {
      return !must;
   }
   if (parm.indexOf(chr, atPos + 1) > - 1)
   {
      return false;
   }
   return true;
}

function adjacent(parm, chrs)
{
   return(parm.indexOf(chrs, 0) != -1);
}

function onlyAdjacent(parm, comb, chrs)
{
   var a = parm.split(comb);
   var b = a.join('');
   for (i = 0; i < parm.length; i++)
   {
      if (val.indexOf(parm.charAt(i), 0) != -1) return false;
   }
   return true;
}

function setOrder(parm, first, second)
{
   var pos1 = parmField.indexOf(first, 0);
   if (pos1 == -1) return false; // first char not found
   var pos2 = parmField.indexOf(second, pos1 + 1);
   if (pos2 == -1) return false; // second char doesn't follow first
   return true;
}

function setDistance(parm, first, last, min, max)
{
   var pos1 = first == '' ? 0 :
              parmField.indexOf(first, 0);
   var pos2 = last == '' ? parmField.length - pos1 - 1 :
              parmField.indexOf(second, pos1 + 1);
   if (pos1 == -1) return false;
   if (pos2 < min || pos2 > max) return false;
   return true;
}

function endOption(fld, val)
{
   return fld.substring(fld.lastIndexOf(val)) == val;
}

function helpOpen(helpPageUrl,windowName)
{
   return window.open(helpPageUrl,windowName,'toolbar=yes, location=no, directories=no, status=no, scrollbars=yes, resizable=yes');
}

// Bytt ut referanser til denne med getSelectedCheckboxValuesString
/*
function getSelectedCheckboxesString(checkRef, delim)
{
    var resStr="" ;
    if (checkRef)
    {
        if (checkRef[0])
        {

           for (i = 0; i<checkRef.length; i++)
           {
              if (checkRef[i].checked)
              {
                 if (resStr != "") resStr += delim;
                 resStr=resStr + checkRef[i].value;
              }
           }
       }
       else
       {
          if (checkRef.checked)
          {
             resStr=checkRef.value;
          }
       }
    }

    return resStr;
}
*/
/*

function getDurationString(time)
{
   var aDateValues = decomposeDuration(time);
   var aBuffer = "";
   if (!aDateValues || aDateValues.length == 0)
   {
      // if the duration is empty
      //we chose as a standard to return "PTOS"
      return "PT0S";
   }

   var negate = getNegate(aDateValues);
   if (negate == -1)
   {
      aBuffer += '-';
   }

   aBuffer += 'P';

   if (aDateValues[0] != 0)
   {
      aBuffer += aDateValues[0];
      aBuffer += 'Y';
   }

   if (aDateValues[1] != 0)
   {
      aBuffer += aDateValues[1];
      aBuffer += 'M';
   }

   if (aDateValues[2] != 0)
   {
      aBuffer += aDateValues[2];
      aBuffer += 'D';
   }

   var isThereTime = ((aDateValues[3] != 0) || (aDateValues[4] != 0) ||
         (aDateValues[5] != 0) || (aDateValues[6] != 0)) ? true : false;
   if (isThereTime)
   {
      aBuffer += 'T';

      if (aDateValues[3] != 0)
      {
         aBuffer += aDateValues[3];
         aBuffer += 'H';
      }

      if (aDateValues[4] != 0)
      {
         aBuffer += aDateValues[4];
         aBuffer += 'M';
      }

      if (aDateValues[5] != 0 || aDateValues[6] != 0)
      {
         aBuffer += aDateValues[5];
         if (aDateValues[6] != 0)
         {
            aBuffer += '.';
            aBuffer += aDateValues[6];
         }
         aBuffer += 'S';
      }
   }

   return aBuffer;
}

function decomposeDuration(time)
{
   var dateValues = new Array();
   var refSecond = 1000;
   var refMinute = 60 * refSecond;
   var refHour = 60 * refMinute;
   var refDay = 24 * refHour;
   var refMonth = Math.floor(30.42 * refDay);
   var refYear = 12 * refMonth;


   var negate = 1;
   if (time < 0)
   {
      negate = -1;
      time = -time;
   }

   var year = Math.floor(time / refYear);
   dateValues[0] = negate * year;
   time = time % refYear;


   var month = Math.floor(time / refMonth);
   dateValues[1] = negate * month;
   time = time % refMonth;


   var day = Math.floor(time / refDay);
   dateValues[2] = negate * day;
   time = time % refDay;


   var hour = Math.floor(time / refHour);
   dateValues[3] = negate * hour;
   time = time % refHour;


   var minute = Math.floor(time / refMinute);
   dateValues[4] = negate * minute;
   time = time % refMinute;


   var seconds = Math.floor(time / refSecond);
   dateValues[5] = negate * seconds;
   time = time % refSecond;


   var milliseconds = (time);
   dateValues[6] = negate * milliseconds;

   return dateValues;
}

function getNegate(aDateValues)
{
   for (var i = 0; i < aDateValues.length; i++)
   {
      if (aDateValues[i] < 0)
      {
         return -1;
      }
   }
   return 1;
}


function formatTime(millis)
{
    var time = new Date(millis)
    var hours = time.getHours()
    var minutes = time.getMinutes()
    minutes=((minutes < 10) ? "0" : "") + minutes
    var timeStr = hours + ":" + minutes;
    return timeStr;
}

function setFormItemValue(formItem, val, idx)
{
    if (!formItem) return;
    if (formItem[0])
    {
        formItem[idx].value = val;
    }
    else
    {
        formItem.value = val;
    }
}

function TimeDurationUtil()
{
    this.YEAR = 0;
    this.MONTH = 1;
    this.DAY = 2;
    this.HOUR = 3;
    this.MIN = 4;
    this.SEC = 5;
    this.MILLI_SEC = 6;

    this.refSecond = 1000;
    this.refMinute = 60 * this.refSecond;
    this.refHour = 60 * this.refMinute;
    this.refDay = 24 * this.refHour;
    this.refMonth = (30.42 * this.refDay);
    this.refYear = 12 * this.refMonth;

    this.decompose = function(time)
    {
        dateValues = new Array();
        var negate = 1;
        if (time < 0)
        {
         negate = -1;
         time = -time;
        }

        var year = Math.floor(time / this.refYear);
        dateValues[this.YEAR] = negate * year;
        time = time % this.refYear;


        var month = Math.floor(time / this.refMonth);
        dateValues[this.MONTH] = negate * month;
        time = time % this.refMonth;


        var day = Math.floor(time / this.refDay);
        dateValues[this.DAY] = negate * day;
        time = time % this.refDay;


        var hour = Math.floor(time / this.refHour);
        dateValues[this.HOUR] = negate * hour;
        time = time % this.refHour;


        var minute = Math.floor(time / this.refMinute);
        dateValues[this.MIN] = negate * minute;
        time = time % this.refMinute;


        var seconds = Math.floor(time / this.refSecond);
        dateValues[this.SEC] = negate * seconds;
        time = time % this.refSecond;


        var milliseconds = (time);
        dateValues[this.MILLI_SEC] = negate * milliseconds;

        return dateValues;
    }

    this.hourMinutesString = function(timeVar)
    {
        var timeArray = timeVar;
        if (timeVar[0] == null) timeArray = this.decompose(timeVar);
        var str = "";
        var minStr = "";
        var tmpHours = timeArray[this.YEAR] * (this.refYear / this.refHour);
        tmpHours += timeArray[this.MONTH] * (this.refMonth / this.refHour);
        tmpHours += timeArray[this.DAY] * (this.refDay / this.refHour);
        tmpHours += timeArray[this.HOUR];

        if (tmpHours < 10) str = "0";
        if (timeArray[this.MIN] < 10) minStr = "0";

        str += tmpHours + ":" + minStr + timeArray[this.MIN];

        return str;
    }
}

function MapServer(name, url, layers, srs, version, format)
{
   this.name = name;
   this.url = url;
   if (url.indexOf('http://') == -1)
   {
      this.url = 'http://' + url;
   }

   this.layers = layers;
   this.srs = srs;
   this.version = version;
   this.format = format;
}

function getMapServerByName(mapServerArray, name)
{
   if (mapServerArray == null || name == null) return null;

   for (var i = 0; i < mapServerArray.length; i++)
   {
      if (mapServerArray[i].name == name) return mapServerArray[i];
   }
}



*/
