function number_format(a, b, c, d) {
	 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	 e = a + '';
	 f = e.split('.');
	 if (!f[0]) {
	  f[0] = '0';
	 }
	 if (!f[1]) {
	  f[1] = '';
	 }
	 if (f[1].length < b) {
	  g = f[1];
	  for (i=f[1].length + 1; i <= b; i++) {
	   g += '0';
	  }
	  f[1] = g;
	 }
	 if(d != '' && f[0].length > 3) {
	  h = f[0];
	  f[0] = '';
	  for(j = 3; j < h.length; j+=3) {
	   i = h.slice(h.length - j, h.length - j + 3);
	   f[0] = d + i +  f[0] + '';
	  }
	  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
	  f[0] = j + f[0];
	 }
	 c = (b <= 0) ? '' : c;
	 return f[0] + c + f[1];
}

var alreadyrunflag=0 //flag to indicate whether target function has already been run

if (document.addEventListener)
  document.addEventListener("DOMContentLoaded", function(){alreadyrunflag=1; replaceTargets()}, false)
else if (document.all && !window.opera){
  document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>')
  var contentloadtag=document.getElementById("contentloadtag")
  contentloadtag.onreadystatechange=function(){
    if (this.readyState=="complete"){
      alreadyrunflag=1
      replaceTargets();
    }
  }
}

window.onload=function(){
  setTimeout("if (!alreadyrunflag) walkmydog()", 0)
}

function replaceTargets(){

  links = document.getElementsByTagName('a');
  
  for(i = 0; i < links.length; i++){
  	if(links[i].getAttribute('rel') == 'external'){
  		links[i].target = '_Blank';
  	}
  }

}

function changeBg(name) {
	document.getElementById('tab'+name).style.backgroundImage = "url('images/tabhover.jpg')";
	document.getElementById('link'+name).style.color ="White";
}
function changeBgBack(name) {
	document.getElementById('tab'+name).style.backgroundImage = "";
	document.getElementById('link'+name).style.color ="Black";
}

function MM_openBrWindow(theURL,winName,features)
{ //v2.0
          window.open(theURL,winName,'statusbar=yes,scrollbars=yes');
}

function isValidEmail(str) {
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
} 

function addItem(id)
{
        count = 1;
        options = '';
        alert('Het item is aan uw winkelwagen toegevoegd.');
        new Ajax.Request('includes/shoppingcart/additem.php',  { method: 'post', parameters: 'id='+id+'&count='+count+'&options='+options, onSuccess: function(transport) {
            $('winkelwagen_count').innerHTML = transport.responseText;
          }}); 
        hideLightbox();
}

function emptyShoppingCart()
{
        new Ajax.Request('http://www.salaris.nl/includes/shoppingcart/emptycart.php',  { method: 'post'});
        $('shoppingcart_items').style.display = 'none';
        $('winkelwagen_count').innerHTML = '0';
        $('emptycartlink').innerHTML = 'Er bevinden zich nog geen producten in uw winkelwagen';
        $('form_search').style.display = 'block';
}

function updateAantalCart(id)
{
        
        aantal = $('aantal['+id+']').value;
        new Ajax.Request('http://www.salaris.nl/includes/shoppingcart/updateaantalcart.php',  { method: 'post', parameters: 'id='+id+'&aantal='+aantal, onSuccess: function(transport){
                
                var result = transport.responseText.evalJSON();
                $('total_incl_vat_verzendkosten').innerHTML = '&euro; ' + result.total_prijs_incl_vat_verzendkosten;
                $('total_price'+id).innerHTML = '&euro; ' + result.total_prijs_item;
                $('total_incl_vat').innerHTML = '&euro; ' + result.total_prijs_incl_vat;
                
                
        }});
}

function updateGegevens(id)
{
        var adresForm = document.forms['adresgegevens'];
        aantal_items = adresForm.elements.length;
        var parameter_string = '';
        for(i = 0; i < aantal_items; i++)
        {
                
                if(i == 0)
                        parameter_string = adresForm.elements[i].name + '=' + adresForm.elements[i].value;
                else if(adresForm.elements[i].value != '')
                        parameter_string = parameter_string + '&' +  adresForm.elements[i].name + '=' + adresForm.elements[i].value;
        }

        new Ajax.Request('http://www.salaris.nl/includes/shoppingcart/updategegevens.php',  { method: 'post', parameters: parameter_string, onSuccess: function(transport){
                var result = transport.responseText.evalJSON();
                $('verzendkosten').innerHTML = '&euro; ' +  result.verzendkosten;
                $('total_incl_vat_verzendkosten').innerHTML = '&euro; ' + result.total_prijs_incl_vat_verzendkosten;
        }
        });
}

function controleerGegevens()
{
        var message  = '';
        check = true;
        var adresForm = document.forms['adresgegevens'];
        aantal_items = adresForm.elements.length;
        var parameter_string = '';
        for(i = 0; i < aantal_items; i++)
        {
                if(adresForm.elements[i].name != 'telefoon' && adresForm.elements[i].name != 'email' && adresForm.elements[i].value == 'land_anders' && adresForm.elements[i].value == '')
                {
                        check = false;
                        message = message + '\nHet veld ' + adresForm.elements[i].name + ' is niet ingevuld';
                        adresForm.elements[i].className = 'error';
                        adresForm.elements[i].focus();
                }
                if(adresForm.elements[i].name == 'email' && ! isValidEmail(adresForm.elements[i].value) )
                {
                        check = false;
                        message = message + '\nU heeft geen geldig emailadres ingevuld';
                                
                }
        }
        if(check == false)
                alert(message);
        else
                adresForm.submit();
}

var prijs = 0.00;

function addPrice(add, id){
	if(document.getElementById(id).checked){
		prijs += add;
	}else {
		prijs -= add;
	}
	updatePrice();
}

function updatePrice(){
	select = document.getElementById('periode');
	periode = parseInt(select.options[select.selectedIndex].value);
	if(periode >= 6 && periode < 12){
		periode -= 2;
	}else if(periode >= 12){
		periode -= 4;
	}
	totaal = prijs*periode;
	document.getElementById('maand').innerHTML = number_format(prijs,2, ',', '.');
	document.getElementById('totaal').innerHTML = number_format(totaal,2, ',', '.');
}