	/***************/
	/* Constructor */
	/* @param string: sFormId ~ the element id on form from which data is collected
	/* @param string: sCookieName ~ [optional] if set it is possible to save multible formcookies in the same directory
	/***************/

	function Questionaire_FormDataCollecter( sFormId, sCookieName ){
		//Formhandling
		this.sFormId	= sFormId
		this.oForm 		= null
		this.aFieldsAndValues = {} //Collection containing FieldNames and and those values
		this.aFieldsExluded 	= new Array() //Array containing Fields to be excluded from collecting data. [ name (fieldName/fieldId) ] => [ type (name/id)]

		//Cookiehandling
		this.cookieIsLoaded		= false	//Check if formdata has been loaded from cookie. This is build to prevent that local_init runs this class twice or more
		this.cookieName = typeof(sCookieName) != "undefined" && sCookieName != "" ? sCookieName : "questionaire_data";
		this.cookieValue		= ""
		this.cookieExpMinutes	= 30 //For how many minutes shall the cookie stay alive
		this.cookieExpires		= 0
		this.cookiePath			= "/"
		this.cookieDomain		= document.domain
		this.cookieSecure		= false	//In case secure forms has been build set to true
	}

	/*******************/
	/* Private methods */
	/*******************/

	Questionaire_FormDataCollecter.prototype._clear = function (){
		this.aFieldsAndValues = {}
	}

	Questionaire_FormDataCollecter.prototype._setValues = function (){
		this._fetchForm()
		for( var i=0; i < this.oForm.elements.length; i++){
			var element = this.oForm.elements[i]
			if( typeof(this.aFieldsAndValues[i]) != "undefined" && this.aFieldsAndValues[i] != "" ){
				//Check if the element is radio og checkbox and if the state is checked or not
				if( element.type == "radio" || element.type ==  "checkbox" ){
					if( element.value == this.aFieldsAndValues[i] ) element.checked = true
				} else {
					element.value = this.aFieldsAndValues[i];
				}
			}
		}
	}

	Questionaire_FormDataCollecter.prototype._setCookieExpireDate = function (){
		var date = new Date()
		date.setTime( date.getTime() + ( this.cookieExpMinutes * 60 * 1000 ) )
		this.cookieExpires = date
	}

	Questionaire_FormDataCollecter.prototype._setCookie = function (){
		this._setCookieExpireDate()
		//Prepare data for saving in cookie
		var cookieData = ''
		for ( var elementNum in this.aFieldsAndValues ) {
			var sEleValue = this.aFieldsAndValues[elementNum]
			if( typeof( sEleValue ) != "undefined" && sEleValue != "" ){
				cookieData += elementNum + "==" + sEleValue + "||"
			}
		}
		document.cookie = escape( this.cookieName ) +
			'=' + escape(cookieData) +
			(this.cookieExpires ? '; EXPIRES=' + this.cookieExpires.toGMTString() : '') +
			(this.cookiePath ? '; PATH=' + this.cookiePath : '') +
			(this.cookieDomain ? '; DOMAIN=' + this.cookieDomain : '') +
			(this.cookieSecure ? '; SECURE' : '')
	}

	Questionaire_FormDataCollecter.prototype._checkIfFieldIsExcluded = function ( sInpName ){
		for( var i=0; i < this.aFieldsExluded.length; i++ ){
			if( this.aFieldsExluded[i] == sInpName ) return true
		}
		return false
	}

	Questionaire_FormDataCollecter.prototype._getCookie = function (){
		var dc 		= document.cookie
		var prefix 	= this.cookieName + "="
		var begin 	= dc.indexOf("; " + prefix)
		if( begin == -1 ){
			begin = dc.indexOf(prefix)
			if( begin!=0 ) return null
		} else
			begin += 2
		var end=document.cookie.indexOf( ";" ,begin )
		if( end == -1 ) end = dc.length
		var cookieData = unescape( dc.substring( begin + prefix.length, end ) )
		return cookieData
	}

	/******************/
	/* Public methods */
	/******************/

	Questionaire_FormDataCollecter.prototype._fetchForm = function ( ){
		if( !getEle( this.sFormId ) ){
			alert("error in Questionaire_FormDataCollecter._fetchForm: Couldīnt get form by id '" + this.sFormId + "'")
		} else {
			this.oForm = getEle( this.sFormId )
		}
	}

	Questionaire_FormDataCollecter.prototype.excludeFieldByName = function ( sInpName ){
		if( !this._checkIfFieldIsExcluded(sInpName) ){
			this.aFieldsExluded[ this.aFieldsExluded.length ] = sInpName
		}
	}

	Questionaire_FormDataCollecter.prototype.loadFormData = function (){
		// Only load formdata once from cookie
		if( this.cookieIsLoaded ) return

		// Check if cookie and content exists
		var sCookieContent = this._getCookie()
		if( sCookieContent == "" || sCookieContent == null || !sCookieContent) return

		// Load data from cookie
		var aTmp	= sCookieContent.split("||")
		for( var row=0; row < aTmp.length; row++ ){
			var aRow = aTmp[row].split("==")
			var element	= aRow[0]
			var value	= aRow[1]
			this.aFieldsAndValues[element] = value
		}

		// Insert into form
		this._setValues()
		this.cookieIsLoaded = true
	}


	Questionaire_FormDataCollecter.prototype.saveFormData = function (){
		// Reset old data before collecting new data
		this._clear()
		// Collecting data from form elements
		this._fetchForm()
		for( var i=0; i < this.oForm.elements.length; i++){
			var element = this.oForm.elements[i]
			//Only collect if NOT hidden field (because PHP may have changed hidden values on refresh)
			if( element.type != "hidden" && !this._checkIfFieldIsExcluded( element.name ) ) {
				// Check if radio or checkbox and if so the element is checked
				if( element.type == "radio" || element.type ==  "checkbox" ){
					if( element.checked ) this.aFieldsAndValues[i] = element.value
				} else if( element.value != "" && typeof(element.value) != "undefined" ){
					this.aFieldsAndValues[i] = element.value
				}
			}
		}
		// Save formdata to cookie
		this._setCookie()
	}

	/*
	 * Debugging
	 *
	 */
	Questionaire_FormDataCollecter.prototype.debug = function () {
		var sDebugTxt = "", item
		//Get formfields and values
		sDebugTxt += "FORMFIELDS AND VALUES:\n"
		for(item in this.aFieldsAndValues){
			sDebugTxt += "item: " + item + " => " + this.aFieldsAndValues[item] + "\n"
		}
		//Get Excluded fields
		sDebugTxt += "aFieldsExluded:\n"
		for(item in this.aFieldsExluded){
			sDebugTxt += "item: " + item + " => " + this.aFieldsExluded[item] + "\n"
		}

		//Get cookiedata
		sDebugTxt += "\nCOOKIEDATA:\n" +
								 "cookieName: " + this.cookieName	+ "\n" +
								 "cookieValue: " + this.cookieValue	+ "\n" +
								 "cookieExpires: " + this.cookieExpires	+ "\n" +
 								 "cookieExpMinutes: " + this.cookieExpMinutes	+ "\n" +
								 "cookiePath: " + this.cookiePath	+ "\n" +
								 "cookieDomain: " + this.cookieDomain	+ "\n" +
								 "cookieSecure: " + this.cookieSecure	+ "\n"
		alert( sDebugTxt )
	}

