
var epets_xmlhttp;

var epets_fb_session = "";
var epets_fb_point_id = "";
var epets_fb_confirm_key = "";

function epets_fb_confirm()
{

	epets_xmlhttp = null;

	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		epets_xmlhttp = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		try {
			epets_xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){
			try{
				epets_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}
	else
		return false

	
	
	
	var currentTime = new Date();
	var now = String(currentTime.getHours())+String(currentTime.getMinutes())+String(currentTime.getSeconds());

	var url = "http://engineeringisfun.com.sg/epets/action-points-validate.php?session=" + epets_fb_session + "&id=" + epets_fb_point_id + "&key=" + epets_fb_confirm_key + "&timestamp=" + now
	//alert(url);
	
	epets_xmlhttp.onreadystatechange = epets_fb_state_change;
	epets_xmlhttp.open("GET", url, true);
	epets_xmlhttp.send(null);
	
}

function epets_fb_state_change()
{
	if (epets_xmlhttp.readyState==4)
	{// 4 = "loaded"
		if (epets_xmlhttp.status==200)
		{// 200 = "OK"
		   	formSend("success");
			alert("FB Sharing DONE: " + epets_xmlhttp.responseText);
		}
		else
		{
			formSend("fail");
			alert("FB Sharing Error: " + epets_xmlhttp.statusText);
		}
	}
}

function epets_fb_callback(post_id, exception) 
{
	//alert(globe_text);
	//alert(post_id + ":" + exception);

	if (post_id!="null") {
		if(exception!=null) {
			formSend("fail");
			alert("FB Sharing Error: " + exception);
		}
		else {
			//alert("OK!");
			epets_fb_confirm();
		}
	}
	else {
		if(exception!=null) {
			formSend("fail");
			alert("FB Sharing Error: " + exception);
		}
		else {
			formSend("fail");
			alert("FB Sharing Cancelled");
		}
	}
}

function epets_fb_share2(session, id, key)
{

	epets_fb_session = session;
	epets_fb_point_id = id;
	epets_fb_confirm_key = key; 
	
	FB.ensureInit(function () 
	{
	
		var attachment = 
		{
			'name' : 'Well Done!', 
			'description' : 'I\'ve got the correct answer! Join E.Pet today!',
			'media':[{'type':'image','src':'http://www.engineeringisfun.com.sg/images/FB.png','href':'http://www.engineeringisfun.com.sg'}]
		};
		var actionLinks = [{ "text": "Join E.Pet today!", "href": "http://www.engineeringisfun.com.sg"}];

		FB.Connect.streamPublish('', attachment, actionLinks, null, null, epets_fb_callback);
		
	});
	
}

function epets_fb_share3(session, id, key, message, description)
{

	epets_fb_session = session;
	epets_fb_point_id = id;
	epets_fb_confirm_key = key; 
	
	FB.ensureInit(function () 
	{
	
		var attachment = 
		{
			'name' : ""+message+"", 
			'description' : ""+description+"",
			'media':[{'type':'image','src':'http://www.engineeringisfun.com.sg/images/FB.png','href':'http://www.engineeringisfun.com.sg'}]
		};
		var actionLinks = [{ "text": "Join E.Pet today!", "href": "http://www.engineeringisfun.com.sg"}];

		FB.Connect.streamPublish('', attachment, actionLinks, null, null, epets_fb_callback);
		
	});
	
}