﻿(function(jQuery) {

    jQuery.fn.summarize = function(options) {
        
        _fields = this.each( function() {
            $this = $(this);
            $this
                // trigger update of calculation
                .unbind('keyup')
                .bind('keyup', function(e) {
                    update();
                })
                // clear fields with error state 
                // when the loose focus.
                //.unbind('blur')
                .bind('blur', function(e) {
                    $this = $(e.target);
                    if ($this.hasClass('error')) {
                        $this.removeClass('error');
                        $this.val('0');
                    }
                })
                // block illegal charactes
                .unbind('keypress')
                .bind('keypress', function(e) {
                    if (e.which == 60 || e.which == 62) {
                        return false;
                    };
                })
                // block illegal charactes
                .unbind('focus')
                .bind('focus', function(e) {
					jQuery.event.trigger('field_change',e);
                })
        });        
        return _fields;
    };
    
    var _fields;
    var _values = new Object;
    
    jQuery.getSessionValues = function(){
		_fields.each( function() {
			$this = $(this);
			$this.val(_values[$this.attr('id')]);
		});
		
		update();
    }
       
    function update() {
        var value;
        var sum = 0;
        
        _fields.each( function() {
            $this = $(this);
            value = $this.val();
            
            value = value.replace(/^\s*|\s*$/g,'');
		    value = value.replace(/ /g,'');
		    
            if (value.length > 0) {
                if(validateNumeric(value)) {
                   sum = sum + parseInt(value,10);
                   
                   _values[$this.attr('id')] = value;
                   
                   $this.removeClass('error');
                } else {
                    $this.addClass('error');
                }
            } else {
                $this.removeClass('error');
            }
        });
       
        jQuery.event.trigger('update',sum);
    };
    
    function validateNumeric(value) {
            
        var rx = new RegExp(/^[0-9]{0,10}$/);
		var matches;
			
		matches = rx.exec(value);
		
		if (matches != null && value == matches[0]) {
		    return true;
		} else {
		    return false;
		}
    }
    
})(jQuery);


(function(jQuery) {

    jQuery.fn.sumtotal = function(options) {
        
        return this.each(function(){
            $(this)
                .unbind('update')
                .bind('update', function(e,sum) {
                    $(this).html(formatAmount(sum));
                })
        });        
    };
    
    function formatAmount(amount) {
        amount = amount.toString();
        for (var i = 0; i < Math.floor((amount.length-(1+i))/3); i++) {
            amount = amount.substring(0,amount.length-(4*i+3))+' ' + amount.substring(amount.length-(4*i+3));
        }
        return amount;
    };
    
})(jQuery);

$(document).ready(function() {
    $(document).bind('field_change',function(e,sender) {
		var senderid = sender.target.id;
		$('.calc-temp-sum').hide();
		$('#' + senderid + '_sum').show();
	})
});