//Declare variables
var xmlHttp
var menuItems
var itemId
var intCounter
var intervalId
var intervalIdOut
var currentState
var currentPage
var currentImage
var time
var intIncrement = 22;
var intTabCounter = 18;
var tabId;
var originalMoveDone = "false";
var tabTimer;
var contractTimer;
var	expandTimer;
var safeToMoveTab = "false";
var currentMenuHTML
var currentCompany
var currentSolutions
var currentLeftDivHeight
var currentRightDivHeight
var currentNavItem
var emailClean
var currentHTML
var returnedMenu = "Loading Sub Navigation ..."
var safeToLoad = "false"
var currentOpenMenu = "";
var noMouseOverOn = "";
var expand = "true";
var currentHeight;
var intCounter = 0;
var returnValue;
var highLightItem;

//Declare form variables
var returnValue
var hearAbout
var title
var firstName
var lastName
var hospitalName
var address
var city
var zip
var emailAddress
var telephone
var numBeds
var numVisits
var currentEMR
var typeofEMR
var longEMR
var EDtrans
var contactTime
var str

//Do this function on page load
function OriginalMoveTab(action,item)
{
	//Took this out at client request
/*	if(originalMoveDone == "false")
	{
		clearInterval(tabTimer);
		clearInterval(expandTimer);
		clearInterval(contractTimer)
		tabId = item;
		contractTimer = setTimeout(DoExpandTab,3000);
		expandTimer = setTimeout(DoContractTab,8000);
		originalMoveDone = "true";
	}*/
}

//Function for first expansion - on page load
function DoExpandTab()
{
	clearInterval(tabTimer);
	tabTimer = setInterval(ExpandTab,20);
}

//Call this function after 5 seconds of opening
function DoContractTab()
{
	clearInterval(tabTimer);
	tabTimer = setInterval(ContractTab,20);
}

//Expand or contract the website trail tab
function MoveTab(action,item)
{	
	//Do this if the image alt tag says open tab'
	if(safeToMoveTab == "true")
	{
		clearInterval(tabTimer);
		safeToMoveTab = "false";
		tabId = item;
		
		if(action == "Open Tab")
		{
			tabTimer = setInterval(ExpandTab,20);
		}
		else if(action == "Close Tab")
		{
			tabTimer = setInterval(ContractTab,20);
		}
	}
}

//Expand the trail tab
function ExpandTab()
{
	var intMaxWidth = 450;
	
	if(intTabCounter > intMaxWidth)
	{
		document.getElementById(tabId).src = "/images/popout2.gif";
		document.getElementById(tabId).alt = "Close Tab";
		clearInterval(tabTimer)
		safeToMoveTab = "true";
		intIncrement = 22;
		//HighLightPage()
	}
	else
	{
		var intTabCounterNew
		document.getElementById("site_trail").style.width=intTabCounter+'px';
		intTabCounterNew = intTabCounter-16;
		if(intTabCounterNew == -1)
		{
			intTabCounterNew = 0;
		}
		document.getElementById(tabId).style.left = intTabCounterNew+'px';
		intTabCounter += intIncrement;
		if(intIncrement > 1)
		{
			intIncrement = (intMaxWidth - intTabCounterNew) / 20;
		}
		else
		{
			intIncrement = 1;
		}
	}	
}

//Contract the trail tab
function ContractTab()
{
	var intMinWidth = 18;
	
	if(intTabCounter < 18)
	{
		document.getElementById(tabId).src = "/images/popout.gif";
		document.getElementById(tabId).alt = "Open Tab";
		clearInterval(tabTimer)
		safeToMoveTab = "true";
		intIncrement = 22;
		//Tidy Up
		document.getElementById("site_trail").style.width = 16+'px';
		document.getElementById(tabId).style.left = 0+'px';
	}
	else
	{
		var intTabCounterNew
		document.getElementById("site_trail").style.width=intTabCounter+'px';
		intTabCounterNew = intTabCounter-16;
		document.getElementById(tabId).style.left = intTabCounterNew+'px';
		intTabCounter -= intIncrement;
		
		if(intIncrement > 1)
		{
			intIncrement = (intTabCounterNew - intMinWidth) / 20;
		}
		else
		{
			intIncrement = 1;
		}
	}	
}

//Roll Over
function DoRollOver(item,src)
{
	if(noMouseOverOn == item)
	{
		return;
	}
	document.getElementById(item).src = src;
}

/*//This function is loaded upon body load and it is used to load in the menu from the xml file
function LoadNavigation()
{
	//Get the navigation list
	menuItems = document.getElementById("subNavList").innerHTML;

	if (menuItems.length==0)
	{ 
		document.getElementById("subNavList").innerHTML="";
		return;
	}

	xmlHttp=GetXmlHttpObject()
		
	if (xmlHttp==null)
	{
		alert("Browser does not support HTTP Request");
		return;
	}

	var url="/getSubMenu.php";
	str="menuItems="+menuItems;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.onreadystatechange=StateChanged;
	xmlHttp.send(str);
}*/

//This function is loaded upon body load and it is used to load in the menu from the xml file
function LoadNavigationNew()
{
	//Get the navigation list
	menuItems = document.getElementById("subNavList").innerHTML;

	if (menuItems.length==0)
	{ 
		document.getElementById("subNavList").innerHTML="";
		return;
	}

	xmlHttp=GetXmlHttpObject()
		
	if (xmlHttp==null)
	{
		alert("Browser does not support HTTP Request");
		return;
	}
	
	var url="/getSubMenu.php";
	str="menuItems="+menuItems;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.onreadystatechange=StateChanged;
	xmlHttp.send(str);

	var item;
	item = document.getElementById("currentPage").innerHTML;
	
	noMouseOverOn = item;
	expand = "true";
	
	switch(item)
	{
		case "accomodations":
			document.getElementById(item).src= "/images/ers_menu_acc-over.gif";
			break;
		case "aboutt":
			document.getElementById(item).src= "/images/ers_menu_acc-over.gif";
			break;
		case "spa":
			document.getElementById(item).src= "/images/ers_menu_spa-over.gif";
			break;
		case "dining":
			document.getElementById(item).src= "/images/ers_menu_din-over.gif";
			break;
		case "shopping":
			document.getElementById(item).src= "/images/ers_menu_sho-over.gif";
			break;
		case "meetings":
			document.getElementById(item).src= "/images/ers_menu_mee-over.gif";
			break;
		case "reservations":
			document.getElementById(item).src= "/images/ers_menu_res-over.gif";
			break;
		default:
			break;
	}
}

//Expand the menu
function expandDown()
{
	var heightCurr;
	var heightNew;
	heightCurr = document.getElementById(itemId+"_subNavigation").offsetHeight;
	heightCurr = heightCurr - 10;
	heightNew = heightCurr + 5;
	document.getElementById(itemId+"_subNavigation").style.height = heightNew+"px"; 
	if(heightNew >= (currentHeight - 5))
	{
		clearInterval(time);
		time = setInterval(fadeInSubMenu,60);
	}
}

//Fade in the sub navigation after the slide down
function fadeInSubMenu()
{
	if(intCounter >= 95)
	{
		clearInterval(time)
		HighLightPage()
	}
	else
	{
		if (document.getElementById(itemId+"_subNavigation").filters)
		{
			document.getElementById(itemId+"_subNavigation").filters.alpha.opacity=intCounter
		}
		else
		{
			document.getElementById(itemId+"_subNavigation").style.opacity=intCounter/100
		}
		intCounter += 5		
	}	
}



//Highlight the current page menu item in the menu
/* function HighLightPage()
{
	xmlHttp=GetXmlHttpObject();
		
	if (xmlHttp==null)
	{
		alert("Browser does not support HTTP Request");
		return;
	}
	
	var currentUrl;
	var currentSubMenu;
	currentUrl = window.location.href;
	currentSubMenu = document.getElementById(itemId+"_subNavigation").innerHTML;
	currentSubMenu = currentSubMenu.toLowerCase();
	
	var url="/highLight.php";
	str  ="url="+currentUrl;
	str +="&subnav="+currentSubMenu;
	xmlHttp.onreadystatechange=highLightStateChanged;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send(str);
}
*/
function highLightStateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		document.getElementById(itemId+"_subNavigation").innerHTML = xmlHttp.responseText;
	} 	
}


//Temporary Show Sub Navigation depending on what page it sits
function ShowSubNav()
{
	var item;
	item = document.getElementById("currentPage").innerHTML;
	
	if(item != "donotexpand")
	{
		itemId = item;
		document.getElementById(item+"_subNavigation").innerHTML = document.getElementById(item+"_section").innerHTML;
		if(expand == "true")
		{
			document.getElementById(item+"_subNavigation").className = "shown";
			/*if (document.getElementById(item+"_subNavigation").filters)
			{
				document.getElementById(item+"_subNavigation").filters.alpha.opacity=0
			}
			else
			{
				document.getElementById(item+"_subNavigation").style.opacity=0
			}*/
			currentHeight = document.getElementById(item+"_subNavigation").offsetHeight;
			
			document.getElementById(item+"_subNavigation").style.height = "0px";
			//alert(currentHeight+" "+document.getElementById(item+"_subNavigation").style.height)
			clearInterval(time);
			time = setInterval(expandDown,60);
		}
		else
		{
			document.getElementById(item+"_subNavigation").className = "shown";
		}
		document.getElementById("menu").className = "visible";
	}
	else
	{
		document.getElementById("menu").className = "visible";
	}
}

//Get the menu from the php output file once the state is complete
function StateChanged() 
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		returnedMenu = xmlHttp.responseText;
		SetMenu();
	} 
}

//Inject menu into the page and set safe to load to true indicating it's safe to allow mouse over events
function SetMenu()
{
	document.getElementById("navigation").innerHTML=returnedMenu;
	ShowSubNav();
}

//indicate it's safe to allow mouse over events
function setSafeToLoad(itemSafe)
{
	safeToLoad = itemSafe;
}

//Show the sub navigation
function ShowMenu(item)
{	
	//show the menu if it's safe to do so
	if(safeToLoad == "true")
	{
		setSafeToLoad("false");
		
		if(currentOpenMenu != "")
		{
			CloseMenu(currentOpenMenu);
		}
		
		document.getElementById(item+"_subNavigation").innerHTML = document.getElementById(item+"_section").innerHTML;
		document.getElementById(item+"_subNavigation").className = "shown";
		currentOpenMenu = item;
		clearInterval(time)
		time = setTimeout(setSafe,100)
	}
	else
	{
		return;
	}
}

function setSafe()
{
	setSafeToLoad("true");	
}

function HideMenu(e,item)
{
	if(safeToLoad == "true")
	{
		if(!e)
		{
			var e = window.event
		}
			
		var relTarg = e.relatedTarget || e.toElement;
			
		relTarg = relTarg.parentNode
		relTarg = relTarg.parentNode
		relTarg = relTarg.parentNode
		relTarg = relTarg.nodeName

		if(relTarg != 'BODY')
		{
			return;
		}
		else
		{
			itemId = item
			document.getElementById(item+"_subNavigation").innerHTML = "";
			document.getElementById(item+"_subNavigation").className = "hidden";				
		}
	}
	else
	{
		return
	}
}

function LeaveSubNav(e,item)
{
	if(safeToLoad == "true")
	{
		if(!e)
		{
			var e = window.event
		}
			
		var relTarg = e.relatedTarget || e.toElement;
			
		relTarg = relTarg.parentNode
		relTarg = relTarg.parentNode
		relTarg = relTarg.parentNode
		relTarg = relTarg.nodeName
		
		if(relTarg != 'BODY')
		{
			return
		}
		else
		{
			itemId = item
			document.getElementById(item).innerHTML = "";
			document.getElementById(item).className = "hidden";				
		}
	}
	else
	{
		return
	}
}

function CloseMenu(item)
{
	document.getElementById(item+"_subNavigation").innerHTML = "";
	document.getElementById(item+"_subNavigation").className = "hidden";
	currentOpenMenu = "";	
}

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

//Submit the form function
function submitForm()
{
	//Initialize the variables
	returnValue  = true;
	reservation  = document.contactform.reservation.value
	firstName    = document.contactform.fname.value
	lastName     = document.contactform.lname.value
	emailAddress = document.contactform.email.value

	//Check to make sure that everything required was filled out
	if(reservation == "Please select...")
	{
		document.getElementById("hearAbout").innerHTML = '<span id="error"><b>Reservation inquiry regarding:*&nbsp;</b></span>'
		returnValue = false;
	}
	else
	{
		document.getElementById("hearAbout").innerHTML = '<b>Reservation inquiry regarding:&nbsp;</b>'
	}
	
	if(firstName == "")
	{
		document.getElementById("firstName").innerHTML = '<span id="error"><b>First Name *</b></span>'
		returnValue = false;
	}
	else
	{
		document.getElementById("firstName").innerHTML = '<b>First Name</b>'
	}
	
	if(lastName == "")
	{
		document.getElementById("lastName").innerHTML = '<span id="error"><b>Last Name *</b></span>'
		returnValue = false;
	}
	else
	{
		document.getElementById("lastName").innerHTML = '<b>Last Name</b>'
	}
	
	if(emailAddress == "")
	{
		document.getElementById("emailAddress").innerHTML = '<span id="error"><b>Email Address *</b></span>'
		returnValue = false;
	}
	else
	{
		document.getElementById("emailAddress").innerHTML = '<b>Email Address</b>'
	}
	
	if(returnValue == false)
	{
		alert("Please fill out all of the required fields before submitting this form.")
	}
	else if(returnValue == true)
	{
		xmlHttp=GetXmlHttpObject()
	
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
		
		var url="/submitForm.php"
		str="email="+emailAddress
		xmlHttp.onreadystatechange=formStateChanged
		xmlHttp.open("POST",url,true)
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.send(str)
	}
}

function formStateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		emailClean = xmlHttp.responseText

		if(emailClean == 'true')
		{
			submitCleanForm();
		}
		else
		{
			alert("Please enter a valid Email Address")
			document.getElementById("emailAddress").innerHTML = '<span id="error"><b>Email Address *</b></span>'			
		}
		emailClean = 'false'
	} 	
}

function submitCleanForm()
{
	//Initialize the variables
	returnValue  	= true;
	str = ""
	hearAbout    	= document.contactform.reservation.value
	firstName    	= document.contactform.fname.value
	lastName     	= document.contactform.lname.value
	address      	= document.contactform.address.value
	city         	= document.contactform.city.value
	state        	= document.contactform.state.value
	zip          	= document.contactform.zip.value
	emailAddress 	= document.contactform.email.value
	telephone    	= document.contactform.telephone.value
	fax      	 	= document.contactform.fax.value
	
	var url="/submitForm.php"
	str="emailClean="+'true'
	str=str+"&reservation="+hearAbout
	str=str+"&fname="+firstName
	str=str+"&lname="+lastName
	str=str+"&address="+address
	str=str+"&city="+city
	str=str+"&state="+state
	str=str+"&zip="+zip
	str=str+"&email="+emailAddress
	str=str+"&telephone="+telephone
	str=str+"&fax="+fax
	
	if(hearAbout == "General Inquiry")
	{
		//For default form
		from   		 	= document.contactform.fromday.value + " " + document.contactform.frommonth.value + " " + document.contactform.fromyear.value
		until   	 	= document.contactform.untilday.value + " " + document.contactform.untilmonth.value + " " + document.contactform.untilyear.value
		reason			= document.contactform.reasoninq.value
		
		//Default info
		str=str+"&from="+from
		str=str+"&until="+until
		str=str+"&reason="+reason
	}
	else if(hearAbout == "Wedding")
	{
		//For Weddings form
		weddate   		= document.contactform.wedday.value + " " + document.contactform.wedmonth.value + " " + document.contactform.wedyear.value
		ceremony   	 	= document.contactform.ceremony.value
		num_guests		= document.contactform.num_guests.value
		num_rooms		= document.contactform.num_rooms.value
		reh				= document.contactform.reh.value
		spa_res			= document.contactform.spa_res.value

		//Wedding info
		str=str+"&weddate="+weddate
		str=str+"&ceremony="+ceremony
		str=str+"&num_guests="+num_guests
		str=str+"&num_rooms="+num_rooms
		str=str+"&reh="+reh
		str=str+"&spa_res="+spa_res	
	}
	else if(hearAbout == "Conference")
	{
		//For Conference form
		prefarival   	= document.contactform.prefaday.value + " " + document.contactform.prefamonth.value + " " + document.contactform.prefayear.value
		prefdeparture  	= document.contactform.prefdday.value + " " + document.contactform.prefdmonth.value + " " + document.contactform.prefdyear.value
		altarival  		= document.contactform.altaday.value + " " + document.contactform.altamonth.value + " " + document.contactform.altayear.value
		altdeparture	= document.contactform.altdday.value + " " + document.contactform.altdmonth.value + " " + document.contactform.altdyear.value
		num_room		= document.contactform.num_room.value
		num_part		= document.contactform.num_part.value
		spa_res			= document.contactform.spa_res.value
		spec_event		= document.contactform.spec_event.value
		tour			= document.contactform.tour.value
		purpose			= document.contactform.purpose.value

		//Conference info
		str=str+"&prefarival="+prefarival
		str=str+"&prefdeparture="+prefdeparture
		str=str+"&altarival="+altarival
		str=str+"&altdeparture="+altdeparture
		str=str+"&num_room="+num_room
		str=str+"&num_part="+num_part
		str=str+"&spa_res="+spa_res
		str=str+"&spec_event="+spec_event
		str=str+"&tour="+tour
		str=str+"&purpose="+purpose		
	}
	
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}		
	
	xmlHttp.onreadystatechange=checkSentEmail
	xmlHttp.open("POST",url,true)
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send(str)
	currentHTML = '		<h1>Reservations</h1>'
	currentHTML = currentHTML+'			<p>'
	currentHTML = currentHTML+'		<div id="intro1">'
	currentHTML = currentHTML+'			Inner peace is just a call away. For reservations, please contact us at <span class="footernumber">(877) 688-2828</span>'
	currentHTML = currentHTML+'		</div>'
	currentHTML = currentHTML+'		<img src="/images/underline_shaded.png" width="660" height="12" class="introunderline"/>'
	currentHTML = currentHTML+'		<img src="/images/reservations_big.jpg" width="646" height="325" class="imgborder"/><br/>'
	currentHTML = currentHTML+'<h1>Contact</h1>'
	currentHTML = currentHTML+'<div id="form1">'
	currentHTML = currentHTML+'<p>Thank you for taking the time to submit this form. We will contact you shortly.</p>'
	currentHTML = currentHTML+'</div>'
	currentHTML = currentHTML+'</form>'
	document.getElementById("right_content").innerHTML = currentHTML
	
}

function checkSentEmail()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		/*var response
		response = xmlHttp.responseText
		alert(response)*/
	} 	
}

//This will repopulate the form with fields specific to the category
function RePopulateForm()
{
	var chosenCategory
	chosenCategory    = document.contactform.reservation.value

	switch(chosenCategory)
	{
		case "Wedding":
			document.getElementById("form_container").innerHTML = document.getElementById("wedding_form").innerHTML
			break;
		case "Conference":
			document.getElementById("form_container").innerHTML = document.getElementById("conference_form").innerHTML
			break;
		case "General Inquiry":
			document.getElementById("form_container").innerHTML = document.getElementById("default_form").innerHTML
			break;
		default:
			break;
	}
}
