///////////////////////////////GLOBALS////////////////////////////
var roofing_price=0;
var Leg_Height_Price=0;
var Front_End_Options_Price=0;
var Back_End_Options_Price=0;
var Right_Side_Options_Price=0;
var Left_Side_Options_Price=0;
var Bow_Price=0;
var Gauge12_Price=0;
var Extra_Panels_Price=0;
var Extra_Bracing_Price=0;
var JTrim_Price=0;
var Anchors_Price=0;
var Connection_Fee=0;
var Height_ID=0;
var last_height_id=0;
var Enclosure_Price=0;
var end_closing_price = 0;
var Full_Enclosure = false;
var discount_legs = false;
var discount_applied = false; 	//is the discount currently applied?
var debug_mode = false;			//enabling this turns on alerts for all functions

//Some globals come from outside this document. See array.php for a complete list of all global arrays
//min_height				#minimum leg height in feet
//base_height				#lowest the min height can possibly go
//frame_price
//frameout_door_price
//frameout_window_price
//frameout_garage_price
//gable_price
//Length_ID
//Width_ID
//Length_Feet
//Sheet_ID
//ht_cost_adjustment

//////////////////////////////////////////////////////////////////////////////
//checkValue
//
//Desc: gets the value of the currently selected button from a radion button
//set.
//////////////////////////////////////////////////////////////////////////////
function checkValue(set_name){
	var curValue = document.buildingForm[set_name];

	//error catch
	if(curValue==null){
		return null;
	}
	
	
	for(i=0;i<curValue.length;i++){
		if(curValue[i].checked==true){
			return curValue[i].value;
		}//end if
	}//end for
	
	
	return false; //on failure
}//end function
//////////////////////////////////////////////////////////////////////////////
//changeFrame
//
//Desc: Used to change the frame size from the quote-form2.php page.
//////////////////////////////////////////////////////////////////////////////
function changeFrame(certed, state, gauge, newFrame){
	
	var nlocation = "quote-form2.php?state="+state+"&gauge="+gauge+"&Certified="+certed+"&frame_size="+newFrame;
	
	location.href=nlocation;
}//end function
//////////////////////////////////////////////////////////////////////////////
//changeFrame2
//
//Desc: Used to change the frame size from the quote-form3.php page.
//////////////////////////////////////////////////////////////////////////////
function changeFrame2(certed, state, gauge, newFrame){
	
	var nlocation = "quote-form3.php?state="+state+"&gauge="+gauge+"&Certified="+certed+"&frame_size="+newFrame;
	
	location.href=nlocation;
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateForm
//
//Desc: This function triggers all gathering and altering functions to 
//perform a complete update of all form elements
//////////////////////////////////////////////////////////////////////////////
function updateForm(){
		
	if(debug_mode) alert("updateForm");
		
	//Let's Organize this by question
	updateRoofTypes();
	updateLegHeight();
	updateCenterHeight();	
	updateRightSideOptions();
	updateLeftSideOptions();
	updateFrontEndOptions();
	updateBackEndOptions();
	updateExtraBow();
	update12GaugeFraming();
	updateExtraPanels();
	updateExtraBracing();
	update26Gauge();
	updateJTrim();
	updateEnclosures();
	//updateForm2();
	
}//end function
function updateForm2(){
	if(debug_mode) alert("updateForm2");

	
	updateGroundAnchors();
	updateConnectionFees();
	updateSlabTypes();
	
	//and the sum total
	updateTotal();
	
}
//////////////////////////////////////////////////////////////////////////////
//updateTotal
//
//Desc: Gathers all the price points on the form and sums them. Updates the 
//displayed and hidden total.
//////////////////////////////////////////////////////////////////////////////
function updateTotal(){

	if(debug_mode) alert("updateTotal");

	
	var total = parseFloat(frame_price); //frame_price is a global established in quote-form2.php
	
	total +=  roofing_price;
	total +=  Leg_Height_Price;
	total +=  Front_End_Options_Price;
	total +=  Back_End_Options_Price;
	total +=  Right_Side_Options_Price;
	total +=  Left_Side_Options_Price;
	total +=  Bow_Price;
	total +=  Gauge12_Price;
 	total +=  Extra_Panels_Price;
 	total +=  Extra_Bracing_Price;
 	total +=  JTrim_Price;
 	total +=  Anchors_Price;
 	total +=  Connection_Fee;
 	total +=  Enclosure_Price;

	total = parseFloat(total);
	total = total.toFixed(2);
	
	//Update shown total and hidden total
	document.getElementById('running_total').innerHTML="$"+total;
	document.getElementById('final_price').value = total;

	
	//Determine price for all optional upgrades (used by review form)
	var Optionals_Total = 0;
	Optionals_Total +=  Bow_Price;
	Optionals_Total +=  Gauge12_Price;
 	Optionals_Total +=  Extra_Panels_Price;
 	Optionals_Total +=  Extra_Bracing_Price;
 	Optionals_Total +=  JTrim_Price;	
 	document.getElementById('Optionals_Total').value = Optionals_Total;
 	
 	//Determine price for all misc items
 	var Misc_Total = 0;
 	Misc_Total +=  Anchors_Price;
 	Misc_Total +=  Connection_Fee;
	document.getElementById('Misc_Total').value = Misc_Total;
	
	//Update sectional price totals for review page
	document.getElementById('right_side_price').value = Right_Side_Options_Price;
	document.getElementById('left_side_price').value = Left_Side_Options_Price;
	document.getElementById('front_end_price').value = Front_End_Options_Price;
	document.getElementById('back_end_price').value = Back_End_Options_Price;
	
		
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateRoofTypes
//
//Desc: Grabs the price from the radio button and updates both displayed and
//hidden prices
//////////////////////////////////////////////////////////////////////////////
function updateRoofTypes(){

	if(debug_mode) alert("updateRoofTypes");

	
	//Get current price
	var pre_price = checkValue('roof_options');
	discount_legs = false;
		
		//Abort if there are no roofing options
		if(pre_price==null){
			return;
		}
	
		//Is it BOXED or VERTICAL?
		if(pre_price=="BOXED"){
			//Get the id, look it up
			var id = document.getElementById('boxed_id').value;
			price = Boxed_Eve_Prices[id];
			discount_legs = true;
		}else if(pre_price=="VERT"){
			//Get the id, look it up
			var id = document.getElementById('vert_roof_id').value;		
			price = Vert_Roof_Prices[id];			
			discount_legs = true;
		}else{ //price = 0
			price = 0;
		}//end else
	
	
	//Update Shown price
	var roof_price_shown = document.getElementById('roof_price_shown');
	roof_price_shown.innerHTML = "$"+price;
	
	//Update global price
	roofing_price = price;
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateCenterHeight
//
//Desc: updates the center height stat
//////////////////////////////////////////////////////////////////////////////
function updateCenterHeight(){

	//Find Leg Height in feet
		var id = document.getElementById('leg_height_id').value;
			Height_ID=Form_Heights[id];
		var leg_ht = LegID_To_Height[id];
		
		//Convert to inches
		leg_ht = parseFloat(leg_ht)*12;		

		
	//Find Center Height
	 	//Check roof type
		var roof_type = checkValue('roof_options');
		if(roof_type=="BOXED"){
			var center_ht = parseFloat(Boxed_Roof_Center_Heights[Width_ID]);
		}else if(roof_type=="VERT"){
			var center_ht = parseFloat(Vertical_Roof_Center_Heights[Width_ID]);
		}else{ //Regular roof
			var center_ht = parseFloat(Regular_Roof_Center_Heights[Width_ID]);		
		}//end else	


	//Add them together
		center_ht = parseFloat(center_ht) + parseFloat(leg_ht);

	//Divide out into feet and inches
		var feet = center_ht/12;
		var feet=Math.floor(feet);

		var inches = center_ht-(feet*12);
		
	//Update shown value
		var shown = document.getElementById('center_height');		
		shown.innerHTML = feet+" ft, "+inches+" inches";
		
}//end function
//////////////////////////////////////////////////////////////////////////////
//function updateLegHeight
//
//Desc: This update the pricing for the leg heights.
//////////////////////////////////////////////////////////////////////////////
function updateLegHeight(){

	if(debug_mode) alert("updateLegHeight");
	
	//Get current id
	var id = document.getElementById('leg_height_id').value;
	
	//Check what the minimum height is right now, update if needed.
	//If this isn't the minimum height, set it to the minimum height
	updateMinHeight();
	
	var ht_of_id = LegID_To_Height[id];
	if(ht_of_id<min_height){
		//use min height instead
		id = Height_To_LegID[min_height];
	}//end if

	//Set the leg height pulldown to the correct option
	var el;
	for(var x=0;x<8;x++){
		el = document.getElementById('leg_height_id').options[x];
		if(el && el.value==id){
			el.selected=true;
		}//end if
	}//end for
	
	//Get the price using the leg height id
	var price = Leg_Height_Costs[id];
	
	//Deduct the ht_cost_adjustment (which might be 0), this is so that different
	//standard heights can be taken into account.
	price -= ht_cost_adj;
	
	//The ht_cost_adjustment is for buildings that include the price of 6ft legs
	//Some building features can also trigger this discount - boxed eaves & vertical roofing
	//if we haven't applied a discount already, and the discount flag is up... apply the discount
	if(discount_legs && ht_cost_adj==0){
		price -= six_ft_ht_cost;
		
		//Has the display been updated already to show this?
		if(discount_applied==false){
			//Nope, lets adjust prices
			discount_applied = true;
			
			
			//run ajax and then re-run the process
			var vars = "function_name=ajaxFormHeightCosts&param1="+Frame_ID+"&param2="+six_ft_ht_cost+"&param3="+id;
			updateContentArea('leg_height_box',vars,'run.php','updateForm()');
		}else{
			//Yes, do nothing
		}//end else
		
	}//end if

	
	
	//Unapply the discount if we don't need it anymore
	if(discount_applied && !discount_legs){
		discount_applied = false;
		//run ajax and then re-run the process
		var vars = "function_name=ajaxFormHeightCosts&param1="+Frame_ID+"&param2=0&param3="+id;

		updateContentArea('leg_height_box',vars,'run.php','updateForm()');
	}//end if
	
	//Update price shown
	var newPrice = "$"+price;	
	document.getElementById('leg_height_price_shown').innerHTML=newPrice;
	
	//Update global
	Leg_Height_Price = price;
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateMinHeight
//
//Desc: This function performs numerous checks and alters the global variable
//min_height as needed to reflect changes in the form.
//////////////////////////////////////////////////////////////////////////////
function updateMinHeight(){

	if(debug_mode) alert("updateMinHeight");

 
 	var has_doors = false;
 	var garage_height = 0;
 	var special_roof = false;
 
 	//Check roof type
	var roof_type = checkValue('roof_options');
	if(roof_type=="BOXED" || roof_type=="VERT"){
		special_roof = true;		
	}//end if
	
  
	//If there are doors or door frameouts, that's at least a 6ft min
		if(document.getElementById('right_door_qty').value>0)
			has_doors = true;			
		if(document.getElementById('front_door_qty').value>0)
			has_doors = true;			
		if(document.getElementById('back_door_qty').value>0)
			has_doors = true;			
		if(document.getElementById('left_door_qty').value>0)
			has_doors = true;			
	
		var door_frameout_qty=0;
		if(door_frameout_qty=safeGetElement('right_side_door_frameout_qty')){
			if(door_frameout_qty.value>0)
				has_doors = true;
		}//end if
				
		var door_frameout_qty=0;
		if(door_frameout_qty=safeGetElement('left_side_door_frameout_qty')){
			if(door_frameout_qty.value>0)
				has_doors = true;
		}//end if

		var door_frameout_qty=0;
		if(door_frameout_qty=safeGetElement('front_end_door_frameout_qty')){
			if(door_frameout_qty.value>0)
				has_doors = true;
		}//end if

		var door_frameout_qty=0;
		if(door_frameout_qty=safeGetElement('back_end_door_frameout_qty')){
			if(door_frameout_qty.value>0)
				has_doors = true;
		}//end if

	//If there are garage doors on the front or back, then use the height of
	//the garage door as a min.
		if(document.getElementById('right_garage_door_qty').value>0){
			//Get the height
			var g_id = document.getElementById('right_garage_door_id').value;
			garage_height = 1+Garage_Door_Heights[g_id];
		}//end if
			
		if(document.getElementById('left_garage_door_qty').value>0){
			//Get the height
			var g_id = document.getElementById('left_garage_door_id').value;
			if((1+Garage_Door_Heights[g_id])>garage_height)
				garage_height = 1+Garage_Door_Heights[g_id];
		}//end if

	//If there are garage doors on either of the sides, then use the 
	//height of the gararge door +1Ft as a min.
		if(document.getElementById('front_garage_door_qty').value>0){
			//Get the height
			var g_id = document.getElementById('front_garage_door_id').value;
			if(Garage_Door_Heights[g_id]>garage_height)
				garage_height = Garage_Door_Heights[g_id];
		}//end if
	
		if(document.getElementById('back_garage_door_qty').value>0){
			//Get the height
			var g_id = document.getElementById('back_garage_door_id').value;
			if(Garage_Door_Heights[g_id]>garage_height)
				garage_height = Garage_Door_Heights[g_id];
		}//end if

	//Depending on what we have, set the height to its correct amount.
		if(garage_height>6){
			//has_doors is irrelevant, only use the garage door height
			min_height = garage_height;
		}else if(has_doors || special_roof){
			min_height = 6;
		}else{
			//go with the standard height
			min_height = base_height;
		}//end else		

		

}//end function
//////////////////////////////////////////////////////////////////////////////
//updateRightSideOptions
//
//Desc: Performs changes and updates to right side options in respect to
//the user's selections
//////////////////////////////////////////////////////////////////////////////
function updateRightSideOptions(){

	if(debug_mode) alert("updateRightSideOptions");

	
	//Get Leg Height and update closed price
		var leg_height_id = document.getElementById('leg_height_id').value;
		var height_id = Form_Heights[leg_height_id];
		var price = Side_Closing_Prices[height_id];
			price = price/2;
			price = price.toFixed(2);
			
		//Update shown
		document.getElementById('right_side_closed_cost').innerHTML="($"+price+")";
	
	//Open or Closed?
		var open = checkValue('right_side_option');

		//Open, price is 0
			if(open=="true"){
				//Update hidden price id
				document.getElementById('right_side_closing_id').value=0;	
				
				//Update shown price for that section
				document.getElementById('right_side_price_shown').innerHTML='$0';

				//Update global price
				Right_Side_Options_Price = 0;
				
				//Make sure that the sub-options for this section are hidden
				document.getElementById('right_side_indent').style.display="none";


			}else{//Closed, check for other options as well
				//Update hidden price id for being closed
				document.getElementById('right_side_closing_id').value=Side_Closing_IDs[height_id];	
					
				//start off the section_price
				var section_price = price;
				
				//Add other options
					//Doors?
						//Get door id, qty
							var door_id = document.getElementById('right_door_id').value;
							var door_qty = document.getElementById('right_door_qty').value;
						
						
						//Get price for one door
						var door_price = parseFloat(Door_Prices[door_id]);

						//Add to section price
						sub_price=parseFloat(door_qty*door_price);						
						
						section_price=parseFloat(section_price)+parseFloat(sub_price);
						
					//Garage garage_doors?
						//Get garage_door id, qty
							var garage_door_id = document.getElementById('right_garage_door_id').value;
							var garage_door_qty = document.getElementById('right_garage_door_qty').value;
						
						//Get price for one garage_door
						var garage_door_price = parseFloat(Garage_Door_Prices[garage_door_id]+100);
						
						//Add to section price
						section_price+=parseFloat(garage_door_qty)*parseFloat(garage_door_price);

						

					//Windows?
						//Get window id, qty
							var window_id = document.getElementById('right_window_id').value;
							var window_qty = document.getElementById('right_window_qty').value;

						//Get price for one window
						var window_price = parseFloat(Window_Prices[window_id]);

						//Add to section price
						section_price+=parseFloat(window_qty)*parseFloat(window_price);

					//Frameouts?		
						//Get QTYs (there are no ids needed)
							var door_frameout_qty;
							if(door_frameout_qty=safeGetElement('right_side_door_frameout_qty')){
								door_frameout_qty = door_frameout_qty.value;
								var window_frameout_qty = document.getElementById('right_side_window_frameout_qty').value;
								var garage_frameout_qty = document.getElementById('right_side_garage_frameout_qty').value;								

								//Add final pricing togther
								section_price+=parseFloat(door_frameout_qty)*parseFloat(frameout_door_price);
								section_price+=parseFloat(window_frameout_qty)*parseFloat(frameout_window_price);
								section_price+=parseFloat(garage_frameout_qty)*parseFloat(frameout_garage_price);

							}//end if
						
					
					//Update shown price for that section
					section_price = parseFloat(section_price);
					section_price = section_price.toFixed(2);
					document.getElementById('right_side_price_shown').innerHTML="$"+section_price;
					
					//Update global price
					Right_Side_Options_Price = parseFloat(section_price);



				//Make sure the sub menus for this section are visable
				document.getElementById('right_side_indent').style.display="block";

			}//end else
		
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateLeftSideOptions
//
//Desc: Performs changes and updates to left side options in respect to
//the user's selections
//////////////////////////////////////////////////////////////////////////////
function updateLeftSideOptions(){

	if(debug_mode) alert("updateLeftSideOptions");

	
	//Get Leg Height and update closed price
		var leg_height_id = document.getElementById('leg_height_id').value;
		var height_id = Form_Heights[leg_height_id];
		var price = Side_Closing_Prices[height_id];
			price = price/2;
			price = price.toFixed(2);
			
		//Update shown
		document.getElementById('left_side_closed_cost').innerHTML="($"+price+")";
	
	//Open or Closed?
		var open = checkValue('left_side_option');

		//Open, price is 0
			if(open=="true"){
				//Update hidden price id
				document.getElementById('left_side_closing_id').value=0;	
				
				//Update shown price for that section
				document.getElementById('left_side_price_shown').innerHTML='$0';

				//Update global price
				Left_Side_Options_Price = 0;
				
				//Make sure that the sub-options for this section are hidden
				document.getElementById('left_side_indent').style.display="none";


			}else{//Closed, check for other options as well
				//Update hidden price id for being closed
				document.getElementById('left_side_closing_id').value=Side_Closing_IDs[height_id];	
					
				//start off the section_price
				var section_price = price;
				
				//Add other options
					//Doors?
						//Get door id, qty
							var door_id = document.getElementById('left_door_id').value;
							var door_qty = document.getElementById('left_door_qty').value;
						
						
						//Get price for one door
						var door_price = parseFloat(Door_Prices[door_id]);

						//Add to section price
						sub_price=parseFloat(door_qty*door_price);						
						
						section_price=parseFloat(section_price)+parseFloat(sub_price);
						
					//Garage garage_doors?
						//Get garage_door id, qty
							var garage_door_id = document.getElementById('left_garage_door_id').value;
							var garage_door_qty = document.getElementById('left_garage_door_qty').value;
						
						//Get price for one garage_door
						var garage_door_price = parseFloat(Garage_Door_Prices[garage_door_id]+100);
						
						//Add to section price
						section_price+=parseFloat(garage_door_qty)*parseFloat(garage_door_price);

						

					//Windows?
						//Get window id, qty
							var window_id = document.getElementById('left_window_id').value;
							var window_qty = document.getElementById('left_window_qty').value;

						//Get price for one window
						var window_price = parseFloat(Window_Prices[window_id]);

						//Add to section price
						section_price+=parseFloat(window_qty)*parseFloat(window_price);

					//Frameouts?		
						//Get QTYs (there are no ids needed)
							var door_frameout_qty;
							if(door_frameout_qty=safeGetElement('left_side_door_frameout_qty')){
								door_frameout_qty = door_frameout_qty.value;
								var window_frameout_qty = document.getElementById('left_side_window_frameout_qty').value;
								var garage_frameout_qty = document.getElementById('left_side_garage_frameout_qty').value;								

								//Add final pricing togther
								section_price+=parseFloat(door_frameout_qty)*parseFloat(frameout_door_price);
								section_price+=parseFloat(window_frameout_qty)*parseFloat(frameout_window_price);
								section_price+=parseFloat(garage_frameout_qty)*parseFloat(frameout_garage_price);

							}//end if
						
					
					//Update shown price for that section
					section_price = parseFloat(section_price);
					section_price = section_price.toFixed(2);
					document.getElementById('left_side_price_shown').innerHTML="$"+section_price;
					
					//Update global price
					Left_Side_Options_Price = parseFloat(section_price);



				//Make sure the sub menus for this section are visable
				document.getElementById('left_side_indent').style.display="block";

			}//end else
		
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateFrontEndOptions
//
//Desc: Performs changes and updates to front end options in respect to
//the user's selections
//////////////////////////////////////////////////////////////////////////////
function updateFrontEndOptions(){

	if(debug_mode) alert("updateFrontEndOptions");

	
	//Get Leg Height and update closed price
		var leg_height_id = document.getElementById('leg_height_id').value;
		var height_id = Form_Heights[leg_height_id];
		var price = End_Closing_Prices[height_id];
			price = price.toFixed(2);
	
		end_closing_price = price;
	
		//Update shown
		document.getElementById('front_end_closed_cost').innerHTML="($"+price+")";
	
	//Open, Gabled, or Closed?
		var option = checkValue('front_end_option');
		//Open, price is 0
			if(option=="open"){
				//Update hidden price id
				document.getElementById('front_end_closing_id').value=0;	
				
				//Update shown price for that section
				document.getElementById('front_end_price_shown').innerHTML='$0';

				//Update global price
				Front_End_Options_Price = 0;

				//Make sure that the sub-options for this section are hidden
				document.getElementById('front_end_indent').style.display="none";

			
			}else if(option=="gabled"){
				
				//Update hidden price id
				document.getElementById('front_end_closing_id').value=-1;	
				
				//Update shown price for that section
				document.getElementById('front_end_price_shown').innerHTML='$'+gabled_price;

				//Update global price
				Front_End_Options_Price = parseFloat(gabled_price);
								
			}else{//Closed, check for other options as well
				//Update hidden price id for being closed
				document.getElementById('front_end_closing_id').value=End_Closing_IDs[height_id];	
					
				//start off the section_price
				var section_price = parseFloat(price);
				
				//Add other options
					//Doors?
						//Get door id, qty
							var door_id = document.getElementById('front_door_id').value;
							var door_qty = document.getElementById('front_door_qty').value;
			
			
						//Get price for one door
						var door_price = parseFloat(Door_Prices[door_id]);
	
						//Add to section price
						section_price+=parseFloat(door_qty)*parseFloat(door_price);


					//Garage garage_doors?
						//Get garage_door id, qty
							var garage_door_id = document.getElementById('front_garage_door_id').value;
							var garage_door_qty = parseFloat(document.getElementById('front_garage_door_qty').value);
						
						//Get price for one garage_door
						var garage_door_price = parseFloat(Garage_Door_Prices[garage_door_id]);
						
						//Add to section price
						section_price+=parseFloat(garage_door_qty)*parseFloat(garage_door_price);
						

					//Windows?
						//Get window id, qty
							var window_id = document.getElementById('front_window_id').value;
							var window_qty = parseFloat(document.getElementById('front_window_qty').value);
						
						//Get price for one window
						var window_price = parseFloat(Window_Prices[window_id]);
						
						//Add to section price
						section_price+=parseFloat(window_qty)*parseFloat(window_price);


					//Frameouts?		

						//Get QTYs (there are no ids needed)
							var door_frameout_qty;
							if(door_frameout_qty=safeGetElement('front_end_door_frameout_qty')){
								door_frameout_qty = door_frameout_qty.value;
								var window_frameout_qty = document.getElementById('front_end_window_frameout_qty').value;
								var garage_frameout_qty = document.getElementById('front_end_garage_frameout_qty').value;								

								//Add final pricing togther
								section_price+=parseFloat(door_frameout_qty)*parseFloat(frameout_door_price);
								section_price+=parseFloat(window_frameout_qty)*parseFloat(frameout_window_price);
								section_price+=parseFloat(garage_frameout_qty)*parseFloat(frameout_garage_price);

							}//end if

						if(document.getElementById('front_end_frameOuts')){
							//Get QTYs (there are no ids needed)
								var door_frameout_qty = document.getElementById('front_end_door_frameout_qty').value;
								var window_frameout_qty = document.getElementById('front_end_window_frameout_qty').value;
								var garage_frameout_qty = document.getElementById('front_end_garage_frameout_qty').value;
	
							//Add final pricing togther
								section_price+=parseFloat(door_frameout_qty)*parseFloat(frameout_door_price);
								section_price+=parseFloat(window_frameout_qty)*parseFloat(frameout_window_price);
								section_price+=parseFloat(garage_frameout_qty)*parseFloat(frameout_garage_price);
						}//end if
					//Update shown price for that section
					section_price = parseFloat(section_price);
					section_price = section_price.toFixed(2);
					document.getElementById('front_end_price_shown').innerHTML="$"+section_price;
					
					//Update global price
					Front_End_Options_Price =  parseFloat(section_price);

				//Make sure the sub menus for this section are visable
				document.getElementById('front_end_indent').style.display="block";


			}//end else
		
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateBackEndOptions
//
//Desc: Performs changes and updates to back end options in respect to
//the user's selections
//////////////////////////////////////////////////////////////////////////////
function updateBackEndOptions(){

	if(debug_mode) alert("updateBackEndOptions");

	
	//Get Leg Height and update closed price
		var leg_height_id = document.getElementById('leg_height_id').value;
		var height_id = Form_Heights[leg_height_id];
		var price = End_Closing_Prices[height_id];
			price = price.toFixed(2);
	
		//Update shown
		document.getElementById('back_end_closed_cost').innerHTML="($"+price+")";
	
	//Open, Gabled, or Closed?
		var option = checkValue('back_end_option');
		//Open, price is 0
			if(option=="open"){
				//Update hidden price id
				document.getElementById('back_end_closing_id').value=0;	
				
				//Update shown price for that section
				document.getElementById('back_end_price_shown').innerHTML='$0';

				//Update global price
				Back_End_Options_Price = 0;

				//Make sure that the sub-options for this section are hidden
				document.getElementById('back_end_indent').style.display="none";
				
			}else if(option=="gabled"){
				
				//Update hidden price id
				document.getElementById('back_end_closing_id').value=-1;	
				
				//Update shown price for that section
				document.getElementById('back_end_price_shown').innerHTML='$'+gabled_price;

				//Update global price
				Back_End_Options_Price = gabled_price;
								
			}else{//Closed, check for other options as well
				//Update hidden price id for being closed
				document.getElementById('back_end_closing_id').value=End_Closing_IDs[height_id];	
					
				//start off the section_price
				var section_price = parseFloat(price);
				
				//Add other options
					//Doors?
						//Get door id, qty
							var door_id = document.getElementById('back_door_id').value;
							var door_qty = document.getElementById('back_door_qty').value;
						
						//Get price for one door
						var door_price = Door_Prices[door_id];
						
						//Add to section price
						section_price+=(door_qty*door_price);

					//Garage garage_doors?
						//Get garage_door id, qty
							var garage_door_id = document.getElementById('back_garage_door_id').value;
							var garage_door_qty = document.getElementById('back_garage_door_qty').value;
						
						//Get price for one garage_door
						var garage_door_price = Garage_Door_Prices[garage_door_id];
						
						//Add to section price
						section_price+=(garage_door_qty*garage_door_price);
						

					//Windows?
						//Get window id, qty
							var window_id = document.getElementById('back_window_id').value;
							var window_qty = document.getElementById('back_window_qty').value;
						
						//Get price for one window
						var window_price = Window_Prices[window_id];
						
						//Add to section price
						section_price+=(window_qty*window_price);


					//Frameouts?		
						if(document.getElementById('back_end_frameOuts')){
							//Get QTYs (there are no ids needed)
								var door_frameout_qty = document.getElementById('back_end_door_frameout_qty').value;
								var window_frameout_qty = document.getElementById('back_end_window_frameout_qty').value;
								var garage_frameout_qty = document.getElementById('back_end_garage_frameout_qty').value;
	
							//Add final pricing togther
								section_price+=(door_frameout_qty*frameout_door_price);
								section_price+=(window_frameout_qty*frameout_window_price);
								section_price+=(garage_frameout_qty*frameout_garage_price);
						}//end if
					//Update shown price for that section
					section_price = parseFloat(section_price);
					section_price = section_price.toFixed(2);
					document.getElementById('back_end_price_shown').innerHTML="$"+section_price;
					
					//Update global price
					Back_End_Options_Price =  parseFloat(section_price);

				//Make sure the sub menus for this section are visable
				document.getElementById('back_end_indent').style.display="block";


			}//end else
		
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateExtraBow
//
//Desc: performs price update for the extra bow
//updateExtraBow is height dependant
//////////////////////////////////////////////////////////////////////////////
function updateExtraBow(){

	if(debug_mode) alert("updateExtraBow");


	var option = checkValue('bow_option');
	if(option==null) return;


	//Get Height_ID
		var leg_height_id = document.getElementById('leg_height_id').value;
		var height_id = Form_Heights[leg_height_id];
	//Lookup new bow price id and new bow price
		var bow_price = Bow_Prices[height_id];
		var price_id = Bow_IDs[height_id]
						
	//Update price ID
		document.getElementById('bow_price_id').value=price_id;

	//Update shown and global prices
		if(option=="YES"){

			//Extra bow selected
			var new_price = bow_price;
		}else{

			//Extra bow not selected
			var new_price = 0;
		}//end else		
		
		//update shown values
		document.getElementById('bow_price_shown').innerHTML="$"+new_price;
		document.getElementById('bow_option_shown').innerHTML="YES (+$"+bow_price+")";

		//update global
		Bow_Price = new_price;
				
}//end function
//////////////////////////////////////////////////////////////////////////////
//update12GaugeFraming
//
//Desc: performs price and id updates for 12 Gauge framing option
//////////////////////////////////////////////////////////////////////////////
function update12GaugeFraming(){

	if(debug_mode) alert("update12GaugeFraming");


	//Get id
		var id = checkValue('12gauge_id');
		
		if(!id){ //This optional question is not present
			Gauge12_Price=0;
			return;
		}//end if
		
	//Update global price
		if(id==0)
			Gauge12_Price=0;
		else
			Gauge12_Price = Gauge12_Prices[id];
	//Update shown price
		document.getElementById('12gauge_shown_price').innerHTML="$"+Gauge12_Price;

}//end function
//////////////////////////////////////////////////////////////////////////////
//updateExtraPanels
//
//Desc: performs price and id updates for extra paneling requests
//////////////////////////////////////////////////////////////////////////////
function updateExtraPanels(){

	if(debug_mode) alert("updateExtraPanels");


 	//Are extra panels an option?
 	if(checkValue('Extra_Panels')==null) return;
 
	//If on yes, then make sure its open
	if(checkValue('Extra_Panels')=="YES"){
		document.getElementById('extra_panel_div').style.display='block';
	}else{
		Extra_Panels_Price = 0;

		//Update shown price
		document.getElementById('extra_panels_shown_price').innerHTML="$0";

		document.getElementById('extra_panel_div').style.display='none';		
		return;
	}
	
	//Total up extra panels
		var extra_panels_price_total = 0;
		//Get qtys, this is done specially. Since we don't know how many extra
		//panel sizes or cutting sizes there will be, they are named incrementally
		//1, 2, 3, 4 and so on. Their prices are also lined up in the same way.
		
		x=1;
		while(x>0){
			var target = "Extra_"+x+"_Panel_Qty";
			var qty = document.getElementById(target);
			if(qty==null){
				Extra_Panels_Price=0;
				break;
			}//end else
	
			qty = qty.value;
	
			extra_panels_price_total+=(Extra_Panel_Prices[x-1]*qty);
				
			x++;
			//Safety Net, there's not going to 100 of these
			if(x>100){
			 	alert("Safety Net Hit!");
				break;
			}//end if	
		}//end while		

	
	
	//Total up Cuttings	
		//Cuttings have an identical numbering structure to extra panels.
		x=1;
		while(x>0){
			var target = "Cut_"+x+"_Panel_Qty";
			var qty = document.getElementById(target);
			if(qty==null){
				break;
			}//end else
	
			qty = qty.value;
			extra_panels_price_total+=(Cutting_Prices[x-1]*qty);
				
			x++;
			//Safety Net, there's not going to 100 of these
			if(x>100){
			 	alert("Safety Net Hit!");
				break;
			}//end if	
		}//end while		

	//Update shown price
	document.getElementById('extra_panels_shown_price').innerHTML="$"+extra_panels_price_total;
	
	//Update global price
	Extra_Panels_Price = extra_panels_price_total;
	
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateExtraBracing
//
//Desc: performs price and id updates for extra bracing options
//////////////////////////////////////////////////////////////////////////////
function updateExtraBracing(){

	if(debug_mode) alert("updateExtraBracing");


	//Use length to update price id
		var price_id = Bracing_IDs[Length_ID];

		if(document.getElementById("Bracing_Price_ID")==null){
			Extra_Bracing_Price=0;
			return;	
		}

		document.getElementById("Bracing_Price_ID").value=price_id;
	//Get Extra_option selection
		var choice = checkValue("Extra_Bracing");
	//Abort if the question is not present
		if(!choice){
			return;
		}
	//Lookup price based on selection and price id
		var bracing_price = 0;
		
		if(choice==0){
			//none	
		}else if(choice=="TWO"){
			//2ft bracing
			bracing_price = Bracing_2ft_Prices[price_id];
		}else if(choice=="FOUR"){
			//4ft bracing
			bracing_price = Bracing_4ft_Prices[price_id];
		}//end else

	//update shown prices
	
		//2ft bracing
		var two_foot_price = Bracing_2ft_Prices[price_id];
		document.getElementById('bracing_2ft_price').innerHTML="(+"+two_foot_price+")";	
	
		//4ft bracing
		var four_foot_price = Bracing_4ft_Prices[price_id];
		document.getElementById('bracing_4ft_price').innerHTML="(+"+four_foot_price+")";	
	
		//Sum price
		document.getElementById('extra_bracing_shown_price').innerHTML="$"+bracing_price;
	
	//update global price
		Extra_Bracing_Price = bracing_price;
}//end function
//////////////////////////////////////////////////////////////////////////////
//update26Gauge STUB
//
//Desc: notes if 26 gauge was chosen, sets a global price increase.
//////////////////////////////////////////////////////////////////////////////
function update26Gauge(){
	//Get yes or no
	//Get % increase
	//Set globals
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateJTrim
//
//Desc: handles J trim option
//////////////////////////////////////////////////////////////////////////////
function updateJTrim(){
	
	if(debug_mode) alert("updateJTrim");

	
	//Get the price id
	var price_id = checkValue('JTrim');
	
	//Abort if not present
	if(price_id==null){
		return;
	}
	
	if(price_id==0)
		var price = 0;
	else
		var price = JTrim_Prices[price_id];
	
	//update shown prices
	document.getElementById('jtrim_shown_price').innerHTML="$"+price;
	
	//update globals
	JTrim_Price = price;
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateGroundAnchors
//
//Desc: handles the special anchors issue
//////////////////////////////////////////////////////////////////////////////
function updateGroundAnchors(){

	if(debug_mode) alert("updateGroundAnchors");


	//Get type
	var type = checkValue('anchor_type');
	
	//Calc price
		if(Length_Feet<=36)
			num_anchors = 4;
		else
			num_anchors = 6;

		price = Anchor_Prices[type]*num_anchors;
	
	//Update shown price
	document.getElementById('anchors_shown_price').innerHTML="$"+price;
	
	//Update Global
	Anchors_Price = price;
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateConnectionFees
//
//Desc: does the connection fees thing
//////////////////////////////////////////////////////////////////////////////
function updateConnectionFees(){

	if(debug_mode) alert("updateConnectionFees");


	//What side or end is being connected?
	var connection_point = checkValue('Connection_Fee');
	
	//Get the price based based on the side or end
	if(connection_point=="END"){
		var price_id = document.getElementById('end_connection_fee_id').value;
		var price = End_Connection_Fees[price_id];

	}else if(connection_point=="SIDE"){
		var price_id = document.getElementById('side_connection_fee_id').value;
		var price = Side_Connection_Fees[price_id];		

	}else{
		price = 0;
	}
	
	//Update shown price
	document.getElementById('connection_shown_price').innerHTML = "$"+price;
	
	//Update global price
	Connection_Fee = price;
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateSlabTypes
//
//Desc: Transfers the ID from the select to the Slab_Type for pickup
//////////////////////////////////////////////////////////////////////////////
function updateSlabTypes(){

	if(debug_mode) alert("updateSlabTypes");


	//find the current slab id
	var id = document.getElementById('slab_type_id').value;
	
	//paste it to the radio button
	document.getElementById('Slab_Type').value;

}//end function
//////////////////////////////////////////////////////////////////////////////
//showEl
//
//Desc: shows block the target element
//////////////////////////////////////////////////////////////////////////////
function showEl(target){
	document.getElementById(target).style.display="block";
}
//////////////////////////////////////////////////////////////////////////////
//hideEl
//
//Desc: hides block the target element
//////////////////////////////////////////////////////////////////////////////
function hideEl(target){
	document.getElementById(target).style.display="none";
}
//////////////////////////////////////////////////////////////////////////////
//safeGetElement
//
//Desc: performs document.getElementById but returns false on failure.
//////////////////////////////////////////////////////////////////////////////
function safeGetElement(target){
	var ret = document.getElementById(target);
	
	if(ret==null)
		return false;
	else
		return ret;
		
}//end function
//////////////////////////////////////////////////////////////////////////////
//alertVar
//
//Desc: Prints an alert message with the var
//////////////////////////////////////////////////////////////////////////////
function alertVar(varName,value){
	alert(varName+" = "+value);
}//end function
//////////////////////////////////////////////////////////////////////////////
//updateEnclosures
//
//Desc: Updates the costs of an enclosures and updates the global price listed
//for them.
//////////////////////////////////////////////////////////////////////////////
function updateEnclosures(){

/*
	//Enclosures on or off?
	var none = checkValue('enclosure_option');
	if(none=="true"){
		Enclosure_Price=0;
		document.getElementById('enclosure_price_shown').innerHTML="$0.00";
		document.getElementById('enclosure_price').value=0;
		updateForm2();
		return;
	}//end if
*/	
	
	
	
	//Has the height changed since last time?
//	if(last_height_id!=Height_ID || document.getElementById('one_extra_end').value==1 || 1==1){
	 	//Update Height
//		 	last_height_id=Height_ID;		
		
		//Check for a previous selection in case we need to preserve it
		//Since the value of each radio is and ID, we can only compare position rather than choice.
		//Iterate through the radio array and see if any are checked, note the # of that one.
			var user_choice = document.buildingForm['enclosure_depth_id'];
			var i=0; //declare outside the for loop so we can use it elsewhere.
			
			try{
				//make sure normal options are shown
				var radios = document.getElementById('enclosures_radios');
				radios.style.display = "block";
				
				//hide the error message
				document.getElementById('enclosure_error').style.display='none';
				
				for(i=0;i<=user_choice.length;i++){
					if(i<user_choice.length && user_choice[i].checked==true){
						break;
					}//end if
				}//end for
				
				//Was one checked?
				if(i<user_choice.length){
					//Yes
					var prev_choice_position = i;		
				}else{
					//No (i=user_choice.length), we went off the end of the array 
					var prev_choice_position = -1;
				}//end else
				
			}catch(e){		
		
			//	var prev_choice_position = -1;	
				
			}//end catch


		//Pull new radios using AJAX. This is the only AJAX call on the whole page.
		//Note that if another AJAX call is needed, it will either have to be hooked
		//into this call or hook into this call.
			
			//If an extra end is needed, add its price in
			if(document.getElementById('one_extra_end').value==1 || Full_Enclosure){
				var price_adj = end_closing_price;
			}else{
				var price_adj = 0;
			}

			//Are there any sides closed? Remove their costs as needed
			var openLeft = checkValue('left_side_option');
			var openRight = checkValue('right_side_option');
			
			var salt = Math.round(50*Math.random());
			var vars = "function_name=grabEnclosures&param1="+Height_ID+"&param2="+prev_choice_position+"&param3="+price_adj+"&param4="+openLeft+"&param5="+openRight+"&param6=-1";
			var url = "run.php";
			updateContentArea('enclosure_depths_shown',vars,url,'finishEnclosurePricing('+price_adj+')');		
			
//	}else{
//		finishEnclosurePricing();
//	}

}//end function
function finishEnclosurePricing(price_adj){
	//Time to perform the price retrival and updating
	if(debug_mode) alert("finishEnclosurePricing");

	var depths = document.getElementById('enclosure_depth_id');
	var options = document.getElementsByName('enclosure_option');
	var new_price = 0;

	if(depths==null){
		//there are no enclosures for this leg height!
		//select none, since we cannot 
				
		if(options[1].checked==true || options[2].checked==true){
			alert("Error: There are no enclosures allowed for this leg height.");
			options[0].checked = true;
			document.getElementById('enclosure_indent').style.display="none";
		}//end if				

		var new_price = 0;
		new_price.toFixed(2);

	}else if(options[0].checked==false){

	
		//Get selection, if any
		var selection = checkValue('enclosure_depth_id');
		if(selection != false){
			//They want an enclosure!
			var new_price = parseFloat(price_adj)+parseFloat(Enclosure_Prices[selection]);	

			//Are there any sides closed? Remove their costs as needed
			var openLeft = checkValue('left_side_option');
			var openRight = checkValue('right_side_option');
				//How much do we deduct per side?
				var deduction = parseFloat(Enclosure_Prices[selection]/2);
								
			if(openLeft=="false"){
				new_price -= parseFloat(deduction);
			}
	
			if(openRight=="false"){
				new_price -= parseFloat(deduction);
			}
			
			new_price.toFixed(2);
		

			//start off the section_price
			var section_price = 0;
			
			//Add other options
				//Doors?
					//Get door id, qty
						var door_id = document.getElementById('enclosure_door_id').value;
						var door_qty = document.getElementById('enclosure_door_qty').value;
					
					//Get price for one door
					var door_price = Door_Prices[door_id];
					
					//Add to section price
					section_price+=(door_qty*door_price);
	
				//Garage garage_doors?
					//Get garage_door id, qty
						var garage_door_id = document.getElementById('enclosure_garage_door_id').value;
						var garage_door_qty = document.getElementById('enclosure_garage_door_qty').value;
					
					//Get price for one garage_door
					var garage_door_price = Garage_Door_Prices[garage_door_id];
				
					
					//Add to section price
					section_price+=(garage_door_qty*garage_door_price);
					
	
				//Windows?
					//Get window id, qty
						var window_id = document.getElementById('enclosure_window_id').value;
						var window_qty = document.getElementById('enclosure_window_qty').value;
					
					//Get price for one window
					var window_price = Window_Prices[window_id];
					
					//Add to section price
					section_price+=(window_qty*window_price);
	
	
				//Frameouts?		
					if(document.getElementById('enclosure_frameOuts')){
						//Get QTYs (there are no ids needed)
							var door_frameout_qty = document.getElementById('enclosure_door_frameout_qty').value;
							var window_frameout_qty = document.getElementById('enclosure_window_frameout_qty').value;
							var garage_frameout_qty = document.getElementById('enclosure_garage_frameout_qty').value;
	
						//Add final pricing togther
							section_price+=(door_frameout_qty*frameout_door_price);
							section_price+=(window_frameout_qty*frameout_window_price);
							section_price+=(garage_frameout_qty*frameout_garage_price);
					}//end if
	
				//Update global price if a full enclosure was asked for
				if(Full_Enclosure)
					new_price +=  parseFloat(section_price);

		}else{
			//No enclosure selected
			var new_price = parseFloat('0');
			new_price.toFixed(2);	
		}//end else
				
	}//end else
		
	//Make sure we have a number
	if(new_price=="NaN"){
		new_price = 0;
		new_price.toFixed(2);
	}//end if
		
	//Update shown prices
		document.getElementById('enclosure_price_shown').innerHTML="$"+new_price;
		document.getElementById('enclosure_price').value=new_price;
		
	//update global price
		Enclosure_Price = new_price;
				
		
	updateForm2();
		
}//end function
//////////////////////////////////////////////////////////////////////////////
//closeSides
//
//Desc: Closes both sides, used by enclosures
//////////////////////////////////////////////////////////////////////////////
function closeEnds(){

	if(debug_mode) alert("closeEnds");


	//get front and back options
	var back_opt = document.buildingForm.back_end_option;
	var front_opt = document.buildingForm.front_end_option;
	
	back_opt[2].checked=true;
	
	//Set one extra end to 1
	document.getElementById('one_extra_end').value=1;
	
	Full_Enclosure = true;
	
}//end function
