// ======================================================================================================
// create by webmaster - www.EdaranSalindah.com
//                       abdullah bin ismail, +6019.7624600
// ======================================================================================================
//
// JavaScript function:
// parseFloat  - convert string into floating point number
// parseInt    - convert string into integer
//
function CalculateTotal(frm) {

  // Nested if-else, function like CASE
  if (frm.negeri.selectedIndex == 10) {
      // 10 - Sabah
      rate_sabahsarawak(frm)
   } else {
     if (frm.negeri.selectedIndex == 11) {
        // 11 - Sarawak 
        rate_sabahsarawak(frm)
     } else {
       if (frm.negeri.selectedIndex == 15) {
          // 15 - W.P Labuan
          rate_sabahsarawak(frm)         
        } else {
          rate_semenanjung(frm)      
        }
     }
   }

}



// ======================================================================================================
// delivery cost for Sabah & Sarawak
// ======================================================================================================
function rate_sabahsarawak(frm) {
    var order_total = 0
    var sum_order = 0
    var sum_charged = 0

    var berat_charged = 0
    var berat_item = 0
    var berat_quantity = 0
    var berat_adjustment = 0 


    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {
        // Get the current field
        form_field = frm.elements[i]

        // Get the field's name
        form_name = form_field.name
         
        // PART 1: 
        // Is it a "product" field?
        if (form_name.substring(0,4) == "PROD") {
            // If so, extract the price from the name
            item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }

            if (item_quantity > 0) {
               // 13.08.2007  
               // berat_adjustment = 501.0 
               berat_adjustment = 0 

            }
                           

        }
        
        // PART 2:       
        // sum weight here -  SINI
        // Is it a "weight" field?
        if (form_name.substring(0,5) == "BERAT") {
            // If so, extract the price from the name
            berat_item = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

            // Get the quantity
            berat_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                berat_charged += berat_item * item_quantity  // berat_quantity - field ini dummy saja 
            }
            
                frm.TOTALBERAT.value = round_decimals(berat_charged, 2)
        }    
        
    
    }

    // PART 3:   
    // apply adjustment utk berat keseluruhan (produk + kotak), jika berat melebihi 1.5kg
    //if (berat_charged >= 1500) {
       berat_charged += berat_adjustment
       frm.TOTALBERAT.value = round_decimals(berat_charged, 2)
    //}



    // Display the total rounded to two decimal places
    frm.TOTAL.value = round_decimals(order_total, 2)
    

    // add by webmaster - www.EdaranSalindah.com

    //------ PENENTUAN KOS PENGHANTARAN -------------
    // TOTALBERAT in gram (g) for Semenanjung Malaysia
    
    if (frm.TOTALBERAT.value <= 0.00) {
       sum_charged = 0.0
    }
    if ((frm.TOTALBERAT.value > 0.00) && (frm.TOTALBERAT.value <= 500.00)) {    
       sum_charged = 9.0
    } 
    if ((frm.TOTALBERAT.value > 500.00) && (frm.TOTALBERAT.value <= 1000.00)) {
       sum_charged = 13.0
    }
    if ((frm.TOTALBERAT.value > 1000.00) && (frm.TOTALBERAT.value <= 2000.00)) {
       sum_charged = 20.0
    } 
    if ((frm.TOTALBERAT.value > 2000.00) && (frm.TOTALBERAT.value <= 3000.00)) {
       sum_charged = 33.0
    }
    if ((frm.TOTALBERAT.value > 3000.00) && (frm.TOTALBERAT.value <= 4000.00)) {
       sum_charged = 41.0
    }
    if ((frm.TOTALBERAT.value > 4000.00) && (frm.TOTALBERAT.value <= 5000.00)) {
       sum_charged = 47.0
    }
    if ((frm.TOTALBERAT.value > 5000.00) && (frm.TOTALBERAT.value <= 6000.00)) {
       sum_charged = 56.0
    }
    if ((frm.TOTALBERAT.value > 6000.00) && (frm.TOTALBERAT.value <= 7000.00)) {
       sum_charged = 62.0
    }
    if ((frm.TOTALBERAT.value > 7000.00) && (frm.TOTALBERAT.value <= 8000.00)) {
       sum_charged = 69.0
    }
    if ((frm.TOTALBERAT.value > 8000.00) && (frm.TOTALBERAT.value <= 9000.00)) {
       sum_charged = 76.0
    }
    if ((frm.TOTALBERAT.value > 9000.00) && (frm.TOTALBERAT.value <= 10000.00)) {
       sum_charged = 84.0
    }
    if (frm.TOTALBERAT.value > 10000.00) {
        // RM 6.90/kg bagi berat lebih 10,000 g (10kg)
        sum_charged  = 84.0
        baki_berat   = frm.TOTALBERAT.value - 10000.00
        faktor_berat = baki_berat / 1000.0
        sum_charged  = sum_charged  + (faktor_berat * 6.9)         
    }
    

    frm.DELIVERYCOST.value = round_decimals(sum_charged, 2)
   
    
    //------ PENENTUAN KOS PENGHANTARAN -------------


    // PART 4:   
    // grand total
    if (frm.TOTAL.value >= 1000000.00) { 
       // kalau order >= RM1,000,000.00, kurier free  
       frm.DISKAUN.value    = frm.DELIVERYCOST.value //parseInt(frm.DELIVERYCOST.value) 
       sum_order            = parseFloat(frm.TOTAL.value)  
       frm.GRANDTOTAL.value = round_decimals(sum_order, 2) 
    } else {
       frm.DISKAUN.value    = round_decimals(0.00, 2)
       sum_order            = parseFloat(frm.TOTAL.value) + parseFloat(frm.DELIVERYCOST.value)
       frm.GRANDTOTAL.value = round_decimals(sum_order, 2) 
    }


}




// ======================================================================================================
// delivery cost for Semenanjung Malaysia
// ======================================================================================================
function rate_semenanjung(frm) {
    var order_total = 0
    var sum_order = 0
    var sum_charged = 0

    var berat_charged = 0
    var berat_item = 0
    var berat_quantity = 0
    var berat_adjustment = 0 


    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {
        // Get the current field
        form_field = frm.elements[i]

        // Get the field's name
        form_name = form_field.name
         
        // PART 1: 
        // Is it a "product" field?
        if (form_name.substring(0,4) == "PROD") {
            // If so, extract the price from the name
            item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }


            if (item_quantity > 0) {
               // 13.08.2007 
               // berat_adjustment = 501.0 
               berat_adjustment = 0 
            }
                           

        }
        
        // PART 2:       
        // sum weight here -  SINI
        // Is it a "weight" field?
        if (form_name.substring(0,5) == "BERAT") {
            // If so, extract the price from the name
            berat_item = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

            // Get the quantity
            berat_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                berat_charged += berat_item * item_quantity  // berat_quantity - field ini dummy saja 
            }
            
                frm.TOTALBERAT.value = round_decimals(berat_charged, 2)
        }    
        
    
    }

    // PART 3:   
    // apply adjustment utk berat keseluruhan (produk + kotak), jika berat melebihi 1.5kg
    //if (berat_charged >= 1500) {
       berat_charged += berat_adjustment
       frm.TOTALBERAT.value = round_decimals(berat_charged, 2)
    //}



    // Display the total rounded to two decimal places
    frm.TOTAL.value = round_decimals(order_total, 2)
    

    // add by webmaster - www.EdaranSalindah.com

    //------ PENENTUAN KOS PENGHANTARAN -------------
    // TOTALBERAT in gram (g) for Semenanjung Malaysia
    
    if (frm.TOTALBERAT.value <= 0.00) {
       sum_charged = 0.0
    }
    if ((frm.TOTALBERAT.value > 0.00) && (frm.TOTALBERAT.value <= 500.00)) {    
       sum_charged = 6.0
    } 
    if ((frm.TOTALBERAT.value > 500.00) && (frm.TOTALBERAT.value <= 1000.00)) {
       sum_charged = 8.0
    }
    if ((frm.TOTALBERAT.value > 1000.00) && (frm.TOTALBERAT.value <= 2000.00)) {
       sum_charged = 8.0
    } 
    if ((frm.TOTALBERAT.value > 2000.00) && (frm.TOTALBERAT.value <= 3000.00)) {
       sum_charged = 10.0
    }
    if ((frm.TOTALBERAT.value > 3000.00) && (frm.TOTALBERAT.value <= 4000.00)) {
       sum_charged = 11.0
    }
    if ((frm.TOTALBERAT.value > 4000.00) && (frm.TOTALBERAT.value <= 5000.00)) {
       sum_charged = 13.0
    }
    if ((frm.TOTALBERAT.value > 5000.00) && (frm.TOTALBERAT.value <= 6000.00)) {
       sum_charged = 14.0
    }
    if ((frm.TOTALBERAT.value > 6000.00) && (frm.TOTALBERAT.value <= 7000.00)) {
       sum_charged = 16.0
    }
    if ((frm.TOTALBERAT.value > 7000.00) && (frm.TOTALBERAT.value <= 8000.00)) {
       sum_charged = 18.0
    }
    if ((frm.TOTALBERAT.value > 8000.00) && (frm.TOTALBERAT.value <= 9000.00)) {
       sum_charged = 19.0
    }
    if ((frm.TOTALBERAT.value > 9000.00) && (frm.TOTALBERAT.value <= 10000.00)) {
       sum_charged = 21.0
    }
    if (frm.TOTALBERAT.value > 10000.00) {
        // RM 1.50/kg bagi berat lebih 10,000 g (10kg)
        sum_charged  = 21.0
        baki_berat   = frm.TOTALBERAT.value - 10000.00
        faktor_berat = baki_berat / 1000.0
        sum_charged  = sum_charged  + (faktor_berat * 1.5)         
    }
    

    frm.DELIVERYCOST.value = round_decimals(sum_charged, 2)
   
    
    //------ PENENTUAN KOS PENGHANTARAN -------------


    // PART 4:   
    // grand total
    if (frm.TOTAL.value >= 1000000.00) { 
       // kalau order >= RM1,000,000.00, kurier free  
       frm.DISKAUN.value    = frm.DELIVERYCOST.value //parseInt(frm.DELIVERYCOST.value) 
       sum_order            = parseFloat(frm.TOTAL.value)  
       frm.GRANDTOTAL.value = round_decimals(sum_order, 2) 
    } else {
       frm.DISKAUN.value    = round_decimals(0.00, 2)
       sum_order            = parseFloat(frm.TOTAL.value) + parseFloat(frm.DELIVERYCOST.value)
       frm.GRANDTOTAL.value = round_decimals(sum_order, 2) 
    }

}


// ======================================================================================================
// Round decimal
// ======================================================================================================
function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}



// ======================================================================================================
// Pad with zeros
// ======================================================================================================
function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()

  

    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {

     
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
       

        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1

    }
   

    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
  

    if (pad_total > 0) {
    
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

//-->
