function MyPlanner() {
	var wrapper = "planner";
	removeEmptyTextNodes(wrapper);
	var form = document.forms.planner_form;
	var data = null;
	/*
	 * Request user authorization and bind with drawing function
	 */
	this.login = function () {
		if ($('login-loader')) {
			$('login-loader').style.display = 'inline';
		}
		json(
			"user_login",
			{
				'email'		:form.email.value,
				'password'	:form.password.value
			}, 
			bind(onCallback, this)
		);
		return false;
	};
	/*
	 * Request create user and bind with drawing function
	 */
	this.createUser = function() {
		//stat("Boligmappe :: Opret bruger");
		tstat("Boligmappe","Opret bruger - Din konto er oprettet","Velkommen,Boligmappe,Opret bruger - Din konto er oprettet",{'events':"event6"});
		
		json(
			"user_create", 
			{
				'email'				:form.email.value,
				'password'			:form.pass1.value,
				'password2'			:form.pass2.value,
				'zip'				:form.zip.value,
				'name'				:form.name.value
			},
			bind(onCallback, this)
		);
		return false;
	};
	/*
	 * Request save user setting user and bind with drawing function
	 */
	this.saveSettings = function () {
		json(
			"user_edit", 
			{
				'email'				:form.email.value,
				'password'			:form.pass1.value,
				'password2'			:form.pass2.value,
				'zip'				:form.zip.value,
				'name'				:form.name.value,
				'show_interest'		:form.show_interest.checked,
				'show_calculations'	:form.show_calculations.checked,
				'show_history'		:form.show_history.checked,
				'show_articles'		:form.show_articles.checked
			},
			bind(onCallback, this)
		);
		return false;
	};
	/*
	 * Request forgot password action and bind with drawing function
	 */
	this.forgotPass = function () {
		json(
			"user_forgotpass", 
			{'email' :form.email.value},
			bind(onForgotPass, this)
		);
		return false;
	};
	/*
	 * Show login panel
	 */
	this.showLogin = function () {
		this.showSearchBank();
		return panelTemplate(
			"return myplanner.login();",
			[
				liLink(
					"Glemt kodeord",
					"return myplanner.showForgotPass();",
					null,
					"help.user.forgot_pass"
				),
				liLink(
					"Opret ny boligmappe",
					"return myplanner.showCreate();",
					null,
					"help.user.acount_create"
				)
			],
			[
				input({
					'title'	:"E-mail",
					'id'	:"email"
				}),
				input({
					'title'	:"Kodeord",
					'id'	:"password",
					'type'	:"password"
				}),
				INPUT({
						'value'	:"Åbn",
						'type'	:"submit",
						'class'	:"submit"
				}),
				createLoader({
					id		:'login-loader', 
					cssClass:'findbank-loader'
				})
			]
		);
	};
	/*
	 * Show user settings panel
	 */
	this.showSettings = function() {
		json(
			"user_edit",{},
			bind(loadSettings, this)
		);
		return false;
	};
	/*
	 * Show forgot password panel
	 */
	this.showForgotPass = function (email) {
		return panelTemplate(
			"return myplanner.forgotPass();",
			[
				liLink(
					"Tilbage",
					"return myplanner.showLogin();",
					null,
					"help.user.back"
				)
			],
			[
				input({
					'title'	:"Email",
					'id'	:"email",
					'value' :email ? email : "" 
				}),
				INPUT({
						'value'	:"Send kodeord",
						'type'	:"submit",
						'class'	:"submit"
				})
			]
		);
	};
	/*
	 * Show create new user panel
	 */
	this.showCreate = function () {
		return panelTemplate(
			"return myplanner.createUser();",
			[
				liLink(
					"Glemt kodeord",
					"return myplanner.showForgotPass($('email').value);",
					null,
					"help.user.forgot_pass"
				),
				liLink(
					"Tilbage",
					"return myplanner.showLogin();",
					null,
					"help.user.back"
				)
			],
			[userInfoForm(true)],
			"Opret din egen boligmappe"
		);
	};
	/*
	 * return true if user is logged false if not
	 */
	this.isLogged = function() {
		if (data) {
			return true;
		}
		return false;
	}
	this.getName = function(){
		if (this.isLogged()) {
			return data.name;
		}
		return "";
	}
	this.getBranchId = function(){
		if (this.isLogged()) {
			return data.branchId;
		}
		return "";
	}
	this.getEmail = function(){
		if (this.isLogged()) {
			return data.email;
		}
		return "";
	}
	this.getZip = function(){
		if (this.isLogged()) {
			return data.zip;
		}
		return "";
	}
	/*
	 * Request delete specyfic link, calculation etc. and bind with drawing function
	 */
	this.removeLink = function(elem,action,id) {
		json(
			action,
			{'id':id},
			bind(onRemove,this,elem)
		);
		return false;
	};
	this.hidePanel = function(elem,id) {
		$(id.id).style.display = "none";
		
		setNodeAttribute(
			elem,
			"onclick",
			"return myplanner.showPanel(this," + id.id + ");"
		);
		elem.firstChild.src = "resources/images/panel_arrow_up.gif";
		return false;
	};
	this.showPanel = function(elem,id) {
		$(id.id).style.display = "block";
		
		setNodeAttribute(
			elem,
			"onclick",
			"return myplanner.hidePanel(this," + id.id + ");"
		);
		elem.firstChild.src = "resources/images/panel_arrow_down.gif";
		return false;
	};
	this.refresh = function () {
		if(this.isLogged()) {
			json(
				"user_refreshplanner", 
				{}, 
				bind(this.onRefresh, this)
			);
		} else {
			this.showLogin();
			lookup.fetchAndShowBranch();
		}
		return false;
	};
	this.onRefresh = function (newplanner) {
		data = newplanner;
		myplanner.showPage();
	};
	var onRemove =  function(elem) {
		removeElement(elem.parentNode);
	};
	this.logout = function() {
		json(
				"logout", 
				{}, 
				bind(this.onRefresh, this)
			);
			return false;
	}
	var onCallback = function (newplanner) {
		if ($('login-loader')) {
			$('login-loader').style.display = 'none';
		}
		if (newplanner.errorMessage) {
			showError(newplanner.errorMessage);
			return null;
		} else {
			hideError();
			hideInfo();
			data = newplanner;
			if(this.isLogged()) {
				tstat("Login", "Login - Succes","Velkommen,Login,Login - Succes",{'eVar8':myplanner.getEmail()
																				,'eVar9':"Logget ind"
																				,'events':"event7"});
				this.showPage();
			} else {
				this.showLogin();
			}
		}
	};
	this.showMapBookMode = function() {
		lookup = new Lookup('lookup_map');
		lookup.bookMode = true;
		lookup.showSearchByZip(myplanner.getZip());
		lookup.origination = "planner2";
		return false;
	}
	this.showSearchBank = function() {
		if ($("bankcard")) {
			removeElement("bankcard");
		}
		if ($("advisor")) {
			removeElement("advisor");
		}
		appendChildNodes("mypage",
			DIV({'id':"advisor"},
				DIV({'class':"panelheader"},
					DIV({'class':"header"})
				),
				DIV({'class':"panel"},
					DIV({'class':"panel_body"},
						FORM({
								'id'		:"lookup_panel",
								'method'	:"get",
								'onsubmit'	:"lookup.origination = \"Right menu\";return lookup.showSearchByZip(this.zip.value);"
							},
							INPUT({
								'type'	:"hidden",
								'value'	:"lookup_zip",
								'name'	:"action"
							}),
							LABEL(
								{'for':"zip"},
								"Postnummer"
							),
							INPUT({
								'type'	:"text",
								'name'	:"zip"
							}),
							LABEL(
								{'for':"bank_front"},
								"Pengeinstitut"
							),
							SELECT({
								'id'	:"lookup_bank_front",
								'name'	:"bank_front"
							}),
							INPUT({
								'type'	:"submit",
								'class'	:"searchbutton",
								'value'	:"Søg"
							}),
							createLoader({
								id		:'findbank-loader', 
								cssClass:'findbank-loader'
							}),
							BR()
						)
					)
				),
				DIV(null,
					IMG({
						'class'	:"bottom_shadow_panel",
						'src'	:"resources/images/bottum_shadow.gif"
					})
				)
			)
		);
		
		//add list request here
		if (lookup && !lookup._banks){
			//get banks (chains to filling select box)
			//log("first render of planner");
			lookup.getBanks();
		}else if (lookup && lookup._banks){
			// most likely re-rendering planner, fill front select box again
			//log("re-render planner")
			lookup.handleFrontPi();
		}	
	}
	var onForgotPass = function(response) {
		if (response.errorMessage) {
			showError(response.errorMessage);		
		} else {
			this.showLogin();
			showInfo(response);
		}
	}
	this.showPage = function() {
		if (!data) {
			this.showLogin();
			return;
		};
		var editSettings = null;
		if (data.editSettings) {
			editSettings = UL(null,
				liLink(
					data.editSettings.name,
					"return myplanner.showSettings();",
					data.editSettings.url,
					"help.user.settings"
				)
			);
		}
		replaceChildNodes(wrapper,
			DIV({'class':"panelheader"},
				DIV({'class':"header"})
			),
			DIV({'class':"panel"},
				DIV({'class':"panel_body"},
					DIV(
						{'class':"planner_inside"},
						P(
							{'class':"label_title"},
							A({
									'href'		:"page?action=logout",
									'onclick'	:"return myplanner.logout();"
								},
								IMG({'src':"resources/images/Log_on_off.gif"}),
								SPAN(null,"Log af")
							),
							data.name
						),
						BR(),
						editSettings
					)
				)
			),
			subPanel(
				"panel_hd_calculations",
				data.calcs,
				data.showCalculations,
				"user.remove.saved.calculation"
			), 
			subPanel(
				"panel_hd_interests",
				data.interests,
				data.showInterest,
				"user.remove.saved.interests",
				true
			), 
			subPanel(
				"panel_hd_saved",
				data.articles,
				data.showArticles,
				"user.remove.saved.link"
			), 
			subPanel(
				"panel_hd_history",
				data.history,
				data.showHistory,
				"user.remove.history.link"
			),
			DIV(null,
				IMG({
						'class'	:"bottom_shadow_panel",
						'src':"resources/images/bottum_shadow.gif"
					}
				)
			)
		);
//		this.showBankBook();
		lookup.fetchAndShowBranch(data.branchId);
		return false;
	};
	var showInfo = function(message) {
		getElement("panel_info").style.display = 'block';
		getElement("info_body").innerHTML = message;
	}
	var hideInfo = function() {
		getElement("panel_info").style.display = 'none';
	}
	var showError = function(message) {
		getElement("panel_error").style.display = 'block';
		getElement("error_body").innerHTML = message;
	}
	var hideError = function() {
		getElement("panel_error").style.display = 'none';
	}
	var userInfoForm = function(create) {
		return [
			input({
				'title'	:"Navn",
				'id'	:"name",
				'value'	:(data ? data.name	: false)
			}),
			input({
				'title'		:"E-mail",
				'id'		:"email",
				'value'		:(data ? data.email	: false),
				'readonly'	:(!create)
			}),
			input({
				'title'	:"Kodeord",
				'id'	:"pass1",
				'type'	:"password"
			}),
			input({
				'title'	:"Gentag kodeord",
				'id'	:"pass2",
				'type'	:"password"
			}),
			input({
				'title'	:"Postnummer",
				'id'	:"zip",
				'value'	:(data ? data.zip		: false)
			}),
			INPUT({
					'value'	:(create ? "Opret" : "Gem"),
					'type'	:"submit",
					'class'	:"submit"
			})
		];
	};
	var loadSettings = function(myplanner) {
		data = myplanner;
		if (!data) return false;

		return panelTemplate(
			"return myplanner.saveSettings();",
			[
				liLink(
					"Tilbage",
					"return myplanner.showPage();",
					null,
					"help.user.back"
				)
			],
			[
				input({
					'title'		:"Mine beregninger",
					'id'		:"show_calculations",
					'type'		:"checkbox",
					'clazz'		:"checkbox",
					'checked'	: data.showCalculations
				}),
				input({
					'title'		:"Udvalgte artikler",
					'id'		:"show_interest",
					'type'		:"checkbox",
					'clazz'		:"checkbox",
					'checked'	: data.showInterest
				}),
				input({
					'title'		:"Historik",
					'id'		:"show_history",
					'type'		:"checkbox",
					'clazz'		:"checkbox",
					'checked'	: data.showHistory
				}),
				input({
					'title'		:"Gemte artikler",
					'id'		:"show_articles",
					'type'		:"checkbox",
					'clazz'		:"checkbox",
					'checked'	: data.showArticles
				}),
				HR(),
				SPAN(
					{'class':"label_title"},
					"Ret min profil:"
				),
				[userInfoForm(false)]
				
			],
			"Tjek hvilke bokse du vil se"
		);
	};
	var subPanel = function (clazz, list, show, func, del) {
		if (!list || !show) return;
		var liLinks = [];
		for (var i = 0; i < list.length; i++ ) {
			if (del) {
				liLinks[i] = liLinkNotDelete(list[i], func);
			} else {
				liLinks[i] = liLinkDelete(list[i], func);
			}
		}
		return [
			DIV(
				{
					'class'		: clazz
				},
				A({
						'href' 		:"#",
						'onclick'	:"return myplanner.hidePanel(this," + clazz + ");"
					},
					IMG({
						'src': "resources/images/panel_arrow_down.gif"
						}
					)
				)
				
			),
			DIV(
				{
					'class'	:"panel",
					'id'	: clazz
				},
				DIV({'class' :"panel_body"},
					DIV(
						{'class':"planner_inside"},
						UL(null,liLinks)
					)
				)
			)
		];
	};
	/*
	 * Build panel with given links and form elements
	 * 
	 * @param {String} 		submit 	- form on submit action
	 * @param {Li[]} 		links	- links array, build with 'liLink' function
	 * @param {Element[]} 	form	- form elements (input, select, textarea etc.)
	 * @param {String}		title	- panel title
	 */
	var panelTemplate = function(submit, links, formElems, title) {
		replaceChildNodes(wrapper,
			DIV({'class':"panelheader"},
				DIV(
					{'class':"header"},
					A({
							'href'		:"#",
							'onclick'	:"return showHelp(this,'help.user.acount_create','left');"
						},
						IMG({'src':"resources/images/white_help.gif"})
					)
				)
			),
			DIV({'class':"panel"},
				DIV({'class':"panel_body"},
					FORM({
							'id'		: "planner_form",
							'onsubmit'	: submit
						},
						title == null ? "" : 
						SPAN({'class':"label_title"},title),
						DIV({
								'id'	:"panel_error",
								'class'	: "error"
							},
							IMG({	
									'src'	:"resources/images/warning_icon.gif",
									'class'	:"error_image"
								}
							),
							SPAN({'style':"vertical-align: top;",'id':"error_body"})
						),
						DIV({
								'id'	:"panel_info",
								'class'	: "info"
							},
							SPAN({'style':"vertical-align: top;",'id':"info_body"})
						),
						formElems == null ? "" : (
							[
								formElems,
								BR(),BR(),
								HR()
							]
						),
						UL(null,
							links
						)
					)
				),
				DIV(null,
					IMG({
							'class'	:"bottom_shadow_panel",
							'src':"resources/images/bottum_shadow.gif"
						}
					)
				)
			)
		);
		form = form = document.forms.planner_form;
		return false;
	};
	/*
	 * 
	 * Create label, input pair
	 * 
	 * @param {Object} params 	- object conteining input information, 
	 * Required:
	 * 	title	- label text
	 * 	id 		- input id
	 * Optional:
	 * 	type	- input type, default='text'
	 *	clazz	- input css class
	 *	checked	- checkbox, redio button property
	 *	value	- input value propertty, default=''
	 */
	var input = function(params) {
		var input = INPUT({
			'id'		:params.id,
			'type'		:(params.type == null 	 ? "text"	: params.type)
		});
		if (params.checked) {
			setNodeAttribute(input, 'checked', 	params.checked);
		}
		if (params.value) {
			setNodeAttribute(input, 'value', 	params.value);
		}
		if (params.clazz) {
			setNodeAttribute(input, 'class', 	params.clazz);
		}
		if (params.readonly) {
			setNodeAttribute(input, 'readonly',	"readonly");
		}
		var label = LABEL(
					{'for'	:params.id},
					params.title
			);
		return (params.type == "checkbox") ? DIV({'class':"divcheckbox"},input,label) : [label,input];
	};
	/*
	 * Create li elemnt with a link inside
	 * 
	 * @param {String} name 	- link name
	 * @param {String} onlick	- on click action
	 * @param {String} key		- help message key
	 * @param {String} href		- link href, default value: #
	 */
	var liLink = function(name,onclick,href,key) {
		return LI(null,
			A({
					'href'		:"#",
					'onclick'	:(key == null ? "" : "return showHelp(this,'" + key + "','left');"),
					'class'		:"option"
				},
				IMG({'src':"resources/images/white_help.gif"})
			),
			A({
					'href'		: href == null ? "#": href,
					'onclick'	: onclick == null ? "" : onclick
				},
				name
			)
		);
	};
	
	/*
	 * Create li element with a link insiade without delete button
	 * 
	 * @param {Object} link		- servlet LinkDTO object
	 * @param {String} action 	- onclick action for trash icon	
	 */
	var liLinkNotDelete = function(link,action) {
		return LI(null, 
			A({'class'	:"option"},
				IMG({
					'style'	:"width: 11px; height: 18px;",
					'src'	:"resources/images/1pixel.gif"
				})
			),
			A({
					'href'		:link.url,
					'onclick'	:"this.blur(); return myplanner.loadPage('user.remove.interested.link',this);"
				},
				link.name
			)
		);
	};
	/*
	 * Create li element with a link insiade and delete button
	 * 
	 * @param {Object} link		- servlet LinkDTO object
	 * @param {String} action 	- onclick action for trash icon	
	 */
	var liLinkDelete = function(link,action) {
		return LI(null,
			A({
					'href'		:("page?action=" + action + "&id=" + link.id),
					'title'		:"Vil du slette denne?",
					'hooked'	:true,
					'onclick'	:("return myplanner.removeLink(this, '" + action + "', " + link.id + ");"),
					'class'		:"option"
				},
				IMG({'src':"resources/images/panel_delete.gif"})
			),
			A({
					'href'		:link.url,
					'hooked'	:true,
					'onclick'	:"this.blur(); return myplanner.loadPage('"+action+"',this);"
				},
				link.name
			)
		);
	};
	this.loadPage = function(action, a) {
		try {
		var cb = null;
		return thepage.loadPage(a, cb);
		} catch (e) {
			log(e);
			return false;
		}
	}
	//NOT USED?	
	var getFunctionNameForAction = function(action) {
		switch (action) {
		case "user.remove.history.link":
			return "removeHistoryLink";
		case "user.remove.saved.link":
			return "removeSavedLink";
		case "user.remove.saved.calculation":
			return "removeCalculation";
		case "user.remove.interested.link":
			return "removeInterestedLink";
		}
	};
}
//OD CREATE OK MESSAGE: Tillykke med din nye bruger. Herunder vil du kunne se de artikler og de beregninger, som du læser eller direkte gemmer undervejs i dit besøg.

addLoadEvent(function () {
	myplanner = new MyPlanner();
	json(
		"user_refreshplanner", 
		{'nothing':'f'}, 
		bind(myplanner.onRefresh, this)
	);
}); 
