$(document).ready(function() {

	
	$("#ddlRecruitmentSource").bind("change", function(e) {
	
		if ($("#ddlRecruitmentSource")[0].value == "Other") {
		
			$("#ddlRecruitmentSourceOther").css("display", "block");
				
		} else {
			
			$("#ddlRecruitmentSourceOther").css("display", "none");
			$("#ddlRecruitmentSourceOther")[0].value = "";
		}
	
	});
		
	
	$("#FrmApplication").submit(function() {

		var ferror = false;
		var isValid = true;
		
		// to do: regex on tel nums and email, better message on cv, exclude more file types
		
		if ($("#txtForenames")[0].value == "") {
		$("#txtForenamesValid").css("display", "inline");
		isValid = false;
		}
		if ($("#txtSurname")[0].value == "") {
		$("#txtSurnameValid").css("display", "inline");
		isValid = false;
		}
		if ($("#txtHomeTel")[0].value == "") {
		$("#txtHomeTelValid").css("display", "inline");
		isValid = false;
		}
		if ($("#txtEmail")[0].value == "") {
		$("#txtEmailValid").css("display", "inline");
		isValid = false;
		}
		
		if ($("#ddlRecruitmentSource")[0].value == "" || ($("#ddlRecruitmentSource")[0].value == "Other" && $("#ddlRecruitmentSourceOther")[0].value == "") ) {
			$("#ddlRecruitmentSourceValid").css("display", "inline");
			isValid = false;
		}

		if (isValid == false) {
			alert("Please fill all the required fields")
		}
		
		if (window.frames['uploadiframe'].document.getElementById('filename')) {
			if (window.frames['uploadiframe'].document.getElementById('filename').innerHTML == "") {
				ferror = true
				isValid = false
			}
		} else {
			ferror = true
			isValid = false
		}

		if (ferror == true) {
			$("#txtFileValid").css("display", "inline");
			alert("Please upload your CV. If you are having problems uploading a file you can email it to jobs@castlecover.co.uk")
		}

		if (isValid == true) {
			var filename = window.frames['uploadiframe'].document.getElementById('filename').innerHTML
			$("#filename")[0].value = filename
		}

		return isValid;
	});


});