DrivingDirection = {};

var arrLocations;
DrivingDirection.GetDirections = function(qs)
{
    var sel = document.getElementById('selLocations');
    if (sel)
    { 
        arrLocations = new Array();
        var ctr = 0;
        var startLoc = GetLocationValue('Start');
        
        if (startLoc != 'INVALID')
        {
            if (startLoc != '')
            {
                arrLocations[0] = startLoc;
                ctr = 1;
            }
            for(i=0; i<=sel.length-1; i++)
            {
                arrLocations[ctr] = sel.options[i].value.replace("#", "").replace("&", "");
                ctr++;
            }        
            if (arrLocations.length > 1)
                DrivingDirection.PrintDirections(qs);
            else
                alert('Enter more than one location to get directions');
        }
    }
}

DrivingDirection.ClearLocations = function()
{
    ClearLocationValue('Start');
    document.getElementById('selLocations').options.length = 0;
}
DrivingDirection.ClearDirections = function()
{
    veMap.DeleteRoute();
    document.getElementById('divRoute').innerHTML = "";
    hide('routeDisplay');
}
DrivingDirection.SetDirections = function(routeInfo)
{
    display('routeDisplay');
	var divDisplay = document.getElementById('divRoute');
    var divPrint = document.getElementById('divPrintRoute');
    
    if (divPrint)
        divPrint.innerHTML = routeInfo;  
    else if (divDisplay)
        divDisplay.innerHTML = routeInfo; 
}
DrivingDirection.PrintDirections = function(qs)
{
    //if (document.getElementById('divRoute').style.display != 'none')
    //{
        var url = '/Search/PrintDrivingDirections.aspx';
        if (qs != '' && qs != undefined)
            url += '?' + qs; 
        popup(url, 'PrintDirections', 
            'width=600,height=600,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
    //}
    //else
    //    alert('No directions to print. Please select locations and click "Get Directions" button.');
}
DrivingDirection.AddLocationToList = function(address)
{
    Search.toggleTabs('dd'); // switch to Driving Direction UI
    var sel = document.getElementById('selLocations');
    if (sel)
    {
        var len = sel.length;
        if (len >= 20)
            alert("We only support Driving Directions for 20 locations");
        address = address.replace("  ", " ");
        if (!CheckExists(sel, address))
        {
            var opt = new Option(getAlphabet(len) + ": " + address, address)
            sel.options[len] = opt;        
        }
    }
}
DrivingDirection.RemoveLocationFromList = function(address)
{
    var sel = document.getElementById('selLocations');
    for(i=sel.length-1; i>=0; i--)
    {
        if(sel.options[i].selected)
        {
            sel.options[i] = null; 
        }
    }
    reNumberItems(sel);
}
DrivingDirection.AddLocationFromTextBox = function()
{
    var addLoc = GetLocationValue('Add');
    
    if (addLoc != 'INVALID')
    {
        if (addLoc == '')
            alert('Please Enter Location to Add');
        else
        {
            DrivingDirection.AddLocationToList(addLoc);
            ClearLocationValue('Add');
            hide('divAddLocation');
        }   
    }
}
DrivingDirection.AddLocationPins = function()
{
    var sel = document.getElementById('selLocations');
    if (sel)
    {
        for(i=0;i<sel.length;i++)
        {
            pushpin.AddLocationPin(sel.options[i].text);
        }
    }
}

DrivingDirection.MoveUpList = function() 
{
    var sel = document.getElementById('selLocations');
   if ( sel.length == -1) {  // If the list is empty
      alert("There are no locations which can be moved!");
   } else {
      var selected = sel.selectedIndex;
      if (selected == -1) {
         alert("You must select a location to be moved!");
      } else {  // Something is selected 
         if ( sel.length == 0 ) {  // If there's only one in the list
            //alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == 0 ) {
               //alert("The first entry in the list cannot be moved up.");
            } else {
               // Get the text/value of the one directly above the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = sel[selected-1].text;
               var moveText2 = sel[selected].text;
               var moveValue1 = sel[selected-1].value;
               var moveValue2 = sel[selected].value;
               sel[selected].text = moveText1;
               sel[selected].value = moveValue1;
               sel[selected-1].text = moveText2;
               sel[selected-1].value = moveValue2;
               sel.selectedIndex = selected-1; // Select the one that was selected before
               reNumberItems(sel);
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}
DrivingDirection.MoveDownList = function() 
{
    var sel = document.getElementById('selLocations');
   if ( sel.length == -1) {  // If the list is empty
      alert("There are no locations which can be moved!");
   } else {
      var selected = sel.selectedIndex;
      if (selected == -1) {
         alert("You must select a location to be moved!");
      } else {  // Something is selected 
         if ( sel.length == 0 ) {  // If there's only one in the list
            //alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == sel.length-1 ) {
               //alert("The last entry in the list cannot be moved down.");
            } else {
               // Get the text/value of the one directly below the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = sel[selected+1].text;
               var moveText2 = sel[selected].text;
               var moveValue1 = sel[selected+1].value;
               var moveValue2 = sel[selected].value;
               sel[selected].text = moveText1;
               sel[selected].value = moveValue1;
               sel[selected+1].text = moveText2;
               sel[selected+1].value = moveValue2;
               sel.selectedIndex = selected+1; // Select the one that was selected before
               reNumberItems(sel);
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function reNumberItems(sel)
{
    var opt;
    for(i=sel.length-1; i>=0; i--)
    {
        opt = sel.options[i].text;
        sel.options[i].text = opt.replace(opt.substring(0,opt.indexOf(": ")), getAlphabet(i))
    }
}
function CheckExists(sel, address)
{
    var exists = false;
    for(i=sel.length-1; i>=0; i--)
    {
               
        if(sel.options[i].value == address)
        {
             exists = true;
             break;  
        }
    }
    if (exists)
        alert('Location already in the Selected Location list.');
    return exists;
}
function removeSpaces(string) 
{
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

function GetLocationValue(loc)
{
    var value = '';
    
    var pre = 'txt' + loc;
    
    value += GetLocationField(pre + 'Street');
    value += GetLocationField(pre + 'City');
    value += GetLocationField(pre + 'State');
    value += GetLocationField(pre + 'Zip');

    if (value != '')
    {
        if (!ValidateLocation(loc))
            return 'INVALID';
    }
    return value;
}
function ClearLocationValue(loc)
{
    var pre = 'txt' + loc;
    ClearLocationField(pre + 'Street');
    ClearLocationField(pre + 'City');
    ClearLocationField(pre + 'State');
    ClearLocationField(pre + 'Zip');    
}
function ClearLocationField(id)
{
    SetTxtValue(id,'');
    FieldHelpText(document.getElementById(id));
}
function ValidateLocation(loc)
{
    var pre = 'txt' + loc;
    var pass = false;
    if ((GetTxtValue(pre + 'City') != '' && GetTxtValue(pre + 'City') != document.getElementById(pre + 'City').defaultValue) 
            && (GetTxtValue(pre + 'State') != '' && GetTxtValue(pre + 'State') != document.getElementById(pre + 'State').defaultValue)) 
            pass = true;
    if ((GetTxtValue(pre + 'Zip') != '' && GetTxtValue(pre + 'Zip') != document.getElementById(pre + 'Zip').defaultValue))
        pass = true;
        
    if (!pass)
        alert('City State or Zip is required for ' + loc + ' Location');

    return pass;
}
function GetLocationField(fieldID)
{
    if (GetTxtValue(fieldID) != document.getElementById(fieldID).defaultValue)
        return GetTxtValue(fieldID) + ' ';
    else 
        return '';
}
