/* AUTOCOMPLETE CONFIGURATION */

//DataSource
var myServer = "./autocomplete.aspx";
//var mySchema = ["ResultItem", "KeyDataField"];
var myDataSource = new YAHOO.widget.DS_XHR(myServer, ["\n", "\t"]);
this.myDataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
//AutoComplete
var myAutoComp = new YAHOO.widget.AutoComplete("SearchTerm","container", myDataSource);
myAutoComp.queryDelay = 0.5;
myAutoComp.minQueryLength = 3;
myAutoComp.autoHighlight = false;
myAutoComp.animSpeed = 0.3;
myAutoComp.animVert = true;
myAutoComp.animHoriz = false;
myAutoComp.useShadow = true;
myAutoComp.prehighlightClassName = "yui-ac-prehighlight";

myAutoComp.formatResult = function(oResultItem, sQuery)
{
	var title = oResultItem[0];
	var url = oResultItem[1];
	var sMarkup;		
	//myAutoComp.setFooter("<span id='sitesearchshowall'>View all search results</span>");
	if(url == "None")
	{
		sMarkup = "No results found.";
		myAutoComp.setFooter("");
	}
	else
	{
		//sMarkup = "<a style='text-decoration:none;' href='" + url + "'>" + title + "</a>";
		//myAutoComp.setFooter("<a id='sitesearchshowall' style='text-decoration:none;' href='#'>To view all other search results, click on the GO button</a>");
		myAutoComp.setFooter("<a id='sitesearchshowall' style='text-decoration:none;' onClick='javascript:viewAll();' href='#'>View all other search results</a>");
		sMarkup = title;
	}
	document.getElementById('loading-gif').style.visibility = 'hidden';
	return (sMarkup);
}

//What do we want to do when an item in the suggestion container is selected (either by arrowing and hitting enter or by clicking)? We'll subscribe to the itemSelectEvent and when it fires use its arguments to set a new location for the current page.
	myAutoComp.itemSelectEvent.subscribe(function(type, args) {
 		// this line works around Opera's preventDefault bug:
 		YAHOO.util.Dom.get("gobtn").disabled = true;
 		
 		//now, we go off to the destination page chosen by the user from the AutoComplete suggestion list:
 		 if (args[2][1] != 'None')
 		 {
 		   location.href = (args[2][1]);
     }	
 	});

  //Here's the wiring for the form submission on our footer link. Note that we use the YUI Event Utility to add this listener -- this is part of YUI Core.
  //YAHOO.util.Event.on("sitesearchshowall", "click", function(e) {
  // The Dom Collection's get method is similar to document.getElementById in this instance:
  //	 YAHOO.util.Dom.get("Form").submit();
  	 //document.getElementById("Form").submit();
  //	 searchTerm = document.getElementById("SearchTerm").value;
  //	 searchTerm = searchTerm.replace(" ", "+");
  //	 location.href = 'http://www.abacus24-7.com/search.aspx?SearchTerm=' + searchTerm;
  //});
myAutoComp._updateValue = function() {
	return true;
}


function viewAll()
{
  searchTerm = document.getElementById("SearchTerm").value;
  //searchTerm = searchTerm.replace(" ", "+");
  searchTerm = searchTerm.replace(/ /g, "+");
  location.href = 'http://www.abacus24-7.com/search.aspx?SearchTerm=' + searchTerm;
}

function showLoader(e)
{
  var length = document.getElementById('SearchTerm').value.length;
  var kC  = (window.event) ?    // MSIE or Firefox?
		 event.keyCode : e.keyCode;
  var Esc = (window.event) ?   
		27 : e.DOM_VK_ESCAPE // MSIE : Firefox

  if (length >= 3 && kC != Esc)
  {
	document.getElementById('loading-gif').style.visibility = 'visible';
  }
  else
  {
	document.getElementById('loading-gif').style.visibility = 'hidden';
  }
}


function loadScript(url, callback){

	var script = document.createElement("script")
	script.type = "text/javascript";

	if (script.readyState){  //IE
		script.onreadystatechange = function(){
			if (script.readyState == "loaded" ||
					script.readyState == "complete"){
				script.onreadystatechange = null;
				callback();
			}
		};
	} else {  //Others
		script.onload = function(){
			callback();
		};
	}

	script.src = url;
	document.getElementsByTagName("head")[0].appendChild(script);
}

function lazyLoad(url, idTarget){
	$.ajax({
		   type: "GET",
		   url: url,
		   success: function(data){
				//$(idTarget).html(data).fadeIn('slow');
			}
	 });
}

function lazyLoadChat(){
	$('#chatContainer').load('../lazyload/chat.htm');
}

 function startChat(){
	
	var hblProto = document.location.protocol == 'https:' ? "https:" : "http:";
	document.write(unescape("%3Cscript type='text/javascript' src='" + hblProto + "//static.hab.la/js/wc.js' type='text/javascript'%3E%3C/script%3E"));
	
	/*
	loadScript("https://static.hab.la/js/wc.js", function(){
		config = wc_config();
		config.vars["start_expanded"] = 1;
		wc_init("5314-39719482-10-2063", config);
	});
	*/
	/*
	var fileref = document.createElement('script');
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", "https://static.hab.la/js/wc.js");
	document.getElementsByTagName("head")[0].appendChild(fileref);
	*/
	
	config = wc_config();
	config.vars["start_expanded"] = 1;
	wc_init("5314-39719482-10-2063", config);
} 
 function startChatSC(){
	config = wc_config();
	config.vars["start_expanded"] = 1;
	config.vars["welcome_msg"] = "Welcome to Live Chat with Abacus24-7! How may we assist you with your current purchase?";
	wc_init("5314-39719482-10-2063", config);
}

          
// IFrame workaround for IE
//var ua = navigator.userAgent.toLowerCase();
//if(ua.indexOf('msie') != -1 && ua.indexOf('opera') < 0) 
//{
//this.myAutoComp.useIFrame = true;
//YAHOO.util.Dom.get("useiframe").checked = true;
//}				 					
/* END OF AUTOCOMPLETE CONFIGURATION */

/* jQuery events*/
/*$().ready(function() {
	$("#Form2").submit(function (){
		if( $("#SearchTerm2").val() == "")
		{
			alert('Please enter at least 3 characters in the search field!');
			return false;
		}
		else
		{							
			$("#SearchTerm2").attr({
			id: "SearchTerm",
			name: "SearchTerm"
			})
			return true;
		}
	});
	$("#searchrollbig").click(function () {
	  $("#SearchTerm2").attr({
		id: "SearchTerm",
		name: "SearchTerm"
	  })
	  $("#Form2").submit();
	}).mouseover(function() {
	  $("#searchrollbig").css({"cursor":"pointer"});
	  $("#searchrollbig").attr({
		src: "images/bigsearchon.jpg"
	  })
	}).mouseout(function(){
	  $("#searchrollbig").css({"cursor":"default"});
	  $("#searchrollbig").attr({
		src: "images/bigsearchoff.jpg"
	  })
	}); 	
});
*/
/* end of jQuery events */

$().ready(function() {
  $("#Form2").submit(function (){
  		if( $("#SearchTerm2").val() == "")
  		{
  			alert('Please enter at least 3 characters in the search field!');
  			return false;
  		}
  		else
  		{							
  			$("#SearchTerm2").attr({
  			id: "SearchTerm",
  			name: "SearchTerm"
  			})
  			return true;
  		}
  	});
    $("#searchroll").click(function () {
      $("#Form").submit();
    }).mouseover(function() {
      $("#searchroll").css({"cursor":"pointer"});
      $("#searchroll").attr({
        src: "images/go-on.jpg"
      })
    }).mouseout(function(){
      $("#searchroll").css({"cursor":"default"});
      $("#searchroll").attr({
        src: "images/go-off.jpg"
      })
    });
    
    $("#searchrollbig").click(function () {
      $("#Form2").submit();
    }).mouseover(function() {
      $("#searchrollbig").css({"cursor":"pointer"});
      $("#searchrollbig").attr({
        src: "images/bigsearchon.jpg"
      })
    }).mouseout(function(){
      $("#searchrollbig").css({"cursor":"default"});
      $("#searchrollbig").attr({
        src: "images/bigsearchoff.jpg"
      })
    });	
});
          
function bookmark() {
	var title=document.title;
	var url=location.href;
	if (window.sidebar) window.sidebar.addPanel(title, url,"");
  else if( window.opera && window.print ) {
  	var mbm = document.createElement('a');
  	mbm.setAttribute('rel','sidebar');
  	mbm.setAttribute('href',url);
  	mbm.setAttribute('title',title);
  	mbm.click();
  }
	else if( document.all ) 
    window.external.AddFavorite( url, title);
  }
function bookmark1() {
	var title='Abacus24-7 Printer Ink Cartridges & Electronic Accessories';
	var url='http://www.abacus24-7.com';
	if (window.sidebar) window.sidebar.addPanel(title, url,"");
  else if( window.opera && window.print ) {
  	var mbm = document.createElement('a');
  	mbm.setAttribute('rel','sidebar');
  	mbm.setAttribute('href',url);
  	mbm.setAttribute('title',title);
  	mbm.click();
  }
	else if( document.all ) window.external.AddFavorite( url, title);
}

/* Search */
  	$().ready(function(){
  		$('#size').change(function(){
  			document.SearchForm2.submit();
  		})
  	})
  	function page(pg){
  		$('#pg').val(pg);
  		document.SearchForm2.submit();
  	}
  	function sort(srt){
  		$('#sort').val(srt);
  		document.SearchForm2.submit();
  	}
	function sortRelevance(){
		$('#sort').val('');
		$('#pg').val('');
		document.SearchForm2.submit();
	}
	
/* End of search */

/* search input field */
$().ready(function(){
	
	/* default search text*/
	var defaultText = "Find your product here.";
	var searchInputField = '#SearchTerm';
	$(searchInputField).val(defaultText);
	
	$(searchInputField).click(function(){
		var currentText = $(searchInputField).val();
		if (currentText == defaultText){
			$(searchInputField).val('');
		}
	});
	/*
	$(searchInputField).mouseout(function(){
		var currentText = $(searchInputField).val();
		if (currentText == ''){
			$(searchInputField).val(defaultText);
			$(searchInputField).blur();
		}
	});
	*/
})
/* end of search input field */

/* custom made autocomplete*/
/*
	var searchWords = new Array();
	
	$('#SearchTerm').keyup(function(event){
		var query = $('#SearchTerm').val();
		query = jQuery.trim(query);
		var timeout;
		var id = "#search-" + query.replace(/ /g, "-");
		if (query == "" || event.keyCode == '27'){
			$('#SearchResults').hide();
		}else if ($(id).length != 0){
			var c = $(id).html();
			$('#SearchResults').html(c);
		//} else if (query != lastQuery){
		}else if ($.inArray(query, searchWords) == -1){
			//lastQuery = query;
			if (timeout) {
				clearTimeout(timeout);
			}
			timeout = setTimeout('showSearchResults()', 500);	
		} 
	});
	
	function showSearchResults(){
		var query = $('#SearchTerm').val();
		$.ajax({
			   type: "GET",
			   url: "autocomplete.aspx",
			   data: "query=" + query,
			   cache: true,
			   success: function(data){
					$('#loading-gif').show();
					$('#SearchResults').html(data).show();
					cacheData(data, query);
					$('#loading-gif').hide();
				}
		 })
	}
	
	function cacheData(data, query){
		var hasElement = $.inArray(query, searchWords); 
		if (hasElement == -1){
			addSearchWord(query);
			var id = "search-" + query.replace(/ /g, "-");
			var cache = "<div id='"+ id + "' style='display:none'>" + data + "</div>";
			$('#search-cache').append(cache);
		}
	}
	
	function addSearchWord(query){
		var hasElement = $.inArray(query, searchWords); 
		if (hasElement == -1){
			searchWords[searchWords.length] = query;
		}
	}
	
	function monitorClick(e){    
		var evt = (e)?e:event;
		var theElem = (evt.srcElement)?evt.srcElement:evt.target;
		 if(theElem.id == "SearchResults" || theElem.id == "SearchTerm" && $('#SearchTerm').val() != "" ){
			$('#SearchResults').show();
		 }else if(theElem.id != "SearchResults" && theElem.id != "SearchTerm"){
			$('#SearchResults').hide();
		}
	}

	document.onclick = monitorClick;
*/
/* end custom made autocomplete */
