function Trim(TRIM_VALUE){

  if(TRIM_VALUE.length < 1)
  {
    return"";
  }

  TRIM_VALUE = RTrim(TRIM_VALUE);
  TRIM_VALUE = LTrim(TRIM_VALUE);

  if(TRIM_VALUE=="")
  {
    return "";
  } else {
    return TRIM_VALUE;
  }
}

/*--------*/

function RTrim(VALUE){

  var w_space = String.fromCharCode(32);
  var v_length = VALUE.length;
  var strTemp = "";

  if(v_length < 0)
  {
    return"";
  }

  var iTemp = v_length -1;

  while(iTemp > -1)
  {
    if(VALUE.charAt(iTemp) == w_space)
    {
    } else {
      strTemp = VALUE.substring(0,iTemp +1);
      break;
    }

    iTemp = iTemp-1;

    } //End While

  return strTemp;

} //End Function


function LTrim(VALUE)
{
  var w_space = String.fromCharCode(32);
  if(v_length < 1)
  {
    return"";
  }

  var v_length = VALUE.length;
  var strTemp = "";

  var iTemp = 0;

  while(iTemp < v_length){
  if(VALUE.charAt(iTemp) == w_space)
  {
  } else {
    strTemp = VALUE.substring(iTemp,v_length);
    break;
  }

  iTemp = iTemp + 1;
  } //End While

 return strTemp;

} //End Function


function ValidateEntry()
{
   var date_value;
   var check_the_dob;
   var sex_choice = false;
   exp_finish = false;
   var tshirtsel = false;
   var selected_race;
  
   if(Trim(document.forms[0].Surname.value)==""){
    alert("Please enter your surname!");
    document.forms[0].Surname.focus();
    return false;
   }
   
   if(Trim(document.forms[0].FirstName.value)==""){
    alert("Please enter your first name!");
    document.forms[0].FirstName.focus();
    return false;
   }

   if(Trim(document.forms[0].Address1.value)==""){
    alert("Please enter address line 1!");
    document.forms[0].Address1.focus();
    return false;
   }

   if(Trim(document.forms[0].Town.value)==""){
    alert("Please enter your town of residence!");
    document.forms[0].Town.focus();
    return false;
   }

   if(Trim(document.forms[0].County.value)==""){
    alert("Please enter your county of residence!");
    document.forms[0].County.focus();
    return false;
   }

   if(Trim(document.forms[0].PostCode.value)==""){
    alert("Please enter your Post Code!");
    document.forms[0].PostCode.focus();
    return false;
   }

   if(Trim(document.forms[0].Email.value)==""){
    alert("Please enter your email address!");
    document.forms[0].Email.focus();
    return false;
   }

  if (/^\w+([\.&-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.forms[0].Email.value)){
      
  } else {
    alert("The email address entered is invalid")
    document.forms[0].Email.focus();
    return false
  }

   if(Trim(document.forms[0].DayPhone.value)==""){
    alert("Please enter your daytime telephone number!");
    document.forms[0].DayPhone.focus();
    return false;
   }
   
  // Check Contestant DOB
  check_the_dob = checkdob(date_value);
  if (!check_the_dob) {
   alert("Date of birth is empty or not input correctly");
   return false;
  } 
   
   if(Trim(document.forms[0].Age.value)==""){
    alert("Please enter your age on race day!");
    document.forms[0].Age.focus();
    return false;
   }
   
  for (counter = 0;  counter < document.forms[0].sex.length; counter++) {
      // If a radio button has been selected it will return true If not false
      if (document.forms[0].sex[counter].checked) sex_choice = true; 
  }
     
  if (!sex_choice)
  {
     //If there were no selections made display an alert box 
     alert("Please select your gender type - male or female!");
     return false;
  }
   
  //Race selection
  
  for (x = 0; x < document.forms[0].Race.length; x++) {
  	if (document.forms[0].Race[x].checked) {
  	 selected_race = document.forms[0].Race[x].value;
  	}
  }
  
  if(Trim(document.forms[0].AAAClub.value)=="" && (selected_race == "1" || selected_race == "3")) {
    alert("Please enter your AAA affiliated club for the race selected!");
    document.forms[0].AAAClub.focus();
    return false;
  
  }
  
  
  if(document.forms[0].fullhour.value!="-1" && (document.forms[0].marathon.checked != "1" && document.forms[0].half_marathon.checked != "1")) 
  {
    alert("Please select which race you ran last year");
    return false;
  }
}

function checkdob(theInput){
//	window.onerror=null // for all other strange errors
	var err=0
	a=document.forms[0].dob_date.value + "/" + document.forms[0].dob_month.value + "/" + document.forms[0].dob_year.value
	//a=entry.DateOfBirth.value
	if (a.length != 10) err=1
	d = a.substring(0, 2)// day
	c = a.substring(2, 3)// '/'
	b = a.substring(3, 5)// month
	e = a.substring(5, 6)// '/'
	f = a.substring(6, 10)// year

	//basic error checking
	if (b<1 || b>12) err = 1
	if (c != '/') err = 1
	if (d<1 || d>31) err = 1
	if (e != '/') err = 1
	if (f<0 || f>2002) err = 1
	
	//advanced error checking

	// months with 30 days
	if (b==4 || b==6 || b==9 || b==11){
		if (d==31) err=1
	}

	// february, leap year
	if (b==2){
		// feb
		var g=parseInt(f/4)
		if (isNaN(g)) {
			err=1
		}

		if (d>29) err=1
		if (d==29 && ((f/4)!=parseInt(f/4))) err=1
	}

	if (err==1){
		//alert('Wrong input!');
		theInput=false;
	}
	else{
		//alert('OK!');
		theInput=true;
	}
  return theInput;
}

function CheckCurrentDate(){

  var DefVal = "3";
  var entDate;
  var entMonth = "";
  var entYear;
  var strMonth;
  
  //entry.TotalPayment.value = "";
    
  var ExpireDate = new Date("19 August, 2009");
  var entryDate = new Date(document.forms[0].LateEntryDate.value);
  
  entDate = entryDate.getDate();
  entMonth = entryDate.getMonth() + 1;
  entYear = entryDate.getYear();
  
  if (entMonth == 1) strMonth = "January";
  if (entMonth == 2) strMonth = "February";
  if (entMonth == 3) strMonth = "March";
  if (entMonth == 4) strMonth = "April";
  if (entMonth == 5) strMonth = "May";
  if (entMonth == 6) strMonth = "June";
  if (entMonth == 7) strMonth = "July";
  if (entMonth == 8) strMonth = "August";
  if (entMonth == 9) strMonth = "September";
  if (entMonth == 10) strMonth = "October";
  if (entMonth == 11) strMonth = "November";
  if (entMonth == 12) strMonth = "December";
  
  var todDate = new Date(entDate + " " + strMonth + ", " + entYear);
    
  if (todDate.getTime() > ExpireDate.getTime()) {
    document.forms[0].Late.value="Y"
    document.forms[0].race_price.value="24.50"
    document.forms[0].late_entry_price.value="5"
    document.forms[0].Race[0].checked = "1";
    document.forms[0].RaceLateEntry.checked = "1"
    CalculateTotal("24.50");
  } else {
    document.forms[0].Late.value="N"
    document.forms[0].race_price.value="24.50"
    document.forms[0].Race[0].checked = "1";
    CalculateTotal("24.50");
  }
}

function CharityDonation(theAmt){
  
  var CharityAmt = eval(theAmt);
  
  document.forms[0].donations.value = ""
  document.forms[0].donations.value = CharityAmt
  
}

function CalculateTotal(theAmt){

  
  var donVal;
  var raceVal;
  var lateEntryVal;
  var MedalFees;
  var shirtfees;
  
  if (document.forms[0].donations.value=="") {
      donVal=0;
    } else {
      donVal = eval(document.forms[0].donations.value);
  }
  
  if (document.forms[0].race_price.value=="") {
      raceVal=0;
    } else {
      raceVal = eval(document.forms[0].race_price.value);
  }
  
  if (document.forms[0].tshirt_fees.value=="") {
      shirtfees=0;
    } else {
      shirtfees = eval(document.forms[0].tshirt_fees.value);
  }
    
  if (document.forms[0].medal_fees.value=="") {
      MedalFees=0;
    } else {
      MedalFees = eval(document.forms[0].medal_fees.value);
  }
    
  if (document.forms[0].late_entry_price.value=="") {
      lateEntryVal=0;
    } else {
      lateEntryVal = eval(document.forms[0].late_entry_price.value);
  }
    
  totalVal = raceVal + donVal + shirtfees + lateEntryVal + MedalFees;
  document.forms[0].TotalPayment.value =  pdec(totalVal);// + ".00";
}

function RacePrice(thePrice){

  document.forms[0].race_price.value = "";
  document.forms[0].race_price.value = thePrice;
}

function ShirtPrice(thePrice){

  document.forms[0].tshirt_fees.value = "";
  document.forms[0].tshirt_fees.value = thePrice;
}

function pdec (amount) {

  if (amount.toString().indexOf(".") == -1) {
   amount = amount + ".00"
  } else {
   amount = amount + "0"
  }

  return amount;
}

function SelectMedal(WhichOne){

    if (WhichOne == "1") {
      document.forms[0].medal_fees.value = "0";
    } 
    
    if (WhichOne == "2") {
      document.forms[0].medal_fees.value = "1";
    } 

}

function SelectLateEntry(whichMara){

  var entDate;
  var entMonth = "";
  var entYear;
  var strMonth;
  
  //entry.TotalPayment.value = "";
    
  var ExpireDate = new Date("19 August, 2009");
  var entryDate = new Date(document.forms[0].LateEntryDate.value);
  
  entDate = entryDate.getDate();
  entMonth = entryDate.getMonth() + 1;
  entYear = entryDate.getYear();
  
  if (entMonth == 1) strMonth = "January";
  if (entMonth == 2) strMonth = "February";
  if (entMonth == 3) strMonth = "March";
  if (entMonth == 4) strMonth = "April";
  if (entMonth == 5) strMonth = "May";
  if (entMonth == 6) strMonth = "June";
  if (entMonth == 7) strMonth = "July";
  if (entMonth == 8) strMonth = "August";
  if (entMonth == 9) strMonth = "September";
  if (entMonth == 10) strMonth = "October";
  if (entMonth == 11) strMonth = "November";
  if (entMonth == 12) strMonth = "December";
  
  var todDate = new Date(entDate + " " + strMonth + ", " + entYear);
    
  if (todDate.getTime() > ExpireDate.getTime()) {
    if (whichMara == "1") {
      alert("Your entry is late. You will be charged an extra £5 late fees!");
      document.forms[0].late_entry_price.value = "5";
    } 
    if (whichMara == "2") {
      document.forms[0].late_entry_price.value = "0";
    } 
    
  } else {
      document.forms[0].late_entry_price.value = "";
  } 
}

function LateEntry(whichMara){

  var entDate;
  var entMonth = "";
  var entYear;
  var strMonth;
  
  //entry.TotalPayment.value = "";
    
  var ExpireDate = new Date("19 August, 2009");
  var entryDate = new Date(document.forms[0].LateEntryDate.value);
  
  entDate = entryDate.getDate();
  entMonth = entryDate.getMonth() + 1;
  entYear = entryDate.getYear();
  
  if (entMonth == 1) strMonth = "January";
  if (entMonth == 2) strMonth = "February";
  if (entMonth == 3) strMonth = "March";
  if (entMonth == 4) strMonth = "April";
  if (entMonth == 5) strMonth = "May";
  if (entMonth == 6) strMonth = "June";
  if (entMonth == 7) strMonth = "July";
  if (entMonth == 8) strMonth = "August";
  if (entMonth == 9) strMonth = "September";
  if (entMonth == 10) strMonth = "October";
  if (entMonth == 11) strMonth = "November";
  if (entMonth == 12) strMonth = "December";
  
  var todDate = new Date(entDate + " " + strMonth + ", " + entYear);
    
  if (todDate.getTime() > ExpireDate.getTime()) {
    document.forms[0].RaceLateEntry.checked = "0"
  } else {
    document.forms[0].RaceLateEntry.checked = "0"
  } 

}

function CheckDonationOther(){
  var donate;
  donate = document.forms[0].DonationOther.value;
  
  if (isNaN(document.forms[0].DonationOther.value)){ 
     	alert("Numericals should be entered or leave box empty");
     	document.forms[0].DonationOther.focus();
    } else {
      if (donate.length < 1) { 
        document.forms[0].donations.value = "";
        document.forms[0].donations.value = document.forms[0].DonationOther.value;
      } else {    
        document.forms[0].donations.value = "";
        document.forms[0].donations.value = document.forms[0].DonationOther.value;
        document.forms[0].Donation[2].checked = true
      }
    }
}

function SelectedMarathon() 
{

  if (document.forms[0].marathon.checked == "1") {
    document.forms[0].half_marathon.checked = false;
  } 

}

function SelectedHalfMarathon() 
{

  if (document.forms[0].half_marathon.checked == "1") {
    document.forms[0].marathon.checked = false;
  } 

}

//When user check if they have been entered
function CheckEntry()
{
   if(Trim(document.forms[0].surname.value)==""){
    alert("Please enter your surname!");
    document.forms[0].surname.focus();
    return false;
   }
   
   if(Trim(document.forms[0].postcode1.value)==""){
    alert("Please enter your postcode or the first part of your postcode!");
    document.forms[0].postcode1.focus();
    return false;
   }

}
