/*************************************************************
 **
 ** Depends on: ruby.js, dhtml.js, tools.js, prototype.js
 ************************************************************/
var SimpleEcommerce = new Object();

SimpleEcommerce.UpdateOrderTotal = function ( orderFrm ) {
    
    // Get list of order forms
    var orderForms = null;
    if( orderFrm )
	orderForms = [ orderFrm ];
    else
	orderForms = document.getElementsByClassName( 'se-order-form' );
    
    // Loop through each order form
    orderForms.each( function (orderFrm) {

	// Find quantity inputs
	var qtys = $A( document.getElementsByClassName( 'se-qty', orderFrm ) );
	
	// Calculate subtotal
	var subtotal = 0;
	qtys.each( function (qtyEl) { // loop through qty inputs
	    
	    // Find price
	    var priceEl = $( qtyEl.id + '-price' );
	    var price
		= priceEl.tagName.toUpperCase() == 'INPUT' ? priceEl.value
	        :                                            priceEl.innerHTML
	        ;
	    
	    price  = Tools.ParseMoney( price ) || 0;
	    price *= 100;

	    // Add line item to subtotal
	    var qty   = Tools.ParseNumber( qtyEl.value );
	    
	    if( qty == null )
		qty = 0;

	    subtotal += qty * price;
	} ); // qtys.each

	// Show subtotal
	var subtotalEl = (
	    document.getElementsByClassName( 'se-subtotal', orderFrm )
	)[0];
	if( subtotalEl ) {
	    subtotalEl.innerHTML = Tools.FormatMoney( subtotal/100 );
	}

	// Calculate order total
	var total = subtotal;

	// Find additional "fees"
	var fees = $A( document.getElementsByClassName( 'se-fee', orderFrm ) );
	fees.each( function (feeEl) { // loop through fees
	    // Figure out amount of fee
	    var fee
		= feeEl.tagName.toUpperCase() == 'INPUT' ? feeEl.value
	        :                                          feeEl.innerHTML
	        ;
	    
	    fee  = Tools.ParseMoney( fee );

	    if( fee == null )
		fee = 0;

	    fee *= 100;

	    // Add fee to total
	    total += fee;
	} ); // fees.each

	// Show total
	var totalEl = (
	    document.getElementsByClassName( 'se-total', orderFrm )
	)[0];
	if( totalEl ) {
	    totalEl.innerHTML = Tools.FormatMoney( total/100 );
	}

	// Call any registered handlers
	var handlers 
	    = SimpleEcommerce.RegistredEventHandlers['onordertotalchange'];

	var evtData = {
	    subtotal : subtotal,
	    total    : total
	};
	
	handlers.each( function (handler) {
	    handler( evtData );
	} );
	
	// Attach handlers to any inputs so that totals get recalculated
	if( !orderFrm.observed_by_simple_ecommerce ) {

	    // Create handler function
	    var handler = function () {
		SimpleEcommerce.UpdateOrderTotal( orderFrm );
	    };

	    // Attach to quantity and price inputs
	    qtys.each( function (qty) {
		new Form.Element.EventObserver( qty, handler );
		
		var priceEl = $( qty.id + '-price' );
		if( priceEl.tagName.toUpperCase() == 'INPUT' ) {
		    new Form.Element.EventObserver( priceEl, handler );
		}
	    } ); // qtys.each

	    // Attach to fee inputs
	    fees.each( function (feeEl) {
		if( feeEl.tagName.toUpperCase() == 'INPUT' ) {
		    new Form.Element.EventObserver( feeEl, handler );
		}
	    } ); // fees.each
	    orderFrm.observed_by_simple_ecommerce = true;
	} // if not being observed yet
    } ); // orderForms.each
    
    return;
} // UpdateOrderTotal

SimpleEcommerce.RegistredEventHandlers = {
    onordertotalchange : []
}; // RegisteredEventHandlers

SimpleEcommerce.RegisterEventHandler = function (event, handlerFunc) {

    if( SimpleEcommerce.RegistredEventHandlers[event] ) {
	
	SimpleEcommerce.RegistredEventHandlers[event].push( handlerFunc );
    }

}; // RegisterEventHandler

// Call UpdateOrderTotal on page load
// (wrap in function so that event doesn't get passed to it)
window.addOnLoadListener( function () { SimpleEcommerce.UpdateOrderTotal() } );

