$(document).ready(function() {
	toggleFencing(false);
	toggleSteel(true);
	toggleRustRail();
	toggleFusion();
});
function validateForm() {
	var valid = true;
	var message = "";
	var quoteType = $("#quoteType").val();
	var productAreaValid = true;

	if ($("#firstName").val() == "") {
		valid = false;
		message += "<li>- First Name</li>";
	}	
	if ($("#lastName").val() == "") {
		valid = false;
		message += "<li>- Last Name</li>";
	}
	if ($("#address").val() == "") {
		valid = false;
		message += "<li>- Address</li>";
	}
	if ($("#city").val() == "") {
		valid = false;
		message += "<li>- City</li>";
	}
	if ($("#state").val() == "default") {
		valid = false;
		message += "<li>- State</li>";
	}
	if ($("#zip").val() == "") {
		valid = false;
		message += "<li>- Zip</li>";
	}
	if ($("#phone").val() == "" || $("#phone").val().match("^(\\d-)?\\d{3}-\\d{3}-\\d{4}$") == null) {
		valid = false;
		message += "<li>- A Valid Phone Number (0-000-000-0000)</li>";
	}
	if ($("#contact1").selected && $("#email").match(".+@.+\\.[a-z0-9]+") == null) {
		valid = false;
		message += "<li>- A Valid Email</li>";
	}
	if ($("#contact1").selected || $("#contact2").selected) {
		valid = false;
		message += "<li>- Preferred Contact Type</li>";
	}
	if ($("#type1").selected || $("#type2").selected) {
		valid = false;
		message += "<li>- Are you a contractor?</li>";
	}

	if (quoteType == "default") {
		valid = false;
		message += "<li>- Product Request Type</li>";
	}
	else {
		var info = null;
		if (quoteType == "Doors") {
			info = validateDoors();
		} else if (quoteType == "Buildings") {
			info = validateBuildings();
		} else if (quoteType == "Landscaping") {
			info = validateConcrete();
		} else if (quoteType == "Trusses") {
			info = validateTrusses();
		} else if (quoteType == "Decking") {
			info = validateDecking();
		} else if (quoteType == "Roofing") {
			info = validateSteel();
		}
		
		if (info) {
			if (!info[0]) {
				valid = false;
			}
			
			message += info[1];
		}
	}
	
	if (!valid) { 
		$.prompt("<div class='title'>Missing Required Fields</div>"
				+"<p>The following fields are required:<ul>" + message + "</ul></p>", {
			buttons: {Ok:true}
		});
	}
	
	return valid;
}

function validateDoors() {
	var valid = true;
	var doorTypeSelected = false;
	var msg = "";
	
	if ($("#doorWidth").val() == "") {
		valid = false;
		msg += "<li>- Door Width</li>";
	}

	if ($("#doorHeight").val() == "") {
		valid = false;
		msg += "<li>- Door Height</li>";
	}

	if ($("#doorQuantity").val() == "") {
		valid = false;
		msg += "<li>- Quantity</li>";
	}

	if ($("#wallThickness").val() == "") {
		valid = false;
		msg += "<li>- Wall Thickness</li>";
	}
	
	$(".doorTypes:checkbox").each( function() {
		if (this.checked) {
			doorTypeSelected = true;
		}
	});
	
	if (!doorTypeSelected) {
		valid = false;
		msg += "<li>- Atleast One Door Type</li>";
	}
	
	return [valid, msg];
}

function validateBuildings() {
	var valid = true;
	var msg = "";
	
	if ($("#buildingWidth").val() == "") {
		valid = false;
		msg += "<li>- Building Width</li>";
	}

	if ($("#buildingLength").val() == "") {
		valid = false;
		msg += "<li>- Building Length</li>";
	}

	if ($("#wallHeight").val() == "") {
		valid = false;
		msg += "<li>- Wall Height</li>";
	}

	if ($("#buildingUse").val() == "") {
		valid = false;
		msg += "<li>- Use of Building</li>";
	}
	
	return [valid, msg];
}

function validateConcrete() {
	var valid = true;
	var concreteTypeSelected = false;
	var msg = "";
	if($("#type".val().equals("Concrete"))){
		if ($("#patioWidth").val() == "") {
			valid = false;
			msg += "<li>- Patio Width</li>";
		}

		if ($("#patioLength").val() == "") {
			valid = false;
			msg += "<li>- Patio Length</li>";
		}
	
		$(".concreteTypes:checkbox").each( function() {
			if (this.checked) {
				concreteTypeSelected = true;
			}
		});
	
		if (!concreteTypeSelected) {
			valid = false;
			msg += "<li>- Atleast One Block Type</li>";
		}
	}else{
		if ($("#length").val() == "") {
			valid = false;
			msg += "<li>- Patio Length</li>";
		}
	}
	return [valid, msg];
}

function validateTrusses() {
	var valid = true;
	var msg = "";
	
	if ($("#roof1:checked").val() != null || $("#floor1:checked").val() != null) {
		
	} else {
		valid = false;
		msg += "<li>- Select Either a Roof or Floor Truss</li>";
	}

	if ($("#roof1:checked").val() != null) {
		if ($("#roofType").val() == "default") {
			valid = false;
			msg += "<li>- Select a Roof Truss Type</li>";
		}

		if ($("#roofQuantity").val() == "") {
			valid = false;
			msg += "<li>- Enter Roof Truss Quantity</li>";
		}
		
		if ($("#roofSpan").val() == "") {
			valid = false;
			msg += "<li>- Enter Roof Truss Span</li>";
		}

		if ($("#roofPitch").val() == "") {
			valid = false;
			msg += "<li>- Enter Roof Truss Pitch</li>";
		}
	} 

	if ($("#floor1:checked").val() != null) {
		if ($("#floorQuantity").val() == "") {
			valid = false;
			msg += "<li>- Enter Floor Truss Quantity</li>";
		}

		if ($("#floorSpan").val() == "") {
			valid = false;
			msg += "<li>- Enter Floor Truss Span</li>";
		}

		if ($("#leftFloorEndType").val() == "default") {
			valid = false;
			msg += "<li>- Select a Left End Truss Type</li>";
		}

		if ($("#rightFloorEndType").val() == "default") {
			valid = false;
			msg += "<li>- Select a Right End Truss Type</li>";
		}
	}

	
	return [valid, msg];
}

function validateDecking() {
	var valid = true;
	var msg = "";
	
	if ($("#deckWidth").val() == "") {
		valid = false;
		msg += "<li>- Deck Width</li>";
	}
	
	if ($("#deckHeight").val() == "") {
		valid = false;
		msg += "<li>- Deck Height</li>";
	}

	if ($("#deckLength").val() == "") {
		valid = false;
		msg += "<li>- Deck Length</li>";
	}
	
	return [valid, msg];
}

function validateSteel() {
	var valid = true;
	var msg = "";
	
	if ($("#buildingLength").val() == "") {
		valid = false;
		msg += "<li>- Building Length</li>";
	}

	if ($("#buildingWidth").val() == "") {
		valid = false;
		msg += "<li>- Building Width</li>";
	}
	
	return [valid, msg];
}

function toggleDecking(type) {
	
	if (!type) {
		$(".UDInput:radio").each( function() {
			this.disabled = true;
		});
		$(".notUD").each( function() {
			this.style.display = "block";
		});
		$(".UDFields").each( function() {
			this.style.display = "none";
		});
	} else {
		$(".UDInput:radio").each( function() {
			this.disabled = false;
		});
		$(".notUD").each( function() {
			this.style.display = "none";
		});
		$(".UDFields").each( function() {
			this.style.display = "block";
		});
	}
}
function toggleFusion() {
		$(".Fusion").each( function() {
			this.style.display = "block";
		});
		$(".Rustic").each( function() {
			this.style.display = "none";
		});
		$(".Natural").each( function() {
			this.style.display = "none";
		});
}
function toggleRustic() {
	$(".Fusion").each( function() {
		this.style.display = "none";
	});
	$(".Rustic").each( function() {
		this.style.display = "block";
	});
	$(".Natural").each( function() {
		this.style.display = "none";
	});
}
function toggleNatural() {
	$(".Fusion").each( function() {
		this.style.display = "none";
	});
	$(".Rustic").each( function() {
		this.style.display = "none";
	});
	$(".Natural").each( function() {
		this.style.display = "block";
	});
}
function toggleFusRail() {
	$(".Fus").each( function() {
		this.style.display = "block";
	});
	$(".Rust").each( function() {
		this.style.display = "none";
	});
	$(".Nat").each( function() {
		this.style.display = "none";
	});
}
function toggleRustRail() {
$(".Fus").each( function() {
	this.style.display = "none";
});
$(".Rust").each( function() {
	this.style.display = "block";
});
$(".Nat").each( function() {
	this.style.display = "none";
});
}
function toggleNatRail() {
$(".Fus").each( function() {
	this.style.display = "none";
});
$(".Rust").each( function() {
	this.style.display = "none";
});
$(".Nat").each( function() {
	this.style.display = "block";
});
}
function toggleFencing(type) {
	
	if (!type) {
		$(".CInput:radio").each( function() {
			this.disabled = true;
		});
		
		$(".CFields").each( function() {
			this.style.display = "none";
		});
		$(".WInput:radio").each( function() {
			this.disabled = false;
		});
		
		$(".WFields").each( function() {
			this.style.display = "block";
		});
	} else {
		$(".CInput:radio").each( function() {
			this.disabled = false;
		});
		
		$(".CFields").each( function() {
			this.style.display = "block";
		});
		$(".WInput:radio").each( function() {
			this.disabled = true;
		});
		
		$(".WFields").each( function() {
			this.style.display = "none";
		});
	}
}

function toggleSteel(type) {
	
	if (!type) {
		$(".RInput:radio").each( function() {
			this.disabled = true;
		});
		
		$(".RFields").each( function() {
			this.style.display = "none";
		});
		$(".SInput:radio").each( function() {
			this.disabled = false;
		});
		
		$(".SFields").each( function() {
			this.style.display = "block";
		});
	} else {
		$(".RInput:radio").each( function() {
			this.disabled = false;
		});
		
		$(".RFields").each( function() {
			this.style.display = "block";
		});
		$(".SInput:radio").each( function() {
			this.disabled = true;
		});
		
		$(".SFields").each( function() {
			this.style.display = "none";
		});
	}
}

function showForm(sel) {
	var newForm = sel.options[sel.selectedIndex].value;
	
	if (newForm != "default") {
		$("#RequestForm").html($("#" + newForm + "Form").html());
		
		if (newForm == "Trusses") {
			$(document).ready(function() {
				$("#projectStartDate").datepicker({ showOn : 'both', buttonText: 'Select Date', buttonImage: 'web/img/forms/calendar_icon.jpg', buttonImageOnly: true });
			});
			$( "form#quotes_form" )
				.attr( "enctype", "multipart/form-data" )
				.attr( "encoding", "multipart/form-data" )
			;
		} 
		else if (newForm == "Buildings") {
			$(document).ready(function() {
				$("#projectStartDateBuildings").datepicker({ showOn : 'both', buttonText: 'Select Date', buttonImage: 'web/img/forms/calendar_icon.jpg', buttonImageOnly: true });
			});
		}
		else {
			$( "form#quotes_form" )
				.attr( "enctype", "application/x-www-form-urlencoded" )
				.attr( "encoding", "application/x-www-form-urlencoded" )
			;
		}
	} else {
		$("#RequestForm").html("");
	}
}


function toPhone(input){
	var s=stripNonDigits(input);
	var s14="              "+s;
	var s16="                "+s;
	
	var tmp=s14.substring(s14.length-11, s14.length);
	
	var one=stripNonDigits(tmp.substring(0,1));
	if (one.length>0) one+="-";
	var areacode=stripNonDigits(tmp.substring(1,4));
	if (areacode.length>0) areacode+="-";
	var num1=stripNonDigits(tmp.substring(4,7));
	if (num1.length>0) num1+="-";
	var num2=stripNonDigits(tmp.substring(7,11));
	
	var x=one+areacode+num1+num2;
	
	return x;
}

function formatPhone(obj){
	var str=obj.value;
	obj.value=toPhone(str);
}

function stripNonDigits(str){
	return str.replace(/[^0-9]/g,"");
}

function setPreferredRetail(obj) {
	$("#menardsStore").html(obj.getAttribute("store"));
	$("#menards").val(obj.getAttribute("store"));
	$("#menardsNbr").val(obj.getAttribute("storeID"));
	
	$("#storeLookup").dialog('destroy');
}

function getPreferredRetailLocations(zipItem){
	if(zipItem.value.length >= 5){
		var message = "<p>There were no stores found in your area.</p>";
		var xml = $.ajax({
			  url: "storeLookup.do",
			  cache: false,
			  data: "zip=" + zipItem.value,
			  async: false
			 }).responseXML.documentElement;
		
		var storeList = xml.getElementsByTagName("store");
		
		if (storeList.length > 0) {
		
			message = "<p class='storeList'>";
			for(var i = 0; i < storeList.length; i++){
				message += "<div class='store' store='Menards " + storeList[i].getAttribute("name") + "' " +
								"storeID='" + storeList[i].getAttribute("id") +"'" +
								"onclick='setPreferredRetail(this);'>";
				message += "<div class='name'>Menards " + storeList[i].getAttribute("name") 
								+ " - " + storeList[i].getElementsByTagName("distance")[0].childNodes[0].nodeValue + " " 
								+ storeList[i].getElementsByTagName("distance")[0].getAttribute("units") + "</div>";
				message += "<div class='address'>" + storeList[i].getElementsByTagName("address")[0].childNodes[0].nodeValue + "</div>";
				message += "<div class='city'>" + storeList[i].getElementsByTagName("city")[0].childNodes[0].nodeValue + ", " 
								+ storeList[i].getElementsByTagName("state")[0].childNodes[0].nodeValue + "</div>";
				message += "</div>";
			}
			
			message += "</p>";
		}
		$("#storeLookup").html(message);
		$("#storeLookup").dialog({
			bgiframe: true,
			modal: true,
			resizable: false,
			buttons: {
				"None of the Above": function() {
					$(this).dialog('destroy');
				}
			}
		});
		
	}
}
