Calculator = {		formElement: null,	initialize: function ( formElement ) {				Calculator.formElement = formElement;				var inputElements = Calculator.formElement.getElementsByTagName('input');		for ( var i = 0; i < inputElements.length; i++ ) {						var inputElement = inputElements[i];			if ( inputElement.disabled != "disabled" ) {								inputElement.onchange = function (  ) { Calculator.update(); }							} // if					} // for inputElement				Calculator.update();			},		update: function (  ) {				var documentPattern = new RegExp("documents\\[[0-9]+\\]", "i");		var inputElements = Calculator.formElement.getElementsByTagName('input');				var documentsCount = 0;		var workersCount = 0;		var documentsCountElement = null;		var estimatedPriceElement = null;				// validate and collect form data and elements		for ( var i = 0; i < inputElements.length; i++ ) {						var inputElement = inputElements[i];			if ( documentPattern.test(inputElement.name) ) {								inputElement.value = parseInt(inputElement.value, 10);				if ( isNaN(parseInt(inputElement.value, 10)) || (parseInt(inputElement.value, 10) <= 0) ) { inputElement.value = 0; }				documentsCount += parseInt(inputElement.value, 10);							} else if ( inputElement.name == 'workersCount' ) {								inputElement.value = parseInt(inputElement.value, 10);				if ( parseInt(inputElement.value, 10) <= 1 ) { inputElement.value = 1; }				workersCount = parseInt(inputElement.value, 10);							} else if ( inputElement.name == 'documentsCount' ) {								documentsCountElement = inputElement;							} else if ( inputElement.name == 'estimatedPrice' ) {								estimatedPriceElement = inputElement;							} // else					} // for i				// output documents' count		if ( documentsCountElement != null ) {						documentsCountElement.value = documentsCount;					} // if				// calculate and output estimated price		if ( estimatedPriceElement != null ) {						var documentsCountValuation = [ // entries should be sorted ascendingly by upperBound field				{upperBound:20,  price:700},				{upperBound:40,  price:1000},				{upperBound:60,  price:1300},				{upperBound:80,  price:1500},				{upperBound:100, price:1800},				{upperBound:130, price:2300},				{upperBound:160, price:2600},				{upperBound:190, price:2900},				{upperBound:230, price:3200},				{upperBound:260, price:3500},				{upperBound:290, price:3800},				{upperBound:330, price:4100},				{upperBound:360, price:4400}			];						var workersCountValuation = 50;						// check if special offer should be applied			if ( documentsCount <= documentsCountValuation[documentsCountValuation.length-1].upperBound ) {								// hide special offer				document.getElementById("special-offer").style.display = "none";								// search for valuation in table				var documentsPrice = 0;				for ( var i = 0; i < documentsCountValuation.length; i++ ) {										var tableEntry = documentsCountValuation[i];					if ( documentsCount <= tableEntry.upperBound ) {												documentsPrice = tableEntry.price;						break;											} // if									} // for i								var workersPrice = workersCount*workersCountValuation;				estimatedPriceElement.value = Math.ceil((documentsPrice + workersPrice)*100)/100;							} else { // documents' count not listed in table - special offer								// show special offer				document.getElementById("special-offer").style.display = "block";								estimatedPriceElement.value = "-";							} // else					} // if			}	}
