dukeman911
12-22-2011, 04:27 PM
Let me start by saying I am not the best in JavaScript, so any help would be much appreciated. What i am trying to do is allow a user to get a quote on how much an item would be if they wanted to sell it. all the data is being generated dynamically on the item and passed to this JavaScript correctly. The issue is when a user changes his mind and switches his options a few times the price goes out of wack and some times will actually be more then the original price. For example. If the item was in mint condition it would be 100 dollars. If it is "
good condition" then it would loose lets say 20 percent, so it would be worth 80 dollars. Now each item has 3 sections of questions, what is the condition of the item, any "modifiers" like water damage or functional issues, and then third section is if its missing any parts or cables. All of this is being asked on 1 page, and when a user changes any of the options it should update the price on that page with out the user needing to press update.
So condition goes, 100%, 70%, 40%, 20%. Modifiers are 50 percent of current price of item, so if it was "perfect (yes I know an item cant be perfect and have water damage but this is just an example) thus being 100 dollars, and has water damage, that would make it now worth 50 dollars, and has functional issues, now worth 25 dollars. If it was missing cables, it would loose lets say 5 dollars so now its worth 20 dollars. Thats an example on how i calculate the value of the items. So once again, the actual amount is being passed correctly to this script, but when you make to many changes its not happy.
Here is the java code I have.
function calculateTotal(inputItem,amount1,percent1) {
with (inputItem.form) {
// Process each of the different input types in the form.
if (inputItem.type == "radio") { // Process radio buttons.
// Subtract the previously selected radio button value from the total.
if (inputItem.name =="conditions")
{
if (percent1 ==100)
{
amount2= (amount1*percent1/100)-1;
}
if (percent1 ==70)
{
amount2 = (amount1*percent1/100)-2;
}
if (percent1 ==40)
{
amount2 = (amount1*percent1/100)-3;
}
if (percent1 ==20)
{
amount2 = (amount1*percent1/100)-4;
}
calculatedTotal.value = eval(amount2) ;
}
// Save the current radio selection value.
previouslySelectedRadioButton.value = eval(inputItem.value);
// Add the current radio button selection value to the total.
calculatedTotal.value = eval(calculatedTotal.value) + eval(inputItem.value);
} else { // Process check boxes.
if (inputItem.checked == false) { // Item was unchecked. Subtract item value from total.
if (inputItem.name =="modifiers1")
calculatedTotal.value = eval(calculatedTotal.value) / eval(percent1/100);
if (inputItem.name =="modifiers2")
calculatedTotal.value = eval(calculatedTotal.value) / eval(percent1/100);
if (inputItem.name =="extra1")
calculatedTotal.value = eval(calculatedTotal.value) + eval(percent1) ;
if (inputItem.name =="extra2")
calculatedTotal.value = eval(calculatedTotal.value) + eval(percent1) ;
if (inputItem.name =="extra3")
calculatedTotal.value = eval(calculatedTotal.value) + eval(percent1);
if (inputItem.name =="extra4")
calculatedTotal.value = eval(calculatedTotal.value) + eval(percent1) ;
} else { // Item was checked. Add the item value to the total.
if (inputItem.name =="modifiers1")
calculatedTotal.value = eval(calculatedTotal.value) * eval(percent1/100);
if (inputItem.name =="modifiers2")
calculatedTotal.value = eval(calculatedTotal.value) * eval(percent1/100);
if (inputItem.name =="extra1")
calculatedTotal.value = eval(calculatedTotal.value) - eval(percent1);
if (inputItem.name =="extra2")
calculatedTotal.value = eval(calculatedTotal.value) - eval(percent1);
if (inputItem.name =="extra3")
calculatedTotal.value = eval(calculatedTotal.value) - eval(percent1);
if (inputItem.name =="extra4")
calculatedTotal.value = eval(calculatedTotal.value) - eval(percent1);
}
}
// Total value should never be less than 0.
if (calculatedTotal.value < 0) {
InitForm();
}
// Return total value.
return(formatCurrency(calculatedTotal.value));
}
}
// Format a value as currency.
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
// This function initialzes all the form elements to default values.
function InitForm() {
// Reset values on form.
document.selectionForm.total.value='$0';
document.selectionForm.calculatedTotal.value=0;
document.selectionForm.previouslySelectedRadioButton.value=0;
// Set all checkboxes and radio buttons on form to unchecked.
for (i=0; i < document.selectionForm.elements.length; i++) {
if (document.selectionForm.elements[i].type == 'checkbox' | document.selectionForm.elements[i].type == 'radio') {
document.selectionForm.elements[i].checked = false;
}
}
}
<!-- Paste this code into the HEAD section of your HTML document
</script>
Any help would be greatly appreciated.
good condition" then it would loose lets say 20 percent, so it would be worth 80 dollars. Now each item has 3 sections of questions, what is the condition of the item, any "modifiers" like water damage or functional issues, and then third section is if its missing any parts or cables. All of this is being asked on 1 page, and when a user changes any of the options it should update the price on that page with out the user needing to press update.
So condition goes, 100%, 70%, 40%, 20%. Modifiers are 50 percent of current price of item, so if it was "perfect (yes I know an item cant be perfect and have water damage but this is just an example) thus being 100 dollars, and has water damage, that would make it now worth 50 dollars, and has functional issues, now worth 25 dollars. If it was missing cables, it would loose lets say 5 dollars so now its worth 20 dollars. Thats an example on how i calculate the value of the items. So once again, the actual amount is being passed correctly to this script, but when you make to many changes its not happy.
Here is the java code I have.
function calculateTotal(inputItem,amount1,percent1) {
with (inputItem.form) {
// Process each of the different input types in the form.
if (inputItem.type == "radio") { // Process radio buttons.
// Subtract the previously selected radio button value from the total.
if (inputItem.name =="conditions")
{
if (percent1 ==100)
{
amount2= (amount1*percent1/100)-1;
}
if (percent1 ==70)
{
amount2 = (amount1*percent1/100)-2;
}
if (percent1 ==40)
{
amount2 = (amount1*percent1/100)-3;
}
if (percent1 ==20)
{
amount2 = (amount1*percent1/100)-4;
}
calculatedTotal.value = eval(amount2) ;
}
// Save the current radio selection value.
previouslySelectedRadioButton.value = eval(inputItem.value);
// Add the current radio button selection value to the total.
calculatedTotal.value = eval(calculatedTotal.value) + eval(inputItem.value);
} else { // Process check boxes.
if (inputItem.checked == false) { // Item was unchecked. Subtract item value from total.
if (inputItem.name =="modifiers1")
calculatedTotal.value = eval(calculatedTotal.value) / eval(percent1/100);
if (inputItem.name =="modifiers2")
calculatedTotal.value = eval(calculatedTotal.value) / eval(percent1/100);
if (inputItem.name =="extra1")
calculatedTotal.value = eval(calculatedTotal.value) + eval(percent1) ;
if (inputItem.name =="extra2")
calculatedTotal.value = eval(calculatedTotal.value) + eval(percent1) ;
if (inputItem.name =="extra3")
calculatedTotal.value = eval(calculatedTotal.value) + eval(percent1);
if (inputItem.name =="extra4")
calculatedTotal.value = eval(calculatedTotal.value) + eval(percent1) ;
} else { // Item was checked. Add the item value to the total.
if (inputItem.name =="modifiers1")
calculatedTotal.value = eval(calculatedTotal.value) * eval(percent1/100);
if (inputItem.name =="modifiers2")
calculatedTotal.value = eval(calculatedTotal.value) * eval(percent1/100);
if (inputItem.name =="extra1")
calculatedTotal.value = eval(calculatedTotal.value) - eval(percent1);
if (inputItem.name =="extra2")
calculatedTotal.value = eval(calculatedTotal.value) - eval(percent1);
if (inputItem.name =="extra3")
calculatedTotal.value = eval(calculatedTotal.value) - eval(percent1);
if (inputItem.name =="extra4")
calculatedTotal.value = eval(calculatedTotal.value) - eval(percent1);
}
}
// Total value should never be less than 0.
if (calculatedTotal.value < 0) {
InitForm();
}
// Return total value.
return(formatCurrency(calculatedTotal.value));
}
}
// Format a value as currency.
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
// This function initialzes all the form elements to default values.
function InitForm() {
// Reset values on form.
document.selectionForm.total.value='$0';
document.selectionForm.calculatedTotal.value=0;
document.selectionForm.previouslySelectedRadioButton.value=0;
// Set all checkboxes and radio buttons on form to unchecked.
for (i=0; i < document.selectionForm.elements.length; i++) {
if (document.selectionForm.elements[i].type == 'checkbox' | document.selectionForm.elements[i].type == 'radio') {
document.selectionForm.elements[i].checked = false;
}
}
}
<!-- Paste this code into the HEAD section of your HTML document
</script>
Any help would be greatly appreciated.