// JavaScript Document

numResults = 0;

nl2other = function (text,otherSymbol){
	text = escape(text);
	if(text.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g ;
	}else if(text.indexOf('%0A') > -1){
		re_nlchar = /%0A/g ;
	}else if(text.indexOf('%0D') > -1){
		re_nlchar = /%0D/g ;
	}else{
		return text;	
	}
	return unescape( text.replace(re_nlchar,otherSymbol) );
}

getSearchResults = function(page,res){
	//hide the request form so that the results are above the fold
	
	
	// REMOVED when the RFQ form was moved to the search results to avoid spam bots   //if($('requestform').visible()) toggleDiv('requestform');
	
	if(page == null) page = 1;
	var keywords = nl2other($F('searchwords'),'||');
	var kArray = keywords.split('||');
	//Clean up any blank lines
	if(kArray.length > 0){
		var kArrayNonBlanks = [];
		for(var i = 0; i<kArray.length; i++){
			if(kArray[i] != '') kArrayNonBlanks.push(kArray[i]);
		}
		keywords = kArrayNonBlanks.join('||');
	}
	
	//alert(res); //should we reset the dropdown to use the 1st result... like for a new search when the dropdown with available products will change?
	
	if(res){
		var index=0;	
	}else{
		try{
			var index = $F('availableResults');
		}catch(e){
			var index=0;	
		}
	}
	currentSearch = keywords;
	if(keywords == '') return false;
	
	
	
	//updateHTML2('searchresults','?action=search&search='+keywords+'&page='+page+'&index='+index+'&view=nohtml');//regular search with pages
	var url2request = SITE+'/productlocator/index.php?action=searchmulti&search='+keywords+'&page='+page+'&index='+index+'&view=nohtml';
	//var url2request = SITE+'/productlocator/'; 
	//alert('about to request: ' + url2request);
	updateHTML2('searchresults',url2request);//grouped search
	
}

removeSelect = function(){	
	//try{
		alert($('availableResults'));
		$('availableResults').remove();
	/*}catch(e){
		
	}*/
}

currentSearch = '';

predictiveSearch = function(){
	var keywords = $F('searchwords');
	alert(keywords.length);
	if(keywords != '' && keywords.length){}
	
}

focusRfqSearch = function(){
	var defVal = 'Type or paste the part number(s) you are looking for here. If searching for multiple parts, place each one on a separate line.';
	var curVal = $F('searchwords');
	
	if(curVal == '' || curVal == defVal){
			$('searchwords').value = defVal;			
	}else{
		getSearchResults();	
	}
	Field.activate('searchwords');
}

scrollToForm = function(){
	Field.activate('companyinput');
	Effect.ScrollTo('requestform',{duration:1.5});		
}

scrollToSearch = function(){
	Effect.ScrollTo('searchresults',{duration:1.0});		
}

toggleDiv = function(divid){
	showing = $(divid).style.display == 'none' ? false : true;
	
	//Effect.toggle('resultsdiv', 'blind', { duration: 0.5 });//results just appear and fade
	
	if(!showing){
		Effect.toggle(		divid, 'blind',	{ duration: 0.5,afterFinish:scrollToForm  });
	}else{
		Effect.toggle(divid, 'blind', { duration: 1.5 });	
	}
}



checkRFQ = function(ff){
	var valid;
	//alert('checking');
	// are all required fields present?
	//alert(ff + ", " + $('companyinput')+ ", " + $('contact')+ ", " + $('phone')+ ", " + $('email')+ ", " + ff.email);
	//
	
 	valid = $('companyinput').present() && $('contact').present()	 && $('phone').present()	 && $('email').present();
	//alert(valid);
	if(valid){
		return true;	
	}else{
		alert('Contact information denoted with an asterik (*) is required.');
		return false;
	}
}

checkRFQSubmit = function(){
	//document.getElementById('leadcookie').value = getLeadCookie();
	//document.forms[0];//REMOVED FOR HUBSPOT asp.NET HACK$('rfqform');
	document.forms[0].action = SITE + '/productlocator/index.php?action=submitrfq&template=blank';
	try{document.forms[0].setAttribute('action',SITE + '/productlocator/index.php?action=submitrfq&template=blank');}catch(eNotInternetExplorer){}
	//alert(ff.action);
	if(checkRFQ(document.forms[0])){
		document.forms[0].onsubmit = function(){
			//alert(this);
			//var st=''; for(x in this.attributes) st +='\n'+this.attributes[x].name + '="'+this.attributes[x].value+'"'; 
			//alert(st);
			return false;
		}
		
		if(typeof(document.forms[0].company) == 'undefined'){
			//add elements dynamically
			var debugMessage = '';
			
			var allInputs = document.getElementsByTagName('input');
			
			//if(!confirm('looping over ' + allInputs.length + '?')) return;
			var div = document.createElement('div');
			var i;
			for(i=0; i<allInputs.length;i++){
				var name = allInputs[i].name;
				
				
				if(typeof(name) == 'undefined' || name == '') continue;//null element - id only, no name
				
				var currentElem = document.forms[0][name];
				if(typeof(currentElem) == 'undefined'){
					var currentElem = allInputs[i];
					//add the element to the form using dom methods
					var elem = document.createElement('input');
					elem.setAttribute('name',name);
					elem.setAttribute('type','text');
					var value = $F(currentElem);//typeof(currentElem.selectedIndex) != 'undefined' ? currentElem.options[currentElem.selectedIndex] : currentElem.value;
					value = value == null ? '' : value;
					debugMessage += 'iteration ' + i + '==> creating element with name "' + name+ '", and value="' + value + '"\n';
					
					elem.setAttribute('value',value);
					div.appendChild(elem);
					//document.forms[0].appendChild(elem);
					
				}
			}
			
			//add the select drop down
			var elem = document.createElement('input');
			elem.setAttribute('name','type');
			elem.setAttribute('type','text');
			elem.setAttribute('value',($F('RFQType') == null) ? '' : $F('RFQType') );
			debugMessage += 'iteration ' + (i++) + '==> creating element with name "type", and value="' + elem.getAttribute('value') + '"\n';
			div.appendChild(elem);
			
			//add the message
			var elem = document.createElement('input');
			elem.setAttribute('name','message');
			elem.setAttribute('type','text');
			elem.setAttribute('value',($F('rfqmessage1') == null) ? '' : $F('rfqmessage1') );
			debugMessage += 'iteration ' + (i++) + '==> creating element with name "message", and value="' + elem.getAttribute('value') + '"\n';
			div.appendChild(elem);
		
			
			//alert(debugMessage);
			document.forms[0].appendChild(div);
			
			//return '#';
			
		}
		
		document.forms[0].submit();
	}
}

updateSelections = function(){
	var summaryText 	= '';
	selectedTextDiv		= $('selectedText');
	selectedTextInput	= $('selectedItems');
	var summaryValue 	= '';
	var currentResult 	= 0;
	var atLeast1Checked = false;
	if(numResults){//
		for(var i=0; i<numResults; i++){//loop over checkboxes to search for ones that the user has checked off
			var cbox = $('checkbox_'+i);
			if(cbox.checked){
				currentResult = currentResult + 1;
				summaryText = (summaryText +'<li>'+ cbox.value+'</li>');
				summaryValue = summaryValue + "\n" + currentResult + ") " + cbox.value;//for sending to the RFQ processing page
				atLeast1Checked = true;
			}//if
		}//for
		if(atLeast1Checked) summaryText = '<ol>'+summaryText+'</ol>';
	}
	
	var visible = atLeast1Checked ? 'block' : 'none';
	$('selectedTitle').style.display = $('selectedIcon').style.display = selectedTextDiv.style.display = visible;//show or hide the checked off summary div's depending on if any were selected or not from the search results
	selectedTextDiv.innerHTML 	= summaryText;
	selectedTextInput.value 	= summaryValue;
	
	
	
	
	
}



addLoadEvent(focusRfqSearch);