

function focusOnElement(elementId)
{
  var anElement;

  if (document.getElementById)
  {
    anElement = document.getElementById(elementId);
  }
  else if( document.layers )
  {
    anElement = document[elementId];
  }
  else
  {
    anElement = document.all[elementId];
  }

  if(anElement && anElement.focus)
  {
    anElement.focus();
  }
}

function resetCursorPosition(oInput)
{
  if( oInput.setSelectionRange )
  {
      oInput.setSelectionRange(0,0);
  }
  else if( oInput.createTextRange )
  {
      var range = oInput.createTextRange();
      range.collapse(true);
      range.moveEnd('character',0);
      range.moveStart('character',0);
      range.select();
  }
}

function focusOnFirstElement()
{
  if(document.forms)
  {
    for(j=0; j<document.forms.length; j++)
    {
      for(i=0; i<document.forms[j].elements.length; i++)
      {
        if(document.forms[j].elements[i].type != "hidden")
        {
          document.forms[j].elements[i].focus();
          if( document.forms[j].elements[i].type == "text" )
          {
            resetCursorPosition(document.forms[j].elements[i]);
          }
          return;
        }
      }
    }
  }
}

function doCommonOnLoad(errorFieldId)
{
  if( errorFieldId )
  {
    focusOnElement(errorFieldId);
  }
  else
  {
    focusOnFirstElement();
  }

  if(doOnPageLoad)
  {
    doOnPageLoad();
  }
}