function format_number(pnumber,decimals){
     if (isNaN(pnumber)) { return 0};
     if (pnumber=='') { return 0};
     var snum = new String(pnumber);
     var sec = snum.split('.');
     var whole = parseFloat(sec[0]);
     var result = '';
     if(sec.length > 1){
        var dec = new String(sec[1]);
         dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
         dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
         var dot = dec.indexOf('.');
         if(dot == -1){
             dec += '.';
             dot = dec.indexOf('.');
         }
         while(dec.length <= dot + decimals) { dec += '0'; }
         result = dec;
     } else{
         var dot;
         var dec = new String(whole);
         dec += '.';
         dot = dec.indexOf('.');       
         while(dec.length <= dot + decimals) { dec += '0'; }
         result = dec;
     }   
     return result;
 }



function obliczWartoscPrzesylki()
{
	var poleOzdobne = document.getElementById('ozdobne');
	var poleOpakowanie = document.getElementById('opakowanie');
	var poleSuma = document.getElementById('suma');
	var poleWartosc = document.getElementById('wartosc');
	var opakowanieCena;

	if (poleOzdobne.checked) { 
		opakowanieCena=poleOzdobne.value; // kwota za opakowanie
		poleOpakowanie.value=''+opakowanieCena+'';
	}
	else {
		opakowanieCena=0;
		poleOpakowanie.value='0';
	}

	var poleKoszty = document.getElementById('koszty');
	var koszty=poleKoszty.value; //document.zamowienie.koszty.value;
	var suma=poleSuma.value; //suma
	poleWartosc.value=format_number(suma*1+koszty*1+opakowanieCena*1, 2);	
}

function ustawKosztyPrzesylki(k1, k2, k3, k4)
{
	var poleFormaPlatnosci = document.getElementById('forma_platnosci');
	var poleOdbior = document.getElementById('odbior');
	var poleKoszty = document.getElementById('koszty');
	if (poleOdbior.value==0)	{
		poleFormaPlatnosci.options[1].value='0';
		poleFormaPlatnosci.options[2].value='0';
	}
	if (poleOdbior.value==1)	{
		poleFormaPlatnosci.options[1].value=k1;
		poleFormaPlatnosci.options[2].value=k2;
	}
	if (poleOdbior.value==2)	{
		poleFormaPlatnosci.options[1].value=k3;
		poleFormaPlatnosci.options[2].value=k4;
	}
	poleKoszty.value='0';
	var alt = document.getElementById('alternatywa');
	alt.checked = false;
	poleFormaPlatnosci.disabled = false;
	poleFormaPlatnosci.focus();
	poleFormaPlatnosci.selectedIndex=0;
	
}

function ustawFormePlatnosci() {
	var poleFormaPlatnosci = document.getElementById('forma_platnosci');
	
	var polePlatnoscUkryte = document.getElementById('platnosc');
	if (poleFormaPlatnosci.selectedIndex==0 || poleFormaPlatnosci.selectedIndex==1) {
		polePlatnoscUkryte.value=1;
	}
	if (poleFormaPlatnosci.selectedIndex==2) {
		polePlatnoscUkryte.value=2;
	}

	
}

function checkAlternatywa() {
		
	var alt = document.getElementById('alternatywa');
	var poleFormaPlatnosci = document.getElementById('forma_platnosci');
	if (alt.checked) { 
		poleFormaPlatnosci.focus();
		poleFormaPlatnosci.selectedIndex=2;
		//poleFormaPlatnosci.readOnly;
		poleFormaPlatnosci.disabled = true;
	}
	else	{
		poleFormaPlatnosci.disabled = false;
	}
	
}


function makeVisible(pole, mode) {
		
	var div = document.getElementById(pole);
	div.style.display=mode;
	
}



function getRadioValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function loadKoszty() {
		var url = '/modules/ajax_getPayForm.php';
		var pars = 'dostawa='+ getRadioValue(document.forms['DoKasy'].elements['odbior']) +'&platnosc='+getRadioValue(document.forms['DoKasy'].elements['forma_platnosci']) +'';
		var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars ,onComplete: showResponse, onFailure: reportError});
	}
function reportError(request)
	{
		alert('Sorry. There was an error.');
	}
function showResponse(originalRequest)
	{
		//put returned XML in the textarea
		$('koszty').value = originalRequest.responseText;

		var poleOzdobne = document.getElementById('ozdobne');
		var poleOpakowanie = document.getElementById('opakowanie');
		var poleSuma = document.getElementById('suma');
		var poleWartosc = document.getElementById('wartosc');
		var opakowanieCena;
		
		if (poleOzdobne.checked) { 
			opakowanieCena=poleOzdobne.value; // kwota za opakowanie
			poleOpakowanie.value=''+opakowanieCena+'';
		}
		else {
			opakowanieCena=0;
			poleOpakowanie.value='0';
		}
		
		var koszty=originalRequest.responseText; 
		var suma=poleSuma.value; //suma
		poleWartosc.value=format_number(suma*1+koszty*1+opakowanieCena*1, 2);




	}
	
