var continue_drawing = true;

function getSearchResults( field )
{
  if( field.value.length < 2 )
    return;

  var param = {
    sSearchQuery:field.value,
    nGroupID:2084
  }
  
  continue_drawing = false;
  pageTracker._trackPageview( '/productsearch/'+field.value );
  http( "post", "/shared/components/docdb.cfc?method=searchDocdb", outputSearchResults, param );
}

function outputSearchResults( r )
{
  var resultsObj = $( 'searchresult' );
  clearTbody( resultsObj );

  $("default").innerHTML = "";
  if( $("homepage_container") != null )
  {
    $("homepage_container").style.backgroundImage    = "url( " + s_WEBROOT + "/shared/images/main_body.jpg )";
    $("homepage_container").style.backgroundRepeat   = "no-repeat";
    $("homepage_container").style.backgroundPosition = "top";
    $("homepage_container").style.backgroundColor    = "#4599b4";
  }

  if($("shop"))
    $("shop").innerHTML = "";
  continue_drawing = true;

  for( i=0; i < r.product_nid.length; i++ )
  {
    if( !continue_drawing )
      return;

    newRow = document.createElement( 'tr' );

    nameColumn = document.createElement( 'td' );
    nameColumn.style.width="250px";
    nameColumn.innerHTML = "<a href=\""+s_WEBROOT+"/index.cfm?Detail=1&amp;fuseaction=default&amp;assetmetaAssetmeta_x_nChildID=29127&amp;product_nID="+r.product_nid[i]+"&amp;nExpandGroupID="+r.groep_nid[i]+"\">" + r.product_snaam[i] + "</a>";
    newRow.appendChild( nameColumn );

    groupColumn = document.createElement( 'td' );
    groupColumn.style.width="150px";
    groupColumn.innerHTML = r.groep_snaam[i];
    newRow.appendChild( groupColumn );

    priceColumn = document.createElement( 'td' );
    priceColumn.style.width="50px";
    priceColumn.style.textAlign = "right";
    priceColumn.innerHTML = "<span>&euro; " + currencyFormat( r.product_nprice[i] ) + "</span>";
    newRow.appendChild( priceColumn );
    
    resultsObj.appendChild( newRow );
  }
}

function clearTbody( tbody1 )
{
  while( tbody1.childNodes.length > 0 )
    tbody1.removeChild( tbody1.firstChild );
}

function currencyFormat( amount )
{
  var i = parseFloat( amount );
  var minus = '';

  if( isNaN( i ))
    i = 0.00;

  if( i < 0 )
    minus = '-';

  i = Math.abs( i );
  i = parseInt(( i + .005 ) * 100 );
  i = i / 100;
  s = new String( i );

  if( s.indexOf( '.' ) < 0 )
    s += '.00';

  if( s.indexOf( '.' ) == ( s.length - 2 ))
    s += '0';

  return minus + s.replace( '.', ',' );
}

