
var objectValue = null;

var immosel = {

	//isLocationAll: true,
	isPanelOpen: false, 
	
	
	panelDisplay: function(elemID, show){
	
		for (var i = 0; i < 50; i++){
			immosel.displayElem(elemID+"_"+i,show);
		}
		if (show) {
			immosel.displayElem(elemID+"_true", true);
			immosel.displayElem(elemID+"_false", false);
		}
		else {
			immosel.displayElem(elemID+"_true", false);
			immosel.displayElem(elemID+"_false", true);
		}
	},


	display: function(elemID, show) {
		immosel.displayElem(elemID,show);

		if (show) {
			immosel.isPanelOpen = true;
			immosel.displayElem(elemID+"_true", true);
			immosel.displayElem(elemID+"_false", false);
		}
		else {
			immosel.isPanelOpen = false;
			immosel.displayElem(elemID+"_true", false);
			immosel.displayElem(elemID+"_false", true);
		}
	},


	displayElem: function(elemID, show) {
		
		var myElem = $(elemID);
		if (!immo.isEmpty(myElem)) {
			
			if(show) {
				myElem.show();
			}
			else {
				myElem.hide();
			} 
		}
	},


	updateSel: function(rattail,pagename,mandant) {
		
		function updateSelComplete() {
			new Ajax.Updater("aktuelleSuche", "/letzteSucheRechts.do");
			immosel.incrementEtracker(pagename.replace('/liste','/liste/seite1'),mandant);
		}
		if(validate.validateFields() && immosel.checkFromToFields()) {
			var rattailTmp = rattail.replace(/&amp;/g,'&');
			var tail = encodeURI(immosel.setRattail(rattailTmp));
			immo.showSpinnerOverlay("ergebnisse");
			new Ajax.Updater({success:"ergebnisse"}, "/immobiliensuche.do?outputtype=ajax_sel"+tail, {evalScripts: true, onComplete: updateSelComplete});		
		}
	},
	updateSearch: function(rattail,pagename,mandant,value,objectId,searchroot) {
		this.objectValue = value;
		if(validate.validateFields()&& immosel.checkFromToFields()){
			var rattailTmp = rattail.replace(/&amp;/g,'&');
			var tail = encodeURI(immosel.setRattail(rattailTmp));
			new Ajax.Updater({success:"sucheVerfeinerung"}, "/"+searchroot+"?outputtype=ajax_refinement"+tail, {evalScripts: true});
		}
	},
	updatePage: function(rattail,page,size,pagename,mandant, scrollTop) {
		
		function updateSelComplete() {
			immosel.incrementEtracker(pagename.replace('/liste/seite1','/liste/seite'+page),mandant);
			if (!immo.isEmpty(scrollTop) && scrollTop) {
				window.scrollTo(0, 1);
			}
		}
		var listsize = size;
		var pageoffset = (listsize*page-(listsize-1));
		immo.showSpinnerOverlay("ergebnisse");
		var rattailTmp = rattail.replace(/&amp;/g,'&');
		var tail = encodeURI(rattailTmp);
		new Ajax.Updater({success:"ergebnisse"}, "/immobiliensuche.do?outputtype=ajax_sel&pageoffset="+pageoffset+tail, {evalScripts: true, onComplete: updateSelComplete});	
		
	},
	updatePageSort: function(rattail,pagename,mandant) {
		
		function updateSelComplete() {
			immosel.incrementEtracker(pagename.replace('/liste','/liste/seite1') +"/sortierung",mandant);
		}
		var sortby = document.SortByForm.sortby;
		var value = 0;
		for (var i = 0; i < sortby.length; i++){
			if(sortby.options[i].selected){
				value = sortby.options[i].value;
			}
		}
		var rattailTmp = rattail.replace(/&amp;/g,'&');
		var tail = encodeURI(rattailTmp);
		immo.showSpinnerOverlay("ergebnisse");
		new Ajax.Updater({success:"ergebnisse"}, "/immobiliensuche.do?outputtype=ajax_sel&pageoffset=1"+tail+"&sortby="+value, {evalScripts: true, onComplete: updateSelComplete});	
		
	},

	
	locationCheck: function(tail) {
		
		var locationcheck = 0;
		var locationInfo = document.FilterListForm.SearchInfos;
		var citysearch = document.FilterListForm.citysearch;
		var districtsearch = document.FilterListForm.districtsearch;
		var rattail = new String();
		if(citysearch != null && districtsearch == null){
			tail = tail.replace(/&city=-1/g,"");
			var locationtype = "&city=";
		} else if(districtsearch != null){
			if (tail.indexOf('city=') == -1 && document.FilterListForm.city != null && document.FilterListForm.city.value != null) {
				rattail += "&city="+document.FilterListForm.city.value;
			}
			var locationtype = "&district=";
		}
		if(!immo.isEmpty(locationInfo)){
			
			//Das Location-Panel sollte geoeffnet bleiben, so ein Request daraus abgeschickt wurde
			if (this.objectValue != null)
				immosel.isPanelOpen = true;
		
			//gibt es überhaupt Einträge?
			if(locationInfo.length > 0){
				isLocationChecked = false;
				
				/* es gibt eigentlich nur zwei wichtige Unterscheidungskriterien: */
				/* entweder wurde "Alle" ausgewählt oder etwas "anderes" 		  */
				/* -1 entspricht "Alle" ist ausgewählt 							  */
				/* Problem hierbei ist, zu erkennen, was für ein Element dieser   */
				/* beiden Kriterien zuletzt ausgewählt wurde.					  */
				
				// eine Element ungleich "Alle" hat das Ereignis ausgelöst
				if (this.objectValue != -1){
					for (var i = 1; i < locationInfo.length; i++){
						if(locationInfo[i].checked){
							locationcheck++;
							tail = tail.replace(locationtype+locationInfo[i].value,"");
							rattail += locationtype+locationInfo[i].value;
							isLocationChecked = true;	
						}
					}
					locationInfo[0].checked = !isLocationChecked;
				
				// Element "Alle" wurde zuletzt ausgewählt	
				}else{
					//alle Location Checkboxes ausschalten
					for (var i = 1; i < locationInfo.length; i++){
						locationInfo[i].checked = false;
					}
				}
			}
			
			if (!isLocationChecked){
				rattail = locationtype+"-1";
			}
		}
		
		if(immosel.isPanelOpen){
			rattail += "&openPanel=true"
		}
		
		return tail+rattail;
	},

	checkFromToFields: function(){
		
		var fromprice = $F("fromprice");
		var toprice = $F("toprice");
		var fromrooms = $F("fromrooms");
		var torooms = $F("torooms");
		var fromarea = $F("fromarea");
		var toarea = $F("toarea");
		if (fromprice != "" && toprice != "" && Number(fromprice) > Number(toprice)) {
			return false;
		}
		else if (fromrooms != "" && torooms != "" && Number(fromrooms) > Number(torooms)){
			return false;
		}
		else if (fromarea != "" && toarea != "" && Number(fromarea) > Number(toarea)){
			return false;
		}
		return true;
	},


	setMarketing: function(rattail){
		
		
		
		
		var marketingtypes = document.FilterListForm.marketingtype;
		var marketingtype = 2;
		for (var i=0; i<marketingtypes.length; i++){
			if(marketingtypes[i].checked){
				marketingtype= marketingtypes[i].value;
			}
		}
		return rattail += "&marketingtype=" + marketingtype;
	},


	setRattail: function(rattail){
		
		var fromprice = $F("fromprice");
		var toprice = $F("toprice");
		var fromrooms = $F("fromrooms");
		var torooms = $F("torooms");
		var fromarea = $F("fromarea");
		var toarea = $F("toarea");
		 
		var parentcat = document.FilterListForm.parentcat.value;

		var objectcats = document.FilterListForm.objectcat;
		
		if (!immo.isEmpty(objectcats)) {
			for (var i = 0; i < objectcats.length; i++){
				if (objectcats[i].checked) {
					rattail += "&objectcat=" + objectcats[i].value;
				}
			}
		}
		
		var features = document.FilterListForm.sft;
		
		if(!immo.isEmpty(features)){
			if(features.length != null) {
				for (var i = 0; i < features.length; i++){
					if(features[i].checked){
						rattail += "&sft="+features[i].value;
					}
				}
			} else {
				// Wenn es nur 1 checkbox bzw Ausstattung gibt, 
				// JavaScript erkennt dieses Formular-Element als Array NICHT, 
				// d.h. array.length == UNDEFINED
				if(features.checked){
					rattail += "&sft="+features.value;
				}
			}
		}
		
		rattail = this.setMarketing(rattail);
		rattail +="&parentcat="+parentcat+"&fromprice="+fromprice+"&toprice="+toprice+"&fromrooms="+fromrooms+"&torooms="+torooms+"&fromarea="+fromarea+"&toarea="+toarea;
		return this.locationCheck(rattail);
	},
	
	getElementsByClassName: function(tagName, className, rootNode) {
		if (!rootNode) { rootNode = document.getElementsByTagName('body')[0]; }
		var a = [];
		var re = new RegExp('\\b' + className + '\\b');
		var els = rootNode.getElementsByTagName(tagName);
		for (var i = 0, j = els.length; i < j; i++) {
			if ( re.test(els[i].className) ) {
				a.push(els[i]);
			}
		}
		return a;
	},

	incrementEtracker: function(subsystem, mandant) {
		
		var subsystemInternal = subsystem;
		//Der Mandantenname ist hier bereits im Subsystem enthalten 
		etracker.incrementEtrackerSimple("", subsystemInternal, true);
	}

}

