// JavaScript Document

$(document).ready(function() {
	
	$('#postform').validate();

	$('.new-topic').click(function() {
		$('#form1loader').submit();
	});
	
	$('#jmAirport').change(function() {
	var portname = $('select[name=jmAirport]').val();
		if($('#portname').length!=0) {$('#portname').remove();}
		$('#criteria_list').append('<li id="portname">Near ' + portname + ' Airport</li>');
	});
	
	$('#setlocval').click(function() {setlocval()});
	
	
			var step2port = $('#step2port').val();
			if(step2port!='') {
				$('select[name=jmAirport] option[value^='+step2port+']').attr("selected", "selected");
			}
			
			var step2hours = $('#step2hours').val();
			if(step2hours!='') {
				$('select[name=lstHoursFromAirport] option[value^='+step2hours+']').attr("selected", "selected");
			}
	
	//alert('change');
	//function for diasble selection
	$('#jmLocation').change(function() {
	diasble_selection();
	});
	//function for select countries
	select_countries();
	//function for change region and resort
	$('#jmCountry').change(function() {
	change_region();
	change_resort();
	setlocval();
	});
	$('#jmResort').change(function() {
	setlocval();
	});
	//function for change  resort when change region
	$('#jmArea').change(function() {
	change_resort();
	setlocval();
	});
	});
function diasble_selection() {
 		var selected_value=$('select.#jmLocation option:selected').val();
		if(selected_value=='dontMind')
		{
      	$("#jmCountry").attr("disabled", true); 
	 	$("#jmArea").attr("disabled", true);
	  	$("#jmResort").attr("disabled", true);
		}	
			else
			{
			select_countries();
			change_region();
			change_resort();
			$("#jmCountry").attr("disabled", false); 
	  		$("#jmArea").attr("disabled", false);
	  		$("#jmResort").attr("disabled", false);
			}
}
function select_countries(){
	
	$.getJSON('my-templates/chalets/db/countries.php',function(data) {
		
		var select = $('#jmCountry');
        var options = select.attr('options');
        $('option', select).remove();
		
		for (key in data) {
		$('#jmCountry').append('<option value="' + data[key] + '">' + data[key] + '</option>');
   	 	}
		
			var step2country = $('#step2country').val();
				if(step2country!='') {
					$('select[name=jmCountry] option[value^='+step2country+']').attr("selected", "selected");
					change_region();
				}
			 
	});
}
function change_region(){
$.getJSON('my-templates/chalets/db/regions.php',{country:$('select.#jmCountry option:selected').val()},function(data) {
		$('#ani_region').show();
		
		var select = $('#jmArea');
        var options = select.attr('options');
        $('option', select).remove();
		
		for (key in data) {
		$('#jmArea').append('<option value="' + data[key] + '">' + data[key] + '</option>');
   	 	}
 		$('#ani_region').fadeOut();
		
			var step2region = $('#step2region').val();
				if(step2region!='') {
					$('select[name=jmArea] option[value^='+step2region+']').attr("selected", "selected");
					change_resort();
				}
	});
}
function change_resort(){
$.getJSON('my-templates/chalets/db/resorts.php',{country:$('select.#jmCountry option:selected').val(),region:$('select.#jmArea option:selected').val()},function(data) {
		$('#ani_resort').show();
		
		var select = $('#jmResort');
        var options = select.attr('options');
        $('option', select).remove();
		
		
		for (key in data) {
		$('#jmResort').append('<option value="' + data[key] + '">' + data[key] + '</option>');
   	 	}
		
		
 		$('#ani_resort').fadeOut();
		
		var step2resort = $('#step2resort').val();
				if(step2resort!='') {
					$('select[name=jmResort] option[value^='+step2resort+']').attr("selected", "selected");
					setlocval();
				}
	});
}

function setlocval() {
	var country = $('select[name=jmCountry]').val(); if(country=='Any') {country = 'Any country'};
	var area = $('select[name=jmArea]').val(); if(area=='Any') {area = 'Any area'};
	var resort = $('select[name=jmResort]').val(); if(resort=='Any') {resort = 'Any resort'};
//	var locname = resort + ' > ' + area + ' > ' + country;
	var locname = country + ', ' + area + ', ' + resort;

		if($('#locname').length!=0) {$('#locname').remove();}
		if(locname == 'Any > Any > Any') { $('#criteria_list').append('<li id="locname">Location: No Preference</li>'); }
		else { $('#criteria_list').append('<li id="locname">In ' + locname + ' </li>'); }
		
		$('#txtLocation').attr('value', locname);
	}
