$(document).ready(function() {
LoadRegions();    
});
function LoadRegions(){
jQuery.getJSON("/Vacancy/LoadRegions.castle?ms=" + new Date().getTime(), function(j){ 
var options = '';
options +='<option value="NotSelected">Select your Region</option>';
for (var i = 0; i < j.length; i++) 
{
options += '<option value=\"' + j[i].optionValue + '\">' + j[i].optionDisplay + '</option>'; 
} 
jQuery("select#Region").html(options);
updateSalarySelect();
});    
}
function updateSalarySelect(){
if(jQuery("select#Region").val() == "NotSelected")
{
loadingSelection(jQuery("select#BasicSalary"), 'Select your Salary');
loadingSelection(jQuery("select#MarketSector"), 'Select your Sector');
loadingSelection(jQuery("select#JobFunction"), 'Select your Job Function');
}
else
{
loadingSelection(jQuery("select#BasicSalary"), 'Select your Salary');
loadingSelection(jQuery("select#MarketSector"), 'Select your Sector');
loadingSelection(jQuery("select#JobFunction"), 'Select your Job Function');
jQuery.getJSON("/Vacancy/LoadSalaries.castle",{region: jQuery("select#Region").val(), ajax: 'true'}, function(j){ 
var options = '';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'; 
}
jQuery("select#BasicSalary").html(options);
updateSectorSelect();
});    
}
}
function updateSectorSelect(){
if(jQuery("select#BasicSalary").val() == "NotSelected")
{
loadingSelection(jQuery("select#MarketSector"));
loadingSelection(jQuery("select#JobFunction"));
}
else
{
loadingSelection(jQuery("select#MarketSector"));
loadingSelection(jQuery("select#JobFunction"));
jQuery.getJSON("/Vacancy/LoadSectors.castle",{region: jQuery("select#Region").val(), salary: jQuery("select#BasicSalary").val(), ajax: 'true'}, function(j){ 
var options = '';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'; 
}
jQuery("select#MarketSector").html(options);
updateJobFunctionSelect();
});    
}
}
function updateJobFunctionSelect(){
if(jQuery("select#model").val() == "NotSelected")
{
loadingSelection(jQuery("select#JobFunction"));
}
else
{
loadingOption(jQuery("select#JobFunction"));
jQuery.getJSON("/Vacancy/LoadJobFunctions.castle", {region: jQuery("select#Region").val(), salary: jQuery("select#BasicSalary").val(), marketsector: jQuery("select#MarketSector").val(), ajax: 'true'} , 
function(j){ 
var options = '';
for (var i = 0; i < j.length; i++) 
{
options += '<option value=\"' + j[i].optionValue + '\">' + j[i].optionDisplay + '</option>'; 
} 
jQuery("select#JobFunction").html(options);
});  
}  
}
function loadingOption(select) {
select.html('<option>Loading...</option>');
}
function loadingSelection(select) {
select.html('<option value="NotSelected">...</option>');
}
function loadingSelection(select, text) {
select.html('<option value="NotSelected">' + text + '</option>');
}