// Global Variables
var map = ""; // Initialize to string for printing function
var mobile_redirect = "http://m.bart.gov";

// Check for empty searches
function validateSearch()
{
    if ( document.search_form.sp_q.value.match( /^\s*$/ ) != null )
    {
        alert( "Please enter a search phrase" );
        return false;
    }
    document.getElementById('site-search').submit();	
    return true;
}

// Standard print routine
function doPrint()
{
    window.print();
}

// Add trim function to strings
String.prototype.trim = function () {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function clearForm() {
	theForm = document.contact;
	for( var i=0; i<theForm.elements.length; i++ ) {
		if (theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea") {
			theForm.elements[i].value = "";
		}
	}
}

function submitForm() {
	var flag = true;
	var alertMsg = "";

	if ( document.contact.category && document.contact.category.value == "-1" ) {
		flag = false;
		alertMsg += "You must select a comment category.\n";
	}
	if ( document.contact.firstname.value == "" || document.contact.firstname.value == null) {
		flag = false;
		alertMsg += "First Name is a required field.\n";
	}
	if ( document.contact.contact.value == "" || document.contact.contact.value == null) {
		flag = false;
		alertMsg += "Email is a required field.\n";
	}
	if ( document.contact.comment.value == "" || document.contact.comment.value == null) {
		flag = false;
		alertMsg += "Comment is a required field.\n";
	}
	tmp = document.contact.comment.value;
	if ( tmp.indexOf("<") >= 0 || tmp.indexOf(">") >= 0 ) {
		flag = false;
		alertMsg += "Invalid characters in form. Please do not use '<' or '>' characters.";
	}
	// Submit if there are no errors
	if ( flag ) {
		document.contact.submit();
	} else {
		alert(alertMsg);
	}
}

function submitEventForm() {
	var flag = true;
	var alertMsg = "";

	if ( document.event.name.value == "" || document.event.name.value == null) {
		flag = false;
		alertMsg = "Name is a required field.\n";
	}
	if ( document.event.email.value == "" || document.event.email.value == null) {
		flag = false;
		alertMsg += "Email is a required field.\n";
	}
	if ( document.event.event_title.value == "" || document.event.event_title.value == null) {
		flag = false;
		alertMsg += "Event Title is a required field.\n";
	}
	tmp = document.event.event_desc.value;
	if ( tmp == "" || tmp == null ) {
		flag = false;
		alertMsg += "Event Description is a required field.\n";
	} else 	if ( tmp.indexOf("<") >= 0 || tmp.indexOf(">") >= 0 ) {
		flag = false;
		alertMsg += "Invalid characters in the event description. Please do not use '<' or '>' characters.";
	}
	if ( document.event.event_start.value == "" || document.event.event_start.value == null) {
		flag = false;
		alertMsg += "Start Date is a required field.\n";
	}
	if ( document.event.event_end.value == "" || document.event.event_end.value == null) {
		flag = false;
		alertMsg += "End Date is a required field.\n";
	}
	if ( document.event.station_id.value == "" || document.event.station_id.value == null) {
		flag = false;
		alertMsg += "Closest BART Station is a required field.\n";
	}
	tmp = document.event.event_get_there.value;
	if ( tmp == "" || tmp == null ) {
		flag = false;
		alertMsg += "Getting There is a required field.\n";
	} else 	if ( tmp.indexOf("<") >= 0 || tmp.indexOf(">") >= 0 ) {
		flag = false;
		alertMsg += "Invalid characters in the getting there field. Please do not use '<' or '>' characters.";
	}
	// Submit if there are no errors
	if ( flag ) {
		document.event.submit();
	} else {
		alert(alertMsg);
	}
}

function submitNDAPrimeForm() {
	document.ndaform.submit();
}

function submitNDASubForm() {
	document.ndaform.submit();
}

// Correcly handle external links in HTML 4.01
// Created by Robert Falconer 2/21/2008
function externalLinks() { 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank"; 
	} 
}

rad = function(x) {return x*Math.PI/180;} 
 
distHaversine = function(p1, p2) { 
  var R = 3959; // earth's mean radius in km 
  var dLat  = rad(p2.lat() - p1.lat());
  var dLong = rad(p2.lng() - p1.lng()); 

  var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
          Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) * Math.sin(dLong/2) * Math.sin(dLong/2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; 
 
  return d.toFixed(3); 
}

window.onload = externalLinks;
