/*
*******************************************************************************
* Copyright (C) 2004-2005 PurposeDriven.Com. All Rights Reserved.
* Unauthorized use, duplication or distribution is strictly prohibited.
*******************************************************************************
*/

// -------------------------------------------------------------------------------------------------
// Search Section 
// -------------------------------------------------------------------------------------------------
var bTbTermHasFocus = false;

function SearchTermOnFocus()
{
	bTbTermHasFocus = true;
}

function SearchTermOnBlur()
{
	bTbTermHasFocus = false;
}


function GoToSearch( event, prefix ) {
	var keycode;
	if( window.event ) {
		// ie code
		if( window.event.keyCode == 13 ) {
			var control = document.getElementById( prefix + "_lnkSearch" );
			if( control != null )
				control.click();				
			event.keyCode = 0;
			return false;
		}
	}
	else if( event ) {
		// gecko engine code
		if( event.which == 13 ) {
			var control = document.getElementById( prefix + "_lnkSearch" );
			if( control != null )
				control.click();
			event.preventDefault();
			return false;
		}
	}
}

function PerformSearch( prefix, pageName ) {

	var _term = document.getElementById( prefix + '_tbSearchTerm' );
	// don't act if they do not search if term is missing
	if( _term != null )
	{
		if ( ( _term.value.length > 0 ) && ( bTbTermHasFocus ) )
		{
			var searchPage;
			if( pageName && ( pageName.length > 0 ) ) {
				searchPage = pageName;
		    }
			else {
				searchPage = location.pathname;
			}
			// qualify by search term
			searchPage += '?term=' + escape( _term.value );
			
			var addedSearchType = false;
			// qualify by search type: see if search type exists or is specified ( exists for pcom/pdl only )
			var l_type = document.getElementById( prefix + '_ddlSearchType' );
			if( l_type != null ) {
				var selected = l_type.options[ l_type.selectedIndex ];			
				if( selected != null ) {
					searchPage += '&searchType=' + escape( selected.value );
					addedSearchType = true;				        				
				}
			}

			// qualify by search type: pass along if it exists
			if( !addedSearchType && ( location.search != null ) && ( location.search.indexOf( '&searchType=' ) > -1 ) ) {
			    var valuePairs = location.search.split( '&' );
	            for( i = 0; i < valuePairs.length; i++ ) {
		            if( valuePairs[ i ].indexOf( 'searchType=' ) > -1 ) {
			            searchPage += '&' + valuePairs[ i ];
			            break;
		            }
	            }			    
			}				
		
			// qualify by category:  see if we need to categorize
			var _category = document.getElementById( prefix + '_ddlCategories' );
			if( ( _category != null ) && ( _category.selectedIndex != 0 ) ) {
				var selected = _category.options[ _category.selectedIndex ];			
				if( selected != null ) {
					searchPage +=  '&category=' + escape( selected.value );
				}
			}
			
			document.location.assign( searchPage );
		}
	}
}			

// -------------------------------------------------------------------------------------------------
// Cookie Manipulation Section 
// -------------------------------------------------------------------------------------------------
function getCookie( p_searchCookieName ) {
	var cookies = document.cookie.split( ';' );
	for( i = 0; i < cookies.length; i++ ) {
        // sometimes cookies begin with spaces, so we replace them
        var cookieValue = cookies[ i ].replace( ' ', '' );
        // we add the "=" character to indicate that it is the Name portion of the Name/Value pair
		var foundAtBeginning = cookieValue.indexOf( p_searchCookieName + "=" );

        // we are looking for the p_searchCookieName in the 0th index, because that is the beginning..  
        // we might have values that include the p_searchCookieName, so we are checking that it is in the beginning only
		if( foundAtBeginning == 0 ) {
			return cookieValue.substring( foundAtBeginning + p_searchCookieName.length + 1 );
		}
	}			
	return null;
}

function getCookieCrumbValue( Cookie, CrumbName ) {
	if (Cookie != null && CrumbName != null) {
		var cookieCrumbs = Cookie.split( '&' );
		for( i = 0; i < cookieCrumbs.length; i++ ) {
			var crumb = cookieCrumbs[ i ].split( '=' );
			if( crumb[ 0 ] == CrumbName ) {
				if( ( crumb[ 1 ].length > 0 ) && ( crumb[ 1 ] != '0' ) )
					return crumb[ 1 ];
				else 
					return null;
			}
		}
	}
	return null;
}

// -------------------------------------------------------------------------------------------------
// Visual Modification Section 
// -------------------------------------------------------------------------------------------------
function ChangeEnabledState( ControlID, IsEnabled ) {
	var _control = document.getElementById( ControlID );
	if( _control != null )
		_control.disabled = !IsEnabled;
}

function ChangeVisibleState( ControlID, IsVisible ) {
	var _control = document.getElementById( ControlID );
	if( _control != null ) {
		_control.style.visibility = IsVisible ? "visible" : "hidden";
	}
}

function ChangeDisplayState( ControlID, Show ) {
    var _control = document.getElementById(ControlID);
    if (_control == null) return;
	else if (_control.style != null) {
		_control.style.display = Show ? "" : "none";
	}
}

function ChangeCheckedState( ControlID, IsChecked ) {
	var _control = document.getElementById( ControlID );
	if( _control != null ) {
		_control.checked = IsChecked;
	}	
}


// -------------------------------------------------------------------------------------------------
// URL Manipulation Section 
// -------------------------------------------------------------------------------------------------
function UpdateOrAddURLParam( originalQuery, paramName, paramValue ) {
	var bReplaced = false;
	var modifiedQuery = "";
	var params = ( originalQuery == null ) ? "" : originalQuery.split( "&" );
	
	var lowerCaseCurrentParam = paramName.toLowerCase();
	for( var i = 0; i < params.length; i++ ) {
	    var copyUpper = params[ i ].toLowerCase()
		if( copyUpper.indexOf( lowerCaseCurrentParam ) >= 0 ) {
			// when replacing the PageNumber query
			modifiedQuery += ( i > 0 ) ? '&' : '?';
			modifiedQuery += paramName + "=" + paramValue;
			bReplaced = true;
		}
		else {
			if( ( params[ i ].indexOf( '?' ) < 0 ) && ( params[ i ].indexOf( '&' ) < 0 ) )
				modifiedQuery += ( i > 0 ) ? '&' : '?';
			modifiedQuery += params[ i ];
		}	
	}

	if( !bReplaced ) {	// when we need to add the param
		// if it has a param already.. use '&' instead of '?'
		modifiedQuery += ( params.length > 0 ) ? '&' : '?';
		modifiedQuery += paramName + "=" + paramValue;
	}

	return modifiedQuery;
}

// -------------------------------------------------------------------------------------------------
// Misc. Section 
// -------------------------------------------------------------------------------------------------
function IsPOBox( address ) {
	var reg = new RegExp( "[.| ]", "g" );
	var addr = address.replace(reg, "");
	addr = addr.toUpperCase();
	var index = addr.indexOf( "POBOX", 0 );
	if( index >= 0 )
		return true;
	return false;
}

//--------------- GetKey 
function GetPressedKey( e )
{
	if( window.event ) 	// for ie
		return window.event.keyCode;
	else if( e ) 		// for gecko engine
		return e.which;
	else 
		return null;
}

//--------------- Check for valid key
function IsKeyInCharList( pressedKey, validCharList, acceptControlKeys )
{
	if( pressedKey == null ) 
		return true;

	// get character
	var keychar;
	keychar = String.fromCharCode( pressedKey );
	keychar = keychar.toLowerCase();
	validCharList = validCharList.toLowerCase();
	
	// check if it's a valid key
	if (validCharList.indexOf( keychar ) != -1)
		return true;

	// control keys -- only if we specify that we want to accept control keys
	if( acceptControlKeys )
		if ( pressedKey==null || pressedKey==0 || pressedKey==8 || pressedKey==9 || pressedKey==13 || pressedKey==27 )
			return true;

	// else return false
	return false;
}

//--------------- Textbox Navigation
function enforceTextboxLength( textboxControl, nextControl )
{
	var textboxValue = textboxControl.value;
	var maxLength = textboxControl.maxLength;

	if (textboxValue.length >= maxLength)
	{	
		try {
			document.getElementById(nextControl).focus();
		} catch (e) {}
	}
}

//--------------- Standard Checks
function isNumeric( event )
{
    return IsKeyInCharList( GetPressedKey( event ), "0123456789", true );   
}

function hasFlashPlugin()
{
	var flashVersion = 0;				
	if ( navigator.plugins && navigator.plugins.length > 0 )
	{
		var plugin = navigator.plugins["Shockwave Flash"];
		if ( !plugin )
			plugin = navigator.plugins["Shockwave Flash 2.0"];
		if ( plugin )
		{
			var desc = plugin.description;
			if ( desc )
			{
				flashVersion = desc.charAt( desc.indexOf('.') - 1 );
			}
			
			if ( flashVersion >= 6 )
				return true;
			else
				return false;
		}
	}

	// Since we cannot determine the version number of these, 
	// we'll have to assume they won't work.				
//				if ( navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] )
//				{
//					var plugin = navigator.mimeTypes['application/x-shockwave-flash'];
//					if ( plugin && plugin.enabledPlugin )
//						return true;
//					else
//						return false;
//				}

	// To detect on IE based browsers, excepted IE 5 on Mac, we need to try to create
	// the flash object.
	for( var i = 7; i > 5; i -- )
	{	
		try
		{
			var flash = new ActiveXObject( "ShockwaveFlash.ShockWaveFlash." + i );
			if ( flash && i >= 6 )
				return true;
		}
		catch( e )
		{
		}
	}
	
	// flash version below 6, cannot determine version, or no plugin found.				
	return false;
}

function createFlashObject( embedString, divId )
{
	if ( hasFlashPlugin() )
	{
		var divLayer = document.getElementById( divId );
		if ( divLayer != null )
		{
			var flashHTML = embedString
			divLayer.innerHTML = flashHTML;				
		}
	}
}


