﻿// JavaScript File
function toggleSearchPanel()
{
    //alert('Toggle');
    if (document.getElementById('SearchResult').style.display == 'none') {
            document.getElementById('SearchResult').style.display = 'block';
            document.getElementById('SearchMinimize').style['background'] = 'url(../images/min_normal.gif) no-repeat';
    }
    else {
        document.getElementById('SearchResult').style.display = 'none';
        document.getElementById('SearchMinimize').style['background'] = 'url(../images/max_normal.gif) no-repeat';
    }  
}

function findPos(objName)
{
    var obj = document.getElementById(objName);
    var posX = obj.offsetLeft;
    var posY = obj.offsetTop;
    var strTemp;
    var screenWidth;
    var screenHeight;
    var popupX;
            
    while(obj.offsetParent){
        posX=posX+obj.offsetParent.offsetLeft;
        posY=posY+obj.offsetParent.offsetTop;
        if(obj==document.getElementsByTagName('body')[0]){break}
        else{obj=obj.offsetParent;}
     }
        //alert(posX+'-'+posY);
    if(document.all) {//this is IE 4+
        if(document.body.offsetWidth)
        {
            screenWidth = document.body.offsetWidth;
        }
        else
        {
            screenWidth = document.body.clientWidth;
        }
        screenWidth = screenWidth + 10;
    }
    else {  //For NS, Firefox
        screenWidth = window.innerWidth;
    }
    
    //alert(screenWidth+','+posX);
    popupX = posX - 310;
    if(popupX + 600 > screenWidth)
    {
        popupX = screenWidth - 610;
    }
    if(popupX < 0)
    {
        popupX = 0;
    }
    //alert('posx=' + posX + ' posY=' + posY + ' width=' + screenWidth);
    document.getElementById('SearchPopup').style.left = popupX + 'px';
    document.getElementById('SearchPopup').style.top = posY + 20 + 'px';
    //alert(document.getElementById('SearchPopup').style.left);
}

function search_igetit(siteName)
{
    //var strServer = 'http://64.78.239.123/search';
    var strServer = '../Search/search_gsa.aspx';
    var strPar;
    var strText;
    var strSource;
    var strScopeInner;
    var strScope;
    var strHeight = '720';
    var screenHeight;
    
    if(document.all) {//this is IE 4+
        if(document.documentElement.clientHeight)
        {
            screenHeight = document.documentElement.clientHeight;
        }
        else
        {
            screenHeight = document.body.clientHeight;
        }
    }
    else {  //For NS, Firefox
            screenHeight = window.innerHeight;
    }
    screenHeight = screenHeight - 160;
    if (screenHeight < 400) {
        strHeight = '400';
    }
    else {
        strHeight = String(screenHeight);
    } 
    
    if( siteName == "find" ) {
        strScope = document.getElementById('searchScope').value;
    }
    else {
	    strScope = siteName;
        document.getElementById('searchScope').value = strScope;
    }
    
    findPos('SearchButton');
    switch (strScope) {
        case "course":
            strPar = 'entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8&client=app_frontend&proxystylesheet=app_frontend&proxyreload=1&site=igetit_courses_public&filter=0';
            strScopeInner = 'SearchScopeInner0';
            break;
        case "document":
            strPar = 'entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8&client=app_frontend&proxystylesheet=app_frontend&proxyreload=1&site=igetit_documents_public&filter=0';
            strScopeInner = 'SearchScopeInner1';          
            break;
        case "techtips":
            strPar = 'entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8&client=app_frontend&proxystylesheet=app_frontend&proxyreload=1&site=techtips_public&filter=0';
            strScopeInner = 'SearchScopeInner2';          
            break;
        default:
            strPar = 'entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8&client=app_frontend&proxystylesheet=app_frontend&proxyreload=1&site=igetit_courses_public&filter=0';
    }
    strText = document.getElementById("SearchBoxInput").value;

    //unhighlight all the scope menu items
    document.getElementById('SearchScopeInner0').style['background'] = 'transparent none repeat scroll 0%';
    document.getElementById('SearchScopeInner0').style['border'] = 'none';
    document.getElementById('SearchScopeInner0').style['padding'] = '5px 12px';
    document.getElementById('SearchScopeInner1').style['background'] = 'transparent none repeat scroll 0%';
    document.getElementById('SearchScopeInner1').style['border'] = 'none';
    document.getElementById('SearchScopeInner1').style['padding'] = '5px 12px';
    document.getElementById('SearchScopeInner2').style['background'] = 'transparent none repeat scroll 0%';
    document.getElementById('SearchScopeInner2').style['border'] = 'none';
    document.getElementById('SearchScopeInner2').style['padding'] = '5px 12px';
    if (document.getElementById('SearchScopeInner3'))
    {
        document.getElementById('SearchScopeInner3').style['background'] = 'transparent none repeat scroll 0%';
        document.getElementById('SearchScopeInner3').style['border'] = 'none';
        document.getElementById('SearchScopeInner3').style['padding'] = '5px 12px';
    }
    
    //highlight the selected scope: course, document or tips.
    document.getElementById(strScopeInner).style['background'] = 'rgb(150, 100, 150) none repeat scroll 0%';
    document.getElementById(strScopeInner).style['border'] = 'solid 1px rgb(157, 178, 180)';
    document.getElementById(strScopeInner).style['padding'] = '4px 12px';
    strText = document.getElementById("SearchBoxInput").value;
    if(strText.length == 0) {
        alert("Please type the text for search in the text box!");
        strSource = '../search/search_blank.htm';
    }
    else {
        strText = escape(strText);
        //escape will not process +, it should be turned to %2B; / should be turned to %2F
        strText = strText.replace("+", "%2B");
        strText = strText.replace("/", "%2F");
        //escape will turn space to %20, in URL-encode, space should be +
        strText = strText.replace("%20", "+");
        //alert(strText);
        strSource = strServer + '?q=' + strText + '&' + strPar + '&winH=' + strHeight + '&igiSearch=1';
    }
    document.getElementById('SearchPopup').style.display = 'block';
    document.getElementById('SearchResult').style.display = 'block';
    document.getElementById('SearchMinimize').style['background'] = 'url(../images/min_normal.gif) no-repeat';
    document.getElementById('searchResultIframe').src = strSource;
    return false;
}

function search_close_popup()
{
    document.getElementById('SearchPopup').style.display = 'none'
}
        
function search_textbox() 
{ 
    var q = document.getElementById('SearchBoxInput'); 

  if (q) {
    var n = navigator; 
    var l = location; 
    if (n.platform == 'Win32') 
    { q.style.cssText = 'border: 1px solid #7e9db9; padding: 2px;';
    } 

    var b = function() 
      { if (q.value == '')
	{ 
		q.style.background = '#FFFFFF url(../images/googlesearch_watermark.gif) left no-repeat';
	 }
      }; 
    var f = function() 
	{ q.style.background = '#ffffff'; 
	}; 
    q.onfocus = f; 
    q.onblur = b; 
    if (!/[&?]q=[^&]/.test(l.search)) 
    { b(); }
  }
}

function search_textbox_keypress(eventObj)
{

    if (eventObj.keyCode == 13)
    {
        search_igetit('find');
        eventObj.returnValue=false; 
        eventObj.cancel = true;
        return false;
    }
    else {
        return true;
    }
}

function set_to_laststatus(strQuery, strSource, strSite)
{
    var strText;
    var strServer = '../Search/search_gsa.aspx';
    var strScopeInner = '';
    var strScope = 'course';
    
    switch (strSite) {
        case "igetit_courses":
            strScopeInner = 'SearchScopeInner0';
            strScope = 'course';
            break;
        case "igetit_documents_public":
            strScopeInner = 'SearchScopeInner1';
            strScope = 'document';         
            break;
        case "techtips":
            strScopeInner = 'SearchScopeInner2';
            strScope = 'techtips';    
            break;
        case "igetit_mycontents":
            strScopeInner = 'SearchScopeInner3';
            strScope = 'mycontents';
            break;        
        default:
            strScopeInner = 'SearchScopeInner0';
    }
    
    findPos('SearchButton');
    strText = escape(strQuery);
    strText = strText.replace("+", " ");
    document.getElementById("SearchBoxInput").value = strText;
    document.getElementById('searchResultIframe').src = strServer + strSource + "&q=" + strQuery + "&site=" + strSite;
    document.getElementById('SearchPopup').style.display = 'block';
    document.getElementById('SearchResult').style.display = 'block';
    document.getElementById('SearchMinimize').style['background'] = 'url(../images/min_normal.gif) no-repeat';
    document.getElementById('searchScope').value = strScope;
    
    //highlight the selected scope: course, document or tips.
    if (document.getElementById(strScopeInner))
    {
        document.getElementById(strScopeInner).style['background'] = 'rgb(150, 100, 150) none repeat scroll 0%';
        document.getElementById(strScopeInner).style['border'] = 'solid 1px rgb(157, 178, 180)';
        document.getElementById(strScopeInner).style['padding'] = '4px 12px';
    }
    
}