
//If required change the race entry pice here:
//set costs of registration

var AFFILIATED_COST = 13.50
var UNAFFILIATED_COST = 15.50

function fcheckRaceApplication(){

    if(document.getElementById("txtSurname").value==""){
        alert("Please enter a surname");
        document.getElementById("txtSurname").focus();
        return 1;
    }
    else if(document.getElementById("txtForename").value==""){
        alert("Please enter a forename");
        document.getElementById("txtForename").focus();
        return 1;   
    }
    else if(document.getElementById("seldobDD").value=="?"){
        alert("Please enter a date of birth DAY");
        document.getElementById("seldobDD").focus();
        return 1;   
    }   
    else if(document.getElementById("seldobMM").value=="?"){
        alert("Please enter a date of birth MONTH");
        document.getElementById("seldobMM").focus();
        return 1;   
    }
    else if(document.getElementById("seldobYY").value=="?"){
        alert("Please enter a date of birth YEAR");
        document.getElementById("seldobYY").focus();
        return 1;   
    }
    else if(isNaN(document.getElementById("txtAgeOnRaceDay").value) || document.getElementById("txtAgeOnRaceDay").value==""){
        alert("Please enter an Age on Race Day");
        document.getElementById("txtAgeOnRaceDay").focus();
        return 1;   
    }	
    else if(document.getElementById("Affiliated").value=="?"){
        alert("Please let us know if you are affiliated with a club");
        document.getElementById("Affiliated").focus();
        return 1;   
    }
    else if(document.getElementById("Affiliated").value=="YES" && document.getElementById("txtRunningClub").value==""){
        alert("Please enter your club name");
        document.getElementById("txtRunningClub").focus();
        return 1;   
    }
    else if(document.getElementById("txtSex").value=="?"){
        alert("Please let us know if you are Male or Female");
        document.getElementById("txtSex").focus();
        return 1;
    }
    else if(document.getElementById("txtAddress").value==""){
        alert("Please enter your address");
        document.getElementById("txtAddress").focus();
        return 1;
    }
    else if(document.getElementById("txtPostcode").value==""){
        alert("Please enter your postcode");
        document.getElementById("txtPostcode").focus();
        return 1;
    }
	else if(isNaN(document.getElementById("txtTelephone").value) || document.getElementById("txtTelephone").value==""){
        alert("Please enter a contact telephone number");
        document.getElementById("txtTelephone").focus();
        return 1;
    }
    else if(document.getElementById("txtEmail").value==""){
        alert("Please enter your email address");
        document.getElementById("txtEmail").focus();
        return 1;
    }
    else if(document.getElementById("paypalEmail").value==""){
        alert("Please enter your Paypal email address");
        document.getElementById("paypalEmail").focus();
        return 1;
    }	
    else if(document.getElementById("whichRace").value=="?"){
        alert("Please tell us which race you would like to enter");
        document.getElementById("whichRace").focus();
        return 1;
    }
    else if(document.getElementById("T-shirtSize").value=="?"){
        alert("Please tell us which T-shirt size you would like");
        document.getElementById("T-shirtSize").focus();
        return 1;
    }	
    else if(isNaN(document.getElementById("GiftAidamount").value)==true && document.getElementById("chkGiftAid").checked==true){
		alert("Please enter a valid number for donation amount");
		document.getElementById("GiftAidamount").focus();
		return 1;
    }
    else if(document.getElementById("chkTandC").checked!=true){
        alert("Please agree to the race Terms and Conditions");
        document.getElementById("chkTandC").focus();
        return 1;
    }
    else{
        //validation returned ok
        //create a DOB string and validate
			if(MakeDob()==1){
			//set the date to english version for email
				document.getElementById("txtDOB").value = document.getElementById("seldobDD").value + "/" + document.getElementById("seldobMM").value + "/" + document.getElementById("seldobYY").value;
				document.all.Wellington10.submit();
			}
			else{
				return 1;
			}
	}
	
	return 1;

}

function showHideDiv(divname){


	if(divname=="divRunningClub"){

		if(document.getElementById("Affiliated").value=="YES"){

			document.getElementById(divname).style.display="block";
		}
		else{
			document.getElementById(divname).style.display="none";
		}
		
		calculateCosts();
	}
	else if(divname=="divGiftAidinfo"){
		
		if(document.getElementById(divname).style.display=="none"){
		   
			document.getElementById("ShowMoreInfo").innerHTML="Hide Gift Aid information"
			document.getElementById(divname).style.display="block";
		}
		else{
			document.getElementById("ShowMoreInfo").innerHTML="Show more Gift Aid information"
			document.getElementById(divname).style.display="none";
		}
	}
	else if(divname=="divTandC"){
		
		if(document.getElementById(divname).style.display=="none"){
		   
			document.getElementById("ShowMoreInfoTandC").innerHTML="Hide Terms & Conditions"
			document.getElementById(divname).style.display="block";
		}
		else{
			document.getElementById("ShowMoreInfoTandC").innerHTML="Show Terms & Conditions"
			document.getElementById(divname).style.display="none";
		}
	}

	else if(divname=="divOnlineReg"){
	
			document.getElementById(divname).style.display="block";
			document.getElementById("divdownloadReg").style.display="none";
	
	}
	else if(divname=="divdownloadReg"){
			
			document.getElementById(divname).style.display="block";
			document.getElementById("divOnlineReg").style.display="none";
	} 
  
}

function calculateCosts(){
	
	iDonation=document.getElementById("GiftAidamount").value;
	
	if(isNaN(iDonation)){
		alert("Please enter a valid number for donation amount");
		document.getElementById("GiftAidamount").focus();	
		return;
	}
	else{
		iDonation = formatCurrency(iDonation);
		//alert("iDonation : " + iDonation)
		
	}
	var total;
	var desc;
	
    if(document.getElementById("Affiliated").value=="YES"){
		 		 
		 if(!isNaN(iDonation)){
			total = eval(formatCurrency(AFFILIATED_COST)) + eval(formatCurrency(iDonation));
			desc = "Fee (with £" + iDonation + " donation) : "
		 }
		 else{
			desc = "Fee: "
			total = formatCurrency(AFFILIATED_COST);
		 }
    }
    else{
	
		 if(!isNaN(iDonation)){
			total = eval(formatCurrency(UNAFFILIATED_COST)) + eval(formatCurrency(iDonation));
			//alert("UNAFFILIATED_COST : " + formatCurrency(UNAFFILIATED_COST) + "   |[+]|   iDonation : " + formatCurrency(iDonation));
			desc = "Fee (with £" + iDonation + " donation) : ";
		 }
		 else{
			desc = "Fee: ";
			total = formatCurrency(UNAFFILIATED_COST);
		 }		
    }

		 document.getElementById("divamountToPay").innerHTML = "<p>"+ desc +" £" + formatCurrency(total) + "</p>";
		 document.getElementById("amount").value=total; 
		 document.getElementById("GiftAidamount").value = formatCurrency(iDonation);
		 return;
}

//loops input box and creates an option for the past 100 years
function makeYears(){

var CTime = new Date();
var year = CTime.getFullYear();
var myear = year - 100;

    while(myear<=year){
        createOption("seldobYY", year, year); 
        year--
    }

}

//creates a DOB string from the checkboxes
function MakeDob(){

    document.getElementById("txtDOB").value = document.getElementById("seldobMM").value + "/" + document.getElementById("seldobDD").value + "/" + document.getElementById("seldobYY").value;
	//check if the date created above is valid
	if(isDate(document.getElementById("txtDOB").value)==true){
		return 1;
	}
		return 0;
}

function createOption(selectbox,text,value ){   
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
	document.getElementById(selectbox).options.add(optn);
}


function isDate(dtStr)
{
	// Get Todays Date
	var dToday = new Date();
	//alert("Date Today: " + dToday);

	var dtCh= "/";
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	var strYr=strYear;

	// Set Date to Passed Date
	var dDate = new Date();
	dDate.setFullYear(strYr,strMonth - 1,strDay);
	//alert("Entered Date: " + dDate);
	
	if (dDate>dToday)
	{

			alert("Please enter a valid DOB");
			return false;

	}//end if
	
	if (strDay.charAt(0)=="0" && strDay.length>1) 
	{
		strDay=strDay.substring(1)
	}//end if
	if (strMonth.charAt(0)=="0" && strMonth.length>1) 
	{
		strMonth=strMonth.substring(1)
	}//end if
	for (var i = 1; i <= 3; i++) 
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) 
		{
			strYr=strYr.substring(1)
		}//end if
	}//end for

	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);


	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		alert("Please enter a valid DOB");
		return false;
	}//end if

	return true;
}//end function

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    	// EXCEPT for centurial years which are not also divisible by 400.
    	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}//end function

function DaysArray(n) {
	for (var i = 1; i <= n; i++){
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) 
		{
			this[i] = 30
		}//end if
		else if (i==2) 
		{
			this[i] = 29
		}//end else
   	}//end for
   	return this
}//end function 


function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}
