
// Deklaracja zmiennych.
var Wartosc_Za_Wysylke = -1;
var Forma_Za_Wysylke = 1;
var Wartosc_Ilosc_Sztuk = 1;


// Zmiana wartości "Za wysyłkę"
function Za_Wysylke ( Wartosc, Forma ) {
	
	Wartosc_Za_Wysylke = Wartosc;
	Forma_Za_Wysylke = Forma;
	document.getElementById ( 'wartosc_wysylki' ).innerHTML = '' + Wartosc + ' zł';

}
// Zmiana ilości sztuk
function Ilosc_Sztuk ( Wartosc ) {
	
	Wartosc_Ilosc_Sztuk = Wartosc;
	cena_wrap = document.getElementById ( 'nasza_cena' );
	

	if ( cena_wrap.innerHTML.indexOf ( '*', cena_wrap ) >=0 )
		cena_wrap.innerHTML = Wartosc > 1 ? cena_wrap.innerHTML.replace( /([0-9]+)\*/i, Wartosc + '*' ) : cena_wrap.innerHTML.replace( /([0-9]+)\*/i, '' );
	else
		cena_wrap.innerHTML = Wartosc + '*' + cena_wrap.innerHTML;

}

// Przelicz kwotę
function Przekalkuluj_Kwote ( ) {
	
	cena_wrap = cena_wrap = document.getElementById ( 'nasza_cena' );
	// Pobierz cenę produktu:
	var CenaProduktu = document.getElementById('nasza_cena').innerHTML;
	if ( cena_wrap.innerHTML.indexOf ( '*', cena_wrap ) >=0 )
		CenaProduktu = CenaProduktu.substr ( 1+ cena_wrap.innerHTML.indexOf ( '*', cena_wrap ) );
	CenaProduktu = parseInt(CenaProduktu.replace(' zł', ''));

	// Oblicz cenę wyjściową jako Sztuk*Cena + Wysylka
	if ( Wartosc_Za_Wysylke == -1 ) Wartosc_Za_Wysylke = parseInt(document.getElementById ('wartosc_wysylki').innerHTML.replace(' zł', ''));
	WartoscZamowienia = Wartosc_Ilosc_Sztuk * CenaProduktu + Wartosc_Za_Wysylke;

	if ( WartoscZamowienia > 500 ) 
		Wylacz_Pobranie ();
	else
		Wlacz_Pobranie();

	if ( WartoscZamowienia < 100 )
		Wylacz_ERaty ();
	else
		Wlacz_ERaty ();

	document.getElementById ( 'wartosc_sumaryczna' ).innerHTML = '' + WartoscZamowienia + ' zł';
}

// Zapisz informacje o preferencjach przed zalogowaniem
function Zamowienie_Logowanie ( ) {
	
	// Sformatuj url zwrotny.
	BackUrl = document.getElementById('zamowienie_logowanie_backurl');
	BackUrl.value = BackUrl.value.replace('%Sztuk%', Wartosc_Ilosc_Sztuk).replace('%Forma%', Forma_Za_Wysylke);

	// Wyślij formularz.
	document.getElementById('zamowienie_logowanie_form').submit();

}

function Wylacz_Pobranie () {
	label = document.getElementById('forma_wysylki_1_label');
	input = document.getElementById('forma_wysylki_1');

	input.disabled = true;
	label.className = 'disabled';

	if (input.checked)
		document.getElementById('forma_wysylki_2').checked = true;
	
}

function Wlacz_Pobranie () {
	label = document.getElementById('forma_wysylki_1_label');
	input = document.getElementById('forma_wysylki_1');

	input.disabled = false;
	label.className = '';

}

function Wylacz_ERaty () {
	label = document.getElementById('forma_wysylki_4_label');
	input = document.getElementById('forma_wysylki_4');

	input.disabled = true;
	label.className = 'disabled';

	if (input.checked)
		document.getElementById('forma_wysylki_2').checked = true;	
}

function Wlacz_ERaty () {
	label = document.getElementById('forma_wysylki_4_label');
	input = document.getElementById('forma_wysylki_4');

	input.disabled = false;
	label.className = '';

}


function Pokaz_Liste_Adresow ( ) {
	
	document.getElementById ( 'wybrany' ).parentNode.getElementsByTagName('h3')[0].innerHTML = 'Wybierz adres:';
	document.getElementById ( 'lista_wysylek' ).style.display = '';
	document.getElementById ( 'wybrany' ).style.display = 'none';

}


function Zmien_Adres ( WysylkaId ) {
	
	advAJAX.get({
		url : '/xml/pobierz_adres?adres_id=' + WysylkaId,
		onSuccess : function(obj) {
				

			if ( window.ActiveXObject ) {
				var doc = new ActiveXObject ( 'Microsoft.XMLDOM' );
				doc.async = "false";
				doc.loadXML ( obj.responseText );
			} else {
				var parser = new DOMParser ( );
				var doc = parser.parseFromString ( obj.responseText, 'text/xml' );
			}
			
			
			Id = getXml(doc, 'Id' );

			Imie = getXml(doc, 'Imie' );
			Nazwisko = getXml(doc, 'Nazwisko' );
			
			Ulica = getXml(doc, 'Ulica' );
			NrLokalu = getXml(doc, 'NrLokalu' );
			
			KodPocztowy = getXml(doc, 'KodPocztowy' );
			Miasto = getXml(doc, 'Miasto' );


			Output = Imie + ' ' + Nazwisko + '<br />' + Ulica + ' ' + NrLokalu + '<br />' + KodPocztowy + ' ' + Miasto;
			document.getElementById ( 'wybrany' ).innerHTML = Output;
			document.getElementById ( 'wybrany' ).parentNode.getElementsByTagName('h3')[0].innerHTML = 'Adres wysyłki:';
			
			document.getElementById ( 'lista_wysylek' ).style.display = 'none';
			document.getElementById ( 'wybrany' ).style.display = '';
			document.getElementById ( 'wysylka_id' ).value = Id;
			
		}
	});


	}


	function getXml ( xml, tag ) {
		
		try {
			return xml.getElementsByTagName ( tag )[0].childNodes[0].nodeValue;
		} catch ( e ) {
			return '';
		}

	}

