function toTimestamp(strDate){
	var datum = Date.parse(strDate);
	return datum/1000;
}

$(document).ready(function() {
	$("#apply_application").validate({
		rules: {
			apply_name: "required",
			apply_address: "required",
			apply_postcode: "required",
			apply_phone: {
				required: true,
				number: true
			},
			apply_email: {
				required: true,
				email: true
			},
			apply_dob: {
				required: true,
				date: true
			}
		},
		messages: {
			apply_name:  "&nbsp;&nbsp;<span style='font-weight: bold; color: #ff0000;'>Please enter your name.</span>",
			apply_address: "&nbsp;&nbsp;<span style='font-weight: bold; color: #ff0000;'>Please enter your address.</span>",
			apply_postcode: "&nbsp;&nbsp;<span style='font-weight: bold; color: #ff0000;'>Please enter your postcode.</span>",
			apply_phone: "&nbsp;&nbsp;<span style='font-weight: bold; color: #ff0000;'>Please enter your phone number.</span>",
			apply_email: "&nbsp;&nbsp;<span style='font-weight: bold; color: #ff0000;'>Please enter your email address.</span>",
			apply_dob: "&nbsp;&nbsp;<span style='font-weight: bold; color: #ff0000;'>Please enter your date of birth.</span>"
		},
		submitHandler: function(form) {
			$("#apply_application").ajaxSubmit(function(data) { 
				$("#application_content").empty();
				$("#application_content").hide().append("Thank you. Your application has been submitted, it will be reviewed by one of our team and we will get in touch with you soon.").fadeIn(1000);
            });		
		}
	});
	
	//function for send email
	$("#send_email").validate({
		rules: {
			email_name: "required",
			email_address: {
				required: true,
				email: true
			},
			email_comment: "required"
		},
		messages: {
			email_name: "&nbsp;&nbsp;<span style='font-weight: bold; color: #ff0000;'>Please enter your name.</span>",
			email_address:  "&nbsp;&nbsp;<span style='font-weight: bold; color: #ff0000;'>Please enter a valid email address.</span>",
			email_comment: "<br /><span style='font-weight: bold; color: #ff0000;'>Please fill out the form with as much information as you can.</span>"
		},
		submitHandler: function(form) {
			$("#send_email").ajaxSubmit(function(data) { 
				$("#load_email_form").empty();
				$("#load_email_form").hide().append("Thank you. Your email has been sent, it will be reviewed by one of our team and we will get in touch with you as soon as possible.").fadeIn(1000);
            });		
		}
	});
	
	
	$("#button_search").click(function () {
		var courseTypeId = $("#available_course_types").val();
		var courseDistrictId = $("#available_districts").val();
		var courseSportId = $("#available_sport").val();
		var postCode = $("#post_code").val();
		
		var dayFrom = $("#from_day").val();
		var monthFrom = $("#from_month").val();
		var yearFrom = $("#from_year").val();
		
		var dayTo = $("#to_day").val();
		var monthTo = $("#to_month").val();
		var yearTo  = $("#to_year").val(); 
		
		if (dayFrom!='' && monthFrom!='' && yearFrom!='')
		{
			var fromStamp = dayFrom+"/"+monthFrom+"/"+yearFrom;
		}
		else
		{
			var fromStamp ='';
		}
		
		if (dayTo!='' && monthTo!='' && yearTo!='')
		{
			var toStamp = dayTo+"/"+monthTo+"/"+yearTo;
		}
		else
		{
			var toStamp = '';
		}
		
		var additionalString = "&from="+fromStamp+"&to="+toStamp;	
		
		if (postCode == 'Postcode')
		{
			var postCode = '';
		}
		else
		{
			testPostCode();
		}
	
		var postCode = $("#post_code").val();
		
		$("#info_content").hide();
		$("#application_content").hide();
		$("#loading_div").show();
		$("#load_content").hide();
		$("#load_email_form").hide();
		
		$("#load_content").load("./search.php?type=full&c=&t="+courseTypeId+"&d="+courseDistrictId+"&s="+courseSportId+""+additionalString, { postPostCode: postCode }, function(){
			$("#loading_div").hide();
			$("#load_content").show();
		});
				
	});
	
	$("#button_code").click(function () {
		var courseCode = $("#course_code").val();
		if (courseCode == 'Enter a course code')
		{
			var courseCode = '';
		}
		$("#info_content").hide();
		$("#load_content").hide();
		$("#application_content").hide();
		$("#loading_div").show();
		$("#load_email_form").hide();
		
		$("#load_content").load("./search.php?type=code&c="+courseCode, function(){
			$("#loading_div").hide();
			$("#load_content").show();
		});

	});
	
	
	
	$("#post_code").click(function () {
		$("#post_code").val("");
	});
	
	$("#post_code").blur(function () {
		 var postcodeValue = $("#post_code").val();
		 if (postcodeValue == "") 
		 {
			$("#post_code").val("Postcode"); 
		 }
		 
    });
	
});

function loadInformation(courseId) 
{
	$("#load_content").hide();
	$("#application_content").hide();
	$("#info_content").load("./information.php?id="+courseId).fadeIn(1000);
	$("#load_email_form").hide();
}
function backToSearch() 
{
	$("#load_content").show();
	$("#application_content").hide();
	$("#info_content").hide();
	$("#load_email_form").hide();
}
function backToCourse() 
{
	$("#load_content").hide();
	$("#application_content").hide();
	$("#info_content").show();
	$("#load_email_form").hide();
}
function courseApplication(courseId) 
{
	$("#info_content").hide();
	$("#load_content").hide();
	$("#application_content").load("./application.php?id="+courseId).fadeIn(1000);
	$("#load_email_form").hide();
	var applyId = courseId;
}
function sendEmail(emailaddress, instanceId)
{
	$("#info_content").hide();
	$("#load_content").hide();
	$("#application_content").hide();
	$("#load_email_form").load("./email.php?email="+emailaddress+"&id="+instanceId).fadeIn(1000)
}
function testPostCode() {
	var myPostCode = document.getElementById('post_code').value;
	
	if (checkPostCode (myPostCode)) {
		document.getElementById('post_code').value = checkPostCode (myPostCode)
		//alert ("Postcode has a valid format")
	} 
	else 
	{
		document.getElementById('post_code').value = 'Postcode';
	}
}

function checkPostCode(toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}



