BankBook = function(form_name) {
	this.form_name = form_name;
};
/* Call servlet action */
BankBook.prototype.show = function(bankId) {
	if ($("page_old")) {
		documentList();
		return false;
	}
	if (bankId) {
		this.bankId = bankId;
		lookup.lastBranch = bankId;
	} else if (lookup.lastBranch) {
		this.bankId = lookup.lastBranch;
	}
	try{
				ADFtrack(7642,28992); //kontakt rådgiver kald
	} catch (E) {}
	tstat("Velkommen", "Bestil møde","Velkommen,Bestil møde",{'events':"event28"}); 
	json("bank_book", {'bank_id':this.bankId}, bind(this.fill, this));
	return false;
};
BankBook.prototype.submit = function() {
	try {
		var error = false;
		$('email').style.borderColor = "#73655A";
		$('name').style.borderColor = "#73655A";
		if ($("bank_book_error")) {
			removeElement("bank_book_error");
		}
	
		$("customer").value = $("customer").checked;
		var params = formContents("page");
		var args = combine(params[0],params[1]);
		if (!args.email) {
			$("bank_book_form").email.style.borderColor = "red";
			error = true;
		}
		if (!args.name) {
			$("bank_book_form").name.style.borderColor = "red";
			error = true;
		}
		if (error) {
			appendChildNodes("message_content",
				DIV(
					{'id':"bank_book_error", 'style':"border: 1px solid rgb(189, 182, 170); margin: 8px; padding: 4px; width: 320px; color: red;"},
						IMG({'src':"resources/images/warning_icon.gif",'style':"vertical-align: middle; padding-right: 4px;"}),
						"Vær venlig at udfylde navn og e-mail adresse."
				)
			);
			return false;
		} 
		
		//stat("Bestil møde :: "+lookup.branchName);
		json("bank_book", args, bind(this.greetings, this));
	} catch (e) {
		log(e);
	}
	return false;
};
BankBook.prototype.greetings = function(page) {
	setNodeAttribute("mainpage","class", "");
	replaceChildNodes("mainpage",
		H1({
				'id':"mainheader",
				'class':"fullheader"
			},
			this.data.headline),
			DIV(
				{'id':"page",'style':"height: 147px !important; height: 189px;"},
				/*page.contents*/""
			),
			footer()
	);
	$('page').innerHTML = page.contents;
	toolbox();
	if (page.headline.indexOf("Tak for din bestilling")>-1||page.contents.indexOf("Vi takker")>-1){
		try{
			ADFtrack(7642,29006);
		} catch (E) {}
		gstatBook();
		fb_advisorbanner_convert();
		
		if(lookup.origination){
			org = lookup.origination;
			if(org='Boligmarkedesstatistik'){
				roiBMSBookMeetingReceipt();
			}
			delete lookup.origination;
		}else{
			org = "unknown";
		}
		tstat("Bestil møde", "Tak for din mail","Velkommen,Bestil møde,Tak for din mail",{'events':"event8",'prop7':this.bankId,'eVar18':this.bankId,'eVar24':org});
	}
	thepage.updateLocation(page.headline, "bank_book_greetings");
}
/* Build page content */
BankBook.prototype.field = function(text,name,type,value) {
	return [
		LABEL(
			{'for': name},
			text
		),
		INPUT({
			'name'	: name,
			'id'	: name,
			'type'	: type,
			'class'	: type,
			'value'	: value ? value : ""
		}),
		BR()
	];
}
function documentList() {
	removeElement('page');
	thepage.updateLocation("bank_book");
	document.getElementsByName("loan_type")[0].style.display = "";
	$("page_old").style.display="";
	$("page_old").id = "page";
	return false;
}
BankBook.prototype.showList = function(result) {
	thepage.updateLocation("bank_book_documents");
	document.getElementsByName("loan_type")[0].style.display = "none";
	$("page").style.display = "none";
	$("page").id = "page_old";
	insertSiblingNodesBefore("footer", DIV({'id':"page"}));
	$("page").innerHTML = result.contents;
	thepage.changeBanner(result.banner);
	return false;
}
BankBook.prototype.loadDocList = function() {
	json("page", {'pageId':345}, bind(bank_book.showList, bank_book));
	return false;
}
BankBook.prototype.fill = function(bankDTO) {
	this.data = bankDTO;
	setNodeAttribute("mainpage","class", "");
	thepage.changeBanner(this.data.banner);
	replaceChildNodes("mainpage",
		H1({
				'id':"mainheader",
				'class':"fullheader"
			},
			this.data.headline
		),
		DIV(
			{'id':"page"},
			FORM({
					'id'		: this.form_name,
					'method'	:"get",
					'onsubmit'	:"return bank_book.submit();"
				},
				INPUT({'type':"hidden", 'name':"send","value":'true'}),
				INPUT({'type':"hidden", 'name':"bank_id","value":this.bankId}),
				/*DIV({ 'id'	:"bank_box" },
					H2(null,
						this.data.boxHeader
					),
					DIV(null,
						this.data.boxContent
					),
					INPUT({
							'type'	:"button",
							'value'	:"Se listen her",
							'onclick' : "return bank_book.loadDocList();",
							'style'	:"float: right;"
						}
					)
				),*/
				DIV({'id':"message_content"},
					this.data.contents
				),
				H4("Dine personlige oplysninger"),
				BR(),
				this.field(
					"Navn: ",
					"name",
					"text",
					myplanner.getName()
				),
				this.field(
					"Adresse: ",
					"address",
					"text"
				),
				this.field(
					"Postnr. og by:",
					"post",
					"text",
					myplanner.getZip()
				),
				this.field(
					"E-mail: ",
					"email",
					"text",
					myplanner.getEmail()
				),
				this.field(
					"Telefon: ",
					"phone",
					"text"
				),
				this.field(
					"Jeg er kunde i\240forvejen:",
					"customer",
					"checkbox"
				),
				BR(),
				LABEL(
					{'for': "loan_type"},
					"Hvilken type lån har du?:"
				),
				SELECT({
					'name'	:"loan_type"
					},
					OPTION({'value':""}, ""),
					map( 
						function(v){
							return OPTION(
									{'value':v.value},
									v.text
								);
						},
						this.data.loanTypes
					)
				),
				INPUT({
					'class'		 :"helpbutton",
					'type'		 :"image",
					'src'		 :"resources/images/white_help.gif",
					'onmouseover':"return showHelp(this,'help.bookmeeting.type');",
					'onclick'	 :"return false;"
				}),
				BR(),
				BR(),
				(myplanner.isLogged() && this.data.calculations && this.data.calculations.length)
					?[
						LABEL(
							{'for': "user_calculation"},
							"Dine beregninger:"
						),
						SELECT({
							'name'	:"user_calculation"
						},
							OPTION({'value':-1}, ""),
							map(
								function(v) {
									return OPTION({'value':v.id}, v.text)	
								},
								this.data.calculations
							)
						)
						,
						INPUT({
							'class'		 :"helpbutton",
							'type'		 :"image",
							'src'		 :"resources/images/white_help.gif",
							'onmouseover':"return showHelp(this,'help.bookmeeting.calculation');",
							'onclick'	 :"return false;"
						}),
						BR(),
						BR()
					]
					: ""
				,LABEL(
					{'for':"cal_button"},
					"Datoer"
				),
				INPUT({
						'id'		:"cal_button",
						'type'		:"image",
						'src'		:"resources/images/calendar_icon.gif",
						'class'		:"calendar_button",
						'onclick' 	:"calendar.showAtElement(this); return false;"
				}),
				INPUT({
					'id'	  :"month",
					'type'	  :"text",
					'class'	  :"date",
					'disabled':"disabled",
					'style'	  :"width: 72px;"
				}),
				INPUT({
					'id'	  :"day",
					'type'	  :"text",
					'class'	  :"date",
					'disabled':"disabled",
					'style'	  :"width: 20px;"
				}),
				INPUT({
					'id'	  :"year",
					'type'	  :"text",
					'class'	  :"date",
					'disabled':"disabled",
					'style'	  :"width: 32px;"
				}),
				INPUT({
					'id'	  :"time",
					'type'	  :"text",
					'class'	  :"date",
					'style'	  :"width: 36px;",
					'disabled':"disabled"
				}),
				INPUT({
					'class'		 :"helpbutton",
					'type'		 :"image",
					'src'		 :"resources/images/white_help.gif",
					'onmouseover':"return showHelp(this,'help.bookmeeting.date');",
					'onclick'	 :"return false;"
				}),
				INPUT({
					'type'		:"button",
					'value'		:"Tilføj",
					'class'		:"button",
					'onclick'	:"return bank_book.addDate();"
				}),
				BR(),
				DIV({'id':"meeting_date"}),
				LABEL(
					{'for': "comments"},
					"Evt. kommentar:"
				),
				TEXTAREA({'name':"comments"}),
				BR(),
				INPUT({
					'type'		:"submit",
					'value'		:"Send",
					'class'		:"button",
					'style'		:"margin-left: 326px !important; margin-left: 324px;"
				})
			)
		),
		footer()
		);
	calendar = new Calendar(
		1, null,
		calendar_onSelect,
		function () {
			calendar.hide();
			return false;
		}
	);
	calendar.isPopup = true;
	calendar.showsTime = true;
	calendar.minYear = new Date().getYear();
	calendar.create();
	toolbox();
	thepage.updateLocation(this.data.headline, "bank_book");
};
BankBook.prototype.addDate = function() {
	appendChildNodes("meeting_date",
		DIV({'class' :"meeting_date"},
			DIV(
				{'style': "width: 12px;"},
				INPUT({
					'type'	:"hidden",
					'name'	:"meeting_date",
					'value'	: //Date format, locale DK: yyyy-MMM-dd-h:m
						$("year").value  + "-" +
						monthToNumber($("month").value) + "-" +
						$("day").value + "-" +
						$("time").value
				})
			),
			DIV(
				{'style': "width: 72px;"},
				$("month").value
			),
			DIV(
				{'style': "width: 20px;"},
				$("day").value
			),
			DIV(
				{'style': "width: 32px;"},
				$("year").value
			),
			DIV(
				{'style': "width: 36px; "},
				$("time").value
			),
			INPUT({
				'class'		 :"deletebutton",
				'type'		 :"image",
				'src'		 :"resources/images/panel_delete.gif",
				'onclick'	 :"return bank_book.deleteDate(this);"
			})
		),
		BR()
	);
	return false;
}
BankBook.prototype.deleteDate = function(elem) {
	removeElement(elem.parentNode.nextSibling);
	removeElement(elem.parentNode);
	return false;
}
function monthToNumber(m) {
	for (var i = 0; i < Calendar._MN.length; i++) {
		if (Calendar._MN[i] == m) {
			return i + 1;
		}
	}
	return 1;
}
function calendar_onSelect(calendar, date) {
	$("year").value  = calendar.date.getFullYear();
	$("month").value = Calendar._MN[calendar.date.getMonth()];
	$("day").value   = calendar.date.getDate();
	$("time").value  = calendar.date.getHours() + ":" + calendar.date.getMinutes();
	return false;
}
addLoadEvent(function () {
	bank_book = new BankBook("bank_book_form");
});
