var isDOM;
var ns4;
var op5; 
var op; 
var op6; 
var ns;
var agt;
var mac;
var ie; 
var mac_ie;
var isMozilla;
var isSafari;

InitBrowsers();

function IsSysAvailable() {
  try {
    return Sys;
  }
  catch(e)
  {
    return false;
  }
}

function isIE()
{
  var naVer=navigator.appVersion;
  var naAgn=navigator.userAgent;
  var nIE=document.all ? 1 : 0;
  var nVer=parseFloat(naAgn.substring(naAgn.indexOf("MSIE ")+5,naAgn.length));
  var nOP=naAgn.indexOf("Opera")>=0;

  if(nIE && nVer>=5 && !nOP)
    return true;

  return false;
}

function InitBrowsers()
{
    ns4   = document.layers;
    
    isDOM     = document.getElementById; 
    isMozilla = isDOM && navigator.appName=="Netscape";
    isSafari  = navigator.userAgent.indexOf("Safari") != -1;
    
    op  = navigator.userAgent.indexOf("Opera") != -1;
    op5 = (navigator.userAgent.indexOf("Opera 5")!=-1) 
                    || (navigator.userAgent.indexOf("Opera/5")!=-1);
    op6 = (navigator.userAgent.indexOf("Opera 6")!=-1) 
                    || (navigator.userAgent.indexOf("Opera/6")!=-1); 
                    
    agt    = navigator.userAgent.toLowerCase();
    mac    = (agt.indexOf("mac")!=-1);
    ie     = isIE(); 
    mac_ie = mac && ie;
}

// Adds trim function to javascript string object.
String.prototype.Trim = function() 
{ 
  return this.replace(/^\s+|\s+$/, ''); 
};

// Adds trim function to javascript string object.
String.prototype.EndsWith = function(pattern) 
{ 
   var d = this.length - pattern.length;
   return d >= 0 && this.lastIndexOf(pattern) === d;
};

String.prototype.IsNumeric = function ()
{
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var pointIsfound = false;
    var Char;

    var sText = this.Trim();

    if (sText.length == 0) return false; 

    Char = sText.charAt(0); 
    var startIndex = 0;
    if (Char == '-' || Char == '+')
      startIndex = 1;
      
    for (var i = startIndex; i < sText.length && IsNumber == true; i++) 
    { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) IsNumber = false;
        if (Char == ".") 
        {
            if (pointIsfound) return false;
            pointIsfound = true;     
        }
    }
    return IsNumber; 
};

String.prototype.IsInteger = function ()
{
    var ValidChars = "0123456789";
    var IsNumber=true;
    var Char;

    var sText = this.Trim();
    
    if (sText.length == 0) return false; 
    
    Char = sText.charAt(0); 
    var startIndex = 0;
    if (Char == '-' || Char == '+')
      startIndex = 1;
      
    for (var i = startIndex; i < sText.length && IsNumber == true; i++) 
    { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) IsNumber = false;
    }
    return IsNumber; 
};

function DisableControl(c)
{
    if (c.type == 'text' || c.type == 'password' || (c.type == 'textarea'))
    c.readOnly = true;
  else
    c.disabled = true; 
  
  if (c.type == 'submit')
    c.className= "flatButtonDisabled";
  else
  {
      if (IsSysAvailable()) {
          if (c.className.indexOf("txtReadOnly"))
              Sys.UI.DomElement.removeCssClass(c, "txtReadOnly");
          else
              Sys.UI.DomElement.removeCssClass(c, "txtReadonly");
          Sys.UI.DomElement.addCssClass(c, "txtReadOnly");
      }
      else {
          c.className = "txtReadOnly";
      }
  }
}

function ShowHideControl(c, show)
{
    c.style.display = (show) ? "" : "none";
    c.style.visibility = (show) ? "" : "hidden";
}

function ShowHideControlEx(c, show) 
{
    if (IsSysAvailable()) 
    {
        if (show)
            Sys.UI.DomElement.removeCssClass(c, "hidden");
        else  
            Sys.UI.DomElement.addCssClass(c, "hidden");
    }
    else {
        if (show)
            c.className = "";
        else
            c.className = "hidden";    
    }
}

function EnableControl(c)
{
  if (c.type == 'text' || c.type == 'password' || (c.type == 'textarea'))
  {
    c.readOnly = false;
    
    if (IsSysAvailable()) {
        if (c.className.indexOf("txtReadOnly"))
            Sys.UI.DomElement.removeCssClass(c, "txtReadOnly");
        else
            Sys.UI.DomElement.removeCssClass(c, "txtReadonly");
        Sys.UI.DomElement.addCssClass(c, "txtEditable");
    }
    else  
    {
      c.className = "txtEditable";  
    }
  }
  else
  {
    if (c.type == 'submit')
      c.onmouseout();  
    else
      c.className = "txtEditable";   
    
    c.disabled = false;
  }
}

function UpdateControlEnabledState(c, enabled)
{
  if (enabled)
    EnableControl(c);
  else 
    DisableControl(c);
}

function UpdateDateControlEnabledState(c, enabled)
{
  if (enabled)
  {
    EnableControl(c);
    
    c.nextSibling.style.display = 'inline';
  }
  else 
  {
    DisableControl(c);
    
    c.nextSibling.style.display = 'none';  
  }
}

function SuppressEvent()
{
  if (window.event)
    window.event.cancelBubble = true;
}

function Confirm(msg)
{
    if (!confirm(msg)) 
    {
        SuppressEvent();
        
        return false;
    }
    else
    {
        return true;
    }
}

var popUpWindows = new Array();
function closePopup(name)
{
  if (name)
  {
    if (popUpWindows[name])
    {
      try
      {
        popUpWindows[name].close();
      } catch (e) {}
    }
    return;
  }

  for(s in popUpWindows)
  {              
    if (popUpWindows[s])
    {
      try
      {
        popUpWindows[s].close();
      } catch (e) {}
    }
  }
}  

var popupNotifyMessage = "Hold down CTRL if you\nuse a popup blocker.";

function openDialogWindow(url, name, width, height, notifyAboutPopupBlockers)
{
  closePopup(name);

  try
  {
      posx = (screen.width)  ? (screen.width  - width)  / 2 : 0;
      posy = (screen.height) ? (screen.height - height) / 2 : 0;
      settings = "resizable=yes,toolbar=no,menubar=no,location=no,titlebar=no,status=no,scrollbars=yes,left=" + posx + ",top=" + posy + ",height=" + height + ",width=" + width;
      var win = window.open(url, name, settings);
      
      if (!win)
      {
        if (notifyAboutPopupBlockers)
          alert(popupNotifyMessage);
        return;
      }
      
      popUpWindows[name] = win;
      if (win.window.focus)
      {
        win.window.focus();
      }

      return win;
  }
  catch(e)
  {
    if (notifyAboutPopupBlockers)
          alert(popupNotifyMessage);
  }
}

function ChkInputRequired_Validate(sender, args)
{
  args.IsValid = (document.getElementById(sender.ControlToValidate).checked);
}

function ResetScrollPosition() 
{
  var scrollX = document.getElementById('__SCROLLPOSITIONX');
  var scrollY = document.getElementById('__SCROLLPOSITIONY');
  if (scrollX && scrollY) 
  {
    scrollX.value = 0;
    scrollY.value = 0;
  }
}


//
//  Filter actions
//
function HideSearchResults(resultsPanelId)
{
  var resultsPanel = document.getElementById(resultsPanelId);            
  if (resultsPanel)
  {
    resultsPanel.className = "hidden";
    return true;
  }      
  else
  {
    return false;
  }      
}

function SetFilterControlDefaultValue(c, v)
{
  if (c && (v || v == ""))
  {
    if (c.type == 'checkbox')
      c.checked = v;
    else
      c.value = v;  
  }
}

///<summary>
/// Resets filters band hides result - can be used for Reset button on filter panel
///</summary>
function ResetFilters()
{
   if ((!arguments) || (arguments.length == 0))
     return true;
     
   if (document.getElementById(arguments[0]))  
   {
     var gridHidden = HideSearchResults(arguments[0]);  
   
     if (!gridHidden)
       return true;
   }    
   
   if (arguments.length == 1)
     return false;
     
   for(var i = 1; i < arguments.length; i++)
   {
     var c = document.getElementById(arguments[i]);
     var v = arguments[i + 1];
     
     SetFilterControlDefaultValue(c, v);
     i++;
   }
        
   return false;  
}

function addEvent(control, type, handler) 
{
  var el = control;
  if (el.addEventListener) {
      el.addEventListener(type, handler, false);
  } else if (el.attachEvent) {
      el.attachEvent("on" + type, handler);
  } else {
      el.onclick = handler;
  }
}

// http://weblogs.asp.net/rajbk/archive/2006/08/04/Clickable-GridView-Headers.aspx
function ReqGVData(linkId, cellElement, evt) 
{
    var evtSource;
    evt = (evt)? evt : window.event;
    evtSource = (evt.srcElement)? evt.srcElement : evt.target;


    //When a hyperlink is clicked, Safari returns the text node as the source element rather
    //than the hyperlink. parentNode will give us the hyperlink element.
    //ref: http://developer.apple.com/internet/webcontent/eventmodels.html
    if (evt.target) {
        if (evt.target.nodeType == 3) {
            evtSource = evtSource.parentNode;
        }
    }

    //If event was raised from an element other than the LinkButton
    if ((evtSource.getAttribute("id") != linkId) && (evt.type == "click")) {

        //Get a collection of "a" tags inside the cell        

        var linkCollection = cellElement.getElementsByTagName("a");
        for (var i = 0; i < linkCollection.length; i++) {

           //If the link button has an onclick attribute, call the onclick.
           //The onclick attribute is present when the GridView is using callback
           //example: onclick="java script:__gvGridSort1_GridView1.callback(...); return false;"

           var onClickAttribute = linkCollection[i].getAttribute("onclick");
           if (onClickAttribute != null) {
            linkCollection[i].onclick();
            break;
           }

           //If the link button has a href attribute, set the location of the page
           //to the href value.
           //The href attribute is used when the GridView is not using callbacks
           //example: href="java script:__doPostBack('GridSort1$GridView1','Sort$UnitsOnOrder')" 
           var hrefAttribute = linkCollection[i].getAttribute("href");
           this.location.href = hrefAttribute;
           break;
        }
    }
}