// Constant Settings

var MAP_WIDTH_DEFAULT = 325;
var MAP_WIDTH_SMALL = 325;

var MAP_WINDOW_WIDTH_ADDITION = 220;
var MAP_WINDOW_HEIGHT_ADDITION = 230;

var METER_PER_FEET = 0.3048;
var KM_PER_MILE = 1.609344;
var NM_PER_MILE = 0.868976242;

var POST_URLS = true;

var OpenWins = new Array();
var OpenWinCount = 0;

var ie = false;
var ns4 = false;
var ns6 = false;

if (document.all)
  ie = true;
else if (document.layers)
  ns4 = true;
else if(document.getElementById)
  ns6 = true;
  
///////////////////////////////////////////////////////////
// StringValue Class: For returning string values
function ReturnValue()
{
  this.value = null;
}

///////////////////////////////////////////////////////////
// Common functions

function setPointer(s)
{
  document.body.style.cursor = s; // set mouse pointer
}

function getelement(id)
{
  var el = null;
  var elements = null;

  el = document.all ? document.all[id] : document.getElementById(id);  // for both
  if( typeof(el) == 'undefined' ) el = null;

  if( (el == null) && (document.getElementByName) )  // for IE (IE5.5 and earlier?)
  {
    el = document.getElementByName(id);
    if( typeof(el) == 'undefined' ) el = null;
  }
  
  if( (el == null) && (document.getElementsByName) )// for Mozilla
  {
    elements = document.getElementsByName(id); // returns and array
    if( elements && (elements.length > 0) )    // use 0th element for el
      el = elements[0];
  }

  return el;
}

function getelementex(id, doc)
{
  var el = null;
  var elements = null;

  if( !doc )
    doc = document;

  el = doc.all ? doc.all[id] : doc.getElementById(id);  // for both
  if( typeof(el) == 'undefined' ) el = null;

  if( (el == null) && (doc.getElementByName) )  // for IE (IE5.5 and earlier?)
  {
    el = doc.getElementByName(id);
    if( typeof(el) == 'undefined' ) el = null;
  }
  
  if( (el == null) && (doc.getElementsByName) )// for Mozilla
  {
    elements = doc.getElementsByName(id);      // returns and array
    if( elements && (elements.length > 0) )    // use 0th element for el
      el = elements[0];
  }

  return el;
}


function GetURLParam(ParamName) 
{
  var sURL = window.document.URL.toString();
  var sResult = "";
  var iStart = 0;
  var iEnd = 0;

  if ( (iStart = sURL.indexOf(ParamName + "=")) > 0 )
  {
    iStart += ParamName.length + 1;
    if( (iEnd = sURL.indexOf("&", iStart)) > 0 )
      sResult = sURL.substring(iStart, iEnd);
    else
      sResult = sURL.substring(iStart);
  }

  return sResult;
}

function showhide(id, show) 
{
  var el = getelement(id);
  if( (el == null) || (el.style == null) )
    return false;

  if( el )
  {
    if (show)
      el.style.display = 'inline';
    else 
      el.style.display = 'none'; 

    return true;
  }

  return false;
}

function altshowhide(id) 
{
  var el = getelement(id);
  if( (el == null) || (el.style == null) )
    return false;
  
  if( el )
  {
    if ( (el.style.display == 'inline') || (el.style.display == 'visible') )
      el.style.display = 'none';
    else 
      el.style.display = 'inline'; 
  
    return true;
  }

  return false;
}

function getOffsetLeft (el) 
{
  var ol = el.offsetLeft;
  while ((el = el.offsetParent) != null)
    ol += el.offsetLeft;
 
 return ol;
}

function getOffsetTop (el) 
{
  var ot = el.offsetTop;
  while((el = el.offsetParent) != null)
    ot += el.offsetTop;

  return ot;
}

function Open(page, name, width, height)
{
  var winl;
  var wint;
  var win = null;

  if( width && height )
  {
    if(width < screen.width)
      winl = (screen.width - width) / 2;
    else
      winl = 10;

    if(width < screen.height)
      wint = (screen.height - height) / 2;
    else
      wint = 10;

    win = OpenWins[OpenWinCount++] = this.open(page, name, 'width=' + width + ',height=' + height + ',top=' + wint + ',left=' + winl +
                                               ',toolbar=no,menubar=no,location=no,status=yes,scrollbars=yes,resizable=yes');
  }
  else
    win = OpenWins[OpenWinCount++] = this.open(page, name);

  if( !win || (typeof(win) == "undefined") )
    alert(LOGON_DISABLE_POPUP_BLOCKER);
  else
    OpenWins[OpenWinCount++] = win;
}

function OpenHelp(helppath, bAdmin, bAccount)
{
  var width = 800, win = null;
  var height = 600
  var page = "";
  var CurPage = "";

  if(width < screen.width)
    winl = (screen.width - width) / 2;
  else
    winl = 10;

  if(width < screen.height)
    wint = (screen.height - height) / 2;
  else
    wint = 10;

  if( helppath && (helppath.length > 0) )
  {
    if( (typeof(bAdmin) != 'undefined') && bAdmin )
      page = HTMLPath + 'help/adminhelp/index.htm?Page='+helppath;
    else
      page = HTMLPath + 'help/index.htm?Page='+helppath;
  }
  else
  {
    if( (typeof(bAdmin) != 'undefined') && bAdmin )
    {
      if( CurrentTab == '' || CurrentTab == 'Debug' )
        CurPage = '?Page=Debug/Index.htm';
      else if( CurrentTab == 'User' )
        CurPage = '?Page=Users/Index.htm';
      else if( CurrentTab == 'Server' )
        CurPage = '?Page=Servers/Index.htm';
      else if( CurrentTab == 'Stat' )
        CurPage = '?Page=Statistics/Index.htm';

      page = HTMLPath + 'help/adminhelp/index.htm' + CurPage;
    }
    else
    {
      if( (typeof(bAccount) == 'undefined') || !bAccount )
      {
        if( CurrentTab == '' || CurrentTab == 'Vehicle' )
          CurPage = '?Page=VehicleTracking/Index.htm';
        else if( CurrentTab == 'Location' )
          CurPage = '?Page=CreateLocation.htm';
        else if( CurrentTab == 'Report' )
          CurPage = '?Page=Reports/Index.htm';
      }
      else
      {
        if( CurrentTab == '' || CurrentTab == 'User' )
          CurPage = '?Page=MySettings/UserSettings/Index.htm';
        else if( CurrentTab == 'Contact' )
          CurPage = '?Page=MySettings/ContactSettings/Index.htm';
        else if( CurrentTab == 'Display' )
          CurPage = '?Page=MySettings/DisplaySettings/Index.htm';
        else if( CurrentTab == 'Vehicle' )
          CurPage = '?Page=MySettings/VehicleSettings/Index.htm';
        else if( CurrentTab == 'Location' )
          CurPage = '?Page=MySettings/LocationSettings/Index.htm';
        else if( CurrentTab == 'Trigger' )
          CurPage = '?Page=MySettings/UserNotifications/Index.htm';
      }

      page = HTMLPath + 'help/index.htm' + CurPage;
    }
  }

  win = this.open(page, 'Help', 'width=' + width + ',height=' + height + ',top=' + wint + ',left=' + winl +
                  'location=yes,toolbar=yes,menubar=yes,status=yes,scrollbars=yes,resizable=yes');

  if( !win || (typeof(win) == "undefined") )
    alert(LOGON_DISABLE_POPUP_BLOCKER);
}

function OpenFixed(page, name, width, height)
{
  var winl, win = null;
  var wint;

  if(width < screen.width)
    winl = (screen.width - width) / 2;
  else
    winl = 10;

  if(width < screen.height)
    wint = (screen.height - height) / 2;
  else
    wint = 10;

  win = this.open(page, name, 'width=' + width + ',height=' + height + ',top=' + wint + ',left=' + winl +
                  ',toolbar=no,menubar=no,location=no,status=no,scrollbars=no,resizable=no');

  if( !win || (typeof(win) == "undefined") )
    alert(LOGON_DISABLE_POPUP_BLOCKER);
  else
    OpenWins[OpenWinCount++] = win;
}

function OpenReport(page, name)
{
  var winl, win = null;
  var wint;
  var width = 640;
  var height = 480;

  if(width < screen.width)
    winl = (screen.width - width) / 2;
  else
    winl = 10;

  if(width < screen.height)
    wint = (screen.height - height) / 2;
  else
    wint = 10;

  win = this.open(page, name, 'width=' + width + ',height=' + height + ',top=' + wint + ',left=' + winl +
                  'toolbar=yes,menubar=yes,location=no,status=yes,scrollbars=yes,resizable=yes');

  if( !win || (typeof(win) == "undefined") )
    alert(LOGON_DISABLE_POPUP_BLOCKER);
  else
    OpenWins[OpenWinCount++] = win;
}

function GetSelectedValue(selectbox)
{
  if( !selectbox || (selectbox.selectedIndex == -1) )
    return null;
  
  return selectbox.options[selectbox.selectedIndex].value;
}

function GetSelectedText(selectbox)
{
  if( !selectbox || (selectbox.selectedIndex == -1) )
    return null;
  
  return selectbox.options[selectbox.selectedIndex].text;
}

function GetSelectedValueFromText(selectbox, text)
{
  var i, iCount;

  if( !selectbox || (selectbox.selectedIndex == -1) || !text || (!text.length) )
    return null;

  iCount = selectbox.options.length;
  for(i = 0; i < iCount; i++)
  {
    if( selectbox.options[i].text == text )
      return selectbox.options[i].value
  }
  
  return null;
}

function GetCheckedValue(checkbox)
{ 
  var chkbox = checkbox;

  if( !chkbox )
    return null;

  if( typeof(chkbox) == 'string' )
    chkbox = getelement(chkbox);

  if( typeof(chkbox.length) == 'undefined' )
  {
    if(chkbox.checked)
      return chkbox.value;
  } 
  else
  {
    for (i = 0; i < chkbox.length; i++)
    {
      if(chkbox[i].checked)
        return chkbox[i].value;
    }
  }

  return null;
}

function setChoice(radio, value)
{
  var bSelectionMade = false;
  var i, iCount;
  
  if( !radio )
    return false;

  iCount = radio.length;
  for (i = 0; i < iCount; i++)
  {
    if (radio[i].value == value)
    {
      radio[i].checked = true;
      bSelectionMade = true;
    }
  }

  return bSelectionMade;
}

function SetSelectedValue(selectbox, value, bNoCase)
{
  if( !selectbox )
    return false;
    
  if( typeof(bNoCase) == "undefined" )
    var bNoCase = false;
    
  return SetValue(selectbox, value, bNoCase);
}

function SetValue(formfield, value, bNoCase)
{
  var LCValue;
  var i, iCount;
  
  if( !formfield )
    return false;

  if( typeof(bNoCase) == "undefined" )
    var bNoCase = false;

  if( bNoCase )
    LCValue = value.toLowerCase();
    
  if( formfield.options ) // is select box
  {
    iCount = formfield.options.length;
    for(i = 0; i < iCount; i++)
    {
      if( (!bNoCase && (formfield.options[i].value == value)) ||
          (bNoCase && (formfield.options[i].value.toLowerCase() == LCValue)) )
      {
        formfield.options[i].selected = true;
        return true;
      }
    }
  }
  else if( formfield.value ) // textbox, hidden, checkbox, etc.
  {
    formfield.value = value;
    return true;
  }
  
  return false;
}

function SetSelectedText(selectbox, text)
{
  var i, iCount;
  var sel;

  if( !selectbox )
    return false;

  sel = selectbox;
  if( typeof(sel) == 'string' )
    sel = getelement(sel);
    
  iCount = sel.options.length;
  for(i = 0; i < iCount; i++)
  {
    if( sel.options[i].text == text )
    {
      sel.options[i].selected = true;
      return true;
    }
  }
  return false;
}

function SetSelectionName(selectbox, value, name)
{
  var i, iCount;

  if( !selectbox || !name.length )
    return false;
    
  iCount = selectbox.options.length;
  for(i = 0; i < iCount; i++)
  {
    if( (selectbox.options[i].text == value) ||
        (selectbox.options[i].value == value) )
    {
      selectbox.options[i].text = name;
      return true;
    }
  }
  return false;
}

function AddSelections(selectbox, aSelections)
{
  var i, iCount;
  
  if( !selectbox || !aSelections || 
      (aSelections.length == 0) || (aSelections[0].length < 2) )
    return false;

  iCount = aSelections.length;

  for(i = 0; i < iCount; i++)
  {
    if( aSelections[i].length == 2 )
      selectbox.options[selectbox.options.length] = new Option(aSelections[i][1],aSelections[i][0]);
  }

  return true;
}

function RemoveSelection(selectbox, value)
{
  var i, iCount;

  if( !selectbox )
    return false;
  
  iCount = selectbox.options.length;
  for(i = 0; i < iCount; i++)
  {
    if( (selectbox.options[i].text == value) ||
        (selectbox.options[i].value == value) )
    {
      selectbox.options[i] = null;
      return true;
    }
  }
  return false;
}

function SetCheckedValue(checkbox, value)
{
  var i, iCount;

  if( !checkbox )
    return false;
    
  iCount = checkbox.length;
  for (i = 0; i < iCount; i++)
  {
    if(checkbox[i].value == value)
    {
      checkbox[i].checked = true;
      return true;
    }
  }
  return false;
}

/////////////////////////////////////////////////////////
// Input Validation Functions

function ValidateEmail(Email)
{ 
  var valid = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \"@.!#$%&'*+-/=?^_`{|}~";
  var length = Email.length;
  var bValid;
  var i, iAts = 0;
  
  if( self.OnCustomValidateEmail && OnCustomValidateEmail(window, Email, bValid) )
    return bValid;

  if( length == 0 )
  {
    alert(ACCT_INVALID_EMAIL);
    return false;
  }

  for(i = 0; i < length; i++)
  {
    if( valid.indexOf(Email.charAt(i)) == -1 )
    {
      alert(ACCT_INVALID_EMAIL);
      return false;
    }
    
    if( Email.charAt(i) == '@' )
      iAts++;
  }

  iAtIndex = Email.indexOf('@');
  if( iAtIndex != -1 )
    iDotIndex = Email.indexOf('.', iAtIndex);
  
  if( (iAts != 1) ||                    // there is more or less than one @
      (iAtIndex <= 0) ||                // no @ or no characters before @
      (iDotIndex == -1) ||              // no dot after @
      ((iDotIndex - iAtIndex) <= 1) ||  // no characters between @ and dot
      ((length - iDotIndex) <= 1) )     // no characters after dot
  {
    alert(ACCT_INVALID_EMAIL);
    return false;
  }

  return true;
}

function ValidatePhone(PhoneNumber, bCheckEmpty)
{ 
  var Result;
  var bValid;

  if( !bCheckEmpty && (PhoneNumber.length == 0) )
    return true;

  Result = PhoneNumber.match(/[1]{1}[-][0-9]{3}[-][0-9]{3}[-][0-9]{4}/);
  if( !Result )
    Result = PhoneNumber.match(/[0-9]{3}[-][0-9]{3}[-][0-9]{4}/);
  if( !Result )
    Result = PhoneNumber.match(/[0-9]{3}[-][0-9]{4}/);
    
  if( PhoneNumber != Result )
  {
    alert(ACCT_INVALID_PHONE);
    return false;
  }
  
  return true;
}

function ValidateMultipleEmails(EmailAddresses, bCheckEmpty)
{
  var i = 0, iCount;
  var Emails = EmailAddresses.split(';');

  if( EmailAddresses.length > 255 )
  {
    alert(ACCT_LONG_EMAIL);
    return false;
  }

  if( bCheckEmpty && EmailAddresses.length == 0 )
  {
    alert(ACCT_ENTER_EMAIL);
    return false;
  }
  else if( EmailAddresses.length == 0 )
    return true;

  iCount = Emails.length;
  if( iCount == 0 )
  {
    alert(ACCT_ENTER_EMAIL);
    return false;
  }

  for(i = 0; i < iCount; i++)
  {
    if( !ValidateEmail(Emails[i]) )
      return false;
  }

  return true;
}

function ValidateMultiplePhones(PhoneNumbers, bCheckEmpty)
{
  var i = 0;
  var Value = '';
  var Phones = PhoneNumbers.split(';');

  if( bCheckEmpty && PhoneNumbers.length == 0 )
  {
    alert(ACCT_ENTER_PHONE);
    return false;
  }
  else if( PhoneNumbers.length == 0 )
    return true;

  iCount = Phones.length;
  if( iCount == 0 )
  {
    alert(ACCT_ENTER_PHONE);
    return false;
  }

  for( i = 0; i < iCount; i++ )
  {
    if( !ValidatePhone(Phones[i]) )
      return false;
  }

  return true;
}

function OnlyNumbers(textbox)
{
  var accept = "1234567890";
  
  if( textbox.value.match(/[^0-9]/gi) )
  {
    textbox.value = textbox.value.replace(/[^0-9]/gi, '');
    alert('Please only enter numbers.');
    return false;
  }
}

function IsValid(name)
{ 
  var str = name;
  var valid="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ "
  var i, iCount;
  
  if(name.length == 0)
    return -1;

  iCount = str.length;
  for(i = 0; i < iCount; i++)
  {
    if ( valid.indexOf(name.charAt(i)) < 0 ) 
      return -2;
  }

  return 1;
}

function IsValidNumber(name)
{ 
  if ( text.match(/[^0-9]/gi) ) 
    return false;

  return true;
}

//////////////////////////////////////////////////////////

function isDefined(param) 
{
  if (param) 
    return true;
  else 
  {
    if ( param == false)
      return true
    else
      return false
  }
}

function ChangeClass(element, newclass)
{
  if (element == null)
    return false;

  element.className = newclass;
  return true;
}

function SelectDivLink(s)
{
  var div = getelement(s + 'Div');
  var link = getelement(s + 'Link');
 
  if( div )
    div.style.display = 'inline';
  if( link )
    ChangeClass(link,'SecMenuItemSel');
}

function UnselectDivLink(s)
{
  var div = getelement(s + 'Div');
  var link = getelement(s + 'Link');
  
  if( div )
    div.style.display = 'none';
  if( link )
    ChangeClass(link,'SecMenuItem');
}

function SingleMenuSelectDivLink(s)
{
  var div = getelement(s + 'Div');
  var link = getelement(s + 'Link');
 
  if( div )
    div.style.display = 'inline';
  if( link )
    ChangeClass(link,'SingleMenuItemSel');
}

function SingleMenuUnselectDivLink(s)
{
  var div = getelement(s + 'Div');
  var link = getelement(s + 'Link');
  
  if( div )
    div.style.display = 'none';
  if( link )
    ChangeClass(link,'SingleMenuItem');
}

function ShowSecMenu(s)
{
  var div = getelement(s);
  
  if(div)
    div.style.display = 'inline';
}

function HideSecMenu(s)
{
  var div = getelement(s);
  
  if(div)
    div.style.display = 'none';
}

function ReplaceValue(Incoming, StartTag, EndTag, ReplaceString) 
{
  var iStart, iEnd;
  var Temp;

  Temp = Incoming;

  iStart = Temp.indexOf(StartTag)
  if( iStart != -1 )
  {
    iStart += StartTag.length;
    if( EndTag.length > 0 )
    {
      iEnd = Temp.indexOf(EndTag, iStart)
      if( iEnd != -1 )
      {
        Incoming = Temp.substring(0, iStart);
        Incoming += ReplaceString;
        Incoming += Temp.substring(iEnd, Temp.length);
      }
    }
    else  // if EndTag empty, replace rest of string
    {
      Incoming = Temp.substring(0, iStart);
      Incoming += ReplaceString;
    }
  }

  return Incoming;
}

function WriteFooter()
{
  document.write(COMN_FOOTER);
}

function OnTriggerEventChanged(selectbox, textbox)
{
  var iSelIndex;
  var EventList = getelement(selectbox);
  var EventNum = getelement(textbox);

  if( !EventList || !EventNum )
    return false;
  
  if( (iSelIndex = EventList.selectedIndex) != -1 )
  {
    if( EventList.options[iSelIndex].value == "" )
    {
      EventNum.readOnly = false;
      return true;
    }

    EventNum.value = EventList.options[iSelIndex].value;
  }
  
  EventNum.readOnly = true;
  return true;
}

function Trim(string) 
{
  while( string.substring(0,1) == ' ' )
    string = string.substring(1, string.length);

  while( string.substring(string.length-1, string.length) == ' ' )
    string = string.substring(0, string.length-1);

  return string;
}

function zeroPad(num, width) 
{
  num = num.toString();
  
  while (num.length < width)
    num = "0" + num;

  return num;
}

function round (n, dec) 
{
  var result;
  result = Math.round(n * Math.pow(10, dec)) / Math.pow(10, dec);
  if( result.toFixed )
    result = result.toFixed(dec);
  return result;
}

function setCaretToEnd(control) 
{
  if (control.createTextRange) 
  {
    var range = control.createTextRange();
    range.collapse(false);
    range.select();
  }
  else if (control.setSelectionRange) 
  {
    control.focus();
    var length = control.value.length;
    control.setSelectionRange(length, length);
  }
}

function Request(url, frame, useget)
{
  if( self.OnCustomRequest && OnCustomRequest(window, url, frame) )
    return;

  if( POST_URLS && (!useget || (typeof(useget) == 'undefined') || (useget == false)) )
    Post(url, frame);
  else
  {
    if( (ie && url.length > 2040) ||
        (ns4 && url.length > 30000) || 
        (ns6 && url.length > 9000) )
    {
      alert(COMN_URL_TOO_LONG);
      return;
    }

    if( !frame || (typeof(frame) == 'undefined') )
      location.href = url;
    else
      frame.src = url;
  }
}

/////////////////////////////////////////////////////////
// Form Post: Allows posting of urls in javascript.  This is
//            needed to get around GET url limits.
//            
// NOTE: If loading into a frame, the frame must be named.

var PostForm = null;
var UrlNameValuePairs;

function Post(url, frame)
{
  if( !url && !url.length )
    return -1;

  if( CreateForm(url, frame) < 0 )
    return -1;

  PostForm.submit();
  return 1;
}

function MakeHiddenForm(action, frame)
{
  var Form;
  var doc;
  var now = new Date();

  if( frame )
    doc = frame.document;
  else
    doc = document;

  Form = doc.createElement("form");
  Form.id = "" + now;
  Form.style.visibility='hidden';
  Form.style.display = 'none';
  Form.action = action;
  if( frame )
    Form.target = frame.name;

  Form.method = "POST";
  doc.body.appendChild(Form);
  return Form;
}

function CreateForm(url, frame) 
{
  var input;
  var text;
  var Name, Value;
  var doc;
  var iIndex;
  
  if( frame )
    doc = frame.document;
  else
    doc = document;

  // Split URL
  UrlNameValuePairs = url.split("?");
  
  if( UrlNameValuePairs.length == 0 )
    return -1;
    
  // get action
  PostForm = MakeHiddenForm(UrlNameValuePairs[0], frame);
  if( !PostForm )
    return -2;
  
  if( (iIndex = url.indexOf("?")) != -1 )
    url = url.substring(iIndex+1);

  // Parse and create from fields
  UrlNameValuePairs = url.split("&");
  iCount = UrlNameValuePairs.length;
  for(i = 0; i < iCount; i++)
  {
    input = doc.createElement('input'); 
    input.type = 'text';
    if( (iIndex = UrlNameValuePairs[i].indexOf('=')) != -1 )
    {
      Name = UrlNameValuePairs[i].substring(0, iIndex);
      Value = UrlNameValuePairs[i].substring(iIndex+1);
    }
    else
    {
      Name = "";
      Value = UrlNameValuePairs[i];
    }

    if( (Name.length == 0) && (i == 0) )  // most likely the MfcISAPICommand
    {
      input.id = "MfcISAPICommand";
      input.name = "MfcISAPICommand";
      input.value = Value;
    }
    else
    {
      input.id = Name;
      input.name = Name;
      if( Value.length > 0 )
        input.value = Value;
      else
        input.value = "";
    }

   // alert(input.id+","+input.value);

    PostForm.appendChild(input);
  }

  return 1;
}

function CreateIFrame(id, src)
{
  var IFrame = getelement(id);

  if( !IFrame )
  {
    IFrame = document.createElement('<iframe id="'+id+'" name="'+id+'">');
    document.body.appendChild(IFrame);
  }

  if( src )
    IFrame.src = src

  //IFrame.width = '200px';
  //IFrame.height = '200px';
  IFrame.style.visibility='hidden';
  IFrame.style.display = 'none';

  return document.frames[IFrame.id];
}

function ExtractString(String, iIndex, Start, End, Return)
{
  var iStart, iEnd;

  Return.value = "";
  iStart = String.indexOf(Start, iIndex);
  if( iStart != -1 )
  {
    iStart += Start.length;
    iEnd = String.indexOf(End, iStart);
    if( iEnd != -1 )
      Return.value = String.substring(iStart, iEnd);

    return iEnd;
  }

  return -1;
}

function ExtractDelimitedString(String, Delimiter, iIndex, Return)
{
  var       i, iStart, iEnd;

  Return.value = "";
  if ( (!String) || (!String.length) || (!Delimiter) || (iIndex < 0) )
    return false;

  iStart = 0;
  for(i = 0; i < iIndex; i++)
  {
    iStart = String.indexOf(Delimiter, iStart);
    if( iStart == -1 )
      return false;

    iStart += Delimiter.length;
  }

  if( iStart != -1 )
  {
    iEnd = String.indexOf(Delimiter, iStart);
    if( iEnd != -1 )
      Return.value = String.substring(iStart, iEnd);
    else
      Return.value = String.substring(iStart, String.length);

    return true;
  }

  return false;
}

function WriteMapSizes(CurrentMapSize, bHorizontal, bIncludeNoMap, bMain)
{
  var bSelected;

  if( self.OnCustomWriteMapSizes && OnCustomWriteMapSizes(window, CurrentMapSize, bHorizontal, bIncludeNoMap, bMain) )
    return;

  if( !bMain )
  {
    document.write('<table cellpadding="0" cellspacing="0" style="color:white;font-size:3px">');
    document.write('<tr><td style="width:30px;height:18px;color:white;font-size:10px"><a href="javascript:mapwidth(0)">'+ MAPWIN_MAPWIDTH_NONE +'</a></td>');

    if( !bHorizontal )
      document.write('</tr><tr>');
  
    iCount = aMapMapSizes.length;
    for(i = 0; i < iCount; i++)
    {
      if( (aMapMapSizes[i][1] == '0') && !bIncludeNoMap )
        continue;

      if( CurrentMapSize == aMapMapSizes[i][1] )
        document.write('<td><table align="center"><tr><td style="width:'+aMapMapSizes[i][2]+'px;height:'+aMapMapSizes[i][3]+'px;" align="center" class="MapPopupMapSizeButtonSelected" onmouseover="ChangeClass(this, \'MapPopupMapSizeButtonHover\');" onmouseout="ChangeClass(this, \'MapPopupMapSizeButtonSelected\');" onclick="mapwidth('+aMapMapSizes[i][1]+')">'+aMapMapSizes[i][0]+'</td></tr></table></td>');
      else
        document.write('<td><table align="center"><tr><td style="width:'+aMapMapSizes[i][2]+'px;height:'+aMapMapSizes[i][3]+'px;" align="center" class="MapPopupMapSizeButton" onmouseover="ChangeClass(this, \'MapPopupMapSizeButtonHover\');" onmouseout="ChangeClass(this, \'MapPopupMapSizeButton\');" onclick="mapwidth('+aMapMapSizes[i][1]+')">'+aMapMapSizes[i][0]+'</td></tr></table></td>');

      if( (i != (iCount-1)) && !bHorizontal )
        document.write('</tr><tr>');
    }
    
    document.write('</tr></table>');
  }
  else
  {
    bSelected = false;
    iCount = aMainMapSizes.length;
    for(i = 0; i < iCount; i++)
    {
      if( CurrentMapSize == aMainMapSizes[i][1] ) // selected
        bSelected = true;
    }

    for(i = 0; i < iCount; i++)
    {
      if( (aMainMapSizes[i][1] == '0') && !bIncludeNoMap )
        continue;

      if( (CurrentMapSize == aMainMapSizes[i][1]) || (!bSelected && (aMainMapSizes[i][1] != '0')) )  // select first non-zero size if nothing is selected
      {
        bSelected = true;
        document.write('<input type="radio" name="mapwidth" value="'+aMainMapSizes[i][1]+'" checked="checked"> '+aMainMapSizes[i][0]+'<br>');
      }
      else
        document.write('<input type="radio" name="mapwidth" value="'+aMainMapSizes[i][1]+'"> '+aMainMapSizes[i][0]+'<br>');
    }
  }
}

function WriteZooms(ZoomType, CurrentZoom, bMain)
{
  var Type, i, iCount, iZooms = 0;

  if( self.OnCustomWriteZooms && OnCustomWriteZooms(window, CurrentZoom, ZoomType, bMain) )
    return;

  if( ZoomType == 'Statute' )
    Type = 1;
  else if( ZoomType == 'Metric' )
    Type = 2;
  else if( ZoomType == 'Nautical' )
    Type = 3;

  if( !bMain )
  {
    document.write('<table border="0" cellpadding="0" cellspacing="2">');
    document.write('<tr><td align="center" style="height:30px;"><a href="javascript:zoomin()"><img border="0" src="' + HTMLPath + 'images/plus.gif" width="20" height="20" alt="'+MAPWIN_ZOOM_IN+'"></a></td></tr>');


    iCount = aMapZooms.length;
    for(i = 0; i < iCount; i++)
    {
      if( aMapZooms[i][0] == Type )
      {
        if( (aMapZooms[i][2] >= (CurrentZoom * 0.98)) && (aMapZooms[i][2] <= (CurrentZoom * 1.02)) )
          document.write('<tr><td align="center" class="MapPopupZoomButtonSelected" onmouseover="ChangeClass(this, \'MapPopupZoomButtonHover\');" onmouseout="ChangeClass(this, \'MapPopupZoomButtonSelected\');" onclick="zoom(\''+aMapZooms[i][2]+'\')">'+aMapZooms[i][1]+'</td></tr>');
        else
          document.write('<tr><td align="center" class="MapPopupZoomButton" onmouseover="ChangeClass(this, \'MapPopupZoomButtonHover\');" onmouseout="ChangeClass(this, \'MapPopupZoomButton\');" onclick="zoom(\''+aMapZooms[i][2]+'\')">'+aMapZooms[i][1]+'</td></tr>');
      }
    }
  
    document.write('<tr><td align="center" style="height:30px;"><a href="javascript:zoomout()"><img border="0" align="middle" src="' + HTMLPath + 'images/minus.gif" width="20" height="20" alt="'+MAPWIN_ZOOM_OUT+'"></a></td></tr>');
    document.write('</table>');
  }
  else
  {
    iCount = aMainZooms.length;
    for(i = 0; i < iCount; i++)
    {
      if( aMainZooms[i][0] == Type )
      {
        if( (aMainZooms[i][2] >= (CurrentZoom * 0.98)) && (aMainZooms[i][2] <= (CurrentZoom * 1.02)) )
          document.write('<input type="radio" name="Zoom" value="'+aMainZooms[i][2]+'" checked="checked"> '+aMainZooms[i][1]+'<br>');
        else
          document.write('<input type="radio" name="Zoom" value="'+aMainZooms[i][2]+'"> '+aMainZooms[i][1]+'<br>');
        iZooms++;
      }
    }
  }
}
