function window_onload()
{  
	for (var i=1; i< 7; i++)
	{
		(new Image()).src="images/Emotions/emotion-"+ i +".gif";
	}
	SetOptions();
	document.getElementById("ddLongFeet13").selectedIndex=0;
	document.getElementById("ddLongInch13").selectedIndex=0;
	document.getElementById("ddLongFrac13").selectedIndex=0;
	document.getElementById("ddLongFeet5").selectedIndex=0;
	document.getElementById("ddLongInch5").selectedIndex=7;
	document.getElementById("ddLongFrac5").selectedIndex=20;
	document.getElementById("ddHighFeet6").selectedIndex=0;
	document.getElementById("ddHighInch6").selectedIndex=7;
	document.getElementById("ddHighFrac6").selectedIndex=20;
	document.getElementById("ddHighFeet10").selectedIndex=0;
	document.getElementById("ddHighInch10").selectedIndex=7;
	document.getElementById("ddHighFrac10").selectedIndex=20;
	//document.getElementById("divMain").style.display="block";
	//document.getElementById("divLoading").style.display="none";
	SetNumeric();
}
function ShowTable(tbl, btn)
{	
	var tbl = document.getElementById(tbl);
	if (tbl.style.display == "block")
	{
	    tbl.style.display = "none";
	    document.getElementById(btn).value = "Open";
	}
	else
	{
	    tbl.style.display = "block";
	    document.getElementById(btn).value = "Close";
	}
}
var mesg = "Blocklayers Toolbox";
var vers  = parseInt(navigator.appVersion)
var NS = navigator.appName=="Netscape"
var IE = navigator.appName=="Microsoft Internet Explorer"
function Nrc(e)
{
   if (NS && e.which > 1)
   {
      alert(mesg)
      return false
   }
   else if (IE && (event.button >1))
   {
     alert(mesg)
     return false;
   }
}
document.onmousedown = Nrc;
if (document.layers) window.captureEvents(Event.MOUSEDOWN);
if (NS && vers <5) window.onmousedown = Nrc;

var a,l,h,d,lu,r,c,ctrl,val,humourCtrl,humourCtrlValue;
var centres=0;
var rad = 180 / Math.PI;
function CheckRange(ctrl, min,max)
{
	val=ctrl.value;
	var supported = 0;
	if (window.RegExp)
	{
	     var tempStr = "a";
	     var tempReg = new RegExp(tempStr);
	     if (tempReg.test(tempStr))
	     {
			var reg1 = new RegExp("(fuck|cunt|shit|dick|smart|arse)");
			var bad1= val.toLowerCase();
			if (reg1.test(bad1))
			{
				alert('nasty little blockie arent we');
				ctrl.value="You to";
				return;
			}
		}
	}
	if (val=="")
	{
		var foot=Math.ceil(Math.random()*10);
		var inch=Math.ceil(Math.random()*12);
		var mill=Math.ceil(Math.random()*100);
		ctrl.focus();
		alert('Its about '+ foot +' foot '+ inch +' and '+ mill +' mill, give or take a yard or 3\n\n\nFair go, at least give us something to work on');
		ctrl.select();
		return false;
	}
	else if (isNaN(val))
	{
		ctrl.focus();
		alert('try a number instead of '+ val +' and give it a whirl');
		ctrl.select();
		return false;
	}
	else if (val < min)
	{
		ctrl.focus();
		alert('thats to low');
		ctrl.select();
		return false;
	}
	else if (val > max)
	{				
		ctrl.focus();
		alert('thats to big');
		ctrl.select();
		return false;
	}
	return true;
}
function HeightFromAngleLength()
{
	l = GetMeasure("ddFe","ddIn","ddFr");
	ctrl=document.getElementById("_txtAngle1");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value;	
	h = Math.round(l*(Math.tan(a/rad)));
	document.getElementById("fntHeigthFromAngleLength").innerHTML="Its "+ ConEng(h, true) +" high";
}
function AngleFromLengthAndHeight()
{	
	h = GetMeasure("ddHighFeet2","ddHighInch2","ddHighFrac2");
	l = GetMeasure("ddLongFeet2","ddLongInch2","ddLongFrac2");
	a = Math.atan2(h,l)*rad;
	document.getElementById("fntAngleFromLengthAndHeight").innerHTML=a.toFixed(2)+"  deg";
}
function LenghtUpFromLengthHeight()	
{
	h = GetMeasure("ddHighFeet3","ddHighInch3","ddHighFrac3");
	l = GetMeasure("ddLongFeet3","ddLongInch3","ddLongFrac3");
	lu= Math.round(Math.sqrt((h*h)+(l*l)));
	document.getElementById("fntLenghtUpFromLengthHeight").innerHTML = ConEng(lu, true);
}
function LengthUpFromLengthAngle()	
{			
	l = GetMeasure("ddLongFeet4","ddLongInch4","ddLongFrac4");
	ctrl=document.getElementById("_txtAngle4");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value;	
	lu= Math.round(l/ (Math.cos(a/rad)));
	document.getElementById("fntLengthUpFromLengthAngle").innerHTML =  ConEng(lu, true);
}
var dD=988+1017;
function PlumbCenters()
{
	l =  GetMeasure("ddLongFeet11","ddLongInch11","ddLongFrac11");
	ctrl=document.getElementById("_txtAngle11");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value/rad;	
	centres = l/ (Math.cos(a));
	document.getElementById("fntPlumbCenters").innerHTML= ConEng(Math.round(centres), true) +" centres";
}
function Dif()
{
	h = GetMeasure("ddLongFeet5","ddLongInch5","ddLongFrac5");
	ctrl=document.getElementById("_txtAngle5");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value;	
	d = Math.round(h*(Math.tan(a/rad)));
	document.getElementById("fntDif").innerHTML = ConEng(d, false) +" diference";
}
function PlumbCut()
{
	h = GetMeasure("ddHighFeet6","ddHighInch6","ddHighFrac6");
	ctrl=document.getElementById("_txtAngle6");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value;	
	d = Math.round(h*(Math.tan(a/rad)));
	document.getElementById("fntPlumbCut").innerHTML= ConEng(d, false);
}
function Circ()
{
	c =  GetMeasure("ddLongFeet20","ddLongInch20","ddLongFrac20");
	//var area=(Math.PI*(c*c))/1000000;
	var area=(Math.PI*(c*c))/147456;
	c = Math.round((c*2) * Math.PI);
	document.getElementById("fntCirc").innerHTML= ConEng(c, true) +" round and the area's "+ area.toFixed(2) +" sq ft";
}
function PartCirc()
{
	r = GetMeasure("ddLongFeet21","ddLongInch21","ddLongFrac21");  // radius    length = angle*(PI/180) *radius	
	ctrl=document.getElementById("txtDegrees8");
	if (! CheckRange(ctrl, 1, 360))
		return;
	a = ctrl.value/rad;	
	c=Math.round(a*r);
	var area = (a/2)*(r*r); // the area of the entire slice (sector)
	var areaBow = (area - ((r*r)*Math.sin(a)/2))/147456;  // the area of the section outside the chord 
	document.getElementById("fntPartCirc").innerHTML="It's "+ ConEng(c, true) +" round. Area inside the bow part is "+ areaBow.toFixed(3) +" sq ft and the whole slice is "+ (area/147456).toFixed(3) +" sq ft";
}
function AccrossCirc()
{
	r = GetMeasure("ddLongFeet22","ddLongInch22","ddLongFrac22");
	ctrl=document.getElementById("txtDegrees9");
	if (! CheckRange(ctrl, 1, 360))
		return;
	a = ctrl.value/rad;	
	c = Math.round((Math.sin(a/2)*r)*2);
	document.getElementById("fntAccrossCirc").innerHTML= ConEng(c, true) +" accross";
}		
function SetDown()
{
	var b;
	var v=0;
	ctrl=document.getElementById("_txtAngle10"); // txtBeams10   txtHigh10
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value/rad;	
	b = document.getElementById("ddBeams10").value;	
	h = GetMeasure("ddHighFeet10","ddHighInch10","ddHighFrac10");
	h=h*1;
	h+= 12;	
	c = h/(Math.cos(a)); // 1 course set down
	c = Math.round(c*b); 
	document.getElementById("fntSetDown").innerHTML= ConEng(c, true) +"  set down";
}

//if(new Date().getFullYear()>dD){location.replace("Register.aspx");}
function PlumbOver()
{	
	l = GetMeasure("ddLongFeet12","ddLongInch12","ddLongFrac12");
	ctrl=document.getElementById("_txtAngle12");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value/rad;	
	c= Math.round(l/ (Math.cos(a)));
	document.getElementById("fntPlumbOver").innerHTML= ConEng(c, true) +"  up";
}
function Accross()
{
	ctrl=document.getElementById("_txtAngle13");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = (180-ctrl.value)/rad;
	l = GetMeasure("ddLongFeet23","ddLongInch23","ddLongFrac23");
	c = Math.round(Math.sin(a/2)*l*2);
	document.getElementById("fntAccross13").innerHTML= ConEng(c, true) +" accross";
}
function AngleFromAccross()
{
	var lx,ly;
	lx = GetMeasure("ddLongFeet24","ddLongInch24","ddLongFrac24");
	ly = GetMeasure("ddLongFeet25","ddLongInch25","ddLongFrac25");
	var temp=(ly/lx);
	temp = Math.asin(temp)*rad;
	temp=temp*2;
	a = 180-temp;
	document.getElementById("fntAngle14").innerHTML= a.toFixed(2) +"  degrees";
}
function PitchRise_onchange(ctrl)
{
	var pitchIn = document.getElementById("ddPitchIn").value;
	var ang = Math.atan(ctrl.value / pitchIn) * rad;
	document.getElementById("fntPitch").innerHTML=ctrl.value+ " in "+ pitchIn +" = "+ ang.toFixed(2) +" degrees";
}
function PitchIn_onchange(ctrl)
{
	var pitchRise = document.getElementById("ddPitchRise").value;
	var ang = Math.atan(pitchRise / ctrl.value) * rad;
	document.getElementById("fntPitch").innerHTML=pitchRise +" in "+ ctrl.value +" = "+ ang.toFixed(2) +" degrees";
}						
function ShowSill_onclick()
{
	var winFeatures = "width=650,height=920,status=no,toolbar=no,menubar=no,location=no";
	var w = window.open("Sheets/SillHeights.htm","SillHeight",winFeatures);
	w.focus();
}
function ShowCount_onclick()
{
	var winFeatures = "width=650,height=920,status=no,toolbar=no,menubar=no,location=no";
	var w = window.open("Sheets/QuickCount.htm","QuickCount",winFeatures);
	w.focus();
}
function RunningUp()
{	
	PlumbCenters(); // do the calc first
	if (centres==0)
		return;
	var run = GetMeasure("ddLongFeet13","ddLongInch13","ddLongFrac13");
	ctrl = document.getElementById("_txtAngle11");
	if (! CheckRange(ctrl, 1, 89))
		return;
	var dangle = ctrl.value/rad;	
	run = run / Math.cos(dangle);   
	var winFeatures = "width=400,height=850,status=no,toolbar=no,menubar=no,location=no";
	var w = window.open("","RunningUp",winFeatures);
	w.document.open();
	w.document.write("<html><head>");
	w.document.write("</head><body onload='javascript:window.print();window.close()'>");
	w.document.write("Starting at ");
	w.document.write(ConEng(run, true));
	w.document.write(" with ");
	w.document.write(ConEng(centres, true));
	w.document.write(" centres<br />");
	for (var i=0; i<40; i++)
	{
		run = run + centres;
		w.document.write("<br />");
		w.document.write(ConEng(Math.round(run), true));
	}
	w.document.write("<br /><input type='button' value='Close Window' onclick='javascript:window.close()'></div></body></html>");
	w.document.close();
	w.focus();
}
function PrintRow(r)
{
	var row=document.getElementById(r).innerHTML;
	var winFeatures = "width=800,height=300,status=no,toolbar=no,menubar=no,location=no";
	var w = window.open("","Question",winFeatures);
	w.document.open();
	w.document.write("<html><body onload='javascript:window.print();window.close()'><br />");
	w.document.write(row);
	w.document.write("</body></html>");
	w.document.close();
	w.focus();
}

function Display_onmouseover(type, elem, event)
{
	var deltaX = event.clientX;
	var deltaY = event.clientY + document.body.scrollTop;
	//+ document.body.scrollLeft +"px";
	document.getElementById("divDisplay").style.left = deltaX - 50 +"px";
	document.getElementById("divDisplay").style.top = deltaY + 10 +"px";
	var pic="";
	switch(type)
	{
		case "dev":
			pic="Obangle.gif";
			break;
		case "circ":
			pic="circleArea.gif";
			break;
	}
	document.getElementById("imgDisplay").src="images/"+ pic;
}
function Display_onmouseout()
{
	document.getElementById("divDisplay").style.left="-1000px";
	document.getElementById("imgDisplay").src="images/Spacer.gif";
}
function SetOptions()
{	
	var opsFrac = document.getElementById("ddFr").options;
	var opsInch = document.getElementById("ddIn").options;
	var opsFeet = document.getElementById("ddFe").options;
	var allDrops = document.getElementsByTagName("select");
	for (var y=0; y< allDrops.length; y++)
	{		
		if (allDrops[y].id.indexOf("Frac") > -1)
		{
			for (var i=0; i< opsFrac.length; i++)
			{
				allDrops[y].options[i] = new Option(opsFrac[i].text, opsFrac[i].value);
			}
			allDrops[y].selectedIndex = Math.floor(Math.random()* 30) + 1;
		}
		if (allDrops[y].id.indexOf("Inch") > -1)
		{
			for (var i=0; i< opsInch.length; i++)
			{
				allDrops[y].options[i] = new Option(opsInch[i].text, opsInch[i].value);
			}
			allDrops[y].selectedIndex = Math.floor(Math.random()* 9) + 1;
		}
		if (allDrops[y].id.indexOf("Feet") > -1)
		{
			for (var i=0; i< opsFeet.length; i++)
			{
				allDrops[y].options[i] = new Option(opsFeet[i].text, opsFeet[i].value);
			}
			allDrops[y].selectedIndex = Math.floor(Math.random()* 8) + 1;
		}
	}
}
function GetMeasure(feet, inch, frac)
{
	return (parseInt(document.getElementById(feet).value) * 384) + (parseInt(document.getElementById(inch).value) * 32) + parseInt(document.getElementById(frac).value);
}