/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}


// check timout
var timeoutInterval;
var serverResponseFailCtr = 0;
var xmlhttp=null;
var isDisplayTimeout = false;

function CallAjax(url, method, isAsync, oFunction)
{
   
	    try
	    {
		    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		    if (typeof(xmlhttp) != 'object' )
		    {	 	
		       // If IE7, Mozilla, Safari, etc: Use native object
		       xmlhttp = new XMLHttpRequest();
		    }
	    }
	    catch (e) 
	    {
		    // fallback: use native object 
		    xmlhttp = new XMLHttpRequest();
	    }

	    if (xmlhttp!=null)
	    {
		    xmlhttp.open(method,url,isAsync);        
		    if (isAsync == true)
		    {
		         xmlhttp.onreadystatechange  = oFunction ;
                 xmlhttp.send(null);  
		    }
		    else
		    {
		         xmlhttp.open(method,url,false);
		         xmlhttp.send(null);
    		     
		         return xmlhttp.responseText;
		    }
          
	    }	
}

// This function is an async ajax callback for the session check..
// if the session timeout is imminant (within 120 seconds) display a submodal warning window 

function GetTimeoutInterval()
{  
    try
    {
        if ( xmlhttp.readyState  == 4)
        {

            timeoutInterval  = xmlhttp.responseText;

         
            if (timeoutInterval == null || timeoutInterval==undefined || isNaN(timeoutInterval) )
            {
                // check if server failed to respond 
                serverResponseFailCtr ++;
            }
            else if ( timeoutInterval != null && timeoutInterval != '' && (timeoutInterval <= 120 && timeoutInterval >=0))
            {
               isDisplayTimeout = true;
                // show submodal
               top.openLightBox(virtualPath + 'promptSessionTimeout.aspx?timeout=' + timeoutInterval, 300, 100, null, false, false);
            }
        }
    }
    catch(e){}
}


// check the amount of time remaining to which the session will timemout
function CheckSession()
{
   // do not check for imminant session timeout if the warning is already being displayed
   // or if the server failed to respond after 3 attempts
   if (isDisplayTimeout == false )
   {
         if (serverResponseFailCtr < 3 && (top.opener == null 
                || (top.opener != null && (top.opener.isDisplayTimeout == null || top.opener.isDisplayTimeout == false))) && (top.dialogWin == null || top.dialogWin.win == null || top.dialogWin.win.closed ))
         {
             
              // Check the time to timeout
              var url = virtualPath + "checkTimeoutInterval.aspx"; 
              CallAjax(url, "POST", true, GetTimeoutInterval);
              
              
         } 
    }
}

function resizeIframe(ifrmID, widthonly)
{
// this function stretches an iframe to the content (scroll) width and
// height of the contained document makeing it appear as part of the 
// parent frame (ie. eliminating the need for scrollbars)

  var ifr = document.getElementById(ifrmID);
  var ifrBody = ifr.contentWindow.document[getDocBody(ifr.contentWindow)];
      
      if (widthonly != null && widthonly != true)
      {
        ifr.style.height=ifrBody.scrollHeight+"px";
        ifr.style.width=ifrBody.scrollWidth+"px";

      }
      else
      {
            ifr.style.width=(ifrBody.scrollWidth+ 18) +"px";
      
      }


      
}

function getDocBody(win)
{
  if (!win) win=window;
  if (win.document.compatMode && win.document.compatMode != 'BackCompat') { //standards compliant mode 
    return "documentElement";
  }
  return "body";
}


	
	

