    // --------------------------------------------------
   // /FormSaver v2.0
  // /Author: Johan Dam. http://johandam.com/
 // /Desc: Saves forms using localStorage or sessionStorage.
// --------------------------------------------------

function FormSaver(f, session){
	this.form = f;
	this.storage = session ? sessionStorage : localStorage;
	this.tags = new Array('input','textarea','select');
	
	// Optionele sanity check. Optioneel omdat alle moderne browsers (zelfs IE8) localStorage ondersteunen.
	this.check = function(){
		try {
			return 'localStorage' in window && window['localStorage'] !== null;
		} catch(e){
			return false;
		}
	}
	
	this.load = function(){
		var input = this.get_input();
		for(var i = 0; i < input.length; i ++){
			if(!this.storage[this.form + '_' + input[i].name])
				this.storage[this.form + '_' + input[i].name] = '';
			
			switch(input[i].type){
				case "checkbox":
				case "radio":
					if(this.storage[this.form + '_' + input[i].name] == input[i].value)
						input[i].checked = true;
					break;
				case 'textarea':
					input[i].innerHTML = this.storage[this.form + '_' + input[i].name];
					break;
				case "button":
				case "submit":
					// Nothing to save here, move along citizen. Dit is om te voorkomen dat ze onder de default komen.
					break;
				default:
					input[i].value = this.storage[this.form + '_' + input[i].name];
			}
		}
	}
	
	this.save = function(input){
		switch(input.type){
			case "textarea":
				this.storage[this.form + '_' + input.name] = input.innerHTML;
				break;
			case "checkbox":
				this.storage[this.form + '_' + input.name] = input.checked ? input.value : '';
				break;
			default:
				this.storage[this.form + '_' + input.name] = input.value;
		}
	}
	
	this.clear = function(){
		var input = this.get_input();
		for(var i = 0; i < input.length; i ++)
			this.storage[this.form + '_' + input[i].name] = '';
	}
	
	this.get_input = function(){
		var input = new Array();
		for(t in this.tags){
			var temp = document.getElementById(this.form).getElementsByTagName(this.tags[t]);
			for(i in temp)
				input.push(temp[i])
		}
		return input;
	}
}