/**
 * zmiana wyposażenia (sprawdzam czy wyświetlić inputa czy nie)
 * @param value int wartość selecta
 * @param id int id wyposażenia
 */
function changeEquipment(value, id){
	if (value < 2){
		$('#Equipment'+id).hide();
	}else{
		$('#Equipment'+id).show();
	}
}

/**
 * Konfigurator - wybranie auta
 * @param car_id int id auta
 * @param url_update_cars string url dla ajaxa
 * @param td_to_update string id td'ka do update'a
 * @param tr_to_update string id tr'a do wyświetlenia
 * @param div_to_clear string id div'a do wyczyszczenia zawartości
 */
function selectCar(car_id, url_update_cars, td_to_update, tr_to_update, div_to_clear){
	if ($('#ColorPrice') != null){
		$('#ColorPrice').val(0);
	}	
	
	$.ajax({
		url: url_update_cars + '/' + car_id,
		cache: false,
		async: true,
		beforeSend: function(){
			$('#ajax_waiting').show();
		},
		success: function(html){
			$('#ajax_waiting').hide();
			$('#' + td_to_update).html(html);
			
			if (car_id != ''){
				$('#' + tr_to_update).show();
			}else{
				$('#' + tr_to_update).hide();
			}
			
			if ($('#CarVersionId').val() == ''){
				$('#' + div_to_clear).html('');
			}	
			
			if ($('#CarVersionId').val() == ''){
				$('#color_tr').hide();
				$('#ColorId').attr('selectedIndex', 0);
			}
			updatePrettySelect();
			
			if (car_version_to_select > 0){
				selectCarVersion(car_version_to_select, url_configurator_update_equipment, 'equipment');
				car_version_to_select = 0;
			}
		}
	});
}

/**
 * Konfigurator - wybranie wersji modelu
 * @param car_version_id int id wersji
 * @param url_configurator_update_equipment string url do ajaxa
 * @param div_to_update string id div'a do update'a
 */
function selectCarVersion(car_version_id, url_configurator_update_equipment, div_to_update){
	if ($('#ColorPrice') != null){
		$('#ColorPrice').val(0);
	}
	
	$('#' + div_to_update).html('Pobieranie danych, proszę czekać');
	
	$.ajax({
		url: url_configurator_update_equipment + '/' + car_version_id,
		cache: false,
		async: true,
		beforeSend: function(){
			$('#ajax_waiting').show();
		},
		success: function(html){
			$('#ajax_waiting').hide();
			$('#' + div_to_update).html(html);
			if (!configurator_first_open){
				try{
					var url = car_version_links[car_version_id];
					if (url != '' && url != 'undefined'){
						pageTracker._trackPageview(url);
					}
				}catch(err){
				}
			}
			configurator_first_open = false;
			
			$('#CarVersionId').val(car_version_id);
			updatePrettySelect();
			
			$('input[type=checkbox]').prettyCheckboxes({
				checkboxWidth: 16,
				checkboxHeight: 16
			});
		}
	});
}


/**
 * Konfigurator - wybranie wyposażenia -> FLOTA
 * @param select_value int wartość selecta (0 => nie, 1 => tak
 * @param equipment_id int id wyposażenia
 * @param span_brutto_id string id spana z ceną brutto
 * @param span_netto_id string id spana z ceną netto
 * @param prices array tablica z cenami za wyposażenie (id_wyposazenia=>cena)
 */
function updateConfigurator(select_value, equipment_id, span_brutto_id, span_netto_id, prices, span_brutto_with_accessory_id, span_netto_with_accessory_id){
	if (equipment_id != current_upholstery_equipment){
		var current_price = parseInt($('#' + span_brutto_id).html());
		//var current_price_accessory = parseInt($(span_brutto_with_accessory_id).innerHTML);
		if (select_value == 0){//wybrano 'NIE'
			$('#' + span_brutto_id).html(current_price - prices[equipment_id]);
			//aktualizacja ceny brutto
			$('#equipment_price_brutto_' + equipment_id).html('&#151;');
			//aktualizacja ceny netto
			$('#equipment_price_netto_' + equipment_id).html('&#151;');
		}else if(select_value == 1){//wybrano 'TAK'
			$('#' + span_brutto_id).html(current_price + prices[equipment_id]);
			//aktualizacja ceny brutto
			$('#equipment_price_brutto_' + equipment_id).html(prices[equipment_id]);
			//aktualizacja ceny netto
			$('#equipment_price_netto_' + equipment_id).html(Math.round((prices[equipment_id] / 1.23)*Math.pow(10,2))/Math.pow(10,2));
		}
		
		$('#' + span_netto_id).html(Math.round((parseInt($('#' + span_brutto_id).html()) / 1.23)*Math.pow(10,2))/Math.pow(10,2));
		$('#SaleNettoPrice').value = Math.round((parseInt($('#' + span_brutto_id).html()) / 1.23)*Math.pow(10,2))/Math.pow(10,2);
	}else{
		if (check_current_upholstery){
			alert('Nie możesz zmienić tej opcji. Patrz: Tapicerka');
			$('#CarVersionsEquipment' + equipment_id).val($('#CarVersionsEquipment' + equipment_id).val() == 1 ? 0 : 1);
			updatePrettySelect();
		}
	}
}

function displayMoreGama(){
	$('#more_gama').hide();
	$('#more_gama_show').slideDown();
}

function disableMoreGama(){
	$('#more_gama').show();
	$('#more_gama_show').slideUp();
}

function updatePhoto(){
	if (!added_promotion_photo){
		$.ajax({
			url: url_car_photos_show + $('#CarId').val() + '/' + car_version_doors + '/' + $('#ColorId').val() + '?jquery=true',
			cache: false,
			async: true,
			beforeSend: function(){
				$('#ajax_waiting').show();
			},
			success: function(html){
				$('#ajax_waiting').hide();
				if (!added_promotion_photo){
					$('#car_photos').html(html);
				}
			}
		});
		
		$('#SaleColor').val(document.getElementById('ColorId').options[document.getElementById('ColorId').selectedIndex].innerHTML);
	}
}

function selectUpholstery(id){
	if (id != current_upholstery){
		if (current_upholstery > 0){
			$('#upholstery_' + current_upholstery).css('border-color', 'white');
		}
		
		$('#upholstery_' + id).css('border-color', 'red');
		$('#UpholsteryId').val(id);
		
		$('#SaleUpholstery').val(upholstery_names[id]);
		
		if (current_upholstery > 0){
			for (var i = 0; i < upholsteries_equipments_disabled[current_upholstery].length; i++){
				$('#CarVersionsEquipment' + upholsteries_equipments_disabled[current_upholstery][i]).removeAttr('disabled');
			}
		}
		
		current_upholstery = id;
		
		if (upholsteries_standards[id] == 1){
			$('#upholstery_brutto').html('standard');
			if (current_upholstery_equipment > 0){
				var tmp_current_upholstery_equipment = current_upholstery_equipment;
				current_upholstery_equipment = 0;
				$('#CarVersionsEquipment' + tmp_current_upholstery_equipment).val(0);
				updateConfigurator(0, tmp_current_upholstery_equipment, 'price_brutto', 'price_netto', version_prices, 'price_brutto_with_accessory', 'price_netto_with_accessory');
			}else{
				current_upholstery_equipment = 0;
			}
		}else if (upholsteries_standards[id] == 0){
			for (var i = 0; i < upholsteries_equipments_disabled[id].length; i++){
				$('#CarVersionsEquipment' + upholsteries_equipments_disabled[id][i]).attr('disabled', 'disabled');
			}
			$('#upholstery_brutto').html('wybrano opcję');
			var tmp_current_upholstery_equipment = current_upholstery_equipment;
			current_upholstery_equipment = 0;
			
			if (upholsteries_equipments[id] != tmp_current_upholstery_equipment){
				if (upholsteries_equipments[id] > 0){
					$('#CarVersionsEquipment' + upholsteries_equipments[id]).val(1);
					updateConfigurator(1, upholsteries_equipments[id], 'price_brutto', 'price_netto', version_prices, 'price_brutto_with_accessory', 'price_netto_with_accessory');
				}
				if (tmp_current_upholstery_equipment > 0){				
					$('#CarVersionsEquipment' + tmp_current_upholstery_equipment).val(0);
					updateConfigurator(0, tmp_current_upholstery_equipment, 'price_brutto', 'price_netto', version_prices, 'price_brutto_with_accessory', 'price_netto_with_accessory');
				}
			}
			
			current_upholstery_equipment = upholsteries_equipments[id];
		}
	}
	
	updatePrettySelect();
}

function checkMail(){
	if ($('#OffertMail').value == ''){
		alert('Proszę podać adres e-mail');
		return false;
	}
	
	return true;
}

function updateCarPrice(price){
	if (!is_promotion && $('#price_brutto')){
		$('#price_brutto').html(parseInt($('#price_brutto').html()) - $('#ColorPrice').val() + price);
		$('#price_netto').html(Math.round(parseInt($('#price_brutto').html() / 1.23) * Math.pow(10,2))/Math.pow(10,2));
		$('#ColorPrice').val(price);
	}
}

function goToMailForm(){
	var url = document.location.href;
	if (url.match('#.*') != null){
		url = url.replace(url.match('#.*'), '');
	}
	
	url += '#contact';
	document.location.href = url;
}

/**
 * Aktualizuje stan wszystkich selectów
 * Pożyteczne przy zmianie np disabled i prettyChecbox
 */
function updatePrettySelect(){
	$('select').msDropDown({
		showIcon: false,
		visibleRows: 10
	});
}

function updatePromotionPhoto(promotion_id){
	$.ajax({
		url: url_user_sales_show_promotion_photo + '/' + promotion_id,
		cache: false,
		async: true,
		beforeSend: function(){
			$('#ajax_waiting').show();
		},	
		success: function(html){
			$('#ajax_waiting').hide();
			$('body').append(html);
		}
	});
}

function activateAllInputs(){
	$('input').removeAttr('disabled');
	$('select').removeAttr('disabled');
}

