/*
        File: browser.js
     Version: 1.0

     Created: 02/05/05
    Modified: 13/04/06
   Copyright: Matthew Cassidy 2005
       Email: cassidym@bne.catholic.net.au

     Purpose: Returns information about the client browser and performs browser functions
        Note: None

     License: For use solely by the Catholic Archdiocese of Brisbane. 
*/


/*----------------------------------------*/
/* Function: clientAppName()              */
/* Returns the name of the client browser */
/*----------------------------------------*/
function clientAppName() {
	return navigator.appName;
}


/*-------------------------------------------*/
/* Function: clientAppVer()                  */
/* Returns the version of the client browser */
/*-------------------------------------------*/
function clientAppVer() {
	return navigator.appVersion;
}


/*---------------------------------------------------------*/
/* Function: testClient(details, testCase)                 */
/* Returns true if the client details supplied are correct */
/*---------------------------------------------------------*/
function testClient(details, testCase) {
	if (details.indexOf(testCase) >= 0) { 
		return 1; 
	}
	return 0;
}


/*---------------------------------------------------------*/
/* Function: isIE()                                        */
/* Returns true if the client browser is Internet Explorer */
/*---------------------------------------------------------*/
function isIE() {
	return testClient(clientAppName(), "Internet Explorer");
}


/*-----------------------------------------------*/
/* Function: isMozilla()                         */
/* Returns true if the client browser is Mozilla */
/*-----------------------------------------------*/
function isMozilla() {
	return testClient(clientAppName(), "Netscape");
}


/*---------------------------------------------*/
/* Function: isWebTV()                         */
/* Returns true if the client browser is WebTV */
/*---------------------------------------------*/
function isWebTV() {
	return testClient(clientAppName(), "WebTV");
}


/*------------------------------------------*/
/* Function: isWindows()                    */
/* Returns true if the client OS is Windows */
/*------------------------------------------*/
function isWindows() {
	return testClient(clientAppVer(), "Win");
}


/*--------------------------------------*/
/* Function: isMac()                    */
/* Returns true if the client OS is Mac */
/*--------------------------------------*/
function isMac() {
	return testClient(clientAppVer(), "Mac");
}


/*-----------------------------------------*/
/* Function: isLinux()                    */
/* Returns true if the client OS is Linux */
/*----------------------------------------*/
function isLinux() {
	return testClient(clientAppVer(), "Linux");
}


/*---------------------------------------*/
/* Function: isUNIX()                    */
/* Returns true if the client OS is UNIX */
/*---------------------------------------*/
function isUNIX() {
	return testClient(clientAppVer(), "X11");
}


/*-----------------------------------*/
/* Function: bookmarkUrl(url, title) */
/* Bookmarks the URL provided        */
/*-----------------------------------*/
function bookmarkUrl(url, title) {
	/* Determine if this is IE and bookmark */
	if (isIE) {
		if (document.all) {
			window.external.AddFavorite(url, title);
		}
		else {
			bookmarkError();
		}
	}
	else {
		bookmarkError();
	}
}


/*----------------------------------------------------------------------------------*/
/* Function: bookmarkError()                                                        */
/* Displays the error message to present to users when auto bookmark is unavailable */
/*----------------------------------------------------------------------------------*/
function bookmarkError() {
	alert("Cannot auto-bookmark\nPlease press CTRL+D to bookmark this page");
}


/*-----------------------------------------------------------------------------*/
/* Function: textSize(sheet, cookieName, expireDays)                           */
/* Swaps the style sheet to the number specified by 'sheet'. Also sets cookie. */
/*-----------------------------------------------------------------------------*/
function textSize(sheet, cookieName, expireDays) {
  if (sheet == '+') {
    (currentSheet == numSheets) ? whichSheet = 1 : whichSheet = currentSheet + 1;
    currentSheet = whichSheet;
    whichSheet = whichSheet - 1;
  }
  else {
    whichSheet = parseInt(sheet);
    currentSheet = whichSheet + 1;
  }
  setCookie(cookieName, whichSheet, expireDays);
  if (document.styleSheets) {
    var c = document.styleSheets.length;
    for (var i=0;i<c;i++) {
      if (i!=whichSheet) {
        document.styleSheets[i].disabled=true;
      }
	  else {
        document.styleSheets[i].disabled=false;
      }
    }
  }
}

/*-------------------------------------------------------*/
/* Function: changeSize(sheet, cookieName, expireDays)   */
/* Calls functions to change the stylesheet on body load */
/*-------------------------------------------------------*/
function changeSize(sheet, cookieName, expireDays) {
  if (sheet == 0 || sheet == 1 || sheet == 2) {
    textSize(sheet, cookieName, expireDays);
  }
}


/*-----------------------------------------------*/
/* Function: setCookie(name, data, expireDays)   */
/* Sets the cookie name, data and days to expire */
/*-----------------------------------------------*/
function setCookie(name, data, expireDays) {
  var date = new Date();
  date.setTime(date.getTime()+(expireDays*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
  document.cookie = name+'='+data+'; expires='+expires+'; path=/'
}


/*---------------------------------------------------------*/
/* Function: getCookie(name)                               */
/* Retrieves the data stored in a cookie specified by name */
/*---------------------------------------------------------*/
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } 
  else {
    begin += 2;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) {
    end = dc.length;
  }
  return unescape(dc.substring(begin + prefix.length, end));
}
