var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;

function isEmpty(s){ return ((s == null) || (s.length == 0)) }

function isFloat (s){
	
	if (isEmpty(s)) 
		if (isFloat.arguments.length == 1) return false;
		else return (isFloat.arguments[1] == true);
	return reFloat.test(s)
}
var tysWire = 0;
var twsWire = 0;
var tysLabor = 0;
var twsLabor = 0;
var tysTips = 0;
var twsTips = 0;

function updateTotalSavings(){
	var totalYealySaving = tysWire + tysLabor + tysTips;
	var totalWeeklySaving = twsWire + twsLabor + twsTips;
	$('totalSavings').setStyle('background', '#ccc');
	$('totalSavings').setHTML("<h3>Total Savings</h3><ul><li><strong>Weekly: $" + totalWeeklySaving.toFixed(2) + "</strong></li><li><strong>Yearly: $" + totalYealySaving.toFixed(2) + "</strong><br /><em>(Based on 50 week year)</em></li></ul>");
}

var dollarError = "Please enter a dollar amount without a dollar sign.  example 8.00";

window.addEvent('domready', function(){
		$('wccFrm').addEvent('submit', function(e){
			new Event(e).stop();
			var a1 = this.onelbspoolcost.value;
			var b1 = this.lbcostlgspool.value;
			var d1 = this.lbsofwireperweek.value;
			
			if(!isFloat(a1)){
				alert(dollarError);
				return false;
			}
			
			if(!isFloat(b1)){
				alert(dollarError);
				return false;
			}
			
			var c1 = a1 - b1;
			
			var e1 = c1 * d1;
			var f1 = e1 * 50;
			
			var wccsavings = "<h3>Your Wire Consumption Savings:</h3><ul><li><strong>Weekly: $" + e1.toFixed(2) + "</strong></li><li><strong>Yearly: $" + f1.toFixed(2) + "</strong><br /><em>(Based on 50 week year)</em></li></ul>";
			if(window.ie) {
				$('wirehide').setStyle('display', 'block');
			} else {
				$('wirehide').setStyle('display', 'table-row');
			}
			$('wirediff').setHTML("$" + c1.toFixed(2));
			$('wccSavings').setHTML(wccsavings);
			tysWire = f1;
			twsWire = e1;
			updateTotalSavings();
		});
		
		$('laborFrm').addEvent('submit', function(e){
			new Event(e).stop();
			var a2 = this.onelbspoolchangetime.value;
			var b2 = this.rollspershift.value;
			
			if(!isFloat(a2)){
				alert("Please enter the minutes as a numeric value.");
				return false;
			}
			
			if(!isFloat(b2)){
				alert("Please enter the number of rolls per shift as a numeric value.");
				return false;
			}
			
			var c2 = (a2 * b2) - a2;
			var d2 = this.operatorhourlywage.value;
			var e2 = this.shiftsperweek.value;
			
			if(!isFloat(d2)){
				alert(dollarError);
				return false;
			}
			
			if(!isFloat(e2)){
				alert("Please enter the number of shifts per week as a numeric value.");
				return false;
			}
			
			document.tipsFrm.tipsrollspershift.value = b2;
			document.tipsFrm.tipsshifts.value = e2;
			
			var f2 = c2 * d2 * (e2/60);
			var g2 = f2 * 50;
			
			var laborsavings = "<h3>Your Labor Cost Savings:</h3><ul><li><strong>Weekly: $" + f2.toFixed(2) + "</strong></li><li><strong>Yearly: $" + g2.toFixed(2) + "</strong><br /><em>(Based on 50 week year)</em></li></ul>";
			if(window.ie) {
				$('laborhide').setStyle('display', 'block');
			} else {
				$('laborhide').setStyle('display', 'table-row');
			}
			$('timesavings').setHTML(c2 + " (Time change figured in for large spool change)")
			$('laborSavings').setHTML(laborsavings);
			tysLabor = g2;
			twsLabor = f2;
			updateTotalSavings();
		});
		
		$('tipsFrm').addEvent('submit', function(e){
			new Event(e).stop();
			
			var a3 = this.tipsrollspershift.value;
			var b3 = this.tipsshifts.value;
			
			if(!isFloat(a3)){
				alert("Please enter the number of spool changed per shift.");
				return false;
			}
			
			if(!isFloat(b3)){
				alert("Please enter the number of shifts per week as a numeric value.");
				return false;
			}
			
			c3 = a3 - 1;
			
			var d3 = this.avgtipcost.value;
			
			if(!isFloat(d3)){
				alert(dollarError);
				return false;
			}
			
			e3 = b3 * c3 * d3;
			f3 = e3 * 50;
			
			if(window.ie) {
				$('tipshide').setStyle('display', 'block');
			} else {
				$('tipshide').setStyle('display', 'table-row');
			}
			$('tipsburned').setHTML(c3);
			var savings1 = "<strong>Weekly:  $" + e3.toFixed(2) + "</strong>";
			var savings2 = "<strong>Yearly:  $" + f3.toFixed(2) + "</strong><br /><em>(Based on 50 week year)</em>";
			var tipsavings = "<h3>Your Tips Cost Savings:</h3><ul><li>" + savings1 + "</li><li>" + savings2 + "</li></ul>";
			$('tipsSavings').setHTML(tipsavings);
			tysTips = f3;
			twsTips = e3;
			updateTotalSavings();
		});
	});
