
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;
		function onUpdateSearchComplete() {
			immosel.incrementEtracker(pagename.replace('/liste','/liste/seite1') +"/verfeinerung",mandant);
			new Effect.Highlight("trefferanzeige", {startcolor: "#6AA2D8", endcolor: "#D2E3F3"});
			//if (this.lastObject != null && this.lastObject != 0)
			//location.hash=this.lastObject;
		}
		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, onComplete: onUpdateSearchComplete});
		}
	},

	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;
	},


/*	locationCheck: function(tail) {
		
		var locationcheck = 0;
		var locationAllSelected = false;
		var locationInfo = document.FilterListForm.SearchInfos;
		var rattail = new String();
		var locationtype = "&district=";
		var tailstring = new String(tail);
		if(tail.indexOf("vcity") > 0 || tail.indexOf("vicinity") > 0 || tail.indexOf("county") > 0 || tail.indexOf("region") > 0){
			var tail = tail.replace(/&city=-1/g,"");
			locationtype = "&city=";
		}
		if(!immo.isEmpty(locationInfo)){
			if(locationInfo.length > 0){

				for (var i = 1; i < locationInfo.length; i++){
					if(locationInfo[i].checked){
						locationcheck++;
						rattail += locationtype+locationInfo[i].value;	
					}
				}
				// wenn nur "alle" ausgewählt ist
				if(locationcheck == 0){
					if(!locationInfo[0].checked){
						locationInfo[0].checked = true;	
					}
					this.isLocationAll = true;
					rattail = locationtype+locationInfo[0].value;
				
				// wenn mindestens einer mehr als "alle" ausgewählt wurde
				}else{
					//wenn "alle" zuletzt alleine ausgewählt war
					if(this.isLocationAll){
						locationInfo[0].checked = false;
						this.isLocationAll = false;
					}
					// wenn mehr ausgewählt waren und "alle" neu
					if(!this.isLocationAll && locationInfo[0].checked){
						this.isLocationAll = true;
						for (var i = 1; i < locationInfo.length; i++){
							if(locationInfo[i].checked){
								locationInfo[i].checked = false;	
							}
						}
						rattail = locationtype+locationInfo[0].value;
					}
				}
			}else{
				rattail = locationtype+"-1";
			}
		}
		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 = 1;
		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 seperablearea = document.FilterListForm.seperablearea;
		if (!immo.isEmpty(seperablearea) && seperablearea.checked) {
			rattail += "&seperablearea=" + seperablearea.value;
		}else{
			rattail += "&seperablearea=0";
		}
		 
		var parentcats = document.FilterListForm.parentcat;
		if (!immo.isEmpty(parentcats)) {
			var parentcat;
			for (var i = 0; i < parentcats.length; i++){
				if(parentcats.options[i].selected){
					parentcat = parentcats.options[i].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;
				}
			}
		}
		
		var radius = document.FilterListForm.radius;
		if (!immo.isEmpty(radius)){
			if(radius.type == "select-one"){
				for (var i = 0; i < radius.length; i++){
					if(radius.options[i].selected){
						rattail += "&radius="+radius.options[i].value;
					}
				}
			}else{
				rattail += "&radius=0"
			}
		}
		rattail = this.setMarketing(rattail);
		
		rattail +="&parentcat="+parentcat+"&fromprice="+fromprice+"&toprice="+toprice+"&fromrooms="+fromrooms+"&torooms="+torooms+"&fromarea="+fromarea+"&toarea="+toarea;
		
		return this.locationCheck(rattail);
	},

	incrementEtracker: function(subsystem, mandant) {
		
		var subsystemInternal = subsystem;
		//Der Mandantenname ist hier bereits im Subsystem enthalten 
		etracker.incrementEtrackerSimple("", subsystemInternal, true);
	}

}
