function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}


/*
popup() is used in conjuntion with prepareLinks().
Assign an <a> tag the class of "popup", and the href will automatically load in a 600x500 popup window with no toolbars.
Example:
<a href="http://www.ftb.com" class="popup">Click here to see ftb.com in a new window!</a>
*/
function popUp(winURL) {
	window.open(winURL,"popup","resizable=1,location=0,toolbar=0,scrollbars=1,width=600,height=500");
}

function popUpEmail(winURL) {
	window.open(winURL,"popup","resizable=1,location=0,toolbar=0,scrollbars=1,width=600,height=650");
}

function popUpCalc(winURL) {
	window.open(winURL,"popupcalc","resizable=1,location=0,toolbar=0,scrollbars=1,width=470,height=400");
}

function popUpReverseMortgageCalc(winURL) {
	window.open(winURL,"popupreversemortgagecalc","resizable=1,location=0,toolbar=0,scrollbars=1,width=675,height=475");
}

function popUpGlossary(winURL) {
	window.open(winURL,"popupglossary","resizable=1,location=0,toolbar=0,scrollbars=1,width=400,height=375");
}

function prepareLinks() {
	if (!document.getElementsByTagName) return false;
	var lnks = document.getElementsByTagName("a");
	for (var i=0; i<lnks.length; i++) {
		if (lnks[i].className == "popup") {
			lnks[i].onclick = function() {
				popUp(this.getAttribute("href"));
				return false;
			}
		}
		if (lnks[i].className == "popupEmail") {
			lnks[i].onclick = function() {
				popUpEmail(this.getAttribute("href"));
				return false;
			}
		}
		if (lnks[i].className == "popupCalc") {
			lnks[i].onclick = function() {
				popUpCalc(this.getAttribute("href"));
				return false;
			}
		}
		if (lnks[i].className == "popupReverseMortgageCalc") {
			lnks[i].onclick = function() {
				popUpReverseMortgageCalc(this.getAttribute("href"));
				return false;
			}
		}
		if (lnks[i].className == "popupGlossary") {
			lnks[i].onclick = function() {
				popUpGlossary(this.getAttribute("href"));
				return false;
			}
		}
	}
}
addLoadEvent(prepareLinks);



/* -----------------------------------------
	automatic table striping with class=stripe
------------------------------------------- */

// this function is needed to work around 
  // a bug in IE related to element attributes
function hasClass(obj) {
    var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  }   

  
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
function stripe() {
    // obtain a reference to the desired table
    // if no such table exists, abort
    var tables = getElementsByClass("tablestripe",document,"TABLE");
    if (! tables) { return; }
 for (var hh = 0; hh < tables.length; hh++) {
 
    // the flag we'll use to keep track of 
    // whether the current row is odd or even
    var even = false;
  
    // if arguments are provided to specify the colours
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var evenColor = arguments[1] ? arguments[1] : "#D9E5ED";
    var oddColor = arguments[2] ? arguments[2] : "#fff";
 
 
 
    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // &lt;tbody&gt;s 
    var tbodies = tables[hh].getElementsByTagName("tbody");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
    
     // find all the &lt;tr&gt; elements... 
      var trs = tbodies[h].getElementsByTagName("tr");
      
      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {

        // avoid rows that have a class attribute
        // or backgroundColor style
        if (! hasClass(trs[i]) &&
            ! trs[i].style.backgroundColor) {
 		  
          // get all the cells in this row...
          var tds = trs[i].getElementsByTagName("td");
        
          // and iterate through them...
          for (var j = 0; j < tds.length; j++) {
        
            var mytd = tds[j];

            // avoid cells that have a class attribute
            // or backgroundColor style
            if (! hasClass(mytd) &&
                ! mytd.style.backgroundColor) {
        
              mytd.style.backgroundColor =
                even ? evenColor : oddColor;
            
            }
          }		  
		   // flip from odd to even, or vice-versa
        even =  ! even;
        }
       
      }
    }
}
  }
  
addLoadEvent(stripe);







/* ----------------------------------- 
   old scripts - might still be needed 
-------------------------------------- */
/*
	Popup window
*/
function PopIt(filename, h, w) {
     popupname = "popDialog"
     popup = window.open(filename,popupname,"height="+h+",width="+w+",scrollbars=yes")
     popup.focus()
}


/*
	Popup window (resizeable)
*/
function PopItResize(filename, h, w) {
     popupname = "popDialogRes"
     popup = window.open(filename,popupname,"height="+h+",width="+w+",scrollbars=yes,resizable=yes")
     popup.focus()
}

/*
	Popup window
*/
function PopItNoScroll(filename, h, w) {
     popupname = "popDialogNS"
     popup = window.open(filename,popupname,"height="+h+",width="+w+",scrollbars=no")
     popup.focus()
}

/* 
	On the PlanningServices.Home page, there are two popups.  each popup contains a link to find a financial planner - to load the dorado site, back in the main window.  This popup script gives a name to the parent window, so that when the link in the popup is clicked, the desired page loads in the parent window.
*/
function popUpParent(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=500,left = 100,top = 100');");
}


