﻿cookieoptions = { path: '/' };

function toggleNeufahrzeug(disabled) {
	if(searchOrInsert()) {
		$('select[name^=ez], input[name^=ez], input[name^=kilometer]').attr('disabled', disabled);
		if(disabled)
			$('select[name^=ez], input[name^=ez], input[name^=kilometer]').addClass('disabled');
		else
			$('select[name^=ez], input[name^=ez], input[name^=kilometer]').removeClass('disabled');
		$('#ezMonat_container, #ezMonat_input' ).remove();
		$('select[name^=ez]').selectbox({
			inputClass: 'selectbox',
			containerClass: 'selectbox-wrapper'
		});
	}
	else if(window.location.href.indexOf('bearbeiten') != -1) {
		$('#vehicle_monat, #vehicle_jahr, #km').attr('disabled', disabled);
		if(disabled)
			$('#vehicle_monat, #vehicle_jahr, #km').addClass('disabled');
		else
			$('#vehicle_monat, #vehicle_jahr, #km').removeClass('disabled');
		$('#vehicle_monat_container, #vehicle_monat_input' ).remove();
		$('#vehicle_monat').selectbox({
			inputClass: 'selectbox',
			containerClass: 'selectbox-wrapper'
		});
	}
	else {
		$('select[name^=ezR], select[name^=kilometerR]').attr('disabled', disabled);
		if(disabled)
			$('select[name^=ezR], select[name^=kilometerR]').addClass('disabled');
		else
			$('select[name^=ezR], select[name^=kilometerR]').removeClass('disabled');
		$('#ezR1_container, #ezR2_container, #ezR1_input, #ezR2_input' ).remove();
		$('#kmR1_container, #kmR2_container, #kmR1_input, #kmR2_input' ).remove();
		$('select[name^=ezR], select[name^=kilometerR]').selectbox({
			inputClass: 'selectbox',
			containerClass: 'selectbox-wrapper'
		});
	}
}

function toggleCompany(disabled) {
	if(disabled) {
		$('#company').removeClass('readonly');
		$('#company').removeClass('disabled');
		$('#company').attr('readonly', false);
		$('#company').attr('disabled', false);
		$('#ustidnr').removeClass('readonly');
		$('#ustidnr').removeClass('disabled');
		$('#ustidnr').attr('readonly', false);
		$('#ustidnr').attr('disabled', false);
	}
	else {
		$('#company').val('');
		$('#company').addClass('readonly');
		$('#company').addClass('disabled');
		$('#company').attr('readonly', true);
		$('#company').attr('disabled', true);
		$('#ustidnr').val('');
		$('#ustidnr').addClass('readonly');
		$('#ustidnr').addClass('disabled');
		$('#ustidnr').attr('readonly', true);
		$('#ustidnr').attr('disabled', true);
	}
}

function searchOrInsert() {
	if(window.location.href.indexOf('inserieren') != -1)
		return true;
	else
		return false;
}

$(document).ready(function() {
	
		//RESIZING COMPARE-TABLE		
		$('.compare_column').css({
			width: ((640)/$('.compare_column').length),
			margin: '0'
		});
		//$('.compare_column').filter(':odd').addClass('bg_grey');
		$('.compare_column div').not('.placerimg, .placer').css({
			padding: '5px'
		});
		$($('.compare_column').get($('.compare_column').length-1)).css({
			width: $($('.compare_column').get($('.compare_column').length-1)).width()-20
		});
		$('#fieldNames div').each(function(index,element) {
			$([element, $('.fieldInserts0 div').get(index), $('.fieldInserts1 div').get(index)]).not('.placerimg, .placer').hover(function(event) {
				$($('#fieldNames div').get(index)).toggleClass('bg_grey');
				$($('.fieldInserts0 div').get(index)).toggleClass('bg_grey');
				$($('.fieldInserts1 div').get(index)).toggleClass('bg_grey');
				$($('.fieldInserts2 div').get(index)).toggleClass('bg_grey');
			});
			$($('.fieldInserts2 div').get(index)).hover(function() {
				$($('#fieldNames div').get(index)).toggleClass('bg_grey');
				$($('.fieldInserts0 div').get(index)).toggleClass('bg_grey');
				$($('.fieldInserts1 div').get(index)).toggleClass('bg_grey');
				$($('.fieldInserts2 div').get(index)).toggleClass('bg_grey');
			});
			
			var max = Math.max(
				$(element).height(),
				$($('.fieldInserts0 div').get(index)).height(),
				$($('.fieldInserts1 div').get(index)).height(),
				$($('.fieldInserts2 div').get(index)).height()
			);
			$(element).height(max);
			$($('.fieldInserts0 div').get(index)).height(max);
			$($('.fieldInserts1 div').get(index)).height(max);
			$($('.fieldInserts2 div').get(index)).height(max);
		});
		
		//ALL CONCEPTS AUTOMATIC CHECKING
		$('#allconcepts').change(function() {
			$('input.concept').attr('checked', false);
			$('#allconcepts').attr('checked', true);
		});
		$('input.concept').change(function() {
			if($(this).is(':checked')) {
				$('#allconcepts').attr('checked', false);
			}
			else {
				var array = [];
				$('input.concept').each(function() {
					if(!$(this).is(':checked'))
						array.push(1);
				});
				if(array.length == $('input.concept').length)
					$('#allconcepts').attr('checked', true);
			}
		});
		
		//AUTOMATIC CHECKING RESULTSETS
		$('input[name=selectAll]').change(function() {
			if($(this).is(':checked'))
				$('input.'+$(this).val()).attr('checked', true);
			else
				$('input.'+$(this).val()).attr('checked', false);
		});
		
		//OBSERVER IMPRINT-Button
		$('#mp_imprint').click(function(event) {
			event.preventDefault();
			$('.mp_imprint').toggle();
		});
		
		//OBSERVER FOR DIRECTPAGE SELECT
		$('select.directPage').change(function() {
			value = $(this).val();
			$('.directPage').each(function() {
				$(this).val(value);
			});
			this.form.submit();
		});
		
		$('select.sortBy').change(function() {
			value = $(this).val();
			$('.sortBy').each(function() {
				$(this).val(value);
			});
			this.form.submit();
		});
		
		//OBSERVE FOR ERRORS
		if($('.pageerror .errors, .pageerror4 .errors').length > 0 && $('.pageerror .errors, .pageerror4 .errors').text() != '&nbsp;') {
			$($('.pageerror .errors, .pageerror4 .errors').text().split(',')).each(function(index, element) {
				$('h3.'+element+', label.'+element).addClass('errorField');
			});
		}
		if($('.pageerror .false, .pageerror4 .false').length > 0 && $('.pageerror .false, .pageerror4 .false').text() != '&nbsp;') {
			$($('.pageerror .false, .pageerror4 .false').text().split(',')).each(function(index, element) {
				$('h3.'+element+', label.'+element).addClass('errorField');
			});
		}
		
		//OBSERVE FILTERHEADLINES
		$('.tabtoggle').click(function() {
			var id = $(this).attr('id');
			$('div.tab.'+id).slideToggle(function() {
				cookie = $.parseJSON($.cookie('closedSearchTabs'));
				if(cookie == null)
					var cookie = new Object();
				cookie[id] = $(this).is(':visible');
				$.cookie('closedSearchTabs', $.toJSON(cookie),cookieoptions);
			});
		});
		$('div.tab').hide();
		if($.cookie('closedSearchTabs') != null) {
			var cookie = $.parseJSON($.cookie('closedSearchTabs'));
			for(var index in cookie) {
				if(cookie[index] === false)
					$('div.tab.'+index).hide();
				else
					$('div.tab.'+index).show();
			}
		}
		
		//OBSERVE DESCRIPTION TEXTAREA FOR 1000 CHARACTERS
		$('textarea[name=bemerkung]').keyup(function() {
			count = $(this).val().length;
			if(count > 1000)
				$(this).val($(this).val().substr(0,1000))
			$('#bemerkungCount').text(1000-($(this).val().length));
		});
		
		//CHECK FOR DISABLING SOME FIELDS
		$('#neufahrzeug').change(function() {
			if($(this).val() == 1 || $(this).val() == '') {
				toggleNeufahrzeug(true);
			}
			else {
				toggleNeufahrzeug(false);
			}
		});
		if($('#neufahrzeug').val() == 1 || $('#neufahrzeug').val() == '')
			toggleNeufahrzeug(true);

		$('#marke').change(function() {
			if($('#marke').val() != '') {
				$.post('/ajax.php', {
					'load': 'models',
					'brand': $(this).val(),
					'inserieren': searchOrInsert()
				},
				function(data) {
					$('#modell option').remove();
					if(searchOrInsert())
						$('#modell').append($("<option></option> ").val('').text('Bitte auswählen'));
					else
						$('#modell').append($("<option></option> ").val('').text('Beliebig'));
					$($.parseJSON(data)).each(function(index, element) {
						$('#modell').append($("<option></option> ").val(element).text(element));
					});
					$('#modell').attr('disabled', false);
					$('#modell').removeClass('disabled');
					$('#modell_container').remove();
					$('#modell_input').remove();
					$('#modell').selectbox({
						inputClass: 'selectbox',
						containerClass: 'selectbox-wrapper'
					});
				});
			}
			else {
				$('#modell option').remove();
				$('#modell').append($("<option></option> ").val('').text(' '));
				$('#modell').attr('disabled', true);
				$('#modell').addClass('disabled');
				$('#modell_container').remove();
				$('#modell_input').remove();
				$('#modell').selectbox({
					inputClass: 'selectbox',
					containerClass: 'selectbox-wrapper'
				});
			}
		});
		
		if(!searchOrInsert()) {
			$('input#zipcode').change(function() {
				$.post('/ajax.php', {
					'load': 'city',
					'post': $(this).val()
				},
				function(data) {
					$('#city').val($.parseJSON(data));
				});
			});
			$('select#industry').change(function() {
				disable = false;
				if($(this).val() == 1)
					disable = true;
				toggleCompany(disable);
			});
			if($('select#industry').val() == 1)
				toggleCompany(true);
			else
				toggleCompany(false);
		}
		
		$('.smallpictures img').mouseover(function() {
			$('.bigpicture img').attr('src', $(this).attr('src').replace('_half', '_c2'));
		});
		
		if(!searchOrInsert()) {
			$('#searchform input, #searchform select, #searchform textarea').change(function() {
				post = new Object();
				count = 0;
				$('#searchform input, #searchform textarea, #searchform select').each(function(index, element) {
					if(!$(element).is(':disabled') && $(element).attr('type') != 'submit' && $(element).attr('name') != '') {
						if($(element).attr('type') == 'checkbox') {
							if($(element).is(':checked')) {
								if(!post[$(element).attr('name').replace(/\[.*\]/, '')])
									post[$(element).attr('name').replace(/\[.*\]/, '')] = new Object();
								post[$(element).attr('name').replace(/\[.*\]/, '')][count] = $(element).val();
							}
							count++;
						}
						else {
							if($(element).attr('name').indexOf('[') != -1) {
								sub = $(element).attr('name').replace(/^.*\[(.*)\]$/, '$1');
								if(!post[$(element).attr('name').replace(/\[.*\]/, '')])
									post[$(element).attr('name').replace(/\[.*\]/, '')] = new Object();
								post[$(element).attr('name').replace(/\[.*\]/, '')][sub] = $(element).val();
							}
							else
								post[$(element).attr('name')] = $(element).val();
						}
					}
				});
				$.post('/ajax.php', {
					'load': 'itemAmount',
					'post': post
				},
				function(data) {
					$('.c6 > .bigsubmit').each(function() {
						$(this).val($.parseJSON(data)+' Treffer');
					});
					$('em > .resultMatchs').each(function() {
						$(this).text($.parseJSON(data)+' Treffer');
					});
				});
			});
	}
});
