function setDays() {

	var daysInMonthSelected;
	var daysInCombo;
	var dayElement;

	daysInMonthSelected = getDaysInMonth(getObject("month").value, getObject("year").value);
	daysInCombo = getObject("day").length;

	if (daysInCombo > daysInMonthSelected) {
		for (dayElement=daysInCombo; dayElement >= daysInMonthSelected; dayElement--) {
			getObject("day").options[dayElement] = null;
		}
	}
	
	if (daysInMonthSelected > daysInCombo) {
		for (dayElement=daysInCombo; dayElement < daysInMonthSelected; dayElement++) {
			getObject("day").options[dayElement] = new Option(dayElement+1, dayElement+1);
		}
	}
	
	if (getObject("day").selectedIndex < 0) getObject("day").selectedIndex = 1;
}

function getDaysInMonth(inMonth, inYear) {

	var numDays = 0;
	var tmpMonth = inMonth.toString();
	var tmpYear = inYear.valueOf();

	switch (tmpMonth) {
		case "1":		// Jan
		case "3":		// Mar
		case "5":		// May
		case "7":		// Jul
		case "8":		// Aug
		case "10":		// Oct
		case "12":		// Dec
			numDays = 31;
			break;			
		case "2":		// Feb, also deal with leap years
				if ((tmpYear%4 == 0 && tmpYear%100 !=0) || (tmpYear%400 == 0)) {
						numDays = 29;
				} else {
						numDays = 28;
				}
			break;
		case "4":		// Apr
		case "6":		// Jun
		case "9":		// Sep
		case "11":		// Nov
			numDays = 30;
			break;
	}

	return(numDays);

}
function isValidEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail address - please check and try again")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail address - please check and try again")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail address - please check and try again")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail address - please check and try again")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail address - please check and try again")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail address - please check and try again")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail address - please check and try again")
		    return false
		 }

 		 return true					
	}

function getObject(objName) {
	if(document.layers){
		return eval("document."+objName);
	}
	if(document.all){
		return eval("document.all." + objName);
	}
	if(!document.all && document.getElementById){
		return eval("document.getElementById('" +objName +"')");
}
}

function checkForm() {
	if (getObject('Full_Name').value == '') {
		alert('Full name must be entered')
		getObject('Full_Name').focus();
		return false;
	} else if (getObject('Address1').value == '') {
		alert('Address must be entered')
		getObject('Address1').focus();
		return false;
	} else if (getObject('PostCode').value == '') {
		alert('PostCode must be entered')
		getObject('PostCode').focus();
		return false;
	} else if (!IsValidPostcode(getObject('PostCode').value)) {
		alert('PostCode is not in the correct format')
		getObject('PostCode').focus();
		return false;
	} else if (getObject('Email').value=='') {
		alert('A valid email address must be entered')
		getObject('Email').focus();
		return false;
	} else if (!isValidEmail(getObject('Email').value)) {
		getObject('Email').focus();
		return false;
	} else if (getObject('Phone').value=='' && getObject('Mobile').value=='') {
		alert('Either Home Telephone or Mobile must be entered')
		getObject('Phone').focus();
		return false;
	} else if (getObject('HeightFeet').selectedIndex<1) {
		alert('Height must be entered')
		getObject('HeightFeet').focus();
		return false;
	} else {
		return true;
	}
}

function IsValidPostcode(postcode){ //check postcode format is valid
 var size = postcode.length
 var test = postcode.toUpperCase(); //Change to uppercase
 while (test.slice(0,1) == " ") //Strip leading spaces
  {test = test.substr(1,size-1);size = test.length
  }
 while(test.slice(size-1,size)== " ") //Strip trailing spaces
  {test = test.substr(0,size-1);size = test.length
  }
 if (size < 6 || size > 8){ //Code length rule
  return false;
  }
 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
   return false;
  }
 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
   return false;
  }
 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
   return false;
  }
 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
   return false;
  }
 if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
   return false;
   }
 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
 if (count1 != count2){//only one space rule
   return false;
  }
return true;
}

