genButtons();

function genButtons() {
	//Populates some radio buttons with suggested donation amounts.
	var suggested_amounts = [10, 25, 50, 100, 500];
	var default_button = 1;
	
	var other_amount = document.getElementById("other_amount");
	var amountinput = document.getElementById("item_price_1");
	
	if (!other_amount)
		return;
		
	for (var amtidx = 0; amtidx < suggested_amounts.length; amtidx++) {
		var newlistelem = document.createElement("li");
		var newlabel = document.createElement("label");
		var newradio = document.createElement("input");
		newradio.type = "radio";
		newradio.name = "custom_amount";
		newradio.value = suggested_amounts[amtidx];
		newradio.onclick = setAmount;
			
		newlabel.appendChild(newradio);
		newlabel.appendChild(document.createTextNode('$' + suggested_amounts[amtidx]));
		
		newlistelem.appendChild(newlabel);
		other_amount.parentNode.insertBefore(newlistelem, other_amount);
		
		if (amtidx == default_button) {
			newradio.checked = true;
			amountinput.value = newradio.value;
		}
	}
	
	other_amount.style.display = "inline";
	other_amount.style.visibility = "visible";
	other_amount.onclick = function () { amountinput.focus(); }
}

function setAmount() {
	amountinput.value = this.value;
	return true;
}

function validateAmount(amount){		
	if(!amount.value.match( /^[0-9]+(\.([0-9]+))?$/)){
		alert('You must enter a valid donation.');
		amount.focus();
		return false;
	}
	else if (parseFloat(amount.value) > parseFloat(document.getElementsByName("item_max_price_1")[0].value)){
		alert('Google Checkout only supports donations up to $' + parseFloat(document.getElementsByName("item_max_price_1")[0].value) + '. To donate more at once, please contact us.');
		amount.focus();
		return false;
	}
	
	return true;
}
