// page_MSDS.js - javascript for the MSDS search page (MSDS).
//
// Actions:	Search MSDS by number
//			(search key changed)
//		Cancel search by number
//			(empty search key)
//		Search MSDS by description
//			(search key changed)
//		Cancel search by description
//			(empty search key)
//		Select description
//			(from search list)
//
//	NOTE: In the case where a callback is dynamically attached to an object, the object
//	becomes the 'this' pointer for the method.  Thus several calls to displayState() are
//	referenced using the global object pointer 'cs' rather than the this.  In these
//	specific cases, the 'teuker' object assigns select/unselect methods to the onclick()
//	event handler of dynamically created divs.

function MSDSPageObject ()
{}

MSDSPageObject.prototype.vars =
{
	timerID: null,
	timerRunning: false
};

//	Display state manages the visibility and display status of all components of the page.
//	displayState(0) is the default for the page and is called by init() to ensure the
//	status is properly initialized.  Lower level components are managed by calls to
//	page specific display methods.  (This page has no lower level components)

/* IDs
id="msds_regionMSDSSelect"
id="msds_lblPartNumSearch"
id="msds_txtPartNumSearch"
id="msds_lblMSDSNumSearch"
id="msds_txtMSDSNumSearch"
id="msds_lblMSDSDescSearch"
id="msds_txtMSDSDescSearch"
id="msds_MSDSSuggestions"
*/
MSDSPageObject.prototype.displayState = function(num)	// No-op for this page
{
	trace('msds.displayState('+num+')');
}

MSDSPageObject.prototype.stopTimer = function()
{
	if (this.vars.timerRunning)
		clearTimeout(this.vars.timerID);
	this.vars.timerRunning = false;
};

/* Events
onkeyup="msds.xxxPartNumSearch();"
onkeyup="msds.xxxMSDSNumSearch();"
onkeyup="msds.xxxMSDSDescSearch();"
onclick="msds.PartMSDSSearch();"
*/

MSDSPageObject.prototype.PartMSDSSearch = function()		// Look for MSDS by part number
{
	var textNode = document.getElementById("msds_txtPartNumSearch");
	var textVal = util.trim(textNode.value);
//alert('part number search:'+textVal);
	xajax_searchPartNumberForMSDS(textVal);
};

MSDSPageObject.prototype.partMSDSLink = function(arr)		// Populate part number to MSDS link field
{
//alert('In partMSDSLink');
	var mesgNode = document.getElementById("msds_divPartSearchError");
	var linkNode = document.getElementById("msds_linkPartMSDS");

	if (arr[1] == '')
	{
		linkNode.style.visibility = "hidden";
		mesgNode.style.visibility = "visible";
//		linkNode.style.display = "none";
//		mesgNode.style.display = "inline";
		mesgNode.innerHTML = arr[0];
	}
	else
	{
		mesgNode.style.visibility = "hidden";
		linkNode.style.visibility = "visible";
//		mesgNode.style.display = "none";
//		linkNode.style.display = "inline";
		linkNode.innerHTML = arr[0];
		linkNode.href = "docs/MSDS/"+arr[1];
	}
};

MSDSPageObject.prototype.xxxPartNumSearch = function()		// Show suggested part - actual query delayed by timer
{
	// Clear any timer.  On receipt of a character, set a delay
	// timer to submit the search.  If no character is received
	// during this interval, the search is submitted.  This
	// suppresses submission while the user is actively entering
	// a search filter.
//trace('msds.PartNumSearch()');
	this.stopTimer();
        this.vars.timerRunning = true;
        this.vars.timerID = self.setTimeout("msds.submitFilteredPartNumSearch()", 400);	// Hold the request for 400 mSecs.
};

MSDSPageObject.prototype.submitFilteredPartNumSearch = function()		// Timer base submission of search
{
	var textNode = document.getElementById("msds_txtPartNumSearch");
	var textVal = util.trim(textNode.value);
trace('part number search:'+textVal);
	document.getElementById("msds_txtMSDSNumSearch").value = '';
	document.getElementById("msds_txtMSDSDescSearch").value = '';
	xajax_searchPartNumbers(textVal);
};

MSDSPageObject.prototype.xxxMSDSNumSearch = function()		// Show suggested MSDS - actual query delayed by timer
{
	// Clear any timer.  On receipt of a character, set a delay
	// timer to submit the search.  If no character is received
	// during this interval, the search is submitted.  This
	// suppresses submission while the user is actively entering
	// a search filter.
//trace('msds.MSDSNumSearch()');
	this.stopTimer();
        this.vars.timerRunning = true;
        this.vars.timerID = self.setTimeout("msds.submitFilteredMSDSNumSearch()", 400);	// Hold the request for 400 mSecs.
};

MSDSPageObject.prototype.submitFilteredMSDSNumSearch = function()		// Timer base submission of search
{
	var textNode = document.getElementById("msds_txtMSDSNumSearch");
	var textVal = util.trim(textNode.value);
trace('number search:'+textVal);
	document.getElementById("msds_txtPartNumSearch").value = '';
	document.getElementById("msds_txtMSDSDescSearch").value = '';
	xajax_searchMSDSNumbers(textVal);
};

MSDSPageObject.prototype.xxxMSDSDescSearch = function()		// Show suggested MSDS - actual query delayed by timer
{
//trace('msds.MSDSDescSearch()');
	this.stopTimer();
        this.vars.timerRunning = true;
        this.vars.timerID = self.setTimeout("msds.submitFilteredMSDSDescSearch()", 400);	// Hold the request for 400 mSecs.
};

MSDSPageObject.prototype.submitFilteredMSDSDescSearch = function()		// Timer base submission of search
{
	var textNode = document.getElementById("msds_txtMSDSDescSearch");
	var textVal = util.trim(textNode.value);
trace('desc search:'+textVal);
	document.getElementById("msds_txtPartNumSearch").value = '';
	document.getElementById("msds_txtMSDSNumSearch").value = '';
	xajax_searchMSDSDescriptions(textVal);
};

MSDSPageObject.prototype.xxxunselectMSDS = function()		// Cancel search based on empty search key
{
	trace('in msds.unselectMSDS()');
};

MSDSPageObject.prototype.selectMSDS = function()		// Select customer from search list
{
	// This is a callback function which is attached to a DOM object by Teuker.
	// The 'this' pointer refers to the DOM object, not to this class.

	trace('in msds.selectMSDS()'+this.id);
	alert('Selected MSDS with UID:'+this.id+'.  Will use to display MSDS sheet for printing.');
//	xajax_selectMSDS(this.id);				// Tell app about selected (this clears the autosuggest)
};
/*
MSDSPageObject.prototype.openMSDSInNewWindow = function(frm)
{
	// open a blank window
	var aWindow = window.open('', 'TableAddRowNewWindow',
	 'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
	 
	// set the target to the blank window
	frm.target = 'TableAddRowNewWindow';
	
	// submit
	frm.submit();
}; */

var msds = new MSDSPageObject;
