﻿// JScript File

var req, searchControlSuffix,controlCategory;
var urlPrefix='/SearchPages/';
//var urlPrefix='';

var isGroupingEnabled='1';
var totalItemCount='-1';
var Categoryitemcount='5'

//http://musthighschool.com/searchpages/searchpage.aspx?c=6&s=1&k=e&tic=-1&cic=5&isGroupingEnabled=1

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)
{
keyword=trim(keyword,' ');

div="autocomplete"+_searchControlSuffix;


        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);
}

//function SendQuery(keyword, _searchControlSuffix, e,searchControlCategory)
//{

//div="autocomplete"+_searchControlSuffix;

//        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(controlCategory=='6')
 PhoneName();
 document.getElementById('tbl_Error').style.display = 'none';
 
   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;

    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)
{     

    
//    if(document.getElementById('autocomplete2').innerHTML == '')
//        {
//            //alert('sdfsdf');
//            window.location.href = '/SearchPages/SearchFAQ.aspx';
//        }
    
    var keyword = document.getElementById('keyword'+_searchControlSuffix).value;

    keyword=trim(keyword,' ');
    
     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;
        controlCategory= searchControlCategory;
        if(req!=null)
        {
            if(searchControlCategory=='4')
            {
                req.onreadystatechange = ApplyNowURLReceived;
                req.open("GET", url, true);
                req.send(null);
            }
            else 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);  
            }

        }
    }
}

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.');
                    Redirector(req.responseText);
                }
                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 DeviceURLReceived()
{
    if (req.readyState == 4)
        {
             count = '0';
        // only if "OK"()
            if (req.status == 200)
            {
           
                SetDeviceValues(req.responseText)
                
            }
        }                
}


function SetDeviceValues(url)
{


		var cvApplyNow=document.getElementById('spanError'); 
		document.getElementById('spanError').innerHTML = '' ;
		
 if(url!="" )
                {
                var i=url.indexOf('@@');
                    if(i>0)
                    {
//                     alert(req.responseText.substr(0,i));
//                     alert(req.responseText.substr(i+2,req.responseText.length-(i+2)));
                    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='';
					  
			          cvApplyNow.innerHTML +='Please enter valid mobile.<br/>';
					  document.getElementById('tbl_Error').style.display = 'block';  
			          window.scrollTo(0,document.getElementById('tbl_Error').offsetTop );           								  
                      //alert ('File Not Found');
                      return false;
		            }
                }

}

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.');
                    Redirector(req.responseText);
                }
                else
                {
                   Redirector(req.responseText);
                }
            }
            else
            {
                document.getElementById("autocomplete"+searchControlSuffix).innerHTML="There was a problem retrieving data:<br />" + req.statusText;
            }
        }
}


function Redirector(link)
{

   if(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;
}


// For MobileApp
function PhoneName()
{   

    var txtDeviceCode = document.getElementById('txtDeviceCode');
    if (txtDeviceCode.value!='0')
    {        
        document.getElementById('dv').style.display='inline';
        if (txtDeviceCode.value=='1')
        {
        document.getElementById('lblFileName').innerHTML='Jad';
        document.getElementById('lblFileName1').innerHTML='Jad';
        document.getElementById('dv1').style.display='none';
        }
        if (txtDeviceCode.value=='2')
        {
        document.getElementById('lblFileName').innerHTML='Cab';
        document.getElementById('lblFileName1').innerHTML='Cab';
        document.getElementById('dv1').style.display='none';
        }
        if (txtDeviceCode.value=='3')
        {
        document.getElementById('lblFileName').innerHTML='Jad';
        document.getElementById('lblFileName1').innerHTML='Jad';
        document.getElementById('dv1').style.display='inline';
        document.getElementById('dv').style.display='none';
        }
        if (txtDeviceCode.value=='4')
        {
        document.getElementById('lblFileName').innerHTML='Jad';
        document.getElementById('lblFileName1').innerHTML='Jad';
         document.getElementById('dv1').style.display='inline';
        document.getElementById('dv').style.display='none';
        }
        if (txtDeviceCode.value=='5')
        {
        document.getElementById('lblFileName').innerHTML='Apk';
        document.getElementById('lblFileName1').innerHTML='Apk';
         document.getElementById('dv').style.display='inline';
        document.getElementById('dv1').style.display='none';
        }
                       
    }
    else
      {    
      }    
                             
}


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";
}




