function populate_date(month, day, year) {
  ge('date_month').value = month;
  ge('date_day').value = day;
  ge('date_year').value = year;
}

function ge(elem) {
  return document.getElementById(elem);
}

/*
 * Simple Ajax call method.
 *
 * From http://en.wikipedia.org/wiki/XMLHttpRequest
 */
function ajax(url, vars, callbackFunction) {
  var request =  new XMLHttpRequest();
  request.open("POST", url, true);
  request.setRequestHeader("Content-Type",
                           "application/x-www-form-urlencoded");

  request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status == 200) {
      if (request.responseText) {
        callbackFunction(request.responseText);
      }
    }
  };
  request.send(vars);
}

function select_town(town) {
	window.location.href = '/'+town+'/';
//	document.getElementById('town').value = town;
//	document.getElementById('cover_all').style.display = 'block';
//	document.getElementById('popup_container').style.display = 'block';
}

function check_form(form) {
	if (document.getElementById('emails').checked == true) {
		if (document.getElementById('name').value == "") {
			document.getElementById('name_label').className = 'error';
			document.getElementById('name_label').innerHTML = "Your name is required to receive emails.";
			return_false = true;
		}
		if (document.getElementById('email_add').value == "") {
			document.getElementById('email_label').className = 'error';
			document.getElementById('email_label').innerHTML = "Your email address is required to receive emails.";
			return_false = true;
			
		}
		
		if (return_false == true) {
			return false;
		} else {
			return true;
		}
	}
}

function checkLoggedIn() {
	FB.Connect.IfUserConnected(function() { loggedin = true; });
}

function close_popup() {
	$("#popup").fadeOut("fast");
	$("#cover_all").fadeOut("slow", function() {
		document.getElementById('cover_all').style.display = 'none';
		document.getElementById('popup_container').style.display = 'none';
		if (document.getElementById('comments')) {
			document.getElementById('comments').style.display = 'none';
			document.getElementById('popup3').style.display = 'none';
			document.getElementById('mistake').style.display = 'none';
			document.getElementById('map_canvas_large_container').style.display = 'none';
			document.getElementById('map_canvas_large').style.display = 'none';
		}
		document.getElementById('popup').style.height = '200px';
	});
}

function show_popup(html) {
	if (html == 'telephone') {
		html = "<h1 style='font-size: 70px; text-align: center;'>"+telephone+"</h1>";
		document.getElementById('popup3').style.display = 'block';

	} else if (html == 'register_to_rate') {
		html = "<h1>Connect to rate</h1><p>Sorry, but you need to connect with your facebook account to rate takeaways.<p><p>If you connect, we will also track your favourites and help you share your favourites with your friends!</p><a href='#' onclick='FB.Connect.requireSession(); return false;' ><img id='fb_login_image' src='http://static.ak.fbcdn.net/images/fbconnect/login-buttons/connect_light_medium_long.gif' alt='Connect'/></a>";
		document.getElementById('popup3').style.display = 'block';

	} else if (html == 'comments') {
		html = "";
		document.getElementById('popup').style.height = 'auto';
		document.getElementById('comments').style.display = 'block';
		document.getElementById('comments').style.height = 'auto';
		
	} else if (html == 'mistake') {
		html = "";
		document.getElementById('popup').style.height = 'auto';
		document.getElementById('mistake').style.display = 'block';
		document.getElementById('mistake').style.height = 'auto';
	
	} else if (html == 'why_connect') {
		html = "<h3>Why connect with facebook</h3>";
		html += "<p>Conencting with your facebook account gives you loads more features, here's a few of them</p>";
		html += "<ul><li>Remember your town and favourite takeaways each time you visit</li>";
		html += "<li>Rate and write reviews of your favourite takeaways</li>";
		html += "<li>See which of your friends use Feeling Hungry</li>";
		html += "<li>Share Feeling Hungry with your friends by posting to your wall</li></ul>";
		document.getElementById('popup3').style.display = 'block';
		
	} else if (html == 'map') {
		html = "";
		document.getElementById('map_canvas_large_container').style.display = 'block';
		document.getElementById('map_canvas_large').style.display = 'block';
		document.getElementById('popup').style.height = 'auto';
		var next = 'setup_map'
		
	} else if (html == 'town_map') {
		html = "";
		document.getElementById('town_map_container').style.display = 'block';
		document.getElementById('town_map_canvas').style.display = 'block';
		document.getElementById('popup').style.height = 'auto';
		
	} else {
		document.getElementById('popup').style.height = 'auto';
		document.getElementById('popup3').style.display = 'block';
	}
	document.getElementById('popup3').innerHTML = html;
	document.getElementById('popup_container').style.display = 'block';
	document.getElementById('cover_all').style.display = 'block';
	$("#popup").fadeIn("fast");
	$("#cover_all").fadeIn("slow");
	
	if (next == 'setup_map') { setup_large_map(); }
	scrollWin('#header');
//	document.getElementById('popup').scrollIntoView(true);
}

function scrollWin(elementName){
	$('html, body').animate({
	scrollTop: $(elementName).offset().top
	}, 1000);
}

function publish_story(story, rating) {
	if (story == 'competition') {
		var message = 'Check out this great competition!'; 
		var attachment = {'name':'I\'ve entered a competition to win an iPod!', 
		                  'href':' http://www.feelinghungry.co.uk/winanipod/',  
		                  'caption':'{*actor*} want you to also have a change of winning an iPod!', 
		                  'media':[{'type':'image','src':'http://www.feelinghungry.co.uk/images/facebook_feed.jpg','href':'http://www.feelinghungry.co.uk/'}]};
	var action_links = {'text':'Visit', 
	                    'href':' http://www.feelinghungry.co.uk/'};
	
	} else if (story == 'rating') {
		var message = 'Check out this great takeaway!'; 
		var attachment = {'name':'I\'ve given '+takeawayname+' a '+rating+' star rating!', 
		                  'href':' http://www.feelinghungry.co.uk/'+townname+'/'+takeawayurlname+'/',  
		                  'caption':'{*actor*} rated a great takeaway, '+takeawayname+', at FeelingHungry.co.uk \n Submit a menu and win an iPod!', 
		                  'media':[{'type':'image','src':'http://www.feelinghungry.co.uk/images/facebook_feed.jpg','href':'http://www.feelinghungry.co.uk/'}]};
	var action_links = {'text':'Visit', 
	                    'href':' http://www.feelinghungry.co.uk/'+townname+'/'};
	} else {
		var message = 'Check out this great takeaway!'; 
		var attachment = {'name':'I\'ve found my favourite takeaway menu!', 
		                  'href':' http://www.feelinghungry.co.uk/'+townname+'/'+takeawayurlname+'/',  
		                  'caption':'{*actor*} found a great takeaway, '+takeawayname+', at FeelingHungry.co.uk \n Submit a menu and win an iPod!', 
		                  'media':[{'type':'image','src':'http://www.feelinghungry.co.uk/images/facebook_feed.jpg','href':'http://www.feelinghungry.co.uk/'}]};
	var action_links = {'text':'Visit', 
	                    'href':' http://www.feelinghungry.co.uk/'+townname+'/'};
	}
	FB.Connect.streamPublish(message, attachment, action_links);
}