var x = -1; // used as loop counter.
var ExClearedInputs = new Array(); //Array that holds your previously cleared input fields names.

//window.onload = initForms;



function clearTextField(textField) {
	textField.value = "";
}

function clearNamedFields(fieldName) {
	var i;
	var ClearedBefore = false;	

	//searching the past cleared input field names array.
	for (i=0; i<=x; i++)
	{
	if (ExClearedInputs[i] == fieldName.name) {ClearedBefore = true;}
	}

	if (ClearedBefore != true) 
	{
	x++;
	ExClearedInputs[x]	= fieldName.name;	
	fieldName.value = "";
	}	
}

function changeFocusToOther() {
	document.donation_form.amtticked[3].checked = true;
}





function checkdonationdetails(form) {
	


if (form.M_reason_for_donation.value == "") {
    alert( "Please tell us why you're donating." );
    form.M_reason_for_donation.focus();
    return false ;
  }






if (form.M_title.value == "" || form.M_title.value == "Title") {
    alert( "Please tell us your title (e.g. Ms or Mr)." );
    form.M_title.focus();
    return false ;
  }

if (form.M_firstname.value == "" || form.M_firstname.value == "Name") {
    alert( "Please tell us your first name." );
    form.M_firstname.focus();
    return false ;
  }

if (form.M_surname.value == "" || form.M_surname.value == "Surname") {
    alert( "Please tell us your surname." );
    form.M_surname.focus();
    return false ;
  }


// Next section concatenates the name fields
form.name.value = form.M_title.value +" "+ form.M_firstname.value +" "+ form.M_surname.value;







if (form.M_address1.value == "") {
    alert( "Please tell us the name or number of your house." );
    form.M_address1.focus();
    return false ;
  }

if (form.M_address2.value == "") {
    alert( "Please tell us your street name." );
    form.M_address2.focus();
    return false ;
  }


if (form.M_address3.value == "") {
    alert( "Please tell us the name of your town or city." );
    form.M_address3.focus();
    return false ;
  }

// Next section concatenates the address fields for worldpay's address field
form.address.value = form.M_address1.value +" "+ form.M_address2.value +", "+ form.M_address3.value; +", "+ form.M_address4.value;

if (form.postcode.value == "") {
    alert( "Please tell us your postcode." );
    form.postcode.focus();
    return false ;
  }

// Next section concatenates the address fields for the confirmation email
form.M_fulladdress.value = form.M_address1.value +" "+ form.M_address2.value +", "+ form.M_address3.value +", "+ form.M_address4.value +", "+ form.postcode.value; +", "+ form.country.value; 




//this section checks the minimm lengh of the postcode
var postcodeminlength = 5;
if (form.postcode.value.length < postcodeminlength) {
	alert("Please amend your postcode as it must be at least " + postcodeminlength + " characters long.");
	form.postcode.focus();
	return false;
}



if (form.tel.value == "") {
    alert( "Please tell us your telephone number." );
    form.tel.focus();
    return false ;
  }


// This section checks that the phone number contains the correct characters
var telcharacters = /^[0-9\+\-\(\)\ ]+$/;
if (form.tel.value.match(telcharacters)){}else{
    alert("Please amend your telephone number as it may only contain the numbers 1-9 and the characters ( ) + -.");
	form.tel.focus();
    return false ;
}

// This section checks the munimum length of the phone number
var telminlength = 7;
if (form.tel.value.length < telminlength) {
	alert("Please amend your telephone number as it must be at least " + telminlength + " digits long.");
	form.tel.focus();
	return false;
}




if (form.email.value == "") {
    alert( "Please tell us your email address." );
    form.email.focus();
    return false ;
  }

if (form.emailb.value == "") {
    alert( "Please confirm your email address." );
    form.email.focus();
    return false ;
  }


// Next section validates that the email addresses match
if (form.email.value != form.emailb.value) {
    alert( "Please check that your email addresses match." );
    form.email.focus();
    return false ;
  }


// Next section validates the email address format
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;

if (form.email.value.search(emailRegEx) == -1) {
    alert("Please check your email address as it doesn't appear to be in a valid format.");
	form.email.focus();
    return false ;
  }




/*

This section makes the Date of Birth a required area. Commented out at present.

if (form.M_day_of_birth.value == "") {
    alert( "Please tell us your day of birth." );
    form.M_day_of_birth.focus();
    return false ;
  }

if (form.M_month_of_birth.value == "") {
    alert( "Please tell us your month of birth." );
    form.M_month_of_birth.focus();
    return false ;
  }

if (form.M_year_of_birth.value == "") {
    alert( "Please tell us your year of birth. (Don't worry, we won't tell anyone!)" );
    form.M_year_of_birth.focus();
    return false ;
  }

*/




if (form.M_how_did_you_hear_about_us.value == "") {
    alert( "Please tell us how you heard about us." );
    form.M_how_did_you_hear_about_us.focus();
    return false ;
  }






// the following code checks the amount of the donation
var amountvariable="";

if (form.amtother.value!="")
  {
  var othercharacters = /^[0-9\.]+$/;
  if (form.amtother.value.match(othercharacters)){}else{
  alert("Please amend the amount you are donating as it can only contain numbers and decimal points.");
  form.amtother.focus();
  return false ;
  }
  amountvariable=form.amtother.value;
  }
else
  {
	for (i = 0; i < 3; i++){
  	if (form.amtticked[i].checked == true){
	amountvariable=form.amtticked[i].value;}
	}
  }
form.amount.value = amountvariable;

if (form.amount.value == "") {
    alert( "Please enter a value for your donation." );
    form.amtother.focus();
    return false ;
  }




// The following code confirms whether the donor wants to receive further information from us
if (form.further_information_checkbox.checked==true)
	{
	form.M_further_information.value = "No";
	}
else
	{
	form.M_further_information.value = "Yes";
	}





  return true ;
}





