// ===============================================================
// CBkort version 2.x, copyright Carl Bro GIS&IT, 2006
// ===============================================================
// $Archive: /Products/CBKort2/development/2.3/standard_patch_03/wwwroot/js/standard/dhtmlapi.js $ 
// $Date: 18-02-08 13:30 $
// $Revision: 6 $ 
// $Author: Kpo $
// =============================================================== 

// 1k DHTML API - standards version - modified by PDL
// See http://www.dithered.com/experiments/1kdhtml/

var d=document; // shorter reference
var nn4 = d.layers; // netscape 4?
var nua = navigator.userAgent;
var op=nua.indexOf('Opera')!=-1;
var nn6=((nua.indexOf('Netscape6/')!=-1)||(nua.indexOf('Netscape/7')!=-1))||(nua.indexOf('Gecko')!=-1);
var ie=(!nn4)&&(!op)&&(!nn6);
var px='px';

function div(op1, op2)
{
  return (op1 / op2 - op1 % op2 / op2)
}

function sizeLayer(e, left, top, right, bottom)
{
   setX(e, left);
   setY(e, top);
   setWidth(e, right - left);
   setHeight(e, bottom - top);
}

function getCoords(e)
{
    var posX = 0;
    var posY = 0;
    if(!e) e = window.event;
    if(!e) return [ 0, 0 ];
    if( typeof(e.pageX ) == 'number')
    {
        posX = e.pageX;
        posY = e.pageY;
    }
    else
    {
        if(typeof( e.clientX ) == 'number')
        {
            posX = e.clientX; posY = e.clientY;
            if( document.body && !(window.opera || window.debug || navigator.vendor == 'KDE'))
            {
                if(typeof(document.body.scrollTop) == 'number')
                {
                    posX += document.body.scrollLeft;
                    posY += document.body.scrollTop;
                }
            }
            if( document.documentElement && !(window.opera || window.debug || navigator.vendor == 'KDE'))
            {
                if(typeof(document.documentElement.scrollTop) == 'number')
                {
                    posX += document.documentElement.scrollLeft;
                    posY += document.documentElement.scrollTop;
                }
            }
        }
        // IE5.5 apparently adds two pixels
        //posX-= 2;
        //posY-= 2;
    }
    return [ posX, posY ];
}

// Returns a reference to the named element (but not nested elements in NS4).
// This reference is used by the other functions.
function getElement(e,f)
{
    if(nn4)
    {
        f=(f)?f:self;
        var V=f.document.layers;
        if(V[e]) return V[e];
        for(var W=0;W<V.length;)
            t=gE(e,V[W++]);
        return t;
    }
    if(d.getElementById)
        return d.getElementById(e);

    return d.all[e];
}

function showElement(e)
{
    nn4 ? e.visibility='show' : e.style.visibility='visible';
}

function isVisible(e)
{
    if (nn4)
        return e.visibility=='show';
    else {
             var vis= ahabGetCurrentStyle(e, "visibility");
             return (vis!='hidden');
        }
}

function hideElement(e)
{
    nn4 ? e.visibility='hide' : e.style.visibility='hidden';
}

function isBlock(e)
{
    return (e.style.display=='' || e.style.display=='block');
}

function showBlock(e)
{
    e.style.display = "block";
}

function hideBlock(e)
{
    e.style.display = "none";
}

function setX(e,x)
{
    nn4 ? e.left=x : op ? e.style.pixelLeft=x : e.style.left=x+px;
}

function setY(e,y)
{
    nn4 ? e.top=y : op?e.style.pixelTop=y : e.style.top=y+px;
}

function getX(e)
{
    if (ie||nn6)
    {
        var xPos = e.offsetLeft;
        var tempEl = e.offsetParent;
        while (tempEl != null) {
            xPos += tempEl.offsetLeft;
            tempEl = tempEl.offsetParent;
        }
        return xPos;
    }
    else
        return e.offsetLeft;
}

function getY(e)
{
    if (ie||nn6)
    {
        var yPos = e.offsetTop;
        var tempEl = e.offsetParent;
        while (tempEl != null) {
            yPos += tempEl.offsetTop;
            tempEl = tempEl.offsetParent;
        }
        return yPos;
    }
    else
        return e.offsetTop;
}

function setZ(e,z)
{
    nn4 ? e.zIndex=z : e.style.zIndex=z;
}

function setWidth(e,w)
{
    nn4 ? e.clip.width=w : op ? e.style.pixelWidth=w : e.style.width=w+px;
}

function getWidth(e)
{
    if (e.style.pixelWidth)
        return e.style.pixelWidth;
    else if (e.offsetWidth)
        return e.offsetWidth;
    else if (e.clientWidth)
       return e.clientWidth;
    else
       return 0;
}

function getHeight(e)
{
    if (e.style.pixelHeight)
        return e.style.pixelHeight;
    else if (e.offsetHeight)
        return e.offsetHeight;
    else if (e.clientHeight)
       return e.clientHeight;
    else
       return 0;
}

function setHeight(e,h)
{
    nn4 ? e.clip.height=h : op ? e.style.pixelHeight=h : e.style.height=h+px;
}

function setClip(e,t,r,b,l)
{
    if (nn4)
    {
        clipRegion=e.clip;
        clipRegion.top=t;
        clipRegion.right=r;
        clipRegion.bottom=b;
        clipRegion.left=x;
    }
    else
    {
        e.style.clip='rect('+t+px+' '+r+px+' '+b+px+' '+l+px+')';
    }
}

function writeHTML(e,h)
{
    if (nn4||op)
    {
        Y=e.document;
        Y.open();
        Y.write(h);
        Y.close();
    }
    if(typeof(e.innerHTML)=='string')
        e.innerHTML=h;
}

function appendHTML(e,h)
{
    if (e.insertAdjacentHTML)
        e.insertAdjacentHTML('beforeEnd',h);
    else
        if (e.ownerDocument)
        {
            var r = e.ownerDocument.createRange();
            r.setStartBefore(e);
            var parsedHTML = r.createContextualFragment(h);
            e.appendChild(parsedHTML);
        }
}

function writeTableCellHTML(t,c,r,h)
{
    if (!t)
    {
        alert("writeTableHTML: t eksisterer ikke");
        return;
    }
    else
        if (!t.rows)
        {
            alert("writeTableHTML: Dette er ikke en tabel "+t.id);
            return;
        }

    var cell = t.rows[r].cells[c];
    if (nn4||op)
    {
        Y=cell.document;
        Y.open();
        Y.write(h);
        Y.close();
    }
    if(typeof(cell.innerHTML)=='string')
        cell.innerHTML=h;
}


// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 4;

function dragStart(event, id)
{

  var el;
  var x, y;

  dragObj.elNode = getElement(id);

  // Get cursor position with respect to the page.

  if (ie)
  {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (nn6)
  {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (ie)
  {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (nn6)
  {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event)
{

  var x, y;

  // Get cursor position with respect to the page.

  if (ie)
  {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (nn6)
  {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  var newX = (dragObj.elStartLeft + x - dragObj.cursorStartX);
  var newY = (dragObj.elStartTop  + y - dragObj.cursorStartY);
  var maxX = getInnerWidth(window)-getWidth(dragObj.elNode);
  var maxY = getInnerHeight(window);
  if (!dragObj.elNode.limitMovements||(newX>0&&newY>0&&newX<maxX&&newY<maxY))
  {
      dragObj.elNode.style.left =  newX + "px";
      dragObj.elNode.style.top  =  newY + "px";
  }

  if (ie)
  {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (nn6)
    event.preventDefault();
}

function dragStop(event)
{

  // Stop capturing mousemove and mouseup events.

  if (ie)
  {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (nn6)
  {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
  var x = getX(dragObj.elNode);
  var y = getY(dragObj.elNode);
  var cookieName = dragObj.elNode.id;
  var cookieText = x+"!"+y+"!true";
  setCookie(cookieName,cookieText,forever());
}

function isLeftClick(e)
{
    if (nn6&&e.which>1)
        return false;
    if (ie&&event.button>1)
        return false;
    return true;
}

function pixelAdd(val1,val2)
{
    var val = parseInt(val1,10)+parseInt(val2,10);
    return val+'px';
}

// Window properties  v1.0
// http://www.dithered.com/javascript/window_properties/index.html
// code by Chris Nott (chris@NOSPAMdithered.com - remove NOSPAM)


function getInnerWidth(win) {
  var winWidth;
  document.compatMode == "CSS1Compat";
  if (document.compatMode == "CSS2Compat" || navigator.userAgent.indexOf('Gecko') != -1) {
    winWidth = parseInt(win.document.defaultView.getComputedStyle(document.documentElement, null).getPropertyValue("width"));
  }
  else if (document.compatMode == "CSS1Compat") {
    winWidth = win.document.documentElement.clientWidth;
  }
  else if (navigator.appName == 'Netscape') {
    winWidth = win.innerWidth;
  }
  else {
    winWidth = win.document.body.clientWidth;
  }
  return winWidth;
}

function getInnerHeight(win) {
  var winHeight;
  document.compatMode == "CSS1Compat";
  if (document.compatMode == "CSS2Compat" || navigator.userAgent.indexOf('Gecko') != -1) {
    winHeight = parseInt(win.document.defaultView.getComputedStyle(document.documentElement, null).getPropertyValue("height"));
  }
  else if (document.compatMode == "CSS1Compat") {
    winHeight = win.document.documentElement.clientHeight;
  }
  else if (navigator.appName == 'Netscape') {
    winHeight = win.innerHeight;
  }
  else {
    winHeight = win.document.body.clientHeight;
  }
  return winHeight;
}

/********************************************************
 * This file includes source code which is taken
 * from, or derived from the Ahab Cross Browser Library,
 * Copyright (C) 2003, CodeHouse.com
 * http://www.codehouse.com/javascript/ahab
 * This notice may not be removed.
 ********************************************************/
function ahabGetCurrentStyle(elem, prop)
{
   if( elem.currentStyle )
   {
      var ar = prop.match(/\w[^-]*/g);
      var s = ar[0];

      for(var i = 1; i < ar.length; ++i)
      {
         s += ar[i].replace(/\w/, ar[i].charAt(0).toUpperCase());
      }

      return elem.currentStyle[s]
   }
   else if( document.defaultView.getComputedStyle )
   {
      return document.defaultView.getComputedStyle(elem, null).getPropertyValue(prop);
   }
}

/******************************************************
 * Test for pop-up blocker
 * Return true if there is no blocker else return false
 ******************************************************/
function testPopupBlocker() 
{
  var testwindow = null;
  if(testwindow = window.open())
  {
    testwindow.close();
    return true;
  }
  else
    return false;
}


function pixelAdd(val1,val2)
{
    var val = parseInt(val1,10)+parseInt(val2,10);
    return val+'px';
}

function cursorLocationCorrection(dx,dy)
{
    var corr = -2;
    if(ie)
        corr = -1;
    return {x:dx+corr,y:dy+corr};
}

/******************************************************
 * Post an URL to an Iframe
 * url: compleet url
 * target: name of the frame
 ******************************************************/
function postToTarget(url, target)
{
    if(!target)
        target = '_blank';
    var body = document.getElementsByTagName("body")[0];
    var e = getElement('tempform');
    var temp = url.split("?");
    var action = temp[0];
    var tempform = document.createElement("form");
    tempform.id = 'tempform';
    tempform.name = 'tempform';
    tempform.action = action;
    tempform.target = target;
    tempform.method = 'POST';
    body.appendChild(tempform);
    
    if(temp.length > 1)
    {
        params = temp[1].split("&");
        for (i=0; i<params.length; i++)
        {
            var varval = params[i].split("=");
            var input = document.createElement("input");
            input.id = varval[0];
            input.name = varval[0];
            input.value = varval[1];
            input.type = "hidden";
            tempform.appendChild(input);
        }
    }
    tempform.submit();
    body.removeChild(tempform);
}
