﻿//basicSearch.js

/* add trim function to javascript strings */
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

/* set global variables */
var _TypeID = 0;

/* set the focus on the search box */
function FocusOnSearch()
{
    try
    {
        $('txtBasicSearchString').focus();
    }
    catch(err)
    {    
    }
}
DomLoaded.load(FocusOnSearch);

/* set basic search string from cookie */
function PopulateBasicSearch()
{
    if(Get_Cookie('IBsearch') != null)
    {
        $('txtBasicSearchString').value = Get_Cookie('IBsearch');
    }
}
DomLoaded.load(PopulateBasicSearch);

function BasicSearchType(searchType)
{    
    // remove selected from all options
    $('lnkAll').className = "";
    $('lnkPerson').className = "";
    $('lnkProject').className = "";
    $('lnkCompany').className = "";
    
    switch (searchType.toLowerCase()) {
        case 'person':
            $('lnkPerson').className = "selected";
            _TypeID = 2; 
            break;
        case 'project':
            $('lnkProject').className = "selected";
            _TypeID = 3;
            break;
        case 'company':
            $('lnkCompany').className = "selected";
            _TypeID = 1;
            break;
        default: 
            $('lnkAll').className = "selected";
            _TypeID = 0;
    } 
}

function BasicSearch()
{
    //regex to remove space between words
    var reTrimSpaces= /\s+/g;
    
    // grab search string
    var searchString = $('txtBasicSearchString').value.replace(reTrimSpaces, " ").trim();
           
    // set cookie for 24 hours
    Set_Cookie('IBsearch',searchString,1,'/','','');

    if(searchString.length > 0)
    {
        RedirectToResults(_TypeID,searchString);
    }
}

function RedirectToResults(searchType,searchString)
{   
    var searchUrl = GetSearchResultUrl();

    this.document.location.href = searchUrl + "?typeId=" + escape(searchType) + "&keyword=" + escape(searchString);
}

/* Cookie management from: http://techpatterns.com/downloads/javascript_cookies.php */

/* Example: Set_Cookie( 'mycookie', 'visited 9 times', 30, '/', '', '' );*/
function Set_Cookie( name, value, expires, path, domain, secure ) 
{
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    /*
    if the expires variable is set, make the correct 
    expires time, the current script below will set 
    it for x number of days, to make it for hours, 
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires )
    {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" ) + 
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			if(a_temp_cookie[1])
			{
			    cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}				

function Delete_Cookie( name, path, domain ) 
{
    if ( Get_Cookie( name ) ) 
        document.cookie = name + "=" +
        ( ( path ) ? ";path=" + path : "") +
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
//End basicSearch.js

//viewMoreLess.js
/* Common js for "view more", "view less" buttons */

/***
targetLayer = the id of a div, span, tablebody tag you want to hide/show
more = the id of a div or span that surrounds the view more link and graphic 
less = the id of a div or span that surrounds the view less link and graphic 
***/

function viewMore(targetLayer, more, less)
{
    /*new Effect.BlindDown(targetLayer, {duration:_speed});
    new Effect.Fade(more, {duration:.0});
    new Effect.Appear(less, {duration:_speed});*/
    $(targetLayer).style.display = '';
    $(more).style.display = 'none';
    $(less).style.display = '';
}

function viewLess(targetLayer, more, less)
{
    /*new Effect.BlindUp(targetLayer, {duration:_speed});
    new Effect.Appear(more, {duration:_speed});
    new Effect.Fade(less, {duration:.0});*/
    $(targetLayer).style.display = 'none';
    $(more).style.display = '';
    $(less).style.display = 'none';

}

function viewMoreToggle(moreLayer, lessLayer, moreBtn, lessBtn)
{
    $(moreLayer).style.display = '';
    $(lessBtn).style.display = '';
    $(lessLayer).style.display = 'none';
    $(moreBtn).style.display = 'none';
}

function viewLessToggle(moreLayer, lessLayer, moreBtn, lessBtn)
{
    $(moreLayer).style.display = 'none';
    $(lessBtn).style.display = 'none';
    $(lessLayer).style.display = '';
    $(moreBtn).style.display = '';
}
//end viewMoreLess.js
//focusInput.js

function addLoadEvent(func)
{
    var oldonload = window.onload;
    
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function()
        {
            oldonload();
            func();
        }
    }
}

function setFocus()
{
    var labels = document.getElementsByTagName("label");
    //var focusElements = {"input", "select"};
    var focusElement;
    for (var lcnt=0; lcnt < labels.length; lcnt++)
    {
        labels[lcnt].onclick = function()
        {
            try
            {
                if (this.parentNode.getElementsByTagName("input").length > 0)
                {
                    this.parentNode.getElementsByTagName("input")[0].focus();
                }
                else if (this.parentNode.getElementsByTagName("select").length > 0)
                {
                    this.parentNode.getElementsByTagName("select")[0].focus();
                }
                else if (this.parentNode.getElementsByTagName("textArea").length > 0)
                {
                    this.parentNode.getElementsByTagName("textArea")[0].focus();
                }
            }
            catch (err)
            {}
        }
    }
}
addLoadEvent(setFocus);
//End focusInput.js

//Sets the cookie and redirects to the browse.aspx page.
function gotoBrowse(currentTab)
{
    Set_Cookie('Browse_CurrentTab',currentTab,1,'/','','');
    document.location.href="Browse.aspx";
}

function searchOnEnter(e)
{
    var keycode; 

    if (window.event) 
    { 
        keycode = window.event.keyCode; 
    } 
    else if (e) 
    { 
        keycode = e.which; 
    } 
    else 
    { 
        return true; 
    } 

    if (keycode == 13) 
    { 
        BasicSearch();
        return false; 
    } 
    else 
    { 
        return true; 
    }
}