function camselection(cammanufacturer,cammodel) {
	var x = document.getElementById("removable");
    x.innerHTML = "<br /><div style='text-align:center;'><strong>You Selected:</strong> &nbsp;&nbsp;Manufacturer: <input type='text' style='text-align:center;' name='manufacturer' readonly='readonly' /> &nbsp;&nbsp;Model Number: <input type='text' style='text-align:center;' name='model' readonly='readonly' /></div>"
	var y = document.getElementById("crcalc");
	y.manufacturer.value = cammanufacturer;
	y.model.value = cammodel;
}

function calculatecr() {

	var x = document.getElementById("crcalc");
	var cylinders = x.cylinders.value / 1;
	var bore = x.bore.value / 1;
	var gbore = x.gbore.value / 1;
	var stroke = x.stroke.value / 1;
	var chamber = x.chamber.value / 1;
	var deck = x.deck.value / 1;
	var gasket = x.gasket.value / 1;
	var notch = x.notch.value / 1;
	var pclearance = x.pclearance.value / 2;
	var prheight = x.prheight.value / 1;
	var RL = x.RL.value / 1;
	var duration = x.duration.value / 1;
	var lsa = x.lsa.value / 1;
	var ivca = x.ivca.value / 1;
	var advance = x.advance.value / 1;

// area of bore
	var borex = bore / 2;
	var bore2 = borex * borex * Math.PI;
	
// area of bore for head gasket
	var borez = gbore / 2;
	var bore9 = borez * borez * Math.PI;

// swept volume per cylinder
	var bore3 = bore2 * stroke;
	var bore3m = bore3 * 2.54 * 2.54 * 2.54;

// displacement
	var ci = bore3 * cylinders;
	var cc = ci * 2.54 * 2.54 * 2.54;
	var liters = cc / 1000;

// deck height volume
	var deck3 = deck * bore2;
	var deck3m = deck3 * 2.54 * 2.54 * 2.54;

// gasket thickness volume
	var gasket3;
	if (gbore != '') {
		gasket3 = gasket * bore9;
	} else {
	    gasket3 = gasket * bore2;
	}
	var gasket3m = gasket3 * 2.54 * 2.54 * 2.54;

// piston clearance volume
	var phb3 = bore2 * prheight;
	var phradius = borex - pclearance;
	var ph2 = phradius * phradius * Math.PI;
	var ph3 = ph2 * prheight;
	var pr3 = phb3 - ph3;
	var pr3m = pr3 * 2.54 * 2.54 * 2.54;

// compression volume
	var compvol = deck3m + gasket3m + notch + chamber + pr3m;

// static compression ratio
	var scompratio = (bore3m + compvol) / compvol;

// dynamic compression ratio
	var duration1 = duration / 2;
	var ICA;
	var ICAx;
	if (ivca != '' && advance != '') {
    	ICA = ivca - advance;
	} else if (ivca != '') {		
	    ICA = ivca;
	} else {
	    ICAx = lsa - advance;
	    ICA = duration1 - (180 - ICAx);
	}
    
// Quench
	var quench = deck + gasket / 1;

// IVCA from Duration, LSA, and Advance / Retard
	if (x.hideduration.checked) {
    	var ivcavalue = ivca;
	} else {
    	var ivcavalue = (((duration / 2) + lsa) - advance) - 180 / 1;
	}
    
// huh?
	// var hstroke = stroke / 2;
	// var RD1 = Math.sin(ICA / 180 * Math.PI);
	// var RD = hstroke * RD1;
	// var RR1 = Math.cos(ICA / 180 * Math.PI);
	// var RR = hstroke * RR1;
	// var RD2 = RD * RD;
	// var RL2 = RL * RL;
	// var RDRL = RL2 - RD2;
	// var PR1 = Math.sqrt(RDRL);
	// var PR2 = PR1 - RR;
	// var DS1 = PR2 + hstroke;
	// var DS2 = DS1 - RL;
	// var DST = stroke - DS2;

	var RDy = (stroke / 2) * Math.sin(ICA / 180 * Math.PI);
	var RRy = (stroke / 2) * Math.cos(ICA / 180 * Math.PI);
	var PR1y = Math.sqrt((RL * RL) - (RDy * RDy));
	var PR2y = PR1y - RRy;
	var DSTy = stroke - ((PR2y + (stroke / 2)) - RL);

	var DVL = bore2 * DSTy;
	var DVLm = DVL * 2.54 * 2.54 * 2.54;
	var dcompratio = (DVLm + compvol) / compvol;

//	results
	x.result_ci.value = (Math.round(ci * 100) / 100);
	x.result_liters.value = (Math.round(liters * 100) / 100);
	x.result_scr.value = (Math.round(scompratio * 100) / 100);
	x.result_dcr.value = (Math.round(dcompratio * 100) / 100);
	x.result_quench.value = (quench * 100) / 100;
    x.result_ivca.value = (Math.round(ivcavalue * 100) / 100);
	
// Has our cam selection fields already been filled in from a previous selection?
	var abcd = document.getElementById("removable");
	if (abcd.innerHTML == '' && x.hideduration.checked == false) {
    	// Removed no need to display
    	// camselection('No Selection','No Selection');
	}
	
}
    
function clearresults() {
	var x = document.getElementById("crcalc");
	x.result_ci.value = '';
	x.result_liters.value = '';
	x.result_scr.value = '';
	x.result_dcr.value = '';
	x.result_quench.value = '';
    x.result_ivca.value = '';
}

function clearcamselection() {
	var abcd = document.getElementById("removable");
    abcd.innerHTML = '';
}

function fillbore(tbf) {
	var x = document.getElementById("crcalc");
	x.bore.value = tbf;
	clearresults();
}

function fillstroke(tbf) {
	var x = document.getElementById("crcalc");
	x.stroke.value = tbf;
	clearresults();
}

function fillrod(tbf) {
	var x = document.getElementById("crcalc");
	x.RL.value = tbf;
	clearresults();
}

function fillduration(tbf) {
	var x = document.getElementById("crcalc");
	x.duration.value = tbf;
	clearresults();
}

function filllsa(tbf) {
	var x = document.getElementById("crcalc");
	x.lsa.value = tbf;
	clearresults();
}

function filladvance(tbf) {
	var x = document.getElementById("crcalc");
	x.advance.value = tbf;
	clearresults();
}

function hidestuff() {
	var x = document.getElementById("crcalc");
	if (x.hideduration.checked) {
		x.lsa.disabled = true;
        x.lsa.style.backgroundColor = '#C0C0C0';
		x.duration.disabled = true;
        x.duration.style.backgroundColor = '#C0C0C0';
        x.advance.disabled = true;
        x.advance.style.backgroundColor = '#C0C0C0';
		x.ivca.disabled = false;
        x.ivca.style.backgroundColor = '';
		x.lsa.value = '';
		x.duration.value = '';
        x.advance.value = '';
		clearcamselection();
	} else {
    	x.lsa.disabled = false;
        x.lsa.style.backgroundColor = '';
		x.duration.disabled = false;
        x.duration.style.backgroundColor = '';
        x.advance.disabled = false;
        x.advance.style.backgroundColor = '';
		x.ivca.disabled = true;
        x.ivca.style.backgroundColor = '#C0C0C0';
		x.ivca.value = '';
	}
    clearresults();
}

function usedurlsa() {
	var x = document.getElementById("crcalc");
	if (x.hideduration.checked) {
    	x.hideduration.checked = false;
        hidestuff();
	}
}

function popup(url,name) 
{
	var width  = 600;
 	var height = 300;
 	var left   = (screen.width  - width)/2;
 	var top    = (screen.height - height)/2;
 	var params = 'width='+width+', height='+height;
 	params += ', top='+top+', left='+left;
 	params += ', directories=no';
 	params += ', location=no';
 	params += ', menubar=no';
 	params += ', resizable=yes';
 	params += ', scrollbars=no';
 	params += ', status=no';
 	params += ', toolbar=no';
 	newwin=window.open(url,'name', params);
 	if (window.focus) {newwin.focus()}
 	return false;
}

function printpopup(url,name) 
{
 	newwin=window.open(url,'name');
 	if (window.focus) {newwin.focus()}
 	return false;
}

