/**
 * Siteswift Admin Form Utilities
 */
var SWFormUtil = {
	focusSet: false,
	
	/**
	 * OnClick Form Submit Handler
	 * Usage: return SWFormUtil.postToUrl({url: '{|$linksave|}', element: this, hash: 'tags'});
	 * hash parameter is optional
	 */
	postToUrl: function(parms) {
		var url = parms["url"];
		var elm = parms["element"];

		// find parent form
		while (elm != null && elm.tagName != "FORM") {
			elm = elm.parentNode;
		}
		if (elm != null) {
			elm.action = url;
			return true;
		} else {
			alert("Error: SWFormUtil.postToUrl: parent form not found");
		}
		return false;
	},

	/**
	 * in case of a validation error add a css class to the form element
	 * and append a span with the error message
	 * the first error field gets the input focus
	 */
	showFormError: function(parms) {
		var fieldId = parms["field"];
		var message = parms["message"];
		Element.addClassName(fieldId, "formerror");
		new Insertion.After(fieldId, "<span class=\"formerrorLabel\">"+message+"</span>");
		if (this.focusSet == false) {
			this.focusSet = true;
			$(fieldId).focus();
		}
		
	},
	
	/**
	 * set the imput focus on the first element of a form
	 * if it was not already set by another function
	 * optionally a element parameter can be passed which will
	 * be searched upwards for a form element
	 */
	activateFirstFormFocus: function(parms) {
		if (this.focusSet == true) return;
		
		if (parms == null) elm = document.forms[0];	
		else elm = parms["element"];
		
		// find parent form
		while (elm != null && elm.tagName != "FORM") {
			elm = elm.parentNode;
		}
		if (elm != null) {
			elm.elements[0].focus();
			this.focusSet = true;
			return true;
		} 
	}
};

var Plz2Dioezese = {
	select: function(parms) {
		var elm = parms['element'];
		var value = parms['value'];
		var dioezese = this.convert(value);
		$(elm).value=dioezese;
	},
	
	convert: function(plz) {
		if (plz >= 1000 && plz <= 2080) return "w";
		if (plz >= 2081 && plz <= 2095) return "p";
		if (plz >= 2096 && plz <= 2412) return "w";
		if (plz >= 2413 && plz <= 2413) return "e";
		if (plz >= 2414 && plz <= 2420) return "w";
		if (plz >= 2421 && plz <= 2425) return "e";
		if (plz >= 2426 && plz <= 2442) return "w";
		if (plz >= 2443 && plz <= 2443) return "w";
		if (plz >= 2444 && plz <= 2472) return "w";
		if (plz >= 2473 && plz <= 2475) return "e";
		if (plz >= 2476 && plz <= 2484) return "w";
		if (plz >= 2485 && plz <= 2491) return "e";
		if (plz >= 2492 && plz <= 2571) return "w";
		if (plz >= 2572 && plz <= 2572) return "p";
		if (plz >= 2573 && plz <= 2772) return "w";
		if (plz >= 2773 && plz <= 2871) return "w";
		if (plz >= 2872 && plz <= 2872) return "g";
		if (plz >= 2873 && plz <= 2881) return "w";
		if (plz >= 2882 && plz <= 3000) return "p";
		if (plz >= 3001 && plz <= 3003) return "w";
		if (plz >= 3004 && plz <= 3004) return "p";
		if (plz >= 3005 && plz <= 3031) return "w";
		if (plz >= 3032 && plz <= 3333) return "p";
		if (plz >= 3334 && plz <= 3335) return "l";
		if (plz >= 3336 && plz <= 3399) return "p";
		if (plz >= 3400 && plz <= 3421) return "w";
		if (plz >= 3422 && plz <= 3454) return "p";
		if (plz >= 3455 && plz <= 3493) return "w";
		if (plz >= 3494 && plz <= 3700) return "p";
		if (plz >= 3701 && plz <= 3712) return "w";
		if (plz >= 3713 && plz <= 3713) return "p";
		if (plz >= 3714 && plz <= 3729) return "w";
		if (plz >= 3730 && plz <= 3730) return "p";
		if (plz >= 3731 && plz <= 3741) return "w";
		if (plz >= 3742 && plz <= 3999) return "p";
		if (plz >= 4000 && plz <= 4999) return "l";
		if (plz >= 5000 && plz <= 5119) return "s";
		if (plz >= 5120 && plz <= 5149) return "l";
		if (plz >= 5150 && plz <= 5160) return "s";
		if (plz >= 5161 && plz <= 5199) return "l";
		if (plz >= 5200 && plz <= 5210) return "s";
		if (plz >= 5211 && plz <= 5299) return "l";
		if (plz >= 5300 && plz <= 5309) return "s";
		if (plz >= 5310 && plz <= 5319) return "l";
		if (plz >= 5320 && plz <= 5359) return "s";
		if (plz >= 5360 && plz <= 5399) return "l";
		if (plz >= 5400 && plz <= 5999) return "s";

		if (plz >= 6000 && plz <= 6229) return "i";
		if (plz >= 6230 && plz <= 6260) return "s";
		if (plz >= 6261 && plz <= 6261) return "i";
		if (plz >= 6262 && plz <= 6270) return "s";
		if (plz >= 6271 && plz <= 6271) return "i";
		if (plz >= 6272 && plz <= 6273) return "s";
		if (plz >= 6274 && plz <= 6279) return "i";
		if (plz >= 6280 && plz <= 6282) return "s";
		if (plz >= 6283 && plz <= 6289) return "i";
		if (plz >= 6290 && plz <= 6290) return "s";
		if (plz >= 6291 && plz <= 6299) return "i";
		if (plz >= 6300 && plz <= 6399) return "s";
		if (plz >= 6400 && plz <= 6699) return "i";
	
		if (plz >= 6000 && plz <= 6699) return "i";
		if (plz >= 6700 && plz <= 6999) return "f";
		if (plz >= 7000 && plz <= 7420) return "e";
		if (plz >= 7421 && plz <= 7421) return "g";
		if (plz >= 7422 && plz <= 7999) return "e";
		if (plz >= 8000 && plz <= 8379) return "g";
		if (plz >= 8380 && plz <= 8383) return "e";
		if (plz >= 8384 && plz <= 8999) return "g";
		if (plz >= 9000 && plz <= 9781) return "k";
		if (plz >= 9782 && plz <= 9782) return "i";
		if (plz >= 9783 && plz <= 9899) return "k";
		if (plz >= 9900 && plz <= 9999) return "i";
		return "";
	}
};
