function comparePass(p1, p2){
	var pass1=document.getElementById(p1).value;
	var pass2=document.getElementById(p2).value;

	if(pass1==pass2 && pass1!="" && pass2!=""){
		return true;
	}
	else{
		alert('Powtórzenie hasła jest niepoprawne');
		return false;
	}
}

//Usuwanie spacji
function trim(value)
{
  //return value.replace(/\s+/g,'');
  //usuniecie space oraz usuniecie no-break space
  //return value.replace(/\s+/g,'').replace('\u00A0','').replace('&nbsp;','');

  //liczba może byc duza wiec trzeba zrobic kilkukrotnie
  var trimed = value.replace(/\s+/g,'').replace('\u00A0','').replace('&nbsp;','');
  trimed = trimed.replace(/\s+/g,'').replace('\u00A0','').replace('&nbsp;','');
  trimed = trimed.replace(/\s+/g,'').replace('\u00A0','').replace('&nbsp;','');
  return trimed;
}

//Sprawdzanie formatu waluty
function checkCurrency(value)
{
	var res;
	res=/^(\d|\s)*(\,\d{0,2})?$/.test(trim(value)) || value == "";
	return res;
}

//Parsuje walutę
function floatToCurrency(value)
{
	return trim(value).replace(".",",");
}

//Sformatowana waluta do float
function currencyToFloat(value)
{
	return trim(value).replace(",",".");
}


function updatePrice(){
//	var inputs = document.getElementById('commodities').getElementsByTagName('INPUT');
    var inputs = $('.rowPriceNetto');
	var i;
	var value, sum = 0;
	var  priceSum = document.getElementById('priceSum');

	for(i=0;i<inputs.length;i++)
	{
        //alert(inputs[i].outerHTML);
		value = inputs[i].value;
		if(!checkCurrency(value))
		{
			alert("Nieprawidłowy format ceny!");
			inputs[i].focus();
			return false;
		}
        //aby wartość dała przeparsować się na float
        inputs[i].value=floatFormatted(currencyToFloat(value));

		vfloat = currencyToFloat(value);
		sum += parseFloat(vfloat);
	}
	priceSum.innerHTML = floatFormatted(sum.toString());
	//alert(inputs.length);
	return false;
}

function updateUniPrice(){

    var rowsQuantity = $('.rowQuantity');
    var rowsUnitPrice = $('.rowUnitPriceNetto');
    var rowsSumPrice = $('.rowSumNetto');
    //var priceSum = $('#priceSum');
    var  priceSum = document.getElementById('priceSum');
    var quantity, unitPrice, rowSum = 0, sum=0;
    var i;
    for(i=0;i<rowsUnitPrice.length;i++){

      value = rowsUnitPrice[i].value;

      if(!checkCurrency(value)){
        alert("Nieprawidłowy format ceny!");
        rowsUnitPrice[i].focus();
        return false;
      }

      //unitPrice = parseFloat(rowsUnitPrice[i].value.replace(",","."));
      //quantity = parseFloat(rowsQuantity[i].innerHTML.replace(",","."));
      unitPrice =  currencyToFloat(value);

      quantity =   currencyToFloat(rowsQuantity[i].innerHTML);

      rowSum = quantity * unitPrice;
      sum+=rowSum;
      rowSum = rowSum.toFixed(2);
      rowsSumPrice[i].innerHTML = floatFormatted(rowSum.toString());
      sum.toFixed(2);

      rowsUnitPrice[i].value = floatFormatted(unitPrice);

    }
    //priceSum.innerHTML = sum.toString().replace(".",",");
    priceSum.innerHTML = floatFormatted(sum.toString());
    return false;

}

function floatFormatted(amount)
{
  var delimiter = " "; // replace comma if desired
  var a = amount.split('.',2)
  var d = a[1];
  //bo coś dziwnie działało
  if(d==null)
  {
    d='00';
  }
  //-----------------
  var i = parseInt(a[0]);
  if(isNaN(i)) { return ''; }
  var minus = '';
  if(i < 0) { minus = '-'; }
  i = Math.abs(i);
  var n = new String(i);
  var a = [];
  while(n.length > 3)
  {
    var nn = n.substr(n.length-3);
    a.unshift(nn);
    n = n.substr(0,n.length-3);
  }
  if(n.length > 0) { a.unshift(n); }
  n = a.join(delimiter);
  if(d.length < 1) { amount = n; }
  else { amount = n + ',' + d; }
  amount = minus + amount;
  if(d.length==1)
  {
  	amount=amount+'0';
  }
  return amount;
}




function changeFile(){
	var attFile = document.getElementById('attFile');

	if(attFile.style.display == 'none'){
		attFile.style.display = 'block';
	}
	return false;
}
