// JavaScript WINDLOAD module
// Copyright (c) 2002 Engineers Compendium, all rights reserved.
//
//global
var ZO = new Array(.002, .02, .2, 2.0);
var TC = new Array(1.0, 2.0, 3.0, 4.0);
var TBLZ = new Array(3,5,10,15,20,30,40,50,75,100);
var T11 = new Array(.99,1.05,1.12,1.16,1.19,1.22,1.24,1.25,1.27,1.29);
var T12 = new Array(.91,.91,1.00,1.05,1.08,1.12,1.16,1.18,1.22,1.24);
var T13 = new Array(.83,.83,.83,.89,.94,1.0,1.04,1.07,1.12,1.16);
var T14 = new Array(.75,.75,.75,.75,.75,.8,.85,.9,.98,1.03);
var T212 = new Array(.9,.95,1.0,1.07,1.13,1.2,1.25,1.29,1.35,1.4);
var T234 = new Array(.8,.8,.89,.95,1.05,1.15,1.25,1.29,1.35,1.4);
var VS = new Array();
var VU = new Array();
var VO = new Array();
var Zval = new Array();

function displayVR() {
  if (navigator.userAgent.search("Opera")>=0)
  { alert("Sorry, this doesn't work with Opera. Use IE 5.x or Netscape 6.x instead.");
    return;
  }
  if ((navigator.appName=="Netscape")&&(parseInt(navigator.appVersion)<5))
  { if (!confirm("This is not a safe operation with Netscape 4.x."+
                " It can cause a crash of the browser."+
                " Do you want to continue anyway?"))
      return;
  }
  var fx1="";
  var fx2="";
  var RS=parseFloat(document.VR.RS.value);
  if (isNaN(RS)) { alert("RS is not a number"); return; }
  var RU=parseFloat(document.VR.RU.value);
  if (isNaN(RU)) { alert("RU is not a number"); return; }
  var region=document.VR.region.value;
  switch (region){
   case "A" :
      document.VR.cycl[0].checked=true;
      fx1="67-41*pow(RS,-0.1)";
      fx2="67-41*pow(RU,-0.1)";
      break;
   case "W" :
      document.VR.cycl[0].checked=true;
      fx1="104-70*pow(RS,-0.045)";
      fx2="104-70*pow(RU,-0.045)";
      break;
   case "B" :
      document.VR.cycl[0].checked=true;
      fx1="106-92*pow(RS,-0.1)";
      fx2="106-92*pow(RU,-0.1)";
      break;
   case "C" :
      document.VR.cycl[1].checked=true;
      fx1="122-104*pow(RS,-0.1)";
      fx2="1.05*(122-104*pow(RU,-0.1))";
      break;
   case "D" :
      document.VR.cycl[1].checked=true;
      fx1="156-142*pow(RS,-0.1)";
      fx2="1.1*(156-142*pow(RU,-0.1))";
      break;
   default : 
      fx1=document.VR.fx1.value;
      fx2=document.VR.fx2.value;
  }
  document.VR.fx1.value=fx1;
  document.VR.fx2.value=fx2;

  fx1=document.VR.fx1.value;
  fx2=document.VR.fx2.value;
  var tmp1="";
  var tmp2="";
  with (Math) tmp1=eval(fx1);
  tmp1 = Math.round(tmp1);
  with (Math) tmp2=eval(fx2);
  tmp2 = Math.round(tmp2);

  document.VR.VRS.value=tmp1;
  document.VR.VRU.value=tmp2;
  PutCookie("VRS",escape(tmp1));
  PutCookie("VRU",escape(tmp2));
}

function avdist() {
  var HT=parseFloat(document.MZC.HT.value);
  if (isNaN(HT)) { alert("HT is not a number"); return; }
  var AD=0;

  if (HT < 50) {AD = 1000;}
  if (HT >= 50 && HT < 100) {AD = 2000;}
  if (HT == 100) {AD = 3000;}
  if (HT > 100) {
    AD = 3000;
	HT = 100;
	document.MZC.HT.value = HT;
	alert("This program doesn't handle heights over 100 m");
	return;
  }
  var R = 20*HT;
  document.MZC.AD.value=AD;
  document.MS.HTS.value=HT;
  document.MS.R.value=R;
  document.MT.ZMT.value=HT;
}

function getMZC()
{
  var MZ = new Array();
  var MU = new Array();

  var HT=parseFloat(document.MZC.HT.value);
  var AD = parseFloat(document.MZC.AD.value);
  if (isNaN(AD)) { alert("AD is not a number"); return; }

  var D1=parseFloat(document.MZC.D1.value);
  if (isNaN(D1) || D1<=0) { alert("D1 must be a number > 0"); document.MZC.D1.focus(); return; }
  if (D1 > AD) {
    D1 = AD;
	D2 = 0;
	D3 = 0;
	document.MZC.D1.value = D1;
	document.MZC.D2.value = 0;
	document.MZC.D3.value = 0;
  }

  var D2=parseFloat(document.MZC.D2.value);
  if (D1 < AD  && (isNaN(D2) || D2<=0)) { alert("D2 must be a number > 0"); document.MZC.D2.focus(); return; }
  if (D1+D2 > AD) {
    D2 = AD-D1;
	D3 = 0;
	document.MZC.D2.value = D2;
	document.MZC.D3.value = 0;
  }

  var D3=parseFloat(document.MZC.D3.value);
  if (D1+D2+D3 != AD) {
    D3 = AD - (D1+D2);
	document.MZC.D3.value = D3; 
  }
  
  var TC1=parseFloat(document.MZC.TC1.value);
  if (TC1 < 1) {
    TC1 = 1.0;
	document.MZC.TC1.value = 1.0;
  }
  if (TC1 > 4) {
    TC1 = 4.0;
	document.MZC.TC1.value = 4.0;
  }

  var TC2=parseFloat(document.MZC.TC2.value);
  if (TC2 < 1) {
    TC2 = 1.0;
	document.MZC.TC2.value = 1.0;
  }
  if (TC2 > 4) {
    TC2 = 4.0;
	document.MZC.TC2.value = 4.0;
  }

  var TC3=parseFloat(document.MZC.TC3.value);
  if (TC3 < 1) {
    TC3 = 1.0;
	document.MZC.TC3.value = 1.0;
  }
  if (TC3 > 4) {
    TC3 = 4.0;
	document.MZC.TC3.value = 4.0;
  }

  var TCarr = new Array();
  TCarr[0]=TC1;
  if (D2>0) { TCarr[1]=TC2; }
  if (D3>0) { TCarr[2]=TC3; }
//  alert("TCarr = "+TCarr);
   
  var Z=document.MZC.Z.value;
//  alert("HT = "+HT+"\nAD = "+AD+"\nD1,2,3 = "+D1+", "+D2+", "+D3+"\nTC1,2,3 = "+TC1+", "+TC2+", "+TC3+"\nZ = "+Z);
  var Zarr = new Array();
  var Zval = new Array();
  Zarr = Z.split(",");
//  alert("Zarr = "+Zarr+", length = "+Zarr.length);
  var i=0;
  var str="";
  for(i=0;i<Zarr.length;i++)
  {
	Zarr[i] = parseFloat(Zarr[i]);
//	str = str + "element["+i+"] = "+ Zarr[i] +"\n";
  }
//  alert(str);
  var out = "";
//  alert("out = nothing\nZarr[0] = "+Zarr[0]);
  if (isNaN(Zarr[0]) || Zarr[0]>=HT) {
    Zval[0] = HT;
	out = HT;
//	alert("Zarr[0]>=HT, out = "+out);
  }
  else {
    for (i=0;i<Zarr.length;i++) {
	  if (i != Zarr.length-1) {
	    if (Zarr[i]>=HT) {
		  Zval[i] = HT;
		  out += HT;
//          alert("Zarr["+i+"]>=HT, out = "+out);
		  break;
		} else { 
		  Zval[i] = Zarr[i]; 
    	  out = out + Zval[i] + ",";
//          alert("Zarr["+i+"]<HT, out = "+out);
		}
	  } else {
	    if (Zarr[i]>=HT) {
		  Zval[i] = HT;
		  out += HT;
//          alert("Zarr["+i+"]>=HT, out = "+out);
		} else {
		  Zval[i] = Zarr[i];
    	  out = out + Zval[i] + ",";
		  Zval[i+1] = HT;
		  out += HT;
//          alert("Zarr["+i+"]<HT, out = "+out);
		}
	  }
	}
  }
  document.MZC.Z.value = out;
  PutCookie("MZCZ",escape(out));
  var tmpHT, HI, j;
  var MZtmp = new Array();
  var MUtmp = new Array();
  var Z0R = new Array();
  var XI = new Array();

  if (D2>0) {
    Z0R[0]=2*Math.pow(10,(Math.max(TC1,TC2)-4));
  }
  if (D3>0) {
    Z0R[1]=2*Math.pow(10,(Math.max(TC2,TC3)-4));
  }
//  alert("Z0R = "+Z0R);

  cyclonic = (document.VR.cycl[1].checked) ? 1 : 0;
//  alert("cyclonic = "+cyclonic);
  out="";
  for (i=0;i<Zval.length;i++) {
    tmpHT = Zval[i];
    HI = SetHI(tmpHT);
//	alert("tmpHT = "+tmpHT+"\nHI = "+HI);
    MZtmp = new Array();
    MUtmp = new Array();
    for (j=0;j<TCarr.length;j++) {
	  MZtmp[j] = SetMZ(TCarr[j],tmpHT,HI);
//	  alert("MZtmp["+j+"] = "+MZtmp[j]);
	  if (cyclonic=="1") {
        MUtmp[j] = SetMU(TCarr[j],tmpHT,HI);
//        alert("MUtmp["+j+"] = "+MUtmp[j]);
	  }
	}
    XI = new Array();
	if (D2>0) {
	  XI[0] = Math.round(Z0R[0]*Math.pow(tmpHT/(0.3*Z0R[0]), 1.25));
	} else {
	  XI[0] = 0;
	}
	if (D3>0) {
	  XI[1] = Math.round(Z0R[1]*Math.pow(tmpHT/(0.3*Z0R[1]), 1.25));
	} else {
	  XI[1] = 0;
	}
//	alert("XI = "+XI);
	MZ[i]= MZtmp[0]*(D1-XI[0]);
	if (! isNaN(MZtmp[1])) {MZ[i]+= MZtmp[1]*(D2+XI[0]-XI[1]);}
	if (! isNaN(MZtmp[2])) {MZ[i]+= MZtmp[2]*(D3+XI[1]);}
	MZ[i] = Math.round(100*MZ[i]/AD)/100;
	out+=MZ[i];
//	alert("out = "+out);
	MU[i]=MUtmp[0]*(D1-XI[0]);
	if (! isNaN(MUtmp[1])) {MU[i]+= MUtmp[1]*(D2+XI[0]-XI[1]);}
	if (! isNaN(MUtmp[2])) {MU[i]+= MUtmp[2]*(D3+XI[1]);}
	MU[i] = Math.round(100*MU[i]/AD)/100;
//	alert("MZ["+i+"] = "+MZ[i]+"\nMU["+i+"] = "+MU[i]);
	if (! isNaN(MU[i])) {
      out+="/"+MU[i]+", ";
	} else {
	  out+=", ";
	}
//	alert("out = "+out);
  }
  document.MZC.MZC.value=out;
  PutCookie("MZCMZC",escape(out));

//  alert("Not yet fully functional");
}

function SetMZ(CAT,H,I) {
//  alert("in SetMZ:\nCAT = "+CAT+"\nH = "+H+"\nI = "+I);
  var tmp1, tmp2, tmp3, tmp4;
  if (CAT < 2) {
    tmp1 = T11[I-1];
    tmp2 = T11[I];
    tmp3 = T12[I-1];
    tmp4 = T12[I];
  } else {
    if (CAT>=2 && CAT<3) {
      tmp1 = T12[I-1];
      tmp2 = T12[I];
      tmp3 = T13[I-1];
      tmp4 = T13[I];
	} else {
	  if (CAT>=3 && CAT<4) {
        tmp1 = T13[I-1];
        tmp2 = T13[I];
        tmp3 = T14[I-1];
        tmp4 = T14[I];
      } else {
        tmp1 = T14[I-1];
        tmp2 = T14[I];
        tmp3 = T14[I-1];
        tmp4 = T14[I];
	  }
	}
  }
  tmp1 = tmp1 + (tmp2-tmp1)*(H-TBLZ[I-1])/(TBLZ[I]-TBLZ[I-1]);
  tmp2 = tmp3 + (tmp4-tmp3)*(H-TBLZ[I-1])/(TBLZ[I]-TBLZ[I-1]);
  return tmp1 + (tmp2-tmp1)*(CAT-Math.floor(CAT));
}

function SetMU(CAT,H,I) {
//  alert("in SetMU:\nCAT = "+CAT+"\nH = "+H+"\nI = "+I);
  var tmp1, tmp2, tmp3, tmp4;
  if (CAT < 2) {
    tmp1 = T212[I-1];
    tmp2 = T212[I];
    tmp3 = 0;
    tmp4 = 0;
  } else {
    if (CAT>=3) {
      tmp1 = T234[I-1];
      tmp2 = T234[I];
      tmp3 = 0;
      tmp4 = 0;
	} else {
      tmp1 = T212[I-1];
      tmp2 = T212[I];
      tmp3 = T234[I-1];
      tmp4 = T234[I];
	}
  }
  tmp1 = tmp1 + (tmp2-tmp1)*(H-TBLZ[I-1])/(TBLZ[I]-TBLZ[I-1]);
  tmp2 = tmp3 + (tmp4-tmp3)*(H-TBLZ[I-1])/(TBLZ[I]-TBLZ[I-1]);
  return tmp1 + (tmp2-tmp1)*(CAT-Math.floor(CAT));
}

function SetHI(Zval) {
	var i;

	for (i = 1; i < 10; i++) {
		if (Zval <= TBLZ[i]) {
			HI = i;
			break;
		}
	}
	return Math.min(HI, 9);
}

function SetMS() {
  var HT = parseFloat(document.MS.HTS.value);
  var HS = parseFloat(document.MS.HS.value);
  if (HS<HT) {
    document.MS.MS.value = 1.0;
	return;
  } 
  var NS = parseFloat(document.MS.NS.value);
  var BS = parseFloat(document.MS.BS.value);
  var LS = HT*(10/NS+5);
  var S = LS/Math.sqrt(HS*BS);
  var MS = 1.0;
  if (S<=1.5) {
    MS = 0.7;
  } else {
    if (S<=3.0) {
	  MS = 0.7+.1*(S-1.5)/1.5;
	} else {
	  if (S<=6.0) {
	    MS = 0.8+.1*(S-3)/3;
	  } else {
	    if (S<12) {
		  MS = 0.9+.1*(S-6)/6;
		} else {
		  MS = 1.0;
		}
	  }
	}
  }
  MS = Math.round(MS*100)/100;
  document.MS.MS.value = MS;
  PutCookie("MS",escape(MS));
}

function SetR() {
  var HT = parseFloat(document.MS.HTS.value);
  var R = Math.round(20*HT);
  document.MS.R.value = R;
}

function shielding(which) {
  if (which=="0") {
    document.MS.HTS.disabled=true;
    document.MS.R.disabled=true;
    document.MS.NS.disabled=true;
    document.MS.HS.disabled=true;
    document.MS.BS.disabled=true;
    document.MS.button.disabled=true;
    document.MS.MS.value=1.0;
  } else {
    document.MS.HTS.disabled=false;
    document.MS.R.disabled=false;
    document.MS.NS.disabled=false;
    document.MS.HS.disabled=false;
    document.MS.BS.disabled=false;
    document.MS.button.disabled=false;
	SetMS();
  }
}

function SetMT() {
  var H = parseFloat(document.MT.H.value);
  var LU = parseFloat(document.MT.LU.value);
  var X = parseFloat(document.MT.X.value);
  var Z = parseFloat(document.MT.ZMT.value);
  var E = parseFloat(document.MT.E.value);
  var LEE = parseFloat(document.MT.LEE.value);
  var L1 = Math.max(0.36*LU, 0.4*H);
  var L2U = 4*L1;
  var L2D = 10*L1;
  var MH, MLEE, MT, TMP, fromcrest, hilltype;
  fromcrest = document.MT.crest[0].checked ? "upwind" : "downwind";
  hilltype = document.MT.hilltype[0].checked ? "hill" : "escarpment";
//  alert("fromcrest = "+fromcrest+"\nhilltype = "+hilltype);
  TMP = H/(2*LU); 
  if (TMP < 0.05) {
    MH = 1.0;
  } else {
    if (TMP >= 0.45) {
	  if (hilltype=="escarpment" && fromcrest=="downwind") {
    	MH = 1+0.71*(1-X/L2D);
	  } else {
    	MH = 1+0.71*(1-X/L2U);
	  }
	} else {
	  if (hilltype=="escarpment" && fromcrest=="downwind") {
	    MH = 1+H/(3.5*(Z+L1))*(1-X/L2D);
	  } else {
	    MH = 1+H/(3.5*(Z+L1))*(1-X/L2U);
	  }
	}
  }
  if (MH<1) { MH=1.0; }

  if (! isNaN(LEE)) {
    if (LEE<=12) {
	  MLEE = 1.35;
	} else {
	  LEE = Math.min(30, LEE);
	  MLEE = 1+.35*(30-LEE)/18;
	}
  } else {
	MLEE = 1.0;
  }
//  alert("MLEE = "+MLEE);
  if (! isNaN(E) ) {
    if (E<500) {
	  E=500;
	  document.MT.E.value = E;
	}
	MT = MH*MLEE*(1+0.00015*E);
//    alert("E = "+E);
  } else {
    MT = Math.max(MH,MLEE);
  }
  MT = Math.round(100*MT)/100;
  document.MT.MT.value = MT;
  PutCookie("MT",escape(MT));
}

function SetVSB() {
  var VRS = parseFloat(document.VR.VRS.value);
  if (isNaN(VRS)) {
    alert("VRS is not a number");
	document.VR.VRS.focus();
	return;
  } else {
   	PutCookie("VRS",escape(VRS));
  }
  var VRU = parseFloat(document.VR.VRU.value);
  if (isNaN(VRU)) {
    alert("VRU is not a number");
	document.VR.VRU.focus();
	return;
  } else {
   	PutCookie("VRU",escape(VRU));
  }
  var MD1 = parseFloat(document.MD.MD1.value);
  if (isNaN(MD1)) {
    MD1=0.95;
	document.MD.MD1.value = MD1;
  }
  var MD2 = parseFloat(document.MD.MD2.value);
  if (isNaN(MD2)) {
    MD2=1.0;
	document.MD.MD2.value = MD2;
  }
  PutCookie("MD1",escape(MD1));
  PutCookie("MD2",escape(MD2));
  var Zval= document.MZC.Z.value;
  var tmp = document.MZC.MZC.value;
  var MZC = new Array();
  var MZU = new Array();
  var tmp2 = tmp.indexOf(",");
//  alert("tmp = "+tmp+"\ntmp2 = "+tmp2+"MZC = "+MZC);
  if (tmp2==-1) {
    if (isNaN(parseFloat(tmp))) {
      alert("MZC must be a number or comma-separated array of numbers");
	  document.MZC.MZC.focus();
	  return;
	} else {
	  MZC[0] = parseFloat(tmp);
//	  alert("MZC = "+MZC[0]);
	}
  } else {
    var tmp3 = new Array();
    var tmp4 = new Array();
	var tmp5;
    tmp3 = tmp.split(",");
	var len=tmp3.length;
//	alert("tmp3 = "+tmp3+"\nlen = "+len);
    for (i=0;i<len;i++) {
	  tmp2 = tmp3[i].indexOf("/");
	  if (tmp2==-1) {
	    tmp5 = parseFloat(tmp3[i]);
	    if (! isNaN(tmp5)) {
		  MZC[i] = parseFloat(tmp3[i]);
		  MZU[i] = MZC[i];
	    }
	  } else {
	    tmp4 = tmp3[i].split("/");
		MZC[i] = parseFloat(tmp4[0]);
		MZU[i] = parseFloat(tmp4[1]);
	  }
	}
	PutCookie("MZC",escape(MZC));
	PutCookie("MZU",escape(MZU));
  }
//  alert("MZC = "+MZC+"\nMZU = "+MZU);

  var MS = parseFloat(document.MS.MS.value);
  if (isNaN(MS)) {
    alert("MS is not a number");
	document.MS.MS.focus();
	return;
  } else {
   	PutCookie("MS",escape(MS));
  }
  var MT = parseFloat(document.MT.MT.value);
  if (isNaN(MT)) {
    alert("MT is not a number");
	document.MT.MT.focus();
	return;
  } else {
   	PutCookie("MT",escape(MT));
  }
  VS = new Array();
  VU = new Array();
  VO = new Array();
  for (i=0;i<MZC.length;i++) {
    VS[i] = Math.round(VRS*MD2*MZC[i]*MS*MT);
	VU[i] = Math.round(VRU*MD2*MZU[i]*MS*MT);
	if (VU[i] < 30) { VU[i]=30; }
    VO[i] = Math.round(VRU*MD1*MZU[i]*MS*MT);
//	alert("VS = "+VS[i]+", VU = "+VU[i]+", VO = "+VO[i]);
  }
  document.VSB.Zval.value = Zval;
  PutCookie("Zval",escape(Zval));
  document.VSB.VS.value = VS;
  PutCookie("VS",escape(VS));
  document.VSB.VU.value = VU;
  PutCookie("VU",escape(VU));
  document.VSB.VO.value = VO;
  PutCookie("VO",escape(VO));
}

function VSBreport() {
  alert("Not yet programmed");
}

function populate() {
  var VRS=GetCookie("VRS");
  document.VR.VRS.value=VRS;
  var VRU=GetCookie("VRU");
  document.VR.VRU.value=VRU;
  var MD1=GetCookie("MD1");
  document.MD.MD1.value=MD1;
  var MD2=GetCookie("MD2");
  document.MD.MD2.value=MD2;
  var MZCZ=GetCookie("MZCZ");
  document.MZC.Z.value=MZCZ;
  var MZCMZC=GetCookie("MZCMZC");
  document.MZC.MZC.value=MZCMZC;
  var MS=GetCookie("MS");
  document.MS.MS.value=MS;
  var MT=GetCookie("MT");
  document.MT.MT.value=MT;
  var Zval=GetCookie("Zval");
  var VS=GetCookie("VS");
  var VU=GetCookie("VU");
  var VO=GetCookie("VO");
  document.VSB.Z.value = Zval;
  document.VSB.VS.value = VS;
  document.VSB.VU.value = VU;
  document.VSB.VO.value = VO;
}

function populateZV() {
  var Zval=GetCookie("Zval");
  var VS=GetCookie("VS");
  var VU=GetCookie("VU");
  var VO=GetCookie("VO");
  document.ZV.Z.value = Zval;
  document.ZV.VS.value = VS;
  document.ZV.VU.value = VU;
  document.ZV.VO.value = VO;
}

function saveZV() {
  var Zval = document.ZV.Z.value;
  PutCookie("Zval",escape(Zval));
  var VS = document.ZV.VS.value;
  PutCookie("VS",escape(VS));
  var VU = document.ZV.VU.value;
  PutCookie("VU",escape(VU));
  var VO = document.ZV.VO.value;
  PutCookie("VO",escape(VO));
}

function setCPI(what) {
  switch (what) {
  case "one" :
    document.CPI.posCPI.value="0.6";
    document.CPI.negCPI.value="-0.3";
	document.dimension.table51b.options[0].selected=true;
	document.dimension.openingloc.options[2].selected=true;
	break;
  case "two" :
    document.CPI.posCPI.value="0.2";
    document.CPI.negCPI.value="-0.3";
	document.dimension.table51b.options[0].selected=true;
	document.dimension.openingloc.options[2].selected=true;
	break;
  case "all" :
    document.CPI.posCPI.value="0.0";
    document.CPI.negCPI.value="-0.3";
	document.dimension.table51b.options[1].selected=true;
	document.dimension.openingloc.options[2].selected=true;
	break;
  case "sealed" :
    document.CPI.posCPI.value="0.0";
    document.CPI.negCPI.value="-0.2";
	document.dimension.table51b.options[0].selected=true;
	document.dimension.openingloc.options[2].selected=true;
	break;
  default:
    document.CPI.posCPI.value="";
    document.CPI.negCPI.value="";
	document.dimension.table51b.options[5].selected=true;
	document.dimension.openingloc.options[0].selected=true;
  }
}

function setHeight() {
  var Zval=GetCookie("Zval");
  var Zarr= new Array();
  Zarr=Zval.split(",");
  var len=Zarr.length;
  var HeqZ = "";
  var H=parseFloat(Zarr[len-1]);
  if (len==1) {HeqZ = "*";}
  if ( H==0 || isNaN(H) ) {
    H=parseFloat(Zarr[len-2]);
    if (len==2) {HeqZ = "*";}
  }
  document.dimension.height.value=H+HeqZ;
}

function Process() {
  alert("Not yet programmed");
}

function setCPE(which) {
  var mult=0, cpeW=.8, varies=true;
  var tmp = document.dimension.height.value;
//  alert("tmp = "+tmp);
  if (tmp.indexOf("*")>-1) {
    varies=false;
	H = parseFloat(tmp.substr(0,tmp.indexOf("*")));
  } else {
    H = parseFloat(tmp);
  }
  var L = parseFloat(document.dimension.length.value);
  var W = parseFloat(document.dimension.width.value);
  var P = parseFloat(document.dimension.pitch.value);
  if (isNaN(L) || isNaN(W) || isNaN(P)) {return false;}
  var elevated = document.dimension.elevated[0].checked ? true : false;
  var rooftype = document.dimension.rooftype[0].checked ? "hip" : "gable";
  var pitchtype = document.dimension.pitchtype[0].checked ? "single" : "double";

  for (var i=0;i<6;i++) {
    if (document.dimension.table51b.options[0].selected) {
//	  alert("NA");
      document.CPI.permeability[0].checked=true;
	  setCPI('one');
	  mult=0;
	  break;
	}
    if (document.dimension.table51b.options[1].selected) {
//	  alert("<=0.5");
      document.CPI.permeability[2].checked=true;
	  setCPI('all');
	  mult=0;
	  break;
	}
    if (document.dimension.table51b.options[2].selected) {
//	  alert("1");
      document.CPI.permeability[4].checked=true;
	  document.CPI.negCPI.value="-0.3";
      if (document.dimension.openingloc.options[1].selected) {
	    mult = 0.15;
	  } else {
	    mult = 0;
	    document.CPI.posCPI.value="0.2";
		document.dimension.openingloc.options[0].selected=true;
	  }
	  break;
	}
    if (document.dimension.table51b.options[3].selected) {
//	  alert("2");
      document.CPI.permeability[4].checked=true;
	  mult = 0.7;
      if (document.dimension.openingloc.options[2].selected) {
	    document.dimension.openingloc.options[0].selected=true;
	  }
	  break;
	}
    if (document.dimension.table51b.options[4].selected) {
//	  alert("3");
      document.CPI.permeability[4].checked=true;
	  mult = 0.85;
      if (document.dimension.openingloc.options[2].selected) {
	    document.dimension.openingloc.options[0].selected=true;
	  }
	  break;
	} else {
//	  alert(">=6");
      document.CPI.permeability[4].checked=true;
	  mult = 1;
      if (document.dimension.openingloc.options[2].selected) {
	    document.dimension.openingloc.options[0].selected=true;
	  }
	  break;
	}
  }
//  alert("in setCPE:\r\nH = "+H+"\r\nL = "+L+"\r\nW = "+W+"\r\nP = "+P+"\r\nelevated = "+elevated+"\r\nrooftype = "+rooftype+"\r\npitchtype = "+pitchtype+"\r\nvaries = "+varies);
  if (H > 25.0 || elevated || varies) {
    cpeW = 0.8;
  } else {
    cpeW = 0.7;
  }
  document.dimension.cpeW.value = cpeW;
  
  var cpeS = -.65;
  document.dimension.cpeS.value = cpeS;
  
  var cpeL = 0.0;
  var donb = Math.min(L/W, W/L);
//  alert("donb = "+donb);
  if (P < 10) {
    cpeL = -0.5;
  } else {
    if (P <= 15) {
	  cpeL = -0.3;
	} else {
	  if ( P <= 20 ) {
	    cpeL = -0.3 -0.1*(P-15)/5;
	  } else {
	    if ( P >= 25 ) {
		  if (donb <= 0.1) {
		    cpeL = -0.75;
		  } else {
		    if (donb >= 0.3) {
			  cpeL = -0.5;
			} else {
			  cpeL = -0.75 + 0.25*(donb-.1)/.2;
			}
		  }
		} else {
		  if (donb <= 0.1) {
		    cpeL = -0.4 -.35*(P-20)/5;
		  } else {
		    if (donb >= 0.3) {
			  cpeL = -0.4 -.1*(P-20)/5;
			} else {
			  cpeL = -.4 -(0.35 - 0.25*(donb-.1)/.2)*(P-20)/5;
			}
		  }
		}
	  }
	}
  }
  document.dimension.cpeL.value = Math.round(cpeL*100)/100;
  var hond = 0.0, t1=0.0, t2=0.0;
  var cpeU=0.0, cpeD=0.0, cpeR=0.0, cpeUp=0.0, cpeDn=0.0, cpeRg=0.0;
  if ( P < 10) {
//    alert("we are in Table 5.3(A)");
    hond = H/W;
//    alert("5.3A cross = H/W = "+hond);
	if (hond <= 0.5) {
	  cpeU = -0.9;
	  cpeUp = 0.2;
	} else {
	  if (hond >= 1.0) {
        cpeU = -1.3;
	    cpeUp = 0.0;
	  } else {
        cpeU = -1.3 + 0.4*(1-hond)/.5;
	    cpeUp = 0.0 + 0.2*(1-hond)/.5;
	  }
	}
    if (rooftype == "hip") {
	  cpeHip = Table53C(P, hond, W/L);
//    Table 5.3(A) longwind
      hond = H/L;
//      alert("5.3A long = H/L = "+hond);
      if (hond <= 0.5) {
	    cpeR = -0.9;
	    cpeRg = 0.2;
	  } else {
	    if (hond >= 1.0) {
          cpeR = -1.3;
	      cpeRg = 0.0;
	    } else {
          cpeR = -1.3 + 0.4*(1-hond)/.5;
	      cpeRg = 0.0 + 0.2*(1-hond)/.5;
	    }
	  }
	  cpeR = Math.min(cpeR, cpeHip);
    }
  }

  if (rooftype == "gable") {
    hond = H/L;
//    alert("gable = H/L = "+hond);
//    alert("we are in Table 5.3(A)");
	if (hond <= 0.5) {
	  cpeR = -0.9;
	  cpeRg = 0.2;
	} else {
	  if (hond >= 1.0) {
	    cpeR = -1.3;
	    cpeRg = 0.0;
	  } else {
	    cpeR = -1.3 + 0.4*(1-hond)/.5;
	    cpeRg = 0.0 + 0.2*(1-hond)/.5;
	  }
	}
  }
  
  if (P >=10) {
//  Table 5.3(B) crosswind
    hond = H/W;
//    alert("5.3B cross = H/W = "+hond);
    cpeU = Table53Uneg(P, hond);
    cpeUp = Table53Upos(P, hond);
	cpeD = Table53C(P, hond, L/W);
    if (rooftype == "hip") {
//    Table 5.3(B) longwind
      hond = H/L;
//      alert("5.3B long = H/L = "+hond);
      cpeR = Table53Uneg(P, hond);
	  cpeHip = Table53C(P, hond, W/L);
//	  alert("cpeR = "+cpeR+"\r\ncpeHip = "+cpeHip);
	  cpeR = Math.min(cpeR, cpeHip);
//	  alert("cpeR = "+cpeR);
	  cpeRg = Table53Upos(P, hond);
    }
  }
  
  document.dimension.cpeU.value = Math.round(cpeU*100)/100;
  document.dimension.cpeD.value = Math.round(cpeD*100)/100;
  document.dimension.cpeR.value = Math.round(cpeR*100)/100;
  document.dimension.cpeUp.value = Math.round(cpeUp*100)/100;
  if (rooftype == "hip") {
    document.dimension.cpeHip.value = Math.round(cpeHip*100)/100;
  } else {
    document.dimension.cpeHip.value = "NA";
  }
  document.dimension.cpeRg.value = Math.round(cpeRg*100)/100;
  if (!mult==0) {
//    alert("mult = "+mult);
//	alert("roof selected? "+document.dimension.openingloc.options[1].selected);
    if (document.dimension.openingloc.options[1].selected) {
	  t1 = Math.min(cpeU, cpeD, cpeHip, cpeR);
	  t2 = Math.max(cpeUp, cpeRg);
//	  alert("t1 = "+t1+"\r\nt2 = "+t2);
      document.CPI.negCPI.value=Math.round(t1*mult*100)/100;
      document.CPI.posCPI.value=Math.round(t2*mult*100)/100;
	} else {
//      alert("Wall mult = "+mult);
      t1 = Math.min(cpeL, cpeS);
//      alert("t1 = "+t1);
      document.CPI.negCPI.value=Math.round(t1*mult*100)/100;
      document.CPI.posCPI.value=Math.round(cpeW*mult*100)/100;
	}
  }
}  
  
function Table53Uneg(P, hond) {
  var t1=0, t2=0;
  if (P >= 10 && P <= 15) {
	if (hond <= 0.25) {
	  return -0.7 + .2*(P-10)/5;
	} else {
	  if (hond >= 1.0) {
	    return -1.3 + .3*(P-10)/5;
	  } else {
		if (hond <= 0.5) {
		  t1 = -0.9 + 0.2*(.5-hond)/.25;
		  t2 = -0.7 + 0.2*(.5-hond)/.25;
		  return t1 + (t2-t1)*(P-10)/5;
		} else {
		  t1 = -1.3 + 0.4*(1-hond)/.5;
		  t2 = -1.0 + 0.3*(1-hond)/.5;
		  return t1 + (t2-t1)*(P-10)/5;
		}
	  }
	}
  }
  if (P > 15 && P <=20) {
    if (hond <= 0.25) {
      return -0.5 + .2*(P-15)/5;
	} else {
	  if (hond >= 1) {
	    return -1.0 + .3*(P-15)/5;
	  } else {
		if (hond <= 0.5) {
		  t1 = -0.7 + 0.2*(.5-hond)/.25;
		  t2 = -0.4 + 0.1*(.5-hond)/.25;
		  return t1 + (t2-t1)*(P-15)/5;
		} else {
		  t1 = -1.0 + 0.3*(1-hond)/.5;
		  t2 = -0.7 + 0.3*(1-hond)/.5;
		  return t1 + (t2-t1)*(P-15)/5;
		}
	  }
	}
  }
  if (P >20 && P<=25) {
    if (hond <= 0.25) {
	  return -0.3 + .1*(P-20)/5;
	} else {
	  if (hond >= 1) {
	  return -0.7 + .2*(P-20)/5;
	  } else {
	    if (hond <= 0.5) {
	      t1 = -0.4 + 0.1*(.5-hond)/.25;
          t2 = -0.3 + 0.1*(.5-hond)/.25;
	      return t1 + (t2-t1)*(P-20)/5;
		} else {
		  t1 = -0.7 + 0.3*(1-hond)/.5;
		  t2 = -0.5 + 0.2*(1-hond)/.5;
		  return t1 + (t2-t1)*(P-20)/5;
		}
	  }
    }
  }
  if (P>25 && P<=30) {
    if (hond <= 0.25) {
	  return -0.2;
	} else {
	  if (hond >= 1) {
	    return -0.5 + .2*(P-25)/5;
	  } else {
		if (hond <= 0.5) {
		  t1 = -0.3 + 0.1*(.5-hond)/.25;
		  t2 = -0.2;
		  return t1 + (t2-t1)*(P-25)/5;
		} else {
		  t1 = -0.5 + 0.2*(1-hond)/.5;
		  t2 = -0.3 + 0.1*(1-hond)/.5;
		  return t1 + (t2-t1)*(P-25)/5;
		}
	  }
	}
  }
  if (P>30 && P<=35) {
    if (hond <= 0.25) {
	  return -0.2 + 0.2*(P-30)/5;
	} else {
	  if (hond >= 1) {
	    return -0.3 + .1*(P-30)/5;
	  } else {
		if (hond <= 0.5) {
		  t2 = -0.2 + 0.2*(.5-hond)/.25;
		  t1 = -0.2;
		  return t1 + (t2-t1)*(P-30)/5;
		} else {
		  t2 = -0.2;
		  t1 = -0.3 + 0.1*(1-hond)/.5;
		  return t1 + (t2-t1)*(P-30)/5;
		}
	  }
	}
  }
  if (P > 35 && P < 45) {
    if (hond <= 0.25) {
	  return 0.0;
	} else {
	  if (hond >= 1) {
	    return -0.2 + .2*(P-35)/10;
	  } else {
		if (hond <= 0.5) {
		  t1 = -0.2 + 0.2*(.5-hond)/.25;
		  t2 = 0.0;
		  return t1 + (t2-t1)*(P-35)/10;
		} else {
		  t1 = -0.2;
		  t2 = -0;
		  return t1 + (t2-t1)*(P-35)/10;
		}
	  }
	}
  }
  if (P>=45) {
    return 0;
  }
}

function Table53Upos(P, hond) {
  var t1=0, t2=0;
  if (P >= 10 && P <= 15) {
    return 0;
  }
  if (P > 15 && P <=20) {
    if (hond <= 0.25) {
	  return .2*(P-15)/5;
	} else {
	  if (hond >= 1) {
		return 0;
	  } else {
		if (hond <= 0.5) {
		  t1 = 0;
		  t2 = .2*(.5-hond)/.25;
		  return t1 + (t2-t1)*(P-15)/5;
		} else {
		  return 0;
		}
	  }
	}
  }
  if (P >20 && P<=25) {
    if (hond <= 0.25) {
	  return .2 + .1*(P-20)/5;
	} else {
	  if (hond >= 1) {
	  return 0;
	  } else {
	    if (hond <= 0.5) {
		  t1 = .2*(.5-hond)/.25;
		  t2 = .2 + .1*(.5-hond)/.25;
		  return t1 + (t2-t1)*(P-20)/5;
		} else {
		  t1 = 0;
		  t2 = .2*(1-hond)/.5;
		  return t1 + (t2-t1)*(P-20)/5;
		}
	  }
    }
  }
  if (P>25 && P<=30) {
    if (hond <= 0.25) {
	  return .3 + .1*(P-25)/5;
	} else {
	  if (hond >= 1) {
	    return 0.2*(P-25)/5;
	  } else {
		if (hond <= 0.5) {
		  t1 = 0.2 + .1*(.5-hond)/.25;
		  t2 = 0.3 + .1*(.5-hond)/.25;
		  return t1 + (t2-t1)*(P-25)/5;
		} else {
		  t1 = 0.0 + 0.2*(1-hond)/.5;
		  t2 = 0.2 + 0.1*(1-hond)/.5;
		  return t1 + (t2-t1)*(P-25)/5;
		}
	  }
	}
  }
  if (P>30 && P<=35) {
    if (hond <= 0.25) {
	  return .4 + .1*(P-30)/5;
	} else {
	  if (hond >= 1) {
	    return .2 + 0.1*(P-30)/5;
	  } else {
		if (hond <= 0.5) {
		  t1 = 0.3 + .1*(.5-hond)/.25;
		  t2 = 0.4 + .1*(.5-hond)/.25;
		  return t1 + (t2-t1)*(P-30)/5;
		} else {
		  t1 = 0.2 + 0.1*(1-hond)/.5;
		  t2 = 0.3 + 0.1*(1-hond)/.5;
		  return t1 + (t2-t1)*(P-30)/5;
		}
	  }
	}
  }
  if (P > 35 && P < 45) {
    t2 = 0.8*Math.sin(45*Math.PI/180);
    if (hond <= 0.25) {
	  t1 = .5;
	} else {
	  if (hond >= 1) {
	    t1 = .3;
	  } else {
		if (hond <= 0.5) {
		  t1 = 0.4 + .1*(.5-hond)/.25;
		} else {
		  t1 = 0.3 + 0.1*(1-hond)/.5;
		}
	  }
	}
	return t1 + (t2-t1)*(P-35)/10;
  }
  if (P>=45) {
	return 0.8*Math.sin(P*Math.PI/180);
  }
}

function Table53C(P, hond, bond) {
  var t1=0, t2=0;
  
  if (P<10) {
    if (hond <= .25) {
	  retun -.3;
	} else {
	  if (hond >= 1) {
	    return -.7;
	  } else {
	    if (hond <= .5) {
		  return -0.5 + 0.2*(.5-hond)/.25;
		} else {
		  return -.7 + 0.2*(1-hond)/.5;
		}
	  }
	} 
  }
  
  if (P>=10 && P<=15) {
    if (hond <= .25) {
	  return -.3 -.2*(P-10)/5;
	} else {
	  if (hond >= 1) {
	    return -.7 + .1*(P-10)/5;
	  } else {
	    if (hond <= .5) {
		  t1 = -0.5 + 0.2*(.5-hond)/.25;
		  t2 = -0.5;
		  return t1 + (t2-t1)*(P-10)/5;
		} else {
		  t1 = -.7 + 0.2*(1-hond)/.5;
		  t2 = -.6 + 0.1*(1-hond)/.5;
		  return t1 + (t2-t1)*(P-10)/5;
		}
	  }
	}
  }

  if (P>15 && P<=20) {
    if (hond <= .25) {
	  return -.5 -.1*(P-15)/5;
	} else {
	  if (hond >= 1) {
	    return -.6;
	  } else {
	    if (hond <= .5) {
		  t1 = -0.5;
		  t2 = -0.6;
		  return t1 + (t2-t1)*(P-15)/5;
		} else {
		  t1 = -.6 + 0.1*(1-hond)/.5;
		  t2 = -.6;
		  return t1 + (t2-t1)*(P-15)/5;
		}
	  }
	}
  }

  if (P>20 && P<25) {
    t1 = -.6;
	if (bond <= 3) {
	  t2 = -.6;
	} else {
	  if (bond >= 8) {
	    t2 = -.9;
	  } else {
	    t2 = -.06*(7+bond);
	  }
	}
    return t1 + (t2-t1)*(P-20)/5;
  }

  if (P>=25) {
    if (bond <= 3) {
	  return -.6;
	} else {
	  if (bond >= 8) {
	    return -.9;
	  } else {
	    return -.06*(7+bond);
	  }
	}
  }
}


