//____________________________________________________
//if referred from search engine use search string 
//to find part on page searched for and display message
//____________________________________________________
function refer(){
	if(! document.getElementById)	//quit logic if browser doesn's support it
		return;

	var strQuery = getQuery();
	
	if(strQuery != "") {
		
		//pull raw html part list from page
		var parList = document.getElementById("parHiddenParts").innerHTML;
		
		//if not a parts page but a index page for company exit script
		if(/<table>|<tr>|<td>|<a/i.test(parList))
			return;
			
		//diff browsers parse html elements differently, IE makes caps, Mozz makes non caps
		var arrParts = (parList.indexOf("<BR>") != -1) ? parList.split("<BR>") : parList.split("<br>");
		arrParts = cleanArray(arrParts);
	    
		//loop through list of part names on page, if match found add to match array
		var strMatch = null;
	    
		for(var l = 0; l < arrParts.length; l ++) {
			if(new RegExp(strQuery, "i").test(arrParts[l])) {
				strMatch = arrParts[l];
				break;
			}	
		}
		
		if(strMatch == null)
			strMatch = strQuery;
			
	    //check if part has matching picture, is does make it appear with description
	    var arrPartImgs = new Array(["282081-1", "PLUG, RUBBER<br>FIAT P/N: 12309185", "282081_1.jpg"], 
									["12309185", "PLUG, RUBBER<br>FIAT P/N: 12309185", "282081_1.jpg"], 
									["282105-1", "3 POS SEALED HSG, TAB<br>FIAT P/N: 12453287", "282105_1.jpg"], 
									["12453287", "3 POS SEALED HSG, TAB<br>FIAT P/N: 12453287", "282105_1.jpg"],
									["282079-2", "1 POS SEALED HSG, PLUG<br>FIAT P/N: 12452487", "282079_2.jpg"], 
									["12452487", "1 POS SEALED HSG, PLUG<br>FIAT P/N: 12452487", "282079_2.jpg"],
									["282080-1", "2 POS SEALED HSG, PLUG<br>FIAT P/N: 12452587", "282080_1.jpg"], 
									["12452587", "2 POS SEALED HSG, PLUG<br>FIAT P/N: 12452587", "282080_1.jpg"]);
	    var objPart = null;
	    
	    for(i = 0; i < arrPartImgs.length; i ++) {
			objPart = arrPartImgs[i];
			
			if(objPart[0] == strMatch) {
				document.getElementById("partImage").src = "../images/parts/" + objPart[2];
				document.getElementById("partDesc").innerHTML = "<p><strong>" + objPart[1] + "</strong></p>";
				break;
			}
	    }
	    
		//turn div visible that has text box button and text,
		//populate text box, add message
		//---------------------------------------------------
		//populate hidden field for post to "parts to sell" page
		document.getElementById("searchterms").value = strMatch;
		
		var spaPartText = document.getElementById("spaPartText");
		
		if(spaPartText == null)
			return;
		
		if(/###REF_PART###/.test(spaPartText.innerHTML))
			spaPartText.innerHTML = spaPartText.innerHTML.replace("###REF_PART###", strMatch);
		if(/###REF_PART_LINK###/.test(spaPartText.innerHTML))
			spaPartText.innerHTML = spaPartText.innerHTML.replace("###REF_PART_LINK###", "<a href=\"#\" onclick=\"return SubmitForm();\" class=\"partLink\">" + strMatch + "</a>");
			
		spaPartText.style.display = "inline";
		//---------------------------------------------------
    }
}

//____________________________________________________
//if the search engine is used return the query string,
//otherwise return null
//____________________________________________________
function getQuery() {
	var strRef = decodeURI(document.referrer);
	var strQuery = null;
	//pull out manufacturer name from the part list header
	var strManu = document.getElementById("ttdListTitle").firstChild.innerHTML; 
	strManu = strManu.substring(0, strManu.indexOf(","));	 
	//create array of words in welcome text to add to ignore word list
	var arrWelWord = document.getElementById("spaWelCopy").innerHTML.split(" ");
	
	//build list of all words that should be exclued from search string 
	//--------------------------------------------------
	var arrIgnore = new Array("connectorpeople", 
								"connectors", 
								"connector", 
								"people", 
								"nti-the connector people", 
								"winchester", 
								"NTI",
								"AMP",
								"Tyco",
								"Molex",
								"FCI Berg",
								"3M",
								"ITW Pancon Panduit",
								"Samtec",
								"Augat",
								"Foxconn",
								"JST",
								"T&B",
								"electronic",
								"independent distributor",
								"electrical",
								"passive components",
								"excess inventory",
								"D-Subminiature",
								"Headers",
								"Sockets",
								"Receptacles",
								"manufacturer",
								"cross reference",
								"PLCC",
								"fine pitch",
								"mod jack",
								"obsolete",
								"hard to find",
								"components",
								"is",
								"an",
								"independent",
								"stocking",
								"distributor", 
								"of",
								"electrical",
								"and", 
								"electronic", 
								"We", 
								"have",
								"broad",
								"inventory",
								"name", 
								"brand", 
								"commercial",
								"in", 
								"stock", 
								"at", 
								"substantial", 
								"savings", 
								"over", 
								"franchised",
								"pricing", 
								"buy",
								"sell",
								"all",
								"Solderless", 
								"Terminals",
								"Splices",
								"Modular", 
								"Jacks",
								"Plugs", 
								"D-Subminiature",
								"Centronics",
								"Edge", 
								"Card", 
								"Crimp",
								"Housings",
								"Soft",
								"Shell", 
								"CPC-Circular",
								"Plastic",
								"SIP", 
								"DIP", 
								"PGA", 
								"SIMM", 
								"DIMM",
								"PCB",
								"Ribbon",
								"Cable", 
								"IDC-Insulation", 
								"Displacement",
								"Transition", 
								"Circular", 
								"Mini", 
								"DIN", 
								"FFC", 
								"FPC", 
								"Flexible", 
								"Film",
								"SMT-Surface", 
								"Mount", 
								"Dip", 
								"Switches",
								"RF",
								"BNC", 
								"TNC", 
								"SMA", 
								"SMB", 
								"Fiber", 
								"Optic", 
								"Memory", 
								"PCMCIA", 
								"Backplane", 
								"USB", 
								"I\/O", 
								"Board-to-Board",
								"Wire-to-Board",
								"Any", 
								"use", 
								"or", 
								"access", 
								"this",
								"site", 
								"constitutes",
								"acceptance", 
								"our", 
								"Terms", 
								"Privacy", 
								"Statement",
								"HOME",
								"ABOUT",
								"ABOUT US",
								"REGISTRATION",
								"LINKS",
								"product",
								"listing");	
								
	//add current pages manufacturer name to ignore array
	arrIgnore.push(strManu);
	
	//add words from welcome copy to the ignore array
	for(var i = 0; i < arrWelWord.length; i ++)
		//don't add if has html elements, don't add if has regular expression reserved words
		if(! /<(\w|\s)|(\w|\s|\d|'|")>|<\/(\w|\s)|(\w|\s|\d|'|")\/>/i.test(arrWelWord[i]))	
			if(! /\^|\$|\.|\*|\+|\?|\=|\!|\:|\||\\|\/|\(|\)|\[|\]|\{|\}/.test(arrWelWord[i]))	
			arrIgnore.push(arrWelWord[i]); 
	//--------------------------------------------------
	
	//pull out of referrer search terms based on type of search engine used
	if(/google.com/i.test(strRef) && /q=([^&]+)/i.test(strRef)) {		//if from google
		strQuery = strRef.substring(strRef.indexOf("q=") + 2);
		strQuery = (strQuery.indexOf("&") != -1) ? strQuery.substring(0, strQuery.indexOf("&")) : strQuery;
    }else if(/yahoo.com/i.test(strRef) && /p=([^&]+)/i.test(strRef)) {	//if from yahoo
		strQuery = strRef.substring(strRef.indexOf("p=") + 2);
		strQuery = (strQuery.indexOf("&") != -1) ? strQuery.substring(0, strQuery.indexOf("&")) : strQuery;
    }else if(/msn.com/i.test(strRef) && /q=([^&]+)/i.test(strRef)) {	//if from msn
		strQuery = strRef.substring(strRef.indexOf("q=") + 2);
		strQuery = (strQuery.indexOf("&") != -1) ? strQuery.substring(0, strQuery.indexOf("&")) : strQuery;
	
	
	/*}else if(/q=([^&]+)/i.test(strRef)) {		//FOR DEBUGGING ONLY!
		strQuery = strRef.substring(strRef.indexOf("q=") + 2);
		strQuery = (strQuery.indexOf("&") != -1) ? strQuery.substring(0, strQuery.indexOf("&")) : strQuery;
    */
    
    }else {																//make empty string if no search query
		strQuery = "";
    }
    
    
    //some engines use "+" to replace " " chars instead of escaping them, fix it
	strQuery = strQuery.replace(/\+/g, " ");	
	
	//The idea is to eliminate partial matches of ignore words as much as possible. 
	//For accuracy logic looks for right & left space padded words,
	//then left padded words at end of string, then right padded words at start of string. In this order 
	//everything seems to be caught without incorrect truncating from partial matches
	//--------------------------------------------------
	var result = null;
	var expItem = null;
	
	//remove any words padded on left & right with spaces
	for(var i = 0; i < arrIgnore.length; i ++) {
		expItem = new RegExp("[\\s]+" + arrIgnore[i] + "[\\s]+", "ig");	
		
		while(( result = expItem.exec(strQuery) ) != null)
			strQuery = strQuery.substring(0, result.index) + strQuery.substring(expItem.lastIndex - 1);	
	}
	
	//remove any words at end of string padded left with spaces
	for(var i = 0; i < arrIgnore.length; i ++) {
		expItem = new RegExp("[\\s]+" + arrIgnore[i] + "$", "ig");	
		
		while(( result = expItem.exec(strQuery) ) != null)
			strQuery = strQuery.substring(0, result.index);	
	}	
		
	//remove any words at beginning of string padded left with spaces
	for(var i = 0; i < arrIgnore.length; i ++) {
		expItem = new RegExp("^" + arrIgnore[i] + "[\\s]+", "ig");
		
		while(( result = expItem.exec(strQuery) ) != null)
			strQuery = strQuery.substring(expItem.lastIndex);
	}	
	//--------------------------------------------------			

	return strQuery;	
}

//____________________________________________________
//truncate leading/trailing spaces, remove empty array elements, 
//split concated items into seperate items,
//return array clean
//____________________________________________________
function cleanArray(prmList) {
	var arrNew = new Array();
	var arrMatch = new Array();
	var j = 0;
	
	for(var i = 0; i < prmList.length; i ++) {
		if(prmList[i] != "") {						
			/*
			if(/\s+$/.test(prmList[i].toString()))	//if array element has leading white space remove it
				prmList[i] = prmList[i].replace(/\s+$/, "");
				
			if(/^\s+/.test(prmList[i].toString()))	//if array element has trailing white space remove it
				prmList[i] = prmList[i].replace(/^\s+/, "");
				
			//!!FF bug!!, if only one space char in word it makes it 2 spaces intead of 1 space,
			//if array element has single double space chars together remove it	
			if(/\s\s/.test(prmList[i].toString()))  
				prmList[i] = prmList[i].replace(/\s\s/, "");	
			*/
			
			//each item in current array is actual 2 items concated together, use 
			//this logic to figure out where to split the string into 2 diff items.	
			arrMatch = prmList[i].match(/[^\s]+/g);
			
			if(arrMatch != null) {
				if(arrMatch.length == 2) {			//names made up of 1 part each
					arrNew[j] = arrMatch[0];
					j ++;
					arrNew[j] = arrMatch[1];
					j ++;
				}else if(arrMatch.length == 4) {	//names made up of 2 parts each
					arrNew[j] = arrMatch[0] + " " + arrMatch[1];
					j ++;
					arrNew[j] = arrMatch[2] + " " + arrMatch[3];
					j ++;
				}else if(arrMatch.length == 6) {	//names made up of 3 parts each
					arrNew[j] = arrMatch[0] + " " + arrMatch[1] + " " + arrMatch[2];
					j ++;
					arrNew[j] = arrMatch[3] + " " + arrMatch[4] + " " + arrMatch[5];
					j ++;
				}else if(arrMatch.length == 8) {	//names made up of 4 parts each
					arrNew[j] = arrMatch[0] + " " + arrMatch[1] + " " + arrMatch[2] + " " + arrMatch[3];
					j ++;
					arrNew[j] = arrMatch[4] + " " + arrMatch[5] + " " + arrMatch[6] + " " + arrMatch[7];
					j ++;
				}		
			}
		}
			
	}

	return arrNew;
}

function SubmitForm() {
	document.form1.submit();
	return false;
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;

function preloadImages() {
	if (document.images) {
		FastQuote_over = newImage("../images/home/FastQuote-over.gif");
		FastQuote_down = newImage("../images/home/FastQuote-down.gif");
		PartsToSell_over = newImage("../images/home/PartsToSell-over.gif");
		PartsToSell_down = newImage("../images/home/PartsToSell-down.gif");
		parts_fast_spacer_FastTracking_down = newImage("../images/home/parts-fast_spacer-FastTrack.gif");
		FastTracking_over = newImage("../images/home/FastTracking-over.gif");
		FastTracking_down = newImage("../images/home/FastTracking-down.gif");
		fast_login_spacer_FastTracking_down = newImage("../images/home/fast-login_spacer-FastTrack.gif");
		Login_over = newImage("../images/home/Login-over.gif");
		Login_down = newImage("../images/home/Login-down.gif");
		NewUser_over = newImage("../images/home/NewUser-over.gif");
		NewUser_down = newImage("../images/home/NewUser-down.gif");
		AboutUs_FastTracking_down = newImage("../images/home/AboutUs-FastTracking_down.gif");
		AboutUs_over = newImage("../images/home/AboutUs-over.gif");
		AboutUs_down = newImage("../images/home/AboutUs-down.gif");
		ContactUs_over = newImage("../images/home/ContactUs-over.gif");
		ContactUs_down = newImage("../images/home/ContactUs-down.gif");
		Calendar_over = newImage("../images/home/Calendar-over.gif");
		Calendar_down = newImage("../images/home/Calendar-down.gif");
		preloadFlag = true;
	}
}