// Search.js
// author: Mike B
// date: 05/11/2010
// All javascript code used for the search pages

var $j = jQuery.noConflict();




function sendEmail(text){
	$j.ajax({
		  url: '/search/includes/searchAjaxSendEmail.asp',
		  data:  {theBody:[''+text]},
		  cache: false,
		  beforeSend: function(){},
		  complete: function(){				  
		  },
		  error: function(xhr, textStatus, errorThrown){
			  alert(xhr.responseText );
		   },
		  success: function(html){			
	  	  }
	});		
	
}

function sendEmailPlus(text,keyword,wrapperFilters,filterSkuType,countryFilter,strengthFilter,specKeyword,newValue,newFilter){
	$j.ajax({
		  url: '/search/includes/searchAjaxSendEmail-2.asp',
		  //data:  {theBody:[''+text],handleSection:["grid"],keyword:[''+keyword],W:[''+wrapperFilters],F:[''+filterSkuType],C:[''+countryFilter],P:[''+priceFilter],S:[''+strengthFilter],I:[''+accessorySpecific],A:[''+samplerSpecific],'new':[''+newValue],newF:[''+newFilter]},
		  data:  {theBody:[''+text],handleSection:["grid"],keyword:[''+keyword],W:[''+wrapperFilters],C:[''+countryFilter],S:[''+strengthFilter],k:[''+specKeyword],'new':[''+newValue],newF:[''+newFilter]},
		  cache: false,
		  beforeSend: function(){},
		  complete: function(){				  
		  },
		  error: function(xhr, textStatus, errorThrown){
			  alert(xhr.responseText );
		   },
		  success: function(html){			
	  	  }
	});		
	
}

//function addFilter(keyword,wrapperFilters,filterSkuType,countryFilter,priceFilter,strengthFilter,accessorySpecific, samplerSpecific,newValue,newFilter, pageCount, currPage)
function addFilter(keyword,wrapperFilters,countryFilter,strengthFilter,specKeyword,newValue,newFilter, pageCount, currPage)
{
	var topData, leftData, mainData, ajaxhandlerURL
	
	// Initialize the fade
	$j("#searchFilterColumn").animate({ 
	      opacity: .5
		  },500);
	$j("#searchResults").animate({ 
	      opacity: .5
		  },500);
	$j("#appliedFilters").animate({ 
	      opacity: .5
		  },500);
	
	$j(".popContainer").css('display','block');
	
	if(newFilter == 'E')
	{
		ajaxhandlerURL = '/search/includes/searchAjaxHandlerError.asp';
	}
	else
	{
		ajaxhandlerURL = '/search/includes/searchAjaxHandler.asp';
	}



	$j.ajax({
	    url: ajaxhandlerURL,
	    //data:  {handleSection:["grid"],keyword:[''+keyword],W:[''+wrapperFilters],F:[''+filterSkuType],C:[''+countryFilter],P:[''+priceFilter],S:[''+strengthFilter],I:[''+accessorySpecific],A:[''+samplerSpecific],'new':[''+newValue],newF:[''+newFilter],perpage:[''+pageCount],NAV:[''+currPage]},
	    data: { handleSection: ["grid"], keyword: ['' + keyword], W: ['' + wrapperFilters], C: ['' + countryFilter], S: ['' + strengthFilter], k: ['' + specKeyword], 'new': ['' + newValue], newF: ['' + newFilter], perpage: ['' + pageCount], NAV: ['' + currPage] },
	    cache: false,
	    beforeSend: function () { },
	    complete: function () {
	    },
	    error: function (xhr, textStatus, errorThrown) {
	        $j('#loadingBox').css('display', 'none');
	        $j('#errorBox').css('display', 'block');
	        $j('#loadingTextBox').css('display', 'none');
	        $j('#errorTextBox').css('display', 'block');
	        //			  sendEmailPlus(xhr.responseText,keyword,wrapperFilters,filterSkuType,countryFilter,priceFilter,strengthFilter,accessorySpecific, samplerSpecific,newValue,newFilter);
	        sendEmailPlus(xhr.responseText, keyword, wrapperFilters, countryFilter, strengthFilter, specKeyword, newValue, newFilter);
	    },
	    success: function (html) {
	        if (typeof (jQuery) != 'undefined') { jQuery('body').trigger('omni-searchfilterclicked', { 'filterby' : newValue, 'cat' : newFilter }); }
	        $j('#loadingBox').css('display', 'block');
	        $j('#errorBox').css('display', 'none');
	        $j('#loadingTextBox').css('display', 'block');
	        $j('#errorTextBox').css('display', 'none');

	        $j('#tempHolder').html(html);

	        mainData = $j('#gridDiv').html();
	        topData = $j('#topDiv').html();
	        leftData = $j('#leftDiv').html();

	        $j('#tempHolder').html('');

	        // Hide the sections
	        $j("#appliedFilters").animate({
	            opacity: 0.1
	        }, 500);
	        $j("#searchFilterColumn").animate({
	            opacity: 0.1
	        }, 500);

	        $j("#searchResults").animate({
	            opacity: 0.1
	        }, 500, function () {
	            // When done fading, populate them								  
	            $j("#appliedFilters").html(topData);
	            $j("#searchFilterColumn").html(leftData);
	            $j("#searchResults").html(mainData);
	        });

	        // show the sections again
	        $j("#appliedFilters").animate({
	            opacity: 1
	        }, 500);

	        $j("#searchFilterColumn").animate({
	            opacity: 1
	        }, 500);
	        $j("#searchResults").animate({
	            opacity: 1
	        }, 500, function () {
	            // hide the loading bar
	            $j(".popContainer").css('display', 'none');
	        });
	    }
	});
}




var varGlobal;

varGlobal = 0;
var selectionID;
selectionID = -1;
var selectionCieling = 10;

function changeActiveStatus(elm, filterGroup)
{
	
	if (elm.id == 'nonactiveFilter'+filterGroup)
		{
//			if (filterGroup == 'Price')
//				changePriceIds();
			elm.id = 'activeFilter'+filterGroup;
		}
	else if (elm.id == 'activeFilter'+filterGroup)
		{
//			if (filterGroup == 'Price')
//				changePriceIds();
			elm.id = 'nonactiveFilter'+filterGroup;
		}
}





function changeActiveHovers(id)
{
	var lcv = 0;
	selectionCieling = 10;
	
	while (lcv < selectionCieling)
	{
		var suggestionElement = document.getElementById('sug'+lcv);
		if (suggestionElement != null)
		{
			if(document.getElementById('sug'+lcv).className = 'active')
			{
				document.getElementById('sug'+lcv).className = 'nonActive';
			}				
		}
		else
		{
			
			selectionCieling = lcv;
			lcv += 1;
		}
		lcv += 1;
	}
	if(lcv>=9)
	{
		selectionCieling = 10;	
	}
	
	var suggestionElement = document.getElementById('sug'+id);
	if (suggestionElement != null)
	{
		document.getElementById('sug'+id).className = 'active';
	}
	else
	{
		document.getElementById('sug0').className = 'active';
		selectionID = 0;
	}

}

function getkeywordsuggestion(keyword,oEvent,subFlag)
	{
		var currentTime = new Date();
		var temp = 0;			
		if(keyword.length >= 3)
			{								
				if(oEvent.keyCode == 38) {
						if (selectionID > 0){
							selectionID = selectionID - 1;
						}
						else{
							selectionID = 0;
						}
					changeActiveHovers(selectionID);
				}
				else if(oEvent.keyCode == 40){
					if (selectionID < selectionCieling){
						selectionID = selectionID + 1;
					}
					changeActiveHovers(selectionID);
				} 
				/*else if(oEvent.keyCode == 13)
				{
					alert(keyword);
				}*/
				else
				{
					$j.ajax({
					  url: '/search/includes/suggestKeyword.asp',
					  data:  {k:[''+keyword]},
					  cache: false,
					  beforeSend: function(){},
					  success: function(html){	

						  if (html == 'No results returned')
						  {
							  $j('#keywordSearchSuggestContainer').css('display','none');
						  }
						 else
						  {
							  $j('#keywordSearchSuggestContainer').css('display','inline');

							$j('#keywordsuggest').html(html);
					
						  }												
						  
 						  varGlobal = 0;
					  },
					  complete: function(html){
					   }
					});  
				}
		}
	else
		{
			$('keywordSearchSuggestContainer').style.display = 'none';
			// This subflag will be triggered from the form
			if(subFlag == 1)
			{	
				if (selectionID != -1)
				{
					document.getElementById('frmProdKeywordlower').value = document.getElementById('suggestionSearchVal' + selectionID).innerHTML.replace("&amp;", "&");
				}
				
				document.getElementById('searchLeftNav').action = document.getElementById('searchLeftNav').action + document.getElementById('frmProdKeywordlower').value;					
				
				return true;
										
			}
			
		}
}


// will have to add excape characters for quotes
function showResult(prodKeyword)
{
	document.getElementById('frmProdKeywordlower').value = prodKeyword;
	document.getElementById('searchLeftNav').action = document.getElementById('searchLeftNav').action + document.getElementById('frmProdKeywordlower').value;
	document.getElementById('searchLeftNav').submit();
	
/*	$j("#keywordResult").hide("fast");

	$j.ajax({
	  url: '/search/includes/searchresultsajax.asp',
	  data:  {k:[''+prodID]},
	  cache: false,
	  beforeSend: function(){},
	  success: function(html){	
		$j('#suggestQuickClick').html(html);
								
		$j("#suggestQuickClick").show("fast");
		$j('#keywordSearchSuggestContainer').css('display', 'none');
	  },
	  complete: function(html){
	   }
	}); */

}

function showOriginalResult()
{
	$j("#keywordResult").toggle("fast");
	$j("#suggestQuickClick").toggle("fast");
}


function addToCart(itemID, qty)
{

	document.getElementById('load' + itemID).style.display = 'inline';
	
	var url = '/cart/index.asp?itemID=' + itemID + '&qty=' + qty + '&aj=1';
	
	new Ajax(url, {method:'get',  
					onComplete:function(txt, xml) {						
						
						//alert(window.frames[0].innerHTML);
						if (window.frames[0])
						{
							//window.frames[0].reloadFrame();
						}
						var url2 = '/includes/cartDisplayButtonOutput.asp';
						new Ajax(url2, {method:'get',  
											onComplete:function(txt, xml) {
			
												if (typeof (jQuery) != 'undefined') { jQuery('body').trigger('omni-scadded', {'itemID' : itemID, 'qty' : qty}); }			

												document.getElementById('cartRoot').innerHTML = txt;
												
												document.getElementById('load' + itemID).style.display = 'none';
												
												document.getElementById('add' + itemID).innerHTML = '<img src="/graphics/addedItem.gif" width="75" height="15" border="0" style="padding-top: 3px;"> '
												
												document.getElementById('qty' + itemID).value = '';
												
											}}).request();
						
					}}).request();
}


