dom = (document.getElementById) ? true : false;
nn4 = (document.layers) ? true : false;
ie = (document.all) ? true : false;
ie4 = (!dom && ie)?  true : false;
ie5 = document.all && document.getElementById;
ns6 = document.getElementById && !document.all;

function WriteLayer(Layer, Code, Doc)
{
  if(!Doc)
    Doc = 'document';
  if(nn4)
  {
    layer.document.open();
    layer.document.write(Code);
    layer.document.close();
  }
  else if(ie4)
    Layer.innerHTML = Code;
  else if(dom)
    Layer.innerHTML = Code;
}

function FindObject(ID, Doc)
{
  var i;
  if(!Doc)
    Doc = document;
  if(dom)
    return Doc.getElementById(ID);
  else if(ie4)
    return Doc.all[ID];
  for(i = 0; i<Doc.forms.length; i++)
    for(j = 0; j<Doc.forms[i].elements.length; j++)
      if(Doc.forms[i].elements[j].name==ID)
        return Doc.forms[i].elements[j];
  for(i = 0; i<Doc.images.length; i++)
    if(Doc.images[i].name==ID)
      return Doc.images[i];
  if(!Doc.layers)
    return null;
  for(i = 0; i<Doc.layers.length; i++)
  {
    if(doc.layers[i].name==ID)
      return Doc.layers[i];
    var x = FindObject(ID, Doc.layers[i].document );
    if(x)
      return x;
  } 
  return null;
}

function GetWindowWidth()
{
  return ns6 ? window.innerWidth-20 : document.body.clientWidth;
}

function GetWindowHeight()
{
  return ns6 ? window.innerHeight-20 : document.body.clientHeight;
}

function GetRadioValue(RadioButton)
{
  for(var i = 0; i < RadioButton.length; i++)
    if(RadioButton[i].checked)
      return RadioButton[i].value;
}

function SetRadioValue(RadioButton, Value)
{
  for(var i = 0; i < RadioButton.length; i++)
    if(RadioButton[i].value == Value)
    {
      RadioButton[i].checked = true;
      break;
    }
  return false;
}

function GetPosX(Control)
{
  var Result = Control.offsetLeft;
  while(Control.offsetParent != null)
  {
    Control = Control.offsetParent;
    Result += Control.offsetLeft;
  }
  return Result;
}

function GetPosY(Control)
{
  var Result = Control.offsetTop;
  while(Control.offsetParent != null)
  {
    Control = Control.offsetParent;
    Result += Control.offsetTop;
  }
  return Result;
}

function anchorPtrByName(anchorName)
{
  for (var i = 0; i < document.anchors.length; i++)
    if (document.anchors[i].name == anchorName)
      return document.anchors[i];
}

function FloatString(f, Digits)
{
  var s = new String(f);
  if(s=='NaN')
    return '0';
  var p = s.indexOf('.');
  if(p==-1)
    p = s.indexOf(',');
  var i;
  if(p==-1)
    i = s.length;
  else
    i = p;
  i -= 3;
  while(i>0)
  {
    s = s.substr(0, i) + " " + s.substr(i);
    i -= 3;
  }
  if(Digits>0)
  {
    if(p==-1)
    {
      s = s+'.';
      p = s.length-1;
    }
    var ZerosToAdd = s.length-p-1+Digits;
    for(i = 0; i<ZerosToAdd; i++)
      s = s+'0';
  }
  return s;
}

function SelectTextboxRange(Textbox, Start, Finish)
{
  if(Textbox.createTextRange)
  {
    var Range = Textbox.createTextRange();
    Range.moveStart("character", Start);
    Range.moveEnd("character", Finish-Textbox.value.length);
    Range.select();
  }
  else if (Textbox.setSelectionRange)
    Textbox.setSelectionRange(Start, Finish);
}

function CheckField(FieldName, FieldDesc)
{
  var obj;
  obj = FindObject(FieldName);
  if(obj)
  {
    if(obj.value=='' || obj.value==0)
    {
      alert('Please enter a value for the ' + FieldDesc + ' field.');
      obj.focus();
      return false;
    }
  }
  else
  {
    alert('Field ' + FieldName + ' not found');
    return false;
  }
  return true;
}

function myonclick(id, val)
{
  var obj;
  var obj2;

  // deselect current selection
  obj2 = FindObject('StepID');
  obj = FindObject('service_' + obj2.value);

  if(obj)
  {
    obj.style.borderColor = "#000000";
    obj.style.borderStyle = "solid";
    obj.style.borderWidth = "0px";
  }

  // save selected id
  obj = FindObject('StepID');
  if(obj)
    obj.value = val;

  // select selection
  obj = FindObject(id);
  if(obj)
  {
    obj.style.borderColor = "#FF0000";
    obj.style.borderStyle = "solid";
    obj.style.borderWidth = "1px";
  }

  return false;
}

