/*
	CHECK_ORDER.JS
	The following script is based on an original script 
	written by Paul McFedries and Logophilia Limited, 
	and used with permission.

	-------------------------------------------------------------------------------------
	Used with permission of the author and modified by Website Expressions Pty Ltd (2005)
	-------------------------------------------------------------------------------------
	
	ABN:	  87 108 587 131
	Internet: www.websiteexpressions.com
	Email:	  mail@websiteexpressions.com
	Phone:	  +61 2 4948 1555
	Fax:	  +61 2 4948 1666

	Version 1.0	
	09/07/2005
					
*/	


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

    // 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
        field_name = form_field.name

        // Is it a "qty" field?
        if (field_name.substring(0,3) == "qty") {
        
            // If so, extract the price from the name
            item_price = parseFloat(field_name.substring(field_name.lastIndexOf("_") + 1))
            
            // Get the quantity
            item_quantity = parseInt(form_field.value)

	        // Get the qty field and item number
	        qty_field = form_field
	        qty_item_number = field_name.substring(3, field_name.indexOf("_"))

            // Find the corresponding item total field
		    for (var j=0; j < frm.elements.length; ++j) {

		        // Get the current field
		        form_field = frm.elements[j]

		        // Get the field's name
		        field_name = form_field.name

		        // Get the total field's item number
		        total_item_number = field_name.substring(5);

		        // Is it the matching total field?
		        if (field_name.substring(0,5) == "total" && qty_item_number == total_item_number) {

		            // If so, update the item total
		            if (item_quantity >= 0 
		            	&& item_quantity <100 
		            	&& qty_field.value.indexOf(".") < 0
		            	&& isFinite(qty_field.value)) { 
		                item_total = item_quantity * item_price
		                
					    // Display the total rounded to two decimal places
					    form_field.value = round_decimals(item_total, 2)
		            } else {
					    // Clear the item qty and item total 
					    qty_field.value = ""
					    form_field.value = ""
		            }
		        }
	            
			}

            // Update the order total
            if (item_quantity >= 0) { 
                order_total += item_quantity * item_price
            }
        }
    }
    
	// Add freight to the order total
	freight = frm.freight.value
	freight_amount = parseFloat(freight)
	if (freight_amount >= 0) {
	    order_total += freight_amount
	}

    // Display the total rounded to two decimal places
    frm.order_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
}

function AddFreight(frm) {

	// Get the freight
	freight = frm.freight.value;
	freight_amount = parseFloat(freight)
	
	// Check for valid format
	if (freight_amount >= 0 
	&& freight_amount <1000 
	&& isFinite(freight)) { 
	
		// Allow a maximum of 2 decimal places
		dotpos = freight.indexOf(".")
		if (dotpos >= 0) {
			decimals = freight.substring(dotpos + 1)
			if (decimals.length > 2) {
				frm.freight.value = freight.substring(0,freight.length - 1)
			}	
		}	
			
	} else {
	
		// Clear the freight 
		frm.freight.value = ""
	}

	// Update the order total
	CalculateTotal(frm)  
}

function submitOrderCheck(frm)
{
	if(frm.cust_name.value &&
	   frm.address.value &&
	   frm.suburb.value &&
	   frm.state.value &&
	   frm.postcode.value) {
		// Allow the order to proceed
		return true;
	} else {
		// Stop the order from proceeding
		alert("Please enter your details      ");
		return false;
	}	
}