//new TextboxFilterList("search", "divPopup", "", "divThickbox")
function TextboxFilterList(textBoxId, divId, divClass, thickboxId)
{
	// initialize member variables
	var oThis = this;
	var oText = document.getElementById(textBoxId);
	var oDiv  = document.getElementById(divId);
	this.Textbox = oText;
	this.Div = oDiv;
	this.Thickbox = thickboxId;
			
	// attach handlers to the Textbox
	oText.TextboxFilterList = this;
	oText.onkeyup = TextboxFilterList.prototype.OnKeyup;
	oText.onblur  = TextboxFilterList.prototype.OnBlur;
	oText.onfocus = TextboxFilterList.prototype.OnFocus;
		
	
	//oDiv.onmousedown = TextboxFilterList.prototype.Div_MouseDown
	
	
}

TextboxFilterList.prototype.DoAutoSuggest = false;

TextboxFilterList.prototype.ListItemClass = '';

TextboxFilterList.prototype.ListItemHoverClass = '';

// Textbox OnBlur
TextboxFilterList.prototype.OnBlur = function()
{
	this.TextboxFilterList.Textbox_Blur();
}

TextboxFilterList.prototype.Textbox_Blur = function()
{
	this.Div.style.display='none';
}

// Textbox OnKeyup
TextboxFilterList.prototype.OnKeyup = function(oEvent)
{
  //check for the proper location of the event object
  if (!oEvent) 
  {
    oEvent = window.event;
  }    
  this.TextboxFilterList.Textbox_Keyup(oEvent);      
}

TextboxFilterList.prototype.Textbox_Keyup = function(oEvent) {
    var iKeyCode = oEvent.keyCode;
    if ((iKeyCode != 13 & iKeyCode < 32 & iKeyCode != 16 & iKeyCode != 20) || (iKeyCode >= 33 & iKeyCode <= 46) || (iKeyCode >= 112 & iKeyCode <= 123)) {
        return;
    }
    else  // ( iKeyCode == 16 || iKeyCode == 20 || iKeyCode == 8 || anything else) 
    {
        this.DoAutoSuggest = false;  // true
    }

    var txt = trim(this.Textbox.value);
    //if (txt.length >= 3) {
        //var receiver = new FunctionInvoker(this);
        //WebForm_DoCallback('__Page',txt,receiver.invoke,this.Textbox.name,null,false);
        if (iKeyCode == 13) {
            WebSearch(txt);
        }        
    /*    
    }
    else {
        this.Div.innerHTML = '';
        this.Div.style.display = 'none';
    }*/
}

// Textbox OnFocus
TextboxFilterList.prototype.OnFocus = function()
{
	this.TextboxFilterList.Textbox_Focus();
}

TextboxFilterList.prototype.Textbox_Focus = function()
{
    //alert(this.Thickbox);
    //tb_show(this.Thickbox, this.Textbox.id);
}

TextboxFilterList.prototype.ReceiveServerData = function(responseText) {
    while (this.Div.hasChildNodes())
        this.Div.removeChild(this.Div.firstChild);

    //	// get all the matching strings from the server response
    //	var aStr = responseText.d.split('\n');
    //			
    //	// add each string to the popup-div
    //	var i, n = aStr.length;

    var i, n = responseText.length

   
}

TextboxFilterList.prototype.Show_Error = function(status, statusText, responseText)
{
  alert('Error: status=' + status + '\nstatusText=' + statusText + '\n' + responseText);
}

TextboxFilterList.prototype.Div_MouseDown = function(oEvent) {
    if (!oEvent) {
        oEvent = window.event; //oEvent.srcElement.href
        if (oEvent.srcElement != null && oEvent.srcElement.href.length > 0) {
            //this.TextboxFilterList.Textbox_Blur();
            window.location.href = oEvent.srcElement.href;
        }
    }
    //this.TextboxFilterList.Textbox.value = this.innerHTML;
}

TextboxFilterList.prototype.Div_MouseOver = function()
{
    if( this.TextboxFilterList.ListItemHoverClass.length > 0 )
	    this.className = this.TextboxFilterList.ListItemHoverClass;
    else
	{
	    this.style.backgroundColor = 'black';
        this.style.color = 'white';
	}
}

TextboxFilterList.prototype.Div_MouseOut = function()
{
  if( this.TextboxFilterList.ListItemClass.length > 0 )
	  this.className = this.TextboxFilterList.ListItemClass;
	else
	{
	  this.style.backgroundColor = 'white';
    this.style.color = 'black';
	}
}

TextboxFilterList.prototype.AutoSuggest = function(aSuggestions /*:array*/) 
{
  if (aSuggestions.length > 0) 
  {
    this.TypeAhead(aSuggestions[0]);
  }
}

TextboxFilterList.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);
  }
}

TextboxFilterList.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();      
}
             
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;
  }
  */
  //alert(newObj.x + " : " +newObj.y);
  return newObj;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function FunctionInvoker(me) {
  this.me = me;
  this.invoke = function(responseData) {
    me.ReceiveServerData(responseData);
  }
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

// From thickbox: http://jquery.com/demo/thickbox/

function tb_show(thickboxid, txtboxid) {
	try {

		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");				
				$("#TB_overlay").click(tb_remove);
			}
		}
		
		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}
									
		PosInfo = GetCoords(document.getElementById(thickboxid));

		if($("#TB_window").css("display") != "block"){
            $("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal'></div>");	
        }
		else{//this means the window is already up, we are just loading new content via ajax
			$("#TB_ajaxContent")[0].scrollTop = 0;
		}
//	    alert($("#TB_ajaxContent").css("width"));
		
		$("#TB_ajaxContent").append($('#' + thickboxid).children());
		$("#TB_window").unload(function () {
			$('#' + thickboxid).append($("#TB_ajaxContent").children()); // move elements back when you're finished
		});
		
		$('#' + txtboxid).focus(); 		
		tb_position();
		
		$("#TB_window").css({display:"block"}); 

		document.onkeyup = function(e){ 	
			if (e == null) { // ie
				keycode = event.keyCode;
			} else { // mozilla
				keycode = e.which;
			}
			if(keycode == 27){ // close
				tb_remove();
			}	
		};		
	} catch(e) {
		//nothing here
	}
}

function tb_remove() {
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_position() {
$("#TB_window").css({left: PosInfo.x + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
        $("#TB_window").css({top: PosInfo.y + 'px'});
	}
}

function tb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

function WebSearch(txt) {
    document.cookie = 'postbackcookie=';
    //   if (document.forms[0]!=null)
    //	document.forms[0].action = "SearchPage.aspx?searchtext=" + searchText;
    window.location.href = "/WebSearch.aspx?searchtext=" + txt;
}