var numAttachments = 1; var totalAmount = 0; var tabAmount = 0; function addAttachment() { $('.attachment_0').clone().removeClass('attachment_0').addClass('attachment_' + numAttachments).insertBefore('.addattachment'); $('.attachment_' + numAttachments + ' td input').attr('name', 'attachment[' + numAttachments + ']').val(''); var buttonDelete = document.createElement('input'); buttonDelete.type = 'button'; buttonDelete.value = 'Delete'; buttonDelete.className = 'btn'; $('.attachment_' + numAttachments + ' td:last').append(document.createTextNode(' ')) $('.attachment_' + numAttachments + ' td:last').append(document.createElement('strong')) $('.attachment_' + numAttachments + ' td:last strong').addClass('red').append(buttonDelete); $('.attachment_' + numAttachments + ' td input.btn').click(function(){ $(this).parent().parent().parent().remove(); }); numAttachments++; } function changeHostingFlag() { if (this.checked) { $('tr.hosting_from').show(); $('tr.hosting_to').show(); } else { $('tr.hosting_from').hide(); $('tr.hosting_to').hide(); } } function initForm(){ $('tr.tabs td').tabs({idPrefix: 'invoice-tab' }); $('tr.tabs input[name*="amount"]').blur(function(){ recalcAll(); recalcTab($(this).parent().parent().parent().parent().attr('id')); }); $('tr.tabs input[type="checkbox"]').blur(function(){ recalcAll(); recalcTab($(this).parent().parent().parent().parent().attr('id')); }).click(function(){this.blur();}); recalcTab('work_tab'); recalcTab('domain_tab'); recalcTab('hosting_tab'); recalcTab('ssl_tab'); } function recalcAll(){ totalAmount = 0; $('tr.tabs input[name*="amount"]').each(function(){ /*alert($(this).parent().prev().children('input[type="checkbox"]').size());*/ if (!$(this).parent().prev().children('input[type="checkbox"]').size() || $(this).parent().prev().children('input:checked').size()){ amount = parseFloat($(this).val(),10); if (!isNaN(amount)) { totalAmount += amount; } } }); $('tr.amount input').val(number_format(totalAmount, 2, '.', '')); } function recalcTab(tabId) { tabAmount = 0; $('#' + tabId + ' tr').each(function(){ if (!$(this).children('td:0').children('input[type="checkbox"]').size() || $(this).children('td:0').children('input').attr('checked')) { amount = parseFloat($(this).children('td:1').children('input[name*="amount"]').val(),10); if (!isNaN(amount)) { tabAmount += amount; } } }); $('tr.tabs a[href="#' + tabId + '"] span').html('$'+number_format(tabAmount, 2, '.', '')); }