var arrBarnLength1 = new Array("24","32","40","48","56","64","72","80","88","96","104","112","120");
var arrBarnLength2 = new Array("32","40","48","56","64","72","80","88","96","104","112","120");
var arrBarnLength3 = new Array("40","48","56","64","72","80","88","96","104","112","120");

var arrBarnHeight1 = new Array("10","12","14","16","18","20");
var arrBarnHeight2 = new Array("10","12","14","16", "18"); //Removing 20' high barn for 60 wide and 85lb snowload
var arrBarnHeight3 = new Array("10","12","14","16"); //Removing 18, 20 for 70x18 and 70x20, 85lb snowload

var arrWindowSize1 = new Array("2'x2'","3'x2'","3'x3'");
var arrWindowSize2 = new Array('28" x 37"','32" x 45"','36" x 49"');

var singleDoorSizes = new Array("8x10","8x12","8x14","8x16","8x18","8x20","10x10","10x12","10x14","10x16","10x18","10x20","12x10","12x12","12x14","12x16","12x18","12x20","14x10","14x12","14x14","14x16","14x18","14x20");
var splitDoorSizes = new Array("8x10","8x12","8x14","8x16","8x18","8x20","10x10","10x12","10x14","10x16","10x18","10x20","12x10","12x12","12x14","12x16","12x18","12x20","14x10","14x12","14x14","14x16","14x18","14x20","16x10","16x12","16x14","16x16","16x18","16x20","18x10","18x12","18x14","18x16","18x18","18x20","20x10","20x12","20x14","20x16","20x18","20x20");
var overheadDoorOpeningSizes = new Array("8x7","8x8","9x7","9x8","9x9","10x7","10x8","10x9","10x10","10x11","10x12","10x13","10x14","12x7","12x8","12x9","12x10","12x11","12x12","12x13","12x14","14x7","14x8","14x9","14x10","14x11","14x12","14x13","14x14","16x7","16x8","16x9","16x10","16x11","16x12","16x13","16x14","18x7","18x8","18x9","18x10","18x11","18x12","18x13","18x14");



function setBarnSize() {
	var barnWidth = 0;
	if (document.forms['quoteform'].elements["barnwidth"].length > 0) barnWidth = document.quoteform.barnwidth.options[document.quoteform.barnwidth.selectedIndex].value;
	
	barnHeight = arrBarnHeight1;
	if (document.quoteform.snowload.value > 55) {
		if (barnWidth >= 60) barnHeight = arrBarnHeight2;
		if (barnWidth >= 70) barnHeight = arrBarnHeight3;
	}
	
	if (barnWidth >= 50) {
		$("#concrete_credit_container").show();
		$("#upgrade612roofpitch_li").hide();
	} else {
		$("#concrete_credit_container").hide();
		$("#upgrade612roofpitch_li").show();
		$("#concretecredit").attr("checked", false);
	}
	
	if (barnWidth < 28) {
		barnLength = arrBarnLength1;
	} else if (barnWidth <= 32) {
		barnLength = arrBarnLength2;
	} else {
		barnLength = arrBarnLength3;
	}

	removeAllOptions(document.quoteform.barnlength);
	removeAllOptions(document.quoteform.barnheight);
	addOption(document.quoteform.barnlength, "Length", "");
	addOption(document.quoteform.barnheight, "Height", "");

	if (barnWidth != '') {
		for (var i=0; i < barnLength.length; ++i) {
			if (barnWidth > 40) {
				if (barnWidth < parseInt(barnLength[i])) {
					addOption(document.quoteform.barnlength, barnLength[i], barnLength[i]);	
				}
			} else {
				addOption(document.quoteform.barnlength, barnLength[i], barnLength[i]);
			}
		}
		for (var i=0; i < barnHeight.length; ++i) {
			addOption(document.quoteform.barnheight, barnHeight[i], barnHeight[i]);
		}
	}
	resetSizeOptions();
	
}

function resetSizeOptions() {
	for (var i=1; i<=4; ++i) {
		if ($("#doors li#door"+i).length > 0) {
			document.quoteform['door' + i + '_type'].selectedIndex = 0;
			calculateLargeDoorSizes(i);
		}
	}
}

function setWindowSize(val, num) {
	windowSizes = arrWindowSize1;
	el = document.quoteform['window' + num + '_size'];
	removeAllOptions(el);

	if (val == "Insulated Vinyl Double Hung Window") windowSizes = arrWindowSize2;
	if (val == "Opening Only (No Window Included)") {
		jQuery("select[name=window" + num + "_size], label[for=window" + num + "_size]").hide();
	} else {
		jQuery("select[name=window" + num + "_size], label[for=window" + num + "_size]").show();
		addOption(el, "Choose Size", "");
	}

	if (val != '' && val != 'Opening Only (No Window Included)') {
		for (var i=0; i < windowSizes.length; ++i) {
			addOption(el, windowSizes[i], windowSizes[i]);
		}
	}
}

$(document).ready(function(){

	$("#AddDoor a").click(function() {
		var maxDoor = 8;
		var nextNumber = 0;
		for (var i=1; i <= maxDoor; ++i) {
			if ($("#doors li#door"+i).length == 0 && nextNumber == 0) nextNumber = i;
		}
		if (nextNumber > maxDoor) return false;
		if ($("#doors li").size() == (maxDoor-1)) $("#AddDoor").hide();

		var strText = '<li id="door' + nextNumber + '">';
		strText = strText + '<div class="labelholder">';
		strText = strText + '<label for="door' + nextNumber + '_type">Type</label>';
		strText = strText + '<select name="door' + nextNumber + '_type" onchange="calculateLargeDoorSizes(' + nextNumber + ');">';
		strText = strText + '<option value="" selected="selected">Choose Type</option>';
		strText = strText + '<option value="Single Sliding Door">Single Sliding Door</option>';
		strText = strText + '<option value="Split Sliding Door">Split Sliding Door</option>';
		strText = strText + '<option value="Overhead Door Opening">Overhead Door Opening</option>';
		strText = strText + '</select>';
		strText = strText + '</div>';
		strText = strText + '<div class="labelholder">';
		strText = strText + '<label for="door' + nextNumber + '_size">Size (w x h)</label>';
		strText = strText + '<select name="door' + nextNumber + '_size" onchange="buildQuote();">';
		strText = strText + '<option value="" selected="selected">Choose Size</option>';
		strText = strText + '</select>';
		strText = strText + '</div>';
		strText = strText + '<div class="labelholder ohdoptions" id="ohd' + nextNumber + '_options">';
		strText = strText + '<label for="door' + nextNumber + '_ohd">Should we include a door?</label>';
		strText = strText + '<select name="door' + nextNumber + '_ohd" onchange="buildQuote();">';
		strText = strText + '<option value="" selected="selected">Opening Only</option>';
		strText = strText + '<option value="Model 2285">Insulated Garage Door</option>';
		strText = strText + '<option value="Model 3250">Non-Insulated Garage Door</option>';
		strText = strText + '</select></div>';


		strText = strText + '<div class="labelholder">';
		strText = strText + '<a href="#" class="deleteDoor" onclick="return removeDoor(' + nextNumber + ');">Delete</a>';
		strText = strText + '</div>';
		strText = strText + '</li>';
		$("#doors").append(strText);
		return false;
	});

	$("#AddEntryDoor a").click(function() {
		var maxDoor = 8;
		var nextNumber = 0;
		for (var i=1; i <= maxDoor; ++i) {
			if ($("#doors2 li#li_entrydoors"+i).length == 0 && nextNumber == 0) nextNumber = i;
		}
		if (nextNumber > maxDoor) return false;
		if ($("#doors2 li").size() == (maxDoor-1)) $("#AddEntryDoor").hide();

		var strText = '<li id="li_entrydoors' + nextNumber + '">';
		strText = strText + '<div class="labelholder">';
		strText = strText + '<label for="entrydoors' + nextNumber + '">Type</label>';
		strText = strText + '<select name="entrydoors' + nextNumber + '" onchange="buildQuote();">';
		strText = strText + '<option value="" selected="selected">Choose Type</option>';
		strText = strText + '<option value="36&quot; Six-Panel Residential Door w/ Wood Jamb">36&quot; Six-Panel Residential Door w/ Wood Jamb</option>';
		strText = strText + '<option value="36&quot; Nine-Light Residential Door w/ Wood Jamb">36&quot; Nine-Light Residential Door w/ Wood Jamb</option>';
		strText = strText + '<option value="36&quot; Flush Commercial Door w/ Steel Jamb">36&quot; Flush Commercial Door w/ Steel Jamb</option>';
		strText = strText + '<option value="36&quot; Nine-Light Commercial Door w/ Steel Jamb">36&quot; Nine-Light Commercial Door w/ Steel Jamb</option>';
		strText = strText + '<option value="Opening Only (No Door Included)">Opening Only (No Door Included)</option>';
		strText = strText + '</select></div>';
		
		strText = strText + '<div class="labelholder">';
		strText = strText + '<a href="#" class="deleteEntryDoor" onclick="return removeEntryDoor(' + nextNumber + ');">Delete</a>';
		strText = strText + '</div>';
		strText = strText + '</li>';
		$("#doors2").append(strText);
		return false;
	});

	$("#AddEquineDoor a").click(function() {
		var maxDoor = 8;
		var nextNumber = 0;
		for (var i=1; i <= maxDoor; ++i) {
			if ($("#doors3 li#li_equinedoors"+i).length == 0 && nextNumber == 0) nextNumber = i;
		}
		if (nextNumber > maxDoor) return false;
		if ($("#doors3 li").size() == (maxDoor-1)) $("#AddEquineDoor").hide();

		var strText = '<li id="li_equinedoors' + nextNumber + '">';
		strText = strText + '<div class="labelholder">';
		strText = strText + '<label for="equinedoors' + nextNumber + '">Type</label>';
		strText = strText + '<select name="equinedoors' + nextNumber + '" onchange="buildQuote();">';
		strText = strText + '<option value="" selected="selected">Choose Type</option>';
		strText = strText + '<option value="Dutch Door 4\' x 7\'">Dutch Door 4\' x 7\'</option>';
		strText = strText + '<option value="Dutch Door Opening (No Door Included)">Dutch Door Opening (No Door Included)</option>';
		strText = strText + '<option value="Dutch Window 4\' x 3\' 6&quot;">Dutch Window 4\' x 3\' 6&quot;</option>';
		strText = strText + '<option value="Dutch Window Opening (No Window Included)">Dutch Window Opening (No Window Included)</option>';
		strText = strText + '</select></div>';
		
		strText = strText + '<div class="labelholder">';
		strText = strText + '<a href="#" class="deleteEquineDoor" onclick="return removeEquineDoor(' + nextNumber + ');">Delete</a>';
		strText = strText + '</div>';
		strText = strText + '</li>';
		$("#doors3").append(strText);
		return false;
	});

	$("#AddWindow a").click(function() {
		var maxWindow = 8;
		var nextNumber = 0;
		for (var i=1; i <= maxWindow; ++i) {
			if ($("#windows li#window"+i).length == 0 && nextNumber == 0) nextNumber = i;
		}
		if (nextNumber > maxWindow) return false;
		if ($("#windows li").size() == (maxWindow-1)) $("#AddWindow").hide();

		var strText = '<li id="window' + nextNumber + '">';
		strText = strText + '<div class="labelholder">';
		strText = strText + '<label for="window' + nextNumber + '_type">Type</label>';
		strText = strText + '<select name="window' + nextNumber + '_type" onchange="setWindowSize(this.options[this.selectedIndex].value,' + nextNumber + ');buildQuote();">';
		strText = strText + '<option value="" selected="selected">Choose Window Type</option>';
		strText = strText + '<option value="Non-Insulated Vinyl Sliding Window">Non-Insulated Vinyl Sliding Window</option>';
		strText = strText + '<option value="Insulated Vinyl Sliding Window">Insulated Vinyl Sliding Window</option>';
		strText = strText + '<option value="Insulated Vinyl Double Hung Window">Insulated Vinyl Double Hung Window</option>';
		strText = strText + '<option value="Opening Only (No Window Included)">Opening Only (No Window Included)</option>';
		strText = strText + '</select>';
		strText = strText + '</div>';
		strText = strText + '<div class="labelholder">';
		strText = strText + '<label for="window' + nextNumber + '_size">Size (w x h)</label>';
		strText = strText + '<select name="window' + nextNumber + '_size" onchange="buildQuote();">';
		strText = strText + '<option value="" selected="selected">Choose Size</option>';
		strText = strText + '</select>';
		strText = strText + '</div>';
		strText = strText + '<div class="labelholder">';
		strText = strText + '<a href="#" class="deleteWindow" onclick="return removeWindow(' + nextNumber + ');">Delete</a>';
		strText = strText + '</div>';
		strText = strText + '</li>';
		$("#windows").append(strText);
		return false;
	});

	$("#AddPorch a").click(function() {
		var nextNumber = 0;
		var maxNumber = 2;
		for (var i=1; i <=maxNumber; ++i) {
			if ($("#porches li#porch"+i).length == 0 && nextNumber == 0) nextNumber = i;
		}
		if (nextNumber > maxNumber) return false;
		if ($("#porches li").size() == (maxNumber-1)) $("#AddPorch").hide();

		var strText = '<li id="porch' + nextNumber + '">';
		strText = strText + '<div class="labelholder">';
		strText = strText + '<label for="porchtype' + nextNumber + '">Type</label>';
		strText = strText + '<select name="porchtype' + nextNumber + '" onchange="buildQuote();">';
		strText = strText + '<option value="" selected="selected">Choose Type</option>';
		strText = strText + '<option value="Porch">Open Porch Roof</option>';
		strText = strText + '<option value="Lean-To">Enclosed Lean-To</option>';
		strText = strText + '</select>';
		strText = strText + '</div>';
		strText = strText + '<div class="labelholder">';
		strText = strText + '<label for="porchwidth' + nextNumber + '">Width</label>';
		strText = strText + '<select name="porchwidth' + nextNumber + '" onchange="buildQuote();">';
		strText = strText + '<option value="" selected="selected">Choose Width</option>';
		strText = strText + '<option value="8">8</option>';
		strText = strText + '<option value="10">10</option>';
		strText = strText + '<option value="12">12</option>';
		strText = strText + '</select>';
		strText = strText + '</div>';
		strText = strText + '<div class="labelholder">';
		strText = strText + '<a href="#" class="deletePorch" onclick="return removePorch(' + nextNumber + ');">Delete</a>';
		strText = strText + '</div>';
		strText = strText + '</li>';
		$("#porches").append(strText);
		return false;
	});
	
	$("#quotePrint").click(function() {
		if (document.quoteform.grandTotal.value) {
			$("#quoteform").attr("target","_blank").attr("action","/quote-lock/");
			document.quoteform.formtype.value = "print";
			document.quoteform.submit();
		} else {
			alert('Please finish your quote before printing.');
		}
		return false;
	});

	$("#submitlockrequest").click(function() {
		if (document.quoteform.grandTotal.value) {
			if (checkForm(document.lockform)) {
				$("#quoteform").attr("target","_self").attr("action","/quote-lock/");
				document.quoteform.formtype.value = "lockrate";
				document.quoteform.username.value = getHTMLEncode(document.lockform.username.value);
				document.quoteform.email.value = getHTMLEncode(document.lockform.email.value);
				document.quoteform.phone.value = getHTMLEncode(document.lockform.phone.value);
				document.quoteform.comments.value = getHTMLEncode(document.lockform.comments.value);
				$("#overlay_lockquote").jqm().jqmHide();
				document.quoteform.submit();
			}
		} else {
			alert('Please finish your quote before locking in.');
		}
		return false;
	});
	
	$("#overlay_lockquote").jqm({trigger: "a#quoteLock", overlay: "75" });

	$('#zip').keydown(function(e) {
		if(e.keyCode == 13) {
			calculateSnowload();
			return false;
		}
	});

	$("#SnowloadCalculate").click(function() {
		calculateSnowload();
		return false;
	});


});

function calculateSnowload() {
	$("#zipWait").css("display","inline");
	$.post("/wp-content/themes/diy_1.0/getsnowload.php", {
		zip: $("#zip").val()
	}, function(data) {

		var dataArray = new Array();
		var pairs = data.split("&");
		for (i=0; i<pairs.length; i++) {
			var keyval = pairs[i].split("=");
			dataArray[keyval[0]] = keyval[1];
		}
		
		if (dataArray["error"] != undefined) {
			//alert("Error: " + dataArray["error"] + "\n\nYou may need to try again.");
			document.quoteform.city.value = "";
			document.quoteform.state.value = "";
			if (dataArray["error"] == "No Zip Code Entered") {
				$("#ErrorMessage").html("<strong>Error:</strong> No zip code entered. Please try again.");
				document.quoteform.snowload.value = "";
			} else {
				document.quoteform.snowload.value = 25;
				$("#ErrorMessage").html("<strong>Warning:</strong> Our snowload calculation tool can't connect to the data servers right now. You can try again in just a moment or you can create a quote with the default snowload value of 25, but please be aware that your quote will not be official until we can confirm your location and snowload.");
			}
			$("#ErrorMessage").show();
			
		} else {
			$("#ErrorMessage").hide();
			if (checkCoverage(dataArray["state"],dataArray["zip"],"service")) {
				
				//Setup the widths
				checkCoverage(dataArray["state"],dataArray["zip"],"block50plus");
				
				document.quoteform.snowload.value = dataArray["snow"];
				document.quoteform.city.value = dataArray["city"];
				document.quoteform.state.value = dataArray["state"];
				if (checkCoverage(dataArray["state"],dataArray["zip"],"labor")) {
					document.quoteform.allowLabor.value = "1";
					$("#laborArea").show();
				} else {
					document.quoteform.allowLabor.value = "0";
					$("#laborArea").hide();
				}
				
				if (dataArray["state"] == "NY") {
					$("#NewYorkMessage").show();
				} else {
					$("#NewYorkMessage").hide();
				}

				
				buildQuote();
			} else {
				alert("We do not provide service to " + dataArray["city"] + ", " + dataArray["state"] + ".\nWe apologize for the inconvenience.");
				$("#NewYorkMessage").hide();
				document.quoteform.allowLabor.value = "0";
				$("#laborArea").hide();
			}
		}
		$("#zipWait").hide();
	});
}

function checkCoverage(state,zip,coverageType) {
	arrAllowedStates = new Array("DE","IL","IN","IA","KY","MD","MI","MO","NJ","NY","NC","OH","PA","TN","VA","WV","AR","NE","KS","OK","CO");
	arrZipBlock = new Array("49710","49715","49719","49724","49725","49726","49728","49736","49745","49748","49752","49757","49760","49762","49768","49774","49775","49780","49781","49782","49783","49784","49785","49786","49788","49793");
	arrAllowedLaborStates = new Array("DE","IN","MD","MI","NJ","NY","OH","PA","VA","WV","NC","KY","IL");
	arrZipLaborBlock = new Array("49710","49715","49719","49724","49725","49726","49728","49736","49745","49748","49752","49757","49760","49762","49768","49774","49775","49780","49781","49782","49783","49784","49785","49786","49788","49793");
	arrColoradoBlackoutZips = new Array("80025", "80127", "80135", "80302", "80401", "80420", "80421", "80422", "80423", "80424", "80425", "80426", "80427", "80428", "80429", "80430", "80432", "80433", "80434", "80435", "80436", "80437", "80438", "80439", "80440", "80442", "80443", "80444", "80446", "80447", "80448", "80449", "80451", "80452", "80453", "80454", "80455", "80456", "80457", "80459", "80461", "80463", "80465", "80466", "80467", "80468", "80469", "80470", "80471", "80473", "80474", "80475", "80476", "80477", "80478", "80479", "80480", "80481", "80482", "80483", "80487", "80488", "80497", "80498", "80510", "80511", "80512", "80515", "80517", "80532", "80535", "80536", "80540", "80541", "80545", "80809", "80813", "80814", "80816", "80819", "80820", "80827", "80829", "80860", "80863", "80866", "81023", "81024", "81040", "81055", "81091", "81101", "81102", "81120", "81121", "81122", "81123", "81124", "81125", "81126", "81128", "81129", "81130", "81131", "81132", "81133", "81135", "81136", "81137", "81138", "81140", "81141", "81143", "81144", "81146", "81147", "81148", "81149", "81151", "81152", "81154", "81155", "81157", "81201", "81210", "81211", "81212", "81215", "81220", "81221", "81222", "81223", "81224", "81225", "81226", "81227", "81228", "81230", "81231", "81232", "81233", "81235", "81236", "81237", "81239", "81240", "81241", "81242", "81243", "81244", "81247", "81248", "81251", "81252", "81253", "81290", "81301", "81302", "81303", "81320", "81321", "81323", "81324", "81325", "81326", "81327", "81328", "81329", "81330", "81331", "81332", "81334", "81335", "81401", "81402", "81403", "81410", "81411", "81413", "81414", "81415", "81416", "81418", "81419", "81420", "81422", "81423", "81424", "81425", "81426", "81427", "81428", "81429", "81430", "81431", "81432", "81433", "81434", "81435", "81501", "81502", "81503", "81504", "81505", "81506", "81507", "81520", "81521", "81522", "81523", "81524", "81525", "81526", "81527", "81601", "81602", "81610", "81611", "81612", "81615", "81620", "81621", "81623", "81624", "81625", "81626", "81630", "81631", "81632", "81633", "81635", "81636", "81637", "81638", "81639", "81640", "81641", "81642", "81643", "81645", "81646", "81647", "81648", "81649", "81650", "81652", "81653", "81654", "81655", "81656", "81657", "81658");
	arrBlockStates50plus = new Array("CO", "NE", "KS", "OK", "AR", "IA");

	if (coverageType == "service") {
		coverage = false;
		for (i=0;i<arrAllowedStates.length;i++) {
			if (state == arrAllowedStates[i]) coverage = true;
		}
		for (i=0;i<arrZipBlock.length;i++) {
			if (zip == arrZipBlock[i]) coverage = false;
		}
		for (i=0;i<arrColoradoBlackoutZips.length;i++) {
			if (zip == arrColoradoBlackoutZips[i]) coverage = false;
		}
		if (checkRange(zip,coverageType)) coverage = false;
		return coverage;

	} else if (coverageType == "labor") {
		coverage = false;
		for (i=0;i<arrAllowedLaborStates.length;i++) {
			if (state == arrAllowedLaborStates[i]) coverage = true;
		}
		for (i=0;i<arrZipLaborBlock.length;i++) {
			if (zip == arrZipLaborBlock[i]) coverage = false;
		}
		if (checkRange(zip,coverageType)) coverage = false;
		return coverage;

	} else if (coverageType == "block50plus") {
		blocklist = false;
		for (i=0;i<arrBlockStates50plus.length;i++) {
			if (state == arrBlockStates50plus[i]) blocklist = true;
		}
		
		var arrBarnWidth = new Array("24", "28", "30", "32", "36", "40", "50", "60", "70");
		if (blocklist) arrBarnWidth = new Array("24", "28", "30", "32", "36", "40");

		removeAllOptions(document.quoteform.barnwidth);
		addOption(document.quoteform.barnwidth, "Width", "");

		for (var i=0; i < arrBarnWidth.length; ++i) {
			addOption(document.quoteform.barnwidth, arrBarnWidth[i], arrBarnWidth[i]);
		}
	}
}

function checkRange(zip,coverageType) {
	returnValue = false;
	if (coverageType == "service") {
		if (zip >= 49800 && zip < 49999) returnValue = true;
	} else if (coverageType == "labor") {
		if (zip >= 49800 && zip < 49999) returnValue = true;
	}
	return returnValue
}
function getSnowload(strSnowload) {
	if (!isNumeric(strSnowload)) strSnowload = 0;
	if (strSnowload <= 25) {
			strSnowload = 25;
	} else if (strSnowload <= 35) {
			strSnowload = 35;
	} else if (strSnowload <= 55) {
			strSnowload = 55;
	} else {
			strSnowload = 85;
	}
	return strSnowload;
}
function checkOverhangStyle() {
	if (document.quoteform.overhangtype.selectedIndex == 0) {
		$("#overhangstyleli").hide();
	} else {
		$("#overhangstyleli").show();
	}
}
function removeDoor(doorNumber) {
	$("#doors li#door" + doorNumber).remove();
	$("#AddDoor").show();
	buildQuote();
	return false;
}
function removeEntryDoor(doorNumber) {
	$("#doors2 li#li_entrydoors" + doorNumber).remove();
	$("#AddEntryDoor").show();
	buildQuote();
	return false;
}
function removeEquineDoor(doorNumber) {
	$("#doors3 li#li_equinedoors" + doorNumber).remove();
	$("#AddEquineDoor").show();
	buildQuote();
	return false;
}
function removeWindow(windowNumber) {
	$("#windows li#window" + windowNumber).remove();
	$("#AddWindow").show();
	buildQuote();
	return false;
}
function removePorch(doorNumber) {
	$("#porches li#porch" + doorNumber).remove();
	$("#AddPorch").show();
	buildQuote();
	return false;
}

function calculateLargeDoorSizes(doorNumber) {
	var doorType = getSel('door' + doorNumber + '_type');
	var barnheight = getSel("barnheight");
	var barnwidth = getSel("barnwidth");
	var el = document.quoteform['door' + doorNumber + '_size'];
	removeAllOptions(el);
	addOption(el, "Choose Size", "");
	
	if (doorType == "Overhead Door Opening") {
		for (i=0;i<overheadDoorOpeningSizes.length;i++) {
			sizecalc = overheadDoorOpeningSizes[i].split("x");
			if (sizecalc[1] <= (barnheight-1)) addOption(el, overheadDoorOpeningSizes[i], overheadDoorOpeningSizes[i]);
		}
		$("#ohd" + doorNumber + "_options").show();
	} else if (doorType == "Single Sliding Door") {
		for (i=0;i<singleDoorSizes.length;i++) {
			sizecalc = singleDoorSizes[i].split("x");
			if (sizecalc[0] <= (barnwidth / 2) && sizecalc[1] == barnheight) addOption(el, singleDoorSizes[i], singleDoorSizes[i]);
		}
		$("#ohd" + doorNumber + "_options").hide();
	} else if (doorType == "Split Sliding Door") {
		for (i=0;i<splitDoorSizes.length;i++) {
			sizecalc = splitDoorSizes[i].split("x");
			if (sizecalc[0] <= (barnwidth / 2) && sizecalc[1] == barnheight) addOption(el, splitDoorSizes[i], splitDoorSizes[i]);
		}
		$("#ohd" + doorNumber + "_options").hide();
	} else {
		$("#ohd" + doorNumber + "_options").hide();
	}
}
function removeFirstDoor() {
	document.quoteform.door1_type.selectedIndex = 0;
	document.quoteform.door1_size.selectedIndex = 0;
	document.quoteform.door1_ohd.selectedIndex = 0;
	$("#ohd1_options").hide();
	calculateLargeDoorSizes(1);
	buildQuote();
	return false;
}

function removeFirstEntryDoor() {
	document.quoteform.entrydoors1.selectedIndex = 0;
	buildQuote();
	return false;
}
function removeFirstEquineDoor() {
	document.quoteform.equinedoors1.selectedIndex = 0;
	buildQuote();
	return false;
}

function removeFirstWindow() {
	document.quoteform.window1_type.selectedIndex = 0;
	document.quoteform.window1_size.selectedIndex = 0;
	setWindowSize("",1);
	buildQuote();
	return false;
}

function removeFirstPorch() {
	document.quoteform.porchtype1.selectedIndex = 0;
	document.quoteform.porchwidth1.selectedIndex = 0;
	buildQuote();
	return false;
}

function removeOverhang() {
	document.quoteform.overhangtype.selectedIndex = 0;
	checkOverhangStyle();
	buildQuote();
	return false;
}
function resetCupolas() {
	if (!document.quoteform.cupola.checked) {
		document.quoteform.cupolasize.selectedIndex = 0;
		document.quoteform.cupolasize2.selectedIndex = 0;
		document.quoteform.cupolasize3.selectedIndex = 0;
	}
}
	



function buildQuote() {
	
	if (!document.quoteform.snowload.value || !document.quoteform.zip.value) {
		alert("Please enter your zip code and snowload before building your barn quote.");
		return false;
	}
	document.quoteform.snowload1.value = getSnowload(document.quoteform.snowload.value);
	if (document.quoteform.barnwidth.selectedIndex == 0 || document.quoteform.barnlength.selectedIndex == 0 || document.quoteform.barnheight.selectedIndex == 0) return false;
	$("#priceWait").css("display","inline");

	//Hide Laminated Posts Upgrade Option For Tall Barns
	if (getSel('barnheight') > 14) {
		$("#laminatedposts_li").hide();
	} else {
		$("#laminatedposts_li").show();
	}
	
	snowload = document.quoteform.snowload.value;
	barnwidth = getSel('barnwidth');
	barnheight = getSel('barnheight');
	
	//Hide Laminated Posts Upgrade Option For Some Tall Barns Based on Snow Load
	if (snowload > 25 && barnwidth >= 70 && barnheight >= 14) {
		$("#laminatedposts_li").hide();
	} else if (snowload > 35 && barnwidth == 50 && barnheight >= 14) {
		$("#laminatedposts_li").hide();
	} else if (snowload > 35 && barnwidth == 60 && barnheight >= 12) {
		$("#laminatedposts_li").hide();
	} else if (snowload > 35 && barnwidth == 70) {
		$("#laminatedposts_li").hide();
	} else if (snowload > 55 && barnwidth >= 50) {
		$("#laminatedposts_li").hide();
	}
	
	//Hide Post Protector Option
	$("#postprotectoroption").show();
	if (snowload > 5 && barnwidth == 70 && barnheight == 20) {
		$("#postprotectoroption").hide();
	} else if (snowload > 25 && barnwidth >= 60 && barnheight == 20) {
		$("#postprotectoroption").hide();
	} else if (snowload > 35 && barnwidth >= 40 && barnheight == 20) {
		$("#postprotectoroption").hide();
	} else if (snowload > 35 && barnwidth >= 50 && barnheight >= 18) {
		$("#postprotectoroption").hide();
	} else if (snowload > 35 && barnwidth >= 60 && barnheight >= 16) {
		$("#postprotectoroption").hide();
	} else if (snowload > 55 && barnwidth >= 28 && barnheight == 20) {
		$("#postprotectoroption").hide();
	} else if (snowload > 55 && barnwidth >= 36 && barnheight >= 18) {
		$("#postprotectoroption").hide();
	} else if (snowload > 55 && barnwidth >= 50 && barnheight >= 16) {
		$("#postprotectoroption").hide();
	} else if (snowload > 55 && barnwidth >= 70 && barnheight >= 14) {
		$("#postprotectoroption").hide();
	}	
	

	thisForm = document.quoteform;
	$.post("/wp-content/themes/diy_1.0/getquote.php",
		$("#quoteform").serialize(),
		function(data){
			var dataArray = new Array();
			document.quoteform.quotedata.value = getHTMLEncode(data);
			var pairs = data.split("&");

			for (i=0; i<pairs.length; i++) {
				var keyval = pairs[i].split("=");
				dataArray[keyval[0]] = keyval[1];
			}

			var salestaxrate = dataArray["salestaxrate"];
			var materialsTotal = 0;
			var laborTotal = parseFloat(dataArray["labortotal"]);
			var extraLabor = parseFloat(dataArray["extraLabor"]);
			if (laborTotal < 2000) laborTotal = 2000 + extraLabor;

			//$("#console").html("");
			var materialArray = new Array("barnprice","doorprice","entrydoorsprice","equinedoorsprice","windowprice","porchprice","overhangprice","skylightprice","postprotectionprice","cupolaprice","wainscotingprice","foamclosurestripsprice","continuousridgeventprice","cupola2price", "cupola3price", "roofsheathingprice", "linerpanelwallsprice", "linerpanelceilingsprice", "insulationpackagewallsprice", "insulationpackageroofsprice", "insulationpackageceilingsprice", "upgrade612roofpitchprice", "laminatedpostprice", "stampedplansprice", "concretecreditprice", "wallgirt2x6price");
			for (i=0; i<materialArray.length; i++) {
				materialsTotal += parseFloat(dataArray[materialArray[i]]);
				//$("#console").append(materialArray[i] + ": " + parseFloat(dataArray[materialArray[i]]) + "<br />");
			}


			salesTaxTotal = salestaxrate * materialsTotal;

			$("#materialsValue").text("$" + numberFormat(materialsTotal));
			$("#salesTaxValue").text("$" + numberFormat(salesTaxTotal));
			$("#laborValue").text("$" + numberFormat(laborTotal));

			grandTotal = materialsTotal + salesTaxTotal;
			$("#grandtotal").text("$" + numberFormat(grandTotal));

			for (i=1; i<=5; i++) {
				if (dataArray["material"+i]) document.quoteform.elements['material'+i].value = dataArray["material"+i];
			}
			document.quoteform.materials.value = getHTMLEncode(dataArray["materials"]);
			document.quoteform.materialsTotal.value = numberFormat(materialsTotal);
			document.quoteform.salesTaxTotal.value = numberFormat(salesTaxTotal);
			document.quoteform.grandTotal.value = numberFormat(grandTotal);
			document.quoteform.laborTotal.value = numberFormat(laborTotal);
			$("#priceWait").hide();
		}
	);

}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function checkForm(TheForm){
	if(!TheForm.username.value) {
		alert ("Please enter your name.");
		TheForm.username.focus();
		return false;
	}
	if(!isValidEmailAddress(TheForm.email.value)) {
		alert ("Please enter a valid email address.");
		TheForm.email.focus();
		return false;
	}
	return true;
}



function isNumeric(sText) {
	var ValidChars = "0123456789."; var IsNumber = true; var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) {
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1) IsNumber = false;
	}
	return IsNumber;
}
function showOptions(el,selectName) {
	if (el.checked) {
		$("#"+selectName).show();
	} else {
		$("#"+selectName).hide();
	}
}
function getRadio(elName) {
	for (var i=0; i < document.forms['quoteform'].elements[elName].length; i++) {
		if (document.forms['quoteform'].elements[elName][i].checked) return document.forms['quoteform'].elements[elName][i].value;
	}
}
function getSel(elName) {
	if (isObject(document.forms['quoteform'].elements[elName])) {
		if (document.forms['quoteform'].elements[elName].length > 0) return document.forms['quoteform'].elements[elName].options[document.forms['quoteform'].elements[elName].selectedIndex].value;
	}
}
function getCheck(elName) {
	if (document.forms['quoteform'].elements[elName].checked) { return "on"; } else { return ""; }
}
function addOption(selectbox, text, value) {
	var optn = document.createElement("option");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}
function removeAllOptions(selectbox) { var i; for(i=selectbox.options.length-1;i>=0;i--) { selectbox.remove(i); } }
function isObject(what) { return (typeof what == 'object'); }
function getHTMLEncode(t) {
    return t.toString().replace("|","&").replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
}
function numberFormat(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

