var aluminumArray =  new Array("('Select alloy','',true,true)",
"('1100','0.098')",
"('2011','0.102')",
"('2024','0.101')",
"('3003','0.099')",
"('5052','0.097')",
"('5083','0.096')",
"('5086','0.096')",
"('6061','0.098')",
"('6063','0.097')",
"('6262','0.098')",
"('7050','0.102')",
"('7075','0.101')",
"('7150','0.102')");

var copperArray =  new Array("('Select alloy','',true,true)",
"('101','0.323')",
"('102','0.323')",
"('103','0.323')",
"('110','0.322')",
"('122','0.323')",
"('145 - Tellurium Copper','0.323')",
"('182 - Class 2','0.321')",
"('194 - Iron Copper','0.322')",
"('260 - Cartridge Brass','0.308')",
"('280','0.303')",
"('353','0.306')",
"('365','0.304')",
"('385','0.306')",
"('443','0.308')",
"('464','0.304')",
"('485 - Naval Bronze','0.305')",
"('510 - Phos Bronze','0.32')",
"('614 - Al Bronze','0.285')",
"('630 - Ni-Al Bronze','0.274')",
"('632','0.276')",
"('655 - Copper-Silicon','0.308')",
"('706 - Copper-Nickel','0.323')",
"('715 - Nickel Silver','0.323')",
"('752 - Nickel Silver','0.316')",
"('770 - Nickel Silver','0.314')",
"('932 - Bearing Bronze','0.322')",
"('954 - Al Bronze','0.269')");


var nickelArray =  new Array("('Select alloy','',true,true)",
"('201','0.321')",
"('400','0.319')",
"('210','0.32')",
"('220','0.318')",
"('230','0.316')");

var stainlessArray =  new Array("('Select alloy','',true,true)",
"('304','0.287')",
"('316','0.286')",
"('410','0.278')",
"('420','0.28')",
"('430','0.278')");

var titaniumArray =  new Array("('Select alloy','',true,true)",
"('Grade 2','0.163')",
"('Grade 4','0.163')",
"('Grade 5','0.163')");

function getAlloys(form, material) {
	var selectedArray = eval(material + "Array");
	while (selectedArray.length < form.alloy.options.length) {
		form.alloy.options[(form.alloy.options.length - 1)] = null;
	}
	
	for (var i=0; i < selectedArray.length; i++) {
		eval("form.alloy.options[i]=" + "new Option" + selectedArray[i]);
	}
	
	if (form.material.options[0].value == '') {
		form.material.options[0]= null;
	}
}

function loadImage(){
	var image = 'images/' + document.calc.shape.options[document.calc.shape.selectedIndex].value + '.jpg';
	document.images.pic.src = image;
}

function clearFields(){
	document.calc.dim1.value = "";
	document.calc.dim2.value = "";
	document.calc.dim3.value = "";
	document.calc.dim4.value = "";
	document.calc.pieceweight.value = "";
	document.calc.totalweight.value = "";
}

function loadLabels(){
	var shape;
	shape = document.calc.shape.value;
 
	if(shape=="round"){
		document.calc.dim1label.value = "Diameter:";
		document.calc.dim2label.value = "Length:";
		document.calc.dim3label.value = "";
		document.calc.dim4label.value = "";
	} else if(shape== "hex"){
		document.calc.dim1label.value = "Diameter:";
		document.calc.dim2label.value = "Length:";
		document.calc.dim3label.value = "";
		document.calc.dim4label.value = "";
	} else if(shape=="rectangle"||shape=="plate"){
		document.calc.dim1label.value = "Gauge:";
		document.calc.dim2label.value = "Width:";
		document.calc.dim3label.value = "Length:";
		document.calc.dim4label.value = "";
	} else if(shape=="tube"){
		document.calc.dim1label.value = "Outer Diameter:";
		document.calc.dim2label.value = "Wall:";
		document.calc.dim3label.value = "Length:";
		document.calc.dim4label.value = "";
	} else if(shape=="ring"){
		document.calc.dim1label.value = "Thickness:";
		document.calc.dim2label.value = "Outer Diameter:";
		document.calc.dim3label.value = "Inner Diameter:";
		document.calc.dim4label.value = "";
	} else if(shape=="rectangletube"){
		document.calc.dim1label.value = "Height (A):";
		document.calc.dim2label.value = "Width (B):";
		document.calc.dim3label.value = "Wall (C):";
		document.calc.dim4label.value = "Length:";
	}
}

function convertUnits(value, unit){
	if(unit=="ft")
		value = value*12;
	else if(unit=="yd")
		value = value*36;
	else if(unit=="cm")
		value = value/2.54;
	else if(unit=="m")
		value = (value*100)/2.54;
	else if(unit== "mm")
		value = (value/10)/2.54;
	
	return value;
}

function calculate(){
	var shape;
	var pieces;
	var density;
	var weight;
	var pieceWeight;
	var totalWeight;
	var dim1;
	var dim2;
	var dim3;
	var dim4;

	density = document.calc.alloy.value;
	dim1 = convertUnits(document.calc.dim1.value, document.calc.unitsdim1.value);
	dim2 = convertUnits(document.calc.dim2.value, document.calc.unitsdim2.value);
	dim3 = convertUnits(document.calc.dim3.value, document.calc.unitsdim3.value);
	dim4 = convertUnits(document.calc.dim4.value, document.calc.unitsdim4.value);
	pieces = document.calc.pieces.value;
	shape = document.calc.shape.value;

	document.calc.pieceweight.rsize = "4";
	document.calc.totalweight.rsize = "4";

	if(shape=="round"){
		pieceWeight = new String(dim1*dim1*9.42*density*(dim2/12));
	} else if(shape=="hex"){
		pieceWeight = new String(dim1*dim1*density*10.4*(dim2/12));
	} else if(shape=="rectangle"||shape=="plate"){
		pieceWeight = new String(dim1*dim2*density*dim3);
	} else if(shape=="tube"){
		pieceWeight = new String((dim1-dim2)*dim2*density*37.7*(dim3/12));
	} else if(shape=="ring"){
		pieceWeight = new String((dim2+(dim3-dim2))*(dim3-dim2)*density*37.7*(dim1/12));
	} else if(shape=="rectangletube"){
		pieceWeight = new String(((dim1*dim2*12*density)-((dim1-(dim3*2))*(dim2-(dim3*2))*12*density))*(dim4/12));
		
	}
	document.calc.pieceweight.value = pieceWeight.substring(0,pieceWeight.indexOf(".")+5);
	totalWeight = new String(pieceWeight*pieces);
	document.calc.totalweight.value = totalWeight.substring(0,totalWeight.indexOf(".")+5);;
}
