/* 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 = 6.95
	var field1 = document.getElementById('field1').value;
	var field3 = document.getElementById('field3').value;
	
	var field2 = document.getElementById('field2');
	var field4 = document.getElementById('field4');

	field2.value = (field1 * 23.50).toFixed(2);
	field4.value = (field3 * 20.95).toFixed(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, know the price from the name
			if (form_name.substring(4,8) == "urif") {
				
				item_price = 23.50;
			
			} else if (form_name.substring(4,8) == "urim") {
				
				item_price = 20.95;
			}

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


function checkNone() {

var field1 = document.getElementById('field1');
var field3 = document.getElementById('field3');
var field4 = document.getElementById('field4');
var message = document.getElementById('message');
var message2 = document.getElementById('message2');

	var x = field1.value == 0;
	var y = field3.value == 0;
	
if (field1.value == "" ) {
	
	message.style.display = "inline";
	message2.style.display = "none";
	field1.style.borderColor = "red";
	field1.style.borderWidth = "2px";

	
} if (field3.value == "" ) {
	
	message.style.display = "inline";
	message2.style.display = "none";
	field3.style.borderColor = "red";
	field3.style.borderWidth = "2px";
	return false
	
	} else if (x && y) {
	
	message2.style.display = "inline";
	return false
	
	} else {
		field1.style.borderColor = "";
		field1.style.borderWidth = "";
		field3.style.borderColor = "";
		field3.style.borderWidth = "";
		message.style.display = "none";
		message2.style.display = "none";
		return true
		
	} 
	
}

function fillFields() {
	
	var field1 = document.getElementById('field1');
	var field2 = document.getElementById('field2');
	var field3 = document.getElementById('field3');
	var field4 = document.getElementById('field4');
	
	if (field1.value == "") {
	
	field1.value = 0;
	} 
	
	else if (field3.value == "") {
	
	field3.value = 0;
	}
	
}






	

	
	
	
