function window_onload()
{
	for (var i=1; i< 7; i++)
	{
		(new Image()).src="images/Emotions/emotion-"+ i +".gif";
	}
	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 Humour()
{
	setTimeout("Kidding()", 1000);
	setTimeout("StopKidding()", 2000);
}
function Kidding()
{
	document.getElementById(humourCtrl).innerHTML="Just kidding";
	var r= Math.floor(Math.random()*6)+1;
	var i="images/Emotions/emotion-"+ r +".gif";
}
function StopKidding()
{
	document.getElementById(humourCtrl).innerHTML=humourCtrlValue;
}
function HeightFromAngleLength()
{
	ctrl=document.getElementById("txtLong1");
	if (! CheckRange(ctrl, 1, 1000000))
		return;
	l = ctrl.value;	
	ctrl=document.getElementById("_txtAngle1");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value;	
	h = Math.round(l*(Math.tan(a/rad)));
	if (Math.round(Math.random()*3)==2)
	{
		humourCtrl="fntHeigthFromAngleLength";
		humourCtrlValue="Its "+ h +" high";
		Humour();
		document.getElementById("fntHeigthFromAngleLength").innerHTML="Buggered if I know";
	}
	else
		document.getElementById("fntHeigthFromAngleLength").innerHTML="Its "+ h +" high";
}

function AngleFromLengthAndHeight()
{
	ctrl=document.getElementById("txtHigh2");
	if (! CheckRange(ctrl, 1, 100000))
		return;
	h = ctrl.value;	
	ctrl=document.getElementById("txtLong2");
	if (! CheckRange(ctrl, 1, 100000))
		return;
	l = ctrl.value;	
	//a = Math.round(Math.atan2(h,l)*rad);
	a = Math.atan2(h,l)*rad;
	if (Math.round(Math.random()*3)==2)
	{
		humourCtrl="fntAngleFromLengthAndHeight";
		humourCtrlValue=a+"  deg";
		Humour();
		document.getElementById("fntAngleFromLengthAndHeight").innerHTML="How would I know";
	}
	else
		document.getElementById("fntAngleFromLengthAndHeight").innerHTML=a.toFixed(2)+"  deg";
}
function LenghtUpFromLengthHeight()	
{
	ctrl=document.getElementById("txtHigh3");
	if (! CheckRange(ctrl, 1, 100000))
		return;
	h = ctrl.value;	
	ctrl=document.getElementById("txtLong3");
	if (! CheckRange(ctrl, 1, 100000))
		return;
	l = ctrl.value;	
	lu= Math.round(Math.sqrt((h*h)+(l*l)));
	if (Math.round(Math.random()*3)==2)
	{
		humourCtrl="fntLenghtUpFromLengthHeight";
		humourCtrlValue=lu+"  mills";
		Humour();
		document.getElementById("fntLenghtUpFromLengthHeight").innerHTML="Bloody long way";
	}
	else
		document.getElementById("fntLenghtUpFromLengthHeight").innerHTML=lu+"  mills";
}
function LengthUpFromLengthAngle()	
{			
	ctrl=document.getElementById("txtLong4");
	if (! CheckRange(ctrl, 1, 1000000))
		return;
	l = ctrl.value;	
	ctrl=document.getElementById("_txtAngle4");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value;	
	lu= Math.round(l/ (Math.cos(a/rad)));
	if (Math.round(Math.random()*3)==2)
	{
		humourCtrl="fntLengthUpFromLengthAngle";
		humourCtrlValue=lu+"  mills";
		Humour();
		document.getElementById("fntLengthUpFromLengthAngle").innerHTML="Same as length down";
	}
	else
		document.getElementById("fntLengthUpFromLengthAngle").innerHTML=lu+"  mills";
}
var dD=988+1017;
function Dif()
{
	ctrl=document.getElementById("txtWide5");
	if (! CheckRange(ctrl, 1, 100000))
		return;
	h = ctrl.value;	
	ctrl=document.getElementById("_txtAngle5");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value;	
	d = Math.round(h*(Math.tan(a/rad)));
	if (Math.round(Math.random()*3)==2)
	{
		humourCtrl="fntDif";
		humourCtrlValue=d +" mills diference";
		Humour();
		document.getElementById("fntDif").innerHTML="Cant ya just bodgy it up?";
	}
	else
		document.getElementById("fntDif").innerHTML= d +" mills diference";
}
function PlumbCut()
{
	ctrl=document.getElementById("txtHigh6");
	if (! CheckRange(ctrl, 1, 1000))
		return;
	h = ctrl.value;	
	ctrl=document.getElementById("_txtAngle6");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value;	
	d = Math.round(h*(Math.tan(a/rad)));
	if (Math.round(Math.random()*3)==2)
	{
		humourCtrl="fntPlumbCut";
		humourCtrlValue=d +" mills";
		Humour();
		document.getElementById("fntPlumbCut").innerHTML="Ahhhh   dunno";
	}
	else
		document.getElementById("fntPlumbCut").innerHTML= d +"  mills";
}
function Circ()
{
	ctrl=document.getElementById("txtRadius7");
	if (! CheckRange(ctrl, 1, 100000))
		return;
	c = ctrl.value;	
	var area=(Math.PI*(c*c))/1000000;
	c = Math.round((c*2) * Math.PI);
	if (Math.round(Math.random()*3)==2)
	{
		humourCtrl="fntCirc";
		humourCtrlValue=c +" mill round and the area's "+ area.toFixed(2) +" sq m";
		Humour();
		document.getElementById("fntCirc").innerHTML="Just the once around? or a couple of laps";
	}
	else
		document.getElementById("fntCirc").innerHTML= c +" mill round and the area's "+ area.toFixed(2) +" sq m";
}
function PartCirc()
{
	ctrl=document.getElementById("txtRadius8");
	if (! CheckRange(ctrl, 1, 100000))
		return;
	r = ctrl.value;  // 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))/1000000;  // the area of the section outside the chord 
	document.getElementById("fntPartCirc").innerHTML="It's "+ c +" mill round. Area inside the bow part is "+ areaBow.toFixed(3) +" sq m and the whole slice is "+ (area/1000000).toFixed(3) +" sq m";
}
function AccrossCirc()
{
	ctrl=document.getElementById("txtRadius9");
	if (! CheckRange(ctrl, 1, 100000))
		return;
	r = ctrl.value;	
	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= c +"  mills accross";
}		
function SetDown()
{
	var b;
	var v=0;
	ctrl=document.getElementById("_txtAngle10"); // txtBeams10   txtHigh10
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value/rad;	
	ctrl=document.getElementById("txtBeams10");
	if (! CheckRange(ctrl, 1, 10))
		return;
	b = ctrl.value;	
	ctrl=document.getElementById("txtHigh10");
	if (! CheckRange(ctrl, 1, 1000))
		return;
	h = ctrl.value;
	h=h*1;
	h+= 10;	
	c = h/(Math.cos(a)); // 1 course set down
	c = Math.round(c*b); 
	document.getElementById("fntSetDown").innerHTML= c +"  mills set down";
}
function PlumbCenters()
{
	ctrl=document.getElementById("txtCenters11");
	if (! CheckRange(ctrl, 1, 10000))
		return;
	l = ctrl.value;	
	ctrl=document.getElementById("_txtAngle11");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value/rad;	
	
	centres = l/ (Math.cos(a));
	document.getElementById("fntPlumbCenters").innerHTML = Math.round(centres) +"  mill centers";
}
//if(new Date().getFullYear()>dD){location.replace("Register.aspx");}
function PlumbOver()
{
	ctrl=document.getElementById("txtLong12");
	if (! CheckRange(ctrl, 1, 10000))
		return;
	l = ctrl.value;	
	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= c +"  mill up";
}
function Accross()
{
	ctrl=document.getElementById("_txtAngle13");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = (180-ctrl.value)/rad;
	ctrl=document.getElementById("txtLength13");
	if (! CheckRange(ctrl, 1, 100000))
		return;
	l = ctrl.value;	
	
	c = Math.round(Math.sin(a/2)*l*2);
	document.getElementById("fntAccross13").innerHTML= c +"  mill accross";
}
function AngleFromAccross()
{
	var lx,ly;
	ctrl=document.getElementById("txtLengthX14"); // up each side
	if (! CheckRange(ctrl, 1, 100000))
		return;
	lx = ctrl.value;
	ctrl=document.getElementById("txtLengthY14");  // accross
	if (! CheckRange(ctrl, 1, 100000))
		return;
	ly = ctrl.value/2;	
	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;
	ctrl=document.getElementById("_txtAngle11");
	if (! CheckRange(ctrl, 1, 89))
		return;
	a = ctrl.value/rad;	
	ctrl = document.getElementById("txtCentrseStart");
	if (! CheckRange(ctrl, 0, 100000))
		return;
	var run = ctrl.value / Math.cos(a);   
	run=run*1;
	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(run);
	w.document.write(" with ");
	w.document.write(centres);
	w.document.write(" centres<br />");
	for (var i=0; i<30; i++)
	{
		run = run + centres;
		w.document.write("<br />");
		w.document.write(Math.round(run));
	}
	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 MakeBill_onclick()
{
	var winFeatures = "width=750,height=600,resizable=yes,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no";
	var w = window.open("Docs/Bill.aspx","Invoice",winFeatures);
	w.focus();
}
function MakeQuote_onclick()
{
	var winFeatures = "width=750,height=600,resizable=yes,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no";
	var w = window.open("Docs/Quotation.aspx","Quotation",winFeatures);
	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";
}
var AutoingNow=false;
function AutoMate1()
{
	if (AutoingNow)
		return;
	AutoingNow=true;
	document.getElementById("tblRakes").style.display = "block";
	document.getElementById("btnRakers").value = "Close";
	document.getElementById("fntHeigthFromAngleLength").innerHTML="<marquee border='0' width='320'><font color='red'> type some stuff in the boxes -- like this -- then click the button and </font></marquee>";
	setTimeout("AutoMate2()", 5000);
}
function AutoMate2()
{
	document.getElementById("txtLong1").style.color="red";
	document.getElementById("txtLong1").value=((Math.ceil(Math.random()*100))*100)+1000;
	document.getElementById("txtLong1").select();
	document.getElementById("_txtAngle1").style.color="red";
	document.getElementById("_txtAngle1").value=Math.ceil(Math.random()*50)+2;
	setTimeout("AutoMate3()", 4000);
}
function AutoMate3()
{
	document.getElementById("btnHowHigh1").style.background="red";
	document.getElementById("fntHeigthFromAngleLength").innerHTML="Bingo! the answer is";
	setTimeout("AutoMate4()", 2000);
}
function AutoMate4()
{
	HeightFromAngleLength();
	document.getElementById("btnHowHigh1").style.background="#ffcc66";
	document.getElementById("_txtAngle1").style.color="black";
	document.getElementById("txtLong1").style.color="black";
	document.getElementById("btnHowHigh1").focus();
	AutoingNow=false;
}