
function parseAllFloats(_selector) {
	$(_selector).find('input[type="text"]').each(function () {
		$(this).attr('value', parseFloat($(this).attr('value')).toFixed(2));
	});
}

function insideRise(_radius, _chord) {
	return _radius - (0.5 * Math.sqrt((4 * (Math.pow(_radius, 2)) - Math.pow(_chord, 2))));
}

function degrees(_radius, _chord) {
	var a = _chord;
	var b = 2 * _radius;
	var c = Math.PI;
	var d = Math.asin(a / b);
	var e = d * (180 / c);

	var f = 2 * e;

	//alert([a, b, c, d, e, f]);

	return 2 * (Math.asin(_chord / (2 * _radius)) * (180 / Math.PI));
}

function outsideArc(_radius, _memberHeight, _degrees) {
	return insideArc(_radius + _memberHeight, _degrees);
}

function radius(_insideRise, _chord) {
	return ((4 * Math.pow(_insideRise, 2) + Math.pow(_chord, 2)) / (8 * _insideRise));
}

function insideArc(_radius, _degrees) {
	return (_degrees / 360) * 2 * Math.PI * _radius;
}

function deNaN(_selector) {
	$(_selector).find('input[type="text"]').each(function() {
		if (isNaN(this.value)) {
			this.value = '';
		}
	});
}

//Handy dandy rounding function
function round(_number, _decimals) {
	var scalar = Math.pow(10, _decimals);
	return Math.round(_number*scalar)/scalar;
}

//Online Radius Calculator Left Side - Chord and Rise
function onlineRadiusCalcL() {

	var chord =  parseFloat($('[name="chordL"]').attr('value'));
	var insideRisev = parseFloat($('[name="insideRiseL"]').attr('value'));
	var height = parseFloat($('[name="memberHeightL"]').attr('value'));

	var radiusL = radius(insideRisev, chord);
	var degreesOfBendL = degrees(radiusL, chord);
	var insideArcL = insideArc(radiusL, degreesOfBendL);
	var outsideArcL = outsideArc(radiusL, height, degreesOfBendL);

	var data = {
		"radiusTotal" : round(radiusL, 2),
		"insideArcTotal" : round(insideArcL, 2),
		"outsideArcTotal" : round(outsideArcL, 2),

		"insideRiseTotal" : round(insideRisev, 2),
		"chordTotal" : round(chord, 2),

		"degreesOfBendTotal" : round(degreesOfBendL, 2),
		"memberHeightTotal" : round(height, 2)
	}


	var getStr = '';

	var first = true;
	for (var i in data) {
		if (data.hasOwnProperty(i)) {
			$('[name="'+i+'"]').attr('value', data[i]);
			if (! first) {
				getStr += "&";
			} else {
				first = false;
			}
			getStr += escape(i) + "=" + escape(data[i]);
		}
	}

	var newUrl = $("#pdfprintlink").attr("_href") + "&" + getStr;

	$("#pdfprintlink").attr('href', newUrl);

	parseAllFloats('[name="totals"]');
	deNaN('*');

	if (insideRisev >= (chord * 2 )) {
		alert("That is more than a half circle.");
	}

	return;
}

//onlineRadiusCalculatorL

$(function() {
	$("#pdfemaillink").click(function() {
		window.location = $(this).attr("href") + "?pdf=" + encodeURIComponent($("#pdfprintlink").attr("href"));
		return false;
	});
});

//Online Radius Calculator Right Side - Radius and Chord

function onlineRadiusCalcR() {
	var chord =  parseFloat($('[name="chordR"]').attr('value'));
	var radiusR = parseFloat($('[name="radiusR"]').attr('value'));
	var height = parseFloat($('[name="memberHeightR"]').attr('value'));


	var degreesOfBendR = degrees(radiusR, chord);
	var insideArcR = insideArc(radiusR, degreesOfBendR);
	var outsideArcR = outsideArc(radiusR, height, degreesOfBendR);
	var insideRisev = insideRise(radiusR, chord);

	var data = {
	"radiusTotal" : round(radiusR, 2),
	"insideArcTotal" : round(insideArcR, 2),
	"outsideArcTotal" : round(outsideArcR, 2),

	"insideRiseTotal" : round(insideRisev, 2),
	"chordTotal" : round(chord, 2),

	"degreesOfBendTotal" : round(degreesOfBendR, 2),
	"memberHeightTotal" : round(height, 2)
}

	var getStr = '';

	var first = true;
	for (var i in data) {
		if (data.hasOwnProperty(i)) {
			$('[name="'+i+'"]').attr('value', data[i]);
			if (! first) {
				getStr += "&";
			} else {
				first = false;
			}
			getStr += escape(i) + "=" + escape(data[i]);
		}
	}

	var newUrl = $("#pdfprintlink").attr("_href") + "&" + getStr;

	$("#pdfprintlink").attr('href', newUrl);

	if (chord >= (radiusR * 2 )) {
		alert("Chord will not fit in that circle.");
	}


	parseAllFloats('[name="totals"]');
	deNaN('*');


	return;

}//onlineRadiusCalculatorR

//changes from inches to mm and vice-versa for the Left values
function getSelectedItemL() {
} //getSelectedItemL

//changes from inches to mm and vice-versa for the Right values
function getSelectedItemR() {
} //getSelectedItemR

//changes from inches to mm and vice-versa for the Totals values
function getSelectedItemTotals() {
} //getSelectedItemTotals

//jQuery for the win.
$(function(){

	$("input[name=inmm][status=master]:radio").change(function(){
		$("input[value='"+$(this).attr('value')+"']").attr("checked", true);
		return;
	});

	$('#readonly').click(
	function()
	{
		if ($("form#measureLForm :radio[value='Millimeters']").attr("checked") == true)
			{
			$('#readonly').val('');
		}
	});

	$("form#measureLForm input").mousedown(function(){
		$("form#measureRForm input").attr("disabled", true);
	});


	$("form#measureRForm input").mousedown(function(){
		$("form#measureLForm input").attr("disabled", true);
	});

	$("form[name='totals'] :radio").change(function(){
		deNaN('*');
	});

	$("form .radioButtonsIN").click(function(){
		$("div.totalInches").text("Inches");
	});

	$("form .radioButtonsMM").click(function(){
		$(".totalInches").text("Millimeters");
	});

	$(":reset").click(function(){
		$("input:text").val("");
		$(".totalInches").val("Millimeters");
		$("#totalInches6").val("Degrees");
		$("form#measureLForm input, form#measureRForm input").removeAttr("disabled", true);
	});

	var rSelects = $("#measureRForm input[name=inmm]");
	var lastRClicked = rSelects[0];
	rSelects.click(function(){
		if( lastRClicked != this ) {
			var radiusR = parseFloat(document.measureR.radiusR.value);
			var chordR = parseFloat(document.measureR.chordR.value);
			var memberHeightR = parseFloat(document.measureR.memberHeightR.value);

			var chosen = "";

			var lengthB = document.measureR.inmm.length;

			for (i = 0; i < lengthB; i++) {
				if (document.measureR.inmm[i].checked) {
					chosen = document.measureR.inmm[i].value;
				}
			}
			if (chosen == "Millimeters") {
				document.measureR.radiusR.value= Math.round(((radiusR)* 25.4)*100)/100;
				document.measureR.chordR.value = Math.round(((chordR) * 25.4)*100)/100;
				document.measureR.memberHeightR.value = Math.round(((memberHeightR) * 25.4)*100)/100;
			}
			else if (chosen == "Inches") {
				document.measureR.radiusR.value = Math.round(((radiusR) / 25.4)*100)/100;
				document.measureR.chordR.value = Math.round(((chordR) / 25.4)*100)/100;
				document.measureR.memberHeightR.value = Math.round(((memberHeightR) / 25.4)*100)/100;
			}

			for (z = 0; z < 1; z++) {
				onlineRadiusCalcR();
			}
		}

		lastRClicked = this;
	});


	var lSelects = $("#measureLForm input[name=inmm]");
	var lastLClicked = lSelects[0];
	lSelects.click(function(){
		if( lastLClicked != this ) {

			var chordL = parseFloat(document.measureL.chordL.value);
			var insideRiseL = parseFloat(document.measureL.insideRiseL.value);
			var memberHeightL = parseFloat(document.measureL.memberHeightL.value);

			var chosen = "";

			var lengthA = document.measureL.inmm.length;

			for (i = 0; i <lengthA; i++) {
				if (document.measureL.inmm[i].checked) {
					chosen = document.measureL.inmm[i].value;
				}
			}
			if (chosen == "Millimeters") {
				document.measureL.chordL.value = Math.round(((chordL)* 25.4)*100)/100;
				document.measureL.insideRiseL.value = Math.round(((insideRiseL) * 25.4)*100)/100;
				document.measureL.memberHeightL.value = Math.round(((memberHeightL) * 25.4)*100)/100;
			}
			else if (chosen == "Inches") {
				document.measureL.chordL.value = Math.round(((chordL) / 25.4)*100)/100;
				document.measureL.insideRiseL.value = Math.round(((insideRiseL) / 25.4)*100)/100;
				document.measureL.memberHeightL.value = Math.round(((memberHeightL) / 25.4)*100)/100;
			}

			for (y = 0; y < 1; y++) {
				onlineRadiusCalcL();
			}
		}
		lastLClicked = this;
	});

    var bSelects = $("#measureBotForm input[name=inmm]");
    var lastBClicked = bSelects[0];
        bSelects.click(function(){

        if( lastBClicked != this ) {

    var radiusTotal = parseFloat(document.totals.radiusTotal.value);
    var insideArcTotal = parseFloat(document.totals.insideArcTotal.value);
    var outsideArcTotal = parseFloat(document.totals.outsideArcTotal.value);
    var insideRiseTotal = parseFloat(document.totals.insideRiseTotal.value);
    var chordTotal = parseFloat(document.totals.chordTotal.value);

    var radiusTotalBot = parseFloat(document.botCalcs.radiusTotal.value);
    var insideArcTotalBot = parseFloat(document.botCalcs.insideArcTotal.value);
    var outsideArcTotalBot = parseFloat(document.botCalcs.outsideArcTotal.value);
    var insideRiseTotalBot = parseFloat(document.botCalcs.insideRiseTotal.value);
    var chordTotalBot = parseFloat(document.botCalcs.chordTotal.value);

    var chosen = "";

    var lengthC = document.totals.inmm.length;

    for (i = 0; i < lengthC; i++) {
        if (document.totals.inmm[i].checked) {
            chosen = document.totals.inmm[i].value;
        }
    }
    if (chosen == "Millimeters") {
        document.totals.radiusTotal.value = Math.round(((radiusTotal)* 25.4)*100)/100;
        document.totals.insideArcTotal.value = Math.round(((insideArcTotal) * 25.4)*100)/100;
        document.totals.outsideArcTotal.value = Math.round(((outsideArcTotal) * 25.4)*100)/100;
        document.totals.insideRiseTotal.value= Math.round(((insideRiseTotal)* 25.4)*100)/100;
        document.totals.chordTotal.value = Math.round(((chordTotal) * 25.4)*100)/100;

        document.botCalcs.radiusTotal.value = Math.round(((radiusTotal)* 25.4)*100)/100;
        document.botCalcs.insideArcTotal.value = Math.round(((insideArcTotal) * 25.4)*100)/100;
        document.botCalcs.outsideArcTotal.value = Math.round(((outsideArcTotal) * 25.4)*100)/100;
        document.botCalcs.insideRiseTotal.value= Math.round(((insideRiseTotal)* 25.4)*100)/100;
        document.botCalcs.chordTotal.value = Math.round(((chordTotal) * 25.4)*100)/100;

    }else if (chosen == "Inches") {
        document.totals.radiusTotal.value = Math.round(((radiusTotal) / 25.4)*100)/100;
        document.totals.insideArcTotal.value = Math.round(((insideArcTotal) / 25.4)*100)/100;
        document.totals.outsideArcTotal.value = Math.round(((outsideArcTotal) / 25.4)*100)/100;
        document.totals.insideRiseTotal.value= Math.round(((insideRiseTotal) / 25.4)*100)/100;
        document.totals.chordTotal.value = Math.round(((chordTotal) / 25.4)*100)/100;

        document.botCalcs.radiusTotal.value = Math.round(((radiusTotal) / 25.4)*100)/100;
        document.botCalcs.insideArcTotal.value = Math.round(((insideArcTotal) / 25.4)*100)/100;
        document.botCalcs.outsideArcTotal.value = Math.round(((outsideArcTotal) / 25.4)*100)/100;
        document.botCalcs.insideRiseTotal.value= Math.round(((insideRiseTotal) / 25.4)*100)/100;
        document.botCalcs.chordTotal.value = Math.round(((chordTotal) / 25.4)*100)/100;
    }
        }
        lastBClicked = this;
    });

});
