function round(value,precision) { var exp = Math.pow(10,precision); return Math.round(value*exp)/exp; } function initialize() { newParticipant(); recalculate(document.itemForm1.itemName); } function modifyParticipant(field) { var form = field.form; var index = form.name.substring(15); if(form.participantName.value && index==newParticipantIndex) newParticipant(); for (var i = 0; i < document.forms.length; i++) { if(document.forms[i].name.startsWith("itemForm")) { var itemForm = document.forms[i]; if(form.participantName.value) { var optionIndex = selectOptionIndexByValue(itemForm.itemPays,index); if(optionIndex>=0) { itemForm.itemPays.options[optionIndex].text = form.participantName.value; itemForm.itemShouldPay.options[optionIndex].text = form.participantName.value; } else { itemForm.itemPays.options.add(new Option(form.participantName.value,index)); itemForm.itemShouldPay.options.add(new Option(form.participantName.value,index)); } }else { selectRemoveByValue(itemForm.itemPays,index); selectRemoveByValue(itemForm.itemShouldPay,index); } } } recalculate(document.itemForm1.itemName); } var newParticipantIndex = 1; function newParticipant() { var participantDiv = document.getElementById("participantPanel1"); newParticipantIndex++; var newParticipantNode = participantDiv.cloneNode(true); newParticipantNode.getAttributeNode("id").value = "participantPanel"+newParticipantIndex; newParticipantNode.innerHTML = newParticipantNode.innerHTML.replace("participantForm1","participantForm"+newParticipantIndex); newParticipantNode.innerHTML = newParticipantNode.innerHTML.replace("input_selected",""); participantDiv.parentNode.appendChild(newParticipantNode); } var newItemIndex = 1; function newItem() { var itemDiv = document.getElementById("itemPanel1"); newItemIndex++; var newItemNode = itemDiv.cloneNode(true); newItemNode.getAttributeNode("id").value = "itemPanel"+newItemIndex; newItemNode.innerHTML = newItemNode.innerHTML.replace("itemForm1","itemForm"+newItemIndex); newItemNode.innerHTML = newItemNode.innerHTML.replace("input_selected",""); itemDiv.parentNode.appendChild(newItemNode); } function selectOptionIndexByValue(select, value) { for (var i = select.options.length-1; i >= 0 ; i--) { if(select.options[i].value == value) { return i; } } return -1; } function selectRemoveByValue(select, value) { for (var i = select.options.length-1; i >= 0 ; i--) { if(select.options[i].value == value) { select.options.remove(i); } } } function recalculate(field) { var index = field.form.name.substring(8); if(field.form.itemName.value && index==newItemIndex) newItem(); var total = 0.0; var totalPaid = 0.0; var participants = 0; var names = Array(); var paid = Array(); var shouldPay = Array(); for (var i = 0; i < document.forms.length; i++) { if(document.forms[i].name.startsWith("participantForm")) { var participantForm = document.forms[i]; var index = participantForm.name.substring(15); if(participantForm.participantName.value) { names[index] = participantForm.participantName.value; if(participantForm.participantAmount.value) { var participantAmount = parseFloat(participantForm.participantAmount.value); paid[index] = participantAmount; totalPaid += participantAmount; }else { paid[index] = 0; } shouldPay[index] = 0; participants++; } } } for (var i = 0; i < document.forms.length; i++) { if(document.forms[i].name.startsWith("itemForm")) { var itemForm = document.forms[i]; if(itemForm.itemAmount.value) { var itemAmount = parseFloat(itemForm.itemAmount.value); total = total + itemAmount; switch(itemForm.itemPays.value) { case "-1"://ze wspolnej puli break; case "-2": alert("tego jeszcze nie ma"); break; default: paid[itemForm.itemPays.value] += itemAmount; totalPaid += itemAmount; break; } switch(itemForm.itemShouldPay.value) { case "-1"://po równo for(var j = 0; j < shouldPay.length; j++) if(names[j]) shouldPay[j] += itemAmount/participants; break; case "-2": alert("tego jeszcze nie ma"); break; default: shouldPay[itemForm.itemShouldPay.value] += itemAmount; break; } } } } var resultDiv = document.getElementById("splitBillResult"); resultDiv.innerHTML = "Należność w sumie: "+total.toMoney(2,'.',' ')+". Zapłacono: "+totalPaid.toMoney(2,'.',' ')+".

"; /*for(var i = 1; i < names.length; i++) { resultDiv.innerHTML += i.toString()+". "; if(names[i]) resultDiv.innerHTML += names[i]+" "; if(paid[i]) resultDiv.innerHTML += "paid: "+paid[i].toMoney(2,'.',' ')+", "; if(shouldPay[i]) resultDiv.innerHTML += "should: "+shouldPay[i].toMoney(2,'.',' '); resultDiv.innerHTML += "
"; }*/ var diff = Array(); for(var i = 1; i < names.length; i++) { if(names[i]) diff[i] = paid[i] - shouldPay[i]; } var finished = false; while(!finished) { var minIndex = 0; var maxIndex = 0; for(var i = 1; i < names.length; i++) { if(diff[i]) { if(minIndex <=0 || diff[i]diff[maxIndex]) maxIndex = i; } } if(minIndex==0 || diff[minIndex]>=0 || diff[maxIndex]<=0) { finished = true;//all amounts has the same sign continue; } if(minIndex != maxIndex) { if( -diff[minIndex] > diff[maxIndex]) { resultDiv.innerHTML += ""+names[minIndex] +" oddaje "+names[maxIndex]+": "+diff[maxIndex].toMoney(2,'.',' ')+".
"; diff[minIndex] += diff[maxIndex]; diff[maxIndex] = 0; }else { resultDiv.innerHTML += ""+names[minIndex] +" oddaje "+names[maxIndex]+": "+(-diff[minIndex]).toMoney(2,'.',' ')+".
"; diff[maxIndex] += diff[minIndex]; diff[minIndex] = 0; } } } for(var i = 1; i < names.length; i++) { if(names[i] && round(diff[i],2)!=0) { if(diff[i]>0) resultDiv.innerHTML += ""+names[i] +" ma nadpłatę "+diff[i].toMoney(2,'.',' ')+".
"; else resultDiv.innerHTML += ""+names[i] +" musi dopłacić "+(-diff[i]).toMoney(2,'.',' ')+".
"; } } } function validate(field) { if(field.value.indexOf(',')>0) field.value = field.value.replace(',','.'); var numberRegExp = new RegExp("^[0-9\.,]*$"); if(!numberRegExp.test(field.value)) field.value = field.value.replace(/[^0-9\.,]*/ig,''); //check for double decimal separator var firstSep = field.value.indexOf("."); var secondSep = firstSep>0?field.value.indexOf(".",firstSep+1):-1; if(firstSep>0 && secondSep>firstSep) { field.value = field.value.substring(0,secondSep); } if(field.value[field.value.length-1]=='.') return; } function selectInput(field) { addClass(field,"input_selected"); for(var i = 0; i < document.forms.length; i++) for(var j = 0; j < document.forms[i].elements.length; j++) { var currentElement = document.forms[i].elements[j]; if(currentElement == field) continue; removeClass(currentElement,"input_selected"); } } function addClass(item, classname) { var obj = item if (typeof item=="string") { obj = document.getElementById(item) } obj.className += " " + classname } function removeClass(item, classname) { var obj = item if (typeof item=="string") { obj = document.getElementById(item) } var classes = ""+obj.className while (classes.indexOf(classname)>-1) { classes = classes.replace (classname, "") } obj.className = classes } Number.prototype.toMoney = function(decimals, decimalSeparator, thousandsSeparator) { var n = this; c = isNaN(decimals) ? 2 : Math.abs(decimals); d = decimalSeparator || '.'; t = (typeof thousandsSeparator === 'undefined') ? ' ' : thousandsSeparator; sign = (n < 0) ? '-' : '', i = parseInt(n = Math.abs(n).toFixed(c)) + '', j = ((j = i.length) > 3) ? j % 3 : 0; return sign + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ''); } String.prototype.startsWith = function (str) { return this.indexOf(str) == 0; };