//<![CDATA[
	// ustalenie sciezki na ktora wysylac pliki, wywolywac akcja z MultiPowUpload
var step = 0;
	$(document).ready(function(){
		$("#quantity").spinner({min: 0});
		$('#quantity').bind('spinchange', function(event, ui) {
			$('#quantity').val(ui.value);
		});
		$('#photosCount').text(countPhotos());
		if(document.getElementById('stepList')) {
			if(document.getElementById('FlashFilesUpload')) {
				step = 3;
			}
			if(document.getElementById('accepted')) {
				step = 4;
			}
			if(document.getElementById('accept_changes')) {
				step = 3;
			}
			var rozmiar = $('#stepList li').size();
			for(var i=0;i<rozmiar;i++) {
				if(i < (step-1)) {
					$('#stepList li').eq(i).addClass('done');
					$('#stepList li').eq(i).removeClass('active');
				}
				else if(i == (step -1)) {
					$('#stepList li').eq(i).addClass('active');
				}
			}
		}
	});
	function sendUserForm() {
		if(document.user_form.reg_accept.checked == false) {
			if(ga) {
				try {
					var pageTracker = _gat._getTracker("UA-3146697-1");
					pageTracker._trackPageview(ga.replace('dane-kontaktowe', 'blad'));
				} catch(err) {}
			}
			alert('Musisz zaakceptować regulamin aby móc skorzystać z naszych usług.');
			return false;
		}
		if (document.user_form.payment_id.value == 0) {
			if(ga) {
				try {
					var pageTracker = _gat._getTracker("UA-3146697-1");
					pageTracker._trackPageview(ga.replace('dane-kontaktowe', 'blad'));
				} catch(err) {}
			}
			alert('Proszę wybrać sposób płatności.');
			return false;
		}
		if(document.user_form.payment_id.options[document.user_form.payment_id.selectedIndex].className == 'needDest' && document.user_form.destination_id.value == 0) {
			if(ga) {
				try {
					var pageTracker = _gat._getTracker("UA-3146697-1");
					pageTracker._trackPageview(ga.replace('dane-kontaktowe', 'blad'));
				} catch(err) {}
			}
			alert('Wskaż miejsce odbioru zamówienia.');
			return false;
		}
		return true;
	}
	function setUploadUrl() {
		var Flash;
		var uploadUrl = 'http://www.crazyfoto.pl/zamow-odbitki.php?upload=' + document.upload.session_id.value + '&q=' + $('#quantity').val() + '&f=' + document.upload.format.value + '&fr=' + document.upload.frame.value;
		if(document.embeds && document.embeds.length>=1 && navigator.userAgent.indexOf("Safari") == -1)
			Flash = document.getElementById("EmbedFlashFilesUpload");
		else
			Flash = document.getElementById("FlashFilesUpload");
		Flash.SetVariable("uploadUrl", uploadUrl);
	}
	// obsluga pokazywania/ukrywania pol formularza wymaganych gdy zadano faktury
	function init() {
		if(document.getElementById('invoice')) {
			var invoice = document.getElementById('invoice');
			var invoice_fields = document.getElementById('invoice_fields');
			invoice.onclick = function() {
				if(this.checked == true) {
					invoice_fields.style.display='block';
				}
				else {
					invoice_fields.style.display='none';
				}
			}
			if(invoice.checked == false) {
				invoice_fields.style.display='none';
			}
		}
		if(document.getElementById('promocode')) {
			var promocode = document.getElementById('promocode');
			var promocode_fields = document.getElementById('podajKodCennika');
			promocode.onclick = function() {
				if(this.checked == true) {
					promocode_fields.style.display='block';
				}
				else {
					promocode_fields.style.display='none';
				}
			}
			if(promocode.checked == false) {
				promocode_fields.style.display='none';
			}
		}
		if(document.user_form) {
			document.user_form.onsubmit = function() {
				return sendUserForm();
			}
			if(document.user_form.payment_id) {
			function destinationFieldsVisibility() {
				var payment_id = document.user_form.payment_id;
				if(payment_id.options[payment_id.selectedIndex].className == 'needDest') {
					document.getElementById('destination_fields').style.display = 'block';
				}
				else {
					document.getElementById('destination_fields').style.display = 'none';
				}
			}
			destinationFieldsVisibility();
			var payment_id = document.user_form.payment_id;			
			payment_id.onchange = destinationFieldsVisibility;
			}
		}
		if(document.upload) {
			setUploadUrl();
			var multiformat_view = document.getElementById('multiformat_view');
			multiformat_view.onclick = function() {
				setRedirectUrl();
			}
			document.upload.onsubmit = function () {
				if(document.getElementById('file0').value != '') {
					document.upload.action = 'http://www.crazyfoto.pl/zamow-odbitki.php?upload=' + document.upload.session_id.value + '&q=' + document.upload.quantity.value + '&f=' + document.upload.format.value + '&fr=' + document.upload.frame.value;
				}
				else {
					alert('nie wybrano plikow');
					return false;
				}
			}
		}
	}

	var gFiles = 1;
	function addFile() {
	    var li = document.createElement('li');
	    li.setAttribute('id', 'file-' + gFiles);
	    li.innerHTML = '<input type="file" name="file' + gFiles + '" onchange="addFile()" /><span onclick="removeFile(\'file-' + gFiles + '\')" style="cursor:pointer;">Usun</span>';
	    document.getElementById('html_files').appendChild(li);
	    gFiles++;
	}
	function removeFile(aId) {
	    var obj = document.getElementById(aId);
	    obj.parentNode.removeChild(obj);
	}

	function updatePrices(element) {
		var count = document.orderDetails.elements.length;
		var prices = new Array();
		var formats = new Array();
		var odids = new Array();
		if(element.name.substring(0,6) == 'format') {
			// alert('Zmianie ulegl format');
			// zatem trzeba przeliczyc wszystkie wartosci.
		}
		if(element.name.substring(0,8) == 'quantity') {
			// alert('Zmianie ulegla liczebnosc');
			// wystarczy przeliczyc tylko elementy o danym formacie
			$('#photosCount').text(countPhotos());
		}
		var e; // temp
		var j; // temp
		var l; // temp
		for(var i=0;i<count;i++) {
			e = document.orderDetails.elements[i];
			if(e.name.substring(0,6) == 'format' && e.name.substring(0,7) == 'format-') {
				j = prices.length;
				prices[j] = e.name.split('-');
				prices[j][0] = e.value;
			}
			else {
				if(e.name.substring(0,6) == 'format') {
					j = formats.length;
					l = 0;
					for(var k=0;k<j;k++) {
						if(formats[k][0] == e.value) {
							formats[k][1] += parseInt(eval('document.orderDetails.quantity'+e.name.substr(6,e.name.length-6)+'.value'));
							l = 1;
							k = j;
						}
					}
					if(l == 0) {
						formats[j] = new Array(e.value, parseInt(eval('document.orderDetails.quantity'+e.name.substr(6,e.name.length-6)+'.value')));
					}
					// dodaj orderDetailId do odids, jesli go jeszcze tam nie ma.
					j = odids.length;
					l = 0;
					for(var k=0;k<j;k++) {
						if(odids[k] == e.name.substr(6,e.name.length-6)) {
							l = 1;
							k = j;
						}
					}
					if(l == 0) {
						odids[j] = e.name.substr(6,e.name.length-6);
					}
				}
			}
		}
		j = formats.length;
		for(var i=0;i<j;i++) {
			l = prices.length;
			for(var k=0;k<l;k++) {
				if(prices[k][1] == formats[i][0]) {
					if(prices[k][2] <= formats[i][1]) {
						formats[i][2] = prices[k][0];
						formats[i][3] = prices[k][2];
					}
					if(formats[i][2] && formats[i][3] && prices[k][2] <= formats[i][1] && prices[k][2] > formats[i][3]) {
						formats[i][2] = prices[k][0];
						formats[i][3] = prices[k][2];
					}
				}
			}			
		}
		// alert(formats);
		// W tym momencie: format[n] = new Array(format_id, quantity, price, min_quantity);
		// zmienic
		count = odids.length;
		for(var i=0;i<count;i++) {
			var price = 0;
			var format = parseInt(eval('document.orderDetails.format'+odids[i]+'.value'))
			for(var k=0;k<j;k++) {
				if(formats[k][0] == format) {
					price = formats[k][2];
				}
			}
			document.getElementById('price'+odids[i]).innerHTML = Math.ceil(parseInt(eval('document.orderDetails.quantity'+odids[i]+'.value')) * price * 100)/100;
		}
	}
	function checkFTPlogin() {
		var e = document.getElementById('ftp_login');
		if(e.value.length == 0) {
			$('#loginStatus').css('background-image','url(img/silk/information.png)');
			return false
		}
		if(e.value.length > 16) {
			alert('Login nie może być dłuższy aniżeli 16 znaków');
			$('#loginStatus').css('background-image','url(img/silk/delete.png)');
			return false;
		}
		$.post("/sprawdz-login-ftp.php", { login : e.value },
		  function(data){
			data = parseInt(data);
			if(data == 1) {
				$('#loginStatus').css('background-image','url(img/silk/accept.png)');
				$('#loginStatus').css('text-decoration','none');
				$('#loginStatus').html('&nbsp;');
				$('#loginProposals').hide();
				return true;
			}
			if(data == 0) {
				$('#loginStatus').css('background-image','url(img/silk/delete.png)');
				alert('Wybrany przez Ciebie login jest zajęty.');
				$.post("/propozycje-loginow.php", { login : e.value },
					function(data){ 
						var proposals = data.split(';');
						if(proposals.length > 0) {
							$('#loginProposals').show();
							$('#loginProposal').empty();
							for(var i=0;i<proposals.length;i++) {
								$('#loginProposal').append('<li><a href="#">'+proposals[i]+'</a></li>');
							}
							$('#loginProposal li a').each(function() {
								$(this).click(function() {
									e.value = $(this).text();
									checkFTPlogin(e);
									return false;
								});
								});
						}
					});
				e.focus();
				e.value = '';
				return false;
			}
			if(data == -1) {
				alert(data);
				$('#loginStatus').css('background-image','url(img/silk/delete.png)');
				alert('Musisz podać login, nie dłuższy niż 16 znaków');
				return false;
			}
		});
	}
	function checkPriceListCode() {
		var e = document.getElementById('kodcennika');
		if(e.value.length > 0) {
			$.post("/sprawdz-kod-promocji.php", { priceListCode : e.value },
			  function(data){
				if(data == 0) {
					$('#priceListStatus').css('background-image','url(img/silk/delete.png)');
					alert('Nie ma takiego kodu promocji.');
				}
				else {
					var d = data.split(';');
					var papier = d[0];
					var kod = d[1];
					if(d[0]) {
						$('#priceListStatus').css('background-image','url(img/silk/accept.png)');
						$('#priceListStatus').html('');
						if(document.getElementById('loginStatus')) {
							document.getElementById('loginStatus').onfocus = blur();
							$('#priceListStatus').append('<p>Przypisany został papier: '+papier+' i cennik o kodzie promocji: '+kod+'</p>');
							$('#paper_selection').hide();
						}
						
						
					}
				}
			});
		}
		return false;
	}
	function mask(e) {
		e.value = e.value.replace(/([^a-zA-Z0-9_-])/gi, "");
	}
	function setFrame(n) {
		document.upload.frame.options[n].selected=true;
		var e2 = document.getElementById('frame'+n);
		var e3;
		e2.src = e2.src.replace('inactive','active');
		for(var i=0;i<3;i++) {
			if(i != n) {
				e3 = document.getElementById('frame'+i);
				e3.src = e3.src.replace('_active','_inactive');
			}
		}
	}
	function setRedirectUrl() {
		var Flash;
		var normal = 'zamow-odbitki.php?u=1';
		var multiformat = 'zamow-odbitki.php?u=1&m=1';
		if(document.embeds && document.embeds.length>=1 && navigator.userAgent.indexOf("Safari") == -1)
			Flash = document.getElementById("EmbedFlashFilesUpload");
		else
			Flash = document.getElementById("FlashFilesUpload");
		if(document.getElementById('multiformat_view')) {
			if(document.getElementById('multiformat_view').checked == false) { //modyfikacja dzialania multiformat kuba 25.08.09
				Flash.SetVariable("redirectUploadUrl", multiformat);
				$.cookie('multiformat', '1');
			}
			else {
				Flash.SetVariable("redirectUploadUrl", normal);
				$.cookie('multiformat', '0');
			}
		}
	}
	function countPhotos() {
		var c = 0;
		$('.ilosc').each(function() {
			if($(this).attr('name') != 'quantity') {
				c += parseInt($(this).val());
			}
		});
		return c;
	}
//]]>