/*
 * Event handler to execute after page has loaded
 *
 * @param func - Function to execute once the page has loaded
 */
function addLoadEvent(func)
{
    // Store existing onload event
    var oldonload = window.onload;
    // Assign function parameter to onload event if none exists
    if (typeof window.onload != 'function') {
        window.onload = func;
    // Create new function to call existing and new function
    } else {
        window.onload = function()
        {
            if (oldonload) {
                oldonload();
            }
            func();
        } // function()
    } // if/else
} // addLoadEvent

/*
 * Disable submit button upon click
 */
function disableSubmitAfterClick()
{
	// Exit if getElementsByTagName function not supported
	if (!document.getElementsByTagName)
		return;

	var forms = document.getElementsByTagName('form');
	var fields;

	for (var i=0; i<forms.length; i++) {
		forms[i].onsubmit =
			function(){
				fields = this.getElementsByTagName('input');
				for (var i=0; i<fields.length; i++) {
					if (fields[i].type == 'submit') {
						fields[i].disabled = true;						
					}
				} // for
			}; // function
	} // for
} // disableSubmitAfterClick()

/*
 * Change the 'rel="external"' attribute on all anchor tags to the _blank target
 */
function externalLinks()
{

    if (!document.getElementsByTagName)
        return;
    var anchors = document.getElementsByTagName('a');
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == 'external')
            anchor.target = "_blank";
    } // for...
} // externalLinks()

/*
 * Get cookie value
 */
function getCookie(c_name)
{
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
} // getCookie

function serializeForm(theform)
{
  var els = theform.elements;
  var len = els.length;
  var queryString = "";
  this.addField = 
    function(name,value) { 
      if (queryString.length>0) { 
        queryString += "&";
      }
      queryString += encodeURIComponent(name) + "=" + encodeURIComponent(value);
    };
  for (var i=0; i<len; i++) {
    var el = els[i];
    if (!el.disabled) {
      switch(el.type) {
        case 'text': case 'password': case 'hidden': case 'textarea': 
          this.addField(el.name,el.value);
          break;
        case 'select-one':
          if (el.selectedIndex>=0) {
            this.addField(el.name,el.options[el.selectedIndex].value);
          }
          break;
        case 'select-multiple':
          for (var j=0; j<el.options.length; j++) {
            if (el.options[j].selected) {
              this.addField(el.name,el.options[j].value);
            }
          }
          break;
        case 'checkbox': case 'radio':
          if (el.checked) {
            this.addField(el.name,el.value);
          }
          break;
      }
    }
  }
  return queryString;
} // serializeForm

/*
 * Set cookie value
 */
function setCookie(name, value, expires, path, domain, secure)
{
    var today = new Date();
    today.setTime(today.getTime());
    
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));
    
    document.cookie = name + "=" + escape(value) +
    ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
    ((path) ? ";path=" + path : "") +
    ((domain) ? ";domain=" + domain : "") +
    ((secure) ? ";secure" : "");
} // setCookie

function StaticText_getText(element, location)
{
	var http = getHTTPObject();

	http.open("GET", location, true);
	http.onreadystatechange = function()
	{
		if (http.readyState == 4) {
			// Ensure file is found by checking HTTP status code
			if (http.status == 200) {
				document.getElementById(element).innerHTML = http.responseText;
			}
		}
	} // function

	http.send(null);
} // StaticText_getText

/*
 * Alternate element colors on elements within parent element
 *
 * @param parentId - Id of parent element
 * @param childTag - Element within parent to change
 * @param class1 - CSS class for odd-numbered elements
 * @param class2 - CSS class for even-numbered elements
 * @param row - Starting row to apply styles
 */
function stripe(parentId, childTag, class1, class2, row)
{
	var start = 0;

	// Get all child elements of parent Id
	el=document.getElementById(parentId).getElementsByTagName(childTag);
	numChildren=el.length;
	// Set start row for shading
	if (row) {
		start = row;
	}

	for(i=start; i<numChildren; i++){
		if((i+1)%2 == 1) {
			// Change class for odd-numbered element
			el[i].className=class1;
		} else {
			// Change class for even-numbered element
			el[i].className=class2;
		}
	} // for
} // stripe
