var map;
var zoom_level = 10;
var gsvars = getVars();
var myOptions = {
	zoom: zoom_level,
	mapTypeId: google.maps.MapTypeId.ROADMAP
};
var directionsService = new google.maps.DirectionsService();
var directionsDisplay;
var email_type = "";
var bus_centerID;
var has_cookie;
var has_session;
var email_lat;
var email_lng;
var geo_timeout;
var center_id=0;
var lat = 0;
var long = 0;

$(document.body).ready(function(){
	checkCookie("ag_loc");
	checkSession("center_id");
	
	lat = getCookie("ny_lat");
	long = getCookie("ny_long");
	
	if(long && lat){
		if(has_cookie){
			center_id = has_cookie;
		}
		if (has_session){ 
			center_id = has_session;
		}
		getNearestBusCentersWithSetLocation();
	}else{
		if (has_session) {
			center_id = has_session;
			if (geo_position_js.init()) {
				geo_timeout = setTimeout(getNearestBusCentersWithSetLocation,1000);
				geo_position_js.getCurrentPosition(getNearestBusCentersWithSetLocation,  function(){return false;});	
			} 
		}else if (has_cookie) {
			setCookie("ag_loc", has_cookie);
			center_id = has_cookie;
			if (geo_position_js.init()) {
				geo_timeout = setTimeout(getNearestBusCentersWithSetLocation,1000);
				geo_position_js.getCurrentPosition(getNearestBusCentersWithSetLocation, function(){return false;});	
			}
		}
	}
});

function checkCookie(cookie_name) {
	$.ajax({
		type: "POST",
		url: "/ajax/getcookie",
		async: false,
		data:"cookie_name="+cookie_name,
		dataType: 'text',
		complete: function(data){
			if (data) {
				if (data.responseText == "No cookie") {
					has_cookie = false;
				} else {
					has_cookie = data.responseText;
				}
			}
		}
	});
}
function getCookie(cookie_name) {
	var r = false;
	$.ajax({
		type: "POST",
		url: "/ajax/getcookie",
		async: false,
		data:"cookie_name="+cookie_name,
		dataType: 'text',
		complete: function(data){
		if (data) {
			if (data.responseText == "No cookie") {
				r = false;
			} else {
				r = data.responseText;
			}
		}
	}
	});
	return r;
}
function setCookie(cookie_name, cookie_value) {
	$.ajax({
		type: "POST",
		url: "/ajax/setcookie",
		data:"cookie_name="+cookie_name+"&cookie_value="+cookie_value,
		async: false,
		dataType: 'text',
		complete: function(data){
			if (data) {
				//alert(data.responseText);
			}
		}
	});
}

function checkSession(session_var) {
	$.ajax({
		type: "POST",
		url: "/ajax/getsessionvar",
		data:"var_name="+session_var,
		async: false,
		dataType: 'text',
		complete: function(data){
			if (data) {
				if (data.responseText == "No session variable") {
					has_session = false;
				} else {
					has_session = data.responseText;
				}
			}
		}
	});
}

function getNearestBusCentersWithoutSetLocation(position) {
	clearTimeout(geo_timeout);
	$.ajax({
		type: "POST",
		url: "/ajax/getnearbycenters",
		data:"lat="+position.coords.latitude+"&lng="+position.coords.longitude,
		dataType: 'json',
		complete: function(data){
			if (data) {
				var nearest_centers = jQuery.parseJSON(data.responseText);
				populateCenters(nearest_centers);
			}
		}
	});	
}

function getNearestBusCentersWithSetLocation(position) {
	clearTimeout(geo_timeout);
	if(!lat && !long && typeof position > 0){
		lat = position.coords.latitude;
		long = position.coords.longitude
		setCookie("ny_lat",lat);
		setCookie("ny_long",long);
	}
	$.ajax({
		type: "POST",
		url: "/ajax/getnearbycenters2",
		data:"lat="+lat+"&lng="+long+"&bus_center_id="+center_id,
		async: false,
		dataType: 'json',
		complete: function(data){
			if (data) {
				$('#ny_zip_code').hide();
				var nearest_centers = jQuery.parseJSON(data.responseText);
				populateCenters(nearest_centers);
			}
		}
	});	
}

function getLatLongByZipCode(zip_code){
	$.ajax({
		type: "POST",
		url: "/ajax/getlatlong",
		data:"zip_code="+zip_code,
		async: false,
		dataType: 'json',
		complete: function(data){
			if (data) {
				var ll =  jQuery.parseJSON(data.responseText);
				lat = ll.lat;
				long = ll.lng;
				setCookie("ny_lat",lat);
				setCookie("ny_long",long);
				getNearestBusCentersWithSetLocation();
				$('#ny_container')[0].style.height = ($('#ny_addr').height()+82)+"px";
			}
		}
	});	
}
function noGeoLoc() {
	clearTimeout(geo_timeout);
	//Salt Lake City coordinates
	var position = {
		coords: {
			latitude: 40.7608333,
			longitude: -111.8902778
		}
	}
	getNearestBusCentersWithoutSetLocation(position);
}


function populateCenters(bus_centers) {
	var ny_other_centers = $("#ny_other_centers")[0];
	var latlng;
	var svars = getVars();
	ny_other_centers.innerHTML = "";
	for (var i in bus_centers) {
		latlng = new google.maps.LatLng(bus_centers[i].latitude, bus_centers[i].longitude);
		if (i == 0) {
			if(typeof bus_centers[i].statusID != "undefined" && bus_centers[i].statusID == 39)
				$("#ny_coming_soon").show();
			else
				$("#ny_coming_soon").hide();
			$("#ny_name")[0].innerHTML = bus_centers[i].centerWebName;
			$("#ny_addr1")[0].innerHTML = bus_centers[i].address1;
			$("#ny_addr2")[0].innerHTML = bus_centers[i].address2;
			if(bus_centers[i].address2 == "" || bus_centers[i].address2 == null){
				$("#ny_addr2").hide();
			}else{
				$("#ny_addr2").show();
			}
			$("#ny_citystatezip")[0].innerHTML = bus_centers[i].city + ", " + bus_centers[i].abbreviation + " " + bus_centers[i].zip;
			$("#ny_hours")[0].innerHTML = bus_centers[i].businessHours;
			$("#ny_phone")[0].innerHTML = bus_centers[i].websitePhoneNumber;
			$("#ny_tf_phone")[0].innerHTML = bus_centers[i].websiteTollFreeNumber;
			if(bus_centers[i].address2 == "" || bus_centers[i].address2 == null){
				$("#ny_tf_phone").hide();
			}else{
				$("#ny_tf_phone").show();
			}
			$("#ny_mapdir")[0].latlng = latlng;
			$("#ny_mapdir")[0].centerID = bus_centers[i].centerID;
			$("#ny_mapdir")[0].onclick = function() {
				email_lat = bus_centers[i].latitude;
				email_lng = bus_centers[i].longitude
				bus_centerID = bus_centers[i].centerID;
				startDirections(this.centerID);
			}
			$("#ny_visit_site")[0].centername = gsvars[110]+"/"+bus_centers[i].base; 
			$("#ny_visit_site")[0].onclick = function() {
				window.location.href = "/"+this.centername;
			}
			$("#ny_email")[0].href = "mailto:"+bus_centers[i].websiteEmail;
//			if (has_cookie != bus_centers[i].centerID) {
//				$("#ny_set_location")[0].centerID = bus_centers[i].centerID
//				$("#ny_set_location")[0].onclick = function() {
//					setCookie("ag_loc",this.centerID);
//					window.location.reload();
//				}
//			} else {
//				$("#ny_set_location").hide();
//			}
		} else {
			nyo_center_container = document.createElement("div");
			nyo_center_container.className = "ny_center_container";
			nyo_addr = document.createElement("div");
			nyo_addr.className = "ny_addr";
			
			var ny_vr = document.createElement("div");
			ny_vr.className = "ny_vr";

			nyo_name = document.createElement("div");
			nyo_name.className = "ny_name nyws";
			nyo_name.innerHTML = bus_centers[i].centerWebName;
			
			nyo_addr1 = document.createElement("div");
			nyo_addr1.className = "ny_addr1 nyws";
			nyo_addr1.innerHTML = bus_centers[i].address1
			
			nyo_addr2 = document.createElement("div");
			nyo_addr2.className = "ny_addr2 nyws";
			nyo_addr2.innerHTML = bus_centers[i].address2;
			
			nyo_citystatezip = document.createElement("div");
			nyo_citystatezip.className = "ny_citystatezip nyws";
			nyo_citystatezip.innerHTML = bus_centers[i].city + ", " + bus_centers[i].abbreviation + " " + bus_centers[i].zip;
			
			nyo_hours = document.createElement("div");
			nyo_hours.className = "ny_hours";
			nyo_hours.innerHTML = bus_centers[i].businessHours;
			
			nyo_phone = document.createElement("div");
			nyo_phone.className = "ny_phone nyws";
			nyo_phone.innerHTML = bus_centers[i].websitePhoneNumber;
			
			nyo_phone_tf = document.createElement("div");
			nyo_phone_tf.className = "ny_tf_phone nyws";
			nyo_phone_tf.innerHTML = bus_centers[i].websiteTollFreeNumber;
			
			nyo_nav = document.createElement("div");
			nyo_nav.className = "ny_nav";
			
			nyo_mapdir = document.createElement("div");
			nyo_mapdir.className = "cpointer";
			nyo_mapdir.innerHTML = svars[38];
			nyo_mapdir.latlng = latlng;
			nyo_mapdir.centerID = bus_centers[i].centerID;
			nyo_mapdir.onclick = function() {
				email_lat = bus_centers[i].latitude;
				email_lng = bus_centers[i].longitude;
				startDirections(this.centerID);
			}
			
			nyo_visit_site = document.createElement("div");
			nyo_visit_site.className = "cpointer";
			nyo_visit_site.innerHTML = svars[37];
			nyo_visit_site.centername = gsvars[110]+"/"+bus_centers[i].base;
			nyo_visit_site.onclick = function() {
				window.location.href = "/"+this.centername;
			}
			
			nyo_email = document.createElement("a");
			nyo_email.className = "ny_email cpointer";
			nyo_email.innerHTML = svars[36];
			nyo_email.href = "mailto:"+bus_centers[i].websiteEmail;
			
//			nyo_set_location = document.createElement("div");
//			nyo_set_location.className = "cpointer";
//			nyo_set_location.innerHTML = svars[35];
//			nyo_set_location.centerID = bus_centers[i].centerID;
//			nyo_set_location.onclick = function() {
//				setSession("center_id="+this.centerID);
//				setCookie("ag_loc",this.centerID);
//				window.location.reload();
//			}
			
			nyo_clear = document.createElement("div");
			nyo_clear.className = "clear";
			
			ny_other_centers.appendChild(nyo_center_container);
			nyo_center_container.appendChild(nyo_addr);
			nyo_center_container.appendChild(ny_vr);
			nyo_addr.appendChild(nyo_name);
			nyo_addr.appendChild(nyo_addr1);
			if(nyo_addr2.innerHTML != '' && nyo_addr2.innerHTML != null){
				nyo_addr.appendChild(nyo_addr2);
			}
			nyo_addr.appendChild(nyo_citystatezip);
			nyo_addr.appendChild(nyo_hours);
			nyo_addr.appendChild(nyo_phone);
			if(nyo_phone_tf.innerHTML != '' && nyo_phone_tf.innerHTML != null){
				nyo_addr.appendChild(nyo_phone_tf);
			}

			nyo_center_container.appendChild(nyo_nav);
			nyo_nav.appendChild(nyo_mapdir);
			nyo_nav.appendChild(nyo_visit_site);
			nyo_nav.appendChild(nyo_email);
//			nyo_nav.appendChild(nyo_set_location);
			nyo_nav.appendChild(nyo_clear);
			
		}
	}
	nyzip = document.createElement("div");
	nyzip.id = "ny_zip_code_holder2";
	nyzip.className = "ny_addr_under";
	nyzip.innerHTML = svars[39];
	nybr = document.createElement("br");
	nyzip.appendChild(nybr);
	
	nyzipinput = document.createElement("input");
	nyzipinput.id = "ny_zipcode2";
	nyzipinput.type = "text"
		
	$(nyzipinput).keydown(function(e){
		  if(e.keyCode == 13)
			  getLatLongByZipCode(this.value);
	  });
		
	nyzipimg = document.createElement("img");
	nyzipimg.src = "/images/1600/globals/orange_right_button.png";
	nyzipimg.title = "Find Locations";
	nyzipimg.onclick = function(){getLatLongByZipCode($('#ny_zipcode2')[0].value)};
	nyzipimg.style.position = "relative";
	nyzipimg.style.left = "8px";
	nyzipimg.style.top = "3px";
	nyzipimg.style.cursor = "pointer";
	
//	nyzip.appendChild(nyzipinput);
//	nyzip.appendChild(nyzipimg);
//	
//	ny_other_centers.appendChild(nyzip);
//	ny_other_centers.appendChild(nyo_clear);
	
}

function showOtherCenters() {
	$("#ny_other_centers").slideDown("slow",function(){$('#ny_zip_code_holder2').show();});
	$('#ny_container')[0].style.height = ($('.ny_center_container').height()+42)+"px";
	$("#ny_find_another")[0].innerHTML = gsvars[59];
	$("#ny_find_another").addClass('ny_close');
	$("#ny_find_another")[0].onclick = function() {
		hideOtherCenters();
	}
}

function hideOtherCenters() {
	$("#ny_other_centers").slideUp("slow",function(){$('#ny_container')[0].style.height = "212px";});
	$("#ny_find_another")[0].innerHTML = gsvars[34];
	$("#ny_find_another").removeClass('ny_close');
	$('#ny_zip_code_holder2').hide();
	$("#ny_find_another")[0].onclick = function() {
		showOtherCenters();
	}
}

function displayShadow(){
	$("#full_shadow")[0].style.height = $(document.body)[0].scrollHeight + "px";
	$("#full_shadow").fadeIn(1000);
}

function startDirections(centerID) {
	$.ajax({
		type: "POST",
		url: "/ajax/getonebuscenter",
		data:"bus_center_id="+centerID,
		dataType: 'json',
		async: false,
		complete: function(data){
			var map_width = 400;
			var map_height = 300;
			//set global variable to pick up when sending an email.
			bus_centerID = centerID;
			var	bus_centers = jQuery.parseJSON(data.responseText);
			var latlng = new google.maps.LatLng(bus_centers[0].latitude, bus_centers[0].longitude);

			var inner_width = $(window).width() + $(window).scrollLeft();
			var inner_height = $(window).height() + $(window).scrollTop();


			if ($("#directions_input_container")[0]) {
				$(document.body)[0].removeChild($("#directions_input_container")[0]);
			}
			var directions_input_container = document.createElement("div");
			directions_input_container.id = "directions_input_container";
			directions_input_container.style.left = ((inner_width/2) - (map_width/2))+"px";
			directions_input_container.style.top = ((inner_height/2) - (map_height/2))+"px";
			
			var directions_input_title = document.createElement("div");
			directions_input_title.id = "directions_input_title";
			directions_input_title.innerHTML = "Map &amp; Directions";
			
			var directions_print_img = document.createElement("div");
			directions_print_img.id = "directions_print_img";
			directions_print_img.onclick = function() {
				printDirections();
			}
			
			var directions_email_img = document.createElement("div");
			directions_email_img.id = "directions_email_img";
			directions_email_img.onclick = function() {
				emailMap("directions");
			}
			
			var directions_input_middle_text = document.createElement("div");
			directions_input_middle_text.innerHTML = "Get directions from:";
			
			var directions_input_input = document.createElement("input");
			directions_input_input.id = "directions_input";
			directions_input_input.type = "text";
			
			var directions_input_submit = document.createElement("div");
			directions_input_submit.id = "directions_input_submit";
			directions_input_submit.innerHTML = "Submit";
			directions_input_submit.onclick = function() {
				getDirections(latlng);
			}
			
			var directions_input_close = document.createElement("div");
			directions_input_close.id = "directions_input_close";
			directions_input_close.innerHTML = gsvars[59];
			directions_input_close.onclick = function() {
				$("#full_shadow").fadeOut(1000);
				$("#directions_input_container").fadeOut(1000, function() {
					$(document.body)[0].removeChild(directions_input_container);	
				});
				
			}

			var directions_input_map2_canvas = document.createElement("div");
			directions_input_map2_canvas.id = "map2_canvas";
			directions_input_map2_canvas.style.width = map_width+"px";
			directions_input_map2_canvas.style.height = map_height+"px";

			var directions_panel = document.createElement("div");
			directions_panel.id = "directions_panel";
			
			$(document.body)[0].appendChild(directions_input_container);
			directions_input_container.appendChild(directions_print_img);
			directions_input_container.appendChild(directions_email_img);
			directions_input_container.appendChild(directions_input_title);
			directions_input_container.appendChild(directions_input_middle_text);
			directions_input_container.appendChild(directions_input_input);
			directions_input_container.appendChild(directions_input_submit);
			directions_input_container.appendChild(directions_input_close);
			directions_input_container.appendChild(directions_input_map2_canvas);
			directions_input_container.appendChild(directions_panel);

			displayShadow();
			$("#directions_input_container").fadeIn(1000);
			
			var infowindow_text = "<div class='bus_info_container'>";
			infowindow_text += "<div class='bus_center_title'>AlphaGraphics "+bus_centers[0].centerWebName+"</div>";
			infowindow_text += "<div class='bus_center_text'>"+bus_centers[0].address1 + ", " + bus_centers[0].address2+"</div>";
			infowindow_text += "<div class='bus_center_text'>"+bus_centers[0].city + ", " + bus_centers[0].abbreviation + " " + bus_centers[0].zip+"</div>";
			infowindow_text += "<div class='bus_center_email'><a href='mailto:"+bus_centers[0].websiteEmail+"'>Click here to email us</a></div>";
			infowindow_text += "<div class='bus_center_directions' onclick='startDirections("+bus_centers[0].centerID+");'>Directions</div>";
			infowindow_text += "</div>";
			
			myOptions.zoom = 10;
			map2 = new google.maps.Map($("#map2_canvas")[0], myOptions);
			map2.setCenter(latlng);
			
			marker = new google.maps.Marker({
				position: latlng,
				map: map2
			});
			marker.setMap(map2);

			marker.infowindow = infowindow_text;
			var infowindow = new google.maps.InfoWindow();
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.setContent(this.infowindow);
				infowindow.open(map2,this);
			});

			directionsDisplay = new google.maps.DirectionsRenderer();
			directionsDisplay.setMap(map2);
			directionsDisplay.setPanel($("#directions_panel")[0]);
		}
	});
}
function getDirections(latlng) {
	var origin = $("#directions_input")[0].value;
	if (origin == "") {
		return;
	}
	
	var map_loc = findPos($("#directions_input_container")[0]);
	var mheight = getStyle($("#directions_input_container")[0], "height");
	mheight = Number(mheight.substr(0, mheight.length - 2));
	var dp_top = map_loc.top + mheight + 35;
	var dp_width = $("#directions_input_container")[0].width;
	var request = {
		origin:origin,
		destination:latlng,
		travelMode: google.maps.TravelMode.DRIVING
	}

	directionsService.route(request, function(result, status){
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(result);
			$("#directions_panel").fadeIn(1000);
		}
	});
}
function emailMap(which) {
	var container_width = 500;
	var container_height = 300

	var inner_width = $(window).width() + $(window).scrollLeft();
	var inner_height = $(window).height() + $(window).scrollTop();

	var email_container = document.createElement("div");
	email_container.id = "email_container";
	email_container.style.left = (inner_width/2) - (container_width/2);
	email_container.style.top = (inner_height/2) - (container_height/2);
	email_container.style.width = container_width+"px";
	email_container.style.height = container_height+"px";
	
	var email_title = document.createElement("div");
	email_title.id = "email_title";
	email_title.innerHTML = "E-mail this page from AlphaGraphics";

	var email_content = document.createElement("div");
	email_content.id = "email_content";
	email_content.innerHTML = "Please fill out the form below to e-mail this page to a friend. Separate multiple e-mail addresses with commas (,).";
	
	var email_to_text = document.createElement("div");
	email_to_text.id = "email_to_text";
	email_to_text.innerHTML = "To:";
	
	var email_to_input = document.createElement("input");
	email_to_input.type = "text";
	email_to_input.id = "to_addresses";
	
	var email_subject_text = document.createElement("div");
	email_subject_text.className = "email_input_text";
	email_subject_text.innerHTML = "Subject:";
	
	var email_subject_input = document.createElement("input");
	email_subject_input.id = "subject_line";
	email_subject_input.className = "email_input_box";
	email_subject_input.value = "Information About Alphagraphics";
	
	var email_from_text = document.createElement("div");
	email_from_text.className = "email_input_text";
	email_from_text.innerHTML = "Your e-mail address:";
	
	var email_from_input = document.createElement("input");
	email_from_input.id = "from_address";
	email_from_input.className = "email_input_box";
	
	var email_message_text = document.createElement("div");
	email_message_text.innerHTML = "Message:";
	email_message_text.className = "email_input_text";
	
	var email_message_input = document.createElement("textarea");
	email_message_input.id = "message";
	email_message_input.className = "email_message_input";

	var cancel_button = document.createElement("input");
	cancel_button.id = "cancel_button";
	cancel_button.type = "button";
	cancel_button.value = "Cancel";
	cancel_button.onclick = function() {
		$("#email_container").fadeOut(1000, function() {
			$(document.body)[0].removeChild(email_container);	
		});
		if ($("#directions_input_container")[0]) {
			$("#directions_input_container").fadeOut(1000, function() {
				$(document.body)[0].removeChild($("#directions_input_container")[0]);	
			});
		}
		$("#full_shadow").fadeOut(1000);
	}
	
	var submit_button = document.createElement("input");
	submit_button.id = "submit_button";
	submit_button.type = "button";
	submit_button.value = "Submit";
	submit_button.onclick = function() {
		if (validateForm()) {
			sendMapEmail(which);	
			$("#email_container").fadeOut(1000, function() {
				$(document.body)[0].removeChild(email_container);	
			});
			if ($("#directions_input_container")[0]) {
				$("#directions_input_container").fadeOut(1000, function() {
					$(document.body)[0].removeChild($("#directions_input_container")[0]);	
				})
					
			}
			$("#full_shadow").fadeOut(1000);
		}
	}
	
	$("#email_container").fadeIn(1000);
	displayShadow();
	
	email_container.appendChild(email_title);
	email_container.appendChild(email_content);
	email_container.appendChild(email_to_text);
	email_container.appendChild(email_to_input);
	email_container.appendChild(email_subject_text);
	email_container.appendChild(email_subject_input);
	email_container.appendChild(email_from_text);
	email_container.appendChild(email_from_input);
	email_container.appendChild(email_message_text);
	email_container.appendChild(email_message_input);
	email_container.appendChild(cancel_button);
	email_container.appendChild(submit_button);
	$(document.body)[0].appendChild(email_container);
	
}

function validateForm() {
	var emailRegEx = new RegExp("^[\\w\\.-]+@(?:[\\w-]+\\.)*[\\w-]+\\.\\w+$");
	
	if ($("#to_addresses")[0].value == "") {
		alert("Please enter the recipient's email address.");
		return false;
	}

	var to_addresses = $("#to_addresses")[0].value.split(",");
	for (var i in to_addresses) {
		to_addresses[i] = $.trim(to_addresses[i]);
		
		if (emailRegEx.test(to_addresses[i]) === false) {
			alert("You have entered an invalid recipient email address");
			return false;
		}
	}
	
	if ($("#from_address")[0].value == "") {
		alert("Please enter your email address.");
		return false;
	}
	
	if (emailRegEx.test($("#from_address")[0].value) === false) {
		alert("You have entered an invalid From address");
		return false;
	}
	
	return true;
}

function sendMapEmail(which) {
	var to_addresses = $("#to_addresses")[0].value.split(",");
	var tos = "";
	for (var i in to_addresses){
		if (tos != "") {
			tos += ",";
		}
		tos += $.trim(to_addresses[i]);
	}
	var email_subject = $("#subject_line")[0].value;
	var from_address = $("#from_address")[0].value
	var data;
	var message = $("#message")[0].value;
	if (which == "webregion") {
		data = state_code;
	} else if (which == "find") {
		if (email_type == "zip") {
			data = $("#zip_code")[0].value + "|" + $("#search_miles")[0].value;
		} else if (email_type == "city") {
			data = $("#city")[0].value + "|" + $("#state")[0].value;
		} else if (email_type == "country") {
			data = country;
		} else {
			data = email_lat+"|"+email_lng;
		}
	} else if (which == "directions") {
		if (email_type == "zip") {
			data = "zip|"+email_lat+"|"+email_lng+"|"+bus_centerID+"|"+$("#zip_code")[0].value + "|" + $("#search_miles")[0].value;
		} else if (email_type == "city") {
			data = "city|"+email_lat+"|"+email_lng+"|"+bus_centerID+"|"+$("#city")[0].value + "|" + $("#state")[0].value;
		} else if (email_type == "country") {
			data = "country|"+email_lat+"|"+email_lng+"|"+bus_centerID+"|"+country;
		} else if (email_type == "state") {
			data = "state|"+email_lat+"|"+email_lng+"|"+bus_centerID+"|"+state_code;
		} else {
			data = "latlng|"+email_lat+"|"+email_lng+"|"+bus_centerID;
		}
	}
	$.ajax({
		type: "POST",
		url: "/ajax/sendmapemail",
		data:"to_addresses="+tos+"&email_subject="+email_subject+"&from_address="+from_address+"&message="+message+"&which="+which+"&email_type="+email_type+"&data="+data,
		dataType: 'json',
		complete: function(data){
			if (data) {
				//alert(data.responseText);
			}
		}
	});
}
function printLocations() {
	window.print();
}

function printDirections() {
	$("#directions_input_container").printElement();
}

