////////////////////////////////////////////////////////////////////////////////
// Dynamic Search functionality, written by Piotr Pytlik
//
// Copyright (c) 2005-2006, Double P Software. All Rights Reserved.
//
// Permission must be obtained from Double P Software in order to use this code!
// Permission is granted to Indigo Instruments for use on http://www.indigo.com
////////////////////////////////////////////////////////////////////////////////
//   WHEN	VER	WHO/WHAT/WHERE/WHY
// ========	===	================================================================
// 20051120	1.0	PP/Written initially to test the idea of dynamic presentation.
// 20061209	2.0	PP/Optimized with 1/2 sec timeout, showing Hourglass...
////////////////////////////////////////////////////////////////////////////////
var _dbgCount = 0;
var _reqXml = null;
var _reqUrl = null;
var _toSend = null;

////////////////////////////////////////////////////////////////////////////////
function SearchInit()
{
	_GetQuery().focus();
	SearchFocus();
}

////////////////////////////////////////////////////////////////////////////////
function SearchQuery( key, opts )
{
	if( _toSend )
		window.clearTimeout( _toSend );
	if( key )
	{
		_reqUrl = "Results.aspx?query="+key;
		if( opts )
		{
			for( var i = 0; i < opts.length; i++ )
				if( opts[i].checked )
					_reqUrl += "&opt=" + opts[i].value;
		}
		_toSend = window.setTimeout( "_SearchSend()", 500 );
	}
	else
	{
		_HideAuto();
		_reqUrl = null;
		_toSend = null;
	}
}

////////////////////////////////////////////////////////////////////////////////
function SearchFocus()
{
	_HideAuto();
	document.forms[0].query.select();
	SearchQuery( document.forms[0].query.value );
}

////////////////////////////////////////////////////////////////////////////////
function SearchBlur()
{
	_HideAuto();
}

////////////////////////////////////////////////////////////////////////////////
function ShowSearchImg( src )
{
	var elDiv = document.getElementById( "elSearchImgDiv" );
	elDiv.innerHTML = "<img src='" + src + "?w=320&h=200'>";	// So it's properly reloaded for each
	//var elImg = document.getElementById( "elSearchImg" );
	//elImg.src = src + "?w=320&h=200";	// This would show previous image first, while loading new
	elDiv.style.display = "block";
}

////////////////////////////////////////////////////////////////////////////////
function HideSearchImg()
{
	var elDiv = document.getElementById( "elSearchImgDiv" );
	elDiv.style.display = "none";
}

////////////////////////////////////////////////////////////////////////////////
function _InitializeReq()
{
	try
	{
		_reqXml = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			_reqXml = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			_reqXml = null;
		}
	}
	if( !_reqXml && typeof XMLHttpRequest != "undefined" )
	{
		_reqXml = new XMLHttpRequest();
	}
}

////////////////////////////////////////////////////////////////////////////////
function _SearchSend()
{
return; // 2007/03/09 Stephan didn't want the drop-down, confuses people
	if( !_reqUrl )
		return;

	_InitializeReq();
	if( _reqXml != null )
	{
		if( _dbgCount > 0 ) alert( "Trying: " + _reqUrl ); _dbgCount--;

	   _HourglassStart();

		_reqXml.onreadystatechange = _ProcessSearch;
		_reqXml.open( "GET", _reqUrl, true );
		_reqXml.send( null );
	}
	else
		_HideAuto();
}

////////////////////////////////////////////////////////////////////////////////
function _ProcessSearch()
{
	if( _reqXml.readyState != 4 )
		return;

	// only if "OK"
	if( _reqXml.status == 200 )
	{
if( _dbgCount > 0 ) alert( "OK Response:\n\n" + _reqXml.responseText ); _dbgCount--;
		if( _reqXml.responseText.match( /^\s*$/ ) )	// Blank response (may contain spaces/nls...)
		{
			_GetAuto().innerHTML = _WrapError( "No Results found" );
			_ShowAuto();
		}
		else
		{
			_GetAuto().innerHTML = _reqXml.responseText;
			_ShowAuto();
		}
	}
	else 
	{
		_GetAuto().innerHTML =
			_WrapError( "There was a problem retrieving data:<br>" + _reqXml.statusText );
		_ShowAuto();
	}
}

////////////////////////////////////////////////////////////////////////////////
function _WrapError( sError )
{
	return "<div style='color:gray; padding: 8px;'>" + sError + "</div>";
}

////////////////////////////////////////////////////////////////////////////////
function _GetQuery()
{
	return document.forms[0].query;
}

////////////////////////////////////////////////////////////////////////////////
function _GetAuto()
{
	return document.getElementById("autocomplete");
}

////////////////////////////////////////////////////////////////////////////////
function _ShowAuto()
{
	if( document.layers ) document.layers["autocomplete"].visibility="show";
	else _GetAuto().style.display="block";
	_HourglassStop();
}

////////////////////////////////////////////////////////////////////////////////
function _HideAuto()
{
	if( document.layers ) document.layers["autocomplete"].visibility="hide";
	else _GetAuto().style.display="none";
}

////////////////////////////////////////////////////////////////////////////////
function _HourglassStart()
{
	//_GetQuery().style.background = "url (/Images/spinner.gif)";
	_GetQuery().className = "Spinner";
}
////////////////////////////////////////////////////////////////////////////////
function _HourglassStop()
{
	_GetQuery().className = "Query";	//.style.background = "";
}

////////////////////////////////////////////////////////////////////////////////
// TODO: Move the OOS logic into the caller (BuyNow control button?) so that
// it can present the Message in a tooltip as well as in an alert/confirm box
//
// See /Inc/iInitPage _StartLine()
////////////////////////////////////////////////////////////////////////////////
function DoBuyNow( sPn, sQtyEl, nQtyMul, isOOS, nADTR, dExpected, ezSyncUser )
{
	var dExpected = (dExpected == '0001/01/01') ? null : dExpected;

//alert( "ADTR = " + nADTR + "\nExpected = " + dExpected );
	var frm = document.forms[0];	//["BuyNow"];
	var qty = document.getElementById( sQtyEl );
	if( !frm || !qty )
	{
		alert( "Please report this problem to Indigo!\n\nYou may have forgotten to enter Qty or your browser is misbehaving." );
		return false;
	}

	if( isOOS )
	{
		if( ezSyncUser )	// ezSync users may order OOS items after warning.
		{
			var sOOSmsg = ezSyncUser + ",\n\nThis item is Low or Out Of Stock,\nYour order may be Delayed.";
			if( dExpected )
				sOOSmsg += "\n\nExpected after: " + dExpected;
			if( !confirm( sOOSmsg + "\n\n\nDo you still want to Order it?" ) )
				return false;
		}
		else				// All other users cannot Order since 2006/04/01
		{
			if( nADTR && nADTR < 10 )		// TODO: This is usually 7 but shouldn't be hard-coded
			{
				if( !confirm('Item is Low or Out Of Stock but will be available within 7 days.\n\n\nDo you still want to Order it?') )
					return false;
			}
			else
			{
				var sWhen = dExpected ? "after " + dExpected : "shortly";
				alert( "This item is Low or Out Of Stock.\nPlease try again " + sWhen );
				return false;
			}
		}
	}
	
//alert( "BuyNow for: " + sPn + "\nQty: " + qty.value + "\nMQ: " + nQtyMul );
	frm["Add"].value = sPn;
	frm["q"].value = qty.value;
	frm["QM"].value = nQtyMul || 1;
	frm.submit();
	return false;
}

////////////////////////////////////////////////////////////////////////////////

