
function removeSpace(el) {
	var n = el.value.replace(/ /g,'');
	el.value = n;
}

// Sprawdza czy wartosć zawiera cyfrę
function checkValueContainDigit(value) {
	if (value.match(/[0-9]+/) == null) {
		alert('Sprawdź Model urządzenia');
		return false;
	}
}

//===================================================
// KALENDARZ
//===================================================

function pokaz_kalendarz(pole, formName) {
	  o = window.open("kalendarz.php?p="+pole+"&formName="+formName+"", "kalendarz", "width=400px, height=220px,"
   + " left=250,top=200,location=no,status=no,"
   + " directories=no, scrollbars=no, menubar=no, toolbar=no, resizable=no");
  o.focus();
   return false;
}

// zamienia przecinek na kropke
function replaceCommaToPoint(f) {
	var new_value = f.value.replace(',','.');
	if (new_value != '' && new_value.match(/^[0-9\.]+$/) == null) {
		alert('Wpisana wartość nie jest kwotą');
	}
	f.value = new_value;
}
    
function hiddenAddProductErr() {
	var catalogNumber = document.getElementById('catalogNumber');
	var productName   = document.getElementById('productName');
	if (catalogNumber.value != '' && productName.value != '') {  
		var err = document.getElementById('errorField');
		err.innerHTML = ''; 
		err.style.display='none'; 
	}
}

//========================================
// FUNKCJE WALIDACJI FORMULARZY start
//========================================

function checkAddEditOrderProduct(f) {
	
	if (f.modelVersion.value == '') {
		alert('Proszę wpisać model i wersję');
		return false;
	}
	if (f.catalogNumber.value == '') {
		alert('Proszę wpisać numer katalogowy');
		return false;
	}
	if (f.productName.value == '') {
		alert('Nazwa części nie została pobrana.\nWpisz ponownie numer katalogowy lub wpisz ręcznie nazwę części');
		return false;
	}
	
	if (!f.quantity.value.match(/^[0-9]+$/)) {
		alert('Nie podano ilości lub wpisano niepoprawną wartość.\nMożna wpisać tylko liczbę całkowitą.');
		return false;
	}
	
	if (f.repairCat.selectedIndex < 1) {
		alert('Proszę wybrać rodzaj naprawy');
		return false;
	}
	return true;
}

// formularz edycja faktury
function checkFieldsEditInvoice(f) {
	if (f.name.value == '' || f.city.value == '' || f.month.value == '' || f.invoiceDate.value == '' || f.invoiceNumber.value == '') {
		alert('Pola z gwiazdką są obowiązkowe');
		return false;
	}
}

// formularz dodaj fakture (rozliczenie)
function checkFieldsAddInvoice(f) {
	if (f.name.value == '' || f.city.value == '' || f.month.selectedIndex < 1) {
		alert('Pola z gwiazdką są obowiązkowe');
		return false;
	}
}

// formularz faktura - naprawa
function checkFormInvoiceRepair(f) {
	var kodCzesci   = document.getElementsByName('kodCzesci[]');
	var cenaCzesci  = document.getElementsByName('cenaCzesci[]');
	var fakturaCzesci = document.getElementsByName('fakturaCzesci[]');

	var kodCzesciNowe   = document.getElementsByName('kodCzesci_new[]');
	var cenaCzesciNowe  = document.getElementsByName('cenaCzesci_new[]');
	var fakturaCzesciNowe = document.getElementsByName('fakturaCzesci_new[]');
	
	for (i=0;i<kodCzesci.length;i++) {
		if ((kodCzesci[i].value != '' && (cenaCzesci[i].value == '' || fakturaCzesci[i].value == '') ) || 
		    (cenaCzesci[i].value != '' && (kodCzesci[i].value == '' || fakturaCzesci[i].value == '') ) ||
		    (fakturaCzesci[i].value != '' && (cenaCzesci[i].value == '' || kodCzesci[i].value == '') )) {
			alert('Wypełnij wszystkie pola dotyczące wybranej części');
			return false;
		}
	}
	
	for (i=0;i<kodCzesciNowe.length;i++) {
		if ((kodCzesciNowe[i].value != '' && (cenaCzesciNowe[i].value == '' || fakturaCzesciNowe[i].value == '') ) || 
		    (cenaCzesciNowe[i].value != '' && (kodCzesciNowe[i].value == '' || fakturaCzesciNowe[i].value == '') ) || 
		    (fakturaCzesciNowe[i].value != '' && (cenaCzesci[i].value == '' || kodCzesci[i].value == '') )) {
			alert('Wypełnij wszystkie pola dotyczące wybranej części');
			return false;
		}
	}
	if (f.katNaprawy.selectedIndex < 1) { 
		alert('Pole "Kategoria naprawy" jest niewypełnione'); 
		return false; 
	}
	if (f.katNaprawy.value != 3) {
		if (f.nrKartyGwar.value == '') {
			alert('Pole "Numer karty gwarancyjnej" jest niewypełnione');
			return false;
		}
	}
	if (f.nrFabryczny.value == '') {
		alert('Pole "Numer seryjny" jest niewypełnione');
		return false;
	}
  
	if (f.dataZakupu.value.match(/^[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}$/) == null) { 
		alert('Pole "Data zakupu" jest niewypełnione'); 
		return false; 
	}
	if (f.dataZgloszeniaUsterki.value.match(/^[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}$/)== null) { 
		alert('Pole "Data zgłoszenia usterki" jest niewypełnione'); 
		return false;
	}
	if (f.dataUsunUsterki.value.match(/^[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}$/)== null) { 
		alert('Pole "Data usunięcia usterki" jest niewypełnione'); 
		return false;
	}
	
	/*
	if (f.nrSeriiOdcinka.value=='') { 
		alert('Pole "Nr serii odcinka" jest niewypełnione'); 
		f.nrSeriiOdcinka.focus();
		return false; 
	}
	*/
	
		
	if (f.range.selectedIndex < 1) {
		alert('Wybierz grupę materiałową');
		return false;
	}
	/**	
	if (f.rangeGroup.selectedIndex < 1) {
		alert('Wybierz podgrupę materiałową');
		return false;
	}
	*/		
	if (f.nazwaUrzadzenia.value=='') { 
		alert('Wybierz "Model urządzenia"'); 
		f.nazwaUrzadzenia.focus();
		return false; 
	}
	/**
	if (f.nazwaUrzadzenia.value.match(/[0-9]+/) == null) {
		alert('Sprawdź model urządzenia');
		return false;
	}
	*/
	/*
	if (f.nrFabryczny.value=='') { 
		alert('Pole "Nr fabryczny" jest niewypełnione'); 
		f.nrFabryczny.focus();
		return false; 
	}
	*/
			
	if (f.typeIntervention.selectedIndex < 1) {
		alert('Wybierz kategorię usterki');
		return false;
	}
	if (f.interventionSubType.selectedIndex<1) {
		alert('Wybierz podkategorię usterki');
		return false;
	}
	if (f.faultType.selectedIndex < 1) {
		alert('Wybierz rodzaj usterki');
		return false;
	}
	
	  if (f.nazwisko.value=='') { 
       	alert('Pole "Nazwisko"  jest niewypełnione'); 
       	f.nazwisko.focus();
       	return false; 
    }
	if (f.imie.value=='') {
		alert('Pole "Imie" jest niewypełnione'); 
       	f.imie.focus();
		return false; 
	}
	/*
	if (f.telefon.value=='') {
		alert('Pole "Tel kontaktowy" jest niewypełnione'); 
       	f.telefon.focus();
		return false;
	}
	*/
	if (f.miejscowosc.value=='') {
		alert('Pole "Miejscowość" jest niewypełnione'); 
       	f.miejscowosc.focus();
		return false; 
	}
	if (f.kod.value.match(/^[0-9]{2,2}-[0-9]{3,3}$/)== null) {
		alert('Pole "Kod pocztowy" jest niewypełnione, lub wprowadzony kod jest w złym formacie.\nPrawidłowy format to xx-xxx'); 
       	f.kod.focus();
		return false; 
	}
	/**
	if (f.ulica.value=='') { 
		alert('Pole "Ulica" jest niewypełnione'); 
       	f.ulica.focus();
		return false; 
	}
	if (f.nrBudynku.value=='') {
		alert('Pole "Nr budynku" jest niewypełnione'); 
       	f.nrBudynku.focus();
		return false; 
	}
	*/

	if (isNaN(f.kosztNaprawy.value) || f.kosztNaprawy.value == '') { 
		alert('Pole "Robocizna" jest niewypełnione lub wpisana wartość nie jest kwotą'); 
		f.kosztNaprawy.focus();
		return false; 
	}
	/*
	if (isNaN(f.kosztZabudowy.value) || f.kosztZabudowy.value == '') { 
		alert('Pole "Koszt zabudowy" jest niewypełnione lub wpisana wartość nie jest kwotą'); 
		f.kosztZabudowy.focus();
		return false; 
	}
	*/
	//if (isNaN(f.dojazd.value) || f.dojazd.value == '') { 
	//	alert('Pole "Dojazd w 48h" jest niewypełnione lub wpisana wartość nie jest kwotą'); 
	//	f.dojazd.focus();
	//	return false; 
	//}
		
	//if (isNaN(f.kosztUslugiRazem.value) || f.kosztUslugiRazem.value == '') {
	//	alert('Pole "Łączny koszt usługi" jest niewypełnione lub wpisana wartość nie jest kwotą');
	//	f.kosztUslugiRazem.focus();
	//	return false;
	//}
			
	if (isNaN(f.stawkaZaKm.value) || f.stawkaZaKm.value == '') {
		alert('Pole "Urzędowa stawka za km" jest niewypełnione lub wpisana wartość nie jest kwotą');
		f.stawkaZaKm.focus();
		return false;
	}

	if (isNaN(f.iloscKm.value) || f.iloscKm.value == '') {
		alert('Pole "Ilość przejechanych km" jest niewypełnione lub wpisana wartość nie jest kwotą');
		f.iloscKm.focus();
		return false;
	}
	if (isNaN(f.cenaTransportu.value) || f.cenaTransportu.value == '') {
		alert('Pole "Cena transportu" jest niewypełnione lub wpisana wartość nie jest kwotą');
		f.cenaTransportu.focus();
		return false;
	}
}

function checkFormInvoiceRepairParts(f) {
	
	if (f.typeIntervention.selectedIndex < 1) {
		alert('Wybierz kategorię usterki');
		return false;
	}
	if (f.intervention.value == '') {
		alert('Wybierz uszkodzony podzespół');
		return false;
	}
	if (f.kodCzesci.value == '') {
		alert('Pole "Kod wymienionej części" jest niewypełnione');
		return false;
	}
	if (isNaN(f.cenaCzesci.value) || f.cenaCzesci.value == '') {
		alert('Pole "Cena części" jest niewypełnione lub wpisana wartość nie jest kwotą');
		f.cenaCzesci.focus();
		return false;
	}
}

function checkUserForm(f) {
	
	var x = document.getElementsByName('perm[]');
	
	if (f.username.value.length < 6) {
		alert ('Login musi mieć co najmniej 6 znaków!');
		return false;
	}
	if (f.passwd.value.length < 6) {
		alert ('Hasło musi mieć co najmniej 6 znaków!');
		return false;
	}
	
	if (f.passwd.value != f.passwd1.value) {
		alert ('Wpisane hasła nie są identyczne!');
		return false;
	}
	if (f.email.value != '') {
		if (!f.email.value.match(/^[0-9a-zA-Z_.-]+@[0-9a-zA-Z_-]+\.([a-zA-Z-]+\.)*[a-zA-Z]+$/ )) {
			alert('Adres e-mail jest nieprawidłowy!');
			return false;
		}
	}
	if (f.service.selectedIndex < 1) {
		alert ('Nie wybrano nazwy serwisu!');
		return false;
	}
	
	for (i=0;i<x.length;i++) {
		if (x.item(i).checked) {
			var permC = 1;
		}
	}
	if (permC != 1) {
		alert ('Zaznacz przynajmniej jedno uprawnienie dla użytkownika');
		return false;
	}
	
	return true;
	
}
// formularz dodawania /edycji serwisu
function checkServiceForm(f) {
	
	if (f.name.value == '') {
		alert ('Proszę wpisać nazwę serwisu!');
		return false;
	}
	if (f.city.value == '') {
		alert ('Proszę wpisać miasto!');
		return false;
	}
	
	if (f.email.value != '') {
		if (!f.email.value.match(/^[0-9a-zA-Z_.-]+@[0-9a-zA-Z_-]+\.([a-zA-Z-]+\.)*[a-zA-Z]+$/ )) {
			alert('Adres e-mail jest nieprawidłowy!');
			return false;
		}
	}
	
	return true;
	
}
// Sprawdza czy wszystkie pola są wypełnione
function checkFormAllFieldsRequired(formularz) {	
	var j=0
	for (var i=0; i<formularz.elements.length; i++) {
  		if (formularz.elements[i].type == 'text' && formularz.elements[i].value.length == 0){
  			alert('Pola z gwiazdką są obowiązkowe!');
  			return false;
  		}
  		if (formularz.elements[i].type == 'select-one' && formularz.elements[i].selectedIndex <1){
  			alert('Pola z gwiazdką są obowiązkowe!');
  			return false;
  		}
  		if (formularz.elements[i].type == 'textarea' && formularz.elements[i].value.length == 0){
  			alert('Pola z gwiazdką są obowiązkowe!');
  			return false;
  		}
	}
	return true;
}
// Sprawdza czy wartosc pola jest integer
function checkFieldValueIsInteger(elementId, msg) {
	var el = document.getElementById(elementId);
	if (!el.value.match(/^[0-9]+$/)) {
		alert(msg);
		return false;
	}
}
//========================================
// FUNKCJE WALIDACJI FORMULARZY end
//========================================

// wstawia kolejne pole czesci 
//i=3;
function wstaw() {
	
	var i = $('[name="kodCzesci[]"]').length;
	$('#pole_t1').append('<div>Kod wymienionej części '+ (i+1) +'</div>');
	$('#pole_i1').append('<div><input type="text" name="kodCzesci[]" /></div>');
	$('#pole_t2').append('<div>Cena <input type="text" name="cenaCzesci[]" onchange="replaceCommaToPoint(this);calculateTotal()" class="text02" /></div>');
	$('#pole_t3').append('<div>Numer faktury <input type="text" name="fakturaCzesci[]" /></div>');
	
	/**
	var classType = (navigator.appName == "Microsoft Internet Explorer") ? "className" : "class";
    var i     = document.getElementsByName('kodCzesci[]').length + 1;	
    var tekst = document.createTextNode('Kod wymienionej części '+i+' ');
    var input = document.createElement('input');
    
    input.setAttribute('type', 'text');
    input.setAttribute('name', 'kodCzesci[]');
  
   	var div_kod = document.createElement('div');    
    div_kod.appendChild(tekst);
    
  	var div_input = document.createElement('div');    
    div_input.appendChild(input);
            
  	var tekst1 = document.createTextNode('Cena ');
  	var input1 = document.createElement('input');
  	  
    input1.setAttribute('type', 'text');
    input1.setAttribute('name', 'cenaCzesci[]');
 	input1.setAttribute(classType, 'text02');
    input1.setAttribute('onchange', 'replaceCommaToPoint(this);calculateTotal()')
   
  	var tekst2 = document.createTextNode(' Ilość ');
  	var input2 = document.createElement('input');
  
    input2.setAttribute('type', 'text');
    input2.setAttribute('name', 'iloscCzesci[]');
    input2.setAttribute('value', 1);
 	input2.setAttribute(classType, 'text01');
    input2.setAttribute('onchange', 'calculateTotal()')
 
    var div1 = document.createElement('div'); 	  
  	div1.appendChild(tekst1);
    div1.appendChild(input1); 
    div1.appendChild(tekst2);
    div1.appendChild(input2); 
    
    document.getElementById('pole_t1').appendChild(div_kod);  
    document.getElementById('pole_i1').appendChild(div_input);   
    document.getElementById('pole_t2').appendChild(div1);
  */
 // document.getElementById('pola').appendChild(div);
 // document.getElementById('pola1').appendChild(div1);
 // document.getElementById('pola2').appendChild(div2);
}

/**
* Wstawia nowe pole z częściami w edycji rozliczenia.
*/
function add_new_part_field() {
	
	
	//var i = $('[name="kodCzesci_new[]"]').length;
	$('#pole_t1').append('<div>Kod wymienionej części</div>');
	$('#pole_i1').append('<div><input type="text" name="kodCzesci_new[]" /></div>');
	$('#pole_t2').append('<div>Cena <input type="text" name="cenaCzesci_new[]" onchange="replaceCommaToPoint(this);calculateTotalEdit()" class="text02" /></div>');
	$('#pole_t3').append('<div>Numer faktury <input type="text" name="fakturaCzesci_new[]" /></div>');
 // var i = document.getElementsByName('kodCzesci[]').length + 1;	
 // i++;
 /**
 	var classType = (navigator.appName == "Microsoft Internet Explorer") ? "className" : "class";
    
    var tekst = document.createTextNode('Kod wymienionej części');
    var input = document.createElement('input');
      
    input.setAttribute('type', 'text');
    input.setAttribute('name', 'kodCzesci_new[]');
  
    var div_kod = document.createElement('div');
    div_kod.appendChild(tekst);
    var div_input = document.createElement('div');
    div_input.appendChild(input);
      
      
    var tekst1 = document.createTextNode('Cena ');
    var input1 = document.createElement('input');
    var tekst2 = document.createTextNode(' Ilość ');
    var input2 = document.createElement('input');  
    
    input1.setAttribute('type', 'text');
    input1.setAttribute('name', 'cenaCzesci_new[]');
 	input1.setAttribute(classType, 'text02');
    input1.setAttribute('onchange', 'replaceCommaToPoint(this);calculateTotalEdit()');
    
    input2.setAttribute('type', 'text');
    input2.setAttribute('name', 'iloscCzesci_new[]');
    input2.setAttribute('value', 1);
 	input2.setAttribute(classType, 'text01');
    input2.setAttribute('onchange', 'calculateTotalEdit()');
    
    var div1 = document.createElement('div');
  	  
    div1.appendChild(tekst1);
    div1.appendChild(input1); 
    div1.appendChild(tekst2);
    div1.appendChild(input2);
    
  document.getElementById('pole_t1').appendChild(div_kod);
  document.getElementById('pole_i1').appendChild(div_input);
  document.getElementById('pole_t2').appendChild(div1);
  */
}

// Sprawdza czy przed numerem seryjnym jest skrót SN
// Jezeli nie ma dodaje go
function checkSN(sn) {
	var prefix = sn.value.substring(0,2).toUpperCase();
	//var number = sn.value.substring(2);
	if (prefix == 'SN') {
	    var number = sn.value.substring(2);
	} else {
		var number = sn.value;
	}
	sn.value = 'SN' + number;
	
	
}

// AJAX
function SendRequest(plik, id){      
      var req = mint.Request();
      req.Send(plik, id);
}
/**
* Pobiera ceny na podstawie numeru katalogowego
*/
function checkPrices() {
	$('.code').each(function(index) {
		 
		 var id = ($(this).attr('id'));
		 var code = $(this).val();
		 var substr_id = (id.substring(5,6));
		 if (code) {
			 $.ajax({
				 url: 'ajax/prices.php',
				 type: 'POST',
				 dataType: 'json',
				 data: "number="+code,
				 success: function(obj) {
	   				if (obj.cena == 'brak') {
	   					$('#chn_'+substr_id).html('<div style="text-align:left">Brak podanego kodu w bazie cen. W celu ustalenia ceny proszę o kontakt z Serwisem Centralnym</div>');
	   					
	   					
	   				} else {
				    	 $('#chn_'+substr_id).html(obj.chn);
				    	 $('#chb_'+substr_id).html(obj.chb);
				    	 $('#cdn_'+substr_id).html(obj.cdn);
				    	 $('#cdb_'+substr_id).html(obj.cdb);
	   				}
				 }
		     });
		 }
	});
	
}

function checkProduct(f) {
	if (f.name.value == '') {
		alert('Proszę podać nazwę produktu');
		return false;
	}
	if (f.catalog_number.value == '') {
		alert('Proszę podać numer katalogowy');
		return false;
	}
}

function checkProductPrice(f) {
	
	if (f.catalog_number.value == '') {
		alert('Proszę podać numer katalogowy');
		return false;
	}
	return true;
}

function checkProductPriceCodeExists(code) {
	
	$("#productPriceInfo").html('');
	$.ajax({
		url: 'ajax/checkProductPriceCodeExists.php',
		type: 'POST',
		data: "number="+code,
		success: function(exists) {
			if (exists == 1) {
				$("#productPriceInfo").html('Produkt z takim kodem istnieje w bazie');
			} 
		}
    });
}
