var mailForm="email_form.html";

var mailCountry="email";

if(!confForm){

	var confForm="email_conf.html";	

}

function startUp(){

	setNavigation();

	setLowerNavigation();	

	formFunctions();

	goToPrint();

	if($("#countries").length>0){

		setupCountries();

	}

	if($('.bottom_bucket.expand').length>0){

		BUHideLinks();	

	}

	if($('.bottom_bucket .heading h2').length>0){

		setHeight();

	}

	if($("#contact").length > 0) {

		showContact();

	}

	if($('.subnavmenu').length>0){

		setBottomNav();

	}

	if($('#findform').length>0){

		//document.forms.findform.reset();

	}

}

//set up header mouseovers on every page

function setNavigation(){	

	//world mpa hover state on/off

	$('.nav_worldwide>a').mouseover(function(){

		$('#gmap').attr({

			src: "/siteImages/nav_map_on.gif"

		});

	$('.nav_worldwide>a').mouseout(function(){

		if($('.nav_worldwide.on>a').length==0){		

			$('#gmap').attr({

				src: "/siteImages/nav_map_off.gif"

			});

		}

		});

	});	

	if($('.nav_worldwide.on>a').length>0){

		$('#gmap').attr({

			src: "/siteImages/nav_map_on.gif"

		});

	}

	//set up global navigation rollovers

    var navRoot;

	var node='';

	var tempClassName='';

	var leftPosition=0;

	var topPosition=0;

    navRoot=$('#global_menu');

	$(navRoot).children('li').mouseover(function(){

		tempClassName=this.className;

		if(tempClassName.indexOf('over')==-1){

			$(this).addClass('over');	

			if($(this).children('ul:first').length>0){

				node=$(this).children('ul:first')[0];

				//if its IE > 7 hide select boxes on mouseovers

				 if (navigator.appName == "Microsoft Internet Explorer" && (!window.XMLHttpRequest)){

					leftPosition = 0;

					topPosition = 0;

					hideShowSelectBox(findGlobalPosX(node),findGlobalPosY(node),node.offsetWidth,node.offsetHeight); 

				 }

			}

		}

	}).mouseout(function(){

		$(this).removeClass('over');	

		showAllSelectBoxes();

	});;

	//call function to fire Choose Language mouseover

	showLanguage();

	//set up all button elements to do form submits

	$("button").click(function(){$(this).parent().submit()});

}

//called in setNavigation() to find position of menues and select boxes for IE < 7 show/hide

function findGlobalPosX(obj) {

    var curleft = 0;

    if (obj.offsetParent) {

        while (obj.offsetParent) {

            curleft += obj.offsetLeft;

            obj = obj.offsetParent;

        }

    } else if (obj.x) {

        curleft += obj.x;

    }

    return curleft;

}

//called in setNavigation() to find position of menues and select boxes for IE < 7 show/hide

function findGlobalPosY(obj) {

    var curtop = 0;

    if (obj.offsetParent) {

        while (obj.offsetParent) {

            curtop += obj.offsetTop;

            obj = obj.offsetParent;

        }

        curtop += 1;

    } else if (obj.y) {

        curtop += obj.y;

    }

    return curtop;

}

//function called in setNavigation() to hide/show select boxes

function hideShowSelectBox(menuLeftPos,menuTopPos,menuWidth,menuHeight){	

    for(slectLoop2=0;slectLoop2<document.getElementsByTagName("select").length;slectLoop2++){		

        var tempSelectY = findGlobalPosY(document.getElementsByTagName("select")[slectLoop2]);

        var tempSelectX = findGlobalPosX(document.getElementsByTagName("select")[slectLoop2]);

        var tempSelectWidth = document.getElementsByTagName("select")[slectLoop2].offsetWidth;

        var tempSelectHeight = document.getElementsByTagName("select")[slectLoop2].offsetHeight;

        if((tempSelectY <= menuTopPos + menuHeight && tempSelectY + tempSelectHeight >= menuTopPos ) && ((tempSelectX <= menuLeftPos && menuLeftPos <= (tempSelectWidth + tempSelectX)) || ((menuLeftPos <= tempSelectX) && (menuLeftPos + menuWidth > tempSelectX)))){

            document.getElementsByTagName("select")[slectLoop2].style.visibility = 'hidden';

        }else{

            document.getElementsByTagName("select")[slectLoop2].style.visibility = 'visible';

        }

    }

}

//function called in setNavigation() to show all select boxes when global nav is moused off of

function showAllSelectBoxes(){

	if (navigator.appName == "Microsoft Internet Explorer" && (!window.XMLHttpRequest)){

		for(j=0;j<document.getElementsByTagName("select").length;j++){				

			document.getElementsByTagName("select")[j].style.visibility = 'visible';

		}

	}

}

//function to set up the three-tiered Products and Services menu

function setLowerNavigation(){

	var lowerRoot='';

	var lnode='';

	var targetUL=$('#link_series');

	var ulToLoad;

	var ulNew;

	if($('#link_category>ul>li.selected')){

		ulToLoad=$('#link_category>ul>li.selected').children('ul');

		$(targetUL).empty();

		$(ulToLoad).clone().appendTo($(targetUL));

	}

	lowerRoot = $('#link_category>ul:first');

	for(j=0;j<lowerRoot.children('li').length;j++){

		lnode=lowerRoot.children('li')[j];

		lnode.onmouseover=function(){

			removeStyle();

			$(this).addClass('selected');

			ulNew=$(this).children('ul');

			$(targetUL).empty();

			$(ulNew).clone().appendTo($(targetUL));

		}

	}

}

//this function shows the Languages div in the footer on rollover

function showLanguage(){

	if($("#lang_main").length>0){

		$("#lang_main").mouseover(function(){

			$("#lang_holder").css("display","inline");										   

		}).mouseout(function(){

			$("#lang_holder").css("display","none");	

		});

	}	

}

//function called in setLowerNavigation() to remove all .selected styles from links on new mouseover

function removeStyle(){

	var lowerRoot;

	var lnode;

	if($('#prod_list')){

		lowerRoot = $('#link_category>ul:first');

		for(var j=0;j<lowerRoot.children('li').length;j++){

			lnode=lowerRoot.children('li')[j];

			$(lnode).removeClass('selected');

		}

	}

}

//function used only when there is a #countries table to setup mouseover events on the table rows

function setupCountries(){

	var theRow;

	var firstCell;

	var secondCell;

	theRow=$("#countries");

	$("#countries tr").mouseover(function(){

		$(this).addClass("rowOver1");

		firstCell=$(this).children('td')[0];

		secondCell=$(this).children('td')[1];

		$(firstCell).addClass("rowOver1");

		$(secondCell).addClass("rowOver2");

	}).mouseout(function(){

		$(firstCell).removeClass("rowOver1");

		$(secondCell).removeClass("rowOver2");

	});

}

//function to show/hide links in business unit page if more/less than 3 available

function BUHideLinks(){

	var linkText;

	var thisLI;

	var appendLink;

	var numExpands=1;

	var theExpands;

	var theNum=0;

	if($('.bottom_bucket.expand').length>0){

		numExpands = $('.bottom_bucket.expand').length;

		theExpands = $('.bottom_bucket.expand');

		jQuery.each($('.bottom_bucket.expand'),function(){

			linkSpan=$(this).find('.view_link:first');

			linkText=$(this).find('.view_link:first>a');

			theNum++;

			var thisElement=($(this).children('ul:first'));

			var numLIs=$(thisElement).children('li').length;

			if(numLIs>20){

				//first, loop through all items > 3 and hide them

				for(var l=20;l<numLIs;l++){

					thisLI = $(thisElement).children('li')[l];

					$(thisLI).hide();

				}

				//add hidden links for other states

				appendLink="<span class='view_link' id='hideLink"+theNum+"'><img src='/siteImages/lg_arrow.gif' width='5' height='9' alt='' /><a href='#'>"+hideText+"</a></span><span class='view_link' id='allLink"+theNum+"'><img src='/siteImages/lg_arrow.gif' width='5' height='9' alt='' /><a href='"+allLink[theNum]+"'>"+allText+"</a></span>"

				$(appendLink).appendTo(this);

				$("#hideLink"+theNum).hide();

				$("#allLink"+theNum).hide();

				//change text to 'view more'

				linkText.html(moreText);

				linkText.attr("href","#");

				linkSpan.attr("id","moreLink"+theNum);

			} else {

				linkText.html(allText);	

			}

			$(this).children("span[id^='moreLink']").click(function(){

				for(var l=20;l<10;l++){

					thisLI=$(thisElement).children('li')[l];

					if(thisLI){

						$(thisLI).show();

					}

				}

				$(this).hide();

				$(this).parent('div').children("span[id^='hideLink']").show();

				if(numLIs>20){

					$(this).parent('div').children("span[id^='allLink']").show();

				}

			});

			$(this).children("span[id^='hideLink']").click(function(){					 

				for(var l=20;l<numLIs;l++){

				thisLI = $(thisElement).children('li')[l];

					if(thisLI){

						$(thisLI).hide();

					}

				}

				$(this).parent('div').children("span[id^='hideLink']").hide();

				$(this).parent('div').children("span[id^='allLink']").hide();

				$(this).parent('div').children("span[id^='moreLink']").show();

			});

		});

	}

}

//function is called inline when a select box is supposed to enable/disable related select boxes

function dropSense(sel) { 

	var selected = $(sel).val(); 

	if (selected != "") { 

		$(sel).next('select').removeAttr('disabled'); 

		updateCategories();

	} else { 

		$(sel).next('select').val("").attr("disabled", true);

	}

	

}

//function hides/shows the Request More Info form

function showContact(){

	//contact display none unless RMI1 (confirmation)

	if(location.href.indexOf("status=RMI1")==-1){

		$("#contact").css("display","none");

	}

	$("#closeform").show();

	$("#submit").hide();

	//set up expand click to open form, show buttons

	$("#expand").click(function(){

		$("#contact").animate({height: "show"});

		$("#expand").css("visibility","hidden");

		$("#submit").show();

		leadOpen(this,'Request More Info');

	});

	//set up close click to close form, hide buttons

	$("#closeform").click(function(){

		$("#contact").animate({height: "hide"});	

		$("#submit").hide();

		$("#expand").css("visibility","visible");

		leadClose(this,'Request More Info');

	});

	//set pageTitle and Address appropriately and attach them to for hidden fields

	var pageTitle = document.title;

	var pageAddr = location.href + "?status=RMI1";

	document.cform.rpagetitle.value = pageTitle;

	document.cform.rpageaddress.value = pageAddr;	

}

//validation called for Request More Info form

function validateForm() {

	document.getElementById("emailerr").style.display="none";		

	var willReturn = 1;

	//required fields

	var reqAry=new Array(7)

	for (i=0; i <7; i++)

	reqAry[i]=new Array(2)

	reqAry[0][0] = document.cform.C_FirstName.value;

	reqAry[0][1] = document.getElementById("fnamelabel");

	reqAry[1][0] = document.cform.C_LastName.value;

	reqAry[1][1] = document.getElementById("lnamelabel");

	reqAry[2][0] = document.cform.C_Title.value;

	reqAry[2][1] = document.getElementById("titlelabel");

	reqAry[3][0] = document.cform.C_EmailAddress.value;

	reqAry[3][1] = document.getElementById("emaillabel");

	reqAry[4][0] = document.cform.C_Company.value;

	reqAry[4][1] = document.getElementById("companylabel");

	reqAry[5][0] = document.cform.C_BusPhone.value;

	reqAry[5][1] = document.getElementById("phonelabel");

	reqAry[6][0] = document.cform.C_Country.value;

	reqAry[6][1] = document.getElementById("countrylabel");

	for(i=0; i<reqAry.length; i++){

		if(reqAry[i][0]==null||reqAry[i][0]==""){

			reqAry[i][1].className="error";

			willReturn = 0;

		} else {

			reqAry[i][1].className="";	

		}

	}

	if(willReturn==0){

		return false;

	} else {

		if(validate_email(reqAry[3][0])==true){

			completeLead();

			return true;

		} else {

			document.getElementById("emailerr").style.display="inline";	

			return false;

		}

	}

}

//function called to validate Email to a Friend form

function validateEmailForm() {

	document.getElementById("emailtoerr").style.display="none";

	document.getElementById("emailfromerr").style.display="none";

	var willReturn = 1;

	//required fields

	var reqAry=new Array(4)

	for (i=0; i <4; i++)

	reqAry[i]=new Array(2)

	reqAry[0][0] = document.eform.C_To_Email_Address1.value;

	reqAry[0][1] = document.getElementById("emailtolabel");

	reqAry[1][0] = document.eform.C_From_Email_Address1.value;

	reqAry[1][1] = document.getElementById("emailfromlabel");

	reqAry[2][0] = document.eform.C_Subject1.value;

	reqAry[2][1] = document.getElementById("emailsubjlabel");

	reqAry[3][0] = document.eform.recaptcha_response_field.value;

	reqAry[3][1] = document.getElementById("recaptcha_label");

	for(i=0; i<reqAry.length; i++){

		if(reqAry[i][0]==null||reqAry[i][0]==""){

			reqAry[i][1].className="error";

			willReturn = 0;

		} else {

			reqAry[i][1].className="";	

		}

	}

	if(willReturn==0){

		return false;

	} else {

		if(validate_email(reqAry[0][0])==false){

			document.getElementById("emailtoerr").style.display="inline";	

			return false;

		} else if(validate_email(reqAry[1][0])==false) {

			document.getElementById("emailfromerr").style.display="inline";	

			return false;

		} else {

			return true;	

		}

	}

}

//called in validateForm() and validateEmailForm()

function validate_email(value) {

	with (value) {

		apos=value.indexOf("@");

		dotpos=value.lastIndexOf(".");

		if (apos<1||dotpos-apos<2) {

		  	return false;

		} else {

			return true;

		}

	}

}

//this is called when opening the email form via AJAX, to load the reCAPTCHA javascript

//in IE this is necessary to avoid JS errors when the script has not already loaded

function addScript(){

	var script_ID='captcha_script';

	var head=document.getElementsByTagName('head').item(0);

	var scriptTag=document.getElementById(script_ID);

	if(!scriptTag){

		script=document.createElement('script');

		script.src="http://api.recaptcha.net/js/recaptcha_ajax.js";

		script.type='text/javascript';

		script.id=script_ID;

		head.appendChild(script);

	}

}	

//this function calls the goToEmail() function with the appropriate parameter after calling loadScript()

function showEmail(form){

	addScript();

	if(form=='conf') {

		goToEmail('conf');

	} else {

		setTimeout("goToEmail('mail')",500);

	}

}

//function called in showEmail to AJAX lod the email form or email confirmation onto the page

function goToEmail(theForm){

	if($("#email_holder").length>0){

		$("#email_holder").show();	 

	} else {

		$("body").append("<div id='email_holder'></div>");

		if(theForm=="conf"){

			$("#email_holder").load(confForm, shimit);

		} else {

			if(mailForm!=""){

				$("#email_holder").load(mailForm, pageInfo);

			}

		}

	}

	$("#emaillink").click(function(){return false;});	

}

//iframe shim for ie6

function shimit(){

	if (navigator.appName == "Microsoft Internet Explorer" && (!window.XMLHttpRequest)){	

		if($("#shim").length==0){

			shimmer=document.createElement('iframe');

			shimmer.id="shim";

			shimmer.style.height=$("#emailform").height()+30;

			shimmer.setAttribute('frameborder','0');

			shimmer.src="#";

			$("body").append(shimmer);

		} else {

			shimmer.style.height=$("#emailform").height()+30;	

		}

		$("#shim").css("visibility","visible");

	}

}

//removes email popup page from the page

function hideEmail(){

	if(document.getElementById("shim")){

		var shimmer = document.getElementById("shim");

		shimmer.style.visibility='hidden';

	}

	$("#email_holder").remove();

	$("#emaillink").click(showEmail);

}

//function called in goToEmail to set up hidden fields, and add iframe shim technique if > IE7

function pageInfo(){

	var pageTitle = document.title;

	var pageAddr = location.href;

	if((pageAddr.charAt(pageAddr.length-1))=="#"){

		pageAddr=pageAddr.substr(0,pageAddr.length-1);	

	}

	if(pageAddr.indexOf("?status=STF1")==-1){

		pageAddr = pageAddr + "?status=STF1";

	}

	if(document.eform.PageTitle){

		document.eform.PageTitle.value = pageTitle;

	}

	if(document.eform.C_Page_URL1){

		document.eform.C_Page_URL1.value = pageAddr;

	}

	if (navigator.appName == "Microsoft Internet Explorer" && (!window.XMLHttpRequest)){	

		if($("#shim").length==0){

			shimmer=document.createElement('iframe');

			shimmer.id="shim";

			shimmer.style.height=$("#emailform").height();

			shimmer.setAttribute('frameborder','0');

			shimmer.src="#";

			$("body").append(shimmer);

		} else {

			shimmer.style.height=$("#emailform").height();

		}

		$("#shim").css("visibility","visible");

	}

}

//this function is called on page load to check the state of the forms on the page

function formFunctions(){

	//you can change this variable's text to display any "Thank you" text in any language

	

	

	

	//var email_Conf="Thank you. Your message has been sent.";

	if($('#emaillink').length>0){

		mailForm=$("#emaillink").attr("href");

		mailCountry=mailForm.substr(0, mailForm.indexOf("_"));

		confForm=mailCountry+"_conf.html";

		$('#emaillink').attr("href","javascript:showEmail('mail');");	

	}

	var fullURL = location.href;

	//show Send to Friend confirmation if STF1

	if(fullURL.indexOf("status=STF1")!=-1 && $("#emaillink").length>0){

		showEmail('conf');

	}

	//show Request More Info confirmation if RMI1

	if($("#contact").length>0){

		if(fullURL.indexOf("status=RMI1")!=-1){

			if(!window.request_Conf){

				request_Conf=email_Conf;

				leadSubmit(this,'Request More Info');

			}

			$("#expand").css("display","none");

			$(".required").hide();

			$(".contactform").remove();

			$("#submit").remove();

			$("#contact").prepend("<div class='contactform'><p>"+request_Conf+"</p></div>");

		}

	}

	//handle return from reCAPTCHA server-side validation error. Repopulate form fields

	//with their values other than the recaptcha response

	if(fullURL.indexOf("captchaerror=1")!=-1){

		if($("#emaillink").length>0){

			$("#emaillink").click(function(){return false;});

			addScript();

			$("body").append("<div id='email_holder'></div>");

			$("#email_holder").load(mailForm, function(){

				pageInfo();

				var formAry=new Array('ToEmailAddress','FromEmailAddress','Subject','Message');

				var fieldAry=new Array(document.eform.emailto, document.eform.emailfrom, document.eform.subject, document.eform.emailmsg);

				for(q=0;q<formAry.length;q++){

					if(getQueryVariables(formAry[q])){

						fieldAry[q].value=getQueryVariables(formAry[q]);

					}

				}								

				$("#recaptcha_label").addClass("error");

			});

		}

	}

}

//see if variable is defined

function isDefined(object,variable){

	return(typeof(eval(object)[variable])!='undefined');	

}

//called in formFunctions to parse/de-encode the querystring paramaters on a reCAPTCHA error response

function getQueryVariables(variable) {

	var query = window.location.search.substring(1);

	var vars = query.split("&");

	var theAnswer;

	for (var i=0;i<vars.length;i++) {

		var pair = vars[i].split("=");

		if (pair[0] == variable) {

			theAnswer=decodeURIComponent(pair[1]);

			return theAnswer.replace(/\+/g," ");

		}

	}

}

//About Aon subnav menu rollovers, show/hide states, etc

function setBottomNav() {

	$(".subnavmenu").children('li').children('ul').hide();

	//this part adds/removes the blue arrow class on hovers

	$(".subnavmenu").children('li').hover(function(){

		if ($(this).hasClass("opened")) {

			return true;

		} else {

			if($(this).children('ul').length>0){

				$(this).addClass("open");		  

			} else {

				$(this).addClass("over");	

			}

		}

	},function(){

		if ($(this).hasClass("opened")) {

			return true; 			  

		} else {			  

			$(this).removeClass("open");

			$(this).removeClass("over");

		}

	});

	//this part handles the click actions only if the LI/A being clicked ha a child UL of hidden elements

	//otherwise, the link clicked will just take you to the href specified

	$(".subnavmenu").children('li').children('a').click(function(){

		if($(this).parent('li').children('ul').length>0){																	 

			if ($(this).parent('li').hasClass("opened")) {

				$(this).parent('li').children('ul').slideUp("slow");

				$(this).parent('li').removeClass("open").removeClass("opened");			  

			} else {

				$(this).parent('li').children('ul').slideDown("slow");

				$(this).parent('li').addClass("open").addClass("opened");

			}

		}

	});

}

//this function is called in the Request More Info form to show/hide the city and state selects if a user chooses United States

function hideState(sel) {

	$("#C_State").hide(); 

	$("#C_City").hide(); 	

	var selected = $(sel).val(); 

	if ((selected == "us")||(selected == "Australia")) { 

		$("#C_State").show(); 

		$("#C_City").show(); 		 

	} else { 

		$("#C_State").val("").hide();

		$("#C_City").val("").hide();		

	}  

}



//this function is called in the Request More Info form to show/hide the city and state selects if a user chooses United States

function hideStateSpecific(sel,countryVal) {

	$("#C_State").hide(); 

	$("#C_City").hide(); 	

	var selected = $(sel).val(); 

	if (selected == countryVal) { 

		$("#C_State").show(); 

		$("#C_City").show(); 		 

	} else { 

		$("#C_State").val("").hide();

		$("#C_City").val("").hide();		

	}  

}











//function caleld to open popup window to print the page

function printPage(loc) { 

	printPages = window.open(loc,"babywindow","width=880,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizeable=0");

	printPages.focus();

}

//show the print page if this is the print popup window

//add URL to the bottom of the page

function goToPrint() {

	if (window.name == 'babywindow'){

		if (navigator.appName != "Netscape"){

			$('link:last').attr('media','')			

		} else {

			$('link:last').append("<link href='css/print.css' rel='stylesheet' type='text/css'/>");

		}

		$("#container").append("<strong>Find this content at: </strong>" + location.href);

	}

}

function setHeight(){

	$('.bottom_bucket h2').each(function(){

		if($(this).height()<20){

			$(this).css("padding-top",8);			 

		}								 

	});

}



function validateFormWithPrivacyCheck() {

	document.getElementById("emailerr").style.display="none";

	document.getElementById("privacyerr").style.display="none";	

	var willReturn = 1;

	//required fields

	var reqAry=new Array(8)

	for (i=0; i <8; i++)

	reqAry[i]=new Array(2)

	reqAry[0][0] = document.cform.C_FirstName.value;

	reqAry[0][1] = document.getElementById("fnamelabel");

	reqAry[1][0] = document.cform.C_LastName.value;

	reqAry[1][1] = document.getElementById("lnamelabel");

	reqAry[2][0] = document.cform.C_Title.value;

	reqAry[2][1] = document.getElementById("titlelabel");

	reqAry[3][0] = document.cform.C_EmailAddress.value;

	reqAry[3][1] = document.getElementById("emaillabel");

	reqAry[4][0] = document.cform.C_Company.value;

	reqAry[4][1] = document.getElementById("companylabel");

	reqAry[5][0] = document.cform.C_BusPhone.value;

	reqAry[5][1] = document.getElementById("phonelabel");

	reqAry[6][0] = document.cform.C_Country.value;

	reqAry[6][1] = document.getElementById("countrylabel");

	reqAry[7][0] = document.cform.C_privacy.checked;

	reqAry[7][1] = document.getElementById("privacyerr");

	

	for(i=0; i<reqAry.length; i++){

		if(reqAry[i][0]==null||reqAry[i][0]==""){

			reqAry[i][1].className="error";

			willReturn = 0;

		} else {

			reqAry[i][1].className="";	

		}

	}

	if(reqAry[7][0]==false){

		reqAry[7][1].className="error";

		document.getElementById("privacyerr").style.display="inline";	

		willReturn = 0;

	} else{

		reqAry[7][1].className="";	

		document.getElementById("privacyerr").style.display="none";	

	}

	if(willReturn==0){

		return false;

	} else {

		if(validate_email(reqAry[3][0])==true){

			completeLead();

			return true;

		} else {

			document.getElementById("emailerr").style.display="inline";	

			return false;

		}

	}

}



