function calculatePrice(baseURL, position){
	
	
	var defaultDiv = 'priceBerekenen';
	var calculateDiv = 'priceBerekening';
	
	
	if(document.getElementById('berekenChek1').checked){
		userType = 1;
	}
	else if(document.getElementById('berekenChek2').checked){
		userType = 2;
	}
	else{
		userType = 3;
	}

	var users = '';
	
	if(position == 'sidebar'){
		users = document.getElementById('field1').value;	
	}
	else{
		users = document.getElementById('field5').value;	
	}
	
	/* what the hell does this do?
	var IsFound = /^-?\d+$/.test(users);
	
	if(!IsFound){
		return false;
	}
	*/
		
	userPrice = calculateUserPrice(users);
	percent = calculatePercent(users);
	percentPrice = calculatePercentPrice(userPrice,percent);
	totalprice = userPrice-percentPrice;
	yearLicentie = (totalprice/100)*20;
	
	userPrice = userPrice.toFixed(2);
	totalprice = totalprice.toFixed(2);
	yearLicentie = yearLicentie.toFixed(2);
	percentPrice = percentPrice.toFixed(2);
	
	userPrice = FormatNumberBy3(userPrice);
	totalprice = FormatNumberBy3(totalprice);
	yearLicentie = FormatNumberBy3(yearLicentie);
	percentPrice = FormatNumberBy3(percentPrice);
	
	document.getElementById(defaultDiv).style.display = 'none';
	document.getElementById(calculateDiv).style.display = '';
	
	document.getElementById(calculateDiv).innerHTML = '<div id="loadingDiv"><img src="img/loading.gif"/></div>';
	
	setTimeout(doneLoading, 1000);
	function doneLoading(){
		var randnum = Math.random()*11
		var url = baseURL+'calculateprice';
		var params = 'rand='+randnum+'&userprice='+userPrice+'&users='+users+'&percent='+percent+'&percentprice='+percentPrice+'&totalprice='+totalprice+'&licentieprice='+yearLicentie+'&usertype='+userType;
		
	    var myAjax = new Ajax.Updater(calculateDiv,url,{
	    	method: 'get', 
	    	parameters: params
	   });
	}
}

function calculateUserPrice(users){
	if(users <= 5){
		var userPrice = 140.00;
		return userPrice;
	}
	else{
		var userPrice = 140.00;
		users = users - 5;
		var amountUserPacks = Math.ceil(users / 5);	
		userPrice = userPrice+(amountUserPacks*150.00);		
		return userPrice;
	}
}

function calculatePercent(users){
	if(userType == 1){
		if(users > 99 && users < 250){
			percent = 5;
			return percent;
		}
		else if(users > 149 && users < 1000){
			percent = 10;
			return percent;
		}
		else if(users >= 1000){
			percent = 15;
			return percent;
		}
		else{
			percent = 0;
			return percent;
		}	
	}
	else if(userType == 2){
		percent = 25;
		return percent;
	}
	else if(userType == 3){
		if(users < 50){
			percent = 25;
			return percent;
		}
		else if(users >= 50 && users < 100){
			percent = 50;
			return percent;
		}
		else if(users >= 100){
			percent = 75;
			return percent;
		}
	}
}

function calculatePercentPrice(userPrice, percent){
	percentPrice = (userPrice/ 100)*percent;
	return percentPrice;
}

function calculateAgain(){
	var defaultDiv = 'priceBerekenen';
	var calculateDiv = 'priceBerekening';
	
	document.getElementById(defaultDiv).style.display = '';
	document.getElementById(calculateDiv).style.display = 'none';
}

function FormatNumberBy3(num, decpoint, sep) {
  // check for missing parameters and use defaults if so
  if (arguments.length == 2) {
    sep = ",";
  }
  if (arguments.length == 1) {
    sep = ",";
    decpoint = ",";
  }
  // need a string for operations
  num = num.toString();
  // separate the whole number and the fraction if possible
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";


  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return x;
}