var currentMenu = null;
var menu = null;
var intMenuTimer;
var intTimeoutID;

if (!document.getElementById)
    document.getElementById = function() { return null; }

function initializeMenu(menuId, actuatorId) {
    
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);
    
    if (menu == null || actuator == null) return;
	
	menu.onmouseout = function() {
		if (currentMenu == null) {
			intTimeoutID = window.setTimeout("destroyMenu()",500);
		}
    }
    
    menu.onmouseover = function() {
		window.clearTimeout(intTimeoutID);
    }
    
    menu.onmousemove = function() {
		window.clearTimeout(intTimeoutID);
    }
	
    actuator.onmouseover = function() {
	
		window.clearTimeout(intTimeoutID);
		
		//Check to see if no menu has been selected, if not - display the current one selected
		if (currentMenu == null) {
			this.showMenu();
        }
		
		//If a menu is already active and a new one is selected, hide the old display the new
        if (currentMenu) {
            currentMenu.style.visibility = "hidden";
            this.showMenu();
        }
    }
    
    //This function will destroy the current menu with the menubar loses focus
    actuator.onmouseout = function() {
   		intTimeoutID = window.setTimeout("destroyMenu()",500);
    }
	
	actuator.onmousemove = function() {
		window.clearTimeout(intTimeoutID);
    }
	
    actuator.onclick = function() {
        
        if (currentMenu == null) {
			this.showMenu();
        }
        else {
			currentMenu.style.visibility = "hidden";
            currentMenu = null;
        }
    }
	
    actuator.showMenu = function() {
		
		switch(this.id){
		case "homeActuator":
			menu.style.left = document.getElementById("tblMain").offsetLeft + "px";
			break;
		case "optionsActuator":
			menu.style.left = document.getElementById("tblMain").offsetLeft + 81 + "px";
			break;
		case "aboutActuator":
			menu.style.left = document.getElementById("tblMain").offsetLeft + 158 + "px";
			break;
		case "contactActuator":
			menu.style.left = document.getElementById("tblMain").offsetLeft + 237 + "px";
			break;
		default:
			menu.style.left = "0px";
			menu.style.visibility = "hidden";
		}
		
		menu.style.top = document.getElementById("tblMain").offsetHeight + "px";
        menu.style.visibility = "visible";
        currentMenu = menu;
    }
}

var intOpacity = 60
var objImage, objBrowser, hightlighting;

function destroyMenu() {
	
	if (currentMenu != null) {
		
		if (document.all) { 
			//Internet Explorer Section
			//Get the element that was click to call this function. 
			var strCurrentElement;
			strCurrentElement = document.activeElement.name;
			
			if (strCurrentElement == null) {
				//Kill the menu...
				currentMenu.style.visibility = "hidden";
				currentMenu = null;
			}
			else if (strCurrentElement.substring(0,3) != "btn") {
				//Kill the menu...
				currentMenu.style.visibility = "hidden";
				currentMenu = null;
			}		
		}
		else if (document.layers) {
			//Netscape 4+
			currentMenu.style.visibility = "hidden";
			currentMenu = null;
		}
		else if (document.getElementById) {
			//Netscape 6+
			currentMenu.style.visibility = "hidden";
			currentMenu = null;
		} 
	}
}

function fadeIn(strImage) {
	objImage = strImage
	objBrowser = strImage.filters? "ie" : typeof strImage.style.MozOpacity=="string"? "mozilla" : ""
	resetOpacity(intOpacity)
	highlighting = setInterval("startFade(objImage)", 50)
}

function fadeOut(strImage) {
	resetTimer();
	resetOpacity(intOpacity);
}

function resetOpacity(intAmount) {
	if (objBrowser == "mozilla") {
		objImage.style.MozOpacity = intAmount / 100
	}
	else if (objBrowser == "ie") {
		objImage.filters.alpha.opacity = intAmount
	}
}

function resetTimer() {
	if (window.highlighting) {
		clearInterval(highlighting)
	}
}

function startFade(objImageToFade) {
	if (objBrowser == "mozilla" && objImageToFade.style.MozOpacity < 1) {
		objImageToFade.style.MozOpacity = Math.min(parseFloat(objImageToFade.style.MozOpacity) + 0.1, 0.99)
	}
	else if (objBrowser == "ie" && objImageToFade.filters.alpha.opacity < 100) {
		objImageToFade.filters.alpha.opacity += 5
	}
	else if (window.highlighting) {
		clearInterval(highlighting)
	}
}

function loadIFrame(strFrameName, strURL) {
	if ( window.frames[strFrameName] ) {
		window.frames[strFrameName].location = strURL;
		if (currentMenu != null) {
			currentMenu.style.visibility = "hidden";
			currentMenu = null;
		}
		return false;
	}
	else {
		return true;
	}
}

function showSearchOptions() {
	if (document.getElementById) {
			document.getElementById('searchOptions').style.visibility = 'visible';
			document.getElementById('searchResults').style.top = '200px';
		}
		else {
			if (document.layers) {
				document.searchOptions.visibility = 'visible';
			}
			else {
				document.all.searchOptions.style.visibility = 'visible';
			}
		}
}

function hideSearchOptions() {
	if (document.getElementById) {
			document.getElementById('searchOptions').style.visibility = 'hidden';
			document.getElementById('searchResults').style.top = '0px';
		}
		else {
			if (document.layers) {
				document.searchOptions.visibility = 'hidden';
			}
			else {
				document.all.searchOptions.style.visibility = 'hidden';
			}
		}
}

function openContactForm() {
	if (document.getElementById) {
			document.getElementById('contactForm').style.visibility = 'visible';
		}
		else {
			if (document.layers) {
				document.contactForm.visibility = 'visible';
			}
			else {
				document.all.contactForm.style.visibility = 'visible';
			}
		}
}

function closeContactForm() {
	if (document.getElementById) {
			document.getElementById('contactForm').style.visibility = 'hidden';
		}
		else {
			if (document.layers) {
				document.contactForm.visibility = 'hidden';
			}
			else {
				document.all.contactForm.style.visibility = 'hidden';
			}
		}
}

function openEmailForm() {
	if (document.getElementById) {
			document.getElementById('emailForm').style.visibility = 'visible';
		}
		else {
			if (document.layers) {
				document.emailForm.visibility = 'visible';
			}
			else {
				document.all.emailForm.style.visibility = 'visible';
			}
		}
}

function closeEmailForm() {
	if (document.getElementById) {
			document.getElementById('emailForm').style.visibility = 'hidden';
		}
		else {
			if (document.layers) {
				document.emailForm.visibility = 'hidden';
			}
			else {
				document.all.emailForm.style.visibility = 'hidden';
			}
		}
}

function openConstructionForm() {
	if (document.getElementById) {
			document.getElementById('underConstruction').style.visibility = 'visible';
		}
		else {
			if (document.layers) {
				document.underConstruction.visibility = 'visible';
			}
			else {
				document.all.underConstruction.style.visibility = 'visible';
			}
		}
}

function closeConstructionForm() {
	if (document.getElementById) {
			document.getElementById('underConstruction').style.visibility = 'hidden';
		}
		else {
			if (document.layers) {
				document.underConstruction.visibility = 'hidden';
			}
			else {
				document.all.underConstruction.style.visibility = 'hidden';
			}
		}
}

function checkContactForm() {
	if (document.getElementById) {
			
			var contactName = document.getElementById('txtContactName').value;
			var contactEmail = document.getElementById('txtContactEmail').value;
			var contactPhone = document.getElementById('txtContactPhone').value;
			
			//Check to ensure there was sufficient data passed through the contact form
			if (contactName != '') {
				if (contactEmail == '' && contactPhone == '') {
					alert('Oops! It appears you did not enter an email address or phone number...');
				}
				else {
				
					//If the email field has something in it and it does not conform to the email address standard, throw an error
					if (contactEmail != '' && checkMail(contactEmail)) {
						
						//Submit the form for storage
						document.frmContact.submit();
					}
					else {
						alert('Oops! It appears the e-mail address you entered is not valid...');
					}
				}
			}
			else {
				alert('Oops! You seem to have forgotten your name...');
			}
		}
		else {
			if (document.layers) {
				//alert(document.emailForm.txtContactForm.value);
			}
			else {
				//alert(document.all.emailForm.txtContactForm.value);
			}
		}
	}
	
function checkMail(emailAddress) {
				
	//Get the passed address and create the filter
	var currAddress = emailAddress;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	//Check to see if the address passes the expression
	if (filter.test(currAddress)) {
		return true;
	}
	else {
		return false;
	}
}

function checkEmailForm() {
	if (document.getElementById) {
			
			var emailYourName = document.getElementById('txtYourName').value;
			var emailYourEmail = document.getElementById('txtYourEmail').value;
			var emailFriendName = document.getElementById('txtFriendName').value;
			var emailFriendEmail = document.getElementById('txtFriendEmail').value;
			
			//Check to ensure something was entered for each input
			if (emailYourName == '' || emailYourEmail == '' || emailFriendName == '' || emailFriendEmail == '') {
				alert('Oops! It appears you have not completed all required information...');
			}
			else {
				if (!checkMail(emailYourEmail) || !checkMail(emailFriendEmail)) {
					alert('Oops! It appears one or both of the e-mail addresses you entered are not valid...');
				}
				else {
					//Submit the form and send the email
					document.frmEmail.submit();
				}
			}
		}
		else {
			if (document.layers) {
				//alert(document.emailForm.txtContactForm.value);
			}
			else {
				//alert(document.all.emailForm.txtContactForm.value);
			}
		}
	}
						