var __prefix = 'ls_';
var errorPLZ = 'plz fehler';
var errorOrt = 'ort fehler';
var errorMatch = 'passt nicht';

function preparePLZOrt() {
	if (_('iOrt').style.display == 'none') {
		_remove(__prefix + 'iOrt');
	}
	
	if (_('sOrt').style.display == 'none') {
		_remove(__prefix + 'sOrt');
	}
	
	if (_('xOrt').style.display == 'none') {
		_remove(__prefix + 'xOrt');
	}
	
	if (_('iPLZ').style.display == 'none') {
		_remove(__prefix + 'iPLZ');
	}
	
	if (_('sPLZ').style.display == 'none') {
		_remove(__prefix + 'sPLZ');
	}
}

function resetPLZOrt(del, nocheck) {
	_hide('sOrt');
	_hide('xOrt');
	_show('iOrt');
	_hide('sPLZ');
	_show('iPLZ');
	_(__prefix + 'iOrt').message = '';
	window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iOrt'), _(__prefix + 'iOrt').message);
	_(__prefix + 'iPLZ').message = '';
	window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iPLZ'), _(__prefix + 'iPLZ').message);
	if (del) {
		_addClassName(__prefix + 'iOrt', 'invalid');
		_addClassName(__prefix + 'iPLZ', 'invalid');
		_(__prefix + 'iPLZ').value = '';
		_(__prefix + 'iOrt').value = '';
	} else {
		if (_F(__prefix + 'iPLZ').length < 4) {
			_addClassName(__prefix + 'iPLZ', 'invalid');
		} else {
			_removeClassName(__prefix + 'iPLZ', 'invalid');
		}
		
		if (_F(__prefix + 'iOrt').length < 2) {
			_addClassName(__prefix + 'iOrt', 'invalid');
		} else {
			_removeClassName(__prefix + 'iOrt', 'invalid');
		}
	}
	
	/*
	updateField('LSPLZ', '');
	updateField('LSOrt', '');
	*/
	
	if (!nocheck) checkPLZOrt('');
}

function changeLand() {
	if (_F('t_Land') == 'CH' ? !window.__ch : window.__ch) {
		resetPLZOrt(true, true);
	}
	
	window.__ch = _F('t_Land') == 'CH';
}

function checkPLZOrt(req) {
	if (!window.__ch) {
		resetPLZOrt(false, true);
		return true;
	}
	
	switch (req) {
		case 'ort':
			_removeClassName(__prefix + 'iOrt', 'invalid');
			_removeClassName(__prefix + 'iPLZ', 'invalid');
			if (_F(__prefix + 'iPLZ').length >= 4) {
				var land = _(__prefix + 'Land') ? _F(__prefix + 'Land') : 'CH';
				if (land == 'CH') {
					if (_F(__prefix + 'iPLZ').length == 4) {
						new Ajax.Request('/ajax/plzort/index.html?t_req=ort&t_plz=' + _F(__prefix + 'iPLZ') + '&t_ort=', {
							onSuccess: function(tp) {
								try {
									txt = tp.responseText.strip();
									txt = txt.split('§');
									cnt = parseInt(txt[0]);
									if (cnt == 0) {
										if (!_hasClassName(__prefix + 'iPLZ', 'invalid')) {
											_addClassName(__prefix + 'iPLZ', 'invalid');
										}
										_(__prefix + 'iPLZ').message = errorPLZ;
										window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iPLZ'), _(__prefix + 'iPLZ').message);
										_(__prefix + 'iPLZ').message = '';
									} else {
										_removeClassName(__prefix + 'iPLZ', 'invalid');
										_(__prefix + 'iPLZ').message = '';
										window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iPLZ'), _(__prefix + 'iPLZ').message);
										_(__prefix + 'iOrt').message = '';
										window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iOrt'), _(__prefix + 'iOrt').message);
										
										obj = txt[1].split('¬');
										if (cnt == 1) {
											_(__prefix + 'iOrt').value = obj[0];
											_hide('sOrt');
											_hide('xOrt');
											_show('iOrt');
											setTimeout(function() {
												_(__prefix + 'iOrt').focus();
												_(__prefix + 'iOrt').select();
											}, 50);
										} else {
											sel = _(__prefix + 'sOrt');
											while (sel.options.length > 0) {
												sel.options[0] = null;
											}
											var el, i = 0;
											while (el = obj[i++]) {
												sel.options[i - 1] = new Option(el, el);
											}
											_hide('iOrt');
											_hide('xOrt');
											_show('sOrt');
										}
									}
									
									/*
									if (_visible('iPLZ')) {
										updateField('LSPLZ', _F(__prefix + 'iPLZ'));
									} else {
										updateField('LSPLZ', _F(__prefix + 'sPLZ'));
									}
									
									if (_visible('xOrt')) {
										updateField('LSOrt', _F(__prefix + 'xOrt'));
									} else {
										if (_visible('sOrt')) {
											updateField('LSOrt', _F(__prefix + 'sOrt'));
										} else {
											updateField('LSOrt', _F(__prefix + 'iOrt'));
										}
									}
									*/
								} catch (e) {
									console.log(e);
								}
							}
						});
					} else {
						if (!_hasClassName(__prefix + 'iPLZ', 'invalid')) {
							_addClassName(__prefix + 'iPLZ', 'invalid');
						}
						_(__prefix + 'iOrt').value = '';
						_hide('xOrt');
						_hide('sOrt');
						_show('iOrt');
						_(__prefix + 'iPLZ').message = errorPLZ;
						window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iPLZ'), _(__prefix + 'iPLZ').message);
						_(__prefix + 'iPLZ').message = '';
					}
				} else {
					_removeClassName(__prefix + 'iPLZ', 'invalid');
					_(__prefix + 'iPLZ').message = '';
					window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iPLZ'), _(__prefix + 'iPLZ').message);
				}
			} else {
				if (!_hasClassName(__prefix + 'iPLZ', 'invalid')) {
					_addClassName(__prefix + 'iPLZ', 'invalid');
				}
				
				_(__prefix + 'iOrt').value = '';
				_hide('xOrt');
				_hide('sOrt');
				_show('iOrt');
				
				resetPLZOrt();
			}
			
			break;
	
		case 'plz':
			_removeClassName(__prefix + 'iOrt', 'invalid');
			_removeClassName(__prefix + 'iPLZ', 'invalid');
			if (_F(__prefix + 'iOrt').length >= 2) {
				var land = _(__prefix + 'Land') ? _F(__prefix + 'Land') : 'CH';
				if (land == 'CH') {
					new Ajax.Request('/ajax/plzort/index.html?t_req=plz&t_plz=&t_ort=' + P.encodeURIComponent(_F(__prefix + 'iOrt')) + '&t_check=1', {
						onSuccess: function(tp) {
							try {
								txt = tp.responseText.strip();
								txt = txt.split('§');
								cnt = parseInt(txt[0]);
								if (cnt == 0) {
									if (!_hasClassName(__prefix + 'iOrt', 'invalid')) {
										_addClassName(__prefix + 'iOrt', 'invalid');
									}
				
									_(__prefix + 'iOrt').message = errorOrt;
									window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iOrt'), _(__prefix + 'iOrt').message);
									_(__prefix + 'iOrt').message = '';
								} else {
									_removeClassName(__prefix + 'iOrt', 'invalid');
									_(__prefix + 'iOrt').message = '';
									window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iOrt'), _(__prefix + 'iOrt').message);
									
									obj = txt[1].split('¬');
									if (cnt == -1) {
										_(__prefix + 'iOrt').value = obj[0];
										checkPLZOrt('plzfix');
									} else {
										if (cnt < 0) {
											sel = _(__prefix + 'xOrt');
											while (sel.options.length > 0) {
												sel.options[0] = null;
											}
											var el, i = 0;
											while (el = obj[i++]) {
												sel.options[i - 1] = new Option(el, el);
											}
											_hide('iOrt');
											_hide('sOrt');
											_show('xOrt');
											checkPLZOrt('plzfix');
										} else {
											_removeClassName(__prefix + 'iPLZ', 'invalid');
											_(__prefix + 'iPLZ').message = '';
											window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iPLZ'), _(__prefix + 'iPLZ').message);
											if (cnt == 1) {
												_(__prefix + 'iPLZ').value = obj[0];
												_hide('sPLZ');
												_show('iPLZ');
												setTimeout(function() {
													_(__prefix + 'iPLZ').focus();
													_(__prefix + 'iPLZ').select();
												}, 50);
											} else {
												sel = _(__prefix + 'sPLZ');
												while (sel.options.length > 0) {
													sel.options[0] = null;
												}
												var el, i = 0;
												while (el = obj[i++]) {
													sel.options[i - 1] = new Option(el, el);
												}
												_hide('iPLZ');
												_hide('sPLZ');
												setTimeout(function() {
													_(__prefix + 'sPLZ').focus();
												}, 50);
											}
										}
									}
								}
								
								/*
								if (_visible('iPLZ')) {
									updateField('LSPLZ', _F(__prefix + 'iPLZ'));
								} else {
									updateField('LSPLZ', _F(__prefix + 'sPLZ'));
								}
								
								if (_visible('xOrt')) {
									updateField('LSOrt', _F(__prefix + 'xOrt'));
								} else {
									if (_visible('sOrt')) {
										updateField('LSOrt', _F(__prefix + 'sOrt'));
									} else {
										updateField('LSOrt', _F(__prefix + 'iOrt'));
									}
								}
								*/
							} catch (e) {
								console.log(e);
							}
						}
					});
				} else {
					_removeClassName(__prefix + 'iOrt', 'invalid');
					_(__prefix + 'iOrt').message = '';
					window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iOrt'), _(__prefix + 'iOrt').message);
				}
			} else {
				if (!_hasClassName(__prefix + 'iOrt', 'invalid')) {
					_addClassName(__prefix + 'iOrt', 'invalid');
				}
				
				_(__prefix + 'iPLZ').value = '';
				_hide('sPLZ');
				_show('iPLZ');
				
				resetPLZOrt();
			}
			
			break;
			
		case 'plzfix':
			try {
				if (_('iOrt').style.display != 'none') {
					var val = _F(__prefix + 'iOrt');
				} else {
					var val = _F(__prefix + 'xOrt');
				}
				_(__prefix + 'iOrt').message = '';
				window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iOrt'), _(__prefix + 'iOrt').message);
				_(__prefix + 'iPLZ').message = '';
				window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iPLZ'), _(__prefix + 'iPLZ').message);
				_removeClassName(__prefix + 'iOrt', 'invalid');
				_removeClassName(__prefix + 'iPLZ', 'invalid');
				new Ajax.Request('/ajax/plzort/index.html?t_req=plz&t_plz=&t_ort=' + P.encodeURIComponent(val) + '&t_check=0', {
					onSuccess: function(tp) {
						try {
							txt = tp.responseText.strip();
							txt = txt.split('§');
							cnt = parseInt(txt[0]);
							obj = txt[1].split('¬');
	
							if (cnt == 1) {
								_(__prefix + 'iPLZ').value = obj[0];
								_hide('sPLZ');
								_show('iPLZ');
								setTimeout(function() {
									_(__prefix + 'iPLZ').focus();
									_(__prefix + 'iPLZ').select();
								}, 50);
							} else {
								sel = _(__prefix + 'sPLZ');
								while (sel.options.length > 0) {
									sel.options[0] = null;
								}
								var el, i = 0;
								while (el = obj[i++]) {
									sel.options[i - 1] = new Option(el, el);
								}
								_hide('iPLZ');
								_show('sPLZ');
								setTimeout(function() {
									_(__prefix + 'sPLZ').focus();
								}, 50);
							}
							
							/*
							if (_visible('iPLZ')) {
								updateField('LSPLZ', _F(__prefix + 'iPLZ'));
							} else {
								updateField('LSPLZ', _F(__prefix + 'sPLZ'));
							}
							
							if (_visible('xOrt')) {
								updateField('LSOrt', _F(__prefix + 'xOrt'));
							} else {
								if (_visible('sOrt')) {
									updateField('LSOrt', _F(__prefix + 'sOrt'));
								} else {
									updateField('LSOrt', _F(__prefix + 'iOrt'));
								}
							}
							*/
						} catch (e) {
							console.log(e);
						}
					}
				});
			} catch (e) {
				//alert(e.message);
				console.log(e);
			}
			
			break;
			
		case '':
			_removeClassName(__prefix + 'iOrt', 'invalid');
			_removeClassName(__prefix + 'iPLZ', 'invalid');
			_(__prefix + 'iOrt').message = '';
			window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iOrt'), _(__prefix + 'iOrt').message);
			_(__prefix + 'iPLZ').message = '';
			window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iPLZ'), _(__prefix + 'iPLZ').message);
			
			var ret = true;
			var land = _(__prefix + 'Land') ? _F(__prefix + 'Land') : 'CH';
			if (land != 'CH') {
				if (_F(__prefix + 'iPLZ').length < 4) {
					_(__prefix + 'iPLZ').message = errorPLZ;
					window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iPLZ'), _(__prefix + 'iPLZ').message);
					if (!_hasClassName(__prefix + 'iPLZ', 'invalid')) {
						_addClassName(__prefix + 'iPLZ', 'invalid');
					}
					ret = false;
				}
				
				if (_F(__prefix + 'iOrt').length < 2) {
					_(__prefix + 'iOrt').message = errorOrt;
					window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iOrt'), _(__prefix + 'iOrt').message);
					if (!_hasClassName(__prefix + 'iOrt', 'invalid')) {
						_addClassName(__prefix + 'iOrt', 'invalid');
					}
					ret = false;
				}
				
				return ret;
			}
			
			if (_('iPLZ').style.display == 'none') {
				var plz = _F(__prefix + 'sPLZ');
			} else {
				var plz = _F(__prefix + 'iPLZ');
			}
			
			if (_('iOrt').style.display == 'none') {
				if (_('sOrt').style.display == 'none') {
					var ort = _F(__prefix + 'xOrt');
				} else {
					var ort = _F(__prefix + 'sOrt');
				}
			} else {
				var ort = _F(__prefix + 'iOrt');
			}
			
			if (plz.length != 4) {
				_(__prefix + 'iPLZ').message = errorPLZ;
				window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iPLZ'), _(__prefix + 'iPLZ').message);
				if (!_hasClassName(__prefix + 'iPLZ', 'invalid')) {
					_addClassName(__prefix + 'iPLZ', 'invalid');
				}
				ret = false;
			}
			if (ort.length < 2) {
				_(__prefix + 'iOrt').message = errorOrt;
				window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iOrt'), _(__prefix + 'iOrt').message);
				if (!_hasClassName(__prefix + 'iOrt', 'invalid')) {
					_addClassName(__prefix + 'iOrt', 'invalid');
				}
				ret = false;
			}
			
			if (!ret) return false;
			
			req = new Ajax.Request('/ajax/plzort/index.html?t_req=plzort&t_plz=' + plz + '&t_ort=' + P.encodeURIComponent(ort), {
				asynchronous: false
			});
			
			var res = req.transport.responseText.strip();
			if (res == '1') {
				/*
				if (_visible('iPLZ')) {
					updateField('LSPLZ', _F(__prefix + 'iPLZ'));
				} else {
					updateField('LSPLZ', _F(__prefix + 'sPLZ'));
				}
				
				if (_visible('xOrt')) {
					updateField('LSOrt', _F(__prefix + 'xOrt'));
				} else {
					if (_visible('sOrt')) {
						updateField('LSOrt', _F(__prefix + 'sOrt'));
					} else {
						updateField('LSOrt', _F(__prefix + 'iOrt'));
					}
				}
				*/
				
				return true;
			}
			
			switch (res) {
				case '0':
					if (!_hasClassName(__prefix + 'iOrt', 'invalid')) {
						_addClassName(__prefix + 'iOrt', 'invalid');
					}
					if (!_hasClassName(__prefix + 'iPLZ', 'invalid')) {
						_addClassName(__prefix + 'iPLZ', 'invalid');
					}
					_(__prefix + 'iOrt').message = errorMatch;
					window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iOrt'), _(__prefix + 'iOrt').message);
					break;
				case '-1':
					if (_hasClassName(__prefix + 'iOrt', 'invalid')) {
						_removeClassName(__prefix + 'iOrt', 'invalid');
					}
					if (!_hasClassName(__prefix + 'iPLZ', 'invalid')) {
						_addClassName(__prefix + 'iPLZ', 'invalid');
					}
					_(__prefix + 'iPLZ').message = errorPLZ;
					window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iPLZ'), _(__prefix + 'iPLZ').message);
					break;
				case '-2':
					if (!_hasClassName(__prefix + 'iOrt', 'invalid')) {
						_addClassName(__prefix + 'iOrt', 'invalid');
					}
					if (_hasClassName(__prefix + 'iPLZ', 'invalid')) {
						_removeClassName(__prefix + 'iPLZ', 'invalid');
					}
					_(__prefix + 'iOrt').message = errorOrt;
					window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iOrt'), _(__prefix + 'iOrt').message);
					break;
				case '-3':
					if (!_hasClassName(__prefix + 'iOrt', 'invalid')) {
						_addClassName(__prefix + 'iOrt', 'invalid');
					}
					if (!_hasClassName(__prefix + 'iPLZ', 'invalid')) {
						_addClassName(__prefix + 'iPLZ', 'invalid');
					}
					_(__prefix + 'iOrt').message = errorOrt;
					window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iOrt'), _(__prefix + 'iOrt').message);
					_(__prefix + 'iOrt').message = errorPLZ;
					window.__fc.options.onShowMessage.bind(window.__fc)(_(__prefix + 'iPLZ'), _(__prefix + 'iPLZ').message);
					break;
			}
			return false;
			break;
	}
}

