var isNav = (navigator.appName == "Netscape");
var is_mac;
var is_Safari;
var imgSeparatorBaseName = "img_";
var imgBasePath = "/images/";

var left = [0, 154, 308, 462, 617];

window.onload = init;

function init() {
			setCaptcha();
			//adjustOuterContainerPos();
			//adjustLeftPos();
			//showDropDownMenu();
			setGlobalVar();
			changeStyles();
			//setTopNav();
			//setBrain();
			//rollovers();
			//menus();
			//menuPosition();
			url = unescape(window.location.pathname);
			//window.onresize = resizeWindow; //Use this function for layout with possible menu position shift on broweser resizing
	
			window.onresize = function(){
				//setTopNav(); 
//				menuPosition();
				}
		}
		function setCaptcha(){
			var pageLocation = "'" + window.location + "'";
			if(pageLocation.indexOf("drnida/index.asp") > -1){
					var path = "../";
					var alt = ["3WT6$", "J45C$", "N56K$", "X1H8$", "P78F$"];
					var base_alt = "Swap the last and the first characters in the following verification code: ";
					
					var captcha = document.getElementById("captcha");
					if(captcha){
						var randomNumber = Math.floor(Math.random() * alt.length)
						captcha.src = path + "images/captcha_" + parseInt(randomNumber + 1) + ".gif";
						captcha.alt = base_alt + alt[randomNumber];
						captcha.title = base_alt + alt[randomNumber];
					}
			}
		}
		
		function formValidate(myForm) {
			var message = "";
			var sender = myForm.sender;
			var from = myForm.from;
			var question = myForm.question;
			var verify = myForm.verify;

			if(!isNotEmpty(sender) || (sender.value == "Enter Your Name.")){
				message += "Please Enter Your Name." + "\r\n";
			}
			if(!isNotEmpty(question)|| (question.value == "Enter your subject & question here.")){
				message += "Please Enter Your Subject and Question." + "\r\n";
			}
			if(!isEMailAddr(from)){
				message += "Please Enter Your Email." + "\r\n";
			}
			if(!isValidCaptcha(verify)){
				message += "Please Enter Correct Code." + "\r\n";
			}
			
			if(isNotEmpty(sender)){
				if(isNotEmpty(question)){
					if(isEMailAddr(from)){
						if(isValidCaptcha(verify)){
							return true;
						}
					}
				}
			}
			alert(message);
			return false;
		}
// validates CAPTCHA code
function isValidCaptcha(elem){
	var codes = ["3WT6$", "$45CJ", "$56KN", "$1H8X", "$78FP"];
	var str = elem.value;
    var found = false;
	
	for(var i = 0; i < codes.length; i++){
		if(str == codes[i]){
			found = true;
		}
	}
	if(found == true){
		return true;
	}else{
		return false;
	}
}

// validates that the field value string has one or more characters in it		
function isNotEmpty(elem) {
	var str = elem.value;
    var re = /.+/;
    if(!str.match(re)) {
        //alert("Please fill in the required field.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
// validates that the entry is formatted as an e-mail address
function isEMailAddr(elem) {
	var str = elem.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
        //alert("Verify the e-mail address format.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
function focusElement(formName, elemName) {
    var elem = document.forms[formName].elements[elemName];
    elem.focus();
    elem.select();
}



		function setTopNav(){
			var banner = document.getElementById("banner");
			var topNav = document.getElementById("topNav");
			var bannerHeight = 75;
			var bannerLeft = getElementPosition("banner").left;
			var topNav_Left = bannerLeft;
			var topNav_Top = bannerHeight;
			
			if(topNav){
				topNav.style.position = "absolute";
				topNav.style.left = topNav_Left + "px";
				topNav.style.top = topNav_Top + "px";
			}
		}
		
		function setBrain(){
			var brain_home = document.getElementById("brain_home");
			var brain = document.getElementById("brain");
			var exerciseBox_Pos = getElementPosition("exerciseBox");
			var exerciseBox_Left = exerciseBox_Pos.left;
			var exerciseBox_Top = exerciseBox_Pos.top;
			var brain_home_Left;
			var brain_home_Top;
			
			 brain_home_Left = exerciseBox_Left + 212;
			 brain_home_Top = exerciseBox_Top + 25;
			 
			 if(brain_home){
				brain_home.style.left = brain_home_Left + "px";
				brain_home.style.top = brain_home_Top + "px";
			 }
			 
			if(brain){
				brain.style.left = brain_home_Left + "px";
				brain.style.top = brain_home_Top + "px";
			 }
		}
		
		function adjustOuterContainerPos() {
			var containerWidth = 1000;
			var outerContainer = document.getElementById("outerContainer");
			var screenWidthCom = document.body.offsetWidth;
			
				if(outerContainer){
					if(screenWidthCom >= containerWidth){
					
						outerContainer.style.left = 0 + "px";
						outerContainer.style.marginLeft = (screenWidthCom - containerWidth)/ 2  + "px";
					}else{
						outerContainer.style.left = 0 + "px";
						outerContainer.style.marginLeft = 0 + "px";
					}
					
				}
			
		}
		// Adjusts position of the outer Container for small screen resolution (less than 1000)
		function adjustLeftPos() {
			if(screen.width <= 800){
			
				var outerContainer = document.getElementById("outerContainer");
				if(outerContainer){
					var outerContainerX = getElementPosition("outerContainer").left;
			
					if(outerContainerX < 0){
						outerContainer.style.left = 0 + "%";
						outerContainer.style.marginLeft = 0 + "px";
					}else{
						outerContainer.style.left = 50 + "%";
						outerContainer.style.marginLeft = -462 + "px";
					}
				}
			}
		}
		
		function getElementPosition(elemID){
			var offsetTrail = document.getElementById(elemID);
			var offsetLeft = 0;
			var offsetTop = 0;
			while(offsetTrail){
				offsetLeft += offsetTrail.offsetLeft;
				offsetTop += offsetTrail.offsetTop;
				offsetTrail = offsetTrail.offsetParent;
			}
			return {left:offsetLeft, top:offsetTop};
		}

		function showDropDownMenu(){
			var i;
			var numberOfMenus = left.length;
			var baseName = "dropDownMenu_";
			var menuName, menu;
			
			for(i = 0; i < numberOfMenus; i ++){
				menuName = baseName + i;
				menu = document.getElementById(menuName);
				
				if(menu){
					menu.style.visibility = "hidden";
					menu.style.position = "absolute";
					menu.style.top = 102 + "px";
					menu.style.zIndex = 10;
					menu.style.left = left[i];
				}
			}
			
		}
		
		function resizeWindow () {
			//window.location.reload(); // this method doesn't work for FireFox
			window.location.href = url;
		}
		
		function setGlobalVar(){
		//Detecting Mac 
			var version = navigator.appVersion;
			is_mac = (version.toLowerCase().indexOf("mac")!=-1);
			
		//Detecting Safari
			var agt = navigator.userAgent.toLowerCase();
			is_Safari = (agt.indexOf("safari") != -1);
		}
		
		function getIEVersionNumber() {
    		var ua = navigator.userAgent;
    		var MSIEOffset = ua.indexOf("MSIE ");
    		if (MSIEOffset == -1) {
        		return 0;
    		} else {
        		return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    		}
		}


		function changeStyles () {
			
			var noscriptQuiz = document.getElementById("noscriptQuiz");
			if(noscriptQuiz){
				noscriptQuiz.style.display = "none";	
				
			}
			
			var brain_home = document.getElementById("brain_home");
			if(brain_home){
				if(is_Safari){
					brain_home.style.top = 319 + "px";	
				}else if(isNav){
					brain_home.style.top = 316 + "px";
				}
			}
			
			var inputSearch = document.getElementById("inputSearch");
			if(inputSearch){
				if(is_Safari){
					inputSearch.style.marginBottom = 19 + "px";	
				}else{
					inputSearch.style.marginBottom = 9 + "px";
				}
			}
			
			var safari_spacer = document.getElementById("safari_spacer");
			if(safari_spacer){
				if(!is_Safari){
					safari_spacer.style.display = "none";	
				}
			}
			
			var buttonSearch = document.getElementById("buttonSearch");
			if(buttonSearch){
				if(is_Safari){
					buttonSearch.style.marginBottom = 9 + "px";	
				
				}
			}
			
			var arrowGlossary = document.getElementById("arrowGlossary");
			if(isNav && arrowGlossary){
				arrowGlossary.style.marginTop = -7 + "px";
			}
			
			var arrowExercise = document.getElementById("arrowExercise");
			if(isNav && arrowExercise){
				arrowExercise.style.marginTop = -7 + "px";
			}
			
			var arrowDr = document.getElementById("arrowDr");
			if(isNav && arrowDr){
				arrowDr.style.marginTop = -7 + "px";
			}
			
			var arrowMom = document.getElementById("arrowMom");
			if(isNav && arrowMom){
				arrowMom.style.marginTop = -7 + "px";
			}
			
			var arrowAnswer = document.getElementById("arrowAnswer");
			if(isNav && arrowAnswer){
				arrowAnswer.style.marginTop = -9 + "px";
			}
			
			var dropDownMenu4 = document.getElementById("dropDownMenu_4");
			if(isNav && dropDownMenu4){
				dropDownMenu4.style.left = 618 + "px";
			}
			
			var innerLeftDrill_2 = document.getElementById("innerLeftDrill_2");
			/*if(isNav && innerLeftDrill_2){
				innerLeftDrill_2.style.paddingTop = 28 + "px";
				innerLeftDrill_2.style.marginTop = 0 + "px";
			}*/
			
			var innerLeftDrill_3 = document.getElementById("innerLeftDrill_3");
			/*if(isNav && innerLeftDrill_3){
				innerLeftDrill_3.style.paddingTop = 28 + "px";
				innerLeftDrill_3.style.marginTop = 0 + "px";
			}*/

			var form_dr = document.getElementById("form_dr");
			if(is_mac && form_dr){
				form_dr.style.marginTop = -80 + "px";
				
			}
			var nameDrNIDA = document.getElementById("nameDrNIDA");
			if(is_mac && nameDrNIDA){
				nameDrNIDA.style.marginBottom = 2 + "px";
				
			}
			var txt_drill_2 = document.getElementById("txt_drill_2");
			if(is_mac && txt_drill_2){
				txt_drill_2.style.marginBottom = 2 + "px";
				
			}

			var innerLeftDrill_3 = document.getElementById("innerLeftDrill_3");
			if(is_mac && innerLeftDrill_3){
				innerLeftDrill_3.style.paddingBottom = 10 + "px";
				
			}

			var formSpacerTop = document.getElementById("formSpacerTop");
			if(!is_mac && formSpacerTop){
				formSpacerTop.style.display = "none";
				
			}
			if(is_mac && formSpacerTop){
				formSpacerTop.style.marginTop = 168 + "px";
			}
			
			var itemFirst = document.getElementById("itemFirst");
			if(isNav && itemFirst){
				itemFirst.style.marginTop = -6 + "px";
				
			}
			var FF_spacer = document.getElementById("FF_spacer");
			if(isNav && FF_spacer){
				FF_spacer.style.display = "block";
				
			}
			
			/* Changes for Brain Image on the drill pages */
			var isIE5Min = getIEVersionNumber() >= 5;
			var brain = document.getElementById("brain");
			
			if (isIE5Min && brain) {
    			brain.style.marginTop = 0 + "px";
			}
			
			var btn_glossary = document.getElementById("btn_glossary");
			
			if (isIE5Min && btn_glossary) {
    			btn_glossary.style.marginTop = -9 + "px";
			}
			
			var btn_glossary_drill = document.getElementById("btn_glossary_drill");
			
			if (isIE5Min && btn_glossary_drill) {
    			//btn_glossary_drill.style.marginTop = -4 + "px";
			}
			
			var answerBox_1 = document.getElementById("answerBox_1");
			if (isIE5Min && answerBox_1) {
    			answerBox_1.style.marginBottom = -18 + "px";
			}
			
			var btn_dr_home = document.getElementById("btn_dr_home");
			if (isIE5Min && btn_dr_home) {
    			btn_dr_home.style.marginTop = -3 + "px";
			}
			
			var btn_treatment_drill = document.getElementById("btn_treatment_drill");
			
			if (isIE5Min && btn_treatment_drill) {
    			btn_treatment_drill.style.marginTop = -4 + "px";
			}
		}
		
		
		
		function swapBorderColor(id, color){
				var imgID = imgSeparatorBaseName + id;
				var img = document.getElementById(imgID);
				
				var num = parseInt(id.substring(id.indexOf('_') + 1));
				num++ ;
				var imgID_2 = imgSeparatorBaseName + id.substring(0, id.indexOf('_') + 1) + num;
				var img_2 = document.getElementById(imgID_2);
			
				if(img){
					img.src = imgBasePath + color + ".gif";
					
				}
				
				if(img_2){
					img_2.src = imgBasePath + color + ".gif";
 
				}
            	
               
          
       
		}
		
		
