$(document).ready(function(){
	select_province();
	select_city();
	get_cities();
	$("#province_id").change(select_province);
	$("#city_id").change(select_city);
});

function select_province() {
	if(document.getElementById('province_id') && document.getElementById('city_id')) {
		var city_select = document.getElementById('city_id');
		if(document.getElementById('venue_id')) var venue_select = document.getElementById('venue_id');
		var elm = document.getElementById('province_id');
		var province_id = elm.value;
		
		var x = 0;
		var prov_has_city = false;
		var choose_city = false;
		city_select.options.length = 0;
		if(document.getElementById('venue_id') && !document.getElementById('action')) venue_select.options.length = 0;
		
		/*for(i=0;i<(cities.city.length);i++) {
			if(cities.city[i]['province_id'] == province_id) {
				if(!choose_city) {city_select.options[x] = new Option(choose_one,''); choose_city = true; x++;}
				city_select.options[x] = new Option(cities.city[i]['city'],cities.city[i]['city_id']);
				if(typeof(city_id) != 'undefined') {
					if(cities.city[i]['city_id'] == city_id) city_select.options[x].selected = 'selected';
				}
				prov_has_city = true;
				x++;
			}
		}*/
		
		for(var i in cities.city) {
			if(cities.city[i]['province_id'] == province_id) {
				if(!choose_city) {city_select.options[x] = new Option(choose_one,''); choose_city = true; x++;}
				city_select.options[x] = new Option(cities.city[i]['city'],cities.city[i]['city_id']);
				if(typeof(city_id) != 'undefined') {
					if(cities.city[i]['city_id'] == city_id) city_select.options[x].selected = 'selected';
				}
				prov_has_city = true;
				x++;
			}
		}
		
		if(!prov_has_city) city_select.options[x] = new Option(no_cities,'');
		
	}
}

function select_city() {
	if(document.getElementById('city_id') && document.getElementById('venue_id') && !document.getElementById('venue_list')) {
		var venue_select = document.getElementById('venue_id');
		var elm = document.getElementById('city_id');
		var city_id = elm.value;
		
		var x = 0;
		var city_has_venue = false;
		var choose_venue = false;
		venue_select.options.length = 0;
		
		for(i=0;i<(venues.venue.length);i++) {
			if(venues.venue[i]['city_id'] == city_id) {
				if(!choose_venue) {venue_select.options[x] = new Option(choose_one,''); choose_venue = true; x++;}
				venue_select.options[x] = new Option(venues.venue[i]['venue'],venues.venue[i]['venue_id']);
				if(typeof(venue_id) != 'undefined') {
					if(venues.venue[i]['venue_id'] == venue_id) venue_select.options[x].selected = 'selected';
				}
				city_has_venue = true;
				x++;
			}
		}
		
		if(!city_has_venue) venue_select.options[x] = new Option(no_venues,'');
	}
	
	if(document.getElementById('city_id') && document.getElementById('venue_list')) {
		var venue_list = document.getElementById('venue_list');
		var elm = document.getElementById('province_id');
		var province_id = elm.value;
		var elm2 = document.getElementById('city_id');
		var city_id = elm2.value;
		var province_name = elm.options[elm.selectedIndex].text;
		var city_name = elm2.options[elm2.selectedIndex].text;
		
		if(elm.selectedIndex > 0) venue_list.innerHTML = '<h3>' + locations_for + ' ' + city_name + ', ' + province_name + '</h3>';
		
		for(i=0;i<(venues.venue.length);i++) {
			if(venues.venue[i]['city_id'] == city_id) {
				//venue_list.innerHTML += '<a href="?pg=docenten/venues&amp;action=edit&amp;venue_id=' + venues.venue[i]['venue_id'] + '"><img src="_images/b_edit.png" alt="edit"></a>' + " <a href=\"?pg=main/venue&amp;height=350&amp;width=600&amp;id=" + venues.venue[i]['venue_id']+'" class="thickbox" title="'+venues.venue[i]['venue']+'">' + venues.venue[i]['venue'] + '</a><br />';
				image = '<img';
				image += ' id="edit_'+ venues.venue[i]['venue_id'] + '"';
				image += ' src="_images/b_edit.png"';
				image += ' alt="edit"';
				image += ' title="edit"';
				image += ' class="edit_icon"';
				image += ' /> ';
				
				image += '<a';
				image += ' href="?pg=main/venue&amp;height=350&amp;width=600';
				image += '&amp;id=' + venues.venue[i]['venue_id'] + '"';
				image += ' class="venue_thickbox"';
				image += ' title="' + encodeURI(venues.venue[i]['venue']) + '"';
				image += '>';
				image += venues.venue[i]['venue'];
				image += '</a><br />';
				
				$("#venue_list").append(image);
			}
		}
		
		$("a.venue_thickbox").click(function() {
			this.href += "&ref=1";
		});

		$("img.edit_icon").hover(
			function() {
				$(this).css({cursor:"pointer"});
			},
			function() {
				$(this).css({cursor:"default"});
			}
		).click(function() {
			$("#form_title").html(venue_edit);
			$("#new_button").show();
			test = $(this).attr("id").split('_');
			$.post("?pg=docenten/venues_help", {venue_id:test[1],action:'edit'}, fill_venue, "json");
		});
		$("#new_button").click(function() {
			window.location.href = '?pg=' + pg + '&cc=' + cc + '&lc=' + lc;
		});
		
		tb_init('a.venue_thickbox');
		
	}
	
}

function fill_venue(data){
	//data.form_province_id;
	//data.form_city_id;
	//location = data.form_location;
	$("#venue_details").highlightFade({color:'#FFFF66',speed:1000,iterator:'sinusoidal'});
	$("#venue_description").highlightFade({color:'#FFFF66',speed:1000,iterator:'sinusoidal'});
	$("#venue_id").val(data.form_venue_id);
	$("#location").val(data.form_location);
	$("#straat").val(data.form_straat);
	$("#huis_nr").val(data.form_huis_nr);
	$("#pc_nr").val(data.form_pc_nr);
	$("#pc_ltrs").val(data.form_pc_ltrs);
	$("#telefoon").val(data.form_telefoon);
	$("#email").val(data.form_email);
	$("#website").val(data.form_website);
	$("#description_nl").val(data.form_description_nl);
	$("#description_en").val(data.form_description_en);
}

function get_cities() {
	if(document.getElementById('province_id') && document.getElementById('city') && document.getElementById('province_cities')) {
		var province_elm = document.getElementById('province_id');
		var province_cities = document.getElementById('province_cities');
		var province_id = province_elm.value;
		
		for(i=0;i<(provinces.province.length);i++) {
			if(provinces.province[i]['province_id'] == province_id) {
				province_cities.innerHTML = '<h2>' + cities_for + ' ' + provinces.province[i]['province'] + '</h2>';
			}
		}
		
		/*for(i=0;i<(cities.city.length);i++) {
			if(cities.city[i]['province_id'] == province_id) {
				province_cities.innerHTML += cities.city[i]['city'] + '<br />';
			}
		}*/
		
		for(var i in cities.city) {
			if(cities.city[i]['province_id'] == province_id) {
				province_cities.innerHTML += cities.city[i]['city'] + '<br />';
			}
		}
	}
}

