
// make sure the form meets with our approval
function jbe_validate(f) {
	errors = "";
	
	// run our checks
	if(f.name.value == "") { errors += "\n- Must complete the Name field."; }
	if(f.email.value == "") { errors += "\n- Must complete the Email field."; }
	if(f.email.value != f.email_confirm.value) { errors += "\n- Your Emails don't match."; }
	
	categories = 0;
	locations = 0;
	bands = false;
	for(i=0;i<f.length;i++) {
		if((f[i].type == "checkbox") && f[i].checked) {
			if(f[i].name == "category_ids[]") { categories = categories + 1; }
			if(f[i].name == "location_ids[]") { locations = locations + 1; }
			if(f[i].name == "salary_bands[]") { bands = true; }
		}
		else if((f[i].name == "location_ids[]") && (f[i].type == "hidden")) {
			locations = true;
		}
	}
	
//	if(!categories) { errors += "\n- Must select at least one sector."; }
	if(!locations) { errors += "\n- Must select at least one location."; }
	if(!bands) { errors += "\n- Must select at least one salary band."; }
	
	// if error found, complain
	if(errors != "") {
		errors = "The following problems were found with your details:\n" + errors + "\n\nPlease correct and try again.";
		alert(errors);
		return false;
	}
	
	// otherwise cool, let's go
	else {
		return true;
	}
}

//validate the candidate sign up form.
function candidate_validate(f) {
	errors = "";
	
	// run our checks
	if(f.name.value == "") { errors += "\n- Must complete the Name field."; }
	if(f.email.value == "") { errors += "\n- Must complete the Email field."; }
	if(f.email.value != f.email_confirm.value) { errors += "\n- Your Emails don't match."; }
	if(f.password.value == "") { errors += "\n- Must complete the Password field."; }
	if(f.password.value != f.password_confirm.value) { errors += "\n- Your Passwords don't match."; }
	if(f.profile.value == "") { errors += "\n- Must enter a profile."; }
	if(f.cv.value == "") { errors += "\n- Must select a CV to upload."; }

	var hasCategories = $("#category_select_1").val() + $("#category_select_2").val();
	
	var numLocations  = $("input:checkbox:checked[name='location_ids[]']").length;
	numLocations	  = numLocations + $("input:hidden[name='location_ids[]']").length;
	
	if (! hasCategories)   { errors += "\n- Must select at least one job category."; }
	if (! numLocations)    { errors += "\n- Must select at least one location."; }
	
	num_keywords = 0;
	$("input[name=keywords\\[\\]]").each(function() {
		if($(this).val() != "") {
			num_keywords++;
		}
	});
	if(num_keywords < 3) { errors += "\n- Must supply at least three keywords."; }
	
	// if error found, complain
	if(errors != "") {
		errors = "The following problems were found with your details:\n" + errors + "\n\nPlease correct and try again.";
		alert(errors);
		return false;
	}
	
	// otherwise cool, let's go
	else {
		return true;
	}
}

// when they click on the contract checkbox
function contract_check(f) {
	f = f.form;
	
	if(f.contract.checked) {
		f.salary_contract.disabled = false;
		
		// enable all the date range stuff too
		for(i=0;i<f.elements.length;i++) {
			if(f.elements[i].name == "contract_starts[]") {
				f.elements[i].disabled = false;
			}
			else if(f.elements[i].name == "contract_stops[]") {
				f.elements[i].disabled = false;
			}
		}
		
		// the + and - buttons only get undisabled if they'd be showing anyway.
		n = $("#contract_periods > div").length;
		if(n != 1) {
			f.lesscontracts.disabled = false;
		}
		if(n != 5) {
			f.morecontracts.disabled = false;
		}
	}
	else {
		f.salary_contract.disabled = true;
		
		// disable all the date range stuff too
		for(i=0;i<f.elements.length;i++) {
			if(f.elements[i].name == "contract_starts[]") {
				f.elements[i].disabled = true;
			}
			else if(f.elements[i].name == "contract_stops[]") {
				f.elements[i].disabled = true;
			}
		}
		f.lesscontracts.disabled = true;
		f.morecontracts.disabled = true;
	}
}

// when they click on the permanent checkbox
function permanent_check(f) {
	f = f.form;
	
	if(f.permanent.checked) {
		f.salary_permanent.disabled = false;
	}
	else {
		f.salary_permanent.disabled = true;
	}
}

// when they click on the temp checkbox
function temp_check(f) {
	f = f.form;
	
	if(f.temp.checked) {
		f.salary_temp.disabled = false;
		
		// enable all the date range stuff too
		for(i=0;i<f.elements.length;i++) {
			if(f.elements[i].name == "temp_starts[]") {
				f.elements[i].disabled = false;
			}
			else if(f.elements[i].name == "temp_stops[]") {
				f.elements[i].disabled = false;
			}
		}
		
		// the + and - buttons only get undisabled if they'd be showing anyway.
		n = $("#temp_periods > div").length;
		if(n != 1) {
			f.lesstemps.disabled = false;
		}
		if(n != 5) {
			f.moretemps.disabled = false;
		}
	}
	else {
		f.salary_temp.disabled = true;
		
		// disable all the date range stuff too
		for(i=0;i<f.elements.length;i++) {
			if(f.elements[i].name == "temp_starts[]") {
				f.elements[i].disabled = true;
			}
			else if(f.elements[i].name == "temp_stops[]") {
				f.elements[i].disabled = true;
			}
		}
		f.lesstemps.disabled = true;
		f.moretemps.disabled = true;
	}
}

// adds another available date range thing for them
function more_contracts() {
	n = $("#contract_periods > div").length;
	if(n < 5) {
		// get the current bottom dates
		for(i=0;i<document.candidateform.elements.length;i++) {
			if(document.candidateform.elements[i].name == "contract_starts[]") {
				starts = document.candidateform.elements[i].value;
			}
			else if(document.candidateform.elements[i].name == "contract_stops[]") {
				stops = document.candidateform.elements[i].value;
			}
		}
		
		$("#contract_periods").append('<div>\n<label for="contract_starts[]">From:</label>\n<input type="text" name="contract_starts[]" class="datepicker" size="9" maxlength="11" value="' + starts + '" />\n<label for="contract_stops[]">To:</label>\n<input type="text" name="contract_stops[]" class="datepicker" size="9" maxlength="11" value="' + stops + '" />\n</div>');
		
		$(".datepicker").datepicker({
			dateFormat : "dd M yy",
			duration : "",
			constrainInput : true
		});
		
		if(n == 4) {
			document.candidateform.morecontracts.disabled = true;
		}
	}
	document.candidateform.lesscontracts.disabled = false;
}

// removes ones of their available date ranges
function less_contracts() {
	n = $("#contract_periods > div").length;
	if(n > 1) {
		$("#contract_periods > div:last").remove();
	}
	if(n == 2) {
		document.candidateform.lesscontracts.disabled = true;
	}
	document.candidateform.morecontracts.disabled = false;
}

// adds another available date range thing for them
function more_temps() {
	n = $("#temp_periods > div").length;
	if(n < 5) {
		// get the current bottom dates
		for(i=0;i<document.candidateform.elements.length;i++) {
			if(document.candidateform.elements[i].name == "temp_starts[]") {
				starts = document.candidateform.elements[i].value;
			}
			else if(document.candidateform.elements[i].name == "temp_stops[]") {
				stops = document.candidateform.elements[i].value;
			}
		}
		
		$("#temp_periods").append('<div>\n<label for="temp_starts[]">From:</label>\n<input type="text" name="temp_starts[]" class="datepicker" size="9" maxlength="11" value="' + starts + '" />\n<label for="temp_stops[]">To:</label>\n<input type="text" name="temp_stops[]" class="datepicker" size="9" maxlength="11" value="' + stops + '" />\n</div>');
		
		$(".datepicker").datepicker({
			dateFormat : "dd M yy",
			duration : "",
			constrainInput : true
		});
		
		if(n == 4) {
			document.candidateform.moretemps.disabled = true;
		}
	}
	document.candidateform.lesstemps.disabled = false;
}

// removes ones of their available date ranges
function less_temps() {
	n = $("#temp_periods > div").length;
	if(n > 1) {
		$("#temp_periods > div:last").remove();
	}
	if(n == 2) {
		document.candidateform.lesstemps.disabled = true;
	}
	document.candidateform.moretemps.disabled = false;
}

// transform a couple of nested category lists into tree things
$(document).ready(function(){
	$("#jbe_cats_tree").treeview({ collapsed: true });
	$("#jbe_locs_tree").treeview({ collapsed: true });
	$("#candidate_locs_tree").treeview({ collapsed: true });
	
	$(".datepicker").datepicker({
		dateFormat : "dd M yy",
		duration : "",
		constrainInput : true
	});
});

// adds another keyword bitty
function more_keywords() {
	n = $("#keywords_list > li").length;
	if(n < 10) {
		$("#keywords_list").append('<li><input type="text" name="keywords[]" value="" size="40" /></li>');
		if(n == 9) {
			$("#more_keywords_button").attr("disabled", "disabled");
		}
	}
	$("#less_keywords_button").attr("disabled", "");
}

// removes ones of their keywords fields
function less_keywords() {
	n = $("#keywords_list > li").length;
	if(n > 3) {
		$("#keywords_list > li:last").remove();
	}
	if(n == 4) {
		$("#less_keywords_button").attr("disabled", "disabled");
	}
	$("#more_keywords_button").attr("disabled", "");
}


