// Set the text input box named "part" and submit the search form
function CheckQuantityViaStockSearch(paramPart, FRM)
{
    var f=document.forms[FRM];
    f.part.value=paramPart;
    f.submit();
}

// Grab the search terms from the various search engines.
// These search terms can then be used on our web page to do
//	things like preload the part number the user might want to search for.
function ExtractSearchTerms( referrer )
{
	referrer = decodeURIComponent(referrer);
	var query = null;
	
	if( referrer.match(/^http:\/\/(www\.)?alltheweb.*/i))
	{
		if( referrer.match( /q=/) )
			query = referrer.replace(/^.*q=([^&]+)&?.*$/i, '$1');
	}
	else if( referrer.match(/^http:\/\/(www)?\.?google.*/i) )
	{
		if( referrer.match( /q=/) )
			query = referrer.replace(/^.*q=([^&]+)&?.*$/i, '$1');
	}
	else if( referrer.match(/^http:\/\/search\.lycos.*/i) )
	{
		if( referrer.match( /query=/) )
			query = referrer.replace(/^.*query=([^&]+)&?.*$/i, '$1');
	}
	else if( referrer.match(/^http:\/\/search\.msn.*/i) )
	{
		if( referrer.match( /q=/) )
			query = referrer.replace(/^.*p=([^&]+)&?.*$/i, '$1');
	}
	else if( referrer.match(/^http:\/\/search\.yahoo.*/i) )
	{
		if( referrer.match( /p=/) )
			query = referrer.replace(/^.*p=([^&]+)&?.*$/i, '$1');
	}
	
	// Turn "+" separated string into comma separated string
	if( query )
	{
		query = query.replace(/\'|"/, '');
		query = query.split(/[\s,\+\.]+/);
	}
	
	return query;
}

// Set the text input box named "part" to the search term(s) gathered from HTTP_REFERRER
function SetPartNumberFromSearchTerms(FRM, referrer)
{
	var f = document.forms[FRM];
	f.part.value = ExtractSearchTerms( 'http://www.google.com/search?sourceid=ie7&rls=com.microsoft:en-US&ie=utf8&oe=utf8&q=jsfdareferrer' );
	//
	// Works!:        document.links[6].href = "http://microsoft.com";
	// Works!:        document.getElementById('idAnchorPartNumber').href = ExtractSearchTerms( 'http://www.google.com/search?sourceid=ie7&rls=com.microsoft:en-US&ie=utf8&oe=utf8&q=jsfdareferrer' );
	// Works!:        document.getElementById("idAnchorPartNumber").innerHTML = "inner html";
	// Doesn't work:  document.links["nameA"].href = "http://gotuit.com";
	// Doesn't work:  f.nameAnchorPartNumber.href.value = "http://microsoft.com";
	// Doesn't work:  f.nameAnchorPartNumber.href = "http://microsoft.com";
	if( referrer == null  ||  referrer == '')
	    f.part.value = ''
	else
	{
		// Set the text input box to the part # from HTTP_REFERRER
		f.part.value = ExtractSearchTerms( referrer );
		
		// For some reason, we need to go to the document and getElementById to change the text of the anchor tag
		document.getElementById('idAnchorPartNumber').innerHTML = ExtractSearchTerms( referrer );
		document.getElementById('idAnchorRfq').innerHTML = ExtractSearchTerms( referrer );
		
		// Also send the part number as a query string parameter to RFQ page
		document.getElementById('idAnchorPartNumber').href = "/rfq.aspx?partNumber=" + ExtractSearchTerms( referrer );
		document.getElementById('idAnchorRfq').href = "/rfq.aspx?partNumber=" + ExtractSearchTerms( referrer );
	}
}