function checkValue(it){
	if(isNaN(parseInt(it.value))) it.value = "";
	else it.value = parseInt(it.value);
};

function tylko_cyfry(input){
	input.value = input.value.replace(/[^0-9]/g,'');
	};

function changeValue(it, row){
	ilosc = it.value;
	ilosc = parseInt(ilosc);
	if(isNaN(ilosc)) ilosc = 0;
	it.value = ilosc;
	
	var do_zaplaty = $('wartosc_zamowienia').innerHTML;
	do_zaplaty = do_zaplaty.replace(/[^0-9,]/g,'');
	do_zaplaty = do_zaplaty.replace(/,/g,'.');
	do_zaplaty = parseFloat(do_zaplaty);
	if(isNaN(do_zaplaty)) do_zaplaty = 0;		
	
	var cena 	= row.getElementsByTagName('td')[4].innerHTML;
	cena = cena.replace(/[^0-9,]/g,'');
	cena = cena.replace(/,/g,'.');
	cena = parseFloat(cena);
	if(isNaN(cena) || cena == 0) return false;
	
	var wartosc_old	= row.getElementsByTagName('td')[6].innerHTML;
	wartosc_old = wartosc_old.replace(/[^0-9,]/g,'');
	wartosc_old = wartosc_old.replace(/,/g,'.');		
	wartosc_old = parseFloat(wartosc_old);
	if(isNaN(wartosc_old) || wartosc_old == 0) return false; 
	
	//alert(cena+"\n\n"+wartosc_old);
	
	wartosc_new = eval(cena*ilosc);
	row.getElementsByTagName('td')[6].innerHTML = number_format (wartosc_new, '2', ',', ' ')+'&nbsp;zł';
	
	do_zaplaty = eval(do_zaplaty +(wartosc_new-wartosc_old));
	$('wartosc_zamowienia').innerHTML = number_format (do_zaplaty, '2', ',', ' ')+'&nbsp;zł';
	
};

function SaveOrder(){
	$('OrderForm').submit();
};

function SendOrder(){
	$('FormOrderMsg').innerHTML = "";
	
	var PARADISO_ORDER = parseFloat($('order_tpl_cost').value);
	if(isNaN(PARADISO_ORDER)) PARADISO_ORDER = 0;
	
	if( PARADISO_MIN_ORDER > PARADISO_ORDER ){
		$('FormOrderMsg').innerHTML = '<b>Wartość zamówienia jest mniejsza od <u>'+PARADISO_MIN_ORDER+'</u>&nbsp;zł</b>';
		return false;
	}
	
	advAJAX.submit(document.getElementById("OrderForm"),{
		url: "orders/save_order.php",
	    onInitialization : function(obj) {
			 $('FormOrderMsg').innerHTML = "<img style='vertical-align: middle;' src='./img/ajax-loader.gif' alt='Loading' /> <b>Wysyłam Twoje zamówienie</b>";
			 },
	    onSuccess : function(obj) {
    		var stan = obj.responseXML.getElementsByTagName("response")[0].childNodes[0].nodeValue;

			stan = parseInt(stan);
			//alert(stan);
			if(stan > 0){
					if($('order_tpl_type') && $('order_tpl_type').value == 'wregister'){ // zamowienie bez rejestracji
						$('FormOrderMsg').innerHTML = "<b>Zamówienie zostało przyjęte.</b><br/><b>Zadzwonimy by potwierdzić zamówienie na podany numer telefonu.</b><br/> Na podany adres email zostało wysłane potwierdzenie";						
					}else{
						$('FormOrderMsg').innerHTML = "<b>Zamówienie zostało przyjęte.</b><br/>Na Twój adres email zostało wysłane potwierdzenie";
					}
					$('order_btn_1').style.display = $('order_btn_2').style.display = $('order_btn_3').style.display = "none";
					$('order_btn_close').style.display = "";	
			 }
			else{
				//alert(stan);
				if(stan == -6) $('FormOrderMsg').innerHTML = "<b>Zamówienie zostało przyjęte</b>, wystąpił błąd pod czas wysłania e-mail`a potweirdzającego";
				else $('FormOrderMsg').innerHTML = "<b>Zamówienie nie zostało przyjęte.</b><br/> Proszę uzupełnić brakujące dane";
			}
	
	    },
	    onError : function(obj) { $('FormOrderMsg').innerHTML = "<b>Wystąpił błąd w komunikacji.</b><br/> Proszę spróbować jeszcze raz"; },
	    onFinalization : function() { $('FormOrderMsg').style.display ="block"; }
	});		
};

function ShowForm(form){
	$(form).style.display = "block";
};

function Register(){

	$('FormsMsg').innerHTML = "";
	$('FormsMsg').innerHTML = "<img style='vertical-align: middle;' src='./img/ajax-loader.gif' alt='Loading' /> <b>Rejestruję Cię w pizzeri Paradiso</b>";
	
	check 	= true;
	msg		= '';

	imie	=	$('register_tpl_imie').value	= $('register_tpl_imie').value.trim();
	if( ! imie.length ){
		$('FormsMsg').innerHTML = "Brak imienia";
		$('register_tpl_imie').style.border = '1px solid #FF0000';
		$('register_tpl_imie').focus();	
		return false;	
	}
	else $('register_tpl_imie').style.border = '';

	nazwisko	=	$('register_tpl_nazwisko').value	= $('register_tpl_nazwisko').value.trim();
	if( ! nazwisko.length ){
		$('FormsMsg').innerHTML = "Brak nazwiska";
		$('register_tpl_nazwisko').style.border = '1px solid #FF0000';
		$('register_tpl_nazwisko').focus();		
		return false;	
	}
	else $('register_tpl_nazwisko').style.border = '';

	telefon	=	$('register_tpl_telefon').value	= $('register_tpl_telefon').value.replace(/[^0-9]/g,'');
	var objRegExp  = /^0[0-9]{9}$/;
	if( !telefon.length || !objRegExp.test(telefon)){
		$('FormsMsg').innerHTML = "Błąd nr telefonu. Zero na początku";
		$('register_tpl_telefon').style.border = '1px solid #FF0000';
		$('register_tpl_telefon').focus();		
		return false;	
	}
	else $('register_tpl_telefon').style.border = '';
	
	login	=	$('register_tpl_login').value	= $('register_tpl_login').value.trim();
	if( ! login.length ){
		$('FormsMsg').innerHTML = "Brak loginu";
		$('register_tpl_login').style.border = '1px solid #FF0000';	
		$('register_tpl_login').focus();	
		return false;	
	}
	else $('register_tpl_login').style.border = '';
	
	pass	=	$('register_tpl_pass').value	= $('register_tpl_pass').value.trim();
	pass2	=	$('register_tpl_pass2').value	= $('register_tpl_pass2').value.trim();
	if( ! pass.length ||  ! pass2.length || pass!=pass2){
		$('FormsMsg').innerHTML = "Brak hasła lub hasła różnią się";
		$('register_tpl_pass').style.border = '1px solid #FF0000';	
		$('register_tpl_pass').focus();	
		return false;	
	}
	else $('register_tpl_pass').style.border = '';
	
	email	=	$('register_tpl_email').value	= $('register_tpl_email').value.trim();
	if( ! email.length || ! email.isEmailAddress){
		$('FormsMsg').innerHTML = "Brak lub błędny adres email";
		$('register_tpl_email').style.border = '1px solid #FF0000';	
		$('register_tpl_email').focus();	
		return false;	
	}
	else $('register_tpl_email').style.border = '';

	
	adres	=	$('register_tpl_adres').value	= $('register_tpl_adres').value.trim();
	if( ! adres.length ){
		$('FormsMsg').innerHTML = "Brak adresu";
		$('register_tpl_adres').style.border = '1px solid #FF0000';	
		$('register_tpl_adres').focus();	
		return false;	
	}
	else $('register_tpl_adres').style.border = '';

	miasto	=	$('register_tpl_miasto').value	= $('register_tpl_miasto').value.trim();
	if( ! adres.length ){
		$('FormsMsg').innerHTML = "Brak adresu";
		$('register_tpl_miasto').style.border = '1px solid #FF0000';	
		$('register_tpl_miasto').focus();	
		return false;	
	}
	else $('register_tpl_miasto').style.border = '';

	if( $('register_tpl_zgoda').checked != true ){
		$('FormsMsg').innerHTML = "Brak zgody na przetwarzanie danych";
		$('register_tpl_zgoda').style.backgroundColor = '#FF0000';	
		$('register_tpl_zgoda').focus();	
		return false;	
	}
	else $('register_tpl_zgoda').style.backgroundColor = '';	
	/*
	if( $('register_tpl_regulamin').checked != true ){
		$('FormsMsg').innerHTML = "Brak akceptacji regulaminu";
		$('register_tpl_regulamin').style.backgroundColor = '#FF0000';	
		$('register_tpl_regulamin').focus();	
		return false;	
	}
	else $('register_tpl_regulamin').style.backgroundColor = '';
	*/
	advAJAX.submit(document.getElementById("FormRegister"),{
		url: "orders/register.php",
	    onInitialization : function() { /* Ukrycie warstwy */
	    },
	    onSuccess : function(obj) {
    		var stan = obj.responseXML.getElementsByTagName("response")[0].childNodes[0].nodeValue;

			stan = parseInt(stan);
			
			if(stan > 0){
					$('FormsMsg').innerHTML = "Rejestracja poprawna. Na podany adres email został wysłany list celem aktywacji konta";	
			 }
			else{
				if(stan == -1) $('FormsMsg').innerHTML = "Proszę uzupełnić wymagane pola";
				if(stan == -2) $('FormsMsg').innerHTML = "Podane hasła są niepoprawne";
				if(stan == -3) $('FormsMsg').innerHTML = "Adres email jest niepoprawny";
				if(stan == -4) $('FormsMsg').innerHTML = "Brak zgód";
				if(stan == -5) $('FormsMsg').innerHTML = "Login lub adres email jest już zajęty";
				if(stan == -6) $('FormsMsg').innerHTML = "Konto zostało założone, lecz wystąpił błąd pod czas wysłania e-mail`a celem aktywacji";
			}
	
	    },
	    onError : function(obj) { },
	    onFinalization : function() { }
	});		
	
};

function ChangePass(){

	$('FormsMsg').innerHTML = "";
	$('FormsMsg').innerHTML = "<img style='vertical-align: middle;' src='./img/ajax-loader.gif' alt='Loading' /> <b>Zmieniam hasło</b>";
	
	check 	= true;
	msg		= '';

	
	pass0	=	$('register_tpl_pass0').value	= $('register_tpl_pass0').value.trim();
	if( ! pass0.length ){
		$('FormsMsg').innerHTML = "Brak aktualnego hasła lub proponowane hasło jest takei same jak aktualne";
		$('register_tpl_pass0').style.border = '1px solid #FF0000';	
		$('register_tpl_pass0').focus();	
		return false;	
	}
	else{
		$('register_tpl_pass0').style.border = '';
	}
	
	pass1	=	$('register_tpl_pass1').value	= $('register_tpl_pass1').value.trim();
	pass2	=	$('register_tpl_pass2').value	= $('register_tpl_pass2').value.trim();
	if( ! pass1.length ||  ! pass2.length || pass1!=pass2){
		$('FormsMsg').innerHTML = "Brak hasła lub hasła różnią się";
		$('register_tpl_pass1').style.border = '1px solid #FF0000';	
		$('register_tpl_pass2').style.border = '1px solid #FF0000';
		$('register_tpl_pass1').focus();	
		return false;	
	}
	else{
		$('register_tpl_pass1').style.border = '';
		$('register_tpl_pass2').style.border = '';
	}

	if( pass0 == pass1 || pass0 == pass2	){
		$('FormsMsg').innerHTML = "Nowe hasło musi być inne od obecnego.";
		$('register_tpl_pass1').style.border = '1px solid #FF0000';	
		$('register_tpl_pass2').style.border = '1px solid #FF0000';
		$('register_tpl_pass1').focus();	
		return false;	
	}
	else{
		$('register_tpl_pass1').style.border = '';
		$('register_tpl_pass2').style.border = '';
	}
 
 	pass0 = hex_md5(pass0);
 	pass1 = hex_md5(pass1);
 	pass2 = hex_md5(pass2);
 
	advAJAX.post({
		url: "orders/register.php",
	    parameters : {
	      "register_tpl_act" : "new_pass",	
	      "register_tpl_pass0" : pass0,
	      "register_tpl_pass1" : pass1,
	      "register_tpl_pass2" : pass2
	      
	    },		
	    onInitialization : function() { /* Ukrycie warstwy */
	    },
	    onSuccess : function(obj) {
    		var stan = obj.responseXML.getElementsByTagName("response")[0].childNodes[0].nodeValue;

			stan = parseInt(stan);
			
			if(stan > 0){
					$('FormsMsg').innerHTML = "Hasło zostało zmienione.";	
			 }
			else{
				$('FormsMsg').innerHTML = "Hasło <u>nie</u> zostało zmienione. Proszę wpisać poprawnie obecne hasło.";
			}
	
	    },
	    onError : function(obj) { },
	    onFinalization : function() { }
	});		
	
};

function SaveProfil(){

	$('FormsMsg').innerHTML = "";
	$('FormsMsg').innerHTML = "<img style='vertical-align: middle;' src='./img/ajax-loader.gif' alt='Loading' /> <b>Aktualizacja danych w pizzeri Paradiso</b>";
	
	check 	= true;
	msg		= '';

	imie	=	$('register_tpl_imie').value	= $('register_tpl_imie').value.trim();
	if( ! imie.length ){
		$('FormsMsg').innerHTML = "Brak imienia";
		$('register_tpl_imie').style.border = '1px solid #FF0000';
		$('register_tpl_imie').focus();	
		return false;	
	}
	else $('register_tpl_imie').style.border = '';

	nazwisko	=	$('register_tpl_nazwisko').value	= $('register_tpl_nazwisko').value.trim();
	if( ! nazwisko.length ){
		$('FormsMsg').innerHTML = "Brak nazwiska";
		$('register_tpl_nazwisko').style.border = '1px solid #FF0000';
		$('register_tpl_nazwisko').focus();		
		return false;	
	}
	else $('register_tpl_nazwisko').style.border = '';

	telefon	=	$('register_tpl_telefon').value	= $('register_tpl_telefon').value.replace(/[^0-9]/g,'');
	var objRegExp  = /^0[0-9]{9}$/;
	if( ! telefon.length ||  !objRegExp.test(telefon)){
		$('FormsMsg').innerHTML = "Błąd nr telefonu. Zero na początku";
		$('register_tpl_telefon').style.border = '1px solid #FF0000';
		$('register_tpl_telefon').focus();		
		return false;	
	}
	else $('register_tpl_telefon').style.border = '';
	

	email	=	$('register_tpl_email').value	= $('register_tpl_email').value.trim();
	if( ! email.length || ! email.isEmailAddress){
		$('FormsMsg').innerHTML = "Brak lub błędny adres email";
		$('register_tpl_email').style.border = '1px solid #FF0000';	
		$('register_tpl_email').focus();	
		return false;	
	}
	else $('register_tpl_email').style.border = '';

	
	adres	=	$('register_tpl_adres').value	= $('register_tpl_adres').value.trim();
	if( ! adres.length ){
		$('FormsMsg').innerHTML = "Brak adresu";
		$('register_tpl_adres').style.border = '1px solid #FF0000';	
		$('register_tpl_adres').focus();	
		return false;	
	}
	else $('register_tpl_adres').style.border = '';

	miasto	=	$('register_tpl_miasto').value	= $('register_tpl_miasto').value.trim();
	if( ! adres.length ){
		$('FormsMsg').innerHTML = "Brak adresu";
		$('register_tpl_miasto').style.border = '1px solid #FF0000';	
		$('register_tpl_miasto').focus();	
		return false;	
	}
	else $('register_tpl_miasto').style.border = '';

	advAJAX.submit(document.getElementById("FormProfilDane"),{
		url: "orders/register.php",
	    onInitialization : function() { /* Ukrycie warstwy */
	    },
	    onSuccess : function(obj) {
    		var stan = obj.responseXML.getElementsByTagName("response")[0].childNodes[0].nodeValue;

			stan = parseInt(stan);
			
			if(stan > 0){
					if(stan == 1) $('FormsMsg').innerHTML = "Aktualizacja poprawna";	
					if(stan == 6) $('FormsMsg').innerHTML = "Aktualizacja poprawna.<br/> Na podany adres email został wysłany list celem aktywacji konta po zmianie adresu email";
					
			 }
			else{
				if(stan == -6) $('FormsMsg').innerHTML = "Aktualizacja poprawna, lecz wystąpił błąd pod czas wysłania e-mail`a celem aktywacji konta po zmianie adresu email";
				else $('FormsMsg').innerHTML = "Aktualizacja <u>niepoprawna</u>";
			}
	
	    },
	    onError : function(obj) { },
	    onFinalization : function() { }
	});		
	
};

function SaveOrderWithoutRegister(){
	$('FormsMsg').innerHTML = "";
	//alert("SaveOrderWithoutRegister");
	check 	= true;
	msg		= '';

	imie	=	$('order_tpl_imie').value	= $('order_tpl_imie').value.trim();
	if( ! imie.length ){
		$('FormsMsg').innerHTML = "Brak imienia";
		$('order_tpl_imie').style.border = '1px solid #FF0000';
		$('order_tpl_imie').focus();	
		return false;	
	}
	else $('order_tpl_imie').style.border = '';

	nazwisko	=	$('order_tpl_nazwisko').value	= $('order_tpl_nazwisko').value.trim();
	if( ! nazwisko.length ){
		$('FormsMsg').innerHTML = "Brak nazwiska";
		$('order_tpl_nazwisko').style.border = '1px solid #FF0000';
		$('order_tpl_nazwisko').focus();		
		return false;	
	}
	else $('order_tpl_nazwisko').style.border = '';
	
	telefon	=	$('order_tpl_telefon').value	= $('order_tpl_telefon').value.replace(/[^0-9]/g,'');
	var objRegExp  = /^085[0-9]{7}$/;
	if( ! telefon.length || !objRegExp.test(telefon)){
		$('FormsMsg').innerHTML = "Proszę podać stacjonarny nr telefonu";
		$('order_tpl_telefon').style.border = '1px solid #FF0000';
		$('order_tpl_telefon').focus();		
		return false;	
	}
	else $('order_tpl_telefon').style.border = '';
	
	email	=	$('order_tpl_email').value	= $('order_tpl_email').value.trim();
	if( ! email.length || ! email.isEmailAddress){
		$('FormsMsg').innerHTML = "Brak lub błędny adres email";
		$('order_tpl_email').style.border = '1px solid #FF0000';	
		$('order_tpl_email').focus();	
		return false;	
	}
	else $('order_tpl_email').style.border = '';

	
	adres	=	$('order_tpl_adres').value	= $('order_tpl_adres').value.trim();
	if( ! adres.length ){
		$('FormsMsg').innerHTML = "Brak adresu";
		$('order_tpl_adres').style.border = '1px solid #FF0000';	
		$('order_tpl_adres').focus();	
		return false;	
	}
	else $('order_tpl_adres').style.border = '';

	miasto	=	$('order_tpl_miasto').value	= $('order_tpl_miasto').value.trim();
	if( ! adres.length ){
		$('FormsMsg').innerHTML = "Brak adresu";
		$('order_tpl_miasto').style.border = '1px solid #FF0000';	
		$('order_tpl_miasto').focus();	
		return false;	
	}
	else $('order_tpl_miasto').style.border = '';

	if( $('order_tpl_zgoda').checked != true ){
		$('FormsMsg').innerHTML = "Brak zgody na przetwarzanie danych";
		$('order_tpl_zgoda').style.backgroundColor = '#FF0000';	
		$('order_tpl_zgoda').focus();	
		return false;	
	}
	else $('order_tpl_zgoda').style.backgroundColor = '';	
	/*
	if( $('order_tpl_regulamin').checked != true ){
		$('FormsMsg').innerHTML = "Brak akceptacji regulaminu";
		$('order_tpl_regulamin').style.backgroundColor = '#FF0000';	
		$('order_tpl_regulamin').focus();	
		return false;	
	}
	else $('order_tpl_regulamin').style.backgroundColor = '';
	*/
	$('FormOrderWithoutRegister').submit();	
	
}

function Forgot(){
	if( $('FormsMsg')) var msg = $('FormsMsg');
	else var msg = $('FormLoginMsg'); 
	msg.innerHTML = ""
	
	if(msg.style.display == "none") msg.style.display = "block";
	
	check 	= true;

	forgot	=	$('register_tpl_forgot').value	= $('register_tpl_forgot').value.trim();
	if( ! forgot.length ){
		msg.innerHTML = "Brak wpisu";
		$('register_tpl_forgot').style.border = '1px solid #FF0000';
		$('register_tpl_forgot').focus();	
		return false;	
	}
	else $('register_tpl_forgot').style.border = '';


	forgot = hex_md5(forgot);

	advAJAX.post({
	    url: "orders/forgot.php",
	    parameters : {
	      "register_tpl_forgot" : forgot
	    },	    
	    onInitialization : function() {
	    	msg.innerHTML = "<img src='./img/ajax-loader.gif' alt='Loading' /> <b>Resetuję hasło, dla podanych danych</b>";
	    },
	    onSuccess : function(obj) {
	    	
    		var stan = obj.responseXML.getElementsByTagName("response")[0].childNodes[0].nodeValue;

			stan = parseInt(stan);
			
			if(stan == 1){
					msg.innerHTML = "<b>Hasło zostało zresetowane.</b> Na podany adres email zostało wysłane nowe hasło";	
			 }
			else{
				if(stan == -6) msg.innerHTML = "<b>Hasło zostało zresetowane</b>, lecz wystąpił błąd pod czas wysłania e-mail`a z nowym hasłem.<br />Prosimy spróbować jeszcze raz";
				else { msg.innerHTML = "Podane dane nie odpowiadają Naszym klientom."; }
			}	    	
	    	
	    },
	    onError : function(obj) { },
	    onFinalization : function() {  }
	});	
};

function Login(){

	if( $('FormsMsg')) var msg = $('FormsMsg');
	else var msg = $('FormLoginMsg'); 
	msg.innerHTML = ""
	
	if(msg.style.display == "none") msg.style.display = "block";
	
	check 	= true;

	login	=	$('login_tpl_login').value	= $('login_tpl_login').value.trim();
	if( ! login.length ){
		msg.innerHTML = "Brak loginu";
		$('login_tpl_login').style.border = '1px solid #FF0000';
		$('login_tpl_login').focus();	
		return false;	
	}
	else $('login_tpl_login').style.border = '';


	pass	=	$('login_tpl_pass').value	= $('login_tpl_pass').value.trim();
	if( ! pass.length ){
		msg.innerHTML = "Brak hasla";
		$('login_tpl_pass').style.border = '1px solid #FF0000';
		$('login_tpl_pass').focus();		
		return false;	
	}
	else $('login_tpl_pass').style.border = '';


	$('login_tpl_pass').value = hex_md5(pass);

	advAJAX.submit(document.getElementById("FormLogin"),{
		url: "orders/login.php",
	    onInitialization : function() {
			msg.innerHTML = "<img src='./img/ajax-loader.gif' alt='Loading' /> <b>Sprawdzam podane dane</b>";
	    },
	    onSuccess : function(obj) {
    		var incoming = obj.responseXML.getElementsByTagName("response")[0].childNodes[0].nodeValue;
			incoming = incoming.split("|#|");
			stan = parseInt(incoming[0]);
			if(isNaN(stan)) stan = 0;
			klient = incoming[1];
	    },
	    onError : function(obj) { },
	    onFinalization : function() {
			if(stan == 1){
					//msg.innerHTML = "Logowanie poprawne";
					$('klient_zalogowany').innerHTML = klient;
					document.getElementById("FormLogin").reset();
					document.getElementById("FormLogin").style.display = "none";
					//$('FormKlient').style.display = "block";
					 $('FormKlient').style.display 				= '';
					 msg.innerHTML = "";
					 msg.style.display 				= 'none';					
			 }
			else{
					if(stan == -1){
						msg.innerHTML = "Konto wymaga <u>aktywacji</u>";
						$('login_tpl_pass').value = '';
						$('login_tpl_login').focus();						
					}
					else{
					msg.innerHTML = "Logowanie nie poprawne";
					$('login_tpl_pass').value = '';
					$('login_tpl_login').focus();
					}		
				}	    	
		}
	});		
	
};	
