// For scripts commonly used throughout the site only

// Preload images for Nav
 
if (document.images)
{
  preloadImage = new Image();
  // set image url
  imageUrl = new Array();
  imageUrl[0] = "../img/header/subnavBg.gif";
  imageUrl[1] = "../img/header/navBgHover.gif";
  imageUrl[2] = "../img/header/navBgSel.gif";
  imageUrl[3] = "../img/header/navBg2.gif";
  imageUrl[4] = "../img/header/navBg.gif";
  imageUrl[5] = "../img/header/subNavTop.png";
  imageUrl[6] = "../img/header/subNavTop.gif";
  imageUrl[7] = "../img/header/subNavTopSelect.gif";
  imageUrl[8] = "../img/header/subNavBtm.png";
  imageUrl[9] = "../img/header/subNavBtm_short.png";
  imageUrl[10] = "../img/header/subNavBg_short.png";
  


   var i = 0;
   for(i=0; i<=10; i++) 
	 preloadImage.src = imageUrl[i];
} 


// This script toggles the visibility of divs
// Used for sign in drop down, referral affiliates info, classifieds advanced search
function toggle(elementName) {
	var ele = document.getElementById(elementName);
	if(ele.style.display == "block") {
			ele.style.display = "none";
	}
	else {
		ele.style.display = "block";
	}
} 


// BROWSER DETECT AND VARIABLES FOR DROPDOWN MENUS
// time before menu disappears when rolled out (1000 = 1 sec)
delayMenuTimeHover = 300;
delayMenuTime = 500;
// declare the timeout variable to prevent errors
var showDropDown;
var hideDropDown;
var topAdjust;
var currLeft;
var currTop;
var browserType;
// prevent positioning errors with browser rendering differences
if(window.opera){
	browserType="opera";
} else{
	if(navigator.userAgent.indexOf('Safari')!=-1){
			browserType="safari";
	}else{
		if(navigator.userAgent.indexOf('Gecko')!=-1){
		browserType="mozilla";
		}else{
			if(document.getElementById||document.all){
				browserType="iexplore";
			}
		}
	}
}
   
// SCRIPT TO CONTROL SUB /SUB SUB NAV MENU ITEMS
function navMenuDisplay(menuId,menuType){
	// hide all sub sub nav dropdowns from view, before showing the current one
	if(showDropDown>0){
		clearTimeout(showDropDown);
	}
	
	if(hideDropDown>0){
		clearTimeout(hideDropDown);
	}
	for(i=1;document.getElementById("navDropMenu"+i);i++){
		document.getElementById("navDropMenu"+i).style.display="none";
	}
	// go thru each menu button and make them normal state again
	for(j=1;j<=6;j++){
		parentTab=document.getElementById("navDropMenu"+j).getAttribute("rel");
		if(document.getElementById(parentTab).className=="navMenuHover"){
			document.getElementById(parentTab).className="navMenu";
		}
	}
	// now that previous hover states are cleared, make the rolled over link show hover state
	if(menuType=="main" && document.getElementById(menuId).className!="navMenuSelected"){
		document.getElementById(menuId).className="navMenuHover";
	}
	
	if(menuType=="main"){
		/* the hover states for forum nav will be back to normal states */
		if (document.getElementById('sectionNavForumCat1')){
			document.getElementById('sectionNavForumCat1').className="sectionNavOptDrop";
		}
		if (document.getElementById('sectionNavForumCat2')){
			document.getElementById('sectionNavForumCat2').className="sectionNavOptDrop";
		}
		if (document.getElementById('sectionNavNewTopic1')){
			document.getElementById('sectionNavNewTopic1').className="sectionNavOptDrop2";
		}
		if (document.getElementById('sectionNavNewTopic2')){	
			document.getElementById('sectionNavNewTopic2').className="sectionNavOptDrop2";	
		}
	}
		
	// go thru each menu button and make them normal state again
		
	if(menuType=="sub" && document.getElementById(menuId).className=="sectionNavOptDrop"){
		document.getElementById(menuId).className="sectionNavOptDropHover";
	}
	else if(menuType=="sub" && document.getElementById(menuId).className=="sectionNavOptDrop2"){
		document.getElementById(menuId).className="sectionNavOptDropHover2";
	}
	
	// check if it is a sub nav menu or sub sub nav menu (they have different offset widths/heights for diff browsers)	
	
	if(browserType=="opera"){
		if(menuType=="main"){
			topAdjust=36;
			leftAdjust=0;
		}else{
			topAdjust=22;
			leftAdjust=0;
		}
	}
	if(browserType=="safari"){
		if(menuType=="main"){
			topAdjust=36;
			leftAdjust=0;
		}else{
			topAdjust=18;
			leftAdjust=0;
		}
	}
	if(browserType=="mozilla"){
		if(menuType=="main"){
			topAdjust=36;
			leftAdjust=0;
		}else{
			topAdjust=20;
			leftAdjust=0;
		}
	}
	if(browserType=="iexplore"){
		
		if (navigator.appName == 'Microsoft Internet Explorer') {
			var ua = navigator.userAgent;
			var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); 
			if (re.exec(ua) != null)
			rv = parseFloat(RegExp.$1);
		} 
		
		if (rv >= 8.0){		
			if(menuType=="main"){
				topAdjust=35;
				leftAdjust=1;
			}else{
				topAdjust=22;
				leftAdjust=1;
			}
		} 		
		else{
			if(menuType=="main"){
				topAdjust=36;
				leftAdjust=0;
			}else{
				topAdjust=22;
				leftAdjust=1;
			}	
		} 
	}
	// check the links' rel attribute for the relevant dropdown to display
	menuRef=document.getElementById(menuId).getAttribute("rel");
	// run the findPos function to find the "top" position (relevant to the <li> top position) to place the dropdown div
	// not relevant if used for a help tool tip
	if(menuType!=="pageHelp"){
		findPos(document.getElementById(menuId).parentNode);
		document.getElementById(menuRef).style.top=currTop+topAdjust+"px";
		document.getElementById(menuRef).style.left=currLeft+leftAdjust+"px";
	}
	// now display it
	
	if(menuType=="sub")
		showDropDown = setTimeout(showDropDownMenu,0);
	else
		showDropDown = setTimeout(showDropDownMenu,delayMenuTimeHover);
	function showDropDownMenu(){
		document.getElementById(menuRef).style.display="block";
	}
	
	// clear the timeouts when user rolls over the dropdown (stop it from disappearing)
	document.getElementById(menuRef).onmouseover=function(){		
		if (showDropDown >0){			
		clearTimeout(showDropDown);		
		}	
		
		if(hideDropDown>0){
			clearTimeout(hideDropDown);
		}
	}
	// make sure that the menu fades off after the menu is rolled out
	document.getElementById(menuRef).onmouseout=function(){
		hideDropDown = setTimeout(hideDropDownMenu,delayMenuTime);
	}
	// make sure that the menu fades off after the parent menu link is rolled out also
	document.getElementById(menuId).onmouseout=function(){
		if (showDropDown >0){			
			clearTimeout(showDropDown);		
		}
		// clear timeout first! Important, or the previous setTimeout may still be in effect
		if(hideDropDown>0){
			clearTimeout(hideDropDown);
		}
		hideDropDown = setTimeout(hideDropDownMenu,delayMenuTime);
	}
	function hideDropDownMenu(){
		// hide the dropdown
		document.getElementById(menuRef).style.display="none";
		// go thru each menu button and make them normal state again (except the selected category link)
		for(k=1;k<=6;k++){
			parentTab=document.getElementById("navDropMenu"+k).getAttribute("rel");
			if(document.getElementById(parentTab).className=="navMenuHover"){
				document.getElementById(parentTab).className="navMenu";
			}
		}
		
		if(menuType=="sub" && document.getElementById(menuId).className=="sectionNavOptDropHover"){
			document.getElementById(menuId).className="sectionNavOptDrop";
		}
		else if(menuType=="sub" && document.getElementById(menuId).className=="sectionNavOptDropHover2"){
			document.getElementById(menuId).className="sectionNavOptDrop2";
		}
	}
}

// used for finding position to place the dropdown menu (based on parent object (list item) position of <a> object)
function findPos(obj) {
		currLeft = currTop = 0;
	if (obj.offsetParent) {
		do {
		currLeft += obj.offsetLeft;
		currTop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [currLeft,currTop];
}
}


// This script toggles the visibility of a div and switches text
// Currently used for the QuickStart Guide
function toggleDivTxt(divBody, divTxt) {
	var ele = document.getElementById(divBody);
	var text = document.getElementById(divTxt);
	if(ele.style.display == "block") {
    		ele.style.display = "none";
		text.innerHTML = "<a href=\"#\" onclick=\"toggleDivTxt('guideBodyWrap', 'guideBase');return false;\">View</a>";
  	}
	else {
		ele.style.display = "block";
		text.innerHTML = "<a href=\"#\" onclick=\"toggleDivTxt('guideBodyWrap', 'guideBase');return false;\">Hide</a>";
	}
}

// This script clears default text in form fields
function doClear(theText) 
{
	 if (theText.value == theText.defaultValue) {
		 theText.value = "";
	 }
}

function CheckInput(txtInput,errDiv) {
    var t = document.getElementById(txtInput),
        d = document.getElementById(errDiv);
    if(d) {
        d.style.display=t.value.length?"none":"block";
        t.style.border=t.value.length?"":"solid 1px #CC0000";
    }
    return t.value.length>0;
}

 
