﻿var req, searchControlSuffix,controlCategory;
var urlPrefix='/SearchPages/';
//var urlPrefix='/xsearchdeploy/SearchPages/';
//var urlPrefix='';
var isGroupingEnabled='1';
var totalItemCount='-1';
var Categoryitemcount='10'
var hideDivOnMouseOut='0'

//controlCategory 6 : isGroupingEnabled='1', Categoryitemcount='5'
// controlCategory 5  : isGroupingEnabled='0'

function Initialize()
{
try
{
req=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
req=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(oc)
{
req=null;
}
}
if(!req&&typeof XMLHttpRequest!="undefined")
{
req=new XMLHttpRequest();
}
}

function SendQuery(keyword, _searchControlSuffix, e,searchControlCategory)
{
try
{
keyword=trim(keyword,' ');

div="autocomplete"+_searchControlSuffix;

if(searchControlCategory=='6')
{
Categoryitemcount='5'
isGroupingEnabled='1';
}

if(searchControlCategory=='5')
{
isGroupingEnabled='0';
}

var key;
if (window.event)
key = window.event.keyCode;
else
key = e.which;


if(keyword!='')
{

if(key==13)//enter key
{
GetRedirectURL(_searchControlSuffix,searchControlCategory);
}

else if(!( (searchControlCategory=='1' ||searchControlCategory=='2' )  ))//&& keyword.length <10
{


Initialize();
var url=urlPrefix+"SearchPage.aspx?k="+keyword+"&s="+_searchControlSuffix+"&c="+searchControlCategory+"&cic="+Categoryitemcount+"&isGroupingEnabled="+isGroupingEnabled+"&tic="+ totalItemCount;
searchControlSuffix = _searchControlSuffix;
controlCategory=searchControlCategory;

if(req!=null)
{
  req.onreadystatechange = Process;
  req.open("GET", url, true);
  req.send(null);
}
}
}
else
HideDiv("autocomplete"+_searchControlSuffix);
}
catch(oc)
{
//debugger;
}
}
function Process()
{
try
{
if (req.readyState == 4)
{
// only if "OK"
if (req.status == 200)
{
if(req.responseText=="")
{
HideDiv("autocomplete"+searchControlSuffix);
}
else
{
ShowDiv("autocomplete"+searchControlSuffix);
document.getElementById("autocomplete"+searchControlSuffix).innerHTML = req.responseText;
}
}
else
{
document.getElementById("autocomplete"+searchControlSuffix).innerHTML="There was a problem retrieving data:<br />" + req.statusText;
}
}
}
catch(oc)
{
//debugger;
}
}
function ShowDiv(divid)
{
if (document.layers) document.layers[divid].visibility="show";

else document.getElementById(divid).style.visibility="visible";
}
function HideDiv(divid)
{ 
document.getElementById(divid).style.visibility="hidden";
}
function SelectListBox(e, lstResults)
{
var key;
if (window.event)
key = window.event.keyCode;
else
key = e.which;

if(key==13)//enter key      
{        
GetRedirectURL(searchControlSuffix,controlCategory);
return;
}
if(key ==40) //down arrow key

try { document.getElementById(lstResults).focus(); }
catch (err) { }
}
function ShowResultWindow(lstResults)
{

if(controlCategory!='6')
Redirector(lstResults.value);
else
{
SetDeviceValues(lstResults.value);
HideDiv("autocomplete"+searchControlSuffix);
}
}
function OnlstResultsKeyUp(e, lstResults)
{
var key;
if (window.event)
key = window.event.keyCode;
else
key = e.which;

if(key==13)//enter key
{   
ShowResultWindow(lstResults);
}

else if (key == 27)
{        
HideDiv('autocomplete'+searchControlSuffix);
document.getElementById('keyword'+searchControlSuffix).focus();
}  

}
function ShowSelectionInTextBox(lstResults)
{
document.getElementById('keyword'+searchControlSuffix).value = lstResults.options[lstResults.selectedIndex].text;
}
var chk ='';
var count = '0';

function GetRedirectURL(_searchControlSuffix,searchControlCategory)
{     
try
{
var keyword = document.getElementById('keyword'+_searchControlSuffix).value;
keyword=trim(keyword,' ');

keyword=keyword.replace('&','%26');

chk=keyword;


if(keyword!='')
{


Initialize();
var url=urlPrefix+"GetRedirectURL.aspx?k="+keyword+"&c="+searchControlCategory;

searchControlSuffix = _searchControlSuffix;
controlCategory= searchControlCategory;
if(req!=null)
{
if(searchControlCategory=='6')
{
req.onreadystatechange = DeviceURLReceived;
req.open("GET", url, true);
req.send(null);
}
else
{

req.onreadystatechange = URLReceived;
req.open("GET", url, true);
req.send(null);  
}

}
}
}
catch(oc)
{
//debugger;
}
}
function URLReceived()
{
if (req.readyState == 4)
{

count = '0';
// only if "OK"()
if (req.status == 200)
{

if(req.responseText=="" || req.responseText=="0")
{
alert('Search Not Found');
//window.location =urlPrefix+"SearchNotFound.aspx?c=-1";
}
else if (req.responseText == "1")
{
// alert('More than one results found.');
Redirector(req.responseText);
}
else
{
Redirector(req.responseText);
}
}
else
{
//document.getElementById("autocomplete"+searchControlSuffix).innerHTML="There was a problem retrieving data:<br />" + req.statusText;
}
}                
}
function DeviceURLReceived()
{
if (req.readyState == 4)
{
count = '0';
// only if "OK"()
if (req.status == 200)
{
SetDeviceValues(req.responseText)
}
}                
}
function SetDeviceValues(url)
{
try {
if(url!="" )
{
var i=url.indexOf('@@');
if(i>0)
{
document.getElementById("txtDeviceUrl").value=url.substr(0,i);
document.getElementById("txtDeviceCode").value=url.substr(i+2,url.length-(i+2));
}
else
{
document.getElementById("txtDeviceUrl").value='';
document.getElementById("txtDeviceCode").value='';
alert ('File Not Found');
return false;
}
}
}
catch(oc)
{
}
}
function Redirector(link)
{
if(link!=null && link!='')
window.location = link;

}
function trim(str, chars) {
return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

var div;
document.onclick=check; 
function check(e){ 

if(document.getElementById(div)!=null)
document.getElementById(div).style.visibility="hidden";
}
