
/*
===============================================================================

 template: Template 44 - JS

===============================================================================
*/

/* -------------------------------------------------------------
{URL} - jQuery
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Autor:		SSo
Company:	onOffice Software GmbH
Release:	1.0
------------------------------------------------------------- */

$(document).ready(function() {

var form = $(".wertermittlung");

	form.steps({
		headerTag: ".step",
		bodyTag: ".lead-fieldset",
		autoFocus: true,

		labels: {
			cancel: "Cancel",
			current: "current step:",
			pagination: "Pagination",
			finish: "Absenden",
			next: "Weiter",
			previous: "Zurück",
			loading: "Loading ..."
		},

		onStepChanging: function (event, currentIndex, newIndex)
		{
			// Allways allow previous action even if the current form is not valid!
			if (currentIndex > newIndex)
			{
				return true;
			}
			// Needed in some cases if the user went back (clean up)
			if (currentIndex < newIndex)
			{
				// To remove error styles
				form.find(".body:eq(" + newIndex + ") label.error").remove();
				form.find(".body:eq(" + newIndex + ") .error").removeClass("error");
			}

			form.validate().settings.ignore = ":disabled,:hidden";
			return form.valid();

		},

		onFinishing: function (event, currentIndex)
		{
			form.validate().settings.ignore = ":disabled";
			return form.valid();
		},

		onFinished: function (event, currentIndex)
		{
			form.submit();
		}

		}).validate({
			lang: 'de',
			errorPlacement: function errorPlacement(error, element) { element.before(error); },
			rules: {
				confirm: {
					equalTo: "#password-2"
				}
			}
		});

	$('.steps li').addClass('lead-background-color lead-one-fourth');

	$('.actions').each(function(){

		$(this).find('li').addClass('lead-background-color');

		$(this).find('li:first').addClass('previous-step lead-one-fifth');
		$(this).find('li:eq(1)').addClass('next-step lead-one-fifth');
		$(this).find('li:last').addClass('finish-step lead-one-fifth');

	});

	$('.immotyp label input').change(function(){

		$('.reset-kat').attr('value','');

		$('.only-haus, .only-wohnung, .only-grundstueck, .only-gewerbe').addClass('hidden');

		var inputValue = $(this).val();

		var inputDataSetName = $(this).attr('data-set-name');

		var inputDataSetType = $(this).attr('data-set-objekttyp');

		console.log(inputDataSetType);

		if(inputDataSetName == 'name-objekttyp') {

			$('#objekttyp').attr('value',inputValue);

		}

		else if(inputDataSetName == 'name-objektart') {

			$('#objektart').attr('value',inputValue);
			
		}

		else if(inputDataSetName == 'name-nutzungsart') {

			$('#nutzungsart').attr('value',inputValue);
			
		}

		if(inputDataSetType == 'only-gewerbe') {

			$('.only-gewerbe').removeClass('hidden')
			
		}

		if(inputDataSetType == 'only-haus') {

			$('.only-haus').removeClass('hidden')
			
		}

		if(inputDataSetType == 'only-wohnung') {

			$('.only-wohnung').removeClass('hidden')
			
		}

		if(inputDataSetType == 'only-grundstueck') {

			$('.only-grundstueck').removeClass('hidden')
			
		}

	});

	var leadSuccess = $.trim($('.lead-success').html());

	if(leadSuccess != '') {
		$('.steps, .lead-fieldset, .actions').hide();
		$('.lead-success').addClass('lead-background-color lead-success-padding');
	}

	swapElements($('.previous-step')[0], $('.next-step')[0]);
	swapElements($('.previous-step')[0], $('.finish-step')[0]);

});

function swapElements(elm1, elm2) {
	var parent1, next1,
		parent2, next2;

	parent1 = elm1.parentNode;
	next1   = elm1.nextSibling;
	parent2 = elm2.parentNode;
	next2   = elm2.nextSibling;

	parent1.insertBefore(elm2, next1);
	parent2.insertBefore(elm1, next2);
}