function switchLanguage( pTargetLanguage ) {
	
	//Update the language cookie to the new target language
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+90);
	document.cookie = "language=" + pTargetLanguage + ";path=/;expires='" + exdate.toGMTString() + "'";

	var wUrl = document.location.href;
	if ( wUrl.indexOf( '.jsp' ) > 0 ) {
		var wUrlBeginning = wUrl.substring( 0, wUrl.indexOf( '.jsp' ) - 2 );
		var wUrlEnding = wUrl.substring( wUrl.indexOf( '.jsp' ) );
		wUrl = wUrlBeginning + pTargetLanguage + wUrlEnding;
	} else {
		//We are on the home page so switch to the target language page
		wUrl = '/index-' + pTargetLanguage + '.jsp';
	}
	document.location.href = wUrl;
	
}

function getUrlParameter( pParameterName )
{
  pParameterName = pParameterName.replace(/[[]/,"\\\[").replace(/[]]/,"\\\]");
  var regexS = "[\\?&]"+ pParameterName +"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeUTFParameter( unescape( results[1] ) );
} 

function decodeUTFParameter(utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string.replace( "+", " " );
	}

function resetSearchCriteria() {
	//If there was input criteria from the search box, put it back in the search box 
	var wSearchCriteria = getUrlParameter( 'q' );
	if ( wSearchCriteria != null && wSearchCriteria.length > 0 ) {
		document.getElementById( 'searchBox' ).value = wSearchCriteria;
	}
}

function executeGoogleSearch( pSearchLanguage, pSearchCriteria ) {
	google.load('search', '1', {"language" : pSearchLanguage });
	google.setOnLoadCallback(
		function(){
			var searchControl = new google.search.SearchControl();
			
			var siteSearch = new google.search.WebSearch();
			siteSearch.setSiteRestriction("002550041611703613216:wdw5eercpqk");
			siteSearch.setRestriction(google.search.Search.RESTRICT_EXTENDED_ARGS, { "lr" : "lang_" + pSearchLanguage, "filter" : "0"});
			
			var searchOptions = new google.search.SearcherOptions();
			searchOptions.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
			searchControl.addSearcher(siteSearch, searchOptions);
			
			searchControl.setResultSetSize( google.search.Search.FILTERED_CSE_RESULTSET );
			
			var options = new google.search.DrawOptions();
			options.setSearchFormRoot('cse-search-form');
		
			searchControl.draw('cse', options);
			
			searchControl.execute( pSearchCriteria );
			
			searchControl.setSearchCompleteCallback( this, 
				function displaySearchExtraInfo(searchControl, searcher) {
					if ( searcher.results && searcher.results.length > 0) {
						//Fill the string Results X to Y of estimated Z for criteria Q
						document.getElementById( 'result1' ).innerHTML = ( searcher.cursor.currentPageIndex * 10 ) + 1;
						document.getElementById( 'result2' ).innerHTML = ( searcher.cursor.currentPageIndex * 10 ) + searcher.results.length;
						document.getElementById( 'estimatedResults' ).innerHTML = searcher.cursor.estimatedResultCount;
						document.getElementById( 'searchString1' ).innerHTML = pSearchCriteria;
						document.getElementById( 'resultsFound' ).style.display = 'block';
						if ( searcher.cursor.estimatedResultCount > 100 ) {
							//More than 100 results (10 pages of 10) is an estimate, so show the estimate label: " of about"
							document.getElementById( 'estimatedResultsLabel' ).style.display = '';
						} else {
							//Show the exact label: " of "
							document.getElementById( 'exactResultsLabel' ).style.display = '';
						}
					} else {
						document.getElementById( 'searchString2' ).innerHTML = pSearchCriteria;
						document.getElementById( 'noResultsFound' ).style.display = 'block';
					}
					
				}			
			);

		}
	, true);
}
