// Pass in a query string variable and it returns the value. 
// Legacy function. The query string function below is better because it doesn't rely on
// searches. The searches will get tripped up if you have mulitple names in the query string (via checkboxes).
function queryString(keyName) { 
    var keyValue = "";
    keyName = keyName.toUpperCase() + "="; 

    var queryString = location.search;
    if (location.search.indexOf("?") != -1) { 
        queryString = location.search.substring(1, queryString.length); 
        var searchString = queryString.toUpperCase(); 
        if (searchString.indexOf(keyName) != -1) { 
            var keyValueStart = parseInt(searchString.indexOf(keyName)) + keyName.length; 
            var keyValueEnd = searchString.indexOf("&", keyValueStart); 
            var keyValueEnd = (keyValueEnd != -1) ? keyValueEnd : searchString.length; 
            keyValue = queryString.substring(keyValueStart,keyValueEnd); 
        }

        var plusPos = keyValue.indexOf('+');
        while (plusPos != -1) {
            keyValue = keyValue.substring(0, plusPos) + " " + keyValue.substring(plusPos + 1, keyValue.length);
            plusPos = keyValue.indexOf('+');
        }
    } 

    return unescape(keyValue); 
} 

/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg
    Enhanced by Dennis Pierce to allow multiple names in the querystring. 
*/
function QueryString(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=QueryString_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
        // If the name already exists, then we need to make the values into
        // an array. 
        if (this.params[name] == undefined) {
          this.params[name] = new Array(value);
        } else {
          this.params[name].push(value);
        }
        
	}
}

function QueryString_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}

// Retrieves an emailed url and redirects the browser to that page.
function gotoEmailedUrl() {
  var cook = new Cookie("sessionDataHolder");
  var destUrl = cook.emailedUrl;
  if (cook.emailOverrideUrl != undefined && cook.emailOverrideUrl != null) {
    destUrl = cook.emailOverrideUrl;
  }
  cook.remove();
  if (destUrl) {
    location.href = destUrl;
  }
  
}
// Sets opacity of an object. 
function setOpacity(obj, opacity) {
  if (obj == null || obj == undefined) { return false; }
  opacity = (opacity == 100)?99.999:opacity;

  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";

  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

// Helper function for Safari. Safari can't detect when a mouseoff occurs in an 
// iframe. So we need to attach an onmouseout event to the iframe to dim out the image.
function setOpacityIframe(iframe, val) {
  var x = iframe;
  x = (x.contentDocument || x.contentWindow.document);
  setOpacity(x.getElementById('titleImg'), val)
}


// Trims space from the left side of a string. 
function LTrim(txt) {
    return txt.replace(/^[\s]+/g, "");
}


// Returns true if letter is alphanumeric; otherwise false. 
function isAlphaNumeric(letter) {
  var re = new RegExp();
  re = /[a-zA-Z0-9]/;
  if (letter.match(re) != null) {
    return true;
  } else {
    return false;
  }
}
// Returns true if letter is alpha; otherwise false. 
function isAlpha(letter) {
  var re = new RegExp();
  re = /[a-zA-Z]/;
  if (letter.match(re) != null) {
    return true;
  } else {
    return false;
  }
}


// Helper function used for dropcaps. Creates an empty span tag and 
// fills it with HTML. Used for text nodes since those nodes don't have
// access to the innerHTML attribute. 
function buildSpan(txt) {
  var newSpan = document.createElement("span");
  newSpan.innerHTML = processTxt(txt);
  return newSpan;
}

// Helper function for dropcaps. Applies a span to the first letter in a string.
function processTxt(txt) {
  var letter = txt.charAt(0);
  var strIndex = 1;
  // There is a special exception for double quotes. Ditch them and use the next
  // letter. 
  if (letter == "\"") { 
    letter = txt.charAt(1);
    strIndex++;
  }

  
  if (isAlpha(letter)) {
    txt = "<span class=\"firstletter\" id=\"dropcap_" + letter.toLowerCase() + "\">" + 

letter + "</span>" + txt.substr(strIndex);
  }
  return txt;

}

// Searches an array for a value. Returns index of array if found. -1 if not found.  
function searchArray(arr, val) {
  for (var i=0; i<arr.length; i++) {
    if (arr[i] == val) { return i }
  }
  return -1;
}

function gotoPage(arg) { 
  document.location.href = document.location.pathname + "?currentPage=" + arg;
 
}

// Draws drop caps. Pass in the id of the DIV you want to modify. Also can pass in
// an array of div ids to exclude. 
function drawDropCap(targetDiv, excludeList) {
return false; // disabled until css is built for dropcaps otherwise will crash browsers. 
// Check query string values. Don't drop cap on pages after page 1.
  var qs = queryString("currentPage");
  if (qs != "all" && qs != "1" && qs != "") { return true; }

  // Suppress drop caps if there is a style named "noDropCaps" in the body.
  var bodyClass = document.getElementsByTagName('body')[0].className;
  if (bodyClass.indexOf("noDropCaps") != -1) {return true; }

  var startingNode = document.getElementById(targetDiv);
  var children =  startingNode.childNodes;
  
  // Loop through the nodes. 
  // Look for nodes of type 1 (html) or 3 (text).
  for (var i = 0; i<children.length; i++) {
    if (children[i].nodeType==1) {
      // Don't process ids in the exclude list. 
	  // Don't process HTML tags with a firstChild of null (ex. br, hr). 
      if ((excludeList != null && searchArray(excludeList, children[i].getAttribute("id")) != -1) || (children[i].firstChild == null)) { continue; }

      
       // We're in the node we want to modify.
       // Loop down the children till we find a text node. 
        var nodeFlag = true;
        var tmpNode = children[i].firstChild;

		while (nodeFlag) {
          if (tmpNode.nodeType == 3) {
            // We've found the text node. Process it.
            var txt = LTrim(tmpNode.parentNode.innerHTML);
            tmpNode.parentNode.innerHTML = processTxt(txt);
	    return true;
          }
          tmpNode = tmpNode.firstChild;
          if (tmpNode == null) { nodeFlag = false; }
        }
    
    } else if (children[i].nodeType == 3) {
      // Make sure this isn't whitespace.
      var nVal = LTrim(children[i].nodeValue); 
      if (nVal == "" || nVal == "null") { continue; }
      var txt = LTrim(children[i].nodeValue);
      var newSpan = buildSpan(txt);
      children[i].parentNode.replaceChild(newSpan, children[i]);
      return true;
 
    }
  }
}




