// Author    : Phil Locker (phil@philsfoils.com) 

function click(){
	if(event.button==2){
		alert("Sorry."); 
		}
	}
document.onmousedown=click

function momentcalc(form){
	// assumptions: seated position C of G is 6 inches inside gunwale
	//              hiked position C of G  is 25 % of the body's height outside the gunwale
	//              trapezed position C of G is 50% of the body's height outside of the gunwale
    // hull half width
	if (form.halfwidth.value == null || form.halfwidth.length == 0) {
            form.rightingmoment.value = "error";
		return false;
      }
   // inches or cm to feet.
	var widthconversion = 0.08333;
	if (form.w_units.selectedIndex == 1) {
		widthconversion = 0.03281;
	}
	var halfwidth = form.halfwidth.value * widthconversion;
	
	var weightconversion = 1;
	if (form.w_measure.selectedIndex == 1) {
		weightconversion = 2.2046;
	}
	
	// inches or cm to feet.
	var heightconversion = 0.08333;
	if (form.h_measure.selectedIndex == 1) {
		heightconversion =  0.03281;
	}
	
	var rightingmoment = 0;
	var crew1w = form.crew1weight.value * weightconversion;
	var crew1h = form.crew1height.value * heightconversion;
	var position1 = form.crew1posn.selectedIndex;
	if (position1 == 0) {
		rightingmoment += (crew1w  * (halfwidth - 0.5));
	}
	else {
		if (position1 == 1) {
			rightingmoment += (crew1w * (halfwidth + (crew1h * 0.25)));
		}
		else {
			rightingmoment += (crew1w * (halfwidth + (crew1h * 0.5 )));
		}
	}
	
	// if position2 index is zero, there is no 2nd crew
	var position2 = form.crew2posn.selectedIndex;
	if (position2 != 0) {
		var crew2w = form.crew2weight.value * weightconversion;
		var crew2h = form.crew2height.value * heightconversion;

		if (position2 == 1) {
			rightingmoment += (crew2w  * (halfwidth - 0.5));
		}
		else {
			if (position2 == 2) {
				rightingmoment += (crew2w  * (halfwidth + (crew2h * 0.25)));
			}
		else {
				rightingmoment += (crew2w  * (halfwidth + (crew2h * 0.5)));
			}
		}
	}
	
	
	// if position3 index is zero, there is no 3rd crew
	var position3 = form.crew3posn.selectedIndex;
	if (position3 != 0) {
		var crew3w = form.crew3weight.value * weightconversion;
		var crew3h = form.crew3height.value * heightconversion;
		if (position3 == 1) {
			rightingmoment += (crew3w  * (halfwidth - 0.5));
		}
		else {
			if (position3 == 2) {
				rightingmoment += (crew3w  * (halfwidth + (crew3h * 0.25)));
			}
			else {
				rightingmoment += (crew3w * (halfwidth + (crew3h * 0.5)));
			}
		}
	}

   // at this point righting moment is in foot-pounds
   var poundfeet = formatnumbers(rightingmoment, 6);
   var kgmeters = formatnumbers(poundfeet * 0.1488164, 6);

   form.rightingmoment.value = poundfeet + "lbft, " + kgmeters +"kgM";
	return true;
}


function formatnumbers(input, rsize) {

   var invalid = "**************************";

   var nines = "999999999999999999999999";

   var strin = "" + input;

   var fltin = parseFloat(strin);

   if (strin.length <= rsize) return strin;

   if (strin.indexOf("e") != -1 ||

       fltin > parseFloat(nines.substring(0,rsize)+".4"))

      return invalid.substring(0, rsize);

   var rounded = "" + (fltin + (fltin - parseFloat(strin.substring(0, rsize))));

   return rounded.substring(0, rsize);

}


function clearmomentform(form) {
    form.crew1weight.value = "";
    form.crew1height.value = "";
    form.crew2weight.value = "";
    form.crew2height.value = "";
    form.crew3weight.value = "";
    form.crew3height.value = "";  
    form.rightingmoment.value = "";
    return true;
}

function liftcalc(form){
    // velocity
	if (form.velocity.value == null || form.velocity.value.length == 0) {
            form.lift.value = "error";
		return false;
      }

	// area
	if (form.area.value == null || form.area.value.length == 0) {
            form.lift.value = "error";
		return false;
      }

	// lift coefficient
	if (form.CL.value == null || form.CL.value.length == 0) {
            form.lift.value = "error";
		return false;
      }

    // fluid constant
	if (form.fluid.value == null || form.fluid.value.length == 0) {
            form.fluid.value = "error";
		return false;
      }
      	
	var velftsec;
	var areasqft;
   
    if (form.v_units.selectedIndex == 0){
    	velftsec = form.velocity.value;
    }
    else {
    	if (form.v_units.selectedIndex == 1){
    		velftsec = form.velocity.value* 3.28084;
    	}
    	else {
    		velftsec = form.velocity.value* 1.68781 ;
    	}
     }
     
    if (form.a_units.selectedIndex == 0){
    	areasqft = form.area.value / 144.0;
    }
    else {
    		areasqft = form.area.value / 929.0;
    }
      
   var calculatedlift = (form.CL.value / 2) * form.fluid.value * velftsec * velftsec * areasqft;
	form.lift.value = formatnumbers(calculatedlift, 6) + "lb, " + formatnumbers(calculatedlift * 0.4536, 6) + "kg";
	return true;
}


function clearliftform(form) {
    form.velocity.value = "";
    form.area.value = "";
    form.CL.value = "1.5";
    form.fluid.value = "1.9905";
    form.lift.value = "";
    return true;
}

