//*****************************
// Global Variable Declarations
//*****************************
var precacheComplete = false			//Navigation Bar Variables
var timerOn = false
var dropTab = false
var navigating = false
var timerID = null
var prevTab = null

var objPrevMenu = null            //Drop-Down Menu Variables
var menuTimerID = null
var inputHasFocus = false

//*************************
// Navigation Bar Functions
//*************************
function newImage(iPath) {
	if (document.images) {
		var imgObj = new Image()
		imgObj.src = iPath
		return imgObj
	}
}

if (document.images) {
	var imgCache = new Array(14)
	imgCache[0] = newImage("/images/ntab1_on.gif")
	imgCache[1] = newImage("/images/ntab2_on.gif")
	imgCache[2] = newImage("/images/ntab3_on.gif")
	imgCache[3] = newImage("/images/ntab4_on.gif")
	precacheComplete = true
}

function swapImages() {
	if (precacheComplete) {
		for (var i=0; i<swapImages.arguments.length; i+=2)
			document.getElementById(swapImages.arguments[i]).src = swapImages.arguments[i+1]
	}
}

function showTabBar(tBar) {
	if (precacheComplete) {
		switch (tBar) {
			case 1:
				document.getElementById("bar1").style.zIndex = 4
				document.getElementById("bar2").style.zIndex = 3
				document.getElementById("bar3").style.zIndex = 2
				document.getElementById("bar4").style.zIndex = 1
				break
			case 2:
				document.getElementById("bar1").style.zIndex = 3
				document.getElementById("bar2").style.zIndex = 4
				document.getElementById("bar3").style.zIndex = 2
				document.getElementById("bar4").style.zIndex = 1
				break
			case 3:
				document.getElementById("bar1").style.zIndex = 3
				document.getElementById("bar2").style.zIndex = 2
				document.getElementById("bar3").style.zIndex = 4
				document.getElementById("bar4").style.zIndex = 1
				break
			case 4:
				document.getElementById("bar1").style.zIndex = 3
				document.getElementById("bar2").style.zIndex = 2
				document.getElementById("bar3").style.zIndex = 1
				document.getElementById("bar4").style.zIndex = 4
		}
	}
}

function stopTimer() {
	clearTimeout(timerID)
	timerOn = false
}

function stopReset() {
	navigating = true
}

function setNavControl(c, r) {                                  // c = current tab
	if (timerOn) {                                                // r = return tab
		stopTimer()
		dropTab = true
	}
	timerID = setTimeout("processSet(" + c + "," + r + ")", 100)
	timerOn = true
}

function resetNavControl(c, r) {
	if (!navigating) {
		if (timerOn) stopTimer()
		timerID = setTimeout("processReset(" + c + "," + r + ")", 750)
		timerOn = true
	}
}

function processSet(c, r) {
	timerOn = false
	if (dropTab) {
		if (prevTab == null) prevTab = r
		swapImages("tab" + prevTab, "/images/ntab" + prevTab + "_off.gif")
		dropTab = false
	}
	prevTab = c
	  
	if (c != r)
		swapImages("tab" + c, "/images/ntab" + c + "_on.gif", "tab" + r, "/images/ntab" + r + "_off.gif")
	else
		swapImages("tab" + c, "/images/ntab" + c + "_on.gif")

	showTabBar(c)
}

function processReset(c, r) {
	timerOn = false
	swapImages("tab" + r, "/images/ntab" + r + "_on.gif", "tab" + c, "/images/ntab" + c + "_off.gif")
	showTabBar(r)
}

//*************************
// Drop-Down Menu Functions
//*************************
function menuHeadOver(menuNum) {
	clearTimeout(menuTimerID)
	showMenu(menuNum)
	if (inputHasFocus) menuLoginReset()
}

function menuOver() {
	clearTimeout(menuTimerID)
	document.onclick = null
}

function menuOut() {
	if (inputHasFocus)
		document.onclick = hideMenu
	else
		menuTimerID = setTimeout("hideMenu()", 200)
}

function menuLoginOver(frm) {
	frm.className = 'menu-item-over'
}

function menuLoginOut(frm) {
	if (!inputHasFocus) frm.className = 'menu-item-out'
}

function inputFocus() {
	inputHasFocus = true
}

function inputBlur() {
	inputHasFocus = false
}

function menuLoginReset() {
	with (document.getElementById('menuLogin')) {
		className = 'menu-item-out'
		username.blur()
		password.blur()
	}
}

function showMenu(menuNum) {
	var objMenu = document.getElementById('menu' + menuNum)
	if (objPrevMenu != null) hideMenu()	
	if (objMenu) {
		if (menuNum == 1) document.getElementById('siteSearchOptions').style.visibility = 'hidden'
		objMenu.style.visibility = 'visible'
		objPrevMenu = objMenu
	}
}

function hideMenu() {
	document.onclick = null
	if (objPrevMenu) {
		objPrevMenu.style.visibility = 'hidden'
		objPrevMenu = null
		document.getElementById('menuLogin').className = 'menu-item-out'
		document.getElementById('siteSearchOptions').style.visibility = 'visible'
	}
}

//**********************
// Site Search Functions
//**********************
function setQryField(frm) {
	if (frm.opt.value == "2" || frm.opt.value == "3") {
		if (frm.qry.value == "" || frm.qry.value == "Enter Keywords")
			frm.qry.value = "Enter City or Zip Code"
	} else {
		if (frm.qry.value == "" || frm.qry.value == "Enter City or Zip Code")
			frm.qry.value = "Enter Keywords"
	}
}

function clearQryField(fld) {
	if (fld.value == "Enter City or Zip Code" || fld.value == "Enter Keywords")
		fld.value = ""
}

function validateSearch(frm) {
	if (frm.opt.value == 1) {
		if (frm.qry.value.length == 0 || frm.qry.value == "Enter Keywords") {
				alert("You must enter one or more keywords.")
				frm.qry.focus()
				return false
		} else if (frm.qry.value.search(/\*/) != -1 && frm.qry.value.search(/\s/) != -1) {
				alert("You cannot enter more than one keyword when using a wildcard (*).")
				frm.qry.focus()
				return false
		} else if (frm.qry.value.search(/\*\S+$/) != -1) {
				alert("You can only enter a wildcard (*) at the end of your search term.")
				frm.qry.focus()
				return false
		} else if (frm.qry.value == '*') {
				alert("You cannot search for a wildcard (*) by itself.")
				frm.qry.focus()
				return false
		}
	}
	return true
}

//*****************
// Set Current Date
//*****************
function dateString(today) {
	var months = new Array(12)
	months[0]  = "January"
	months[1]  = "February"
	months[2]  = "March"
	months[3]  = "April"
	months[4]  = "May"
	months[5]  = "June"
	months[6]  = "July"
	months[7]  = "August"
	months[8]  = "September"
	months[9]  = "October"
	months[10] = "November"
	months[11] = "December"

	var theMonth = months[today.getMonth()]
	var theDate = today.getDate()
	var theYear = today.getFullYear()
	return theMonth + " " + theDate + ", " + theYear
}
