function addBasket(name, price) {
	
	var adoption = document.getElementById('adoption_total');
	var donation = document.getElementById('donation');
	var items = document.getElementById('basketItems');
	
	if(items.value.indexOf(name) == -1) {
	
		price = Number(price);
		
		var current_total = adoption.value;
		
		if(current_total != '' && current_total != '0') {
		
			current_total = Number(adoption.value);
		
		} else {
		
			current_total = 0;
		
		}
		
		var new_value = current_total+price;
		
		new_value = numberFormat(new_value, 2);
		
		adoption.value = new_value;
		
		basketTotal(adoption.value, donation.value); 
		
		updateItems(name);
	
	} else {
	
		alert('You have already sponsored this animal.');
	
	}
	
	return false;
	

}

function updateItems(name) {

	var items = document.getElementById('basketItems');
	
	items.value = items.value + '/' + name;
	
	return true;

}

function basketTotal(adopt, donate) {

	var adopt = Number(adopt);
	var donate = Number(donate);
	
	var new_total = adopt+donate;
	
	new_total = numberFormat(new_total, 2);
	
	var total = document.getElementById('total');
	
	total.value = new_total;

}

function addDonation() {

	var adoption = document.getElementById('adoption_total');
	var donation = document.getElementById('donation');
	
	basketTotal(adoption.value, donation.value); 

}

function numberFormat(number, decimalPlaces) {

	var formatted = '';
	
	var number = String(number);
	
	var bits = number.split('.');
	
	formatted += bits[0] + '.';
	
	if(bits.length > 1) {
	
		var afterPoint = bits[1].substr(0, decimalPlaces);
		
		if(afterPoint.length < decimalPlaces) {
		
			afterPoint = increaseDecimalPlaces(afterPoint, decimalPlaces);
		
		}
		
		formatted += afterPoint;
	
	} else {
	
		formatted += '00';
	
	}
	
	return formatted;

}

function increaseDecimalPlaces(afterPoint, decimalPlaces) {

	var point = afterPoint;

	for(var i=afterPoint.length; i<decimalPlaces; i++) {
	
		point += 0;
	
	}
	
	return point;

}

function loadFunctions() {

	var total = document.getElementById('total');
	
}

window.onload = loadFunctions;

