/* 
Readable version of the Javascript/common.js file 

The "Javascript/common_unpacked.js" file must contain the latest changes.

Compressing JS code:
==================
Requirements:
1) Two script libraries:
	- Javascript/common.js					(file downloaded by browsers, when live, it must be compressed/compacted)
	- Javascript/common_unpacked.js  	(this file - readable version)

Steps:
1) Make necessary changes to JS code below.
2) Fully test that the js code is working
3) Check requirements listed above have been met.
4) Copy this file contents and pasted into a JS compactor/compressor website
		Use http://javascriptcompressor.com/ a compressed version
5) Paste compressed js code into Javascript/common.js
6) Save ...
7) Smile!

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 

Adjust the following lines:
var cookieName = '.....' ;
var searchURL = '/......nsf/Search?OpenForm&Query=' ;

*/

var cookieName = 'HRGUIDE_ViewMode' ;
var searchURL = '/Sectors/HRG/HRGuide.nsf/Search?OpenForm&Query=' ;

function SetPageVariables() {

	// set the following variables :
	// this.IE50  - browser = IE 5.0 - 5.49
	// this.IE55up - browser = IE 5.5+
	// this.Mozzies - browser = netscape 7+ and mozilla family
	// this.BrowserIsCompatible - Site is designed for IE 5+ & Netscape 7+ & Mozillas
	// this.PageMode - what mode is this page? R = RichText, T = Plain Text

	// browser compatibility ...
	// only intersted in IE 5+, NS7+ and Mozzie family.

	var agent = navigator.userAgent.toLowerCase();
	var version = parseInt(navigator.appVersion);
	var appVer = navigator.appVersion.toLowerCase();
	var iePos  = appVer.indexOf('msie');
	var ieVer = appVer.substring(iePos+5,appVer.indexOf(';',iePos)) ;

	Opera = (agent.indexOf('opera') != -1) ;

	this.IE50 = ( ( iePos !=-1 ) && ( ( parseFloat(ieVer) >=5.0 ) &&  ( parseFloat(ieVer) <5.5 ) ) && !( Opera ) ) ;
	this.IE55up = ( ( iePos !=-1 ) && ( parseFloat(ieVer) >=5.5 ) && !( Opera ) ) ;

	NS6 = (agent.indexOf('netscape/6') !=-1 ) || (agent.indexOf('netscape6') !=-1 ) ;
	NSC = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)) && (version >=5 ) );
	
	this.Mozzies = ( NSC && !( NS6 || Opera ) ) ;
	this.BrowserIsCompatible = ( IE50 || IE55up || Mozzies ) ;

	// set PageMode for the menu hover mimic code.
	var pmvalue = ( BrowserIsCompatible ) ? 'R' : 'T' ;
	if ( document.cookie )
	{
		index = document.cookie.indexOf(cookieName);
		if ( index >=0 ) 
		{
			pmvalue = document.cookie.substr( index + cookieName.length + 1 , 1 ) ;
		}
	} 
	
	this.PageMode = pmvalue ;

	return true ;
}

// set global variables ..
SetPageVariables() ;

// switch page mode .. for switching between graphic and text mode ..
function SwitchPageMode( pmode ) {
	var cexpire = new Date() ;
	cexpire.setFullYear( cexpire.getFullYear() +1 ) ; 

	pgmode = ( BrowserIsCompatible ) ? pmode : 'T' ;

	document.cookie = cookieName + '=' + pgmode + '; expires=' + cexpire.toGMTString() + '; path=/;' ; 
	window.location.reload( true )
}


function get_cookie ( cookie_name )
{
    // http://www.thesitewizard.com/javascripts/cookies.shtml
    var cookie_string = document.cookie ;
    if (cookie_string.length != 0) {
        var cookie_value = cookie_string.match (
                        '(^|;)[\s]*' +
                        cookie_name +
                        '=([^;]*)' );
        return decodeURIComponent ( cookie_value[2] ) ;
    }
    return '' ;
}

/* capture enter event - for search box */
function isEnterPressed( keyevent , obj ) {

	// keyboard detection ...
	if(window.event) // IE
  	{
 		keynum = keyevent.keyCode;
  	}
	else if(keyevent.which) // Netscape/Firefox/Opera
  	{
  		keynum = keyevent.which;
  	}
	
	if ( ( keynum == 13 )  || ( obj.type == "button" ) )
	{
		if ( window.event ) 
		{
			// stops the form from being submitted ...   (IE code)
			window.event.cancelBubble = true;	
			window.event.returnValue = false;
		}
		else
		{
			// stops the form from being submitted ... (Netscape/Firefox/Opera/etc code)
			keyevent.stopPropagation() ;
			keyevent.preventDefault();
		}
			return true ;
	}
	else
	{
		return false ;
	}
}

// next three functions are for the search facility.
// =================================================================
function trimspaces(aStr) {
	return aStr.replace(/^\s{1,}/, "").replace(/\s{1,}$/, "")
}

function SearchTermOk( qstr , search_hint ) {

	var regExp1 = /\bfield\b/i; 		//used to test for reserved word field in query string
	var regExp2 = /[(,),<,>,\[,\]]/; 		//used to test for reserved char(s) in the query string

	var str = trimspaces( qstr );

	if ( str == "" )
	{
		alert( "Please enter something to search for." );
		return 1 ;
	} 
	else 
	if ( str == search_hint )
	{
		alert( "Please enter something to search for." );
		return 1 ;	
	}
	else	
	{
		if ( typeof regExp1.source != 'undefined' ) 
		{ 	//supports regular expression testing
			if ( regExp1.test( str ) || regExp2.test( str ) ) 
			{
				var alrt = "Please note that you can not include:\n\n";
				alrt += "The reserved word 'field'\n";
				alrt += "The characters comma, [, ], (, ), < or >\n\n";
				alrt += "in your search query!";
				alert( alrt );
				return 2 ;
			}
			else
			{
				// search term is reasonably ok ...
				return 0 ;
			}
		}
		else
		{
			alert( "Unsupported browser function - search cannot proceed.\n\nPlease use a different browser." ) ;
			return 3 ;
		}
	}
}

function searchSite( keyevent, obj, searchHint ) {

	if ( isEnterPressed( keyevent , obj ) )
	{
		if ( ( obj.name == 'siQuery' ) || ( obj.name == 'BTNSearch' ) )
		{
			var qr = SearchTermOk( document.forms[0].siQuery.value , searchHint ) ;
			var qs = document.forms[0].siQuery.value ;
		}
		else if ( ( obj.name == 'siQuery2' ) || ( obj.name == 'BTNSearch2' ) )
		{
			var qr = SearchTermOk( document.forms[0].siQuery2.value , searchHint ) ;
			var qs = document.forms[0].siQuery2.value ;
		}
		if ( qr == 0 ) 
		{
		  		window.open( searchURL + trimspaces( qs ) , "_top" ) ;
		}
	}
}

// next two functions are for the additional options facility at the bottom of the inner pages.
// =================================================================
function bookmarkSite(title, url){
 
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} else if(window.opera && window.print) {
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} else if(document.all) {
		window.external.AddFavorite(url, title);
	}
}

function validateLogin( ) 
{
	if (document.getElementById('Username').value == "") {
		alert('Please supply a User Name to login.');
		return false;
	}

	if (document.getElementById('Password').value == "") {
		alert('Please supply a Password to login.');
		return false;
	}
}  //end function validateLogin


function openPopup( srcfile ) 
{
	var wTmp = window.open(srcfile, "_puPodcast", "width=500,height=275,left=55,top=50,resizable=yes,scrollbars=yes,status=no,toolbar=no,menubar=no" );
	wTmp.focus();
}


function ChangeSection( theField )
{
theValue = ""; 

for ( i = 0; i < theField.options.length; i++ ) 
  {
  if ( theField.options[i].selected ) 
    {
      theValue += theField.options[i].value ;
     break ;
    }
  }

if ( theValue != "ignore" )
{
window.open( '/Sectors/HRG/HRGuide.nsf/vPages/' + theValue + '?OpenDocument' , '_self' )
}
return( true ) ;
}
