var unreserved = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~";
var reserved = "!*'();:@&=+$,/?%#[]";
var allowed = unreserved + reserved;
var hexchars = "0123456789ABCDEFabcdef";

url1	= document.URL;
url2	= url1.split('?');
url3	= url2[1].split('&');
for (i=0;i<url3.length;i++) {
	feld	= url3[i].split('=');
	if (feld[1]) {
		for (n=0;n<document.kontaktform.elements.length;n++) {
			nfeld = document.kontaktform.elements[n];
			if (nfeld.name==feld[0]) {
				if (nfeld.type=='text' || nfeld.type=='textarea') {
					nfeld.value	= decode(feld[1]);
				} else if ((nfeld.type=='checkbox' || nfeld.type=='radio') && decode(feld[1])=='on') {
					nfeld.checked	= 1;
				} else if (nfeld.type=='select-one') {
					for (p=0;p<nfeld.options.length;p++) {
						if (nfeld.options[p].text==decode(feld[1])) {
							nfeld.options[p].selected = true;
							break;
						}
					}
				}
				break;
			}
		}
	}
}

function decode(encoded) {
	var decoded = "";
	var illegalencoding = "";
	var byte1, byte2, byte3, byte4 = 0;
	var i = 0;
	while (i < encoded.length) {
		var ch = encoded.charAt(i);
		if (encoded.substr(i,3)=='%E4') {
			decoded = decoded + 'ä';
			i = i + 3;
		} else if (encoded.substr(i,3)=='%F6') {
			decoded = decoded + 'ö';
			i = i + 3;
		} else if (encoded.substr(i,3)=='%FC') {
			decoded = decoded + 'ü';
			i = i + 3;
		} else if (encoded.substr(i,3)=='%DF') {
			decoded = decoded + 'ß';
			i = i + 3;
		} else if (ch == "%") {
			if (getdec(encoded.substr(i,3)) < 255) {
				byte1 = getdec(encoded.substr(i,3));
				byte2 = getdec(encoded.substr(i+3,3));
				byte3 = getdec(encoded.substr(i+6,3));
				byte4 = getdec(encoded.substr(i+9,3));
				if (byte1 < 128) {
					decoded = decoded + String.fromCharCode(byte1);
					i = i + 3;
				}
				if (byte1 > 127 && byte1 < 192) {
					decoded = decoded + encoded.substr(i,3);
					illegalencoding = illegalencoding + encoded.substr(i,3) + " ";
					i = i + 3;
				}
				if (byte1 > 191 && byte1 < 224) {
					if (byte2 > 127 && byte2 < 192) {
						decoded = decoded + String.fromCharCode(((byte1 & 0x1F) << 6) | (byte2 & 0x3F));
					} else {
						decoded = decoded + encoded.substr(i,6);
						illegalencoding = illegalencoding + encoded.substr(i,6) + " ";
					}
					i = i + 6;
				}
				if (byte1 > 223 && byte1 < 240) {
					if (byte2 > 127 && byte2 < 192) {
						if (byte3 > 127 && byte3 < 192) {
							decoded = decoded + String.fromCharCode(((byte1 & 0xF) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F));
						} else {
							decoded = decoded + encoded.substr(i,9);
							illegalencoding = illegalencoding + encoded.substr(i,9) + " ";
						}
					} else {
						decoded = decoded + encoded.substr(i,9);
						illegalencoding = illegalencoding + encoded.substr(i,9) + " ";
					}
					i = i + 9;
				}
				if (byte1 > 239) {
					if (byte2 > 127 && byte2 < 192) {
						if (byte3 > 127 && byte3 < 192) {
							if (byte4 > 127 && byte4 < 192) {
								decoded = decoded + String.fromCharCode(((byte1 & 0x7) << 18) | ((byte2 & 0x3F) << 12) | ((byte3 & 0x3F) << 6) | (byte4 & 0x3F));
							} else {
								decoded = decoded + encoded.substr(i,12);
								illegalencoding = illegalencoding + encoded.substr(i,12) + " ";
							}
						} else {
							decoded = decoded + encoded.substr(i,12);
							illegalencoding = illegalencoding + encoded.substr(i,12) + " ";
						}
					} else {
						decoded = decoded + encoded.substr(i,12);
						illegalencoding = illegalencoding + encoded.substr(i,12) + " ";
					}
					i = i + 12;
				}
			} else {
				decoded = decoded + encoded.substr(i,3);
				illegalencoding = illegalencoding + encoded.substr(i,3) + " ";
				i = i + 3;
			}
		} else {
			if (allowed.indexOf(ch) == -1) notallowed = notallowed + ch + " ";
			decoded = decoded + ch;
			i++;
		}
	}
	return decoded;
}

function getdec(hexencoded) {
	if (hexencoded.length == 3) {
		if (hexencoded.charAt(0) == "%") {
			if (hexchars.indexOf(hexencoded.charAt(1)) != -1 && hexchars.indexOf(hexencoded.charAt(2)) != -1) {
				return parseInt(hexencoded.substr(1,2),16);
			}
		}
	}
	return 256;
}
