//-------------- BEGIN CallBackObject --------------------
var selectedTxt = '';
var selectedPos = -1;
var maxPos = -1;
function CallBackObject()
{
  this.XmlHttp = this.GetHttpObject();
}
 
CallBackObject.prototype.GetHttpObject = function()
{ 
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
 
CallBackObject.prototype.DoCallBack = function(eventTarget, eventArgument)
{
  var theData = '';
  var theform = document.forms[0];
  var thePage = window.location.pathname + window.location.search;
  var eName = '';
 
  theData  = '__EVENTTARGET='  + escape(eventTarget.split("$").join(":")) + '&';
  theData += '__EVENTARGUMENT=' + eventArgument + '&';
  theData += '__VIEWSTATE='    + escape(theform.__VIEWSTATE.value).replace(new RegExp('\\+', 'g'), '%2b') + '&';
  theData += 'IsCallBack=true&';
  for( var i=0; i<theform.elements.length; i++ )
  {
    eName = theform.elements[i].name;
    if( eName && eName != '')
    {
      if( eName == '__EVENTTARGET' || eName == '__EVENTARGUMENT' || eName == '__VIEWSTATE' )
      {
        // Do Nothing
      }
      else
      {
        theData = theData + escape(eName.split("$").join(":")) + '=' + theform.elements[i].value;
        if( i != theform.elements.length - 1 )
          theData = theData + '&';
      }
    }
  }
 
  if( this.XmlHttp )
  {
    if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
    {
      var oThis = this;
      this.XmlHttp.open('POST', thePage, true);
      this.XmlHttp.onreadystatechange = function(){ oThis.ReadyStateChange(); };
      this.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      this.XmlHttp.send(theData);
    }
  }
}
 
CallBackObject.prototype.AbortCallBack = function()
{
  if( this.XmlHttp )
    this.XmlHttp.abort();
}
 
CallBackObject.prototype.OnLoading = function()
{
  // Loading
}
 
CallBackObject.prototype.OnLoaded = function()
{
  // Loaded
}
 
CallBackObject.prototype.OnInteractive = function()
{
  // Interactive
}
 
CallBackObject.prototype.OnComplete = function(responseText, responseXml)
{
  // Complete
}
 
CallBackObject.prototype.OnAbort = function()
{
  // Abort
}
 
CallBackObject.prototype.OnError = function(status, statusText)
{
  // Error
}
 
CallBackObject.prototype.ReadyStateChange = function()
{
  if( this.XmlHttp.readyState == 1 )
  {
    this.OnLoading();
  }
  else if( this.XmlHttp.readyState == 2 )
  {
    this.OnLoaded();
  }
  else if( this.XmlHttp.readyState == 3 )
  {
    this.OnInteractive();
  }
  else if( this.XmlHttp.readyState == 4 )
  {
    if( this.XmlHttp.status == 0 )
      this.OnAbort();
    else if( this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK" )
      this.OnComplete(this.XmlHttp.responseText, this.XmlHttp.responseXML);
    else
      this.OnError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText);   
  }
}
//----------------End CallBackObject---------------------

//----------------BEGIN AutoCompleteTextBox ------------------------

function AutoCompleteTextBox(TextBoxId, DivId, DivClass)
{
	// initialize member variables
	var oThis = this;
	var oText = document.getElementById(TextBoxId);
	var oHidden = document.getElementById(TextBoxId + "_hidden");
	var oImg  =  document.getElementById(TextBoxId + "_img");
	var oDiv  = document.getElementById(DivId);
	this.TextBox = oText;
	this.txtHidden = oHidden;
	this.Div = oDiv;
	
	// CallBackObject + Event Handlers
	this.Cbo = new CallBackObject();
	this.Cbo.OnComplete = function(responseText,responseXML){oThis.Cbo_Complete(responseText,responseXML);};
	this.Cbo.OnError    = function(status,statusText,responseText){oThis.Cbo_Error(status,statusText,responseText);};
	this.Cbo.OnLoaded  = function(){ setLoading(oImg, "hidden")}
	this.Cbo.OnLoading = function(){ setLoading(oImg, "visible")}
			
	// attach handlers to the TextBox
	oText.AutoCompleteTextBox = this;
	oText.onkeydown = AutoCompleteTextBox.prototype.OnKeyDown;
	oText.onkeyup = AutoCompleteTextBox.prototype.OnKeyUp;
	oText.onblur  = AutoCompleteTextBox.prototype.OnBlur;
	
	// align the drop down div
	var c = GetCoords(oText);
	var n = oText.style.pixelHeight;
	if( !n )
	{ 
	  n = 23;
	}
	else
	{
	  n += 0;
	}
	oDiv.style.left = c.x;
	oDiv.style.top = c.y + n;
	oDiv.style.display = 'none';
	oDiv.style.position = 'absolute';
	
	// Set some default styles
	if( DivClass )
	  oDiv.className = DivClass;
	else
	{
	  oDiv.style.border = '1';
	  oDiv.style.borderColor = 'black';
	  oDiv.style.borderStyle = 'solid';
	  oDiv.style.backgroundColor = 'white';
	  oDiv.style.padding = '2';
	}
}

AutoCompleteTextBox.prototype.DoAutoSuggest = false;

AutoCompleteTextBox.prototype.ListItemClass = '';

AutoCompleteTextBox.prototype.ListItemHoverClass = '';

// TextBox OnBlur
AutoCompleteTextBox.prototype.OnBlur = function()
{
	this.AutoCompleteTextBox.TextBox_Blur();
}

AutoCompleteTextBox.prototype.TextBox_Blur = function()
{
	this.Div.style.display='none';
	estadosCombos('visible');
}

// TextBox OnKeyDown
AutoCompleteTextBox.prototype.OnKeyDown = function(oEvent)
{
  //check for the proper location of the event object
  if (!oEvent) 
  {
    oEvent = window.event;
  }    
  this.AutoCompleteTextBox.TextBox_KeyDown(oEvent);      
}

AutoCompleteTextBox.prototype.TextBox_KeyDown = function(oEvent)
{
  var iKeyCode = oEvent.keyCode;

	if ((iKeyCode==9 || iKeyCode==13) && selectedPos > -1)
	{
		//this.AutoSuggest(selectedTxt.split(','));
		//this.Div.style.display='none';
		this.TextBox.value = document.getElementById(this.Div.id + "_" + selectedPos).innerHTML;
		this.txtHidden.value = document.getElementById(this.Div.id + "_" + selectedPos).hidden;
		selectedPos = this.pos;
		selectedPos = -1;	
		estadosCombos('visible');
	}
}

// TextBox OnKeyUp
AutoCompleteTextBox.prototype.OnKeyUp = function(oEvent)
{
  //check for the proper location of the event object
  if (!oEvent) 
  {
    oEvent = window.event;
  }    
  this.AutoCompleteTextBox.TextBox_KeyUp(oEvent);      
}
AutoCompleteTextBox.prototype.TextBox_KeyUp = function(oEvent)
{
  var iKeyCode = oEvent.keyCode;
  /*if( iKeyCode == 8 )
  {
    this.Div.innerHTML = '';
		this.Div.style.display = 'none';
		return;
  }
  else*/

  if (iKeyCode==40 || iKeyCode==38)
  { 

	if(selectedPos>-1)
	{
		if( this.TextBox.AutoCompleteTextBox.ListItemClass.length > 0 )
		{
		document.getElementById(this.Div.id + "_" + selectedPos).className = this.TextBox.AutoCompleteTextBox.ListItemClass;
		}
		else
		{
		document.getElementById(this.Div.id + "_" + selectedPos).style.backgroundColor = 'white';
		document.getElementById(this.Div.id + "_" + selectedPos).style.color = 'black';
		}
	}
	
	if (iKeyCode==40 && selectedPos<(maxPos-1)) selectedPos++;
	if (iKeyCode==38 && selectedPos>0) selectedPos--;
	
	//odiv = document.getElementById(this.Div.id + "_" + pos);
	if( this.TextBox.AutoCompleteTextBox.ListItemHoverClass.length > 0 )
	document.getElementById(this.Div.id + "_" + selectedPos).className = this.TextBox.AutoCompleteTextBox.ListItemHoverClass;
	else
	{
	document.getElementById(this.Div.id + "_" + selectedPos).style.backgroundColor = 'black';
	document.getElementById(this.Div.id + "_" + selectedPos).style.color = 'white';
	}
	

	selectedTxt = document.getElementById(this.Div.id + "_" + selectedPos).innerHTML;
  }
  else 
  {
	selectedPos = -1;
	if (iKeyCode==13)
	{
		this.AutoSuggest(selectedTxt.split(','));
		this.Div.style.display='none';
		estadosCombos('visible');
	}
	else
	{
		if( iKeyCode == 16 || iKeyCode == 20 )
		{
			this.DoAutoSuggest = false;
		}
		else if (iKeyCode == 8)
		{
			this.DoAutoSuggest = false;
		}
		else if ( iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) 
		{
			return;
		}
		else
		{
			this.DoAutoSuggest = false;
		}
	  
		var txt = this.TextBox.value;
		if( txt.length > 1 )
		{
		this.Cbo.DoCallBack(this.TextBox.name, txt);
		}
		else
		{
		this.Div.innerHTML = '';
			this.Div.style.display = 'none';
			estadosCombos('visible');
			this.Cbo.AbortCallBack();
		}
	}
  }
 }

AutoCompleteTextBox.prototype.Cbo_Complete = function(responseText, responseXML)
{ 
	while ( this.Div.hasChildNodes() )
		this.Div.removeChild(this.Div.firstChild);
			
	if(responseText == 'N/A') {
		this.TextBox.onkeydown = null;
		this.TextBox.onkeyup = null;
		this.TextBox.onblur = null;
		this.TextBox.onfocus = null;
		return;
	}
	
	// get all the matching strings from the server response
	var aStr = responseText.split('\n');
			
	// add each string to the popup-div
	var i, n = aStr.length;
	
	maxPos = n;
	
	if( n > 0 && responseText.length > 0 )
	{
	  for ( i = 0; i < n; i++ )
	  {
		  if (aStr[i].indexOf("|") == 3)
		  {
		  var oDiv = document.createElement('div');
		  this.Div.appendChild(oDiv);
		  try
		  {
		    oDiv.innerHTML  = aStr[i].split("|")[1];
		    oDiv.hidden     = aStr[i].split("|")[0];
		  }
		  catch(e)
		  {
		    this.Cbo_Error('405','Error','Text returned from Call Back was invalid');
		    return;
		  }
		  oDiv.noWrap       = true;
		  oDiv.style.width  = '100%';
		  oDiv.className    = this.ListItemClass;
		  oDiv.onmousedown  = AutoCompleteTextBox.prototype.Div_MouseDown;
		  oDiv.onmouseover  = AutoCompleteTextBox.prototype.Div_MouseOver;
		  oDiv.onmouseout   = AutoCompleteTextBox.prototype.Div_MouseOut;
		  oDiv.AutoCompleteTextBox = this;
		  oDiv.pos = i;
		  oDiv.id = this.Div.id + '_' + i;
		  }
	  }
	  this.Div.style.display = 'block';
	  estadosCombos('hidden');
	  
	  if( this.DoAutoSuggest == true )
	    this.AutoSuggest( aStr );
	}
	else
	{
		this.Div.innerHTML = '';
		this.Div.style.display='none';
		estadosCombos('visible');
	}
}

AutoCompleteTextBox.prototype.Cbo_Error = function(status, statusText, responseText)
{
	alert('CallBackObject Error: status=' + status + '\nstatusText=' + statusText + '\n' + responseText);
}

AutoCompleteTextBox.prototype.Div_MouseDown = function()
{
	this.AutoCompleteTextBox.TextBox.value = this.innerHTML;
	this.AutoCompleteTextBox.txtHidden.value = this.hidden;
	selectedPos = this.pos;
}

AutoCompleteTextBox.prototype.Div_MouseOver = function()
{
  if( this.AutoCompleteTextBox.ListItemHoverClass.length > 0 )
	  this.className = this.AutoCompleteTextBox.ListItemHoverClass;
	else
	{
	  this.style.backgroundColor = 'black';
    this.style.color = 'white';
	}
	
	    if(selectedPos>-1)
    {
		if( this.AutoCompleteTextBox.ListItemClass.length > 0 )
		{
		document.getElementById(this.AutoCompleteTextBox.Div.id + "_" + selectedPos).className = this.AutoCompleteTextBox.ListItemClass;
		}
		else
		{
		document.getElementById(this.AutoCompleteTextBox.Div.id + "_" + selectedPos).style.backgroundColor = 'white';
		document.getElementById(this.AutoCompleteTextBox.Div.id + "_" + selectedPos).style.color = 'black';
		}
	}

	
	selectedPos = this.pos;
	selectedTxt = this.innerHTML;
}

AutoCompleteTextBox.prototype.Div_MouseOut = function()
{
  if( this.AutoCompleteTextBox.ListItemClass.length > 0 )
	  this.className = this.AutoCompleteTextBox.ListItemClass;
	else
	{
	  this.style.backgroundColor = 'white';
    this.style.color = 'black';
	}
}

AutoCompleteTextBox.prototype.AutoSuggest = function(aSuggestions /*:array*/) 
{
  if (aSuggestions.length > 0) 
  {
    this.TypeAhead(aSuggestions[0]);
  }
}

AutoCompleteTextBox.prototype.TypeAhead = function( sSuggestion /*:string*/)
{
  if( this.TextBox.createTextRange || this.TextBox.setSelectionRange)
  {
    var iLen = this.TextBox.value.length; 
    this.TextBox.value = sSuggestion; 
    this.SelectRange(iLen, sSuggestion.length);
  }
}

AutoCompleteTextBox.prototype.SelectRange = function (iStart /*:int*/, iLength /*:int*/) 
{
  //use text ranges for Internet Explorer
  if (this.TextBox.createTextRange) 
  {
    var oRange = this.TextBox.createTextRange(); 
    oRange.moveStart("character", iStart); 
    oRange.moveEnd("character", iLength - this.TextBox.value.length);      
    oRange.select();
   
  //use setSelectionRange() for Mozilla
  } 
  else if (this.TextBox.setSelectionRange) 
  {
      this.TextBox.setSelectionRange(iStart, iLength);
  }     

  //set focus back to the textbox
  this.TextBox.focus();      
}

//----------------END AutoCompleteTextBox ------------------------
             
function GetCoords(obj /*:object*/) 
{   
  var newObj = new Object();
  newObj.x = obj.offsetLeft;
  newObj.y = obj.offsetTop;
  theParent = obj.offsetParent;
 while(theParent != null)
  {
    newObj.y += theParent.offsetTop;
    newObj.x += theParent.offsetLeft;
    theParent = theParent.offsetParent;
  }

  return newObj;
}
function estadosCombos(estado)
{
	document.getElementById('SearchObj_ddlViajeTipo').style.visibility = estado;
	document.getElementById('SearchObj_ddlVueloTipo').style.visibility = estado;
	document.getElementById('SearchObj_ddlClass').style.visibility = estado;
	document.getElementById('SearchObj_ddlAdults').style.visibility = estado;
	document.getElementById('SearchObj_ddlYoung').style.visibility = estado;
	document.getElementById('SearchObj_ddlBabies').style.visibility = estado;
}
function setLoading(obj, estado){
	try{ 
		obj.style.visibility= estado ;
	} catch(e){}
}
