var warnedAlready = false;

function isBlank(s)
{
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

function checkForm (donateForm) {
  if (donateForm.M_message.value.length > 160) {
	donateForm.M_message.value = donateForm.M_message.value.slice(0,160);
	confirmMsg = "Your message was longer than 160 characters and has been truncated to:\n";
	confirmMsg += donateForm.M_message.value + "\n";
	confirmMsg += "Click OK if you're happy with the message or click CANCEL to alter it";
	if (!window.confirm(confirmMsg)) {
		warnedAlready = false;
		return false;
	}
  }
  if ((donateForm.amount.value=="")||(isBlank(donateForm.amount.value))) {
	alert ("Please fill in a donation amount");
	return false;
  } else {
	if (donateForm.currency.options[donateForm.currency.selectedIndex].value=="GBP") {
	  if ((!donateForm.M_giftAid[0].checked)&&(!donateForm.M_giftAid[1].checked)) {
		alert ("Please select a giftaid option.");
		return false;
	  }
	} else {
	  if ((!donateForm.M_giftAid[0].checked)&&(!donateForm.M_giftAid[1].checked)) {
		donateForm.M_giftAid[1].checked = true;
	  }
	}
  }
}

function checkTax (donateForm) {
  if ((donateForm.M_giftAid[0].checked)&&(donateForm.M_anonymous.checked)) {
	donateForm.M_giftAid[1].checked = true;
	alert ("In order for us to reclaim tax on your donation we need your personal details.\nTo allow us to reclaim tax please clear the Privacy checkbox before clicking here again");
  }
}

function checkAnon (donateForm) {
  if ((donateForm.M_giftAid[0].checked)&&(donateForm.M_anonymous.checked)) {
	donateForm.M_anonymous.checked = false;
	alert ("In order for us to reclaim tax on your donation we need your personal details.\nTo remain anonymous please clear the gift aid checkbox before clicking here again");
  }
}

function checkMessageLength (donateForm) {
  if ((donateForm.M_message.value.length >= 160)&&(!warnedAlready)) {
	 alert ("Messages over 160 characters will be truncated");
	 warnedAlready = true;
  }
}

