﻿Sys.Browser.WebKit = {}; //Safari 3 is considered WebKit
if( navigator.userAgent.indexOf( 'WebKit/' ) > -1 )
{
  Sys.Browser.agent = Sys.Browser.WebKit;
  Sys.Browser.version = parseFloat(
navigator.userAgent.match(/WebKit\/(\d+(\.\d+)?)/)[1]);
  Sys.Browser.name = 'WebKit';
}

function ToggleElementVisibility(elementID) {
    var element = $get(elementID);

    if (element.style.display == 'none') {
        element.style.display = '';
    }
    else {
        element.style.display = 'none';
    }
}

// for Mozilla browsers

if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}

/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
	if (this.readyState == "complete") {
		init(); // call the onload handler
	}
};
/*@end @*/

if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      clearInterval(_timer);
      init(); // call the onload handler
    }
  }, 10);
}

window.onload = init;


function init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;

  // do stuff
  fixSize(); 
};

function calendarFix()
{
  var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
  
  attachCalendarCode();
  
  if(IE6)
  {    
    attachAutoCompleteCode();
  }
}

function fixSize()
{
    
}

function attachAutoCompleteCode()
{
    var behaivor = $find('AutoComplete');
    
    if( behaivor != null )
    {
        behaivor.add_shown( function hideCombosOnSearchAutoCompleteIfIE6(sender,args)
        {
            $get('combos2').style.display = 'none';
        });

        behaivor.add_hidden( function showCombosOnSearchAutoCompleteIfIE6(sender,args)
        {
            $get('combos2').style.display = '';
        });

    }
}

function attachCalendarCode()
{
    var behaivor = $find('dtArrival_calendar_behaivor')
    
 /*   if( behaivor != null )
    {
        behaivor.add_shown( function hideCombosOnSearchCalendarIfIE6(sender,args)
        {
            $get('combos').style.display = 'none';
        });
        
        behaivor.add_hidden( function showCombosOnSearchCalendarIfIE6(sender,args)
        {
            $get('combos').style.display = '';
        } );
    }*/
}

var search = false;

function pageLoad(sender, args){  
    if(!args.get_isPartialLoad()){  
        //  register for our events
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);    
    }       
    
    calendarFix();
}

function setSearch()
{    
    search = true;
}

function removeSearch()
{    
    search = false;
    enableScreen();
}

function beginRequest(sender, args)
{
    if( search )
    {
        disableScreen();
    }
}

function pageLoadingHandler(sender, args) 
{
}

function endRequest(sender, args) 
{
    if( search )
    {
        enableScreen();
    }
    
    calendarFix();
}          

function disableScreen()
{   
    var a = $get('ctl00_ctl00_pnlAll');
    
    a.style.display = 'none';
    
    var b = $get('searchwait');
    
    b.style.display = '';
}

function enableScreen()
{
    var a = $get('ctl00_ctl00_pnlAll');
    
    a.style.display = '';
    
    var b = $get('searchwait');
    
    b.style.display = 'none';
}

function resetPosition(object, args)
{	
    var tb = object._element;	
    var tbposition = findPositionWithScrolling(tb);
	var xposition = 0; //tbposition[0];	
	var yposition = 20; //tbposition[1] + 25;	// 25 = textbox height + a few pixels spacing

	var ex = object._completionListElement;	
	
	if (ex)
	{		
	    //$common.setLocation(ex, new Sys.UI.Point(xposition, yposition));
	}
}

 function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }


function findPositionWithScrolling( oElement )
{	
    if( typeof( oElement.offsetParent ) != 'undefined' )
	{		
	    var originalElement = oElement;
		
		for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent )
		{
		    posX += oElement.offsetLeft;			
		    posY += oElement.offsetTop;			
		    
//		    if( oElement != originalElement && oElement != document.body && oElement != document.documentElement )
//			{				
//			    //posX -= oElement.scrollLeft;				
//			    //posY -= oElement.scrollTop;			
//            }		
        }		
        
        return [ posX, posY ];
    } 
    else 
	{
	    return [ oElement.x, oElement.y ];
    }
}
