var map;
var geocoder;
var cityZoom = 11;
var startZoom = 13;
var defaultZoom = 3;
var defaultAddress = "United States";
var waypointNumber = 0;

var returnHotelNum = 10;

var markerHotels=[];
var thePoint;
var test;
var arrHotels=[]; //array of hotel objects
var arrInterestPoints=[]; //array of point of interest objects

var ids = []
var hotels = []
var addresses = []
var cities = []
var states = []
var countries = []
var zips = []

var num;
var pagenum;

//var hotelDisplayFlag = document.getElementById("hotel");
//hotelDisplayFlag.style.visibility = "hidden";


var deselectCurrent = function() {};

function getState()
{
	stateFormValue = stateFormF.replace("+"," ");
	return stateFormValue;
}

function getCity()
{
	cityFormValue = cityFormF.replace("+"," ");
	return cityFormValue;
}

function init()
{
	if (GBrowserIsCompatible())
	{
		
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		geocoder = new GClientGeocoder();
		showDefault(defaultAddress);
	}
}

function showDefault(address) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
		// Create our "tiny" marker icon
		var tinyIcon = new GIcon();
		tinyIcon.image = "/icons/building.png";
		tinyIcon.shadow = "/icons/shadow.png";
		//tinyIcon.iconSize = new GSize(12, 20);
		//tinyIcon.shadowSize = new GSize(22, 20);
		tinyIcon.iconAnchor = new GPoint(6, 20);
		tinyIcon.infoWindowAnchor = new GPoint(6, 1);
		tinyIcon.infoShadowAnchor = new GPoint(13, 13);

		// Set up our GMarkerOptions object literal
		markerOptions = { icon:tinyIcon };
		
        map.setCenter(point, defaultZoom);
		var marker = new GMarker(point, markerOptions);
        map.addOverlay(marker);
		marker.openInfoWindowHtml("<p align='center'><b><font size='5' color='#99b3cc'>Welcome To MapMyCollege!</font></b><br><br><b>Find a college by selecting the City and State below.<br><br>Request application information from any college for FREE!.</b></p>");
		GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml("<p align='center'><b><font size='5' color='#99b3cc'>Welcome To MapMyCollege!</font></b><br><br><b>Find a college by selecting the City and State below.<br><br>Request application information from any college for FREE!.</b></p>");
		});
		//var infoTabs = [
    	//new GInfoWindowTab("Welcome!", "<p align='center'><b><font size='4'>Welcome To CollegeMap!</font></b><br><br>Find a college by selecting the City and State below.<br><br>Your results will display to the right.</p>"),
    	//new GInfoWindowTab('<div style="width:300px">How Does It Work?</div>', "test")
    	//];
    	//GEvent.addListener(marker, "click", function() {
    	//marker.openInfoWindowTabsHtml(infoTabs);
    	//});
		//marker.openInfoWindowTabsHtml(infoTabs);
      }
    }
  );
}

function showCity(address,city,state) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, cityZoom);
		/*
		var listItem = document.createElement('li');
		var listItemLink = listItem.appendChild(document.createElement('p'));
		listItemLink.innerHTML = "<a href=''>Start A New Search</a>";
				
		document.getElementById('citylocation-list').appendChild(listItem);
		*/
		//var listItem = document.createElement('li');
		//var listItemLink = listItem.appendChild(document.createElement('a'));
		//listItemLink.innerHTML = "Start A New Search";
		//listItemLink.href = "";
		
		//document.getElementById('citylocation-list').appendChild(listItem);
		//var goBack = document.getElementById("citylocation");
		//goBack.firstChild.nodeValue = "Start A New Search";
      }
    }
  );
}

function showAddressFocus(address) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
		// Create our "tiny" marker icon
		var tinyIcon = new GIcon();
		tinyIcon.image = "/icons/building.png";
		tinyIcon.shadow = "/icons/shadow.png";
		//tinyIcon.iconSize = new GSize(12, 20);
		//tinyIcon.shadowSize = new GSize(22, 20);
		tinyIcon.iconAnchor = new GPoint(6, 20);
		tinyIcon.infoWindowAnchor = new GPoint(6, 1);
		tinyIcon.infoShadowAnchor = new GPoint(13, 13);

		// Set up our GMarkerOptions object literal
		markerOptions = { icon:tinyIcon };
        map.setCenter(point, startZoom);
        var marker = new GMarker(point,markerOptions);
        //GEvent.addListener(marker, "click", function() {
        //marker.openInfoWindowTabsHtml(infoTabs);
        //});
        map.addOverlay(marker);
      }
    }
  );
}

function trim(s)
{
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
}

/**************************************
// This function singles out a hotel that was picked and displays the hotel information
**************************************/
function removeHotels(num) {
	map.closeInfoWindow();
	for (var i=0;i<markerHotels.length;i++) {
	  if(num != i){
         map.removeOverlay(markerHotels[i]);
	  }else{
		 //markerHotels[i] = thePoint;
	  	 map.setCenter(arrHotels[num].point, cityZoom);
	  }
    }
	
	var infoTabs = [
    new GInfoWindowTab("Details", arrHotels[num].name),
    new GInfoWindowTab("Information", arrHotels[num].name)
    ];
    GEvent.addListener(arrHotels[num].marker, "click", function() {
    arrHotels[num].marker.openInfoWindowTabsHtml(infoTabs);
    });
	
	var focusPoint = function() {
			deselectCurrent();
			listItem.className = 'current';
			deselectCurrent = function() { listItem.className = ''; }
			arrHotels[num].marker.openInfoWindowHtml(arrHotels[num].name + "<br>" + arrHotels[num].address);
			return false;
	}
	
	document.getElementById("sidebar-list").innerHTML="";
	
	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));
	listItemLink.href = "#";
	listItemLink.innerHTML = '<strong>' + arrHotels[num].name + '</strong>';
		
	GEvent.addListener(arrHotels[num].marker, 'click', focusPoint);	
	listItemLink.onclick = focusPoint;
	
	document.getElementById('sidebar-list').appendChild(listItem);
	
	getHotel(arrHotels[num].id);
}

function focusInfo (marker){
	var num;
	//alert(marker);
	for(var i=0;i<=arrHotels.length;i++){
		if(arrHotels[i].marker == marker){
			//marker found
			num = arrHotels[i].num;
			i = arrHotels.length;
			deselectCurrent();
			//listItem.className = 'current';
			//deselectCurrent = function() { listItem.className = ''; }
			arrHotels[num].marker.openInfoWindowHtml(arrHotels[num].name + "<br>" + arrHotels[num].address);
			return false;
		}
	}
}

String.prototype.toCapitalCase = function() {
var re = /\s/;
var words = this.split(re);
re = /(\S)(\S+)/;
for (i = words.length - 1; i >= 0; i--) {
re.exec(words[i]);
words[i] = RegExp.$1.toUpperCase()
+ RegExp.$2.toLowerCase();
}
return words.join(' ');
}

function showAddress(address,name,num,id) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        //alert(address + " not found");
      } else {
		hotelNumber = num + 1;
		
		var listItem = document.createElement('li');
		var listItemLink = listItem.appendChild(document.createElement('a'));
		listItemLink.href = "#";
		listItemLink.innerHTML = '<strong>' + name + '</strong>';
		
		//var listItemS = document.createElement('li');
		//var listItemLinkS = listItemS.appendChild(document.createElement('a'));
		//listItemLinkS.href = "javascript:removeHotels(" + num + ")";
		//listItemLinkS.innerHTML = '<strong>Select This Hotel</strong>';
		
		var infoTabs = [
    	new GInfoWindowTab("Details", name),
    	new GInfoWindowTab("Information", address)
   		];
		// Create our "tiny" marker icon
		var tinyIcon = new GIcon();
		tinyIcon.image = "/icons/building.png";
		tinyIcon.shadow = "/icons/shadow.png";
		//tinyIcon.iconSize = new GSize(12, 20);
		//tinyIcon.shadowSize = new GSize(22, 20);
		tinyIcon.iconAnchor = new GPoint(6, 20);
		tinyIcon.infoWindowAnchor = new GPoint(6, 1);
		tinyIcon.infoShadowAnchor = new GPoint(13, 13);

		// Set up our GMarkerOptions object literal
		markerOptions = { icon:tinyIcon };
        var marker = new GMarker(point,markerOptions);
        //GEvent.addListener(marker, "click", function() {
        // marker.openInfoWindowTabsHtml(infoTabs);
        // });
		
		var focusPoint = function() {
			var infoTabs = [
			new GInfoWindowTab("Request", "<form method='post' action='/request.php' name='thisform' onSubmit='return checkForm();'><input name='school_id' type='hidden' value='" + arrHotels[num].id + "'><input name='affiliate' type='hidden' value='mapmycollege'><input name='school' type='hidden' value='alemap'><table width='500px'><tr><td colspan='2' class='default-text' style='padding-bottom: 8px;'>Information Request</b>:</td></tr><tr><td width='50%' class='default-text'>First Name: <input name='first_name' type='text' value='' size='20'></td><td width='50%' class='default-text'>Last Name: <input name='last_name' type='text' value='' size='20'></td></tr><tr><td width='50%' class='default-text'>Home Phone: <input name='home_phone' type='text' value='' size='20'></td><td width='50%' class='default-text'>Email: <input name='email' type='text' value='' size='20'></td></tr><tr><td width='50%' class='default-text'>Address: <input name='address1' type='text' value='' size='20'></td><td width='50%' class='default-text'>City: <input name='city' type='text' value='' size='20'></td></tr><tr><td width='50%' class='default-text'>State: <SELECT name='state'><option value='' selected='selected'></option><option value='AL'>Alabama</option><option value='AK'>Alaska</option><option value='AZ'>Arizona</option><option value='AR'>Arkansas</option><option value='CA'>California</option><option value='CO'>Colorado</option><option value='CT'>Connecticut</option><option value='DE'>Delaware</option><option value='DC'>District of Columbia</option><option value='FL'>Florida</option><option value='GA'>Georgia</option><option value='HI'>Hawaii</option><option value='ID'>Idaho</option><option value='IL'>Illinois</option><option value='IN'>Indiana</option><option value='IA'>Iowa</option><option value='KS'>Kansas</option><option value='KY'>Kentucky</option><option value='LA'>Louisiana</option><option value='ME'>Maine</option><option value='MD'>Maryland</option><option value='MA'>Massachusetts</option><option value='MI'>Michigan</option><option value='MN'>Minnesota</option><option value='MS'>Mississippi</option><option value='MO'>Missouri</option><option value='MT'>Montana</option><option value='NE'>Nebraska</option><option value='NV'>Nevada</option><option value='NH'>New Hampshire</option><option value='NJ'>New Jersey</option><option value='NM'>New Mexico</option><option value='NY'>New York</option><option value='NC'>North Carolina</option><option value='ND'>North Dakota</option><option value='OH'>Ohia</option><option value='OK'>Oklahoma</option><option value='OR'>Oregon</option><option value='PA'>Pennsylvania</option><option value='RI'>Rhode Island</option><option value='SC'>South Carolina</option><option value='SD'>South Dakota</option><option value='TN'>Tennessee</option><option value='TX'>Texas</option><option value='UT'>Utah</option><option value='VT'>Vermont</option><option value='VA'>Virginia</option><option value='WA'>Washington</option><option value='WV'>West Virginia</option><option value='WI'>Wisconsin</option><option value='WY'>Wyoming</option></select></td><td width='50%' class='default-text'>Zip: <input name='zipcode' type='text' value='' size='20'></td></tr><tr><td colspan='2' class='default-text'>High School Graduation Year: <select name='hs_grad_year'><option value ='1955'>1955</option><option value ='1956'>1956</option><option value ='1957'>1957</option><option value ='1958'>1958</option><option value ='1959'>1959</option><option value ='1960'>1960</option><option value ='1961'>1961</option><option value ='1962'>1962</option><option value ='1963'>1963</option><option value ='1964'>1964</option><option value ='1965'>1965</option><option value ='1966'>1966</option><option value ='1967'>1967</option><option value ='1968'>1968</option><option value ='1969'>1969</option><option value ='1970'>1970</option><option value ='1971'>1971</option><option value ='1972'>1972</option><option value ='1973'>1973</option><option value ='1974'>1974</option><option value ='1975'>1975</option><option value ='1976'>1976</option><option value ='1977'>1977</option><option value ='1978'>1978</option><option value ='1979'>1979</option><option value ='1980'>1980</option><option value ='1981'>1981</option><option value ='1982'>1982</option><option value ='1983'>1983</option><option value ='1984'>1984</option><option value ='1985'>1985</option><option value ='1986'>1986</option><option value ='1987'>1987</option><option value ='1988'>1988</option><option value ='1989'>1989</option><option value ='1990'>1990</option><option value ='1991'>1991</option><option value ='1992'>1992</option><option value ='1993'>1993</option><option value ='1994'>1994</option><option value ='1995'>1995</option><option value ='1996'>1996</option><option value ='1997'>1997</option><option value ='1998'>1998</option><option value ='1999'>1999</option><option value ='2000' selected>2000</option><option value ='2001'>2001</option><option value ='2002'>2002</option><option value ='2003'>2003</option><option value ='2004'>2004</option><option value ='2005'>2005</option><option value ='2006'>2006</option><option value ='2007'>2007</option><option value ='2008'>2008</option><option value ='2009'>2009</option><option value ='2010'>2010</option></select></td></tr><tr><td colspan='2' class='default-text'>I agree to be contacted by an education advisor: <select name='accept_contact'><option value ='Yes'>Yes</option><option value ='No' selected>No</option></select></td></tr><tr><td colspan='2' class='default-text'><input type='submit' value='Submit' name='submit'/></td></tr></table></form>"),
    		new GInfoWindowTab("More Details", "<div class='college'><strong>" + arrHotels[num].name + "</strong><br>" + arrHotels[num].address.toCapitalCase() + "<br><br><a href='http://collegematchingservice.com/colleges/" + arrHotels[num].name.replace(" ","-") + "/" + arrHotels[num].id + ".html' target='_blank'>View our detailed listing at:</a><br><a href='http://collegematchingservice.com/colleges/" + arrHotels[num].name.replace(" ","-") + "/" + arrHotels[num].id + ".html' target='_blank'><img src='http://collegematchingservice.com/degrees/images/logo.gif' border='0'></a></div>"),
			new GInfoWindowTab("Scholarships", "<center><div style=\"width: 225px;\"><form action=\"http://collegematchingservice.com/scholarships/search.php\" method=\"GET\" target=\"_blank\"><div style=\"padding: 3px; background-color: #73a3e3; border: 1px solid #333333;\"><div style=\"border: 1px dashed #999999; padding: 8px 0px 8px 0px; text-align: center; background-color: #ffffff;\"><p style=\"margin: 0px;\"><span style=\"font: 18px Lucida; color: #333333;\"><strong>Free <a href=\"http://collegematchingservice.com/scholarships/\" target=\"_blank\" style=\"font: 18px Lucida; color: #333333; font-weight: bold; text-decoration: none;\">Scholarships</a> Search</strong></p><p style=\"margin: 0px; padding: 2px 0px 2px 0px;\"><input type=\"text\" name=\"term\" size=\"20\" value=\"Technology\" style=\"border: 1px solid #333333; padding: 4px;\" onClick=\"this.value=''\"> <input type=\"submit\" value=\"Go!\" style=\"border: 1px solid #333333; font-weight: bold; background-color: #b7eb63; padding: 3px;\"></p></div></div><p style=\"margin: 0px; padding-top: 3px; text-align: center;\"><span style=\"font: 11px Lucida; color: #333333;\">powered by <a href=\"http://collegematchingservice.com\" target=\"_blank\" style=\"font: 11px Lucida; color: #333333; text-decoration: none;\">CollegeMatchingService.com</a></span></p></form></div></center>")
    		];
			deselectCurrent();
			listItem.className = 'current';
			deselectCurrent = function() { listItem.className = ''; }
			//arrHotels[num].marker.openInfoWindowHtml("<div class='college'><strong>" + arrHotels[num].name + "</strong><br>" + arrHotels[num].address + "<br><br><a href='http://collegematchingservice.com' target='_blank'>Request information from this college!</a></div>");
			arrHotels[num].marker.openInfoWindowTabsHtml(infoTabs);
			return false;
		}
		
		document.getElementById('sidebar-list').appendChild(listItem);
		//document.getElementById('sidebar-list').appendChild(listItemS);
		
		arrHotels[num] = new theHotel(name, address, marker, hotelNumber, id, point);
		markerHotels.push(marker);
		
        map.addOverlay(marker);
		//GEvent.addListener(marker, 'click', focusPoint);
		GEvent.addListener(arrHotels[num].marker, 'click', focusPoint);
		listItemLink.onclick = focusPoint;
      }
    }
  );
}

//Eventually I need to make the info windows use this function, currently they use their own function embedded in another function.
function focusMarkerInfo(num) {
	deselectCurrent();
	listItem.className = 'current';
	deselectCurrent = function() { listItem.className = ''; }
	arrHotels[num].marker.openInfoWindowHtml(arrHotels[num].name + "<br>" + arrHotels[num].address + "<br><a href=''>Book A Room!</a>");
	return false;
}

function showWaypoint(address,name,id,tags,description,icon,private) { //name, address, marker, id, tags, description, icon, private
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        //alert(address + " not found");
      } else {
		var infoTabs = [
    	new GInfoWindowTab("Details", name),
    	new GInfoWindowTab("Information", address)
   		];
        
		var Icon = new GIcon();
		Icon.image = "/images/blue-dot.png";
		Icon.shadow = "/images/mm_20_shadow.png";
		Icon.iconSize = new GSize(12, 20);
		Icon.shadowSize = new GSize(22, 20);
		Icon.iconAnchor = new GPoint(6, 20);
		Icon.infoWindowAnchor = new GPoint(6, 1);
		Icon.infoShadowAnchor = new GPoint(13, 13); 

        var marker = new GMarker(point, Icon);
        GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowTabsHtml(infoTabs);
        });
		
		var listItem = document.createElement('li');
		var listItemLink = listItem.appendChild(document.createElement('a'));
		listItemLink.href = "#";
		listItemLink.innerHTML = '<strong>' + name + '</strong>';

		var hotelDetail = name + "</br>";
		hotelDetail += address;
		hotelDetail += "<p>" + description + "</p>";
		
		var focusPoint = function() {
			deselectCurrent();
			listItem.className = 'current';
			deselectCurrent = function() { listItem.className = ''; }
			marker.openInfoWindowHtml(hotelDetail);
			return false;
		}
		
		GEvent.addListener(marker, 'click', focusPoint);	
		listItemLink.onclick = focusPoint;
		
		//document.getElementById('location-list').appendChild(listItem);
		arrInterestPoints[id] = new theInterestPoints(name, address, marker, id, tags, description, icon, private); //name, address, marker, id, tags, description, icon, private
        map.addOverlay(marker);
      }
    }
  );
}

function theHotel( name, address, marker, num, id, point) {  
    this.name = name;  
    this.address = address;  
    this.marker = marker;
	this.num = num;
	this.id = id;
	this.point = point;
}

function theInterestPoints( name, address, marker, id, tags, description, icon, private) {  
    this.name = name;  
    this.address = address;  
    this.marker = marker;
	this.id = id;
	this.tags = tags;
	this.description = description;
	this.icon = icon;
	this.private = private;
}

function deleteNode(element){
  var label = document.getElementById(element);	
  while( label.hasChildNodes() ) { label.removeChild( label.lastChild ); }
}

function listHotels(start){
	pagenum = (start - 1);
	deleteNode('sidebar-list');
	pagination(num);
	var i = (start - 1) * 10;
	var end = returnHotelNum + i;
	while(i < end) {
		id = ids[i].childNodes[0].nodeValue;
		hotel = hotels[i].childNodes[0].nodeValue;
		address = addresses[i].childNodes[0].nodeValue;
		city = cities[i].childNodes[0].nodeValue;
		state = states[i].childNodes[0].nodeValue;
		country = countries[i].childNodes[0].nodeValue;
		zip = zips[i].childNodes[0].nodeValue;
		if(zip == 00000)
			var addressFinal = address+ ", " +city+ ", " +state+ ", " +country;
		else
			var addressFinal = address+ ", " +city+ ", " +state+ ", " +zip+ ", " +country;
	
		//alert(hotel);
		showAddress(addressFinal,hotel,i,id);
		i++;
	}
}

function pagination(total){
	var pages = Math.round((total / returnHotelNum));
	var start = (pagenum * returnHotelNum) + 1;
	var thePage = pagenum + 1;
	if(thePage == pages)
		var end = total;
	else
		var end = (pagenum * returnHotelNum) + returnHotelNum;
	var sum = document.getElementById('summary');
	
	var html = "Showing " + start + " to " + end + " of " + total + " colleges. Page(s) ";
	for(var i=1;i<=pages;i++){
		if(i == (pagenum + 1))
			html += " " + i + " ";
		else
			html += " [<a href='javascript:listHotels(" + i + ")'>" + i + "</a>] ";
	}
	sum.innerHTML = html;
}

function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	deleteNode('sidebar-list');
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser does not support this website!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
		
				//Update the div in the html with the returned query
				var ajaxDisplay = document.getElementById('ajaxDiv');
				//ajaxDisplay.innerHTML = ajaxRequest.responseText;
				//alert(ajaxRequest.responseText);
			
				//The following allows a string to be parsed as an xml object
				//The cross-browser compatibility needs to be checked here
				function parseXML(val) {
					if (document.implementation && document.implementation.createDocument) {
						xmlDoc = new DOMParser().parseFromString(val, 'text/xml');
					}
					else if (window.ActiveXObject) {
						xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
						xmlDoc.loadXML(val);
					}
					else
					{
						alert('Your browser can\'t handle this script');
						return null;
					}
				
					return xmlDoc;
				}
				//Now parse the readied xml string
				var xmlDoc;
				xmlDoc = parseXML(ajaxRequest.responseText);
				ids = xmlDoc.getElementsByTagName('id');
				hotels = xmlDoc.getElementsByTagName('name');
				addresses = xmlDoc.getElementsByTagName('address');
				cities = xmlDoc.getElementsByTagName('city');
				states = xmlDoc.getElementsByTagName('state');
				countries = xmlDoc.getElementsByTagName('country');
				zips = xmlDoc.getElementsByTagName('zip');
			
				//alert(ajaxRequest.responseText);
				num = hotels.length;
				var i = 0;
				pagenum = 0;
				var j = 0;
				var end = returnHotelNum + i;
				while(i < end) {
					var id = ids[i].childNodes[0].nodeValue;
					var hotel = hotels[i].childNodes[0].nodeValue;
					var address = addresses[i].childNodes[0].nodeValue;
					var city = cities[i].childNodes[0].nodeValue;
					var state = states[i].childNodes[0].nodeValue;
					var country = countries[i].childNodes[0].nodeValue;
					var zip = zips[i].childNodes[0].nodeValue;
					if(zip == 00000)
						var addressFinal = address+ ", " +city+ ", " +state+ ", " +country;
					else
						var addressFinal = address+ ", " +city+ ", " +state+ ", " +zip+ ", " +country;
				
					//alert(hotel);
					showAddress(addressFinal,hotel,i,id);
					i++;
					j++;
				}
				summary.style.visibility = "visible";
				pagination(num);
		}
	}
	
	//Send the request to the php file to return an xml string of information, which will be
	//converted into an xml object to be parsed
	var cityForm = document.getElementById('city').value;
	var stateForm = document.getElementById('state').value;
	var zipForm = document.getElementById('zip').value;
	
	//var waypointFormFlag = document.getElementById("formWaypoint");
	//waypointFormFlag.style.visibility = "visible";
	
	//var attractionsFlag = document.getElementById("attractions");
	//attractionsFlag.style.visibility = "visible";
	
	var hotelFormFlag = document.getElementById("form");
	//hotelFormFlag.style.visibility = "hidden";
	
	var introFlag = document.getElementById("intro");
	introFlag.style.visibility = "hidden";
	
	var popularFlag = document.getElementById("popular");
	popularFlag.style.visibility = "hidden";
	
	reg = /\s+/;
	
	cityFormF = trim(cityForm);
	cityFormF = cityForm.toLowerCase();
	cityFormF = cityForm.replace(reg,"+");
	
	stateFormF = trim(stateForm);
	stateFormF = stateForm.toLowerCase();
	stateFormF = stateForm.replace(reg,"+");
	
	stateFormWaypoint = stateForm.replace(/\+/gi," ");
	
	//document.myWaypointForm.waypointCity.value=cityForm;
	//document.myWaypointForm.waypointState.value=stateFormWaypoint;
	
	zipFormF = trim(zipForm);
	
	var queryString = "?city=" + cityFormF + "&state=" + stateFormF + "&zip=" + zipFormF;
	var finalAddress = cityForm + ", " +stateForm+ " " +zipForm;
	showCity(finalAddress,cityForm,stateForm);
	ajaxRequest.open("GET", "get_hotel_list.php" + queryString, true);
	ajaxRequest.send(null);
}

function getHotel(id){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser does not support this website!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
				//Update the div in the html with the returned query
				//var ajaxDisplay = document.getElementById('ajaxDiv');
				//ajaxDisplay.innerHTML = ajaxRequest.responseText;
				//alert(ajaxRequest.responseText);
			
				//The following allows a string to be parsed as an xml object
				//The cross-browser compatibility needs to be checked here
				function parseXML(val) {
					if (document.implementation && document.implementation.createDocument) {
						xmlDoc = new DOMParser().parseFromString(val, 'text/xml');
					}
					else if (window.ActiveXObject) {
						xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
						xmlDoc.loadXML(val);
					}
					else
					{
						alert('Your browser can\'t handle this script');
						return null;
					}
				
					return xmlDoc;
				}
				//Now parse the readied xml string
				var xmlDoc;
				var text = ajaxRequest.responseText;
				var newNeedle = "";
				
				//Loop the replace to remove <br>'s from descriptions.
				var finalHTML;
				finalHTML = text;
				var loopCount = 5;
				var j = 0;
				while (j < loopCount){
					finalHTML = finalHTML.replace(/<br>/i,newNeedle);
					j++;
				}
				xmlDoc = parseXML(finalHTML);
				var ids = xmlDoc.getElementsByTagName('id');
				var names = xmlDoc.getElementsByTagName('name');
				var addresses = xmlDoc.getElementsByTagName('address');
				var cities = xmlDoc.getElementsByTagName('city');
				var states = xmlDoc.getElementsByTagName('state');
				var countries = xmlDoc.getElementsByTagName('country');
				var descriptions = xmlDoc.getElementsByTagName('description');
				var lowrates = xmlDoc.getElementsByTagName('lowrate');
				var zips = xmlDoc.getElementsByTagName('zip');
				
				//alert(ajaxRequest.responseText);
				var id = ids[0].childNodes[0].nodeValue;
				var name = names[0].childNodes[0].nodeValue;
				var address = addresses[0].childNodes[0].nodeValue;
				var city = cities[0].childNodes[0].nodeValue;
				var state = states[0].childNodes[0].nodeValue;
				var country = countries[0].childNodes[0].nodeValue;
				var lowrate = lowrates[0].childNodes[0].nodeValue;				
				var zip = zips[0].childNodes[0].nodeValue;
				var description = descriptions[0].childNodes[0].nodeValue;
	
				//hotelDisplayFlag.style.visibility = "visible";
				
				document.showHotel = eval('document.getElementById("hotel")');
				
				document.showHotel.style.visibility = "visible";
				
				var hotelDetail = address + "<br>";
				hotelDetail += city + ", " + state + " " + country + "<br>";
				hotelDetail += zip + "<br>";
				hotelDetail += "<a href='http://travel.ian.com/index.jsp?pageName=hotInfo&cid=139972&hotelID=" + id + "&city=" + city + "&stateProvince=" + state + "&country=" + country + "&hotel=1'>Book A Room</a> - ";
				hotelDetail += "<a href='/hotels/hotel-details/" + id + "'>View Hotel Details</a>";
				
				
				document.getElementById("hotel").innerHTML=hotelDetail;
				
				//listItemLink.onclick = focusPoint;
				
				//document.getElementById('hotel-list').appendChild(listItem);
		}
	}
	
	var queryString = "?id=" + id;
	ajaxRequest.open("GET", "get_hotel.php" + queryString, true);
	ajaxRequest.send(null);
}

function ajaxFunctionWaypoint(){
	var privateWaypointFormCheck;
	var addressWaypointForm = trim(document.getElementById('waypointAddress').value);
	var nameWaypointForm = trim(document.getElementById('waypointName').value);
	var descriptionWaypointForm = trim(document.getElementById('waypointDescription').value);
	//var tagsWaypointForm = trim(document.getElementById('waypointTags').value);
	var tagsWaypointForm = null;
	//var iconWaypointForm = trim(document.getElementById('waypointType').value);
	var iconWaypointForm = null;
	//var privateWaypointFormCheck = trim(document.getElementById('waypointPrivate').value);
	var privateWaypointFormCheck = null;
	var cityWaypointForm = trim(document.getElementById('waypointCity').value);
	var stateWaypointForm = trim(document.getElementById('waypointState').value);
	var cityValue = getCity(); //Value of the city from the hotel search
	var stateValue = getState(); //Value of the state from the hotel search
	
	if(privateWaypointFormCheck == 1)
		privateWaypointForm = 1;
	else
		privateWaypointForm = 0;
	
	var finalAddress = addressWaypointForm + ", " + cityWaypointForm + ", " + stateWaypointForm;

	showWaypoint(finalAddress,nameWaypointForm,waypointNumber,tagsWaypointForm,descriptionWaypointForm,iconWaypointForm,privateWaypointForm); //name, address, marker, id, tags, description, icon, private
	waypointNumber = waypointNumber + 1;
}

window.onload = init;
window.onunload = GUnload;
