// retrieve cookie.
function getCookie(name){
  var cname = name + "=";
  var dc = document.cookie;
  if (dc.length > 0) {
    begin = dc.indexOf(cname);
    if (begin != -1) {
      begin += cname.length;
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    }
  }
  return null;
}

// set cookie.
function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) +
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}

// delete cookie.
function delCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

cookieName = "textSize";
defaultFontSize = 12;
maxFontSize = 14;
minFontSize = 10;
firstTimeThrough = true;
styleRoot = '/CSS/';
styleEnd = '.css';


function changeTextSize (increment) {
	increment = parseInt (increment); // make sure it is an int and not a string
	
	// check if the cookie is set and parse it as an integer
	txtSize = parseInt(getCookie(cookieName));
	if (!txtSize) {
		if ((increment == 0) || (increment == '')) {
			txtSize = defaultFontSize;							// no increment and no cookie set, so make it default
		} else txtSize = defaultFontSize + increment;	// add on the increment
	} else {
		if ((increment != 0) & (increment != '')) {
			txtSize = txtSize + increment;				// add on the increment
		}
		// otherwise we already have the text size set as the cookie value
		firstTimeThrough = 'false';
	}

	// make sure the text size is valid
	if ((txtSize <= minFontSize)) {
		txtSize = minFontSize;
	}
	if ((txtSize >= maxFontSize)) {
		txtSize = maxFontSize;
	}

	setCookie(cookieName, txtSize); // set the cookie
	
	if (!firstTimeThrough) { 
		// not the first time through so just change the stylesheet
		document.styleSheets[cookieName].href = styleRoot + cookieName + '_' + txtSize + styleEnd;
	} else {
		// first time through so need to add the stylesheet
		var headID = document.getElementsByTagName("head")[0];         
		var cssNode = document.createElement('link');
		cssNode.href = styleRoot + cookieName + '_' + txtSize + styleEnd;
		cssNode.name = cookieName;
		cssNode.type = 'text/css';
		cssNode.rel = 'stylesheet';
		cssNode.media = 'screen';
		headID.appendChild(cssNode);
	}
}



