/*	name			: ClassBehaviours, the javascript framework based on class-name parsing	update			: 9.3.17	author			: Maurice van Creij	dependencies	: jquery.classbehaviours.js	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.
    
    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};
	
	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};
	
	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}

	// Calculate the sum total of a set of input fields
	jQuery.classBehaviours.handlers.sumOfInput = {
		// properties
		name: 'sumOfInput',
		// methods
		start: function(node){
			// if this is the input field
			if(typeof(node.value)!='undefined'){
			// add the events 
				//node.onchange = this.calculate;
				node.onkeyup = this.calculate;				//node.onfocus = this.calculate;				node.onclick = this.calculate;				
				// initial value
				this.calculate(node);
			}
		},
		// events
		calculate: function(that){
		var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;		
			// get the target id
			targetId = jQuery.classBehaviours.utilities.getClassParameter(objNode, 'id', 'frmSumTotal');
			targetNode = document.getElementById(targetId);
			// get all elements with the same classname as this one
			sourceNodes = jQuery.classBehaviours.utilities.getElementsByClassName('id_' + targetId);
			// for all elements
			totalValue = 0;
			for(var a=0; a<sourceNodes.length; a++){
				// add the values to a total				if (sourceNodes[a].checked == true){					inputValue = parseFloat(sourceNodes[a].value);					totalValue += (isNaN(inputValue)) ? 0 : inputValue;								} 
			}
			// output the total to the target id			totalValue=totalValue.toFixed(2);			targetNode.innerHTML = totalValue;												if (typeof(document.forms.fifty)!='undefined'){				document.forms.fifty.product_price.value = totalValue; 			} else{				document.forms.compose.product_price.value = totalValue; 			}			/*			if (typeof(document.forms.compose)!='undefined'){							}				*/																		
			// get the limits
			minValue = parseInt(jQuery.classBehaviours.utilities.getClassParameter(targetNode, 'min', '0'));
			maxValue = parseInt(jQuery.classBehaviours.utilities.getClassParameter(targetNode, 'max', '100'));
			// change the class of the id if the limits are reached
			if(totalValue<minValue) targetNode.className = targetNode.className.replace('inLimit','underLimit').replace('overLimit','underLimit')
			else if(totalValue>maxValue) targetNode.className = targetNode.className.replace('inLimit','overLimit').replace('underLimit','overLimit')
			else targetNode.className = targetNode.className.replace('overLimit','inLimit').replace('underLimit','inLimit');
		}
	}
			
	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.sumOfInput = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.sumOfInput.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".sumOfInput").sumOfInput();
			}
		);
	}


