
alert ("Test");
function CheckAll(x)
{
	validateFirstName()
	validateSurname()
	validateLicenceNumber()
	validateIDNumber()
	
		
if (ISBLANK(x.BirthDate.value)) 
	{ 	
		    alert("Please Enter Birth Date in format yyyy-mm-dd in the Birth Date Field !!");
    	    return false;
    }

if (ISBLANK(x.Email.value)) 
{ 	
	document.frmRaceEntry.Email.style.backgroundColor = "yellow";
	document.frmRaceEntry.Email.focus();
	alert("Please enter Email Address field !!");
    	return false;
}
else	
{
		document.frmRaceEntry.Email.style.backgroundColor = "white";
}

if(checkemail(x.Email.value)==false)
	{
	document.frmRaceEntry.Email.style.backgroundColor = "yellow";
	document.frmRaceEntry.Email.focus();
	alert("Please enter a valid email address for Email Address field !!");
	return false;
}
else
{document.frmRaceEntry.Email.style.backgroundColor = "white";
}


if (ISBLANK(x.LicenceNumber.value)) 
	{ 	
	    document.frmRaceEntry.LicenceNumber.focus();
	    document.frmRaceEntry.LicenceNumber.style.backgroundColor = "yellow";
	    alert("Please define value for License Number field !!");
    	    return false;
    }

if(!ValidPhone(x.ContactNumber.value))
         {
	    document.frmRaceEntry.ContactNumber.focus();
	    document.frmRaceEntry.ContactNumber.style.backgroundColor = "yellow";
         return false;
         }
       else
       {
	    document.frmRaceEntry.ContactNumber.style.backgroundColor = "white"
        }
    //call ValidDate 
	
if (x.Club.value == 1)
	{
	document.frmRaceEntry.Club.style.backgroundColor = "yellow";
	document.frmRaceEntry.Club.focus();
	alert("Please enter a club from the drop down menu. If it is not listed, please enter Temp and in Other details your club name!!");
	return false;
	}
	else
	{
	document.frmRaceEntry.Club.style.backgroundColor = "white";
    }

if (x.Event.value == 4)
	{
	document.frmRaceEntry.Event.focus();
	document.frmRaceEntry.Event.style.backgroundColor = "yellow";
	alert("Please enter an Event from the drop down menu.!!");
	return false;
	}
	else
	{
	document.frmRaceEntry.Event.style.backgroundColor = "white";
    }

if (ISBLANK(x.Address1.value)) 
	{ 	
		    document.frmRaceEntry.Address1.focus();
		    document.frmRaceEntry.Address1.style.backgroundColor = "yellow";
		    alert("Please define value for Address field in Line 1 !!");
    	    return false;
    	    }
    	    else
    	    {
    	    document.frmRaceEntry.Address1.style.backgroundColor = "white";
    }

if (ISBLANK(x.AddressCode.value)) 
	{ 	
		    document.frmRaceEntry.AddressCode.focus();
		    document.frmRaceEntry.AddressCode.style.backgroundColor = "yellow";
		    alert("Please enter a code for Postal Code field !!");
    	    return false;
    	    }
    	    else
    	    {
    	    document.frmRaceEntry.AddressCode.style.backgroundColor = "white";
		    }
	
if (document.frmRaceEntry.Accept.checked == false)
	{ 	
			document.frmRaceEntry.Accept.focus();
			document.frmRaceEntry.Accept.style.backgroundColor = "yellow";
		    alert("The terms and conditions of entry needs to be selected to continue!!");
    	    return false;
    }

 	 return true;
}

function get_radio_value()
	{
	var radios = document.getElementById ('Event');
	if (radios) {
	var inputs = radios.getElementsByTagName ('RaceFees');
	if (inputs) {
    for (var i = 0; i < inputs.length; ++i) {
    if (inputs[i].type == '35' && inputs[i].name == '25')
    inputs[i].checked = inputs[i].value == '10';
    }
  }
}
	}

function validateFirstName() 
{
	if(document.frmRaceEntry.FirstName.value=='')
		{
		document.frmRaceEntry.FirstName.style.backgroundColor = "yellow";
		alert('Fill the Input box before submitting');
		return false;}
		if (isSingleAlphabet(document.frmRaceEntry.FirstName.value))
		{
		document.frmRaceEntry.FirstName.style.backgroundColor = "yellow";
		alert("Please Enter more than THREE Alphebetical Characters in the First Name Field !!");
		return false;
		}
		if (!isAlpha(document.frmRaceEntry.FirstName.value))
		{
		document.frmRaceEntry.FirstName.style.backgroundColor = "yellow";
		alert("Please Enter only Alphebetical Characters in the First Name Field !!");
		return false;
		}
	
	else	{
		document.frmRaceEntry.FirstName.style.backgroundColor = "white";
		return true;
		}
}

function validateSurname() 
{
if (ISBLANK(document.frmRaceEntry.Surname.value)) 
	{ 	
		    document.frmRaceEntry.Surname.style.backgroundColor = "yellow";
			alert("Please Enter a Surname in the Surname Field !!");
			return false;
    }
		
if (!isAlpha(document.frmRaceEntry.Surname.value))
	{
	document.frmRaceEntry.Surname.style.backgroundColor = "yellow";
	alert("Please Enter only Alphebetical Characters in the Surname Field !!");
	return false;
  }
	
if (isSingleAlphabet(document.frmRaceEntry.Surname.value))
	{
	document.frmRaceEntry.Surname.style.backgroundColor = "yellow";
	alert("Please Enter more than THREE Alphebetical Characters in the Surname Field !!");
	return false;
  }
	
else	{
		document.frmRaceEntry.Surname.style.backgroundColor = "white";
		return true;
		}
}

function validateLicenceNumber() 
{
if (ISBLANK(document.frmRaceEntry.LicenceNumber.value)) 
	{ 	
		    document.frmRaceEntry.LicenceNumber.style.backgroundColor = "yellow";
			alert("Please Enter Licence Number in the Licence Number Field !!");
    	    return false;
    }
	
if (document.frmRaceEntry.LicenceNumber.value == Temp)
	{ 
				return true; 
	} 
}


function validateIDNumber()
{ 
if (ISBLANK(document.frmRaceEntry.IDNumber.value)) 
	{ 	
		    document.frmRaceEntry.IDNumber.style.backgroundColor = "yellow";
			alert("Please Enter ID Number in the ID Number Field !!");
    	    return false;
    }
	
if (isNaN(document.frmRaceEntry.IDNumber.value)) 
	{ 
		document.frmRaceEntry.IDNumber.style.backgroundColor = "yellow";
		alert("Please enter ID Number as a Numeric only.!!"); 
		return false; 
	} 
		
if (document.frmRaceEntry.IDNumber.value.length !=13)
         {
		document.frmRaceEntry.IDNumber.style.backgroundColor = "yellow";
		alert("A valid ID Number has 13 Characters. Please re-enter !!")
        return false
         }
		
if (document.frmRaceEntry.IDNumber.value.length ==13)
        {
		validno()
		StripDOB_ID()
		return false
         }
				
		else	
		{
		//StripDOB_ID()
		//validno()
		document.frmRaceEntry.IDNumber.style.backgroundColor = "white";
		return true;
		}
		
}	

function validno()
{
	StripValidID = document.frmRaceEntry.IDNumber.value;
	checkdigit = StripValidID.slice(-1)*1;
	str1 = StripValidID.slice(0,1);
	str2 = StripValidID.slice(1,2);
	str3 = StripValidID.slice(2,3);
	str4 = StripValidID.slice(3,4);
	str5 = StripValidID.slice(4,5);
	str6 = StripValidID.slice(5,6);
	str7 = StripValidID.slice(6,7);
	str8 = StripValidID.slice(7,8);
	str9 = StripValidID.slice(8,9);
	str10 = StripValidID.slice(9,10);
	str11 = StripValidID.slice(10,11);
	str12 = StripValidID.slice(11,12);
	str13 = StripValidID.slice(-1); 
	str14 = str1*1 + str3*1 + str5*1 + str7*1 + str9*1 + str11*1;
	str15 = str2 + str4 + str6 + str8 + str10 + str12;
	str16 = str15*2;
	str17 = str16.toString().slice(0,1)*1;
	str18 = str16.toString().slice(1,2)*1;
	str19 = str16.toString().slice(2,3)*1;
	str20 = str16.toString().slice(3,4)*1;
	str21 = str16.toString().slice(4,5)*1;
	str22 = str16.toString().slice(5,6)*1;
	str26 = str16.toString().slice(6,7)*1;
	str23 = str17 + str18 + str19 + str20 + str21 + str22 + str26
	str24 = str14 + str23
	str25 = 10 - str24.toString().slice(-1)*1;
	if (str25 ==10)
	{str25 = 0}
	else
	{
	str25
	}
	
	if (checkdigit != str25)
	{
	document.frmRaceEntry.IDNumber.style.backgroundColor = "yellow";
	alert("The ID Number captured is not valid. Please re-enter !!")
	return false
	}
	else
	{
	document.frmRaceEntry.IDNumber.style.backgroundColor = "white";
	//document.frmRaceEntry.Other_Comments.value = checkdigit + "-" + str25;
	return true;
	}
	
}

function validateRaceFees()
{
	if ((document.frmRaceEntry.Entry[0].checked == true)
	&& ( document.contact_form.Entry[1].checked == false ) )
    	{
	document.frmRaceEntry.RaceFees.value = 35
	}
	
	if ((document.frmRaceEntry.Entry[0].checked == false)
	&& ( document.contact_form.Entry[1].checked == true ) )
    {
	document.frmRaceEntry.RaceFees.value = 25
	}
	
	if (document.frmRaceEntry.Entry[2].checked == true)
	{
	document.frmRaceEntry.RaceFees.value = 20
	}
}


function StripDOB_ID() 
{
StripID = document.frmRaceEntry.IDNumber.value;
stryyyy = StripID.slice(0,2);
strmm = StripID.slice(2,4);
strdd = StripID.slice(4,6);
strsex = StripID.slice(6,7);
document.frmRaceEntry.BirthDate.value = "19" + stryyyy + "-" + strmm + "-" + strdd;
strBirthDate = new Date("19" + stryyyy + "/" + strmm + "/" + strdd  + " 00:00:00");
strRaceDate = new Date("2009/05/10 00:00:00");
strDateDiff = Math.round(((strRaceDate - strBirthDate)/1000/60/60/24/365.25)-.499)
strDateDiff1 = ((strRaceDate - strBirthDate)/1000/60/60/24/365.25);
//document.frmRaceEntry.Other_Comments.value = strBirthDate + " - " + strRaceDate + " - " + strDateDiff + " - " + strDateDiff1;
document.frmRaceEntry.AgeonDay.value = strDateDiff
	if (strsex*1 >= 5)
	{
	//document.frmRaceEntry.Other_Comments.value = "19" + stryyyy + "-" + strmm + "-" + strdd;
	document.frmRaceEntry.Sex[0].checked = true;
	document.frmRaceEntry.Sex[0].value = 1;
	}
	else
	{
	document.frmRaceEntry.Sex[1].checked = true;
	document.frmRaceEntry.Sex[1].value = 2;
	}
	if( strDateDiff*1 <20)
	{
	document.frmRaceEntry.AgeCat.value = "(Junior)"
	}
	if( strDateDiff*1 >19)
	{
	document.frmRaceEntry.AgeCat.value = "(Senior)"
	}
	if( strDateDiff*1 >34)
	{
	document.frmRaceEntry.AgeCat.value = "(35-39)"
	}
	if( strDateDiff*1 >39)
	{
	document.frmRaceEntry.AgeCat.value = "(40-49)"
	}
	if( strDateDiff*1 >49)
	{
	document.frmRaceEntry.AgeCat.value = "(50-59)"
	}
	if( strDateDiff*1 >59)
	{
	document.frmRaceEntry.AgeCat.value = "60+)"
	}
	
}

function roundNumber(rnum, rlength) 
{ // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  document.roundform.numberfield.value = newnumber; // Output the result to the form field (change for your purposes)
}

function isSingleAlphabet(val)
{
      if(val==null){return false;}
      if (val.length==0 || val.length>3){return false;}
      var chars2check="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
      if (chars2check.indexOf(val)!=-3){return true;}
      return false;
      }
	
function ValidPhone(aphone)
{
// declare valid variable as a string with all valid characters (digits from 0 to 9 )
    var valid = "0123456789";
//if phone field is empty - display a warning and return false
         if(aphone=="")
         {
    alert ("A contact Number is required. Please enter phone number without dashes!")
         return false
         }
//if number of character in phone field is not equal 10 - display warning and return false
         if(aphone.length !=10)
         {
    alert("Invalid phone number length! Please try again.")
         return false
         }
        //check each character entered in the phone field 
         for (var i=0; i < aphone.length; i++)
         {
         //put in temp variable each character, one at a time.
         temp = "" + aphone.substring(i, i+1);
    //check index of a phone character in the "valid" variable.
    // if temp contains a character which is not in "valid" variable, 
    //then valid.indexOf(temp) will be -1, otherwise it may be 0.1.2.3.4.5.6.7.8 or 9

         if (valid.indexOf(temp) == "-1") 
         {
    alert("Invalid characters in your Phone Number. 10 unspaced characters are required. Please try again.")
          return false;
         }
    }
    //if all conditions are passed, then return true
    return true
}

var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz ';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ';
 
function isValid(parm,val) {
  if (parm == "") return true;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}
 
function isNum(parm) {return isValid(parm,numb);}
function isLower(parm) {return isValid(parm,lwr);}
function isUpper(parm) {return isValid(parm,upr);}
function isAlpha(parm) {return isValid(parm,lwr+upr);}
function isAlphanum(parm) {return isValid(parm,lwr+upr+numb);} 
	
/// email check

function checkemail(myemail)
{
var str=myemail;
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
{
testresults=true
}
else
{
testresults=false
}
return (testresults)
}

/// to check that perticular value is EMPTY OR NOT
function ISBLANK(xx)
{ 
        var cc=0,tt;
		for(tt=0; tt<xx.length; tt++)
		{
		     if (xx.charAt(tt)==' ')
			 {
			 	cc=cc+1; // count blank character
			 }
		}
		if (cc==xx.length)
		{
			return true;  //// means it is BLANK
		}
	     return false;	//// means it is NOT BLANK
}

function is_radio_button_selected(fieldnm)
{
// set var radio_choice to false
var radio_choice = false;

// Loop from zero to the one minus the number of radio button selections
for (counter = 0; counter < fieldnm.length; counter++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (fieldnm[counter].checked)
radio_choice = true; 
}

if (!radio_choice)
{
return (false); /// means not selected
}
return (true); /// means selected
}

function checkdate(objName) {
var datefield = objName;
if (chkdate(objName) == false) {
datefield.select();
alert("That date is invalid.  Please try again.");
datefield.focus();
return false;
}
else {
return true;
   }
}
function chkdate(objName) {
//var strDatestyle = "US"; //United States date style
var strDatestyle = "EU";  //European date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
strDate = datefield.value;
if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) {
err = 1;
return false;
}
else {
strDay = strDateArray[2];
strMonth = strDateArray[1];
strYear = strDateArray[0];
//strDay = strDateArray[0];
//strMonth = strDateArray[1];
//strYear = strDateArray[2];
}
booFound = true;
   }
}
if (booFound == false) {
if (strDate.length>5) {
strDay = strDate.substr(0, 2);
strMonth = strDate.substr(2, 2);
strYear = strDate.substr(4);
   }
}
if (strYear.length == 2) {
strYear = '20' + strYear;
}
// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
   }
}
if (isNaN(intMonth)) {
err = 3;
return false;
   }
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
err = 4;
return false;
}
if (intMonth>12 || intMonth<1) {
err = 5;
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}
if (intMonth == 2) {
if (intday < 1) {
err = 8;
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 10;
return false;
}
}
}
if (strDatestyle == "US") {
datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
}
else {
datefield.value = strYear + "-" + intMonth + "-" + intday;
}
return true;
}
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}

