﻿// JScript File

/*
 * rClearText
 * Clears an input box if text is equal to default text onfocus. Adds default back on blur if input box is empty
 */
function rClearText(el) {
	this.initialize(el);
}
rClearText.prototype = {
	initialize: function(el) {
		$(el).focus(function() {
			if (el[0].value == el[0].defaultValue) { // Text hasn't been changed yet
				el[0].value = '';
			}
		});
		$(el).blur(function() {
			if (el[0].value == '') { // Text hasn't been changed yet
				el[0].value = el[0].defaultValue;
			}
		});
	}
}
function rClearTextFocus(el) {
	if (el.value == el.defaultValue) { // Text hasn't been changed yet
		el.value = '';
	}
}

function rClearTextBlur(el) {
	if (el.value == '') { // Text hasn't been changed yet
		el.value = el.defaultValue;
	}
}
 
/*
 * ajaxForm
 * Handles AJAX for form submission. Returns a taconite XML string.
 */

function ajaxForm(frm) {
	$.ajax({
		type: "POST",
		url: frm.action,
		data: $(frm).serialize()
	});
}

/*
 * validateForm
 * Validates form for thickboxes
 */

function validateForm(frm) {
	var errors = new Array();
	
	for (var i=0; i<frm.elements.length; i++) {
		$(frm.elements[i].parentNode.parentNode).removeClass('error');
		if ($(frm.elements[i]).is('.req')) {
			// Not empty
			if(frm.elements[i].value.match(/^\s*$/)) {
				errors.push(frm.elements[i].name + ' cannot be empty');
				$(frm.elements[i].parentNode.parentNode).addClass('error');
			}
			// publication not chosen on printed copy request form	
			if(frm.elements[i].value.match("Select From List")) {
				errors.push(frm.elements[i].name + " cannot be 'Select From List'");
				$(frm.elements[i].parentNode.parentNode).addClass('error');
			}
		}
		else if ($(frm.elements[i]).is('.email')) {
			// Valid email
			if(frm.elements[i].value.match(/^\s*$/)) {
				// Not empty
				errors.push(frm.elements[i].name + ' cannot be empty');
				$(frm.elements[i].parentNode.parentNode).addClass('error');
			}
			else if(!frm.elements[i].value.match(/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/)) {
				// Valid email
				errors.push(frm.elements[i].name + ' is not a valid email');
				$(frm.elements[i].parentNode.parentNode).addClass('error');
			}
		}
		else if ($(frm.elements[i]).is('.email2')) {
			// email2 matches email
			if(frm.elements[i].value != $('.email', frm)[0].value) {
				errors.push('Email addresses do not match');
				$(frm.elements[i].parentNode.parentNode).addClass('error');
			}
		}
	}
	if (errors.length > 0) {
		var errorMsg = '<h3>Important Message: Before you can continue there are a few errors to correct ...</h3>';
		errorMsg += '<ul>';
		for (var i=0; i<errors.length; i++) {
			errorMsg += '<li>'+ errors[i] + '</li>';
		}
		errorMsg += '</ul>';
		$('.errorBox', frm)[0].innerHTML = errorMsg;
		$('.errorBox', frm).show();
		
	}
	else {
		ajaxForm(frm);
	}
}

/*
 * onLoad functions
 * Initializes our functions on page load
 */
$(document).ready(function(){
	 new rClearText($("#Search input"));
	 new rDropDown();
	 // add .last class to last LI item within Breadcrumb
    $("div#Breadcrumb ul li:last-child").addClass("last");
    });

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
/*
 * rDropDown
 * Adds class of 'hover' to LI onmouseover. Removes class onmouseout. Adds iFrame fix for IE
 */
function rDropDown() {
	this.initialize();
}
rDropDown.prototype = {
	open: false,
	timeout: false,
	openLi: null,
	initialize: function() {
	
		var lis = $("#TopNav > ul > li");
		for (var i=0; i<lis.length; i++) {
			$(lis[i]).bind('mouseover', {parentThis: this, li:lis[i]}, function(params) {
				params.data.parentThis.show(params.data.li);
			});
			$(lis[i]).bind('mouseout', {parentThis: this, li:lis[i]}, function(params) {
				params.data2 = params.data;
				params.data.parentThis.timeout = setTimeout(function() {
					params.data = params.data2;
					params.data.parentThis.hide(params.data.li);
				}, 1);
			});
		}
		
	},
	show: function(li) {
		$(li).addClass('hover');
		this.iframeFix(li);
	},
	hide: function(li) {
		$(li).removeClass('hover');
		if (this.iframe) {
			this.iframe.style.display = "none";
		}
	},
	iframeFix: function(li) {
		if(!document.all) {
			//return;
		}
		var subnav = $('div.subnav', li)[0];
		if (subnav) {
			if (!this.iframe) {
				this.iframe = document.createElement('iframe');
				this.iframe.style.position = 'absolute';
				this.iframe.frameBorder = 0;
				this.iframe.style.filter = 'alpha(opacity=0)';
				this.iframe.style.zIndex = -1;
				document.body.appendChild(this.iframe);
			}
			this.iframe.style.display = "block";
			this.iframe.style.top = $(subnav).offset().top + 'px';
			this.iframe.style.left = $(subnav).offset().left + 'px';
			this.iframe.style.width = $(subnav).width() + 'px';
			this.iframe.style.height = $(subnav).height() + 'px';
		}
	}
}


//Extension detection, icon placement, and icon alternate text
function GetAnchors() {
  if (document.getElementById) {
    var elements = new Array('a', 'area');
    for (var j=0; j < elements.length; j++) {
      var x = document.getElementsByTagName(elements[j]);
      for (var i=0;i<x.length;i++) {
        if (x[i].className.indexOf('wmv') != -1) {
          x[i].setAttribute("title", "Windows Media Video");
        } else if (x[i].className.indexOf('realAudio') != -1) {
          x[i].setAttribute("title", "Real Audio");
        } else if (x[i].className.indexOf('rss') != -1) {
          x[i].setAttribute("title", "RSS Feed");
        } else if (x[i].className.indexOf('catchCode') != -1) {
          x[i].onkeypress = gaCatchCode;
          x[i].onclick = gaCatchCode;
        } else if (isAssetDoc(x[i])) { // Set file extensions in isAssetDoc() below
          var fileExt = getFileExt(x[i]);
          x[i].className=fileExt
         switch(fileExt) {
          case "doc":
            x[i].setAttribute("title", "Word document");
            break 
          case "ppt":
            x[i].setAttribute("title", "PowerPoint File");
            break            
           case "xls":
            x[i].setAttribute("title", "Excel File");
            break 
           default:
            x[i].setAttribute("title", fileExt.toUpperCase()+" file");
          }
        }
      }
    }
  }
}
function isAssetDoc(obj) {
  var types = new Array("pdf", "doc", "xls", "ppt");
  var fileExt = getFileExt(obj);
  
  for(i=0; i<types.length;i++) {
    if (types[i] == fileExt) {
      return true;
    }
  }
  return false;
}
function getFileExt(obj) {
  var url = obj.href;
  return url.substr(url.lastIndexOf(".")+1);
}
function getValueFromClass(obj, attrib) {
  if (obj.className.indexOf(" "+attrib) != -1) {
    start = obj.className.indexOf(" "+attrib) + 1
    end = obj.className.indexOf(" ", start);
    end = (end == -1) ? obj.className.length - start : end - start;
    var aLength = attrib.length;
    return obj.className.substr(start+aLength, end-aLength);
  } else {
    return "";
  }
}

function clearSearchText(obj) {
  if(obj.value == "Search Annual Report")
  {
    obj.value = "";
  }
}

function restoreSearchText(obj) {
  if(obj.value == "") {
    obj.value = "Search Annual Report";
  }
}

// Flash Deep Linking
function track( param ) {
	// code here for call to tracking analytics
}
function track( param )	{
	// code here for call to tracking analytics
}
	// grab root url for use in email page form
var url = location.href;
var urlArr = url.split('#',1);
var rooturl = urlArr[0];
	//alert(rooturl);

//hack thickbox to have a callback
tb_show2 = tb_show;
window.tb_show = function() {
	var a = arguments;
	tb_show2.apply(this, a);
	//setTimeout('tb_callback()', 100);
	tb_callback();
}
//The thickbox callback function
function tb_callback() {
	// Add wrappers
	var TB_window = $('#TB_window')[0];
	var container = document.createElement('div');
	container.id = "TB_wrapper";
	var imgContainer = document.createElement('div');
	imgContainer.id = "TB_popupImg";
	container.appendChild(imgContainer);
	var contentPad = document.createElement('div');
	contentPad.id = "TB_contentPad";
	container.appendChild(contentPad);
	var content = document.createElement('div');
	content.id = "TB_content";
	contentPad.appendChild(content);
	while(TB_window.childNodes.length > 0) {
		content.appendChild(TB_window.childNodes[0]);
	}
	TB_window.appendChild(container);
	
	// Remove text nodes from TB_closeAjaxWindow
	var TB_closeAjaxWindow = $('#TB_closeAjaxWindow')[0];
	for (var i=0; i<TB_closeAjaxWindow.childNodes.length; i++) {
		if (TB_closeAjaxWindow.childNodes[i].nodeType == '3') {
			TB_closeAjaxWindow.removeChild(TB_closeAjaxWindow.childNodes[i]);
		}
	}
}

addLoadEvent(GetAnchors);