//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();
		}
	}
}

//**************************************************The following two functions make it possible to have web standard popups**************************************************//
window.onload = function() {
  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;
      }
    }
  }
}

function popUp(winURL) {
  window.open(winURL,"popup");
}

//**************************************************Create a :Hover psuedo attribute for IE for header**************************************************//
function hdrHover() {
  if (ie == 0) return false;
	if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById('headerNav')) return false;
  var headerNav = document.getElementById('headerNav');
	var liList = document.getElementById("headerNav").getElementsByTagName("li");
	for (var i=0; i<liList.length; i++) {
		liList[i].onmouseover=function() {
			this.className+=" hdrhover";
			headerNav.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src="../images/headerNav-ie/bg-headerNav-ie-' + this.id + '.png")';
		}
		liList[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" hdrhover\\b"), "");
			headerNav.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src="../images/headerNav-ie/bg-headerNav-ie.png")';
		}
	}
}

//**************************************************Change headerNav background so divider lines are removed on seleted item**************************************************//
function replaceBackground() {
  if (ie == 1) return false;
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById('headerNav')) return false;
  var headerNav = document.getElementById('headerNav');
  var liList = document.getElementById('headerNav').getElementsByTagName("li");
	for (var i=0; i<liList.length; i++) {
		liList[i].onmouseover=function() {
			headerNav.style.backgroundPosition = ('0 '+ (-40*this.className.replace('lnkWrap','')) + 'px');
		}
		liList[i].onmouseout=function() {
			headerNav.style.backgroundPosition = '0 0';
		}
	}
}

//**************************************************Attach row class**************************************************//
function tableRows() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById('comparisonchart')) return false;
  var comparisonChart = document.getElementById('comparisonchart');
  var trList = document.getElementById('comparisonchart').getElementsByTagName("tr");
	for (var i=0; i<trList.length; i++) {
		if (i%2) {
			trList[i].className = 'row1';
		}
		else {
			trList[i].className = 'row2';
		}
	}
}

//**************************************************Add swfobject flash call**************************************************//
function initializeHomeFlash() {
	if (!document.getElementById) return false;
	if (!document.getElementById('flashcontent')) return false;
	if (!document.getElementById('flashcasestudy1')) return false;
	if (!document.getElementById('flashcasestudy2')) return false;
	if (!document.getElementById('flashcasestudy3')) return false;
	var so1 = new SWFObject("swf/header-homebanner.swf", "flash", "956", "246", "8", "#fff");
	so1.addParam("wmode", "transparent");
	so1.write("flashcontent");
	var so2 = new SWFObject("/swf/DynamicsCRM-HRBlock.swf", "flashcase1", "320", "240", "8", "#fff");
	so2.addParam("wmode", "transparent");
	so2.write("flashcasestudy1");
	var so3 = new SWFObject("/swf/DynamicsGP-Roland.swf", "flashcase2", "320", "240", "8", "#fff");
	so3.addParam("wmode", "transparent");
	so3.write("flashcasestudy2");
	var so4 = new SWFObject("/swf/OCC-GreatPlains.swf", "flashcase3", "320", "240", "8", "#fff");
	so4.addParam("wmode", "transparent");
	so4.write("flashcasestudy3");
}

//**************************************************Clear search text out search box on focus**************************************************//
function bBBContextMenu() {
	if (!document.getElementById) return false;
	if (!document.getElementById('bbb')) return false;
	document.getElementById('bbb').oncontextmenu = function() {alert('Use without permission is prohibited. The BBB Accreditation seal is a trademark of the Council of Better Business Bureaus, Inc.'); return false;}
}

	//**************************************************eNewsletter form validation**************************************************//
	function formValidation() {
		if (!document.getElementById) return false;
		if (!document.getElementById('mb-name')) return false;
		if (!document.getElementById('mb-dlird-dlird')) return false;
		if (!document.getElementById('Company')) return false;
		if (!document.getElementById('Zip')) return false;
		var f1 = new LiveValidation('mb-name');
		var f2 = new LiveValidation('mb-dlird-dlird');
		var f3 = new LiveValidation('Company');
		var f4 = new LiveValidation('Zip');
		f1.add(Validate.Presence);
	 	f2.add(Validate.Presence);
	 	f2.add(Validate.Email);
	 	f3.add(Validate.Presence);
	 	f4.add(Validate.Presence);
		f4.add(Validate.Numericality,{onlyInteger:true});
		f4.add(Validate.Length,{is: 5});
	}

//**************************************************Industry focus show/hide**************************************************//
function showIndustryFocus() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById('showHide')) return false;
	var industry = document.getElementById('showHide').getElementsByTagName('h4');
	var description = document.getElementById('showHide').getElementsByTagName('div');
	for (var i=0; i<description.length; i++) {
		description[i].style.display = 'none';
	}
	for (var i=0; i<industry.length; i++) {
		industry[i].style.cursor = 'pointer';
		industry[i].onclick=function() {
			desc = getNextElement(this.nextSibling);
			if (desc.style.display != 'block') {
				desc.style.display = 'block';
			} else {
				desc.style.display = "none";
			}
		}
	}
	toggleAll();
}

function getNextElement(node) {
	if(node.nodeType == 1) {
		return node;
	}
	if (node.nextSibling) {
		return getNextElement(node.nextSibling);
	}
	return null;
}

function toggleAll() {
	if (!document.getElementById('showalldesc')) return false;
	showAll = document.getElementById('showalldesc');
	description = document.getElementById('showHide').getElementsByTagName('div');
	showAll.onclick=function() {
		if (showAll.innerHTML != 'Hide All Descriptions') {
			for (var i=0; i<description.length; i++) {
				description[i].style.display = 'block';
			}
			showAll.innerHTML = 'Hide All Descriptions';
		} else {
			for (var i=0; i<description.length; i++) {
				description[i].style.display = 'none';
			}
			showAll.innerHTML = 'Show All Descriptions';
		}
	}
}

//**************************************************Load all functions**************************************************//
addLoadEvent(hdrHover);//IE 6 and Below
addLoadEvent(replaceBackground);//All other browsers
addLoadEvent(tableRows);
addLoadEvent(initializeHomeFlash);
addLoadEvent(bBBContextMenu);
addLoadEvent(formValidation);
addLoadEvent(showIndustryFocus);