Javascript Help: change the way this form calculates one of the Fields

Discussion in 'Coding Help' started by darrenhallinan, Aug 6, 2010.

  1. darrenhallinan

    darrenhallinan New Member

    Hi All,

    I'm having an issue trying to change the way this form calculates one of the Fields

    Ill attach code below and would like to know what to change.

    Background: the form is used to calculate the price of a package, the current calculation just adds 20 euro to the price of the package for each person that requires single occupancy. But I need to change it that it adds 20 euro per night per person.

    So Current Calculation is: QtyItem1 * PriceItem1 * QtyItem2 = TotalItem1

    But I need to change it to: QtyItem1 * PriceItem1 * PartNumItem3 = TotalItem1



    Below is the Fields used to do the calculation

    QtyItem1= Number Of Persons That require single ocupancy

    PartNumItem3= Number Of Nights

    QtyItem2= Hidden - Field with a value of one

    PriceItem1= Hidden - Price for single occupancy

    Any Help Much Apprecated

    This is the script i am using
    Code:
    <script type="text/javascript">
        elementExtend();
        window.addEvent('domready', function() {    
            });
    </script>                <script type='text/javascript'>
    //<![CDATA[
    function popup(mylink, windowname) {
        if (! window.focus)return true;
        var href;
        if (typeof(mylink) == 'string') {
               href=mylink;
        } else {
               href=mylink.href;
            window.open(href, windowname, 'width=400,height=600,scrollbars=yes');
            return false;
        }
    }
    
    
    function submitform()
    {
      document.ChronoContact_packagescript.submit();
    }
    
    function NoNegDec(nbr) {
    var Integer = Math.abs(Math.round(nbr));
    var Positive = Math.abs(Integer);
    return(Positive);
    }
    
    function forceright(field,length) {
        var str = "" + field;
        while (str.length < length)
            str = " " + str;
        return str;
    }
    
    function CurrencyDisplay(num,length2) {
    var fieldlength = length2;
    var displayedVersion = (Math.round(num * 100) / 100);
    if (displayedVersion == Math.round(num))
    {
    displayedVersion = displayedVersion + ".00";
    }
    else if ((10 * displayedVersion) == Math.round(num * 10))
    {
    displayedVersion = displayedVersion + "0";
    }
    return(forceright(displayedVersion,fieldlength));
    }
    
    function computeTotal(form) {
    
    if (form.DescItem3.value == '3Star_Hotel_Accommodation')
    {
    var Item3Desc = new Array("3Star Hotel Accommodation", 299, "3night", "Sales:Package Sales");
    }
    
    if (form.DescItem3.value == '4Star_Hotel_Accommodation')
    {
    var Item3Desc = new Array("4Star Hotel Accommodation", 399, "3night", "Sales:PackageSales");
    }
    
    if (form.DescItem3.value == '4Star_Lodgeing_Accommodation')
    {
    var Item3Desc = new Array("4Star Lodge Accommodation", 529, "3night", "Sales:Camera Sales");
    }
    
    document.ChronoContact_packagescript.PriceItem1.value = CurrencyDisplay(20,7);
    document.ChronoContact_packagescript.PriceItem3.value = CurrencyDisplay(Item3Desc[1],7);
    document.ChronoContact_packagescript.PartNumItem3.value = Item3Desc[2];
    document.ChronoContact_packagescript.SalesAcctItem3.value = Item3Desc[3];
    
    var SubT = 0;
    
        var Qty_Item1 = NoNegDec(document.ChronoContact_packagescript.QtyItem1.value);
        var Item1_Price = document.ChronoContact_packagescript.PriceItem1.value;
        var Qty_Item2 = NoNegDec(document.ChronoContact_packagescript.QtyItem2.value);
        var Total_Item1 = (Qty_Item1 * Item1_Price * Qty_Item2);
        SubT = SubT + Total_Item1;
    
        var Qty_Item3 = NoNegDec(document.ChronoContact_packagescript.QtyItem3.value);
        var Item3_Price = document.ChronoContact_packagescript.PriceItem3.value;
        var Total_Item3 = (Qty_Item3 * Item3_Price);
        SubT = SubT + Total_Item3;
    
        document.ChronoContact_packagescript.QtyItem1.value = Qty_Item1;
        document.ChronoContact_packagescript.PriceItem1.value = CurrencyDisplay(Item1_Price,7);
        document.ChronoContact_packagescript.TotalItem1.value = CurrencyDisplay(Total_Item1,12);
    
        document.ChronoContact_packagescript.QtyItem3.value = Qty_Item3;
        document.ChronoContact_packagescript.PriceItem3.value = CurrencyDisplay(Item3_Price,7);
        document.ChronoContact_packagescript.TotalItem3.value = CurrencyDisplay(Total_Item3,12);
        
    
    document.ChronoContact_packagescript.SubTotal.value = CurrencyDisplay(SubT,12);
    }//]]>
    </script>

    and this is the form

    HTML:
        <form name="ChronoContact_packagescript" id="ChronoContact_packagescript" method="post" >
            
                    <h2>Group & Package Details</h2>
     <h3> Group Details </h3>  
    <div class="form_item">
      <div class="form_element cf_datetimepicker">
        <label class="cf_label" style="width: 120px;">Arrival date of your group: </label>
    
        <input  class="cf_datetime required" title="" size="16" id="arrival" name="arrival" type="text" />
        
      </div>
       <div class="cfclear">&nbsp;</div>
    </div>
    
    <div class="form_item">
    <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 120px;">Number of persons in your group: </label>
    
    <select id="QtyItem3" name="QtyItem3" size="1" onchange="computeTotal(this.form);" onblur="computeTotal(this.form);" />
    <option value="">?</option>      
          <option value="6">06</option>
    
           <option value="7">07</option>
          <option value="8">08</option>
          <option value="9">09</option>
          <option value="10">10</option>
          <option value="11">11</option>
           <option value="12">12</option>
    
          <option value="13">13</option>
          <option value="14">14</option>
          <option value="15">15</option>
          <option value="16">16</option>
           <option value="17">17</option>
          <option value="18">18</option>
    
          <option value="19">19</option>
          <option value="20">20</option>
        </select> 
      </div>
       <div class="cfclear">&nbsp;</div>
    </div>
    <input type="hidden" name="QtyItem2" style="text-align: center" maxlength="3" size="10" value="1" onChange="computeTotal(this.form);" onBlur="computeTotal(this.form);" />
    
    <div class="form_item">
    <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 120px;">How Many Persons Require Single Occupancy: </label>
    
    <input type="text" name="QtyItem1" style="text-align: center" maxlength="3" size="3" onchange="computeTotal(this.form);" onblur="computeTotal(this.form);" />
    </div>   
    <div class="cfclear">&nbsp;</div>
    </div>
    <input type="hidden" name="PriceItem1" maxlength="10" size="7" value="20" />
    <h3> Choose Package </h3>
     
    <div class="form_item">
    <label class="cf_label" style="width: 120px;">Choose Package Type: </label>
       <select name="DescItem3" size="1" onchange="computeTotal(this.form);">
    
          <option value="">Choose Option</option>        
      <option value="3Star_Hotel_Accommodation">3Star Hotel Accommodation</option>
    
    <option value="4Star_Hotel_Accommodation">4Star Hotel Accommodation</option>
    <option value="4Star_Lodgeing_Accommodation">4Star Lodge Accommodation</option>
       </select>
     </div>   
    <div class="cfclear">&nbsp;</div>
    </div>
    
     <p></p>Price For Package: 
       <input type="text" name="PriceItem3" maxlength="7" size="7" onfocus="this.blur();" />
       (Per Person Sharing)   <br />
    
        <input type="hidden" name="PartNumItem3" />
        <input type="hidden" name="SalesAcctItem3" />
    
       <table border="0" width="343" cellpadding="0">
    <tr><td align="center" colspan="3"></td></tr>
    
    <tr>
    <td width="66" align="center"></td>
    
    <td colspan="2" align="center"></td>
    </tr>
    
    
    <tr>
      <td align="center">&nbsp;</td>
      <td width="168" align="center"><div align="right">Single Surplus
        <input type="hidden" name="PriceItem1" maxlength="10" size="7" value="20" />
      </div></td>
      <td width="124" align="center">&euro;
        <input type="text" name="TotalItem1" maxlength="12" size="8" onChange="computeTotal(this.form);" /></td>
      </tr>
    
    <tr>
      <td align="center">&nbsp;</td>
    
      <td align="center"><div align="right">Total Package</div></td>
      <td align="center">&euro;
        <input type="text" name="TotalItem3" maxlength="8" size="8" onChange="computeTotal(this.form);" /></td>
      </tr>
    <tr>
      <td colspan="2" align="center"><div align="right">Total Tour Cost:</div></td>
      <td align="center">€
        <input type="text" name="SubTotal" maxlength="8" size="8" onChange="computeTotal(this.form);" />
    
      </td>
      </tr>
    </table>
    
    
    
      <div class="form_item">
      <div class="form_element cf_button">
        <input value="Next Step" name="button_2" type="submit" />
      </div>  
      <div class="cfclear">&nbsp;</div>        <input type="hidden" name="01a72772233d6af41cd5d9f4c4a82ba3" value="1" />    
                        <input type="hidden" name="1cf1" value="add8b479bcf32866fda39877c47e37f2" />
    
                    </form>
    
    
     
  2. Little Pig

    Little Pig New Member

    Try this:

    <script type='text/javascript'>

    function submitform()
    {
    document.ChronoContact_packagescript.submit();
    }

    function NoNegDec(nbr) {
    var Integer = Math.abs(Math.round(nbr));
    var Positive = Math.abs(Integer);
    return(Positive);
    }

    function forceright(field,length) {
    var str = "" + field;
    while (str.length < length)
    str = " " + str;
    return str;
    }

    function CurrencyDisplay(num,length2) {
    var fieldlength = length2;
    var displayedVersion = (Math.round(num * 100) / 100);
    if (displayedVersion == Math.round(num))
    {
    displayedVersion = displayedVersion + ".00";
    }
    else if ((10 * displayedVersion) == Math.round(num * 10))
    {
    displayedVersion = displayedVersion + "0";
    }
    return(forceright(displayedVersion,fieldlength));
    }

    function computeTotal(form) {

    if (form.DescItem3.value == '3Star_Hotel_Accommodation')
    {
    var Item3Desc = new Array("3Star Hotel Accommodation", 299, "3night", "Sales:package Sales");
    }

    if (form.DescItem3.value == '4Star_Hotel_Accommodation')
    {
    var Item3Desc = new Array("4Star Hotel Accommodation", 399, "3night", "Sales:packageSales");
    }

    if (form.DescItem3.value == '4Star_Lodgeing_Accommodation')
    {
    var Item3Desc = new Array("4Star Lodge Accommodation", 529, "3night", "Sales:Camera Sales");
    }
    if (typeof Item3Desc != 'undefined') {
    // use varName

    document.ChronoContact_packagescript.PriceItem1.value = CurrencyDisplay(20,7);
    document.ChronoContact_packagescript.PriceItem3.value = CurrencyDisplay(Item3Desc[1],7);
    var PartNumItem3 = parseInt(Item3Desc[2]);
    document.ChronoContact_packagescript.SalesAcctItem3.value = Item3Desc[3];

    var SubT = 0;

    var Qty_Item1 = NoNegDec(document.ChronoContact_packagescript.QtyItem1.value);
    var Item1_Price = document.ChronoContact_packagescript.PriceItem1.value;
    var Qty_Item2 = NoNegDec(document.ChronoContact_packagescript.QtyItem2.value);
    var Total_Item1 = (Qty_Item1 * Item1_Price * PartNumItem3);
    SubT = SubT + Total_Item1;



    var Qty_Item3 = NoNegDec(document.ChronoContact_packagescript.QtyItem3.value);
    var Item3_Price = document.ChronoContact_packagescript.PriceItem3.value;
    var Total_Item3 = (Qty_Item3 * Item3_Price);
    SubT = SubT + Total_Item3;

    document.ChronoContact_packagescript.QtyItem1.value = Qty_Item1;
    document.ChronoContact_packagescript.PriceItem1.value = CurrencyDisplay(Item1_Price,7);
    document.ChronoContact_packagescript.TotalItem1.value = CurrencyDisplay(Total_Item1,12);

    document.ChronoContact_packagescript.QtyItem3.value = Qty_Item3;
    document.ChronoContact_packagescript.PriceItem3.value = CurrencyDisplay(Item3_Price,7);
    document.ChronoContact_packagescript.TotalItem3.value = CurrencyDisplay(Total_Item3,12);


    document.ChronoContact_packagescript.SubTotal.value = CurrencyDisplay(SubT,12);
    }
    }//]]>
    </script>
     

Share This Page