/*
	BROWSER ALERT SCRIPT v2.1

	The purpose of this script is to throw an alert, and redirect to a specific
	page, if a certain type of 'user agent' (browser) is detected.

	Copyright (C) 2009 Paul Mobbs/Free Range Network.
	
	Produced by Paul Mobbs/The Free Range electrohippie collective 2009
	Released under the Gnu General Public License v3.0 -- for a copy of the 
	license goto http://www.fraw.org.uk/fraw_admin/gpl-3.html or see 
	http://www.gnu.org/licenses/
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
*/



// DECLARE SCRIPT VARIABLES
var idInfoString = new Array( "Microsoft", "MSIE");				// set fields to test strings to find in user agent ID 
var alertTextString = "BROWSER ALERT!!\n\nYou appear to be using a browser based on proprietary software.\n" +
			"This could compromise your free access to the Internet at some\ntime in the future.\n\n" +
			"To find out why this is a problem click 'OK', otherwise click 'Cancel'.\n\n";	// alert string content 
var cookieExpireDays = 30;									// set for number of days for cookie expiry 
var cookieName = "browseralert";								// define name of the cookie 
var redirectUrlString = "/ehippies/browser_alert/index.shtml";		// target url for alert redirection 
var setCookieString = "PREVENT ALERTS IN FUTURE\n\nTo stop the alert popping-up on the FRAW site in future you can \n" +
						"set a cookie (which will expire in " + cookieExpireDays + " days).\n\n" +
						"To set a cookie click 'OK' or to skip click 'Cancel'.\n\n";	// prevent alerts string 



// BROWSER ALERT 'POLL' FUNCTION
// This function controls the operation of browser alert system. This 
// function is called from the web page and calls the required functions 
// for user ID polling, string scanning and redirection.
function BrowserAlertPoll()
{
	// declare variables
	var retval = null;

	if( LoadCookies() != 0)								// execute if no cookie was detected 
	{
		if ( ScanUserAgent() > 0) {						// execute if user agent ID's were found 
			retval = confirm( alertTextString);				// display 'browser alert' 
			if (retval == true)							// if 'OK', redirect to alert target page 
			{
				document.location.href = redirectUrlString;	// redirect to target page 
			}
			if( confirm( setCookieString))					// if 'prevent alerts' is 'OK' set a cookie 
			{
				SetCookie();								// call set cookie function 
			}
		}
	}
}


// BROWSER ALERT TEST FUNCTION 
// This function forces the alert to to work irrespective 
// of the browser used to call it. 
function BrowserAlertTest()
{
	retval = confirm( alertTextString);						// display alert string 
	if (retval == true)									// execute if response 'OK' 
	{
		document.location.href = redirectUrlString;		// redirect to target page 
	}
	if( confirm( setCookieString))							// execute if prevent cookies response 'OK' 
	{
		SetCookie();										// set a cookie 
	}
}


// LOAD COOKIE VALUES FUNCTION
// This function load the cookie values into the 'cookies' array for 
// checking by the 'scan' function 
function LoadCookies()
{
	// declare function variables 
	var input = document.cookie;							// load cookie string value 

	// check if a cookie is set
	if ( input == "" || input.indexOf( escape( cookieName) + '=') == -1)		// execute if the cookie has been set
	{
		return( 1);										// return 'set' 
	}
	return( 0);											// return 'clear' 
}
 

// SCAN USER AGENT FUNCTION
// This function scans the user agent ID 
// for a pre-defined list of strings. 
function ScanUserAgent()
{
	// declare function variables
	var agent = navigator.userAgent;						// load user agent string value 
	var ctr = 0;											// string matching counter 
	var ptr = 0;											// array index pointer 

	// check for incidence of each ID string
	for ( ptr = 0; ptr < idInfoString.length; ptr++)			// read through ID strings array 
	{ 
		if ( agent.indexOf( idInfoString[ ptr]) > -1)			// execute if ID string matches cookie string 
		{
			ctr++;										// increment match counter 
		}
	}

	// return count value 
	return( ctr);											// return string match count 
}


// SET COOKIE FUNCTION 
// This function sets a cookie with an expiry date 
function SetCookie()
{
	var expire = new Date( ( new Date()).getTime() + cookieExpireDays * 86400000);	// make expire date object 
	
	document.cookie = escape( cookieName) + '=set; EXPIRES=' + expire.toGMTString();	// set cookie 

	return( 0);											// return 'success'
}


// END OF SCRIPT
