var amountIsSet = false;
var customAmountIsSet = false;
var payIsSet = false;

function getForm() {
	return document.forms[0];
}


function customA(){
	uncheckRadioBtns();
	
	if (!isNaN(document.getElementById('customAmount').value))
		customAmountIsSet = true;

	if ((document.getElementById('customAmount').value) == "0")
		customAmountIsSet = false;
}


function uncheckRadioBtns() {
	document.getElementById('amount0').checked = "";
	document.getElementById('amount1').checked = "";
	document.getElementById('amount2').checked = "";
}

function set(e) {
	if (e.name == "amount") {
		amountIsSet = true;
		customAmountIsSet = false;
		getForm().customAmount.value = '';
	}

	if (e.name == "paymentMethod") {
		payIsSet = true;
	} 
}

function forceNumbers(e) {
	if (numCheck(e)) {
		customAmountIsSet = true;
		// ok
	} else {
		customAmountIsSet = false;
		e.value = '';
	}
}

function numCheck(e) {
	var numRegexp = /^[0-9]+$/;
	var form = getForm();
	
	if (!numRegexp.test(e.value) || (e.value.charAt(0) == "0")) return false;
	else return true;
}

function setError(str) {
	var p = document.getElementById("errorMsg");		
	p.innerHTML = str;
	p.style.visibility = "visible";
}

function validAmount() {
	if (!amountIsSet) {
		if (customAmountIsSet) {
			return true;
			// ok, custom amount is set
		}	else {
			setError("Var god välj ett antal företag");
			return false;
		}
	} else {
		return true;
		// ok, amount radio is set
	}
}

function submitForm1() {
	if (validAmount()) {
		var form = getForm();
		form.submit();
	}
}

function submitForm2() {
	var form = getForm();
	var emailRegexp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var nameRegexp = /^[a-zA-Z ]+$/;
	
	if (form.recipientFirstname.value == '') {
			setError("Var god ange ditt förnamn");
			return false;
	}

	if (form.recipientLastname.value == '') {
			setError("Var god ange ditt efternamn");
			return false;
	}

	if (!emailRegexp.test(form.recipientEmail.value)) {
			setError("Var god ange din e-mailaddress");
			return false;
	}

	if (!payIsSet) {
		setError("Var god välj en betalmetod");
		return false;
	}	else {
		// allt är korrekt
	}

	form.submit();
}


