	var ridgeadjustment = 0.060;
	var vNailsCorrugate = 6;
	var vNailsRibline = 5;
	var vNailsStyleline = 4;
	var vNailsNC710 = 3;
	var sheetwidth = 0;
	var sheetcover = 0;
	var nails = 0;


	function calcRoof() {
		var error = '';
		if ($('pitch').value == '') {
			error += 'Please enter the roof pitch<br />';
		} else if (!isInt($('pitch').value)) {
			error += 'The roof pitch must be a number<br />';
		} else if ($('pitch').value > 90) {
			error += 'Please enter a valid roof pitch<br />';
		}
		if ($('width').value == '') {
			error += 'Please enter the roof width<br />';
		} else if (!isInt($('width').value)) {
			error += 'The roof width must be a number<br />';
		}
		if ($('length').value == '') {
			error += 'Please enter the roof length<br />';
		} else if (!isInt($('length').value)) {
			error += 'The roof length must be a number<br />';
		}
		if (error != '') {
			setError(error);
			clearResults();
			return false;
		} else {
			setError('');
			doCalculations();
		}
	}

	function doCalculations() {
		setroofvalues();
		var sheets =  2 * getsheetcount();
		var cutlength = Math.round(1000 * (($('width').value / 2) / Math.cos($('pitch').value*Math.PI/180) + ridgeadjustment))/1000;
		var linearqty = Math.round(100 * sheets * cutlength)/100;
		var fasteners = Math.ceil(linearqty) * nails;
		var area = Math.round(1000 * (2 * ($('width').value / 2) / Math.cos($('pitch').value*Math.PI/180) * $('length').value))/1000;
		$('sheets').innerHTML = sheets;
		$('cutlength').innerHTML = cutlength;
		$('linearqty').innerHTML = linearqty;
		$('fasteners').innerHTML = fasteners;
		$('area').innerHTML = area;

	}

	function getsheetcount() {
		// get num of sheets reqd

		var sheets = ($('length').value - sheetwidth)/(sheetcover) + 1;
		//alert(sheetwidth);
		// round up
		if (sheets > 0)
			wholesheets = Math.ceil(sheets);
		else
			wholesheets = 0;
		return wholesheets;
	}

	function setError(error) {
		$('rooferror').innerHTML = "<p>"+error+"</p>";
	}

	function clearResults() {
		$('sheets').innerHTML = '';
		$('cutlength').innerHTML = '';
		$('linearqty').innerHTML = '';
		$('fasteners').innerHTML = '';
		$('area').innerHTML = '';
	}

	function isInt(val) {
   	 	if(val==null) {
        	return false;
    	}
    	if (val.length == 0) {
        	return false;
    	}
    	if (val.length == 1 && val == '.') {
        	return false;
    	}
    	for (var i = 0; i < val.length; i++) {
        	var ch = val.charAt(i);
       		if (i == 0 && ch == "-") {
            	continue
        	}
        	if ((ch < "0" || ch > "9") && ch != '.') {
            	return false
        	}
    	}
    	return true
	}

	function setroofvalues() {
		if ($('material').value == 'corrugate') {
			sheetwidth = 0.852;
			sheetcover = 0.762;
			nails = vNailsCorrugate;
		}
		if ($('material').value == 'ribline') {
			sheetwidth = 0.810;
			sheetcover = 0.762;
			nails = vNailsRibline;
		}
		if ($('material').value == 'styleline') {
			sheetwidth = 0.810;
			sheetcover = 0.750;
			nails = vNailsStyleline;
		}
		if ($('material').value == 'nc710') {
			sheetwidth = 0.760;
			sheetcover = 0.710;
			nails = vNailsNC710;
		}
	}

	function calcspan(s) {
		var es = 0;
		var ms = 0;
		var epic = "";
		var mpic = "";

		if ($('material').value == 'corrugate') {


			epic = "nailendcor.gif";
			mpic = "nailmidcor.gif";
			if (document.spancalc.materialthick[0].status) {
				es = 900;
				ms = 1200;
			}
			else {
				es = 1200;
				ms = 1600;
			}
		}
		if ($('material').value == 'ribline') {

	//alert("Ribline");

			epic = "nailendrib.gif";
			mpic = "nailmidrib.gif";
			if (document.spancalc.materialthick[0].status) {
				es = 900;
				ms = 1200;
			}
			else {
				es = 1200;
				ms = 1650;
			}
		}
		if ($('material').value == 'styleline') {
			epic = "nailendsty.gif";
			mpic = "nailmidsty.gif";
			if (document.spancalc.materialthick[0].status) {
				es = 1200;
				ms = 1500;
			}
			else {
				es = 1300;
				ms = 1700;
			}
		}
		if ($('material').value == 'nc710') {
			epic = "nailendnc.gif";
			mpic = "nailmidnc.gif";
			if (document.spancalc.materialthick[0].status) {
				es = 2000;
				ms = 2600;
			}
			else {
				es = 2400;
				ms = 3200;
			}
		}

		document.spancalc.endspan1.value = es;
		document.spancalc.endspan2.value = es;
		document.spancalc.midspan1.value = ms;
		document.spancalc.midspan2.value = ms;

		document.nailend.src = epic;
		document.nailmid.src = mpic;
	}

