// Formated so you can actually read the script by Richard Keever 11/05/2003
//alert ("Page Load");
G = new Array(0.0000,0.0027,0.0052,0.0076,0.0099,0.0120,0.0141,0.0161,0.0180,0.0198,0.0216,0.0233,0.0250,0.0274,0.0297,0.0327,0.0368,0.0419,0.0482,0.0560,0.0704,0.0864,0.1037,0.1224,0.1425,0.1564,0.1686,0.1800,0.1904,0.1996,0.2076,0.2141,0.2216,0.2270,0.2323,0.2374,0.2424,0.2471,0.2517,0.2560,0.2601,0.2640,0.2677,0.2712,0.2744,0.2760,0.2799,0.2836,0.2874,0.2911,0.2948,0.2985,0.3022,0.3058,0.3095,0.3132,0.3168,0.3205,0.3242,0.3279,0.3316,0.3354,0.3392,0.3430,0.3469,0.3508,0.3547,0.3587,0.3628,0.3669,0.3711,0.3753,0.3797,0.3841,0.3886,0.3931,0.3978,0.4026,0.4074,0.4124,0.4175,0.4227, 0.4280 ,0.4334,0.4389,0.4446,0.4504,0.4564,0.4625);

var dist = 0;
var t = 0;
var rate = 32;
var rate1 = 0;
var dist1 = 0;
var slope = 0;
var dist2 = 0;
var t1 = 0;
var rate2 = 32;
var rate3 = 0;
var dist3 = 0;
var correct = 0;
var drop = 0;
var v2 = 0;
var time = 0;
var d = 0;
var i = 1;
var pressure = 1;
var drift = 0;

var body1 = '<html><body bgcolor = "white" text = "black"><center><table width = "80%">';

function findslope(f){
	var oldv = f.mv.value;
	var los = 0;
	var pt = 0;
	var v3 = f.mv.value;
	var olddist = 0;
	var olddrop = 0;
	var oldvel = 0;
	var G3 = 0;
	var Gap1 = 0;
	var Gsm1 = 0;
	var Glg1 = 0;
	var Gdiff1 = 0;

	while(dist2 < f.distance.value * 3){
		G3 = (v3 / 50);
		Gap1 =  G3%1;
		Gsm1 = G3 - Gap1;
		Glg1 = Gsm1 + 1;
		Gdiff1 = G[Glg1] - G[Gsm1];
		G4 = G[Gsm1] + (Gdiff1 * Gap1);
		olddist = dist2;
		olddrop = dist3;
		oldv = v3; 
		v3 = v3	/ Math.exp((G4 / f.bc.value) * .005 * pressure) + .08;
		dist2 += (v3 + oldv) / 2 * .005;
		t1 += .005;
		rate3 += 0.162;
		rate3 = (rate3) / Math.exp((G4 / f.bc.value) * .005 * pressure);
		dist3 += rate3 * .005;
		};

	dist2 = interpolate(olddist,dist2,f.distance.value * 3);
	drop = (olddrop + ((dist3 - olddrop) * correct)) * 12;
	slope = (drop + parseFloat(f.sights.value)) / (dist2 / 3);
};

function round(places,num){
	var y = 0;
	var x = 1;
	x *= Math.pow(10,places);
	y = num * x;
	y = Math.round(y);
	y /= x;
	return (y)
};

//function round(precision, num) 
//{ 
////convert num to string 
//	num = "" + num 
//	precision = parseInt(precision); 
//	
//	var fullNum = "" + Math.round(num* Math.pow(10, precision)); 
//
////Looking for the decimal point 
//	var decPoint = fullNum.length - precision; 
//	
//	if(decPoint != 0) 
//	{ 
////Merging the left and right part to the decimal together 
//	result = fullNum.substring(0, decPoint); 
//	result += "."; 
//	result += fullNum.substring(decPoint, fullNum.length); 
//	} 
//	else 
//	{ 
//	result = fullNum; 
//	} 
//	return result; 
//} 

function interpolate(old,newd,length){
	var shortd = length - old;
	var interval = newd - old;
	correct = (shortd / interval);
	return(old + ((shortd / interval) * interval))
};


function velocity(f){
	var oldv = f.mv.value;
	var G2 = 0;
	var Gap = 0;
	var Gsm = 0;
	var Glg = 0; 
	var Gdiff = 0;
	var G1 = 0; 

	body(0,f.mv.value,0,t); 

	while(dist < f.maxrange.value * 3){
		while(dist < f.intervals.value * i *3){
 			G2 = (v2 / 50);
 			Gap =  G2%1; 
			Gsm = G2 - Gap;
			Glg = Gsm + 1;
			Gdiff = G[Glg] - G[Gsm];
			G1 = G[Gsm] + (Gdiff * Gap);
			var oldvel = v2;
			var olddrop = dist1;
			var olddist = dist;
			oldv = v2;
			v2 = v2 / Math.exp((G1 / f.bc.value) * .005 * pressure) + .08;
			dist += (v2 + oldv) / 2 * .005;
			distance = round(2,dist / 3);
			t += .005;
			time = t;
			rate1 += 0.162;
			rate1 = (rate1) / Math.exp((G1 / f.bc.value) * .005 * pressure);
			dist1 += rate1 * .005;
		};
// alert (correct)
		distance = interpolate(olddist,dist,f.intervals.value * 3 * i) / 3;
		var v3 = round(0,oldvel - ((oldvel - v2) * correct));
		drop1 = (olddrop + ((dist1 - olddrop) * correct)) * 12;
		d = round(2,parseFloat(drop1));
		time = t - .005 + (.005 * correct);
		drift = Math.sin(f.direction.value / 57.296) * f.mph.value * 17.01 * (time - distance * 3 / f.mv.value)
		body(distance,v3,d,time);
		i += 1;
	};

endtable();
};



function energy(speed){
	return speed * speed * (document.bform.weight.value) / 450337
};

function impact(drop,distance){
	var los = 0;
	var pt = 0;
	los = (parseFloat(document.bform.sights.value) - slope * distance) * -1;
	pt = los - Math.cos(document.bform.angle.value / 57.296) * drop;
	return (pt)
};

function maketable(f){
	var heading = '<http></http> ';

//parent.head.document.close();
//parent.head.document.write(heading);
//if(navigator.appName!="Netscape")
//parent.head.document.close();

	body1 = '<html><body bgcolor = "white" text = "black"><center><table width = "80%"><tr><th align = "right" width = "50"><font face = "Arial">Range<th align = "right" width = "50"><font face = "Arial">Velocity<th align = "right" width = "50"><font face = "Arial">Impact<th align = "right" width = "50"><font face = "Arial">Drop<th align = "right" width = "50"><font face = "Arial">ToF<th align = "right" width = "50"><font face = "Arial">Energy<th align = "right" width = "50"><font face = "Arial">Drift</tr>'
};

function body(distance,speed,drop,tof){
	body1+='<tr><td align = "right" width = "50"><font face = "Arial">'+round(0,distance)+'</td><td align = "right" width = "50"><font face = "Arial">'+round(0,speed)+'</td><td align = "right" width = "50"><font face = "Arial">'+round(2,impact(drop,distance))+'</td><td align = "right" width = "50"><font face = "Arial">'+round(2,drop)+'</td><td align = "right" width = "50"><font face = "Arial">'+round(2,tof)+'</td><td align = "right" width = "50"><font face = "Arial">'+round(0,energy(speed,document.bform.weight.value))+'</td><td align = "right" width = "50"><font face = "Arial">'+round(2,drift)+'</td></tr>';
};

function endtable(){
	body1 += '</table></body></html>';
	parent.body.document.close();
	parent.body.document.write(body1);

//if(navigator.appName!="Netscape")
//parent.body.document.close();
};

function redo(){
	dist = 0;
	t = 0;
	rate1 = 0;
	dist1 = 0;
	slope = 0;
	dist2 = 0;
	t1 = 0;
	rate3 = 0;
	dist3 = 0;
	correct = 0;
	drop = 0;
	v2 = 0;
	time = 0;
	d = 0;
	i = 1;
	body1 = '<html><body bgcolor = "white" text = "black"><center><table width = "80%"><tr><td align = "right" width = "50"><font face = "Arial">Range</td><td align = "right" width = "20"><font face = "Arial">Velocity</td><td align = "right" width = "50"><font face = "Arial">Impact</td><td align = "right" width = "50"><font face = "Arial">Drop</td><td align = "right" width = "50"><font face = "Arial">ToF</td><td align = "right" width = "50"><font face = "Arial">Energy</td><td align = "right" width = "50"><font face = "Arial">Drift</td></tr>';
	drift = 0;
};


function press1(temp,alt){
	ratio = (519 / (parseFloat(temp) + 459.4));
	pressure = (Math.exp(-alt / 32608.69565) * ratio);
	newtemp = temp -alt * 3.57 / 1000 + 459.4;
	newtemp1 = parseFloat(temp) + 459.4;
	newtemp2 = parseFloat(temp) + 459.4;
	newpressure = pressure * (newtemp / newtemp1);
	pressure = newpressure;
};

function check(f){
	
//alert ("Calculating");	

	if(f.bc.value < .005){
		alert("The BC you entered is too small, please enter a BC greater than .005")} 
	else{
		if(f.intervals.value < 5){
			alert("Please enter an interval of at least 5 yards.")}
	else{
		if(f.maxrange.value / f.intervals.value > 50){
			alert("You cannot have more than 50 intervals, please enter a larger value for Intervals or a smaller value for Max range")} 
	else{
		if(f.maxrange.value > 1000){
			alert("Max Range cannot be greater than 1000 yards, please enter new maximum Range")}
	else{
		v2 = f.mv.value;
		press1(f.temp.value,f.alt.value);
		maketable(f);
		findslope(f);
		velocity(f);
		redo();
	};
	};
	};
	};
};

