var abschicken=true;

// Cookie Functions  ////////////////////  (:)

// Set the cookie.
// SetCookie('your_cookie_name', 'your_cookie_value', exp);

// Get the cookie.
// var someVariable = GetCookie('your_cookie_name');

var exp = new Date();
exp.setTime(exp.getTime() + (60*60*1000));

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

// cookieForms saves form content of a page.

// use the following code to call it:
//  <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">

function cookieForms() {
	var mode = cookieForms.arguments[0];
	for(f=1; f<cookieForms.arguments.length; f++) {
		formName = cookieForms.arguments[f];

		if(mode == 'open') {
			cookieValue = GetCookie('saved_'+formName);
			if(cookieValue != null) {
				var cookieArray = cookieValue.split('#cf#');
				for(i=0; i<cookieArray.length; i++)
				{
					var feldwertzuordnung = cookieArray[i].split('#nv#');
					var feldname = feldwertzuordnung[0];
					var feldwert = feldwertzuordnung[1];
					for(j=0; j<document[formName].elements.length; j++)
					{
						if(document[formName].elements[j].name==feldname && feldname<>'bestellung_ausfuehren')
						{
							if(feldwert.substring(0,6) == 'select') { document[formName].elements[feldname].options.selectedIndex = feldwert.substring(7, feldwert.length-1); }
							else if(document[formName].elements[j].type=='checkbox' || document[formName].elements[j].type=='radio')
							{
								if(document[formName].elements[j].value==feldwert)
									document[formName].elements[j].checked=true;
								else
									document[formName].elements[j].checked=false;
							}
							else if(document[formName].elements[j].type=='hidden')
							{
								continue;
							}
							else { document[formName].elements[feldname].value = (feldwert) ? feldwert : ''; }
						}
					}
				}
			}
		}

		if(mode == 'save') {
			cookieValue = '';
			for(i=0; i<document[formName].elements.length; i++) {
				fieldType = document[formName].elements[i].type;
				fieldName = document[formName].elements[i].name;
				if(fieldType == 'password') { passValue = ''; }
				else if(fieldType == 'checkbox' || fieldType == 'radio')
				{
					if(document[formName].elements[i].checked)
						passValue = document[formName].elements[i].value;
				}
				else if(fieldType == 'hidden')
				{
					continue;
				}
				else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }
				else { passValue = document[formName].elements[i].value; }

				cookieValue = cookieValue + fieldName + '#nv#' + passValue + '#cf#';
			}
			cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter
			SetCookie('saved_'+formName, cookieValue, exp);
		}
	}
}
