//maked all added functions work onload. at the end of each function you want to work onload add addLoadEvent(function); to the end of the function.
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//maked all added functions work onresize. at the end of each function you want to work onresize add addResizeEvent(function); to the end of the function.
function addResizeEvent(func) {
	var oldonresize = window.onresize;
	if (typeof window.onresize != 'function') {
		window.onresize = func;
	} else {
		window.onresize = function() {
			oldonresize();
			func();
		}
	}
}

//**************************************************Replace align attribute with class**************************************************//
function replaceAlign() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementsByTagName('img')) return false;
  var imgList = document.getElementsByTagName('img');
  for (var i=0; i<imgList.length; i++) {
    var align = imgList[i].getAttribute('align');
		if (align) {
			if (align == 'left') {
				imgList[i].className += ' left';
			}
			if (align == 'right') {
				imgList[i].className += ' right';
			}
			if (align == 'middle') {
				imgList[i].className += ' middle';
			}
    	imgList[i].removeAttribute('align')
    }
  }
}


//**************************************************Replace target attribute with class**************************************************//
function replaceTarget() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementsByTagName('a')) return false;
  var aList = document.getElementsByTagName('a');
  for (var i=0; i<aList.length; i++) {
    var target = aList[i].getAttribute('target');
		if (target) {
			aList[i].className += ' newwindow';			
    	aList[i].removeAttribute('target')
    }
  }
}

//**************************************************The following two functions make it possible to have web standard popups**************************************************//
function strictNewWindow() {
  if (!document.getElementsByTagName) return false;
  var aList = document.getElementsByTagName('a');
  for (var i=0; i<aList.length; i++) {
    if (aList[i].className == 'newwindow') {
      aList[i].onclick = function() {
        window.open(this.getAttribute('href'),'newwindow');
        return false;
      }
    }
    if (aList[i].className == 'calcLink') {
      aList[i].onclick = function() {
        var h = (document.body.offsetHeight-100);
        window.open(this.getAttribute('href'),'calcLink','toolbar=yes,menubar=yes,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=815,height='+h+'');
        return false;
      }
    }
  }
}

//**************************************************Add swfobject flash call**************************************************//
function initializeFlash() {
	if (document.getElementById('flashHeader')) {
		var so1 = new SWFObject("/swf/header.swf", "flash", "990", "167", "8", "#fff");
		so1.addParam("wmode", "transparent");
		so1.write("flashHeader");
	}
	if (document.getElementById('flashcontent')) {
		var so2 = new SWFObject("/swf/welcome.swf", "flash", "275", "169", "8", "#fff");
		so2.addParam("wmode", "transparent");
		so2.write("flashcontent");
	}
}

//**************************************************Extend footer to bootom of page on short content pages**************************************************//
function extendFooter() {
	if (!document.getElementById) return false;
	if (!document.getElementById('headerWrap')) return false;
	if (!document.getElementById('wrap')) return false;
	if (!document.getElementById('footer')) return false;
	document.getElementById('footer').style.height = 100 + 'px';
	if (self.innerWidth) {
		 windowHeight = this.innerHeight;
	}
	else {
		windowHeight = document.documentElement.clientHeight;
	}
	headerWrapHeight = document.getElementById('headerWrap').offsetHeight;
	wrapHeight = document.getElementById('wrap').offsetHeight;
	footerHeight = document.getElementById('footer').offsetHeight;
	pageHeight = headerWrapHeight + wrapHeight + footerHeight;
	difference = windowHeight - pageHeight;
	if (pageHeight < windowHeight) {
		document.getElementById('footer').style.height = (difference + footerHeight) + 'px';
	} else {	
		document.getElementById('footer').style.height = 100 + 'px';
	}
}

//**************************************************Clear search text out search box on focus**************************************************//
function clearSearchBox() {
	if (!document.getElementById) return false;
	if (!document.getElementById('txtSearch')) return false;
	document.getElementById('txtSearch').onfocus = function() {document.getElementById('txtSearch').value = ''}
}

//**************************************************Our Firm open/close third column**************************************************//
function openClose() {
	if (!document.getElementById) return false;
	if (!document.getElementById('ourfirm')) return false;
	if (!document.getElementById('main_2Nav')) return false;
	var ulList = document.getElementById('main_2Nav').getElementsByTagName('ul');
	var aList = document.getElementById('main_2Nav').getElementsByTagName('a');
	for (var i=0; i<ulList.length; i++) {
		ulList[i].style.display = 'none';
	}
	for (var i=0; i<aList.length; i++) {
		if (aList[i].className == 'openclose') {
			aList[i].onclick = function() {
				if (this.parentNode.childNodes[1].style.display == 'none') {
					this.style.backgroundImage = 'url(/images/main_2Nav-arrow02.gif)';
					this.parentNode.childNodes[1].style.display = 'block';
					return false;
				} else {
					this.style.backgroundImage = 'url(/images/main_2Nav-arrow01.gif)';
					this.parentNode.childNodes[1].style.display = 'none';
					return false;
				}
			}
		}
	}
	extendFooter();
}

//**************************************************Load all functions**************************************************//
addLoadEvent(replaceAlign);
addLoadEvent(replaceTarget);
addLoadEvent(strictNewWindow);
addLoadEvent(initializeFlash);
addLoadEvent(extendFooter);
addResizeEvent(extendFooter);
addLoadEvent(clearSearchBox);
addLoadEvent(openClose);