2 Replies - 868 Views - Last Post: 15 November 2009 - 12:54 PM

#1 jmarian1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 20-October 09

Help in subtracting totalgift when donateamount condition trigger and

Post icon  Posted 12 November 2009 - 01:17 PM

Hi All, I need help on how to subtract totalgift if my condition of donateamount is lesser than the totalgift trigger.

When the field is selected and the DonateAmount is trigger, I need to subtract the entered amount on the field selected to the TotalGift and put the field selected to zero "0" and TotalGift to original amount which is less than the amount entered on the field selected. I have some code that I created but it doesn't work. My initial javascript to calculate all the total is below assuming that DonateAmount is already selected = 25:
 /* this function works fine with the set of code*/
function CalculateTotal(donate_amount_other) {
  var tota = document.getElementById('a').value; 
  var totb = document.getElementById('b').value;
  var totc = document.getElementById('c').value;
  var totd = document.getElementById('d').value;
  var tote = document.getElementById('e').value;
  var totf = document.getElementById('f').value;
  var totg = document.getElementById('g').value;
  var toth = document.getElementById('h').value;
  var toti = document.getElementById('i').value;
  var totj = document.getElementById('j').value;
  var totk = document.getElementById('k').value;
  var totl = document.getElementById('l').value;
  var totm = document.getElementById('m').value;
  var totn = document.getElementById('n').value;
  var toto = document.getElementById('o').value;
  var totp = document.getElementById('p').value;
  var totq = document.getElementById('q').value;
  var totr = document.getElementById('r').value;
  var TotC = document.getElementById('TotalC').value;

TotC = Number(tota) + Number(totb) + Number(totc) + Number(totd) + Number(tote) + Number(totf) + Number(totg) + Number(toth) + Number(toti) + Number(totj) + Number(totk) + Number(totl) + Number(totm) + Number(totn) + Number(toto) + Number(totp) + Number(totq) + Number(totr);
  document.getElementById('TotalC').value = TotC.toFixed(2);


  if (isNaN(TotC)) {   // if grandTotal is not a number
	alert ("You must enter numbers 0-9 only in the boxes");
	document.getElementById('TotalC').value = "ERROR"; // or perhaps value = "ERROR!"
	return false;
  }
 
 /* the condition below triggers only when the toti - totr fields are entered less than 48  but still need to subtract the total if TotalGift is greater than DonateAmount given */	
		  
  if (toti >0 && toti <48 ) {
	alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors");
	document.getElementById('i').value = "0"; // or perhaps value = "ERROR!"

	return false;
  }
  
  if (totj >0 && totj <48) {
	alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors");
	
	document.getElementById('j').value = "0"; // or perhaps value = "ERROR!"
	return false;
  }
  
  if (totk >0 && totk <48) {
	alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors");
	document.getElementById('k').value = "0"; // or perhaps value = "ERROR!"
	return false;
  }
  
  if (totl >0 && totl <48) {
	alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors");
	document.getElementById('l').value = "0"; // or perhaps value = "ERROR!"
	return false;
  }
  
  if (totm >0 && totm <48) {
	alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors");
	document.getElementById('m').value = "0"; // or perhaps value = "ERROR!"
	return false;
  }
  
  if (totn >0 && totn <48) {
	alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors");
	document.getElementById('n').value = "0"; // or perhaps value = "ERROR!"
	return false;
  }
  
  if (toto >0 && toto <48) {
	alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors");
	document.getElementById('o').value = "0"; // or perhaps value = "ERROR!"
	return false;
  }
  
  if (totp >0 && totp <48) {
	alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors");
	document.getElementById('p').value = "0"; // or perhaps value = "ERROR!"
	return false;
  }
  
  if (totq >0 && totq <48) {
	alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors");
	document.getElementById('q').value = "0"; // or perhaps value = "ERROR!"
	return false;
  }
  
  if (totr >0 && totr <48) {
	alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors");
	document.getElementById('r').value = "0"; // or perhaps value = "ERROR!"
	return false;
  }

/* The code below works if I only check the TotalGift is greater than DonateAmount but will cannot do subtraction to the TotalGift and the entered amount. */

if ((DonateText < TotC)) {
	alert('Your total designated amount of $' + TotC + ' is more than your donation amount of $' + DonateText + '. Please revise your entry to avoid any processing errors.');
	//add if here for all fields focus
	
	document.getElementById('TotalC').value = TotC; // or perhaps value = "ERROR!"
  
	document.getElementById('TotalC').focus();
	return false;
  }

}


Below is what I am trying to do but it doesn't work. If the first input field is selected and user enter amount greater than the TotalGift, alert will pop-up then and the amount entered by the user automatically added to the TotalGift. So what I wanted to do is to subtract the amount entered to the TotalGift so it will go back to the original amount before the addition occurs. I know it is possible but I just can't get the right code. Can you please help? I need to make this done as soon as possible, please!!!!!
if ((DonateText < TotC) && (document.getElementById('a').value)) 
 {
	alert('Your total designated amount of $' + TotC + ' is more than your donation amount of $' + DonateText + '. Please revise your entry to avoid any processing errors.');
	//add if here for all fields focus
	if (TotC = "0.00")
	{
	document.getElementById('a').value=0;
	document.getElementById('TotalC').value = TotC; // or perhaps value = "ERROR!"
	}
	else
	{
	TotC = TotC - document.getElementById('a').value;	
	document.getElementById('tota').value = "0";
	document.getElementById('TotalC').value = TotC; // or perhaps value = "ERROR!"
	document.getElementById('TotalC').focus();
	return false;
	}
  
  } 
  
  if ((DonateText < TotC) && (document.getElementById('b').value)) {
	alert('Your total designated amount of $' + TotC + ' is more than your donation amount of $' + DonateText + '. Please revise your entry to avoid any processing errors.');
	if (TotC = "0.00")
	{
	document.getElementById('b').value=0;
	document.getElementById('TotalC').value = TotC; // or perhaps value = "ERROR!"
	}
	else
	{
	TotC = TotC - document.getElementById('b').value;	
	document.getElementById('totb').value = "0";
	document.getElementById('TotalC').value = TotC; // or perhaps value = "ERROR!"
	document.getElementById('TotalC').focus();
	return false;
	  }
  } /* so on



My html code below which I have 18 input fields but all of them only optional wherever the user wants to input number. I will only include some here. Code below works, please ignore any missing code. I just need the calculation code to work.

<label>Donation Amount: $</label>
		 	<input name="TotalDonate" id="TotalDonate" type="text" maxlength="10" readonly="true" onclick="CalculateTotal(this.value,99);" />
<label for="agencysupport" style="font-weight:bold">C. Agency/Program Support ($48 MINIMUM PER CHOICE)</label>
			<BR />
			<p style="color:#000000">My gift is designated to one or more specific agencies or programs. Processing fee may apply. </p>
			<p style="color:#FF0000">(Note: Some Agency Names may be indented on the next line due to space allowed. Choosing either one of the Agency Names will give you the same code.)</p>
			
		<table>
		  <tr>
			<td>
	   <select id="designation_list1" name="designation_list1" size="1" style="overflow-x:scroll; width:380px; ">
	<option>Click to select...</option>
		 <?php foreach ($designation_list as $descode => $prompt) : ?>
		 <option <?php echo helper_selected($descode == $form['designation_list1']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option>
		 <?php endforeach; ?>
	   </select>
	   
	   </td>
	  <td>
	   $<input id="i" name="i" class="text" type="text" value="<?php safeEcho($form['i'])?>" style="width:90px;" onchange="CalculateTotal()"/>
	   <?php helper_error('i');?> 
	   	</td>
	   </tr>
	   <tr>
	   	 <td>
	   <select id="designation_list2" name="designation_list2" size="1" style="overflow-x:scroll; width:380px; ">
	<option>Click to select...</option>
		 <?php foreach ($designation_list as $descode => $prompt) : ?>
		 <option <?php echo helper_selected($descode == $form['designation_list2']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option>
		 <?php endforeach; ?>
	   </select>
	   
	   </td>
	  <td>
	   $<input id="j" name="j" class="text" type="text" value="<?php safeEcho($form['j'])?>" style="width:90px;" onchange="CalculateTotal()"/>
	   <?php helper_error('j');?> 
	   	</td>
	   </tr>
	   <tr>
			<td>
	   <select id="designation_list3" name="designation_list3" size="1" style="overflow-x:scroll; width:380px; ">
	<option>Click to select...</option>
		 <?php foreach ($designation_list as $descode => $prompt) : ?>
		 <option <?php echo helper_selected($descode == $form['designation_list3']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option>
		 <?php endforeach; ?>
	   </select>
	   </td>
	  <td>
	   $<input id="k" name="k" class="text" type="text" value="<?php safeEcho($form['k'])?>" style="width:90px;" onchange="CalculateTotal()"/>
	   	<?php helper_error('k');?> </td>

	   </tr>
	   <tr>
			<td>
	   <select id="designation_list4" name="designation_list4" size="1" style="overflow-x:scroll; width:380px; ">
	<option>Click to select...</option>
		 <?php foreach ($designation_list as $descode => $prompt) : ?>
		 <option <?php echo helper_selected($descode == $form['designation_list4']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option>
		 <?php endforeach; ?>
	   </select>
	   </td>
	  <td>
	   $<input id="l" name="l" class="text" type="text" value="<?php safeEcho ($form['l'])?>" style="width:90px;" onchange="CalculateTotal()"/>
	   	<?php helper_error('l');?> </td>

	   </tr>
	   <tr>
			<td>
	   <select id="designation_list5" name="designation_list5" size="1" style="overflow-x:scroll; width:380px; ">
	<option>Click to select...</option>
		 <?php foreach ($designation_list as $descode => $prompt) : ?>
		 <option <?php echo helper_selected($descode == $form['designation_list5']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option>
		 <?php endforeach; ?>
	   </select>
	   </td>
	  <td>
	   $<input id="m" name="m" class="text" type="text" value="<?php safeEcho($form['m'])?>" style="width:90px;" onchange="CalculateTotal();"/>
	   	<?php helper_error('m');?> </td>

	   </tr>
	   <tr>
			<td>
	   <select id="designation_list6" name="designation_list6" size="1" style="overflow-x:scroll; width:380px; ">
	<option>Click to select...</option>
		 <?php foreach ($designation_list as $descode => $prompt) : ?>
		 <option <?php echo helper_selected($descode == $form['designation_list6']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option>
		 <?php endforeach; ?>
	   </select>
	   </td>
	  <td>
	   $<input id="n" name="n" class="text" type="text" value="<?php safeEcho($form['n'])?>" style="width:90px;" onchange="CalculateTotal();"/>
	   	<?php helper_error('n');?> </td>

	   </tr>
	   <tr>
			<td>
	   <select id="designation_list7" name="designation_list7" size="1" style="overflow-x:scroll; width:380px; ">
	<option>Click to select...</option>
		 <?php foreach ($designation_list as $descode => $prompt) : ?>
		 <option <?php echo helper_selected($descode == $form['designation_list7']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option>
		 <?php endforeach; ?>
	   </select>
	   </td>
	  <td>
	   $<input id="o" name="o" class="text" type="text" value="<?php safeEcho($form['o'])?>" style="width:90px;" onchange="CalculateTotal();"/>
	   	<?php helper_error('o');?> </td>
	   </tr>
	   <tr>
			<td>
	   <select id="designation_list8" name="designation_list8" size="1" style="overflow-x:scroll; width:380px; ">
	<option>Click to select...</option>
		 <?php foreach ($designation_list as $descode => $prompt) : ?>
		 <option <?php echo helper_selected($descode == $form['designation_list8']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option>
		 <?php endforeach; ?>
	   </select>
	   </td>
	  <td>
	   $<input id="p" name="p" class="text" type="text" value="<?php safeEcho($form['p'])?>" style="width:90px;" onchange="CalculateTotal();"/>
	   	<?php helper_error('p');?> </td>
	   </tr>
	   <tr>
			<td>
	   <select id="designation_list9" name="designation_list9" size="1" style="overflow-x:scroll; width:380px; ">
	<option>Click to select...</option>
		 <?php foreach ($designation_list as $descode => $prompt) : ?>
		 <option <?php echo helper_selected($descode == $form['designation_list9']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option>
		 <?php endforeach; ?>
	   </select>
	   </td>
	  <td>
	   $<input id="q" name="q" class="text" type="text" value="<?php safeEcho($form['q'])?>" style="width:90px;" onchange="CalculateTotal();"/>
	   	<?php helper_error('q');?> </td>
	   </tr>
	   <tr>
			<td>
	   <select id="designation_list10" name="designation_list10" size="1" style="overflow-x:scroll; width:380px; ">
	<option>Click to select...</option>
		 <?php foreach ($designation_list as $descode => $prompt) : ?>
		 <option <?php echo helper_selected($descode == $form['designation_list10']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option>
		 <?php endforeach; ?>
	   </select>
	   </td>
	  <td>
	   $<input id="r" name="r" class="text" type="text" value="<?php safeEcho($form['r'])?>" style="width:90px;" onchange="CalculateTotal();"/>
	   	<?php helper_error('r');?> </td>
	   </tr>
	  
	   <tr>
	   <td style="text-align:right; font-size:14px"><b>TOTAL GIFT(S) DESIGNATED (A-C)</b><BR /> (Must not exceed donation amount)</td>
	   <td>$<input name="TotalC" id="TotalC" type="text" maxlength="10" value="<?php echo $TotalC; ?>" readonly="true" onchange="CalculateTotal(this.value,99);" style="width:90px" /></td>
	   </tr>
	  </table>



Please, I really need to make this work today. Hope you can help me please!!!! Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Help in subtracting totalgift when donateamount condition trigger and

#2 jmarian1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 20-October 09

Re: Help in subtracting totalgift when donateamount condition trigger and

Posted 12 November 2009 - 07:33 PM

HI, I created a for...loop array but my calculations is not right. Please help.
 
var amounts = new Array();

  amounts[0] = Number(document.getElementById('a').value); 
  amounts[1] = Number(document.getElementById('b').value);
  amounts[2] = Number(document.getElementById('c').value);
  amounts[3] = Number(document.getElementById('d').value);
  amounts[4] = Number(document.getElementById('e').value);
  amounts[5] = Number(document.getElementById('f').value);
  amounts[6] = Number(document.getElementById('g').value);
  amounts[7] = Number(document.getElementById('h').value);
  amounts[8] = Number(document.getElementById('i').value);
  amounts[9] = Number(document.getElementById('j').value);
  amounts[10] = Number(document.getElementById('k').value);
  amounts[11] = Number(document.getElementById('l').value);
  amounts[12] = Number(document.getElementById('m').value);
  amounts[13] = Number(document.getElementById('n').value);
  amounts[14] = Number(document.getElementById('o').value);
  amounts[15] = Number(document.getElementById('p').value);
  amounts[16] = Number(document.getElementById('q').value);
  amounts[17] = Number(document.getElementById('r').value);


//my function

for (i=0; i<18; i++) 
  
		{
		TotC = Number(document.getElementById('TotalC').value);
		  TotC = TotC + amounts[i];
		if (TotC <= DonateText) 
			{
			  document.getElementById('TotalC').value = TotC.toFixed(2);
			}
			else
			{
				if (TotC = "0.00")
				{
				alert('Your total designated amount of $' + amounts[i] + ' is more than your donation amount of $' + DonateText + '. Please revise your entry to avoid any processing errors.');
				document.getElementById.amounts[i]=0;
				document.getElementById('TotalC').value = "0.00"; // or perhaps value = "ERROR!"
				
				}
				else
				{
				alert('Your total designated amount of $' + document.getElementById('TotalC').value + ' is more than your donation amount of $' + DonateText + '. Please revise your entry to avoid any processing errors.');
				TotC = TotC - amounts[i];
				document.getElementById('TotalC').value = TotC;
				amounts[i] = 0;
				}
				
				
			}


		}




PLease help!!!!!! Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 forest51690   User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: Help in subtracting totalgift when donateamount condition trigger and

Posted 15 November 2009 - 12:54 PM

Can you post the full HTML code and Javascript. Fist load the page in your browser, and then right-click and select view source. Then copy and paste that. That would make it easier for us to help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1