
/**
 *
 * Clientseitige Vorverarbeitung der Suche mittels AJAX
 * 
 * attributeNames = Array von Objects mit folgender Struktur
 * 
 * attributeNames = new Array();
 * attributeNames[0] = new Object();
 * attributeNames[0]["attributeName"] = $attributeName 
 * attributeNames[0]["attributeValue"] = $attributeValue 
 * attributeNames[1] = new Object();
 * ...
 * ..
 * .   
 *
 */
    
var attributeNames; 
var categoryUID;
var offset;
var orderby;
var postVars;
var curPage;
var allPagesCount;
var pageID;
var smenuIDs;

function resetOffset() {
  offset = 0;
//  alert(offset);
}

function setValue(title, value, valuename){
  title = unescape(title);
  valuename = unescape(valuename);
  var unescapedvaluename = valuename;
  tmp = title + ' ' + value + ' ' + valuename;
  // alert(tmp);
  
  postVars = '';
  if (title == 'offset' || title == 'orderby'){
    if (title == 'offset'){
      offset = eval(offset + ' ' + value);
      if (offset < 0) offset = 0;
    } else {
      orderby = value;
    }
  }
  else {
    if (value == '-1') {
      $('ul'+title).removeClassName('active');  
      $('choice'+title).update('Alle');
    } else {
      $('ul'+title).addClassName('active');
      var vname = replaceUmlauts(unescapedvaluename, 0);
      //alert(vname);
      $('choice'+title).update(vname);
    }
  }

  preparePostvars(title, value);  
}

function mysearch(title, value, valuename){
  var escapedtitle = title;
//   title = unescape(title);
//   title = replaceUmlauts(title, 0);
  var unescapedvaluename = valuename;
  valuename = unescape(valuename);
  tmp = title + ' ' + value + ' ' + valuename;
  // alert(tmp);
  
  postVars = '';
  if (title == 'offset' || title == 'orderby'){
    if (title == 'offset'){
      offset = eval(offset + ' ' + value);
      if (offset < 0) offset = 0;
    } else {
      orderby = value;
    }
  }
  else {
    if (value == '-1') {
      $('ul'+escapedtitle).removeClassName('active');  
      $('choice'+escapedtitle).update('Alle');
    } else {
      $('ul'+escapedtitle).addClassName('active');
      var vname = replaceUmlauts(unescapedvaluename, 0);
      // alert(vname);
      $('choice'+escapedtitle).update(vname);
      
    }
  }

//   for (var i = 0; i < attributeNames.length; i++) {
//     if (attributeNames[i].attributeName == title){
//       attributeNames[i].attributeValue = value;
//     }
//     if (attributeNames[i].attributeValue != '' && attributeNames[i].attributeValue != -1){  
//        postVars = postVars + '&attributeNames[]=' + attributeNames[i].attributeName + '&attributeValues[]=' + attributeNames[i].attributeValue + '&attributeUIDs[]=' + attributeNames[i].attributeUID;
//     }
//   }
//   var tmppostvars = postVars;
//   postVars = '&filteroffset='+offset+'&desc='+orderby+'&pageid='+pageID;
// 
//   postVars = postVars + tmppostvars;

  preparePostvars(title, value);
  myajaxsearch();
}

function preparePostvars(title, value){
// alert(title);
// alert(value);
// alert(attributeNames.length);
  postVars = '';
  for (var i = 0; i < attributeNames.length; i++) {
    var stri = attributeNames[i].attributeName + ' === ' + title;
//     alert(stri);
    if (attributeNames[i].attributeName == title){
      attributeNames[i].attributeValue = value;
    }
    if (attributeNames[i].attributeValue != '' && attributeNames[i].attributeValue != -1){  
       postVars = postVars + '&attributeNames[]=' + attributeNames[i].attributeName + '&attributeValues[]=' + attributeNames[i].attributeValue + '&attributeUIDs[]=' + attributeNames[i].attributeUID;
    }
  }
  var tmppostvars = postVars;
  postVars = '&filteroffset='+offset+'&desc='+orderby+'&pageid='+pageID;

  postVars = postVars + tmppostvars;
}

function myajaxsearch(){
  
   // alert(postVars);
  
  url = 'http://www.hai-end.com/index.php?eID=productsearch&categoryUID='+categoryUID+'&'+postVars;
//     node = $('ajaxresult'); 
//     alert(url);
  new Ajax.Request(url, {method: 'post',onSuccess: function(req) {node = $('ajaxresult'); node.update(req.responseText);}});
//  new Ajax.Updater('ajaxresult', url, { method: 'post' });
}

function mySearchInitManufacturer(ObjArray, catUID, thisoffset, thisorderby, manuUID, page_id) {
  mypv = '';
  if (ObjArray == -1) {
    k = attributeNames.length;
    for (var i = 0; i<k; i++){
      if (attributeNames[i].attributeValue != '') mypv = mypv + '&attributeNames[]=' + attributeNames[i].attributeName + '&attributeValues[]=' + attributeNames[i].attributeValue + '&attributeUIDs[]=' + attributeNames[i].attributeUID;
    }
  } else {
    attributeNames = ObjArray;
  }
  
  k = attributeNames.length;
//   for (var f = k; f<i; f++){
//     if (attributeNames[f][attributeName] == 'Hersteller'){
//       attributeNames[f][attributeValue] = manuUID;
//     }
//   }
  
  categoryUID = catUID;
  offset = thisoffset;
  orderby = thisorderby;
  postVars = 'offset='+offset+'&desc='+orderby+'&pageid='+page_id+mypv;
  pageID = page_id;
  curPage = 1;
  allPagesCount = 1;
    
  i = smenuIDs.length;
  for (var f = 1; f<i; f++){
    montre(smenuIDs[f]);
    cache(smenuIDs[f]);
  }
  preparePostvars('Hersteller', manuUID);
  myajaxsearch();
}

function mySearchInit(ObjArray, catUID, thisoffset, thisorderby, page_id) {
  mypv = '';
  if (ObjArray == -1) {
    k = attributeNames.length;
    for (var i = 0; i<k; i++){
      if (attributeNames[i].attributeValue != '') mypv = mypv + '&attributeNames[]=' + attributeNames[i].attributeName + '&attributeValues[]=' + attributeNames[i].attributeValue + '&attributeUIDs[]=' + attributeNames[i].attributeUID;
    }
  } else {
    attributeNames = ObjArray;
  }
  
  categoryUID = catUID;
  offset = thisoffset;
  orderby = thisorderby;
  postVars = 'offset='+offset+'&desc='+orderby+'&pageid='+page_id+mypv;
  pageID = page_id;
  curPage = 1;
  allPagesCount = 1;
  i = smenuIDs.length;
  
  for (var f = 1; f<i; f++){
//    alert(smenuIDs[f]);
    montre(smenuIDs[f]);
    cache(smenuIDs[f]);
  }
  myajaxsearch();
}

function setAllPagesCount(NewCount){
  node = $('pagenavall');
  node.update('<span>'+NewCount+'</span>');  
  node2 = $('pagenavall2');  
  node2.update('<span>'+NewCount+'</span>');
  allPagesCount = NewCount;
  updatePagenavLinks();
}

function setCurrentPageNumber(NewCount){
  node = $('pagenavcurrent');
  node.update('<span>'+NewCount+'</span>');  
  node2 = $('pagenavcurrent2');  
  node2.update('<span>'+NewCount+'</span>');
  curPage = NewCount;
  updatePagenavLinks();
}

function updatePagenavLinks(){
  nodeLeft = $('pagenavrewind');
  nodeRight = $('pagenavforward');
  
  if (curPage == 1) nodeLeft.hide();
  else nodeLeft.show();
  
  if (curPage == allPagesCount) nodeRight.hide();
  else nodeRight.show();

}

function manufacturerInit(valuename){
  title = 'Hersteller';
  $('ul'+title).addClassName('active');
  $('choice'+title).update(valuename);
}

function smenuIDsInit(value){
  smenuIDs = value;
  
}

function replaceUmlauts(string, index){
    var ue = '%C3%BC';
    var ae = '%C3%A4';
    var oe = '%C3%B6';
    var sz = '%C3%9F';
    var aae = '%C3%84';
    var uue = '%C3%9C';
    var ooe = '%C3%96';
    var leer = '%20';
    var openbrace = '%28';
    var closebrace = '%29';
    var doublestring = '%22';
    var ampersand = '%26';

    // , "ü", "%C3%BC"
    var anArray = new Array(1);
    anArray[0] = new Array(ooe, oe, aae, ae, uue, ue, sz, openbrace, closebrace, leer, doublestring, ampersand);
    anArray[1] = new Array("&Ouml;", "&ouml;", "&Auml;", "&auml;", "&Uuml;", "&uuml;", "&szlig;", "(", ")", "&nbsp;", "\"", "&amp;");
    anArray[2] = new Array("Oe", "oe", "Ae", "ae", "Ue", "ue", "sz");

    var string;    
    for (var i=0; i<anArray[index].length; i++){
  		myRegExp = new RegExp(anArray[index][i],"g");
//  		alert(anArray[index][i]);
	   	string = string.replace(myRegExp, anArray[(index==0?1:0)][i]);
//	   	alert(string);
	  }
//	  alert(string);
    return string;
}

