

var geoLocationValidator = new GeoLocationValidator();

var objectInfoColumn = {type: "Column", children: [
	{type: "Header", text: Labels.agentTitle, whereDoYouWantToLiveHeader: true},
	{type: "FormElement", validationLabel: "Ort", children: [
		{type: "Input", inputType: "text", name: "location", width: 300, valueBinding: "locationDescription", validators: (Configuration.VALIDATE_GEO_LOCATON ? [new MinLengthValidator(), geoLocationValidator] : [new MinLengthValidator()]), required: true, visible: function () {return Configuration.AGENT_SEARCH_TYPE == "location" || Configuration.AGENT_SEARCH_TYPE == "none"}},
		{type: "Input",
            inputType: "TreeComboBox",
            name: "regions",
            validationLabel: "Region",
            valueBinding: "regionManager",
            required: true,
            maxLabelSize: 50,
            dataSource: new JsonTreeDataSource(Configuration.REGIONS_DATA_SOURCE),
            footer: [{label: "Ok", width: "30px", onClick: function () {this.content.hide()}}],
            visible: function () {return Configuration.AGENT_SEARCH_TYPE == "region"},
            validators: [new MaxArraySizeValidator({errorMessage: "Bitte w&auml;hlen Sie mindestens einen und maximal {1} Stadtteile aus."})],
            labels: {loading: "Loading...", noRegions: "Keine Region gew&auml;hlt", noRegionsFound: "No regions found"}}
	]},
	{type: "FormElement", label: "Gew&uuml;nschter Umkreis:", visible: function () {return (Configuration.AGENT_SEARCH_TYPE == "location" || (Configuration.AGENT_SEARCH_TYPE == "none" && Configuration.SHOW_RADIUS_IN_AGENT))}, children: [
		{type: "Input", inputType: "select", name: "radius", width: 70, valueBinding: "radius", optionsFn: function () {
			var res = []
			for (var i = 0; i < MandatorConstants.ImmoSearch.RADIUS_VALUES.length; i++) {
				var val = MandatorConstants.ImmoSearch.RADIUS_VALUES[i]
				res.push({value: val, label: val + " km"})
			}
			return res
		}}
	]},
	{type: "Header", style: {paddingTop: "20px"}, text: "Welche Eigenschaften soll Ihr Wunschobjekt haben?"},
	{type: "FormElement", label: Labels.agentPrice, children: [
		{type: "Input", inputType: "select", name: "maxPrice", width: 70, valueBinding: "priceEnd", required: true, options: [
			{value: 100, label: "100"},
			{value: 200, label: "200"},
			{value: 500, label: "500"},
			{value: 700, label: "700"},
			{value: 1000, label: "1000"},
			{value: 1200, label: "1200"},
			{value: 1500, label: "1500"},
			{value: 1700, label: "1700"},
			{value: 2000, label: "2000"}
		]}
	]},
	{type: "FormElement", label: "min. R&auml;ume:", children: [
		{type: "Input", inputType: "select", name: "minRooms", width: 70, valueBinding: "roomsStart", options: [
			{value: 1, label: "1"},
			{value: 2, label: "2"},
			{value: 3, label: "3"},
			{value: 4, label: "4"},
			{value: 5, label: "5"},
			{value: 6, label: "6"}
		]}
	]},
	{type: "FormElement", label: "min. Gr&ouml;&szlig;e:", children: [
		{type: "Input", inputType: "select", name: "minArea", width: 70, valueBinding: "areaStart",  optionsFn: function () {
			var res = []
					for (var i = 0; i < MandatorConstants.ImmoSearch.AREA_VALUES.length; i++) {
			var val = MandatorConstants.ImmoSearch.AREA_VALUES[i]
			res.push({value: val, label: val})
		}
			return res
		}}
	]},
	{type: "FormElement", label: "Anzahl der einziehenden Personen:", children: [
		{type: "Input", inputType: "select", name: "anzahlPersonen", required: true, width: 70, options: [
			{value: -1, label: ""},
			{value: 1, label: "1"},
			{value: 2, label: "2"},
			{value: 3, label: "3"},
			{value: 4, label: "4"},
			{value: 5, label: "5"},
			{value: 6, label: "6"},
			{value: 7, label: "7"},
			{value: 8, label: "8"},
			{value: 9, label: "9"},
			{value: 10, label: "10"}
		]}
	]},
	{type: "Header", style: {paddingTop: "20px"}, visible: Configuration.SPECIALITIES, text: "W&uuml;nschen Sie besondere Merkmale?"},
	{type: "FormElement", cls: "formCheckboxCell", style: {paddingTop: "10px"}, visible: function () {return specManager ? true : false}, children:[
		{type: "Input", inputType: "CheckboxColumnSet", validationLabel: "Merkmale", /*source: Configuration.SPECIALITIES_FOR_AGENT_DATA_SOURCE, */ topTdClass: "specialtyTdInForm", name: "specialties", valueBinding: "specManager"}
	]}
]};

if (transformObjectInfoColumn) {
	transformObjectInfoColumn(objectInfoColumn)
}

/*
for (var i = 0; i < objectInfoColumn.children.length; i++) {
	if (objectInfoColumn.children[i].type == "Header") {
		objectInfoColumn.children[i].cls = "agentHeader"
	}
}
*/

if (transformContactColumn) {
	transformContactColumn(contactColumn)
}

var contactColumnForAgent = cloneObject(contactColumn)

if (transformContactColumnForAgent) {
	transformContactColumnForAgent(contactColumnForAgent)
}

/*
for (var i = 0; i < contactColumnForAgent.children.length; i++) {
	if (contactColumnForAgent.children[i].type == "Header") {
		contactColumnForAgent.children[i].cls = "agentHeader"
	}
}
*/

var agentFormOpts = {
	content: [objectInfoColumn, contactColumnForAgent],
	defaultSubmit: "send",
	headerClass: "agentHeader",
    storage: Configuration.STATEFULL_FORMS ? new CookiesLocalStorage() : new SkleroseLocalStorage(),
	listeners: {
		submit: {
			send: sendAgentForm
		}
	}
}

if (transformAgentFormOpts) {
	transformAgentFormOpts(agentFormOpts)
}

var agentForm = new Form(agentFormOpts);

var agentPanel = new ColumnPanel({
	target: Configuration.AGENT_PLACEMENT,
	columnSource: agentForm,
    listeners: {
        hide: function () {
            agentForm.saveToStorage()
            
            if (agentFormHidden) {
                agentFormHidden()
            }
        }
    }
});

function showAgentForm() {
	if (agentPanel.isVisible()) {
		agentPanel.hide()
        agentForm.saveToStorage()
        
	} else {
		agentPanel.show()
        agentForm.loadFromStorage()
        agentForm.refreshBindings()
	}
}

function sendAgentForm() {
	if (Configuration.VALIDATE_GEO_LOCATON) {
		geoLocationValidator.sendLocation(sendAgentFormReal)
	} else {
		sendAgentFormReal();
	}
}

function sendAgentFormReal() {
	if (agentForm.validate()) {
		var agentParams = new AgentParameters(presentationId)
		agentForm.serializeForm(agentParams)
		agentParams.objectIdentifiers = Configuration.PRESENTATION_OBJECT_IDENTIFIERS

		if (Configuration.VALIDATE_GEO_LOCATON) {
			var loc = geoLocationValidator.lastLocation
			agentParams.realLocation = loc.location
			agentParams.longitude = loc.longitude
			agentParams.latitude = loc.latitude
		}

		for (p in agentParams) {
			var pp = agentParams[p]

			if (pp && pp.constructor.toString().indexOf("Array") != -1 && pp.length == 1 && pp[0] != "0" && pp[0] != "1" && (pp[0] == true || pp[0] == false)) {
				agentParams[p] = pp[0]
			}
		}

		postProcessAgentForm(agentParams)

		var send = function(ap) {
			ImmoObjectDWRManager.sendAgentRequest(ap, function () {
				agentPanel.hide();
				// agentForm.clearForm();
				$('agentHasBeenSentView').style.display = 'block'
			});
		}

		if (Configuration.AGENT_CONFIRMATION) {
			if (!agentForm.confirmationDialog) {
				agentForm.confirmationDialog = new YAHOO.widget.SimpleDialog("agentCopnfirmationDialog", {
					width: "500px",
					fixedcenter: true,
					visible: false,
					draggable: true,
					modal: true,
					close: false,
					appendtodocumentbody: true,
					icon: YAHOO.widget.SimpleDialog.ICON_INFO,
					constraintoviewport: true,
					buttons: [
						{text: Labels.agentConfirmationAcceptButton, isDefault: false, handler: function() {
							try {
								send(this.ap)
							} finally {
								this.hide()
							}
						}},
						{text: Labels.agentConfirmationCancelButton, isDefault: true, handler: function() {
							this.hide()
						}}
					]
				});

				agentForm.confirmationDialog.setHeader(Labels.agentConfirmationTitle);
				agentForm.confirmationDialog.render(document.body);
			}

			agentForm.confirmationDialog.cfg.setProperty("text", Labels.agentConfirmationBody)
			agentForm.confirmationDialog.ap = agentParams
			agentForm.confirmationDialog.show();
		} else {
			send(agentParams);
		}
	}
}
