/*
##############################################################################
# mt Shopping system
# 2006-2007 (c) J3S Jan Sass und Sven Sass GbR, info@j3s.de
# $Revision: 1.10 $ - $Author: jic3s\stan $ - $Date: 2007/05/29 16:01:40 $
##############################################################################
*/

// configuration
var search_height=120;     // when changing this be sure to modify opacity values in faders
var search_fadeheight=10;
var search_fadedelay=10;
var search_blockupdate=0;
var search_refresh_timeout=250;
var search_refresh_event='';
var search_cache = new Array();
var search_maxitems = 8;
var search_items = search_maxitems;
var search_pos   = -1;

function Normalize(txt) {
  txt = txt.replace(/ /g, '_');
  txt = txt.replace(/&/g, 'und');
  txt = txt.replace(/\//g,'_2F_');
  return txt;
}

function SearchGetXMLHttp() {
  var xmlHttp = null;
  if (typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
  }
  if (!xmlHttp) {
    try {        xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch(e) {
      try {      xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch(e) { xmlHttp  = null; }
    }
  }
  return xmlHttp;
}

function DoSearch(query) {
  if (search_pos!=-1) {
    query = document.getElementById('shop_help'+search_pos).title; //HTML.replace(/&nbsp;/g,'');
  }

  if (typeof(query) == 'undefined' || query == null) {
    query = document.forms['shop_search'].q.value;
  } else {
    document.forms['shop_search'].q.value = query;
  }
  location.href='/shopsearch/'+Normalize(document.forms['shop_search'].q.value)+'.html';
  return false;
}

function SearchRefreshHelp() {
  var query=document.forms['shop_search'].q.value.toLowerCase();

  if (typeof(search_cache[query]) != 'undefined') {
    document.getElementById('shop_searchcontent').innerHTML=search_cache[query];
    return false;
  }

  search_xml=SearchGetXMLHttp();
  if (search_xml) {
    search_xml.open('GET', '/sw2q/'+escape(Normalize(query)), true);
    search_xml.onreadystatechange = function () {
      try {
        if (search_xml.readyState == 4) {
          search_pos=-1;
          document.getElementById('shop_searchcontent').innerHTML =search_xml.responseText;
          search_cache[query]=search_xml.responseText;
        }
      }
      catch (e) {};
    }
    search_xml.send(null);
  }
  return false;
}

function ShowFade(height) {
  var obj = document.getElementById('shop_searchhelper');
  obj.style.height=height+'px';
  obj.style.filter='alpha(opacity:'+height+')';
  obj.style.MozOpacity=height/100;

  if (height<search_height) {
    setTimeout('ShowFade('+(height+search_fadeheight)+')',search_fadedelay);
  }
}

function ShowSearchHelp() {
  search_blockupdate=0;
  if (document.forms['shop_search'].q.value != '') {
    if (document.getElementById('shop_searchhelper').style.visibility=='hidden') {
      document.getElementById('shop_searchhelper').style.visibility='visible';
      document.getElementById('shop_searchhelper').style.height='5px';
      setTimeout('ShowFade('+search_fadeheight+')',search_fadedelay);
      UpdateSearchHelp();
    }
  }
}

function HideFade(height) {
  var obj = document.getElementById('shop_searchhelper');
  obj.style.height=height+'px';
  obj.style.filter='alpha(opacity:'+height+')';
  obj.style.MozOpacity=height/100
  if (height>0) {
    setTimeout('HideFade('+(height-search_fadeheight)+')',search_fadedelay);
  } else {
    document.getElementById('shop_searchhelper').style.visibility='hidden';
    search_blockupdate=0;
  }
}

function HideSearchHelp() {
  if (document.getElementById('shop_searchhelper').style.visibility=='visible') {
    search_blockupdate=1;
    document.getElementById('shop_searchhelper').style.height=search_height+'px';
    setTimeout('HideFade('+(search_height-search_fadeheight)+')',search_fadedelay);
  }
}

function UpdateSearchHelp() {
  if (search_blockupdate) {
    search_blockupdate=0;
  }
  if (document.forms['shop_search'].q.value != '') {
    if (search_refresh_event!='') {
      clearTimeout(search_refresh_event);
    }
    setTimeout('SearchRefreshHelp()',search_refresh_timeout);
    ShowSearchHelp();
  } else {
    HideSearchHelp();
  }
}

function GetSearchItemCount() {
  var i;
  for (i=0;i<search_maxitems;i++) {
    var obj=document.getElementById('shop_help'+i);
    if (!obj) {
      break;
    }
  }
  search_items = i;
}

function SearchSelectItems() {
  for (var i=0;i<search_items;i++) {
    var obj=document.getElementById('shop_help'+i);
    if (obj) {
      if (i==search_pos) obj.className='shop_selecteditem';
      else               obj.className='shop_normalitem';
    }
  }
}

function SearchKeyDown(evt) {
  if(!evt) evt=window.event;
  var key = evt.keyCode ? evt.keyCode : evt.which;
  GetSearchItemCount();
  // up navigation
  if (key == 38) {
    if (search_pos>-1) {
      search_pos--;
    }
    SearchSelectItems();
    return false;
  // down
  } else if (key == 40) {
    if (search_pos+1<search_items) {
      search_pos++;
    }
    SearchSelectItems();

    return false;
  }

  return true;
}

function SearchKeyUp(evt) {
  // ignore cursor keys
  if(!evt) evt=window.event;
  var key = evt.keyCode ? evt.keyCode : evt.which;
  if (key == 38 || key == 40) {
    return false;
  }
  UpdateSearchHelp();
  return true;
}

function getCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}
function getCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
// Check whether cookies enabled
var cookiesEnabled=false;
function CookiesEnabled() {
  document.cookie = "Enabled=true";
  var cookieValid = document.cookie;

  // if retrieving the VALUE we just set actually works then we know cookies enabled
  if (cookieValid.indexOf("Enabled=true") != -1) {
    cookiesEnabled = true;
  } else {
    cookiesEnabled = false;
  }
  return cookiesEnabled;
}
