var ButtonLeft      = new Array
var ButtonMidNormal = new Array
var ButtonMidHover  = new Array
var ButtonRight     = new Array
var TextIndex       = 0

function insertButton(caption, url, params) {
/*
  caption	= "caption to be displayed on button"
  url		= "url to be opened"
  params	= "target:new, width:100, imagepath:some_directory, type:bar_nn, left:back, right:fwd"
  ----------------------------------------------------------------------------------------------------
  target	= _top or top (default), _new or new
  width		= defaults to width of text if not specified
  imagepath	= defaults to directory of this script if not specified

  the following parameters have no default values but depend on the name of button gif's
  type		= type such as bar, button, navbar, etc
  left		= back, prev
  right		= fwd, next
*/

  var Target = Param_Extract(params, "target")
  var Width = Param_Extract(params, "width")
  var ImagePath = Param_Extract(params, "imagepath")
  var Type = Param_Extract(params, "type")
  var Left = Param_Extract(params, "left")
  var Right = Param_Extract(params, "right")
  
  while (caption.indexOf(" ") >= 0)
    { caption = caption.replace(" ", "&nbsp;") }
  caption = "&nbsp;" + caption + "&nbsp;"

  if (Target == "")
    { Target = "_top" }
  else if (Target.indexOf("_") != 0)
    { Target = "_" + Target }

  if (Width == "") { Width = "0" }
  if (ImagePath == "") { ImagePath = "." }

  var CompositeMidNormal = ImagePath + Type + "_normal"
  var CompositeMidHover = ImagePath + Type + "_hover"

  if (Left != "")
    { var CompositeLeft = ImagePath + Type + "_left_" + Left }
  else
    { var CompositeLeft = ImagePath + Type + "_normal" }
	
  if (Right != "")
    { var CompositeRight = ImagePath + Type + "_right_" + Right }
  else
    { var CompositeRight = ImagePath + Type + "_normal" }

  CompositeLeft = CompositeLeft + ".gif"
  CompositeMidNormal = CompositeMidNormal + ".gif"
  CompositeMidHover = CompositeMidHover + ".gif"
  CompositeRight = CompositeRight + ".gif"

  TextIndex++
  ButtonLeft[TextIndex] = new Image
  ButtonMidNormal[TextIndex] = new Image
  ButtonMidHover[TextIndex] = new Image
  ButtonRight[TextIndex] = new Image

  ButtonLeft[TextIndex].src = CompositeLeft
  ButtonMidNormal[TextIndex].src =CompositeMidNormal
  ButtonMidHover[TextIndex].src =CompositeMidHover
  ButtonRight[TextIndex].src =CompositeRight

  var onMouseOutStr = 'background="'+ButtonMidNormal[TextIndex].src+'"'
  var onMouseOverStr = 'background="'+ButtonMidHover[TextIndex].src+'"'

  if (url == "" || url.indexOf("javascript:void") == 0)
    { document.writeln('<table border="0" cellpadding="0" cellspacing="0" style="cursor:hand">')
      document.writeln('<a onClick="javascript:void(0)">')
      document.writeln('<tr>')
      document.writeln('<td><img src="'+ButtonLeft[TextIndex].src+'" border="0" alt=""></td>')
      document.writeln('<td class="button" background="'+ButtonMidNormal[TextIndex].src+'" onMouseOver='+onMouseOverStr+' onMouseOut='+onMouseOutStr+' width="'+Width+'" nowrap>')
      document.writeln('<span class="button">'+caption+'</span></td>')
      document.writeln('<td><img src="'+ButtonRight[TextIndex].src+'" border="0" alt=""></td>')
      document.writeln('</tr>')
      document.writeln('</a>')
      document.writeln('</table>') }
  else if (url.indexOf("javascript") == 0)
    { // javascript found
      document.writeln('<table border="0" cellpadding="0" cellspacing="0" style="cursor:hand">')
      document.writeln('<a onClick="'+url+'" target="'+Target+'">')
      document.writeln('<tr>')
      document.writeln('<td><img src="'+ButtonLeft[TextIndex].src+'" border="0" alt=""></td>')
      document.writeln('<td class="button" background="'+ButtonMidNormal[TextIndex].src+'" onMouseOver='+onMouseOverStr+' onMouseOut='+onMouseOutStr+' width="'+Width+'" nowrap>')
      document.writeln('<span class="button">'+caption+'</span></td>')
      document.writeln('<td><img src="'+ButtonRight[TextIndex].src+'" border="0" alt=""></td>')
      document.writeln('</tr>')
      document.writeln('</a>')
      document.writeln('</table>') }
  else if (url.indexOf("togglePopup") == 0)
    { // popup found
      document.writeln('<table border="0" cellpadding="0" cellspacing="0" style="cursor:hand">')
      document.writeln('<span onClick="'+url+'">')
      document.writeln('<tr>')
      document.writeln('<td><img src="'+ButtonLeft[TextIndex].src+'" border="0" alt=""></td>')
      document.writeln('<td class="button" background="'+ButtonMidNormal[TextIndex].src+'" onMouseOver='+onMouseOverStr+' onMouseOut='+onMouseOutStr+' width="'+Width+'" nowrap>')
      document.writeln('<span class="button">'+caption+'</span></td>')
      document.writeln('<td><img src="'+ButtonRight[TextIndex].src+'" border="0" alt=""></td>')
      document.writeln('</tr>')
      document.writeln('</span>')
      document.writeln('</table>') }
  else
    { // regular url
      document.writeln('<table border="0" cellpadding="0" cellspacing="0" style="cursor:hand">')
      document.writeln('<a href="'+url+'" target="'+Target+'">')
      document.writeln('<tr>')
      document.writeln('<td><a href="'+url+'" target="'+Target+'"><img src="'+ButtonLeft[TextIndex].src+'" border="0" alt=""></a></td>')
      document.writeln('<td class="button" background="'+ButtonMidNormal[TextIndex].src+'" onMouseOver='+onMouseOverStr+' onMouseOut='+onMouseOutStr+' width="'+Width+'" nowrap>')
      document.writeln('<span class="button">'+caption+'</span></td>')
      document.writeln('<td><a href="'+url+'" target="'+Target+'"><img src="'+ButtonRight[TextIndex].src+'" border="0" alt=""></a></td>')
      document.writeln('</tr>')
      document.writeln('</a>')
      document.writeln('</table>') }
}



function Param_Extract(params, search) {
  var ParamsLC = params.toLowerCase()
  var SearchLC = search.toLowerCase()
  var SearchFrom = ParamsLC.indexOf(SearchLC)
  var SearchTo, ParamValue

  if (SearchFrom == -1)
    { ParamValue = "" }
  else
    { SearchFrom = SearchFrom + SearchLC.length + 1
      SearchTo = ParamsLC.indexOf(",", SearchFrom)
      if (SearchTo == -1)
        { ParamValue = ParamsLC.substring(SearchFrom) }
      else
        { ParamValue = ParamsLC.substring(SearchFrom, SearchTo) }
    }
  return(ParamValue)
}


function subdirPath(webname) {
  //alert(window.location.host)
  var strWebName = webname
  var strHref = window.location.href
  strWebName = strWebName.toLowerCase()
  strHref = strHref.toLowerCase()    
  strHref = strHref.substr(strHref.indexOf(strWebName))
  var subdirLocator = ""
  var counter = 0
  while (strHref.lastIndexOf("/") > -1) {
    counter++
    if (counter > 1) { subdirLocator = "../" + subdirLocator }
    strHref = strHref.substr(strHref, strHref.lastIndexOf("/"))
  }
  return(subdirLocator)
}
