// Pass in a query string variable and it returns the value. 
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;
  }
  
}

// Log in functions
function isLoggedIn() {
        var cookie_name="amg_user";
        var nameEQ = cookie_name + "=";
        var ca = document.cookie.split(';');
        for (var i=0; i<ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return true;
        }
        return false;
    }

    function username(){
        var name="amg_user_info";
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0; i<ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            // max of 25 chars in username. 
            if (c.indexOf(nameEQ) == 0) {
              // check length. 
              var nameLen = c.substring(nameEQ.length,c.length).length;
              if (nameLen>25) {
                return c.substring(nameEQ.length,nameEQ.length+22)+ "...";
              } else {
                return c.substring(nameEQ.length,nameEQ.length+25);
              }
            }
        }
        return null;
    }
	

// 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 (isAlpha(letter)) {
    txt = "<span class=\"firstletter\" id=\"dropcap_" + letter.toLowerCase() + "\">" + letter + "</span>" + txt.substr(strIndex);
  } else {	
	if (escape(letter).indexOf("201C") > -1 || letter == "\"") {
		txt = "<span class=\"firstletter\" id=\"dropcap_doublequote\">quote</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;
}


// 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) {
// 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; }

  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;
 
    }
  }
}





	