function convertToFloat(mixed_var)
{
	return (parseFloat(mixed_var) || 0);
}

function convertToInt(mixed_var, base)
{
	var tmp;
    var type = typeof( mixed_var );
 
    if (type === 'boolean')
    {   
    	return (mixed_var) ? 1 : 0;
    } 
    else if (type === 'string')
    {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } 
    else if (type === 'number' && isFinite(mixed_var) ) 
    {  
    	return Math.floor(mixed_var);
    }
    else 
    {
        return 0;
    }

}

function zmienIlosc()
{
	var idCena;
	
	try
	{
		idCena = document.form.rozmiary.value;
	}
	catch(ex)
	{
		idCena = '';
	}
	
	try
	{
		var cenaSzt 	= convertToFloat(document.getElementById('cena'+idCena).innerHTML);
		var docIlosc 	= document.getElementById('ilosc');
		var ilosc 		= convertToInt(docIlosc.value);
	}
	catch(ex)
	{
		return false;
	}
	
	/* sprawdz dostępnosc jesli sztuki */
	if (ilosc > dostepnosc[idCena])
	{
		alert(lang['tw']['brakWMagazynie']);
		document.getElementById('ilosc').value = dostepnosc[idCena] > 0 ? dostepnosc[idCena] : 1;
		zmienIlosc();
		return;
	}

	if (ilosc && cenaSzt)
	{
		var wartosc = cenaSzt * ilosc;
		document.getElementById('wartosc').innerHTML = wartosc.toFixed(2);
	}
	else
	{
		if (docIlosc.value != '')
		{
			//alert(1);
			alert(lang['tw']['poprawneWartLiczb']);
			document.getElementById('wartosc').innerHTML = cenaSzt.toFixed(2);
			document.getElementById('ilosc').value = 1;
		}
	}
}

function zmienIloscKalk(idCenaSzt, type)
{
	var cenaSzt = 0;
	
	if (idCenaSzt != null)
	{
		if (type == 'ihtml')
		{
			cenaSzt 	= convertToFloat(document.getElementById(idCenaSzt).innerHTML);
		}
		else
		{
			cenaSzt 	= convertToFloat(document.getElementById(idCenaSzt).value);
		}
	}
	else
	{
		cenaSzt 	= convertToFloat(document.kalk.calkow.value);
	}
	//alert('p1 = ' + idCenaSzt + ' p2 = ' + type);
	//alert('cenaSzt ' + cenaSzt);
	var docIlosc 	= document.getElementById('ilosc');
	var ilosc 		= convertToInt(docIlosc.value);
	//alert('ilosc ' + ilosc);
	
	if (ilosc && cenaSzt)
	{
		var wartosc = cenaSzt * ilosc;
		document.getElementById('wartosc').value = wartosc.toFixed(2);
	}
	else
	{
		if (docIlosc.value != '')
		{
			//alert(2);
			alert(lang['tw']['poprawneWartLiczb']);
			document.getElementById('wartosc').value = cenaSzt.toFixed(2);
			document.getElementById('ilosc').value = 1;
		}
	}
	
	
}
