
//Show and hide radio asterisk for email and phone labels
function radioChanged(form, emailOrPhone) {
	//Save user's values and clear
	var emailValue = form.txtEmail.value;
	var phoneValue = form.txtPhone.value;
	form.txtEmail.value = "";
	form.txtPhone.value = "";
	
	//Swap asterisk position
	if (emailOrPhone == "email") {
		if (document.getElementById("spnEmail").innerHTML.indexOf("Email*") == -1) {
			form.radEmail.disabled = true;
			form.radPhone.disabled = true;
			
			$("#spnEmail").hide("normal", function(){
														document.getElementById("spnEmail").innerHTML = document.getElementById("spnEmail").innerHTML.replace("Email", "Email*");
														$("#spnEmail").show("normal");
														
														$("#spnPhone").hide("normal", function(){
																									document.getElementById("spnPhone").innerHTML = document.getElementById("spnPhone").innerHTML.replace("Phone*", "Phone");
																									$("#spnPhone").show("normal");
														
																									form.radEmail.disabled = false;
																									form.radPhone.disabled = false;
																								});
													});

			//document.getElementById("spnEmail").innerHTML = document.getElementById("spnEmail").innerHTML.replace("Email", "Email *");
			//document.getElementById("spnPhone").innerHTML = document.getElementById("spnPhone").innerHTML.replace("Phone *", "Phone");
		}
	} else {
		if (document.getElementById("spnPhone").innerHTML.indexOf("Phone*") == -1) {
			form.radEmail.disabled = true;
			form.radPhone.disabled = true;
			
			$("#spnPhone").hide("normal", function(){
														document.getElementById("spnPhone").innerHTML = document.getElementById("spnPhone").innerHTML.replace("Phone", "Phone*");
													 	$("#spnPhone").show("normal");
													 
														$("#spnEmail").hide("normal", function(){
																									document.getElementById("spnEmail").innerHTML = document.getElementById("spnEmail").innerHTML.replace("Email*", "Email");
																									$("#spnEmail").show("normal");
																									
																									form.radEmail.disabled = false;
																									form.radPhone.disabled = false;
																								});
													});
			
			//document.getElementById("spnEmail").innerHTML = document.getElementById("spnEmail").innerHTML.replace("Email *", "Email");
			//document.getElementById("spnPhone").innerHTML = document.getElementById("spnPhone").innerHTML.replace("Phone", "Phone *");
		}
	}
	
	//Restore user's values
	form.txtEmail.value = emailValue;
	form.txtPhone.value = phoneValue;
}



//Check fields
function mySubmit(form){
	//Check fields
	if(checkFields(form)){
		//Set result
		changeMessage("spnMessageHolder", "goodFields", "Submitting message...");
		return true;
	}else{
		return false;
	}
}
function checkFields(form) {
	//Check name
	if (form.txtName.value == "") {
		//Set result
		changeMessage("spnMessageHolder", "badFields", "Name cannot be blank.");
		return false;
	}
	
	//Check email and phone
	var emailOrPhone
	for (i=0; i < form.radContact.length; i++) {
		if (form.radContact[i].checked) {
			emailOrPhone = form.radContact[i].value;
		}
	}
	if (emailOrPhone == "email" && form.txtEmail.value == "") {
		changeMessage("spnMessageHolder", "badFields", "Email cannot be blank.");
		return false;
	}
	if (emailOrPhone == "email") {
		var indexMatch = form.txtEmail.value.indexOf("@");
		if (indexMatch < 0) {
			changeMessage("spnMessageHolder", "badFields", "Invalid email address.");
			return false;
		}
		indexMatch = form.txtEmail.value.indexOf(".");
		if (indexMatch < 0) {
			changeMessage("spnMessageHolder", "badFields", "Invalid email address.");
			return false;
		}
	}		
	if (emailOrPhone == "phone" && form.txtPhone.value == "") {
		changeMessage("spnMessageHolder", "badFields", "Phone cannot be blank.");
		return false;
	}
	
	//Check message
	if (form.txtMessage.value == "") {
		//Set result
		changeMessage("spnMessageHolder", "badFields", "Message cannot be blank.");
		return false;
	}
	
	return true;
}



//Setup map
function setupMap(){
	var myMap = new VEMap("divMap");
	var mapOptions = new VEMapOptions();
	mapOptions.BirdseyeOrientation = VEOrientation.East;
	myMap.LoadMap(new VELatLong(40.03579942174773, -76.26634615445751, 0, VEAltitudeMode.RelativeToGround), 19, VEMapStyle.BirdseyeHybrid, false, VEMapMode.Mode2D, true, 1, mapOptions);
}



//Check fields and get directions
function mySubmit2(form){
	//Check fields
	if(checkFields2(form)){
		//Set result
		changeMessage("spnMessageHolder2", "goodFields", "Getting directions...");
		
		//Hide directions
		$("#divDirections").slideUp("normal", function(){
													   		//Clear directions and get new ones
															document.getElementById("divDirections").innerHTML = "";
													   		getDirections(form.txtStartAddress.value, form.txtEndAddress.value);
														});
	}else{
		//Hide directions
		$("#divDirections").slideUp("normal");
		
		//Reset map
		setupMap();
	}
}
function checkFields2(form){
	//Check start address
	if (form.txtStartAddress.value == "") {
		//Set result
		changeMessage("spnMessageHolder2", "badFields", "Start address cannot be blank.");
		return false;
	}
	
	//Check end address
	if (form.txtEndAddress.value == "") {
		//Set result
		changeMessage("spnMessageHolder2", "badFields", "End address cannot be blank.");
		return false;
	}
	
	return true;
}



//Get directions
function getDirections(startAddress, endAddress){
	//Add start and end info
	if(printerFriendlyPage()){
		addLine("<div><strong>Start: </strong>" + startAddress + "</div>");
		addLine("<div><strong>End: </strong>" + endAddress + "</div>");
	}
	
	//Load map and get directions
	var map = new VEMap("divMap");
	map.LoadMap();
	var routeOptions = new VERouteOptions();
	routeOptions.RouteCallback = onGotRoute;
	map.GetDirections([startAddress, endAddress], routeOptions);
}
function onGotRoute(route) {
	//Get form
	var form = document.getElementById("frmDirections");
	
	//Save start and end
	var start = form.txtStartAddress.value;
	var end = form.txtEndAddress.value;
	
	//Add printer friendly link
	var printerFriendlyHtml = "<div style='text-align:right'><a href='directions?start=" + start + "&end=" + end + "' target='_blank'>Printer friendly directions</a></div>"
	if(printerFriendlyPage() == false){
		addLine(printerFriendlyHtml);
	}
	
	//Add total distance info
	//addLine("<br />");
	addLine("<div><strong>Total distance: </strong>" + route.Distance.toFixed(1) + " mile(s)</div>");
	
	//Get route legs
	var legs = route.RouteLegs;
	
	//Get legs
	var curLeg = null;
	for(var i = 0; i < legs.length; i++) {
		//Get current leg
		curLeg = legs[i];
		//Go through each turn
		var turn = null;
		var turnNum = 0;
		for(var j = 0; j < curLeg.Itinerary.Items.length; j ++) {
			//Get turn info
			turn = curLeg.Itinerary.Items[j];
			//Write the turn info
			addLine("<p style='margin-left:25px'>" + turnNum + ".\t" + turn.Text + " - " + turn.Distance.toFixed(1) + " mile(s)</p>");
			turnNum++;
		}
	}
	
	//Add printer friendly link
	if(printerFriendlyPage() == false){
		addLine(printerFriendlyHtml);
	}
	
	//Show directions
	$("#divDirections").slideDown("normal", function(){
														//Success message
														changeMessage("spnMessageHolder2", "goodFields", "Directions retrieved successfully!");
													});
}
function addLine(html) {
	//Add line to directions div
	document.getElementById("divDirections").innerHTML = document.getElementById("divDirections").innerHTML + html;
}
function printerFriendlyPage(){
	var url = document.location.href;
	
	if(url.indexOf("/contact/directions") != -1){
		return true
	}else{
		return false
	}
}



//Reverse directions
function reverseDirections(){
	//Get form
	var form = document.getElementById("frmDirections");
	
	//Save start and end
	var start = form.txtStartAddress.value;
	var end = form.txtEndAddress.value;
	
	//Save and clear href
	var href = document.getElementById("lnkReverseDirections").getAttribute("href");
	document.getElementById("lnkReverseDirections").setAttribute("href", "javascript:");
	
	//Swap
	$("#divStartAddress").hide("normal", function(){
														form.txtStartAddress.value = end;
														$("#divStartAddress").show("normal");
														
														$("#divEndAddress").hide("normal", function(){
																										form.txtEndAddress.value = start;
																										$("#divEndAddress").show("normal");
																										
																										//Reset href
																										document.getElementById("lnkReverseDirections").setAttribute("href", href);
																									
																										//Get directions if necessary
																										mySubmit2(form);
																									});
													});
}



//Hide and show map
function toggleMap(){
	if($("#divMap").is(":visible")){
		$("#divMap").slideUp("normal");
	}else{
		$("#divMap").slideDown("normal");
	}
}

