/*==================================================*
 $Id: gkutil.js ver 20090624
 Copyright 20093 gk@techtrade.se
 http://www.techtrade.se/

 *==================================================*/

// ---------------------------------------------------------------
// yet another version by John Middlemas Apr 16, 2009 10:01 AM
// http://haacked.com/archive/2006/04/06/StopTheWindow.OnloadMadness.aspx
//
// usage:
//   addWinOnLoad(myInitFunction);
// or to pass arguments
//   addWinOnLoad(function(){myInitFunction(arg1)});

function addWinOnLoad(func) {
  if(window.addEventListener && !window.opera) { 
    // Firefox 1.5+, Gecko/Mozilla 1.0.1+, Konqueror, Safari 3.0
    window.addEventListener('load', func, false)
  } else if (window.attachEvent) {
    // Microsoft IE 5+, Opera 8.0+
    window.attachEvent('onload', func)
  } else { // Incomplete solution
    // Add func to any existing window.onload.
    // Any later inconsiderate window.onload can
    // overwrite the window.onload set here
//  alert('hejsan');
    var o=window.onload
    window.onload = function() { o ? o() : o;func() }
  }
  return
}
// ---------------------------------------------------------------
//
//   usage: addEvent(window, 'load', myInitFunction);


function addEvent(obj, evnt, func) {
  if(window.addEventListener) {                                 // Mozilla, Netscape, Firefox
    obj.addEventListener(evnt, func, false);

  } else {                                                      // IE
    obj.attachEvent('on' + evnt, func);
  }
}




// ---------------------------------------------------------------
// adding flashing effect to SPAN elements
// by gk 2009-06-24 22:18
//
// usage:
//   <span class="flashing">slowly flashing text</span>
// ---------------------------------------------------------------


function doFlash(){
//	flash+=dflash;
//	dflash=(flash<0||flash>255)?-dflash:dflash;
//	flash=Math.max(0,flash);
//	flash=Math.min(255,flash);

	flash+=dflash;
	if (flash <= 8 )
  	flash+=dflash*4;
	if (flash <= 0 ) {
	  flash = 0;
	  dflash = flashStep;
	} else if (flash >= 15 ) {
	  flash = 15;
	  dflash = -flashStep;
	}
	
//	flashClass.style.color="rgb("+flash+","+flash+","+flash+")";

//var flashColor = flash*65536 + flash*256 + flash;                          //fungerar EJ i FF
//var flashColor = "rgb(" + flash + ", " + flash + ", " + flash + ")";
//  var flashColor = '#' + (flash*65536 + flash*256 + flash).toString(16);

//var flashColorHex = '000000' + (flash*65536 + flash*256 + flash).toString(16);
//var flashColor = '#' + flashColorHex.substr(flashColorHex.length - 6);

//var flashHex = (((flash < 16) ? '0' : '') + flash.toString(16)).substr(0,1);
//var flashHex = ((flash < 16) ? '0' : '') + flash.toString(16);
  var flashHex = flash.toString(16);
  var flashColor = '#' + flashHex + flashHex + flashHex;


//  document.getElementById("flashElement1").style.color = flashColor;
//  document.getElementById("orTotals").style.color = flashColor;
//document.getElementById("flashElement1").style.textDecoration = 'blink';

//window.status = 'flashColor = ' + flashColor;
  for (var i=0; i<flashClassColl.length; i++) {
    flashClassColl[i].style.color = flashColor;
  }

}

function initFlashClassColl(fInt) {
  if (fInt > 10) {
    flashInterval = fInt;
  }
  var aSpans = document.getElementsByTagName("SPAN");
//for (elementx in aSpans) {
  for (var i=0;i<aSpans.length;i++) {
    var elementx = aSpans[i];
    if (elementx != null) {
      var cn = elementx.className;
      if (cn != null) {
        if ((' ' + cn + ' ').indexOf(' flashing ') > -1)  {
          flashClassColl[foundFlashElements++] = elementx;
        }
      }
    }
  }

/*  
 alert('initFlashClassColl()\n  aSpans.length=' + aSpans.length +
       '\n  foundFlashElements=' + foundFlashElements +
       '\n  flashClassColl.length=' + flashClassColl.length);
*/
  
  if (foundFlashElements > 0)  {
    flashTimer = setInterval(doFlash, flashInterval);
  }
}

var flashTimer = null;
var flashInterval = 50;              // in ms (default)
var flashClassColl = new Array();    // flashing SPAN elements
var foundFlashElements=0;
var flash=0;                         // starting from black
var flashStep=1;
var dflash=flashStep;


//addOnloadEvent(initFlashClassColl);
//addOnloadEvent(function(){initFlashClassColl(400)});

//addWinOnLoad(initFlashClassColl);
addWinOnLoad(function(){initFlashClassColl(100)});




// insertAdjacentHTML simply isn't a supported function. Add this to your script: 
//
// insertAdjacentHTML(), insertAdjacentText() and insertAdjacentElement() 
// for Netscape 6/Mozilla by Thor Larholm me@jscript.dk 
// Usage: include this code segment at the beginning of your document 
// before any other Javascript contents. 

if(typeof HTMLElement!="undefined" && ! HTMLElement.prototype.insertAdjacentElement){ 

  HTMLElement.prototype.insertAdjacentElement = function (where,parsedNode) { 
    switch (where){ 
      case 'beforeBegin': 
        this.parentNode.insertBefore(parsedNode,this) 
        break; 
      case 'afterBegin': 
        this.insertBefore(parsedNode,this.firstChild); 
        break; 
      case 'beforeEnd': 
        this.appendChild(parsedNode); 
        break; 
      case 'afterEnd': 
        if (this.nextSibling) 
        this.parentNode.insertBefore(parsedNode,this.nextSibling); 
      else this.parentNode.appendChild(parsedNode); 
        break; 
    } 
  } 
  
  HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr) { 
    var r = this.ownerDocument.createRange();
    r.setStartBefore(this);
    var parsedHTML = r.createContextualFragment(htmlStr);
    this.insertAdjacentElement(where,parsedHTML)
  } 
  
  
  HTMLElement.prototype.insertAdjacentText = function (where,txtStr) { 
    var parsedText = document.createTextNode(txtStr) 
    this.insertAdjacentElement(where,parsedText) 
    } 


}

//  http://www.faqts.com/knowledge_base/view.phtml/aid/5756





function script_onload() {
  var msg = '';

  msg = msg + showLoadedScripts();
  msg = msg + showLoadedStyleSheets();

  msg = msg + showClientInformation();
  msg = msg + showNavigator();
  

  
  msg = msg + showAllCookies();

  msg = msg + '---------------------------\n'

  
//  msg = msg + 'gksortImgDir = \'' + gksortImgDir + '\'';



if (0) {

  msg = msg + showDims(document.body);
  msg = msg + showDims(document.getElementById('layoutTable'));

  msg = msg + showDims(document.getElementById('layoutTableTopRow'));
  msg = msg + showDims(document.getElementById('layoutTableTopTD'));

}

  dbgMsg(1, msg);


}




// show all Loaded Scripts.
function showLoadedScripts() {
  var msg = '';
  msg = msg + '---------------------------\n'
  var ds = document.scripts;
  if (ds != null) {
    msg = msg + 'loaded scripts\n';
    msg = msg + 'script (type src)\n';
    for (var i=0; i<ds.length; i++) {
      msg = msg + '[' + (i + 0) + ']';
      if (ds[i].type)             msg = msg + '  type=' + ds[i].type
      if (ds[i].src)              msg = msg + '  href=' + ds[i].src
      msg = msg + '\n'
    }
  } else {
    msg = msg + 'no .scripts found\n'
  }
  return msg;
}

// show all Loaded StyleSheets.
function showLoadedStyleSheets() {
  var msg = '';
  msg = msg + '---------------------------\n'
  ds = document.styleSheets;
  if (ds != null) {
    msg = msg + 'loaded styleSheets\n';
    msg = msg + 'styleSheet (type title href parentStyleSheet.href)\n';
    for (var i=0; i<ds.length; i++) {
      msg = msg + '[' + (i + 0) + ']'
      if (ds[i].type)             msg = msg + '  type=' + ds[i].type
      if (ds[i].title)            msg = msg + '  title=' + ds[i].title;
      if (ds[i].href)             msg = msg + '  href=' + ds[i].href
      if (ds[i].parentStyleSheet) msg = msg + '  parentStyleSheet.href=' + ds[i].parentStyleSheet.href;
      msg = msg + '\n'
    }
  } else {
    msg = msg + 'no .styleSheets found\n'
  }

  return msg;
}




// Retrieve ClientInformation.
function showClientInformation() {
  var msg = '';
  msg = msg + '---------------------------\n'
  msg = msg + 'ClientInformation' + '\n';
//  if (typeof(window.clientInformation) != 'undefined') {
  if (window.clientInformation) {
    msg = msg + '  cookieEnabled = ' + window.clientInformation.cookieEnabled + '\n';
    msg = msg + '  javaEnabled() = ' + window.clientInformation.javaEnabled() + '\n';
  } else {
    msg = msg + '  window.clientInformation is not available\n';
  }
  return msg;
}

// Retrieve ClientInformation.
function showNavigator() {
  var msg = '';
  msg = msg + '---------------------------\n'
  msg = msg + 'Navigator' + '\n';
//  if (typeof(window.clientInformation) != 'undefined') {
  if (window.navigator) {
    msg = msg + '  appCodeName = '     + window.navigator.appCodeName + '\n';
    msg = msg + '  appMinorVersion = ' + window.navigator.appMinorVersion + '\n';
    msg = msg + '  appName = '         + window.navigator.appName + '\n';
    msg = msg + '  appVersion = '      + window.navigator.appVersion + '\n';
    msg = msg + '  browserLanguage = ' + window.navigator.browserLanguage + '\n';
    msg = msg + '  cookieEnabled = '   + window.navigator.cookieEnabled + '\n';
    msg = msg + '  cpuClass = '        + window.navigator.cpuClass + '\n';
    msg = msg + '  onLine = '          + window.navigator.onLine + '\n';
    msg = msg + '  platform = '        + window.navigator.platform + '\n';
    msg = msg + '  systemLanguage = '  + window.navigator.systemLanguage + '\n';
    msg = msg + '  userAgent = '       + window.navigator.userAgent + '\n';

    msg = msg + '  javaEnabled() = '   + window.navigator.javaEnabled() + '\n';
    msg = msg + '  taintEnabled() = '  + window.navigator.taintEnabled() + '\n';

    msg = msg + '  plugins.length = '  + window.navigator.plugins.length + '\n';

    pl = window.navigator.plugins;
    for (var i=0; i<pl.length; i++) {
      msg = msg + '[' + (i + 0) + ']'
      if (pl[i].name)             msg = msg + '  name=' + pl[i].name
      if (pl[i].type)             msg = msg + '  type=' + pl[i].type
      if (pl[i].title)            msg = msg + '  title=' + pl[i].title;
      if (pl[i].src)              msg = msg + '  src=' + pl[i].src
      if (pl[i].tagName)          msg = msg + '  tagName=' + pl[i].tagName
      
      msg = msg + '\n'
    }


  } else {
    msg = msg + '  window.navigator is not available\n';
  }
  return msg;
}

function showAllCookies_OLD() {
  var msg = '';
  msg = msg + '---------------------------\n'
  if (document.cookie) {
    // cookies are separated by semicolon+space
    var aCookie = document.cookie.split("; ");
    msg = msg + 'Cookies [' + aCookie.length + ']\n';

    for (var i=0; i < aCookie.length; i++) {
      // a name/value pair (a crumb) is separated by an equal sign
      var aCrumb = aCookie[i].split("=");
      if (aCrumb.length == 2) {
        msg = msg + '[' + (i + 0) + ']  ';
        msg = msg + aCrumb[0] + ' = ' + unescape(aCrumb[1]) + '\n';
      } else if (aCrumb.length > 2) {
        for (var j=1; j < aCrumb.length; j++) {
          msg = msg + '[' + i + '.' + j + ']  ';
          msg = msg + aCrumb[j] + ' = ' + unescape(aCrumb[j+1]) + '\n';
        }
      }
//    msg = msg + aCrumb.length
    }
  } else {
    msg = msg + 'Cookies' + '\n';
    msg = msg + '  document.cookie is not available\n';
  }

  // a cookie with the requested name does not exist
  return msg;
}


// Retrieve the value of the cookie with the specified name.
function showAllCookies() {
  var msg = '';
  msg = msg + '---------------------------\n'
  if (document.cookie) {
    // cookies are name=value pairs separated by semicolon+space
    // value in an indexed cookie is keyName=keyValue pairs separated by ampersand
    var cookies = document.cookie.split("; ");
    msg = msg + 'Cookies [' + cookies.length + ']\n';

    for (var i=0; i < cookies.length; i++) {
      var eqSepIndex = cookies[i].indexOf("=");
      var cValLength = cookies[i].length - eqSepIndex + 1;
      var cValStart = eqSepIndex + 1;

      var cName = cookies[i].substring(0, eqSepIndex).replace(/\+/g, ' ')
      var cValue = cookies[i].substring(cValStart).replace(/\+/g, ' ')

      var haskeys = (cValue.indexOf("=") > -1);

      msg = msg + '[' + (i + 0) + ']  ';
      msg = msg + unescape(cName) + ' = ' + (haskeys?cValue:unescape(cValue)) + '\n';

      if (haskeys) {
        var sCrumb = cValue.split("&");
        for (var j=0; j < sCrumb.length; j++) {
          var aCrumb = sCrumb[j].split("=");
          msg = msg + '[' + i + '.' + j + ']  ';
          msg = msg + unescape(aCrumb[0]) + ' = ' + unescape(aCrumb[1]) + '\n';
        }
      }
    }

  } else {
    msg = msg + 'Cookies' + '\n';
    msg = msg + '  document.cookie is not available\n';
  }

  // a cookie with the requested name does not exist
  return msg;
}


function showDims(obj) {
  var msg = '';
  msg = msg + '---------------------------\n'
  msg = msg + 'showDims: ' + obj.tagName + '\n';


  msg = msg + '     offsetTop = ' + obj.offsetTop + '\n';          /* in parent */
  msg = msg + '  offsetHeight = ' + obj.offsetHeight + '\n';       /* in parent */
  msg = msg + '     clientTop = ' + obj.clientTop + '\n';          /* including padding, but not including margin, border, or scroll bar*/
  msg = msg + '  clientHeight = ' + obj.clientHeight + '\n';       /* including padding, but not including margin, border, or scroll bar*/
  msg = msg + '     scrollTop = ' + obj.scrollTop + '\n';          /* document  */
  msg = msg + '  scrollHeight = ' + obj.scrollHeight + '\n';       /* document  */

  msg = msg + '\n';
  msg = msg + '     style.top = ' + obj.style.top + '\n';             /*           */
  msg = msg + '  style.height = ' + obj.style.height + '\n';             /*           */
//msg = msg + '   style.width = ' + obj.style.width + '\n';             /*           */

  msg = msg + '    style.pixelTop = ' + obj.style.pixelTop + '\n';             /*           */
  msg = msg + ' style.pixelHeight = ' + obj.style.pixelHeight + '\n';             /*           */

//alert(msg);

  return msg;
}



function dbgMsg(level, msg) {
  if (debugLevel >= level) {
    if (dbgWin == null) {
      var dbgdiv = document.getElementById('scriptDebug');
      if (dbgdiv != null) {
        var msg2 = msg + '\n';
        if (typeof HTMLElement != "undefined"){           /* firefox */
          var parsedText = document.createTextNode(msg2) 
          dbgdiv.appendChild(parsedText); 
        } else {                                          /* IE */
          dbgdiv.insertAdjacentText('beforeEnd', msg2);
        }
      }
    } else {

      var dbgdiv = dbgWin.document.getElementById('scriptDebug');
      if (dbgdiv != null) {
        var dbgbody = dbgWin.document.body;
        var msg2 = msg + '\n';
/*
        msg2 = msg2 + 'dbgbody.scrollTop = ' + dbgbody.scrollTop + '\n';
        msg2 = msg2 + 'dbgbody.scrollHeight = ' + dbgbody.scrollHeight + '\n';
        msg2 = msg2 + 'dbgbody.clientHeight = ' + dbgbody.clientHeight + '\n';
*/
        if (typeof HTMLElement != "undefined"){           /* firefox */
          var parsedText = document.createTextNode(msg2) 
          dbgdiv.appendChild(parsedText); 
        } else {                                          /* IE */
          dbgdiv.insertAdjacentText('beforeEnd', msg2);
        }

        dbgbody.scrollTop = dbgbody.scrollHeight - dbgbody.clientHeight - 0
      }

    }
  }
}



var dbgWin = null;
// var dbgWin = open("<%=appMap%>includes/script-debug.asp", "dbgWinId", "toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=yes, resizable=yes, width=650, height=600, top=50, left=50")

var debugLevel = 1;


addWinOnLoad(script_onload);


/* ------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------ */

var showcoords=false;
var relCoords=false;
var posType=0;
var absCoordX=0;
var absCoordY=0;
var relZeroX=0;
var relZeroY=0;
var relCoordX=0;
var relCoordY=0;
var gkMoveElementObj = null;
var gkMoveElementStartX = 0;
var gkMoveElementStartY = 0;

function initShowCoords(scObjIn) {

  var scObj = null;
  
  scObj = document.body;

  if (scObj != null) {
//    showcoords = true;
    showcoords = false;
//    scObj.attachEvent("onmousemove", scObjMouseMove);
//    scObj.attachEvent("onmouseup", scObjMouseUp);

    addEvent(scObj, 'mousedown', scObjMouseDown);
    addEvent(scObj, 'mousemove', scObjMouseMove);
    addEvent(scObj, 'mouseup', scObjMouseUp);

//    scObj.style.cursor = 'crosshair';
  }
}

function gkMoveElement() {
  gkMoveElementObj.style.pixelLeft = gkMoveElementStartX + relCoordX;
  gkMoveElementObj.style.pixelTop = gkMoveElementStartY + relCoordY;
}


function scObjMouseDown(e) {
  setMouseCoords(e);
  relZeroX=absCoordX;
  relZeroY=absCoordY;
  relCoordX = absCoordX - relZeroX;
  relCoordY = absCoordY - relZeroY;
  scObjShowCoords();
  if (gkMoveElementObj != null) {
    gkMoveElementStartX = gkMoveElementObj.style.pixelLeft;
    gkMoveElementStartY = gkMoveElementObj.style.pixelTop;
  }
}

function scObjMouseMove(e) {
  setMouseCoords(e);
  scObjShowCoords();
  if (gkMoveElementObj != null) {
    gkMoveElement();
    return false;
  }
}

function scObjMouseUp(e) {
  setMouseCoords(e);
  gkMoveElementObj = null;
  scObjShowCoords();
  e=e?e:window.event;
  if ((e.altKey) == 1 && e.button == 2) {             // e.ctrlKey & e.shiftKey
    showcoords = !showcoords;
    document.body.style.cursor = showcoords?'crosshair':'auto';
    window.status = 'showcoords = ' + (showcoords?'on':'off');
    window.event.cancelBubble = true;
    return false;
  }
//    window.status = 'mouseUp';

  
}

// } else if (document.documentElement && document.documentElement.scrollTop) {
    // IE 6 strict mode

// *********************************************************************
// http://www.quirksmode.org/js/events_properties.html
// 
// *********************************************************************
function setMouseCoords(e) {
	var x = 0;
	var y = 0;
	if (!e) var e = window.event;

	if (e.pageX || e.pageY) 	{                     // FF
		x = e.pageX;
		y = e.pageY;
		posType = 1;

	}	else if (e.clientX || e.clientY) 	{           // IE
		x  = e.clientX;
//	x += document.body.scrollLeft;
		x += document.documentElement.scrollLeft;

		y  = e.clientY;
//	y += document.body.scrollTop;
		y += document.documentElement.scrollTop;

    if (typeof document.body.style.maxHeight != "undefined") {
      // IE 7, mozilla, safari, opera 9
  		posType = 2;

    } else {
      // IE6, older browsers
  		x -= 2;
  		y -= 2;
  		posType = 3;
    }


	}
	// x and y contain the mouse position relative to the document
	// Do something with this information
  absCoordX = x;
  absCoordY = y;
  relCoordX = absCoordX - relZeroX;
  relCoordY = absCoordY - relZeroY;

}



function scObjShowCoords() {
  if (showcoords == true) {
    var m = '';

//  m += '  d.b.scrollTop = ' + document.body.scrollTop;
//  m += '  d.dE.scrollTop = ' + document.documentElement.scrollTop;
//  m += '  d.dE.scrollLeft = ' + document.documentElement.scrollLeft;


    m += '(' + posType + ')';
  
    m += ' abs: ';
    m += 'x = ' + absCoordX + '  ';
    m += 'y = ' + absCoordY;
  
    m += ' ref: ';
    m += 'x = ' + relZeroX + '  ';
    m += 'y = ' + relZeroY;
  
    m += ' rel: ';
    m += 'x = ' + ((relCoordX > -1) ? '+':'') + relCoordX + '  ';
    m += 'y = ' + ((relCoordY > -1) ? '+':'') + relCoordY;
  
  
  
  
    window.status = m;
  }
}

addWinOnLoad(initShowCoords);


