// see http://www.sitepoint.com/print/scroll-smoothly-javascript/ for details
function ScrollToAnchor(anchorName)
{
    ScrollToAnchor(anchorName, false);
}
function ScrollToAnchor(anchorName, bScrollAlways)
{
    destinationLink = GetAnchor(anchorName);
    if (destinationLink == null)
        window.scrollTo(0, 0);
    else
    {
          var destx = destinationLink.offsetLeft;  
         var desty = destinationLink.offsetTop;
         var thisNode = destinationLink;
         while (thisNode.offsetParent &&  
               (thisNode.offsetParent != document.body)) {
           thisNode = thisNode.offsetParent;
           destx += thisNode.offsetLeft;
           desty += thisNode.offsetTop;
        }
        
        cypos = ss_getCurrentYPos(); 
        
        if (bScrollAlways == true || cypos > desty)    
            window.scrollTo(0, desty);
    }
}

function ss_getCurrentYPos() {
 if (document.body && document.body.scrollTop)
   return document.body.scrollTop;
 if (document.documentElement && document.documentElement.scrollTop)
   return document.documentElement.scrollTop;
 if (window.pageYOffset)
   return window.pageYOffset;
 return 0;
}


function GetAnchor(anchorName)
{
  var allLinks = document.getElementsByTagName('a');
  for (var i=0;i<allLinks.length;i++) {
   var lnk = allLinks[i];
   if (lnk.hash && lnk.hash.length > 1 && (lnk.hash.substr(1) == anchorName)) {
     return lnk;
   }
 }
}
/* scroll functions for validations */
window.scrollTo = function () { };
function ScrollToFirstError(groupName) {
    Page_ClientValidate(groupName);
    if (Page_IsValid == false) {
        $("#spError").show();
        var topMostValidator;
        var lastOffsetTop;
        for (var i = 0; i < Page_Validators.length; i++) {
            var vld = Page_Validators[i];
            if (vld.validationGroup == groupName) {
                if (vld.isvalid == false) {
                    if (PageOffset(vld) < lastOffsetTop || lastOffsetTop == undefined) {
                        topMostValidator = vld;
                        lastOffsetTop = vld.offsetTop;
                    }
                }
            }
        }
        topMostValidator.scrollIntoView();
    }
    else {
        $("#spError").hide();
    }
    return Page_IsValid;
}
function PageOffset(theElement) {
    var selectedPosY = 0;
    while (theElement != null) {
        selectedPosY += theElement.offsetTop;
        theElement = theElement.offsetParent;
    }
    return selectedPosY;
}
