var Behavior_Location = {
	// Defaultmeldungen (werden aus display:none-DIVs befuellt), falls die Ajax-Verbindung fehlschaegt
	innerHTML_if_no_data : new Array(),
	
	// Defaultmeldungen (werden aus display:none-DIVs befuellt), falls in der uebergeordneten Select-Box [alle *] ausgewaehlt ist
	innerHTML_disabled : new Array(),

	// sammelt die Ausgaben des Controllers wieder ein
	prefill : new Array(),

	// allgemeine Ajax-Anfrage-Funktion, soll von speziellen Anfrage-Funktionen ge-wrapp-ed werden
	getList : function(actionName,parentId,selectedId,onCompleteCallbackFunction) {
		var parent_param = ('' != parentId) ? '&parent='+parentId : '&parent=0';
		var selected_param = ('' != selectedId) ? '&selected='+selectedId : '&selected=0';
		var language_param = '&LANGUAGE='+LANGUAGE;
		var style_param = '&style='+this.selectStyle;
		var request_url = AJAX_URL+'ajax.php?controller=location&action='+actionName+parent_param+selected_param+language_param+style_param;
		new Ajax.Request(  request_url, {onComplete: onCompleteCallbackFunction.bind(this)}  );
	},

	// spezielle Ajax-Anfrage-Funktionen, das sind alles Wrapper fuer die allgemeine Ajax-Anfrage-Funktion
	getRegionList : function(countryId) {
		if (('' == countryId) || (0 == countryId)){
			$('Select_Regionid'	).innerHTML = this.innerHTML_disabled['Select_Regionid'	];
			$('Select_Cityid'	).innerHTML = this.innerHTML_disabled['Select_Cityid'	];
			$('Select_Schoolid'	).innerHTML = this.innerHTML_disabled['Select_Schoolid'	];
		} else {
			this.getList('getregion',	countryId,	this.prefill['Regionid'	],	this.displayRegionList	);
		}
	},

	getCityList : function(regionId) {
		if (('' == regionId) || (0 == regionId)){
			$('Select_Cityid'	).innerHTML = this.innerHTML_disabled['Select_Cityid'	];
			$('Select_Schoolid'	).innerHTML = this.innerHTML_disabled['Select_Schoolid'	];
		} else {
			this.getList('getcity',		regionId,	this.prefill['Cityid'	],	this.displayCityList	);
		}
	},

	getSchoolList : function(cityId) {
		if (('' == cityId) || (0 == cityId)){
			$('Select_Schoolid'	).innerHTML = this.innerHTML_disabled['Select_Schoolid'	];
		} else {
			this.getList('getschool',	cityId,		this.prefill['Schoolid'	],	this.displaySchoolList	);
		}
	},

	// allgemeine Callback-Funktion fuer die Verarbeitung der Ajax-Antworten, soll von speziellen Callbacks ge-wrapp-ed werden
	ajaxCallback : function(response,elementIdToBeFilled,elementIdSelectBox,followUpFunction) {
		if (response.readyState == 4 && response.responseText) {
			if (/disabled/.test(response.responseText)) {
				$(elementIdToBeFilled).innerHTML = this.innerHTML_disabled[elementIdToBeFilled];
			} else {
				$(elementIdToBeFilled).innerHTML = response.responseText;
				if (null != followUpFunction) {
					followUpFunction.call(Behavior_Location,$(elementIdSelectBox).value);
				}
			}
		} else {
			$(elementIdToBeFilled).innerHTML = this.innerHTML_if_no_data[elementIdToBeFilled];
		}
		Attitude.start(); // Verhaltens-Anknuepfung auch fuer neu erstellte Formular-Elemente
	},

	// spezielle Callback-Funktionen fuer die Verarbeitung der Ajax-Antworten, das sind alles Wrapper fuer die allgemeine Ajax-Callback-Funktion
	displayRegionList	: function(response) {  this.ajaxCallback(response,	'Select_Regionid',	'Location_Regionid',	this.getCityList	);  },
	displayCityList		: function(response) {  this.ajaxCallback(response,	'Select_Cityid',	'Location_Cityid',	this.getSchoolList	);  },
	displaySchoolList	: function(response) {  this.ajaxCallback(response,	'Select_Schoolid',	'Location_Schoolid',	null);  },

	init : function() {  
		// Defaultmeldungen (werden aus display:none-DIVs befuellt), falls die Ajax-Verbindung fehlschaegt
		this.innerHTML_if_no_data['Select_Regionid'	] = $('Select_Regionid_if_no_data'	).innerHTML;
		this.innerHTML_if_no_data['Select_Cityid'	] = $('Select_Cityid_if_no_data'	).innerHTML;
		this.innerHTML_if_no_data['Select_Schoolid'	] = $('Select_Schoolid_if_no_data'	).innerHTML;

		// Defaultmeldungen (werden aus display:none-DIVs befuellt), falls in der uebergeordneten Select-Box "[alle *]" ausgewaehlt ist
		this.innerHTML_disabled['Select_Regionid'	] = $('Select_Regionid_disabled'	).innerHTML;
		this.innerHTML_disabled['Select_Cityid'		] = $('Select_Cityid_disabled'		).innerHTML;
		this.innerHTML_disabled['Select_Schoolid'	] = $('Select_Schoolid_disabled'	).innerHTML;

		// sammelt die Ausgaben des Controllers wieder ein
		this.prefill['Countryid'] = Location_Prefill_Countryid;
		this.prefill['Regionid'	] = Location_Prefill_Regionid;
		this.prefill['Cityid'	] = Location_Prefill_Cityid;
		this.prefill['Schoolid'	] = Location_Prefill_Schoolid;

		this.selectStyle = Location_Style;

		this.getRegionList(this.prefill['Countryid']);
	}
};


var behavior_sheet = {	
	'#Location_Countryid' :	function(element) {
		element.onchange = function(){
			Behavior_Location.getRegionList.call(Behavior_Location,this.value);
		}		
	},

	'#Location_Regionid' :	function(element) {
		element.onchange = function(){
			Behavior_Location.getCityList.call(Behavior_Location,this.value);
		}		
	},

	'#Location_Cityid' :		function(element) {
		element.onchange = function(){
			Behavior_Location.getSchoolList.call(Behavior_Location,this.value);
		}		
	}
};
Attitude.append(behavior_sheet);

