function OpenMenu( Opener, SubMenuID ){
  var Element = document.getElementById( SubMenuID );
  if ( Element ){
    var Pos = GetPosition( Opener );
    var Pos2 = GetPosition( Opener.parentNode );
    if ( Element.style.display != 'block' ){
      Element.style.display = 'block';
      Element.style.left = Pos.x - Pos2.x + 'px';
      Element.style.top = Pos.y - Pos2.y + 'px';
      Element.Closer = null;
      Opener.onmouseout = function() { OnMouseOut( Opener, Element ) };
      Element.onmouseover = function(){clearTimeout(Element.Closer)};
      Element.onmouseout = function() { OnMouseOut( Opener, Element ) };
      for( i = 0; i < Element.childNodes.length; i ++ ){ 
        Element.childNodes[ i ].onmouseover = function(){clearTimeout(Element.Closer)};
        for( j = 0; j < Element.childNodes[ i ].childNodes.length; j ++ ){
          if ( Element.childNodes[ i ].childNodes[ j ].tagNode == 'A' ){
            Element.childNodes[ i ].childNodes[ j ].onmouseover = function(){clearTimeout(Element.Closer)};
          }
        }
      }
    }
  }
}

function OnMouseOut( Opener, Element ){ 
  CloseMenu( Element );  
}

function CloseMenu( Element ){
  clearTimeout( Element.Closer );
  Element.Closer = setTimeout( function(){
    Element.style.display = 'none';
  }, 1000 );
}

function GetPosition( element ) {
  var Pos = {x:0,y:0};
  var absoluteAncestor = false;

  while ( element.offsetParent ) {

    Pos.y += element.offsetTop - element.scrollTop;
    Pos.x += element.offsetLeft - element.scrollLeft;

    element = element.offsetParent;

    if ( element.nodeName.toLowerCase() != 'html' ) {
      if ( element.currentStyle ) {
        if (element.currentStyle[ 'position' ] == 'absolute')
          absoluteAncestor = true;
      } else {
        if ( window.getComputedStyle ){
          if ( document.defaultView.getComputedStyle(element,null).getPropertyValue( 'position' ) == 'absolute' ){
            absoluteAncestor = true;
          }
        }
      }
    }
  }

  if ( ! absoluteAncestor ){
    var Body = document.getElementsByTagName( 'BODY' ).item( 0 );
    Pos.y += Body.offsetTop;
    Pos.x += Body.offsetLeft;
  }

  return Pos;
}


function SectorImageTo( Element, newURL ){

  var SectorImage = document.getElementById( 'SectorImage' );

  if ( SectorImage ){
    if ( ! SectorImage.Original ){
      SectorImage.Original = SectorImage.src;
    }

    if ( ! Element.onmouseout ){
      Element.onmouseout = function(  ){
        SectorImageTo( Element, SectorImage.Original );
      }
    }
    SectorImage.src = newURL;
  }

}

function RemoteSend( url, variables ){
  this.url = url;
  this.vars = '';
  for( var i in variables ){
    this.vars += ( this.vars ? "&" : "" ) + i + '=' + variables[ i ];
  }
  this.xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
  this.OnReady  = function(){};
}

RemoteSend.prototype.Excecute = function() {
  var ReadyFunction = this.OnReady;
  xmlhttp = this.xmlhttp;
  xmlhttp.open("POST", this.url, true);
  xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xmlhttp.onreadystatechange = function(){
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      response = xmlhttp.responseText;

      if (typeof(response) == "undefined")
        return;
      ReadyFunction( eval( "(" + response + ")" ) );
    }
  }
  xmlhttp.send(this.vars);
}

function LoginUsingNr( Element ){
  var Nr = Element.value;
  var Send = new RemoteSend( AbsPath + 'includes/SearchRelatie.php', {Value:Nr} );
  Send.OnReady = function( Response ){
    if ( Response.Ok ){
      Element.style.borderColor = 'green';
    } else {
      Element.style.borderColor = 'red';
    }
    var Lijst = document.getElementById( 'BedrijfInvoerLijst' );
    if ( Lijst ) {
      Lijst.className = Response.Ok ? 'RelatieGegevens Invoeren GegevensBekend' : 'RelatieGegevens Invoeren';
    }
  };
  Send.Excecute();
}

var SearchTimeout = null;
var SearchOk = false;

function InitProductSearch( Element, TaalID ){
  Element.onkeyup = function(){
    if ( Element.value ){
      SearchTimeout = clearTimeout( SearchTimeout );
      SearchOk = true;
      SearchTimeout = setTimeout( function(){ DoeSearch( Element, TaalID ) }, 500 );
    } else {
      ClearProductSearch();
    }
  }
  Element.onkeyup();
//  Element.onblur = function(){
//    ClearProductSearch();
  //}
}

function ClearProductSearch(){
  var ResultsList = document.getElementById( 'ProductSearchResults');
  if ( ResultsList ){
    ResultsList.parentNode.removeChild( ResultsList );
  }
  SearchOk = false;
  SearchTimeout = clearTimeout( SearchTimeout );
}

function DoeSearch( Element, TaalID ){
  if ( SearchOk ){
    var Send = new RemoteSend( Element.form.action, {Taal:TaalID,Keyword:Element.value} );
    Send.OnReady = function( Response ){ ProductSearchResults( Element, Response ) };
    Send.Excecute();
  }
  SearchOk = false;
  SearchTimeout = clearTimeout( SearchTimeout );
}


function ProductSearchResults( Element, Response ){
  var ResultsList = document.getElementById( 'ProductSearchResults');
  if ( ! ResultsList ){
    ResultsList = document.createElement( 'ul' );
    ResultsList.id = 'ProductSearchResults';
    Element.parentNode.appendChild( ResultsList );    
  }
  if ( ResultsList ){
    ResultsList.innerHTML = '';
    for( var i in Response.Resultaten ){
      var li = document.createElement( 'li' );
      var a = document.createElement( 'a' );
      a.href = Response.Resultaten[ i ].Href; 
      a.innerHTML = Response.Resultaten[ i ].Label;
      li.appendChild( a );
      ResultsList.appendChild( li );
    }
  }
}

var IC_Photo_DarkLayer = null;
var IC_Photo_Popup_referer = null;

var IC_Photo_Settings = {
  MarginToBorders: 10,
  Title: 'Klik hier om te sluiten',
  CloseTitle: 'x',
  UseAltTag: true,
  UseCloseTag: true,
  UseDarkLayer: true,
  Padding: 15
};

function IC_Photo_Popup( Link, URL, Width, Height, Alt ){
  var Body = document.getElementsByTagName( 'body' ).item( 0 );
  var Html = document.getElementsByTagName( 'html' ).item( 0 );

  if ( ! IC_Photo_DarkLayer && IC_Photo_Settings.UseDarkLayer ){
    IC_Photo_DarkLayer = document.createElement( 'div' );
    IC_Photo_DarkLayer.id = 'IC_Photo_DarkLayer';
    Body.appendChild( IC_Photo_DarkLayer );

    IC_Photo_DarkLayer.UpdateResize = function(){
      DLHeight = Math.max( Body.offsetHeight, Html.offsetHeight, Top + IC_Photo_Popup_referer.offsetHeight );
      IC_Photo_DarkLayer.style.height = DLHeight + 'px';
    }

    if (document.all){
      document.body.onresize = IC_Photo_DarkLayer.UpdateResize;
    } else {
      if (window.addEventListener) {
      	window.addEventListener("resize", IC_Photo_DarkLayer.UpdateResize, false);
      } else {
        window.attachEvent("resize", IC_Photo_DarkLayer.UpdateResize);
      }
    }
  }

  if ( IC_Photo_Popup_referer == null ){
    IC_Photo_Popup_referer = document.createElement( 'a' );
    IC_Photo_Popup_referer.id = 'IC_Photo_Popup_Holder';
    IC_Photo_Popup_referer.onclick = IC_Photo_Popup_Close;
    IC_Photo_Popup_referer.href = '#';
    IC_Photo_Popup_referer.title = IC_Photo_Settings.Title;

    if ( IC_Photo_Settings.UseAltTag ){
      var AltTag = document.createElement( 'span' );
      AltTag.className = 'AltLabel';
      AltTag.appendChild( document.createTextNode( Alt ) );
      IC_Photo_Popup_referer.appendChild( AltTag );
      IC_Photo_Popup_referer.Label = AltTag;
    }

    if ( IC_Photo_Settings.UseCloseTag ){
      var ATag = document.createElement( 'a' );
      ATag.className = 'CloseButton';
      ATag.href = '#';
      ATag.appendChild( document.createTextNode( IC_Photo_Settings.CloseTitle ) );
      IC_Photo_Popup_referer.appendChild( ATag );
    }
    Body.appendChild( IC_Photo_Popup_referer );
  }
  if ( IC_Photo_Popup_referer != null ) {
    IC_Photo_Popup_referer.style.display = 'block';

    IC_Photo_Popup_referer.style.height = ( Height + IC_Photo_Settings.Padding * 2 ) + 'px';
    IC_Photo_Popup_referer.style.width = ( Width + 30 ) + 'px';

    LabelHeight = 0;

    if ( IC_Photo_Settings.UseAltTag ){
      IC_Photo_Popup_referer.Label.innerHTML = Alt;

      if ( Alt != '' ){
        LabelHeight = IC_Photo_Popup_referer.Label.offsetHeight;

        IC_Photo_Popup_referer.style.height = ( Height + LabelHeight + IC_Photo_Settings.Padding * 2 ) + 'px';
      }
    }

    IC_Photo_Popup_referer.style.backgroundPosition = 'center ' + ( IC_Photo_Settings.Padding + LabelHeight ) + 'px';

    IC_Photo_Popup_referer.style.backgroundImage = 'url(' + URL + ')';

    IC_Image = null;
    for( i=0; i<Link.childNodes.length; i++ ){
      if ( Link.childNodes[ i ].tagName.toUpperCase() == 'IMG' ){
        IC_Image = Link.childNodes[ i ];
      }
    }
    if ( IC_Image ){
      Position = getPositionFrom( IC_Image );

      Left = ( Position.left + IC_Image.offsetWidth / 2 ) - IC_Photo_Popup_referer.offsetWidth / 2;

      Left = Math.min( Left, Body.offsetWidth - IC_Photo_Popup_referer.offsetWidth - IC_Photo_Settings.MarginToBorders );
      Left = Math.max( Left, IC_Photo_Settings.MarginToBorders );

      Top = ( Position.top + IC_Image.offsetHeight / 2 ) - IC_Photo_Popup_referer.offsetHeight / 2;
      WindowMax = Math.max( Body.offsetHeight, Html.offsetHeight );
      Top = Math.min( Top, WindowMax - IC_Photo_Popup_referer.offsetHeight - IC_Photo_Settings.MarginToBorders );
      Top = Math.max( Top, IC_Photo_Settings.MarginToBorders );

      IC_Photo_Popup_referer.style.top = Top + 'px';
      IC_Photo_Popup_referer.style.left = Left + 'px';

      if ( IC_Photo_Settings.UseDarkLayer ){
        IC_Photo_DarkLayer.style.display = 'block';
        IC_Photo_DarkLayer.UpdateResize();
      }
    }
  }
}


function IC_Photo_Popup_Close(){
  if ( IC_Photo_Popup_referer != null ){
    IC_Photo_Popup_referer.style.display = 'none';
  }
  if ( IC_Photo_Settings.UseDarkLayer && IC_Photo_DarkLayer != null ){
    IC_Photo_DarkLayer.style.display = 'none';
  }
  return false;
}

function getPositionFrom(element) {
  var Pos = {top:0,left:0};
  var absoluteAncestor = false;

  while ( element.offsetParent ) {

    Pos.top += element.offsetTop - element.scrollTop;
    Pos.left += element.offsetLeft - element.scrollLeft;

    element = element.offsetParent;

    if ( element.nodeName.toLowerCase() != 'html' ) {
      if ( element.currentStyle ) {
        if (element.currentStyle[ 'position' ] == 'absolute')
          absoluteAncestor = true;
      } else {
        if ( window.getComputedStyle ){
          if ( document.defaultView.getComputedStyle(element,null).getPropertyValue( 'position' ) == 'absolute' ){
            absoluteAncestor = true;
          }
        }
      }
    }
  }

  if ( ! absoluteAncestor ){
    var Body = document.getElementsByTagName( 'BODY' ).item( 0 );
    Pos.top += Body.offsetTop;
    Pos.left += Body.offsetLeft;
  }

  return Pos;
}

function OfferteChangeTotaal( Element, Prijs, idTotaal ){
  var Totaal = document.getElementById( idTotaal );
  var Aantal = parseInt( Element.value );
  var Aantal = Aantal ? Aantal : 0;
  if ( Totaal ){
    Totaal.innerHTML = money( Prijs * Aantal );
  }
}

function money ( value ){
  before = Math.floor( value );

  after = parseInt( Math.round( ( value%1 ) * 100 ) );

  if ( after == 0 ){
    after = '00';
  }

  if ( after < 10 && after > 0 ){
    after = '0' + after;
  }

  if ( before >= 1000 ){
    befA = Math.floor( before / 1000 );

    befB = Math.round( before - befA * 1000 ) + '';
    while( befB.length < 3 ){
      befB = '0' + befB;
    }


    before = befA + "." + befB;
  }

  return before + "," + after;
}

function DeleteRow( id ){
  var Element = document.getElementById( id );
  if ( Element ){
    Element.parentNode.removeChild( Element ) ;
  }
  var Element = document.getElementById( 'Label' + id );
  if ( Element ){
    Element.parentNode.removeChild( Element ) ;
  }
  return false;
}

function CartChangeTotaal( Element, Prijs, idTotaal ){
  var Totaal = document.getElementById( idTotaal );
  var Aantal = parseInt( Element.value );
  var Aantal = Aantal ? Aantal : 0;
  if ( Totaal ){
    Totaal.innerHTML = money( Prijs * Aantal );
  }
}

function HoverTr( Element ){
  Element.onmouseover = function(){
    Element.parentNode.className = 'Uitvoering Hover';
  };
  Element.onmouseout = function(){
    Element.parentNode.className = 'Uitvoering';
  };
  Element.onmouseover();
}

function SetOfferteRow( Index, Aantal ){
  var Send = new RemoteSend( AbsPath + 'includes/SpecialPages/Offerte/Change.php', {Index:Index,Aantal:Aantal} );
  Send.Excecute();
  return true;
}

