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) {
  var el = document.getElementById("testArea");
  donateForm.amount.value = donateForm.amount.value.split(' ').join('');
  donateForm.amount.value = donateForm.amount.value.replace (/^\$|\u00A3|\u20AC|,/g,"");

  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;
	  }
	}
  }
  
  if ((donateForm.M_giftAid[0].checked) && (isBlank(donateForm.postcode.value))) {
	alert ("You have selected giftaid but not provided a postcode.");
	return false;
  }
  
}

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_message.value.length > 0)&&(!donateForm.M_anonymous.checked)) {
	donateForm.M_anonymous.checked = true;
	alert ("If you leave a message then your name will be displayed.\nIf you wish to uncheck this box then please clear the message field first.");
  }
  if ((donateForm.M_displayDonation.checked)&&(!donateForm.M_anonymous.checked)) {
	donateForm.M_anonymous.checked = false;
	donateForm.M_displayDonation.checked = false;
//	alert ("If you want to display your donation you must also display your name.");
  }
}

function checkDonationDisplay (donateForm) {
  if ((donateForm.M_displayDonation.checked)&&(!donateForm.M_anonymous.checked)) {
	donateForm.M_anonymous.checked = true;
	alert ("If you want to display your donation you must also display your name.");
  }
}


function checkMessageLength (donateForm) {
  if ((donateForm.M_message.value.length >= 160)&&(!warnedAlready)) {
	 alert ("Messages over 160 characters will be truncated");
	 warnedAlready = true;
  }
  if (donateForm.M_message.value.length > 1) {
	  donateForm.M_anonymous.checked = true;
  }
}

function changeDonationCurrency (currencyDropdown) {
  var el = document.getElementById("currSymbol");
  if ( el.style.display == 'none' ) {	el.style.display = 'block';}
  if (currencyDropdown.value == "GBP") {
	el.innerHTML = '&pound;&nbsp;';
  } else if (currencyDropdown.value == "AUD") {
	el.innerHTML = '$&nbsp;';
  } else if (currencyDropdown.value == "USD") {
	el.innerHTML = '$&nbsp;';
  } else if (currencyDropdown.value == "EUR") {
	el.innerHTML = '&euro;&nbsp;';
  }
}

