function antiCrisisProgramm (num) { if (num == 1) { $('select[name="termLease"]').selectOptions('12'); $('input[name="1_cascoInput"]').val('6'); } if (num == 2) { $('select[name="termLease"]').selectOptions('22'); $('input[name="1_cascoInput"]').val('6'); $('input[name="2_cascoInput"]').val('6'); } $('input[name="advance"]').val('35'); $('input[name="casco"]').attr('checked', 'checked'); $('input[name="discount"]').attr('checked', 'checked'); $('input[name="discountInput"]').val('2'); $('input[name="WithoutTakingIntoAccountTheHigherCostOfAdditionalExpenses"]').attr('checked', 'checked'); $('input[name="FeeForRegistrationOfTransactions"]').attr('checked', 'checked'); $('input[name="FeeForRegistrationOfTransactionsInput"]').val('1.99'); $('input[name="PurchaseCommission"]').attr('checked', 'checked'); $('input[name="PurchaseCommissionInput"]').val('1'); $('input[name="FeeForInsurance"]').attr('checked', 'checked'); $('input[name="FeeForInsuranceInput"]').val('3.23'); $('select[name="principle"]').selectOptions('1'); } function cascoGenerate(y) { $('.cascoTable tr[id^="cascoTr_"]').remove(); for (i=1;i<=y;i++) { $('.cascoTable tr:last').after('\ \ ' + i + ' год\ \ \
\ \ \ \ \ Сумма за ' + i + 'ый год\ 0\ '); } return; } function month2years(m) { var y = Math.ceil(m/12); return y; } function aditionalCalc(elName) { var cost = $('input[name="cost"]').val(); $('input[name$="' + elName + 'Input"]').each(function() { var ID = $(this).attr('name').replace('_' + elName + 'Input', ''); var value = $(this).val(); var sumType = $('input[name="' + ID + '_' + elName + 'InputType"]').val(); if (value && cost && sumType) $('#' + ID + '_' + elName + 'Value').text(calc(cost,value,sumType) + ' ' + returnChar(sumType)); else $('#' + ID + '_' + elName + 'Value').text('Данных нет'); }); } function aditional(object, elName) { if ($(object).attr('checked') == true) { $('.' + elName + 'Tr').show(); var cost = $('input[name="cost"]').val(); var value = $('input[name="' + elName + 'Input"]').val(); var sumType = $('input[name="' + elName + 'InputType"]').val(); if (value && cost && sumType) $('#' + elName + 'Value').text(calc(cost,value,sumType) + ' ' + returnChar(sumType)); else $('#' + elName + 'Value').text('Данных нет'); } else { $('.' + elName + 'Tr').hide(); } } function aditionalShowOnly(object, elName) { if ($(object).attr('checked') == true) { $('.' + elName + 'Tr').show(); } else { $('.' + elName + 'Tr').hide(); } } function aditionalInputOpen(elName) { var cost = $('input[name="cost"]').val(); var value = $('input[name="' + elName + '"]').val(); var sumType = $('input[name="' + elName + 'InputType"]').val(); if (value && cost && sumType) $('#' + elName + 'Value').text(calc(cost,value,sumType) + ' ' + returnChar(sumType)); else $('#' + elName + 'Value').text('Данных нет'); } function returnChar(type) { switch (type) { case 'procents': return 'Руб.'; break case 'money': return '%'; break } } function calc(costA, costB, type) { switch (type) { case 'procents': value = costA/100*costB; break case 'money': value = 100/costA*costB; break } if (isNaN(value)) { return '0'; } else { return value.toFixed(2) ; } } function changerProcentsAndMoney(elementClicked,elementInput) { var currentValue = elementInput.val(); switch (elementInput.val()) { case 'procents': $(elementClicked).attr('class', 'money'); $(elementInput).val('money'); break case 'money': $(elementClicked).attr('class', 'procents'); $(elementInput).val('procents'); break } return; } /*$.validator.setDefaults({ submitHandler: function() { alert("submitted!"); } });*/ $(document).ready(function() { $("#form").validate({ rules: { discountInput: { number: true }, agentInput: { number: true }, extrasCostsInput: { number: true }, PurchaseCommissionInput: { number: true }, FeeForInsuranceInput: { number: true }, FeeForRegistrationOfTransactionsInput: { number: true }, advance: { number: true }, balanceInvistitsy: { number: true }, additionalInsurance: { number: true }, DOSAGO: { number: true }, MountingRisks: { number: true }, Transportation: { number: true }, power: { number: true, required: true}, cost: { number: true, required: true }, model: { required: true }, mark: { required: true } } }); var formObject = $('input,select,textarea'); $('div[id$="DivTypeChanger"]').live('click', function() { var changeElName = $(this).attr('id').replace('DivTypeChanger', ''); changerProcentsAndMoney(this, $('input[name="' + changeElName + 'InputType"]')); formObject.change(); }); formObject.live('change', function() { elName = $(this).attr('name'); if ($(this).attr('type') == 'checkbox') { if (elName == 'discount') { aditional($(this), elName); } if (elName == 'agent') { aditional($(this), elName); } if (elName == 'extrasCosts') { aditional($(this), elName); } if (elName == 'PurchaseCommission') { aditional($(this), elName); } if (elName == 'FeeForInsurance') { aditional($(this), elName); } if (elName == 'FeeForRegistrationOfTransactions') { aditional($(this), elName); } if (elName == 'additionalInsurance') { aditionalShowOnly($(this), elName); } if (elName == 'casco') { aditionalShowOnly($(this), elName); aditionalCalc('casco'); } } if ($(this).attr('type') == 'text') { if (elName == 'advance') { aditionalInputOpen(elName); } if (elName == 'balanceInvistitsy') { aditionalInputOpen(elName); } if (elName == 'DOSAGO') { aditionalInputOpen(elName); } if (elName == 'MountingRisks') { aditionalInputOpen(elName); } if (elName == 'Transportation') { aditionalInputOpen(elName); } } if ($(this).attr('type') == 'select-one') { if (elName == 'steps') { if ($(this).val() > 0) { $('.stepsTr').show(); } else { $('.stepsTr').hide(); } } if (elName == 'termLease') { if ($(this).val() == 0) { var y = month2years($('input[name="termLeaseManual"]').val()); $('.termLeaseTr').show(); $('input[name="termLeaseYears"]').val(y); } else { $('.termLeaseTr').hide(); var y = month2years($(this).val()); $('input[name="termLeaseYears"]').val(y); } if ($('input[name="temLeaseChache"]').val() == y) { return; } else { $('input[name="temLeaseChache"]').val(y); } var addOptionsToStepsSelector = {}; var removeOptionsToStepsSelector = []; if (y == 0) { removeOptionsToStepsSelector = ['2','4','6']; } if (y > 0) { addOptionsToStepsSelector["2"] = "2 ступени"; removeOptionsToStepsSelector = ['4','6']; } if (y > 1) { addOptionsToStepsSelector["4"] = "4 ступени"; removeOptionsToStepsSelector = ['6']; } if (y > 2) { addOptionsToStepsSelector["6"] = "6 ступеней"; } $('select[name="steps"]').removeOption(removeOptionsToStepsSelector) $('select[name="steps"]').addOption(addOptionsToStepsSelector, false); cascoGenerate(y); } if (elName == 'programm') { if ($(this).val() > 0) { antiCrisisProgramm($(this).val()); var currentProgramm = $('input[name="programmHidden"]').val(); if (currentProgramm != $(this).val()) { $('input[name="programmHidden"]').val($(this).val()); formObject.change(); } } } } }); formObject.live('keyup', function() { formObject.change(); }); formObject.live('click', function() { formObject.change(); }); });