window.addEvent('domready',function() {
  /* -------------------- */
  if (location.href.test("online-bestellen")) {
    var myCalc = new priceCalculator($E('table[title=products]'),$('mp_mainContent_lblTotal'));
    
    $$('.aantal').addEvent('keyup',function() {myCalc.CalcTotal(this);});
  }
  
  /* -------------------- */
  if (location.href.test("backoffice")) {
    // Confirm item delete
    $$('a[title=Verwijderen]').addEvent('click',function(e) {
      var ev = new Event(e);
      
      var c = confirm('Weet u zeker dat u dit item wilt verwijderen?');
      if (c == false) {
        ev.stop();
      }
    });
    
    /* -------------------- */
    // Check for required fields
    if ($('aspnetForm')) {
      // punyMce Init
      var editor1 = new punymce.Editor({
        id:'ctl00_mainContent_description',
        plugins:'Link',
        toolbar:'bold,italic,underline,strike,ul,ol,link,unlink'
      });
    
      $('aspnetForm').addEvent('submit',function(e) {
        var ev = new Event(e);
        var check = false;
        
        $$('#aspnetForm .reqFld').each(function(fld) {
          if (fld.hasClass('emptyFld')) fld.removeClass('emptyFld');
          if (fld.get('value') == '') {
            fld.addClass('emptyFld');
            check = true
          }
        });
        
        if (check == true) {
          ev.stop();
          alert('Niet alle velden zijn ingevuld');
        }
      });
    }
  } else { // Frontoffice
    /* -------------------- */
    // Header images
    rndHeaderImg.periodical(5000);
    

    /* -------------------- */
    new imgBox({
      overlay_opacity:'0.45',
      showControls:false,
      thumb_url:'/_userdata/_thumb/',
      image_url:'/_userdata/',
      title_color:'#000000'
    });
  }
});

/* -------------------- */
// Class priceCalculator
// version: 0.1
// copyright 2008 02 08 - Huug Helmink & Jack Mondt, Ace Group bv
//
// Calculates the total price in an order form
// requires mootools 1.2b2
// - Core(Core, Browser)
// - Native(Array, Function, Number, String, Hash, Event)
// - Class(Class, Class.Extras)
// - Element(Element, Element.Event)
// - Selectors(Selectors)
// - Utilities(DomReady)
/* -------------------- */
var priceCalculator = new Class({
  Implements: Options,
  options: {
    currencySign: '&euro;'
  },

  initialize: function(productTable,totalPriceContainer,options) {
    this.setOptions(options);
    
    this.TotalPrice = 0;
    this.Amount = '';
    this.Price = '';
    this.productsTable = productTable;
    this.priceContainer = totalPriceContainer
  },
  
  CalcTotal: function(t) {
    var aant = t.value;
    aant = this.GetOnlyNumbers(aant);
    t.value = aant;
    
    this.TotalPrice = 0;
    this.GetTotal(this.productsTable);
    this.TotalPrice += this.GetSum(this.Amount,this.Price);
    this.Amount = '';
    this.Price = '';
    
    this.priceContainer.set('html','<strong>Totaal:</strong> ' + this.options.currencySign + ' ' + this.TotalPrice.toFixed(2).toString().replace(/\./, ','));
    if ( document.getElementById('mp_mainContent_lblMessage') )
    {
        var msg = document.getElementById('mp_mainContent_lblMessage');
        msg.removeAttribute('class');
        msg.setAttribute('class','hidden');
    }
  },
  
  GetTotal: function(element) {
    var nName;

    element.getChildren().each(function(child) {
  		nName = child.get('tag');
  		
  		if (nName == 'tr') {
        this.TotalPrice += this.GetSum(this.Amount,this.Price);
        this.Amount = '';
        this.Price = '';
  		}
  		
      if (child.getProperty('title') == 'prijs') this.Price = child.childNodes[0].nodeValue;
      if (child.getProperty('title') == 'aantal') this.Amount = child.get('value');

  		this.GetTotal(child);
    },this);
  },
  
  GetOnlyNumbers: function(amount) {
    var newAmount = '';

    for (var i=0; i<amount.length; i++) {   
      aChar = amount.substring(i,i+1);
      if ((aChar >= '0') && (aChar <= '9')) newAmount += aChar;
    }
    return newAmount;
  },

  GetSum: function(amount,price) {
    var sum = 0;
    if ((amount != '') && (price != '')) sum = price.replace(',','.').toFloat() * amount;
    
    return sum;
  }
});

/* -------------------- */
function CheckMinAmount(minamount,t) {
  if (minamount > 0) {
    var amount = t.value;
    if (amount != '') {
      if (amount < minamount) {
        alert('Het minimum aantal is ' + minamount );
      }
    }
  }
}

/* -------------------- */
// Random images in header
var rndHeaderImg = function() {
  var rndLargeItems = $$('.rndimgtop');
  var rndSmallItems = $$('.rndimgbottom');
  var imgArray = [];
  var numberPrefix = '';
  var maxPics = 88;
  
  // Array of images which can by displayed
  for(i=1;i<=maxPics;i++) {
    if(i<maxPics) numberPrefix = '';
    if(i<100) numberPrefix = '0';
    if(i<10) numberPrefix = '00';
    
    imgArray.include('image' + numberPrefix + i + '.jpg');
  }

  // Add random pictures
  rndLargeItems.each(function(el) {
    el.setProperty('src','/_images/_rndLarge/' + imgArray.getRandom());
  });
  rndSmallItems.each(function(el) {
    el.setProperty('src','/_images/_rndSmall/' + imgArray.getRandom());
  });
}