Subversion Repositories SmartDukaan

Rev

Rev 10582 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10582 lgm 1
 
2
function spliturl(url)
3
{
4
 fullpath = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));
5
 params =fullpath.split('<?=base_url()?>');
6
 getparams = url.split('?');
7
 var output = new Array();
8
 output[0] = params[1]; //data controller to parameters
9
 output[1] = getparams[1];//get input
10
 output[2]=fullpath; //full url expcept get input
11
 return output;
12
}
13
 
14
function autoComplete(){    
15
  var outp = document.getElementById("search-result"); 
16
  var oldins;
17
  var posi = -1;
18
  var words = new Array();
19
  var input;
20
  var key;
21
  microAjaxAutoComplete();
22
  function microAjaxAutoComplete(){
23
    var qstr = document.getElementById("autocomplete").value;
24
    var autoCompleteItems = 10;
25
    var acItems;
26
    if(qstr.length > 2){
27
     microAjax(base_url+'home/autosuggest/'+encodeURIComponent(qstr),function(data){
28
      data = JSON.parse(data);
29
      var results = data;
30
          acItems = results;  
31
        init(acItems);
32
        if(acItems.length == 0){
33
           clearOutput();
34
         }
35
     });     
36
    }else if(qstr.length < 2){
37
      clearOutput(); 
38
    }
39
  }
40
 
41
function setVisible(visi)
42
{
43
  var x = document.getElementById("search");
44
  var t = document.getElementById("autocomplete");
45
}
46
function init(suggestions)
47
{ 
48
  lookAt(suggestions);
49
  setVisible("hidden");
50
  document.onkeydown = keygetter; //needed for Opera...
51
  document.onkeyup = keyHandler;
52
}
53
function lookAt(suggestions)
54
{
55
   var ins = document.getElementById("autocomplete").value;
56
    if (posi > -1);
57
   else if (ins.length > 1)
58
   {
59
    words = suggestions;
60
     if (words.length > 0)
61
     {
62
        clearOutput();
63
        for (var i=0;i < words.length; ++i){
64
          addWord(words[i], words[i]);
65
        }
66
 
67
        setVisible("visible");
68
        input = document.getElementById("autocomplete").value;
69
     }
70
     else
71
     {
72
        setVisible("hidden");
73
        posi = -1;
74
     }
75
   }
76
   else
77
   {
78
    setVisible("hidden");
79
    posi = -1;
80
   }
81
   oldins = ins;
82
}
83
/* This method is used to display the final result to the page in the form of dropdown.*/
84
function addWord(word,urlFragment)
85
{  
86
  var valu = document.getElementById("autocomplete").value;
87
   var sp = document.createElement("div");
88
   var anchor = document.createElement("a");
89
   var url = base_url+'search?q='+encodeURIComponent(urlFragment);
90
       anchor.setAttribute('href',url);
91
       anchor.setAttribute('class',"anchorHighlight");
92
       anchor.setAttribute('onclick',"setValue("+urlFragment+")");
93
   var regEx = new RegExp(valu, "ig");   
94
   var tempValu='<span class="highlight">'+ valu +'</span>'; /* In order to highlight the enterd text by user */
95
     word = word.replace(regEx, tempValu);
96
     anchor.innerHTML= word;
97
     sp.appendChild(anchor);
98
     sp.onmouseover = mouseHandler;
99
     sp.onmouseout = mouseHandlerOut;
100
     sp.onclick = mouseClick;
101
   outp.appendChild(sp);
102
}
103
function clearOutput()
104
{
105
  while (outp.hasChildNodes())
106
  {
107
    noten=outp.firstChild;
108
    outp.removeChild(noten);
109
  }
110
   posi = -1;
111
}
112
 
113
function setColor (_posi, _color, _forg)
114
{
115
   outp.childNodes[_posi].style.background = _color;
116
   outp.childNodes[_posi].style.color = _forg;
117
}
118
function keygetter(event)
119
{
120
  if (!event && window.event) 
121
      event = window.event;
122
  if (event)
123
      key = event.keyCode;
124
  else
125
      key = event.which;
126
}
127
function keyHandler(event)
128
{
129
  if (document.getElementById("search").style.visibility == "visible")
130
  {
131
     var textfield = document.getElementById("autocomplete");
132
     if (key == 40)//key down
133
     { 
134
        if (words.length > 0 && posi <= words.length-1)
135
        {
136
          if (posi >=0)
137
            setColor(posi, "#fff", "black");
138
          else 
139
             input = textfield.value;
140
             setColor(++posi, "blue", "white");
141
             textfield.value = outp.childNodes[posi].firstChild.nodeValue;
142
        }
143
      }
144
      else if (key == 38)
145
      { //Key up
146
        if (words.length > 0 && posi >= 0)
147
         {
148
           if (posi >=1)
149
           {
150
              setColor(posi, "#fff", "black");
151
              setColor(--posi, "blue", "white");
152
              textfield.value = outp.childNodes[posi].firstChild.nodeValue;
153
           }
154
           else
155
           {
156
              setColor(posi, "#fff", "black");
157
              textfield.value = input;
158
              textfield.focus();
159
              posi--;
160
           }
161
         }
162
        }
163
         else if (key == 27)
164
         { // Esc
165
            textfield.value = input;
166
            setVisible("hidden");
167
            posi = -1;
168
            oldins = input;
169
          }
170
          else if (key == 8) 
171
          { // Backspace
172
            posi = -1;
173
            oldins=-1;
174
          } 
175
              }
176
   }
177
    var mouseHandler=function()
178
    {
179
      for (var i=0; i < words.length; ++i){
180
        setColor (i, "white", "black");       
181
      }
182
      //this.style.background = "blue";
183
      this.style.color= "white";
184
     };
185
     var mouseHandlerOut=function()
186
     {
187
       this.style.background = "white";
188
       this.style.color= "black";
189
     };
190
     var mouseClick=function()
191
     {
192
        document.getElementById("autocomplete").value = this.firstChild.nodeValue;
193
        setVisible("hidden");
194
        posi = -1;
195
        oldins = this.firstChild.nodeValue;
196
     };
197
  }
198
 
199
 
200
function searchTrigger(){
201
    var val = document.getElementById('autocomplete').value;
202
    if(val.length >= 3){
203
     window.location = base_url+'search?q='+ val;
204
    }
205
 
206
  }
207
 
208
function setValue(value){
209
  document.getElementById('autocomplete').value = value;
11938 anikendra 210
}