 	var YWSID = "lVRWLEwuzhHSMa44U_QPGg"; // common required parameter (api key)
		
/*
 * Construct the URL to call for the API request
 */
function constructYelpURL(locale,numBusiness,term) {
    var URL = "http://api.yelp.com/" +
        "business_review_search?"+
        "callback=" + "handleYelpResults" +
        "&term=" + term +           
        "&num_biz_requested=" + numBusiness +
        "&location=" + locale +
        "&ywsid=" + YWSID;
    return encodeURI(URL);
}
	
/*
 * Construct the URL to call for the API request
 */
function constructYelpPhoneURL(phoneNum) {
    var URL = "http://api.yelp.com/" +
        "phone_search?phone="+phoneNum+
        "&callback=" + "handleYelpPhoneResults" +
        "&ywsid=" + YWSID;
    return encodeURI(URL);
}
		
				    
 /*
  * Formats the categories HTML
  */
 function formatCategories(cats) {
     var s = 'Categories: ';
     for(var i=0; i<cats.length; i++) {
         s+= cats[i].name;
         if(i != cats.length-1) s += ', ';
     }
     s += '<br/>';
     return s;
 }

 /*
  * Formats the neighborhoods HTML
  */
 function formatNeighborhoods(neighborhoods) {
     s = 'Neighborhoods: ';
     for(var i=0; i<neighborhoods.length; i++) {
         s += '<a href="' + neighborhoods[i].url + '" target="_blank">' + neighborhoods[i].name + '</a>';
         if (i != neighborhoods.length-1) s += ', ';
     }
     s += '<br/>';
     return s;
 }
 
  /*
  * Formats the phone number HTML
  */
 function formatPhoneNumber(num) {
     if(num.length != 10) return '';
     return '(' + num.slice(0,3) + ') ' + num.slice(3,6) + '-' + num.slice(6,10) + '<br/>';
 }   
 
    
/*
 * If a sucessful API response is received, place
 * markers on the map.  If not, display an error.
 */
function handleYelpResults(data) {
    
    if(data.message.text == "OK") {
    	var text = "";
        if (data.businesses.length == 0) {
            text = "<div class=' padding5' style='margin-bottom:5px'><p />Sorry, no businesses were found in that category for this location.<p />Please try a different category.</div>";
            document.getElementById("yelpResults").innerHTML=text;
            return;
        }
        
        for(var i=0; i<data.businesses.length; i++) {
            biz = data.businesses[i];
	 text += '<div class=" padding5" style="margin-bottom:5px">';

      // image and rating
      text += '<table border="0" cellpadding="3"><tr><td valign="top">';
      text += '<img class="businessimage" src="'+biz.photo_url+'"/>';
		text += '</td>';
		text += '<td valign="top">';
      // div start
      text += '<div class="businessinfo">';
      // name/url
      text += '<a href="'+biz.url+'" target="_blank">'+biz.name+'</a><br/>';
      // stars
      text += '<img class="ratingsimage" src="'+biz.rating_img_url_small+'"/>&nbsp;based&nbsp;on&nbsp;';
      // reviews
      text += '<a href="'+biz.url+'" target="_blank">' + biz.review_count + '&nbsp;reviews</a><br/>';
     
      // neighborhoods
      if(biz.neighborhoods.length)
          text += formatNeighborhoods(biz.neighborhoods);
      // address
      text += biz.address1 + '<br/>';
      // address2
      if(biz.address2.length) 
          text += biz.address2+ '<br/>';
      // city, state and zip
      text += biz.city + ',&nbsp;' + biz.state + '&nbsp;' + biz.zip + '<br/>';
      // phone number
      if(biz.phone.length)
          text += formatPhoneNumber(biz.phone);
	// categories
      text += '<span class="smallText">'+formatCategories(biz.categories)+'</span>';				            
      // div end
      text += '</div></td></tr></table></div>';
      
        }
        document.getElementById("yelpResults").innerHTML=text;
    }
    else {
        alert("Error: " + data.message.text);
    }
} 

/*
 * If a sucessful API response is received, place
 * markers on the map.  If not, display an error.
 */
function handleYelpPhoneResults(data) {
    
    if(data.message.text == "OK") {
    	var text = "";
        if (data.businesses.length == 0) {           
            return;
        }
        
        for(var i=0; i<data.businesses.length; i++) {
            biz = data.businesses[i];
			text += '<div class="padding5" style="margin-bottom:5px">';

			// stars
	      	text += '<img class="ratingsimage" src="'+biz.rating_img_url_small+'"/>&nbsp;based&nbsp;on&nbsp;';
	      	// reviews
	      	text += '<a href="'+biz.url+'" target="_blank">' + biz.review_count + '&nbsp;reviews</a><br/>';
	      

	      text += '</div>';
      
        }
        document.getElementById("yelpResults").innerHTML=text;
    }
    else {
        alert("Error: " + data.message.text);
    }
} 


