// JavaScript for the Desk Estimator
// Copyright Christopher Chong Productions Inc. 2002
// info@christopherchong.com 


//Global Variables
var treated = 18;  // cost for pressure treated wood
var cedar = 22;  // cost for cedar
var trex = 29;  // cost for trex

// This function checks a field for a valid number.
function checknumber(testnum)
{
	var x=testnum;
	var anum=/(^\d+$)|(^\d+\.\d+$)/;
	
	if (anum.test(x))
	testresult=true;
	else
	{
		testresult=false;
	}
	return (testresult);
}

// This function rounds the number.
function round(number,X) 
{
	// rounds number to X decimal places, defaults to 2
	X = (!X ? 2 : X);
	return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

// This function calculates the deck estimate.
function multiply()
{
	var width = document.estimator.width.value;
	var length = document.estimator.length.value;
	var material = "treated" //default
	var materialcost = treated; //default
	var levels = document.estimator.levels.value;
	var height = "24 inches and under";  //default
	var border = "none";  //default
	var direction = "straight"; //default
	var curved = "no"; //default
	var bench1 = "no"; //default
	var bench2 = "no"; //default
	var numbench1 = document.estimator.numbench1.value;
	var numbench2 = document.estimator.numbench2.value;
	var flowerboxes = "no"; //default
	var single = document.estimator.single.value;
	var multi = document.estimator.multi.value;
	var lighting = "no"; //default
	var numlights = document.estimator.numlights.value;
	var stairs1 = "no"; //default
	var stairs2 = "no"; //default
	var stairs3 = "no"; //default
	var stairs4 = "no"; //default
	var numsteps1 = document.estimator.numsteps1.value;
	var numsteps2 = document.estimator.numsteps2.value;
	var numsteps3 = document.estimator.numsteps3.value;
	var numsteps4 = document.estimator.numsteps4.value;
	var landing = "no"; //default
	var railing = "basic"; //default
	var skirting = "no"; //default
	var walls = "no"; //default
	var obstruction = "no"; //default
	var fasten = "screws"; //default
	var retaining = "no"; //default
	var retainingnum = 0; //default
	//var paymentplan = "Payment Plan #1"; //default
	var basetotal = 0; //default
	var total = 0; //default
		
	
	// 1,2. Calulate the square feet.
	if (width <= 0 || !checknumber(width))
	{
		alert("Width must be a number greater than 0");
		document.estimator.width.focus();
	}
	else if (length <= 0 || !checknumber(length))
	{
		alert("Length must be a number greater than 0");
		document.estimator.length.focus();
	}
	else if (levels <= 0 || !checknumber(levels))
	{
		alert("Number of levels must be a number greater than 0");
		document.estimator.levels.focus();
	}
	else
	{
		var sqfeet = width * length;
	
	
		// 3. Determine the material type.
		if (document.estimator.material[1].checked)
		{
			materialcost = cedar;
			material = "cedar";
		}
		else if (document.estimator.material[2].checked)
		{
			materialcost = trex;
			material = "trex";
		}
		else
		{
			materialcost = treated;
			material = "pressure treated";
		}

		// Calculate the Base Total
		basetotal = sqfeet * materialcost;
		total = basetotal;
		

		// 4. Adjust total for multiple levels
		if (levels > 1)
		{
			total = total + (basetotal * 0.025 * (levels - 1));
		}

		// 5. Adjust total for height
		if (document.estimator.height[1].checked)
		{
			total = total + (basetotal * 0.05);
			height = "over 24 inches";
		}

		// 6. Adjust for Border Pattern 	
		if (document.estimator.borderpattern[1].checked)
		{
			total = total + (basetotal * 0.015);
			border = "single";
		}
		else if (document.estimator.borderpattern[2].checked)
		{
			total = total + (basetotal * 0.04);
			border = "double";
		}	
		else if (document.estimator.borderpattern[3].checked)
		{
			total = total + (basetotal * 0.045);
			border = "triple";
		}	

		// 7. Adjust for board direction
		if (document.estimator.direction[1].checked)
		{
			total = total + (basetotal * 0.025);
			direction = "angled";
		}

		// 8. Check if deck is curved
		if (document.estimator.curved[1].checked)
		{
			total = total + (basetotal * 0.15);
			curved = "yes";
		}

		// 9. Check for benches
		if (document.estimator.bench1.checked == true)
		{
			// user wants straight benches
			total = total + (numbench1 * 7.50);
			bench1 = "yes";
		}
		if (document.estimator.bench2.checked == true)
		{
			// user wants angled benches
			total = total + (numbench2 * 10);
			bench2 = "yes";
		}

		// 10. Check for flowerboxes
		if (document.estimator.flowerbox[1].checked)
		{
			total = total + (single * 88) + (multi * 100);
			flowerboxes = "yes"
		}

		// 11. Check for lights
		if (document.estimator.lighting[1].checked)
		{
			total = total + (numlights * 12.50);
			lighting = "yes";
		}

		// 12. Check for stairs
		if (document.estimator.stairs1.checked == true)
		{
			// user picked straight stairs
			stairs1 = "yes";
			total = total + (numsteps1 * 15);
		}
		if (document.estimator.stairs2.checked == true)
		{
			// user picked curved stairs
			stairs2 = "yes";
			total = total + (numsteps2 * 20);
		}
		if (document.estimator.stairs3.checked == true)
		{
			// user picked wrap around stairs
			stairs3 = "yes";
			total = total + (numsteps3 * 15);
		}
		if (document.estimator.stairs4.checked == true)
		{
			// user picked earth stairs
			stairs4 = "yes";
		}

		// 13. Check for landing
		if (document.estimator.landing[1].checked)
		{
			// Calculate the landing sq. footage
			var landingwidth = document.estimator.landingwidth.value;
			var landinglength = document.estimator.landinglength.value;
			var landingarea = landingwidth * landinglength;
			total = total + (landingarea * materialcost);
			landing = "yes";
		}

		// 14. Determine railing
		
		if (document.estimator.railing[1].checked)  // Standard
		{
			var numrailing = document.estimator.numrailing.value;
			railing = "standard";
			if (material == "pressure treated")
			{
				total = total + (numrailing * 6.25);
			}
			else if (material == "cedar")
			{
				total = total + (numrailing * 8.75);
			}
			else
			{
				total = total + (numrailing * 11.25);
			}
		}
		else if (document.estimator.railing[2].checked)  // Ornate
		{
			var numrailing = document.estimator.numrailing.value;
			railing = "ornate";
			if (material == "pressure treated")
			{
				total = total + (numrailing * 7.50);
			}
			else if (material == "cedar")
			{
				total = total + (numrailing * 10.00);
			}
			else
			{
				total = total + (numrailing * 12.50);
			}
		}
		else if (document.estimator.railing[3].checked)  // Glass
		{
			var numrailing = document.estimator.numrailing.value;
			railing = "glass";
			total = total + (numrailing * 40.00);
		}
		else if (document.estimator.railing[4].checked)  // Metal
		{
			var numrailing = document.estimator.numrailing.value;
			railing = "metal";
			total = total + (numrailing * 40.00);
		}				
		else // Basic
		{
			var numrailing = document.estimator.numrailing.value;
			if (material == "pressure treated")
			{
				total = total + (numrailing * 5.00);
			}
			else if (material == "cedar")
			{
				total = total + (numrailing * 7.50);
			}
			else
			{
				total = total + (numrailing * 10.00);
			}
		}

		// 15. Determine skirting
		if (document.estimator.skirting[1].checked)  // Lattice
		{
			skirting = "yes, lattice";
			var numskirting = document.estimator.numskirting.value;
			if (material == "pressure treated")
			{
				total = total + (numskirting * 5.00);
			}
			else if (material == "cedar")
			{
				total = total + (numskirting * 7.50);
			}
			else
			{
				total = total + (numskirting * 10.00);
			}
		}
		else if (document.estimator.skirting[2].checked)  // Solid
		{
			skirting = "yes, solid";
			var numskirting = document.estimator.numskirting.value;
			if (material == "pressure treated")
			{
				total = total + (numskirting * 7.50);
			}
			else if (material == "cedar")
			{
				total = total + (numskirting * 10.00);
			}
			else
			{
				total = total + (numskirting * 12.50);
			}
		}
		// else no skirting...

		// 16. Check for privacy walls
		if (document.estimator.wall[1].checked)
		{
			walls = "yes";
			var wallsize = document.estimator.wallsize.value;
			total = total + (wallsize * materialcost);
		}

		// 17. Check for obstruction
		if (document.estimator.obstruction[1].checked)
		{
			total = total + (basetotal * 0.05);
			obstruction = "yes";
		}

		// 18. Check for fastening method
		if (document.estimator.fasten[1].checked)
		{
			total = total + (basetotal * 0.125);
			fasten = "fastening system";
		}
		else
		{
			total = total + (basetotal * 0.05);
			fasten = "screws";
		}

		// 19. Check for retaining walls
		if (document.estimator.retaining[1].checked)
		{
			retainingnum = document.estimator.retainingnum.value;

			// check if over 3 feet
			if (retainingnum <= 3)
			{
				total = total + (retainingnum * 12.50);
				retaining = "yes";
			}
			else
			{
				total = total + (retainingnum * 20);
				retaining = "yes";
			}
		}

		// Round the total to 2 decimal places
		var roundedtotal = round(total,2);
		// var roundeddeposit = round(deposit,2);
		// var roundedplan2payments = round(plan2payments,2);
		// var roundedplan3payments = round(plan3payments,2);

		// Open a new window...
		win1 = window.open("","popup","height=500,width=400,scrollbars=yes,resizable=yes");
		win1.document.write("<HTML>");
		win1.document.write("<HEAD>");
		win1.document.write("<TITLE>Summary Window</TITLE>")
		win1.document.write("</HEAD>");
		win1.document.write('<BODY background="images/background03.jpg">');
		win1.document.write('<FONT FACE="arial" SIZE="2">');

		// Print a summary of the users specs
		win1.document.write('<H2>Deck Estimator Summary</H2>');
		win1.document.write('<P>Thank you for using our Deck Estimator. We have provided this tool to allow you to dream, budget and plan the ultimate deck for your outdoor living space. As you add or delete different features for your deck, an estimate is provided for your consideration. Amazing Decks strives to bring honesty and integrity to the decking industry by being upfront about potential costs. Please keep in mind you are provided with a rough estimate, which cannot replace a face-to-face quote on your desired deck. We would be happy to meet with you at your convenience.</P>');
		win1.document.write('Width: '+width+' feet'+'<BR>'+'Length: '+length+' feet'+'<BR>');
		win1.document.write('Total Sq. Feet: '+sqfeet+'<BR>');
		win1.document.write('Material: '+material+'<BR>');
		win1.document.write('Number of levels: '+levels+'<BR>');
		win1.document.write('Deck Height: '+height+'<BR>');
		win1.document.write('Border Pattern: '+border+'<BR>');
		win1.document.write('Board Direction: '+direction+'<BR>');
		win1.document.write('Curved Deck: '+curved+'<BR>');
		win1.document.write('Built in Benches (straight): '+bench1+', '+numbench1+' feet<BR>');
		win1.document.write('Built in Benches (angled): '+bench2+', '+numbench2+' feet<BR>');
		win1.document.write('Flower Boxes: '+flowerboxes+', '+single+' single units and '+multi+' multi units'+'<BR>');
		win1.document.write('Built in Lighting: '+lighting+', '+numlights+' lights<BR>');
		win1.document.write('Stairs (straight): '+stairs1+', '+numsteps1+' step(s)<BR>');
		win1.document.write('Stairs (curved or angled): '+stairs2+', '+numsteps2+' step(s)<BR>');
		win1.document.write('Stairs (wrap around): '+stairs3+', '+numsteps3+' step(s)<BR>');
		win1.document.write('Stairs (earth): '+stairs4+', '+numsteps4+' step(s)<BR>');
		win1.document.write('Landing: '+landing+', '+landingarea+' sq feet<BR>');
		win1.document.write('Railing: '+railing+', '+numrailing+' feet<BR>');
		win1.document.write('Skirting: '+skirting+', '+numskirting+' feet<BR>');
		win1.document.write('Privacy Walls: '+walls+', '+wallsize+' sq. feet<BR>');
		win1.document.write('Obstruction: '+obstruction+'<BR>');
		win1.document.write('Fastening Method: '+fasten+'<BR>');
		win1.document.write('Retaining Walls: '+retaining+', '+retainingnum+' feet<BR>');
		win1.document.write('-----------------------------------------------------------------------------------<BR>');
		//win1.document.write('Payment Method: '+paymentplan+'<BR>');
		
				
		// Print the total...
		win1.document.write('<BR>'+'Your Deck Estimate: <FONT COLOR="red"><B>$</B></FONT>');
		win1.document.write('<FONT COLOR="red"><B>'+roundedtotal+'</B></FONT>');

		// Create close & save buttons and close off the HTML tags...
		win1.document.write('<BR><form><INPUT TYPE="BUTTON" NAME="SUB" VALUE="Close" onClick="window.close();">');
		win1.document.write('<input type="button" name="Button" value="Save" onClick=document.execCommand("SaveAs")>');
		win1.document.write("</FONT>");
		win1.document.write("</BODY>");
		win1.document.write("</HTML>");
	}
}


