

// isInt get from:
// http://www.peterbe.com/plog/isint-function

function isInt(x) {
   var y=parseInt(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toString();
 } 
 
function isPlusInt(x) {
   return (isInt(x) && x> 0)
}

function viewItems() {
	$("tr[name^='produkt']").remove();
	var orderList ="";
	
	if (orderItems.length == 0) return nolines;

	
	for (var i=0;i<orderItems.length;i++) {
		var line = newline;
		line = strReplace(line, "%d", i+1);
		line = strReplace(line, "%e", i);
		line = strReplace(line, "%s", orderItems[i]);
		line = strReplace(line, "%q", orderQuant[i]);
		orderList += line;
		}

	return orderList;
}

function exportOrder() {
  	
	if (orderItems.length == 0) return false;
	
  var orderNames = orderNames = orderItems.join(';');
	var orderQuanity =orderQuanity = orderQuant.join(';');

  $('#orderNames').val(orderNames);
  $('#orderQuanity').val(orderQuanity);

	return true;
}

function delItem(index) {
	orderItems.splice(index,1);
	orderQuant.splice(index,1); 
	$('#order').append(viewItems());
	return false;
}

function changeQuant (index) {
	var quanity = window.prompt("Podaj nową ilość",orderQuant[index]);
	if (quanity==null) return false;
	if (!isPlusInt(quanity)) {alert("Podano nieprawidłową ilość"); return false;};
	orderQuant[index] = quanity;
	$('#order').append(viewItems());
	return false;
}

function clearList() {
	if (confirm('Czy na pewno chcesz usunąć dane z formularza?')) {
		orderItems = new Array();
		orderQuant = new Array();
		$('#order').append(viewItems());

	}
	return false;
}

$(function(){ 

	
	$('#addItem').click(function(){
		var text = $('#text').val();
		var quanity = $('#quanity').val();
		if (text == "") {
			alert('Nazwa towaru nie została wprowadzona');
			$('#text').focus();
			return 0;
		};
		if (!isPlusInt(quanity)) {
			alert('Podano nieprawidłową ilość');
			$('#quanity').focus();
			return 0;
		};    
    
    var regex = /["\;\>\<]/;
    if (regex.test(text)) {
      alert('Znaki \"  \;  \<  \> \n nie sa dozwolone w nazwie produktu');
      return 0;
    }
    
		orderItems.push(text);
		orderQuant.push(quanity);

		$('#emptyline').remove();
		$('#order').append(viewItems());
		
		$('#quanity').val('');
		$('#text').val(''); 
		$('#text').focus();
		return false;
	})
	
	$('#sendForm').submit(function(){
  
	
	  if ($('#fname').val()=="") {alert ('Nazwa firmy nie została wprowadzona'); $('#fname').focus(); return false;}
	  if ($('#fnip').val()=="") {alert ('NIP nie został wprowadzony'); $('#fnip').focus(); return false;}
    if (!validateNip($('#fnip').val())) {alert ('Nieprawidłowy numer NIP'); $('#fnip').focus(); return false;}
    if ($('#fstreet').val()=="") {alert ('Ulica nie została wprowadzona'); $('#fstreet').focus(); return false;}
    if ($('#fcode').val()=="") {alert ('Kod pocztowy nie został wprowadzony'); $('#fcode').focus(); return false;}
    if (!validatePostalCodePl($('#fcode').val())) {alert ('Nieprawidlowy kod pocztowy'); $('#fcode').focus(); return false;}
    if ($('#fcity').val()=="") {alert ('Miasto nie zostało wprowadzone'); $('#fcity').focus(); return false;}
    
    if ($('#acode').val()!="" && !validatePostalCodePl($('#acode').val())) {alert ('Nieprawidlowy kod pocztowy'); $('#acode').focus(); return false;}
    
    if ($('#namec').val()=="") {alert ('Imie i nazwisko nie zostały wprowadzone'); $('#namec').focus(); return false;}
    if ($('#phone').val()=="") {alert ('Numer telefonu nie został wprowadzony'); $('#phone').focus(); return false;}
    if (!validatePhoneNumber($('#phone').val())) {alert ('Nieprawidlowy numer telefonu'); $('#phone').focus(); return false;}
    
    if ($('#mail').val()=="") {alert ('Adres e-mail nie został wprowadzony'); $('#mail').focus(); return false;}
    if (!validateEmail($('#mail').val())) {alert ('Nieprawidłowy adres e-mail'); $('#mail').focus(); return false;}
    
    if (!($("input[name='payment']:checked").val())) alert('Nie wybrano formy płatnosc');
	  if (!($("input[name='shipment']:checked").val())) alert('Nie wybrano sposobu dostawy');
	  
	  if (!exportOrder()) {alert('Brak produktów w zamówieniu'); $('text').focus(); return false};  
	return true;  
  })
  
 
	
})

