// SmartScroll methods
var msSmartScrollID;

function RecordSmartScrollInfo() {
   var hidX = document.getElementById(msSmartScrollID + '_hidScrollX');
   var hidY = document.getElementById(msSmartScrollID + '_hidScrollY');

   if (navigator.appName.indexOf('Microsoft Internet Explorer') >= 0) {
      if (hidX != null) hidX.value = document.body.scrollLeft.toString();
      if (hidY != null) hidY.value = document.body.scrollTop.toString();
   }
   else {
      if (hidX != null) hidX.value = window.pageXOffset.toString();
      if (hidY != null) hidY.value = window.pageYOffset.toString();
   }
}

function SmartScrollLoad() {
   var iss = document.getElementById(msSmartScrollID);
   if (iss == null) return;

   var bTrackHoriz = (iss.attributes["TrackHoriz"].value == 'True');
   var bTrackVert  = (iss.attributes["TrackVert"].value  == 'True');
   var bTrackFocus = (iss.attributes["TrackFocus"].value == 'True');
   
   var hidX = document.getElementById(msSmartScrollID + '_hidScrollX');
   var hidY = document.getElementById(msSmartScrollID + '_hidScrollY');
   var hidF = document.getElementById(msSmartScrollID + '_hidFocus');

   var nX = (bTrackHoriz && (hidX != null) && (hidX.value != '')) ? parseFloat(hidX.value) : 0;
   var nY = (bTrackVert  && (hidY != null) && (hidY.value != '')) ? parseFloat(hidY.value) : 0;

   window.scrollTo(nX,nY);

   if (bTrackFocus && (hidF != null) && (hidF.value != '')) {
      var bFocusNext = (iss.attributes["FocusNext"].value == 'True');
      var ctlF = document.getElementById(hidF.value);

      if (bFocusNext && (ctlF != null)) {
         var bCurrFocusFound = false;
         var ctl;

         for (var i = 0; i < document.forms[0].elements.length; i++) {
            ctl = document.forms[0].elements[i];

            if (bCurrFocusFound) {
               if ((ctl.type == 'text') || (ctl.type == 'password') || (ctl.type == 'file') || 
                     (ctl.type == 'checkbox') || (ctl.type == 'radio') || (ctl.type == 'textarea') || 
                     (ctl.type == 'select-one') || (ctl.type == 'select-multiple')) {
                  try {
                     ctl.focus();
                     ctlF = ctl;    // this only happens if the control can be focused
                     ctl.select();  // this only happens if the control can be selected
                     break;
                  }
                  catch (ex) {
                     // can't focus -- skip it
                  }
               }
            }
            else {
               if (ctl.id.toUpperCase() == ctlF.id.toUpperCase()) bCurrFocusFound = true;
            }
         }
      }

      if (ctlF != null) {
         try {
            ctlF.focus();

            if ((ctl.type == 'text') || (ctl.type == 'password') || (ctl.type == 'file') || 
                  (ctl.type == 'checkbox') || (ctl.type == 'radio') || (ctl.type == 'textarea') || 
                  (ctl.type == 'select-one') || (ctl.type == 'select-multiple')) {
               ctlF.select();
            }
         }
         catch (ex) {
            // ignore if fails
         }
      }
   }
   else {
      var bAutoFocus = (iss.attributes["AutoFocus"].value == 'True');

      if (bAutoFocus) {
         var ctl;

         for (var i = 0; i < document.forms[0].elements.length; i++) {
            ctl = document.forms[0].elements[i];

            if ((ctl.type == 'text') || (ctl.type == 'password') || (ctl.type == 'file') || 
                  (ctl.type == 'checkbox') || (ctl.type == 'radio') || (ctl.type == 'textarea') || 
                  (ctl.type == 'select-one') || (ctl.type == 'select-multiple')) {
               try {
                  ctl.focus();
                  if (ctl.select) ctl.select();
                  break;
               }
               catch (ex) {
                  // can't focus -- skip it
               }
            }
         }
      }
   }

   document.body.onscroll = RecordSmartScrollInfo;
   setInterval('SmartScrollUnload()',50);
}

function SmartScrollUnload() {
   var hidF = document.getElementById(msSmartScrollID + '_hidFocus');

   if (hidF != null) {
      if (document.activeElement) hidF.value = document.activeElement.id;
      else {
         // code for non-IE browsers
      } 
   }
}
