var baseURL = null;
var type = null;

// overwrite onload method/event
onload = init;

function init( t ) {
	var s = window.location.href;
	var idx = s.lastIndexOf( "/" );
	baseURL = s.substring( 0, idx+1 ) + "control?";
	
	// set type of the application (current year or comparison of two years)
	if( t == "comp" ){
		type = t;
	} else {
		type = "current";
	}	
	loadMap();
	var selectFederalStates = document.getElementById("FederalStates");
	if(selectFederalStates != null  && selectFederalStates.length > 0){
		selectFederalStates[0].selected="true";
	}
}

/*
* loads the inital click map
*/
function loadMap(){
	submitGetRequest( baseURL + "action=getMap&type="+type, 
		function(result) {
			var div = document.getElementById("MAPAREA");
			div.innerHTML = result;
		}, 
		null, true );
}

/*
* invoked when selecting a federal state
*/
function selectBL(select) {
	var value = select.options[select.options.selectedIndex].value;
	submitGetRequest( baseURL + "action=getAdminDistricts&federalstate=" + value, 
		function (result) {
		
			var select = document.getElementById("AdminDistricts");
			var adminDistrictToSelect = document.getElementById("AdminDistrictToSelect").value;
			var adminDistricts = JSON.parse( result, null );
			// remove all old entries from the selection list!
			select.innerHTML = "";
			// and insert the new entries
			var optionToSelect = 0;
			if( adminDistricts.length > 0 ){
				for ( var i = 0; i < adminDistricts.length; i++ ) {
					// set index of the option to select admin district with key in the hidden element 
					if( adminDistrictToSelect.length > 0 && adminDistricts[i].krsId == adminDistrictToSelect){
						optionToSelect = i;
					}
					var newOption = new Option( adminDistricts[i].name , adminDistricts[i].krsId, false, false );
	  				select.options[i] = newOption;
				}
				select.style.color = "black";
			} else {
				select.style.color = "gray";
				var newOption = new Option( "Bitte erst Bundesland ausw\u00E4hlen" , -1, false, true );
	  				select.options[0] = newOption;
			}	
			// select one option
			select.options.selectedIndex = optionToSelect;
			
			updateCreateButton();
		}, null, true );
		
	submitGetRequest( baseURL + "action=getMap&federalstate=" + value + "&type=" + type, 
		function (result) {
			var map = document.getElementById("MAPAREA");
			map.innerHTML = result;
		}, null, true );
}

/*
* enables or diables the button to create an index
*/
function updateCreateButton(){
	var select = document.getElementById("AdminDistricts");
	var selectedIndex = select.options.selectedIndex;
	var button = document.getElementById("CreateGIProfilButton");
	if( selectedIndex > -1 && select.options[selectedIndex].value != -1 ){
		button.disabled="";
	} else {
		button.disabled="disabled";
	}
}

/*
* creates the index and shows them
*/
function createGIProfil(){
	var select = document.getElementById("AdminDistricts");
	var selectedIndex = select.options.selectedIndex;
	if( selectedIndex > - 1){
		var value = select.options[selectedIndex].value;
		submitGetRequest( baseURL + "action=createProfile&AdminDistrict=" + value + "&type=" + type, 
			function (result) {		
				if( result != null ){	
					openPDF( "profiles/" + result );
				} else {
					alert("Beim Erzeugen des Profils ist ein Fehler aufgetreten!");
				}
			}, null, true );
	}
	
}

/*
* opens a new window with information about how to read the profile
*/
function openHelp(){
	window.open("http://www.genderindex.de/detail-profile.html", "_blank");
}

/* 
* 'private'-methode to open a pdf in a new window 
* window.open does not work in IE 6 and IE 7 !? 
*/
function openPDF( url ) {
	var target = document.getElementById('openPdfForm');
	target.action = url;
	target.submit();
}
