var dlgSpkSearch;
var searchResultWidget;
var type;

	function executeSpkSearchForm(loadinfoId, errormsgId, select) {
		var mode = dojo.byId('spkSearchMode').value;
		executeSpkSearch(mode, loadinfoId, errormsgId, select , true);
	}
	
	function checkIt() {
		return document.getElementById('spkConsultantLeasing').style.display=="none";
	}
	
	function executeSpkSearch(param, loadinfoId, errormsgId, select, isForm) {
		if( checkIt()){
						
			var spkSearchField = dojo.widget.byId('spkSearch');
			var searchTerm = dojo.byId('spkSearch').value;
			
			// 1. Eingaben checken
			if (spkSearchField.isValid() ) {
				 //blendet das "Sparkasse wird gesucht..." ein
				showIt(loadinfoId);	
				//blendet die Fehlermeldung aus
				hideIt(errormsgId);
				
				// 2. Anfrage senden
				dojo.io.bind ({
					url: "/ajax/SpkSearch?search=" + searchTerm + "&mode=" + param,
				    load: loadHandler,
				    error: errorHandler,
				    mimetype: "text/json"
				});		
			
			}
		}
		// 3. Anfrage auswerten
		function loadHandler(type, data, event) {
			
			if (data == "") {
				var type = "keine Sparkasse";
				errorHandler(type);
			} 
			
			// 4.0 Sonderfall bei SAK, wenn zwar eine Bank existiert, diese aber SAK nicht anbietet
			else if (data[0][0] == "product-not-supported") {
				if (isForm) {
					// In Formularen nur hinweisen
					//var type = "keine Sparkasse 2";
					//errorHandler(type);
					// momentan auch auf Hinweis-Seite verweisen.
					window.location.href = '/auto-kredit-suche-erfolglos.html?blz=' + data[0][1];
				} else {
					// In der MP-Suche auf Hinweis-Seite verweisen.
					window.location.href = '/auto-kredit-suche-erfolglos.html?blz=' + data[0][1];
				}
			}
			
			// 4.1 bei einem Suchergebnis Ergebnis-Seite anzeigen
			else if (data.length == 1) {
				document.cookie = "spkSearchBankcode=" + data[0][1];
				document.cookie = "spkSearchBankname=" + data[0][1];

				if (select) {
					selectSpk(data[0][1], param, data[0][0]);
				} else {
					if (param == "2") {
						window.location.href = '/auto-kredit-spezialist.html?blz=' + data[0][1] + '&mode=' + param;
					} else {
						window.location.href = '/leasing-spezialist.html?blz=' + data[0][1] + '&mode=' + param;
					}
				}
			} 
			
			// 4.2a. bei mehreren Ergebnissen Fenster �ffnen mit Liste
			else {
				searchResultWidget.clear();
				
				for (var i=0; i < data.length; i++) {
					createSearchResultEntry(data[i][1], data[i][0], param, i, select);
				}
				
				dlgSpkSearch.show();
				
			}
			//blendet das "Sparkasse wird gesucht..." aus
			hideIt(loadinfoId);
		}
		
		// handle errors
		function errorHandler( type, error ) {
			//blendet das "Sparkasse wird gesucht..." aus
			hideIt(loadinfoId);
			//blendet die Fehlermeldung ein
			showIt(errormsgId);		
			//�ndert die Farbe des TextInput-Feld der ComboBox
			//dojo.html.addClass(spkSearchField, "dojoValidateInvalid");
		}
	}


	// create search result entry
	function createSearchResultEntry(bankcode, bankname, mode, i, select) {
		var li = document.createElement("li");
		
		// TODO class
		dojo.html.addClass(li, "item"+ (i%2));
		li.setAttribute("contentId", bankcode);
		// f�r ie6 notwendig
		li.setAttribute("onmouseover", "this.className='itemMO'");
		li.setAttribute("onmouseout", "this.className='item"+ (i%2)+"'");
		
		li.setAttribute("onItemSelect", select != null ? "selectSpk(" + bankcode + ",'" + mode+ "','"+ bankname + "')" : "showConsultant(" + bankcode + ",'" + mode+ "','"+ bankname + "')");
		li.appendChild(document.createTextNode(bankname));
		searchResultWidget.add(li);
	}

	// forward to details page
	function showConsultant(bankcode, mode, bankname) {
		document.cookie = "spkSearchBankcode=" + bankcode;
	
		if (mode == "2") {
			window.location.href = '/auto-kredit-spezialist.html?blz=' + bankcode + '&mode=' + mode + '&bankname=' + bankname;	
		} else {
			window.location.href = '/leasing-spezialist.html?blz=' + bankcode + '&mode=' + mode + '&bankname=' + bankname;	
		}
	}

	// select sparkasse
	function selectSpk(bankcode, mode, bankname){
		hideIt('spkSearchLeasing');
		showIt('spkConsultantLeasing');
		dojo.byId('spksearchresult').value = bankcode;
		dojo.byId('spksearchresult_view').value = bankname;
		dlgSpkSearch.hide();
		addSitestatPixel('.spk_auswahl');
	}

	function fetchSpkFromCookie() {
		var cookieBankcode = dojo.byId('spkSearchBankcode');
		var cookieBankname = dojo.byId('spkSearchBankname');
		var mode = dojo.byId('spkSearchMode');
		
		if (cookieBankcode != null && mode != null && cookieBankname != null && cookieBankcode.value.length > 0) {
			selectSpk(cookieBankcode.value, mode.value, cookieBankname.value);
		}
		
	}


	// initialize dialog
	function initSpkSearchDialog(e) {
		dlgSpkSearch = dojo.widget.byId("SpkSearchResultsWindow");
		searchResultWidget = dojo.widget.byId("spkSearchGroup");
		fetchSpkFromCookie();
	}
	
	dojo.addOnLoad(initSpkSearchDialog);
