/* 

A few Take Out Scout functions
Don't shoot me
questions?  john@takeoutscout.com

*/

var xmlHttp;
var xmlHttpMin;

var affectedRow = "f";
var affectedRow2;


if (self.location != top.location) {
	document.write('<p>Reloading mtWeb in top frame in 3 seconds...</p>');
	setTimeout('top.location.href = self.location.href', 3000);
}

function validateString(str, warning, min, max) {
	if (!min) { min = 1; }
	if (!max) {	max = 65535; }
	if (!str.value || str.value.length < min || str.value.length > max) {
		alert(unescape(warning));
		str.focus();
		str.select();
		return false;
	}
	return true;
}

/*
	loadMap() sets up a new Google Map
*/

function loadMap() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"+affectedRow));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    geocoder = new GClientGeocoder();
		map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
  }
}

/*
  Loads a map and shows an address on it
*/

function loadFullMap(address){
  loadMap();
  showAddress(address);
}

/*
	showAddress shows displays a restaurant on a google map
*/

function showAddress(address) {
   //alert(address);

  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          map.setCenter(point, 13);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          //marker.openInfoWindowHtml(address);
        }
      }
    );
  }
}
	 
/*
	getData will populate a cell on menuSearch.php with restaurant data 
	from the database via 1337 ajax functionality
*/
function getData( menuid, tablerow, address, name ){

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 } 
 var url="ajaxmenu.php";
 url=url+"?menu="+menuid;
 url=url+"&pos="+tablerow;
 
 xmlHttp.onreadystatechange=populateData;
 affectedRow = tablerow;
 myAddress = address;
 
 document.getElementById("h"+affectedRow).innerHTML = "<a style = 'text-decoration: none' href = 'javascript:minData("+tablerow+","+menuid+")'><img src = 'images/minus.jpg' alt = 'Minimize' >"+name+"</a>";
 
 
 
 xmlHttp.open("GET",url,true);
 xmlHttp.send(null);
}

/*
	populateData() writes content to the cell
	once it is loaded by getData
*/
function populateData() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 	 
		document.getElementById("e"+affectedRow).innerHTML= xmlHttp.responseText;
		loadMap();
		showAddress(myAddress);
 	} 
} 

/*
	minData will minimize the extra data that the ajax function getData populates
	a menusearch.php cell with
*/

function minData(tablerow,menuid){

  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null)
   {
   alert ("Browser does not support HTTP Request")
   return
   } 
   var url="mindata.php";
   url=url+"?menu="+menuid;
   url=url+"&pos="+tablerow;
   
   xmlHttp.onreadystatechange=minimize;
   affectedRow = tablerow;

   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
}

function minimize(){
  GUnload();
	document.getElementById("h"+affectedRow).innerHTML=xmlHttp.responseText;
  document.getElementById("e"+affectedRow).innerHTML="";
}

/*
	addFavorite adds a favorite menu
*/
function favorite(menuid, tablerow, action){
favorite=GetXmlHttpObject()
if (favorite==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 } 
 var url="favorite.php";
 url=url+"?menuid="+menuid;
 url=url+"&action="+action;
 
 favoritePos = tablerow;
 
 favorite.onreadystatechange=changeFavorite 
 favorite.open("GET",url,true)
 favorite.send(null)
}

/*
	changeFavorite() will update the text "favorite this restaurant" 
	once the ajax script is complete
*/

function changeFavorite() 
{ 
	if (favorite.readyState==4 || favorite.readyState=="complete")
	{ 	 
		document.getElementById("fav"+favoritePos).innerHTML= favorite.responseText;
 	} 
} 

/*
	The Ajax Meaty Goodness
*/

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

/*
	Function popUp makes a popup!
*/
function popUp(URL, pWidth, pHeight) {
  		day = new Date();
  		id = day.getTime();
  		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=" + pWidth +  ",height=" + pHeight + "');");
  }

/*
	sfHover is used by the suckerfish dropdown menu
*/
	
sfHover = function() {
	var sfEls = document.getElementById("userPanel").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
	