var selectedCountry = "";
var selectedCity = "";

// Start function when DOM has completely loaded

$(document).ready(function(){ 
	
	$("#veranstalter").append("das ist ein test");
	//on loading page do:
	var veranstalter = "ajaxProxy.php?csurl=" + escape("http://www.giata-xml.de/?uid=171337&pwd=8dce0&sc=list&list=provider");	
		getGiataXML("#veranstalter",veranstalter,"pr","vn","vc");
	
	//if provider is changed do:
	$("#veranstalter").change(function(){	
		var kataloge = "ajaxProxy.php?csurl=" + escape("http://www.giata-xml.de/?uid=171337&pwd=8dce0&sc=list&list=catalog&vc=" + $("#veranstalter").val());
		getGiataXML("#kataloge",kataloge,"cr","cn","cid");
	});

	//if provider is changed do:
	$("#kataloge").change(function(){	
		var content = "ajaxProxy.php?csurl=" + escape("http://www.giata-xml.de/?uid=171337&pwd=8dce0&sc=list&list=catalog&vc=" + $("#veranstalter").val() + "&katid=" + $("#kataloge").val());	
		getGiataContent(content);
	});
	
	
});

function getGiataXML(element,url,xmlRoot,xmlName,xmlValue){
	
	$.get(url,{ },function(xml){
		// Build an HTML string
		myHTMLOutput = '';

		// Run the function for each student tag in the XML file
		$(xmlRoot,xml).each(function(i) {
			ln = $(this).find(xmlName).text();
			lc = $(this).find(xmlValue).text();

			// Build row HTML data and store in string
			mydata = BuildSelectionHTML(ln,lc);
			myHTMLOutput = myHTMLOutput + mydata;
		});

		// Update the DIV called Content Area with the HTML string
		$(element).empty();
		$(element).append('<option>bitte wählen...</option>')
		$(element).append(myHTMLOutput);
		
	});
	
}

function getHotelText(url){			
	$.get(url,{ },function(text){
		$("#beschreibung").html(text.substring(0,250) + "...");
	});

}

function getGiataContent(url){
	
	$.get(url,{},function(xml){
		
		$('cr',xml).each(function(i){
		
			if ($(this).find("cid").text() == $("#kataloge").val()){
				pdf = $(this).find("pdf_url").text();
				pic = $(this).find("catalog_cover_url").text();
				name = $(this).find("cn").text();
			}
			
		});
		
		if (pdf != ""){
			$("#catalogPicture").empty();
			$("#catalogPicture").append('<a href="' + pdf + '" target="_blank"><img src="' + pic + '" style="height: 130px" title="' + name + '" alt="' + name + '"/></a>');

			$("#catalogLink").empty();
			$("#catalogLink").append('<a href="' + pdf + '" target="_blank">Katalog "' + name + '" als Pdf herunterladen</a>');
		}
		else {
			$("#catalogLink").empty();
			$("#catalogLink").append('Dieser Katalog ist derzeit leider nicht online verf&uuml;gbar!');
		}
		
	});
}
	
function BuildSelectionHTML(ln,lc){
	// Build HTML string and return
	output = '';
	output += '<option value="'+ lc + '">' + ln + '</option>';
	return output;
}
	

