/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/

function CalculateTotal(frm) {
    var order_total = 0
	//flyers
	var flyerprice = 0
	var flyerqty = frm.flyersqty.value;
	//next set - brochures
	var brochprice = 0
	var brochqty = frm.brochuresqty.value;
	//next set - Catalogs
	var catalogprice = 0
	var catalogqty = frm.catalogsqty.value;	 
		
//case stmts for diff. values for FLYERS
// 1-99, 100 - 249,250-500,500-1000
if (document.PrintRequest.flyertype[0].checked==true) {
 if (flyerqty > 0 && flyerqty < 100) {
 flyerprice = 0.44 
 }
  if (flyerqty > 99 && flyerqty < 250) {
 flyerprice = 0.39 
 }
  if (flyerqty > 249 && flyerqty < 500) {
 flyerprice = 0.36 
 }
  if (flyerqty > 499 && flyerqty < 1000) {
 flyerprice = 0.29 
 }
 var flyersubt = flyerprice * flyerqty
 //return flyersubt;
 }//end if
else if (document.PrintRequest.flyertype[1].checked==true) {
 if (flyerqty > 0 && flyerqty < 100) {
 flyerprice = 0.68 
 }
  if (flyerqty > 99 && flyerqty < 250) {
 flyerprice = 0.58 
 }
  if (flyerqty > 249 && flyerqty < 500) {
 flyerprice = 0.54 
 }
  if (flyerqty > 499 && flyerqty < 1000) {
 flyerprice = 0.43 
 }
  var flyersubt = flyerprice * flyerqty
 } //end else if
 else if (!(document.PrintRequest.flyertype[0].checked==true || document.PrintRequest.flyertype[1].checked==true)) { 
 flyerprice = 0
 flyerqty = 0
 var flyersubt = flyerprice * flyerqty
 }
 //********************** next part of total func
 //case stmts for diff. values for BROCHURES
// 1-99, 100 - 249,250-500,500-1000
if (document.PrintRequest.brochuretype[0].checked==true) {
 if (brochqty > 0 && brochqty < 100) {
 brochprice = 0.71 
 }
  if (brochqty > 99 && brochqty < 250) {
 brochprice = 0.63 
 }
  if (brochqty > 249 && brochqty < 500) {
 brochprice = 0.59 
 }
  if (brochqty > 499 && brochqty < 1000) {
 brochprice = 0.48 
 }
 var brochsubt = brochprice * brochqty
 //return flyersubt;
 }//end if
else if (document.PrintRequest.brochuretype[1].checked==true) {
 if (brochqty > 0 && brochqty < 100) {
 brochprice = 0.73 
 }
  if (brochqty > 99 && brochqty < 250) {
 brochprice = 0.65
 }
  if (brochqty > 249 && brochqty < 500) {
 brochprice = 0.61 
 }
  if (brochqty > 499 && brochqty < 1000) {
 brochprice = 0.50
 }
  var brochsubt = brochprice * brochqty

 } //end else if
  else if (!(document.PrintRequest.brochuretype[0].checked==true || document.PrintRequest.brochuretype[1].checked==true)) { 
 brochprice = 0
 brochqty = 0
  var brochsubt = brochprice * brochqty
 }//end 2nd else if
  //********************** next part of total func
 //case stmts for diff. values for CATALOGS
// 1-99, 100 - 249,250-500,500-1000
if (document.PrintRequest.catalogtype[0].checked==true) {//4 pg
 if (catalogqty > 0 && catalogqty < 100) {
 catalogprice = 1.76 
 }
  if (catalogqty > 99 && catalogqty < 250) {
 catalogprice = 1.56 
 }
  if (catalogqty > 249 && catalogqty < 500) {
 catalogprice = 1.44 
 }
  if (catalogqty > 499 && catalogqty < 1000) {
 catalogprice = 1.16
 }
 var catalogsubt = catalogprice * catalogqty
 //return flyersubt;
 }//end if
else if (document.PrintRequest.catalogtype[1].checked==true) {//8 pg cat
 if (catalogqty > 0 && catalogqty < 100) {
 catalogprice = 2.64
 }
  if (catalogqty > 99 && catalogqty < 250) {
 catalogprice = 2.32 
 }
  if (catalogqty > 249 && catalogqty < 500) {
 catalogprice = 2.16 
 }
  if (catalogqty > 499 && catalogqty < 1000) {
 catalogprice = 1.72
 }
 var catalogsubt = catalogprice * catalogqty
 //return flyersubt;
 } //end else if
 else if (document.PrintRequest.catalogtype[2].checked==true) {//12 pg cat
 if (catalogqty > 0 && catalogqty < 100) {
 catalogprice = 3.52
 }
  if (catalogqty > 99 && catalogqty < 250) {
 catalogprice = 3.08 
 }
  if (catalogqty > 249 && catalogqty < 500) {
 catalogprice = 2.88 
 }
  if (catalogqty > 499 && catalogqty < 1000) {
 catalogprice = 2.88
 }
 var catalogsubt = catalogprice * catalogqty
 //return flyersubt;
 } //end else if
  else if (document.PrintRequest.catalogtype[3].checked==true) {//16 pg cat
 if (catalogqty > 0 && catalogqty < 100) {
 catalogprice = 4.40
 }
  if (catalogqty > 99 && catalogqty < 250) {
 catalogprice = 3.84 
 }
  if (catalogqty > 249 && catalogqty < 500) {
 catalogprice = 3.60 
 }
  if (catalogqty > 499 && catalogqty < 1000) {
 catalogprice = 2.84 
 }
 var catalogsubt = catalogprice * catalogqty
 //return flyersubt;
 } //end else if of catalogs checked - below for NONE checked
   else if (!(document.PrintRequest.catalogtype[0].checked==true || document.PrintRequest.catalogtype[1].checked==true || document.PrintRequest.catalogtype[2].checked==true || document.PrintRequest.catalogtype[3].checked==true)) { 
 catalogprice = 0
 catalogqty = 0
 var catalogsubt = catalogprice * catalogqty
 }
						/* DID NOT work at all!!
						switch (flyerfr){
						case (flyerqty > 0 && flyerqty < 100): 
						flyerfr = 0.44 
						alert("Value is "+ flyerfr)
						break;
						case flyerqty > 100 && flyerqty < 250: 
						flyerfr = 0.39
						alert("Value is - "+ flyerfr)
						break;
						case flyerqty > 249 && flyerqty < 500: 
						flyerfr = 0.36
						alert("Value is... "+ flyerfr)
						break;
						case flyerqty > 500 && flyerqty < 1000: 
						flyerfr = 0.29
						alert("It has its moments"+flyerfr)
						break;
						}
						*/
  var result=Math.round(flyersubt*100)/100  //returns 28.45
 //return flyersubt;
 // var totresult  
	 //if (!brochsubt==null) {} 
  var result2 =Math.round(brochsubt*100)/100  //returns 28.45
  var result3 =Math.round(catalogsubt*100)/100  //returns 28.45
 // result += result2
 // result += result3
	result = result + result2 + result3
  frm.TotalDue.value =  Math.round(result*100)/100

return;
  //round_decimals(totresult, 2)

}
/*
    // 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

        // 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
            }
        }
    }

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

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)
}

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
}*/

//-->
