﻿// JScript File

var req, searchControlSuffix;
var urlPrefix='/SearchPages/';
//var urlPrefix='';

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)
{

        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;
                    searchControlSuffix = _searchControlSuffix;

                    if(req!=null)
                    {
                        req.onreadystatechange = Process;
                        req.open("GET", url, true);
                        req.send(null);
                    }
          }
    }
    else
        HideDiv("autocomplete"+_searchControlSuffix);
}
        

    

function Process()
{
    if (req.readyState == 4)
        {
        // only if "OK"
            if (req.status == 200)
            {
                if(req.responseText=="")
                {
                //alert('n');
                    HideDiv("autocomplete"+searchControlSuffix);
                }
                else
                {
                //alert(req.status);
                    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;
            }
        }
}

function ShowDiv(divid)
{
    if (document.layers) document.layers[divid].visibility="show";
    
    else document.getElementById(divid).style.visibility="visible";
}

function HideDiv(divid)
{

   if (document.layers) document.layers[divid].visibility="hide";
   else 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 ==40) //down arrow key
    
    try { document.getElementById(lstResults).focus(); }
    catch (err) { }
}

function ShowResultWindow(lstResults)
{
//                   var studentcode='';
//                    if(document.getElementById('hdStudentCode')!=null)
//                    {
//                    studentcode='data='+document.getElementById('hdStudentCode').value;
//                    if(lstResults.value.toString().indexOf('?')!=-1)
//                        studentcode='&'+studentcode;
//                        else
//                          studentcode='?'+studentcode;
//                    }
//    window.location = lstResults.value+studentcode;
    
     Redirector(lstResults.value);
}

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)
{     
     
//    if(document.getElementById('autocomplete2').innerHTML == '')
//        {
//            //alert('sdfsdf');
//            window.location.href = '/SearchPages/SearchFAQ.aspx';
//        }
    
    var keyword = document.getElementById('keyword'+_searchControlSuffix).value;
     keyword=keyword.replace('&','%26');

chk=keyword;
            if((searchControlCategory=='1' ||searchControlCategory=='2' ) && keyword!='')                        
        window.location =urlPrefix+"SearchFAQ.aspx?k="+keyword+'&c='+searchControlCategory;
            
        else
    
    if(keyword!='')
    {

        
        Initialize();
        var url=urlPrefix+"GetRedirectURL.aspx?k="+keyword+"&c="+searchControlCategory;
     
        searchControlSuffix = _searchControlSuffix;
       
        if(req!=null)
        {
            if(searchControlCategory=='4')
            {
            alert('if');
                req.onreadystatechange = ApplyNowURLReceived;
                req.open("GET", url, true);
                req.send(null);
            }
            else
            {
            
                req.onreadystatechange = URLReceived;
                req.open("GET", url, true);
                req.send(null);  
            }

        }
    }
}

function wait(msecs)
{
var start = new Date().getTime();
var cur = start
while(cur - start < msecs)
{
cur = new Date().getTime();
}
}

function URLReceived()
{

    if (req.readyState == 4)
        {
            
           count = '0';
        // only if "OK"()
            if (req.status == 200)
            {
           
                if(req.responseText=="" || req.responseText=="0")
                {
                
                 window.location =urlPrefix+"SearchNotFound.aspx?c=-1";

                }
                else if (req.responseText == "1")
                {
                    alert('More than one results found.');
                }
                else
                {
                
//                    var studentcode='';
//                    if(document.getElementById('hdStudentCode')!=null)
//                    {
//                    studentcode='data='+document.getElementById('hdStudentCode').value;
//                    if(req.responseText.toString().indexOf('?')!=-1)
//                        studentcode='&'+studentcode;
//                        else
//                          studentcode='?'+studentcode;
//                    }
//                  window.location = req.responseText+studentcode;

                Redirector(req.responseText);
                }
            }
            else
            {
                //document.getElementById("autocomplete"+searchControlSuffix).innerHTML="There was a problem retrieving data:<br />" + req.statusText;
            }
        }                
        else if(count == '0' && (chk != 'Type the field of education, profession or degree you are interested in'))
            {
                count = '1';
                window.location.href = '/SearchPages/SearchNotFound.aspx?c=-1';
            }
}

function ApplyNowURLReceived()
{
    if (req.readyState == 4)
        {


        // only if "OK"()
            if (req.status == 200)
            {
                if(req.responseText=="" || req.responseText=="0")
                {
                 window.location =urlPrefix+"SearchNotFound.aspx?c=4";

                }
                else if (req.responseText == "1")
                {
                    alert('More than one results found.');
                }
                else
                {
                   Redirector(req.responseText);
                }
            }
            else
            {
                document.getElementById("autocomplete"+searchControlSuffix).innerHTML="There was a problem retrieving data:<br />" + req.statusText;
            }
        }
}


function Redirector(link)
{

   
        window.location = link;
}


function getQueryString(key)//, query)
{
    query = parent.location.search;//e
    if (query.length > 1)
    {
        query = query.substring(1, query.length); 
        for(var i=0; i < query.split("&").length; i++)
        {
            subQuery = query.split("&")[i];
            subQueryKey =subQuery.substr(0,subQuery.indexOf('='));// subQuery.split("=")[0];
            subQueryValue = subQuery.substr(subQuery.indexOf('=')+1,subQuery.length-(subQuery.indexOf('=')+1));// subQuery.split("=")[1];
            if (subQueryKey.toLowerCase()==key.toLowerCase())
            {
                return subQueryValue;
            }
        }
    }
    else
    {
        return null;
    }
    return null;
}
