
var	bcap4   = (8*7*2)/27;
var	bcap8   = (8*10*2.94)/27;
var	bcap10  = (8*12*2.66)/27;
var	bcap15  = (8*14*4)/27;
var	bcap20  = (8*16*5)/27;
var	bcap30  = (8*18*8)/27;
var	bcap40  = (8*22*6.5)/27;
var bcap50  = (8*22*8)/27;
var i="";
	function ClearForm(form){
			form.boxlength.value = "";
			form.boxheight.value = "";
			form.boxwidth.value = "";
			form.boxvol.value="";
	}
	
		
	function lngth(v, h, w) {
	
					boxl=v/eval(h*w);
					return boxl;
	}
	
	function height(v, l,w) {
	
					boxh=v/eval(l*w);
					return boxh;
	}
	
	function width(v, l, h) {
	
					boxw=v/eval(l*h);
					return boxw;
	}
	
	function vol(l, h, w) {
	
					boxv=eval(l*h*w);
					return boxv;
	}
	
	function checkform(form) {
	
	// i = 0 means we want to calculate volume
	// i = 1 means we want to calculate height
	// i = 2 means we want to calculate width
	// i = 3 means we want to calculate length
	// i = 4 means illegal situation
	
	
	if (form.boxlength.value==""&&form.boxwidth.value==""&&form.boxheight.value=="")  {
		  i = 4;
		  return true;
		  }
	
	if (form.boxlength.value==""&&form.boxwidth.value==""&&form.boxvol.value=="")  {
		  i = 4;
		  return true;
		  }
	if (form.boxvol.value==""&&form.boxlength.value==""&&form.boxheight.value=="")  {
		  i  = 4;
		  return true;
		  }
	if (form.boxvol.value==""&&form.boxwidth.value==""&&form.boxheight.value=="")  {
		  i  = 4;
		  return true;
		  }
	if (form.boxvol.value==""&&form.boxheight.value=="")  {
		  i  = 4;
		  return true;
		  }
	
	if (form.boxlength.value=="" && form.boxvol.value=="")  {
		  i  = 4;
		  return true;
		  }
	if (form.boxwidth.value==""&&form.boxheight.value=="")  {
		  i  = 4;
		  return true;
		  }
	if (form.boxwidth.value==""&&form.boxvol.value=="")  {
		  i  = 4;
		  return true;
		  }
	if (form.boxlength.value==""&&form.boxwidth.value=="")  {
		  i  = 4;
		  return true;
		  }
	if (form.boxlength.value==""&&form.boxheight.value=="")  {
		  i  = 4;
		  return true;
		  }
	
	
	
	
	if (form.boxlength.value==""){
			i = 3; 
			return true;
			}
	
			else if (form.boxheight.value== ""){
			i  = 1;
			}
	
	 
			else if (form.boxwidth.value==""){
			i  = 2;
			}
	
	
			else if (form.boxvol.value==""){
			i  = 0;
			}
	
	
	return true;
	 }
	
	function computeform(form) {
	
		 if (checkform(form)){
	
			if (i == 3){
							boxlen=Math.abs(lngth(form.boxvol.value, form.boxheight.value, form.boxwidth.value));
							form.boxlength.value=boxlen;
				form.answer.value=boxlen;
			}
	
	
			else if (i == 1){
							boxht=Math.abs(height(form.boxvol.value, form.boxlength.value, form.boxwidth.value));
							form.boxheight.value=boxht;
				form.answer.value=boxht;
			}
	 
			else if (i ==2){
							boxwid=Math.abs(width(form.boxvol.value, form.boxlength.value, form.boxheight.value));
							form.boxwidth.value=boxwid;
				form.answer.value=boxwid;
			}
	
			else if (i ==0) {
							boxvolume=Math.abs(vol(form.boxlength.value, form.boxheight.value, form.boxwidth.value));
							form.boxvol.value=boxvolume;
				form.answer.value=boxvolume;
			} 
	
	
			else if (i ==4){
							alert("\n INCOMPLETE DATA! \n Please complete three of the four entries!!");
							ClearForm(form);
	
					}
			}
		}
	/* Function swap graphics as per the volume
	 * document object refers to the document that is being displayed 
	 * in the html page
	 * images is a field of document object it is array of image displayed in the html page
	 * Put the image file names as per your requirement
	 * For testing purpose am swapping between just two images
	 */
	function swapGraphics(vol)
	{
		if(vol<=bcap4)
		{
			document.images["box_visual"].src = "images/boxes/box4midG.gif";
		}
		else if(vol<=bcap8)
		{
			document.images["box_visual"].src = "images/boxes/box8midG.gif";
		}
		else if(vol<=bcap10)
		{
			document.images["box_visual"].src = "images/boxes/box10midG.gif";
		}
		else if(vol<=bcap15)
		{
			document.images["box_visual"].src = "images/boxes/box15midG.gif";
		}
		else if(vol<=bcap20)
		{
			document.images["box_visual"].src = "images/boxes/box20midG.gif";
		}
		else if(vol<=bcap30)
		{
			document.images["box_visual"].src = "images/boxes/box30midG.gif";
		}
		else if(vol<=bcap40)
		{
			document.images["box_visual"].src = "images/boxes/box40midG.gif";
		}
		else if(vol>bcap50)
		{
			document.images["box_visual"].src = "images/boxes/box50midG.gif";

        var nbox = Math.round(vol/bcap50);
        var rbox = vol%bcap50;
		
	if (rbox>0)
	{
		if (rbox<=4)
		{
		remtext = " and One Four Cubic Yard Box";
		}
		else if(rbox<=8)
		{
		remtext = " and One Eight Cubic Yard Box";
		}
		else if(rbox<=10)
		{
		remtext = " and One Ten Cubic Yard Box";
		}
		else if(rbox<=15)
		{
		remtext = " and One Fifteen Cubic Yard Box";
		}
		else if(rbox<=20)
		{
		remtext = " and One Twenty Cubic Yard Box";
		}
		else if(rbox<=30)
		{
		remtext = " and One Thirty Cubic Yard Box";
		}
		else if(rbox<=40)
		{
		remtext = " and One Forty Cubic Yard Box";
		}
		else if(rbox<50)
		{
		remtext = " ";
		}
		}

	alert("You need "+nbox+" Fifty Cubic Yard Boxes"+remtext);
	//document.write("You need "+nbox+" Forty Cubic Yard Boxes"+remtext)
		}
    }
		
	/* The onclick event handler for the button name "button"
	 * calculates volume taking input from the form.
	 * calls the swapGraphics method for swapping the images as per the result
	 * document object refers to the document that is being displayed 
	 * in the html page
	 * forms is a field of document object is an array of all form in the page
	 * You can access any form by its id i.e document.forms["id"] or the index
	 * i.e document.forms[index]	 
	 */
	function calcVolume()
	{
		var length2 = document.forms[1].length2.value;
		var width2 = document.forms[1].width2.value;
		var height2 = document.forms[1].height2.value;
		capfactora = document.forms[1].startcap.options[document.forms[1].startcap.selectedIndex].value;
		capfactorb = document.forms[1].endcap.options[document.forms[1].endcap.selectedIndex].value;
		var result = length2*width2*height2*capfactora/capfactorb;
		document.forms[0].elements["boxval"].value = result;		
		//alert(length2*width2*height2*capfactora/capfactorb);
		return result ;
	}
