
//change the event handler based on if we're testing or not.
onerror = devError;
function devError(errorMessage, url, line)
{
   msg = "There was an error on this page.\n\n";
   msg += "An internal programming error may keep\n";
   msg += "this page from displaying properly.\n";
   msg += "Click OK to continue.\n\n";
   msg += "Error message: " + errorMessage + "\n";
   msg += "URL: " + url + "\n";
   msg += "Line #: " + line;
   alert(msg);
   return true;
}

function publicError(errorMessage, url, line){
	msg = "There was an error on this page";
	msg += "that may keep\n";
	msg += "this page from displaying properly.\n";
	msg += "Click OK to continue viewing this page,\n\n";
	alert(msg);
	return true;
}

function mapError(mapEvent){
	msg = "Map Error\n";
	msg += mapEvent.error+"\n";
	msg += mapEvent.eventName+"\n";
	alert(msg);
	return true;
}

function drawPath(pathObj){
	var polyline = new VEShape(VEShapeType.Polyline, pathObj.arrayPts);
	polyline.SetLineColor(pathObj.color);
	polyline.SetLineWidth(pathObj.width);
	polyline.SetTitle(pathObj.title);
	polyline.SetDescription(pathObj.desc);
	pathObj.layer.AddShape(polyline);
}

function addCCPin(){
	if(!ccPin){
		addPin(ccLoc, 'Career Center Office', 
			'The Stony Brook Career Center is located on\
			the ground floor of Melville Library, room W-0550');	//Career Center
		ccPin = true;
	}
	//TODO
	//Career Center Icon (zebra stripes?)
}

function addPin(vell, title, desc){			
	var shape = new VEShape(VEShapeType.Pushpin, vell);
	shape.SetTitle(title);
	shape.SetDescription(desc);
	//pinid++;
	intPtsLayer.AddShape(shape); 
}

 function DoCenterZoom(vell, zoom){         
	map.SetCenterAndZoom(vell, zoom);
}

 function convertStartLoc(start){
 	if(start == "sblirr"){
 		return "lirr";
 	}
 	else if (start == "genlirr"){
 		genlirr = true;
 		return "lirr";
 	}
 	else if(start == "ferrybpct"){
 		return "Bridgeport, CT";
 	}
 	else if(start == "ferrynlct"){
 		return "New London, CT";
 	}
 	else if(start == "nyc"){
 		//alert("in nyc");
 		return "New York City, NY";
 	}
 	else if(start == "orientpt"){
 		return "Orient Point, NY";
 	}
 	else if(start == "portjeff"){
 		return "Port Jefferson, NY";
 	}
 	else if(start == "jfk"){
 		return "JFK";
 	}
 	else if(start == "lga"){
 		return "LGA";
 	}
 	else if(start == "macarthur"){
 		return "ISP";
 	}
 }
 
 function startToStr(str){
	if(str == "lirr")
		return "LIRR station";
	else
		return str;
 }
 
 function endToStr(str){
	if(str == "cc")
		return "Career Center Office";
	if(str == "sac")
		return "Student Activities Center (SAC)";
	if(str == "javits")
		return "Javits Lecture Hall";
	if(str == "wang")
		return "Wang Center";
	if(str == "union")
		return "Stony Brook Union";
 } 
 
 function ClearAll()
 {
    map.DeleteRoute();
    SetDirections("");
    map.LoadMap(ccLoc, 8);
 }
 
 function deleteRoute(){
	try{
		map.DeleteRoute();
	}catch (err){
		alert(err.message);            
	}
}

function displayCustomDir(){
	drawPath(lineToSacPark);				
	drawPath(lineWalkDir);
 }
 
 
 function addPinCenter(){
	var center = map.GetCenter();
    var pin = map.AddPushpin(center);
    pin.SetTitle('Center pin');
    pin.SetDescription(center.Latitude+', '+center.Longitude);
	lastPin = pin;
 }
 
 function remLastPin(){
	if(lastPin != null)         
	{            
		map.DeleteShape(lastPin);            
		lastPin = null;         
	}
 }
 
 function turnStr(numTurns){
	return "<b>"+numTurns+".</b> ";
}
 
function mapPan(lat, lon){
	map.Pan(lat, lon);
	return false;	
}

 function storeForm(){
	dirState = document.getElementById("directions").innerHTML; 
 }
 
function viewForm(){
	SetDirections(dirState);
}

function disableLirrBusOption(){	
	var d = document.getElementById("lirrbusstate");
	lirrbus = false;
	document.dirForm.lirrbus.checked = false;
	document.dirForm.lirrbus.disabled = true;
	d.innerHTML = "<span style=\"color:silver\"> I want to take the bus from \
			the LIRR station. (disabled)</span>";
}

function enableLirrBusOption(){
	var d = document.getElementById("lirrbusstate");
	document.dirForm.lirrbus.disabled = false;
	d.innerHTML = " I want to take the bus from \
			the LIRR station.";
}

function disableRouteOpt(){
	resetOptType();
	var d = document.getElementById("routeoptld");
	document.dirForm.routeopt.disabled = true;
	d.innerHTML = "Route Optimization (disabled)";
}

function enableRouteOpt(){
	var d = document.getElementById("routeoptld");
	document.dirForm.routeopt.disabled = false;
	d.innerHTML = "Route Optimization";
}

function resetOptType(){
	for(var i = 0; i < document.dirForm.opttype.length; i++){
			if(i == 0)
				document.dirForm.opttype[i].checked = true;
			else if(i == 1)
				document.dirForm.opttype[i].checked = false;
			else if(i == 2)
				document.dirForm.opttype[i].checked = false;
			else
				alert("need to add values to toggleopptype code. unknown index for opttype.");
	}
	//alert("opttype: "+opttype);
}

function remjsmsg(){
	document.getElementById("enablejs").innerHTML = "";
}

function geturlparam(name){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null )    
		return "";  
	else    
		return results[1];
}

function getBusMapLink(linkTxt){
	var link = "<a href=\"http://www.stonybrook.edu/transportation/campusbus/bus_map.shtml\">";
	link += linkTxt;
	link += "</a>";
	return link;
}