var order = {

	// Funkcja przeniesiona z backendu ktora liczy po stronie JS cene
	// zamowionego produktu na podstawie jego opcji
	productPrice: function(ident_options, options)
	{
		var price = 0.0;
		var extra = Array();
		$(ident_options).each(function(i, opt) {
			var default_value = parseInt(opt.value);
			if (isNaN(default_value)) {
				default_value = 1;
			}
			if (opt.name in options) {
				var opt_value = parseInt(options[opt.name]);
				if (isNaN(opt_value)) {
					opt_value = 1;
				}
			}
			else {
				return true; // continue
			}
			if (opt.extra) {
				extra.push($.evalJSON(opt.extra));
			}
			price += opt.cost * opt.cost_count * (opt_value/default_value);
		});

		// Extra funkcje dla opcji
		for (var i = 0; i < extra.length; i++) {
			var ex = extra[i];
			for (var key in ex) {
				if (key == 'total_price') {
					if (ex[key].indexOf('%') > -1) {
						var param = parseFloat(ex[key].replace('%', ''))/100+1
						price = price * param;
					}
				}
			}
		}

		return price.toFixed(2);
	},

	// Autowypelnienie formularza z danymi, wylacznie do debugu
	fill: function()
	{
		$('input[name=login]').val('login');
		$('input[name=password]').val('dupa123');
		$('input[name=password_confirm]').val('dupa123');
		$('input[name=pesel]').val('00000000000');
		$('input[name=nip]').val('123');
		$('input[name=regon]').val('123');
		$('input[name=contact_data.name]').val('name');
		$('input[name=contact_data.surname]').val('surname');
		$('input[name=contact_data.street]').val('street');
		$('input[name=contact_data.city]').val('city');
		$('input[name=contact_data.pc1]').val('62');
		$('input[name=contact_data.pc2]').val('200');
		$('input[name=voice]').val('+48.790613092');
		$('input[name=contact_data.phone]').val('+48790613092');
		$('input[name=email]').val('p.bylina@gmail.com');
		//$('input[name=individual]').attr('checked', 'checked');
		$('input[name=consentforpublishing]').attr('checked', 'checked');
	},

	// Inicjalizacja odpowiednich akcji na stronie koszyka
	step0_init: function()
	{
		$('.del img').click(function() {
			$('#step0').attr('action', '');
			$('#step0').append($('<input type="hidden" name="hash" />').val($('#hash').html()));
			$('#step0').append('<input type="hidden" name="del" value="1" />');
			$('#step0').submit();
		});
		$('#next').click(function() {
			$('#step0').attr('action', '');
			$('#step0').append($('<input type="hidden" name="hash" />').val($('#hash').html()));
			$('#step0').submit();
		});
	},

	// Metoda obsuglujaca akcje w formularzu wypelniania danych osobowych
	// podczas skladania zamowienia
	step1_init: function(edit)
	{
		tp.tooltip();

		$('input[name="individual"]:checked').click();
		if ($('#mail_contact:checked').length) {
			$('#mail_contact:checked').click();
			$('#mail_contact').attr('checked', 'checked');
		}
		else {
			$('#contact_data').find('input').attr('disabled', 'disabled');
		}

		$.ajaxSetup({type: "post"});
		$('#step1').validate({
			onkeyup: false,
			rules: {
				'login': {
					required: true,
					minlength: 4,
					maxlength: 8,
					login: true,
					remote: '/ajax/order/valid/'
				},
				'password': {
					required: true,
					minlength: 5,
					maxlength: 36,
					password: true
				},
				'password_confirm': {
					required: true,
					minlength: 5,
					maxlength: 36,
					password: true,
					equalTo: '#password'
				},
				'email': {
					required: true,
					email: true
				},

				'contact_data.name': {
					required: true
				},
				'contact_data.surname': {
					required: true
				},
				'contact_data.phone': {
					required: true,
					minlength: 12,
					maxlength: 12,
					phone: true
				},
				'contact_data.street': {
					required: true
				},
				'contact_data.city': {
					required: true
				},
				'contact_data.pc1': {
					minlength: 2,
					maxlength: 2,
					number: true
				},
				'contact_data.pc2': {
					minlength: 3,
					maxlength: 3,
					number: true,
					requiredMany: ['contact_data.pc1']
					},
					'contact_data.country': {
						required: true
					},
					'pesel': {
						required: true,
						pesel: true
					},
					'company_name': {
						required: true
					},
					'nip': {
						required: true,
						minlength: 10,
						maxlength: 10,
						number: true,
						nip: true
					},

					'mail_contact_data.name': {
						required: true
					},
					'mail_contact_data.surname': {
						required: true
					},
					'mail_contact_data.phone': {
						required: true,
						minlength: 12,
						maxlength: 12,
						phone: true
					},
					'mail_contact_data.street': {
						required: true
					},
					'mail_contact_data.city': {
						required: true
					},
					'mail_contact_data.pc1': {
						required: true,
						minlength: 2,
						maxlength: 2,
						number: true
					},
					'mail_contact_data.pc2': {
						requiredMany: ['mail_contact_data.pc1'],
						minlength: 3,
						maxlength: 3,
						number: true
					},
					'mail_contact_data.country': {
						required: true
					}
				},
				messages: {
					'login': {
						remote: 'Ten login jest już zajęty!'
					},
					'email': {
						remote: 'Ten e-mail jest już zajęty!'
					}
				}
			});

	}

};

