/*******************************************************************************
*
* © 2008 Copyright A-Vision
*
* File description :       Package :: UserInterface functions
* 
* Created by       :       Arnold Velzel
* Created on       :       11/08/2008
*
* Last changed by  :       Arnold Velzel
* Last changed on  :       <LastChanged>
* 
*******************************************************************************/

function applicationSize()
{
 var topleft = document.getElementById("markertopleft");
 var bottomright = document.getElementById("markerbottomright");
 var w = 0;
 var h = 0;

 if ( topleft && bottomright) {
  w = eval( bottomright.offsetLeft + bottomright.offsetWidth);
  h = eval( bottomright.offsetTop + bottomright.offsetHeight);
 } else {
  w = document.body.offsetWidth;
  h = document.body.offsetHeight;
 }
 
 return( Array(w,h));
}

/*
 Find the absolute Left position of an object
*/
function absoluteX( obj)
{
 var curleft = 0;
 if (obj.offsetParent) {
  while (obj.offsetParent) {
   if ( obj.clientWidth > 0) {
    curleft += obj.offsetLeft;
 	  curleft -= obj.scrollLeft;
   }
	  obj = obj.offsetParent;
	  if ( BrowserDetect.browser=="Explorer" && obj.offsetParent && isNum(obj.currentStyle.borderLeftWidth)) {
    curleft += parseInt(obj.currentStyle.borderLeftWidth);
	  }
	 }
 }
 else if (obj.x) curleft += obj.x;
 return curleft;
}
function positionLeft(obj)
{
 return( absoluteX( obj));
}
/*
 Find the absolute Top position of an object
*/
function absoluteY( obj)
{
 var curtop = 0;
 if (obj.offsetParent) {
  while (obj.offsetParent) {
	  curtop += obj.offsetTop;
	  curtop -= obj.scrollTop;
	  obj = obj.offsetParent;
	  if ( BrowserDetect.browser=="Explorer" && obj.offsetParent && isNum(obj.currentStyle.borderTopWidth)) {
    curtop += parseInt(obj.currentStyle.borderTopWidth);
	  }
	 }
 }
 else if (obj.y) curtop += obj.y;
 return curtop;
}
function positionTop(obj)
{
 return( absoluteY( obj));
}

function switch_style( css_title, active)
{
// You may use this script on your site free of charge provided
// you do not remote this notice or the URL below. Script from
// http://www.thesitewizard.com/javascripts/change-style-sheets.shtml
 var i, link_tag ;
 link_tag = document.getElementsByTagName("link");
 for (i = 0; i < link_tag.length ; i++ ) {
  if ((link_tag[i].rel.indexOf( "stylesheet" ) != -1) && link_tag[i].title) {
   link_tag[i].disabled = active;
   if (link_tag[i].title == css_title) {
    link_tag[i].disabled = !active;
   }
  }
 }
}

function setBlockCenter( blk)
{
 var bsize = applicationSize(); //alert( bsize);
 var w = blk.offsetWidth;   //alert( w);
 var h = blk.offsetHeight;  //alert( h);
 var l = (bsize[0]-w)/2;
 var t = (bsize[1]-h)/2;
 blk.style.left = l+"px";
 blk.style.top = t+"px";
}
function setBlockHeight(blk,unlimited)
{
 if (!blk) return;
 stripEmptyNodes( blk);
 while ( blk && blk.childNodes.length && blk.className!="box") {
  blk = blk.childNodes[0];
 }
 if (!blk || blk.nodeName != "DIV") return;
 var windowBodyCenter = blk.childNodes[1];
 var windowBodyCenterHeight = windowBodyCenter.childNodes[1].childNodes[0].offsetHeight;
 if ( unlimited) {
 } else {
  if ( windowBodyCenterHeight > 550) {
   windowBodyCenterHeight = 550;
  }
 }
 windowBodyCenter.childNodes[0].style.height = windowBodyCenterHeight+"px";
 windowBodyCenter.childNodes[1].style.height = windowBodyCenterHeight+"px";
 windowBodyCenter.childNodes[2].style.height = windowBodyCenterHeight+"px";
}

function Help(bHelp)
{
 if ( bHelp.nodeName == "DIV") {
  helpID = bHelp.parentNode.parentNode.childNodes[1].innerText?bHelp.parentNode.parentNode.childNodes[1].innerText:bHelp.parentNode.parentNode.childNodes[1].textContent;
 } else {
  helpID = bHelp;
 }
 div = document.getElementById("leaveHelp");
 if ( div) {
  closeHelp();
  div = null;
 }
 if ( !div) {
  div = document.createElement("DIV");
  div.id = "leaveHelp";
  div.style.position = "fixed";
  div.helpID = helpID;
  if ( !nextZindex) nextZindex = 9000;
  nextZindex++;
  div.style.zIndex = nextZindex;
  document.body.insertBefore(div,document.body.firstChild);
 }

 url = "help/help.php?random="+Math.random()+"&ID="+encodeURIComponent(helpID);
 //getServerHtml( url, targetObject, furtherAction, errorAction)
 getServerHtml( url, document.getElementById("leaveHelp"), function() {
  setnotifications();
  div = document.getElementById("leaveHelp");
  if ( div) {
   stripEmptyNodes( div);
   setBlockHeight( div.childNodes[0]);
   div.childNodes[0].style.zIndex = nextZindex;
   div.childNodes[0].childNodes[2].childNodes[1].innerHTML = "« "+div.helpID+" »";
   setBlockCenter( div);
   div.onmousedown = function() { initDragging(this); };
  }
 });
}

function closeHelp()
{
 div = document.getElementById("leaveHelp");
 if ( div) {
  document.body.removeChild(div);
 }
}

var W3CDOM = (document.createElement && document.getElementsByTagName);
function initFileUploads() {
	if (!W3CDOM) return;

 /* FIREFOX does not render this optimal */
 zi = ( navigator.userAgent.toUpperCase().indexOf("FIREFOX") >= 0)?1:3;
 ie = ( navigator.userAgent.toUpperCase().indexOf("MSIE") >= 0);

	var fakeFileUpload = document.createElement('div');
 fakeFileUpload.style.position = "absolute";
 fakeFileUpload.style.top = "0px";
 fakeFileUpload.style.left = "0px";
 fakeFileUpload.style.zIndex = zi;
 var i = document.createElement('input');
// i.style.border = "1px solid gray";
// i.style.paddingLeft = "20px";
 i.readOnly = true;
 i.tabIndex = -1;
	fakeFileUpload.appendChild(i);

	var fakeFileUploadImg = document.createElement('div');
 fakeFileUploadImg.style.position = "absolute";
 fakeFileUploadImg.style.top = "0px";
 fakeFileUploadImg.style.left = "0px";
 fakeFileUploadImg.style.zIndex = 1;
	var image = document.createElement('img');
 image.style.position = 'relative';
 image.style.top = '2px';
 image.style.left = '10px';
	fakeFileUploadImg.appendChild(image);


	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].getAttribute('customize') != 'yes') continue;
  iw = x[i].offsetWidth;

		var clone1 = fakeFileUpload.cloneNode(true);
  clone1.style.width = eval(iw) + "px";
  inp = clone1.getElementsByTagName('input')[0];
  inp.value = x[i].value;
  inp.className = x[i].className;
  inp.style.width = eval(iw - 28) + "px";

		var clone2 = fakeFileUploadImg.cloneNode(true);
  clone2.style.left = eval(iw - 28) + "px";
  if ( x[i].getAttribute('image')) {
   img = clone2.getElementsByTagName('img')[0];
   if ( img) {
    img.src = "images/icons/"+x[i].getAttribute('image');
    img.height = 16;
    img.width = 16;
   }
  }

  x[i].parentNode.style.position = "relative";
 	x[i].style.opacity = 0;
  x[i].style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
	 x[i].style.position = "relative";
  x[i].style.zIndex = 2;
  x[i].style.cursor = "pointer";
  x[i].setAttribute("notification","Browse for a file");
		x[i].parentNode.appendChild(clone1);
		x[i].parentNode.appendChild(clone2);
		x[i].relatedElement = clone1.getElementsByTagName('input')[0];
		x[i].onchange = function () {
			this.relatedElement.value = this.value;
		}
		x[i].relatedElement.value = x[i].value;
	}
}

function setShadowField( fld)
{
	var shadowField = document.createElement('div');
 shadowField.style.position = "absolute";
 shadowField.style.top = "0px";
 shadowField.style.left = "0px";
// shadowField.style.zIndex = -1;
 var i = document.createElement(fld.nodeName);
 i.style.zIndex = -1;
 i.style.color = "#ccc";
 i.tabIndex = -1;
// i.readOnly = true;

// i.disabled = true; // Disabling prevents handling events in IE
 i.setAttribute( "notification", fld.getAttribute("notification"));
	shadowField.appendChild(i);

 iw = fld.offsetWidth-6;
 ih = fld.offsetHeight-6;

	var clone1 = shadowField.cloneNode(true);
 clone1.style.width = eval(iw) + "px";
 inp = clone1.getElementsByTagName(fld.nodeName)[0];
 inp.className = fld.className+" shadowfield noprint";
 inp.onclick = function() { this.blur(); this.parentNode.parentNode.children[0].focus();};
 inp.ondblclick = function() {this.blur();};
 inp.style.width = eval(iw) + "px";
 inp.style.height = eval(ih) + "px";

 inp.style.backgroundColor = "#fff";
 inp.style.border = "1px solid #aaa";
 inp.style.padding = "2px";

 if ( fld.getAttribute('default')) {
  inp.value = fld.getAttribute('default');
 } else {
 }

 fld.parentNode.style.position = "relative";
	fld.style.position = "relative";
 fld.style.zIndex = 2;
 fld.style.backgroundColor = "transparent";
 fld.style.border = "none";
 fld.style.padding = "3px";
	fld.parentNode.appendChild( clone1);

 return( clone1.getElementsByTagName(fld.nodeName)[0]);
}

function initFileLinks()
{
	if (!W3CDOM) return;

 ie = ( navigator.userAgent.toUpperCase().indexOf("MSIE") >= 0);

	var x = document.getElementsByTagName('*');
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('filelink') != 'yes') continue;
   x[i].style.cursor = "pointer";
   x[i].linkname = x[i].getAttribute('linkname');
   x[i].onclick = function(e) {
    window.open( this.linkname);
   }
	}
}

function findValue( fld)
{
 if ( !fld) return;
 url = "searchdata.php?random="+Math.random()+"&find="+fld.getAttribute('find')+"&field="+fld.getAttribute('search')+"&return="+fld.getAttribute('return');
 getServerHtmlData( url, fld.shadowField, null );
}
function initAutofillFields() {
	if (!W3CDOM) return;

 ie = ( navigator.userAgent.toUpperCase().indexOf("MSIE") >= 0);

	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'text') continue;
		if (x[i].getAttribute('autofill') != 'yes') continue;
		if (!x[i].getAttribute('search')) continue;

   if (x[i].getAttribute('fill')) {
    fills = x[i].getAttribute('fill').split(",");
    if ( fills.length) {
     for (f=0;f<fills.length;f++) {
      fill = fills[f].split(".");
      fld = x[i].form.elements[ "details["+fill[fill.length-1]+"]"];
      if ( fld) {
       fld.setAttribute('search',x[i].getAttribute('search'));
       fld.setAttribute('return',fills[f]);
       fld.shadowField = setShadowField( fld);
      }
     }
    }
   }

  x[i].shadowField = setShadowField( x[i]);
		x[i].onkeydown = function (e) {
//		x[i].onkeyup = function (e) {
    if ( !e) e = window.event;
    ie = ( navigator.userAgent.toUpperCase().indexOf("MSIE") >= 0);
    fillvalues = false;
    if ((this.value.trim()!="") && (e.type == 'keydown') && (( e.keyCode == 13) || ( e.keyCode == 9))) {
     // Check if the entered value is the same as the found part
     if ( this.shadowField.value.substr( 0, this.value.length).toLowerCase() == this.value.toLowerCase()) {
      fillvalues = true;
      if ( ie) {
       e.keyCode = 9;
      }
     }
    }


    this.id = Math.random(); // Generate a (hopefully) unique ID to refer to
    this.setAttribute('find',this.value);
    if ( fillvalues) {
     this.value = this.shadowField.value;
     this.shadowField.value = "";
     if ( this.getAttribute('furtherfunction')) {
      if ( ie) {
       eval( this.furtherfunction);
      } else {
       eval( this.getAttribute('furtherfunction'));
      }
     }
    } else {
     window.setTimeout("findValue( document.getElementById('"+this.id+"'));",250);
    }
    if (this.getAttribute('fill')) {
     fills = this.getAttribute('fill').split(",");
     if ( fills.length) {
      for (f=0;f<fills.length;f++) {
       fill = fills[f].split(".");
       fld = this.form.elements[ "details["+fill[fill.length-1]+"]"];
       if ( fld) {
        fld.id = Math.random(); // Generate a (hopefully) unique ID to refer to
        fld.setAttribute('find',this.value);
        if ( fld.value.trim()=="") {
         if ( fillvalues) {
          fld.value = fld.shadowField.value;
          fld.shadowField.value = "";
         } else {
          window.setTimeout("findValue( document.getElementById('"+fld.id+"'));",250);
         }
        }
       }
      }
     }
    }
    return( true);
		}
   
   x[i].onkeyup = x[i].onkeydown;
	}
}

function initReadonlyFields()
{
	if (!W3CDOM) return;

 ie = ( navigator.userAgent.toUpperCase().indexOf("MSIE") >= 0);

	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if ( x[i].getAttribute('readonly')) {
    if ( x[i].type == 'checkbox') {
     x[i].onclick = function() { this.blur(); return(false); };
    }
    x[i].className = x[i].className+" readonly";
   }
	}
	var x = document.getElementsByTagName('textarea');
	for (var i=0;i<x.length;i++) {
		if ( x[i].getAttribute('readonly')) {
    x[i].className = x[i].className+" readonly";
   }
	}
}

