function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
    //suppress error
   }
  }
 }
 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  return new XMLHttpRequest()
 else
  return false
}

// Login call from facebook
function ajax_fb_login() {

var fb_div=document.getElementById('fb_profile');
fb_div.innerHTML= "<fb:profile-pic class='fb_profile_pic_rendered FB_ElementReady' facebook-logo='true' size='square' uid='loggedinuser'></fb:profile-pic>";
FB.XFBML.Host.parseDomTree();  

FB_RequireFeatures(["Connect", "Api"], function(){ 
		FB.Facebook.init("dbe91729a28e20c139811e3b07356f84", "facebook-platform/xd_receiver.htm");
		//FB.Facebook.get_sessionState().waitUntilReady(function(){
		var api = FB.Facebook.apiClient;
		var myuid = api.get_session().uid;
	
		api.users_getInfo(myuid,['name'],function(result){
   			var username=result[0].name;
   			loggedin=1;
   			// Call the normal after login function to close the login box, update name, etc
			login_ok(username);
		});

		//});	
});


}


// Login via ajax
function ajax_login() {
document.getElementById("login_feedback").innerHTML="";
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;

var mypostrequest=new ajaxRequest()
mypostrequest.open("POST", "ajax/js_login.php", true);
mypostrequest.setRequestHeader('Content-Type',
  'application/x-www-form-urlencoded');
var encoded = "";
encoded = "username=" + escape(username) +
  "&password=" + escape(password) +
  "&submitted=1";
mypostrequest.setRequestHeader("Content-length", encoded.length);
//mypostrequest.setRequestHeader("Connection", "close");

mypostrequest.onreadystatechange=function(){

 if (mypostrequest.readyState==4){
  if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
  		// Output is either "Success" or an error message.
  		
		if(mypostrequest.responseText=="Success") {
			login_ok(username);
		} else {
   			document.getElementById("login_feedback").innerHTML=mypostrequest.responseText;
   		}
  }
  else {
  	alert("Oops!  An error has occurred trying to create the account.");
  }

 }
}

mypostrequest.send(encoded);

return false;
}

// Attempt to create a new account by sending ajax POST to php form which will create the account.
// Errors are returned as an associative array in JSON format.
function ajax_create_account() {

document.getElementById("username_comments").innerHTML="";
document.getElementById("password_comments").innerHTML="";
   		
var username=document.getElementById("create_username").value;
var password=document.getElementById("create_password").value;

var mypostrequest=new ajaxRequest()

mypostrequest.open("POST", "ajax/js_create_account.php", true);
mypostrequest.setRequestHeader('Content-Type',
  'application/x-www-form-urlencoded');
var encoded = "";
encoded = "create_username=" + escape(username) +
  "&create_password=" + escape(password) +
  "&submitted=1";
mypostrequest.setRequestHeader("Content-length", encoded.length);
mypostrequest.setRequestHeader("Connection", "close");


mypostrequest.onreadystatechange=function(){
 if (mypostrequest.readyState==4){
  if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
  		// the output is an array
  		// evaluates responsetext as raw javascript, forms new input_errors array from the php output.
		eval(mypostrequest.responseText);
   		document.getElementById("username_comments").innerHTML=input_errors.username;
   		document.getElementById("password_comments").innerHTML=input_errors.password;
   		
   		if(input_errors.errors=="0") {
   			//No Errors, login and drop the popup
			login_ok(username);
   			
   		}
  }
  else {
  	alert("Oops!  An error has occurred trying to create the account.");
  }

 }
}

mypostrequest.send(encoded);

return false;
}

// Check if username is available by an ajax request to php function which checks mysql and return 1 or 0
function check_available()
{

var username=document.getElementById("create_username").value;
document.getElementById("username_comments").innerHTML="";
var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatechange=function(){
 if (mygetrequest.readyState==4){
  if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
   //document.getElementById("result").innerHTML=mygetrequest.responseText
   if(mygetrequest.responseText==0) {
   		document.getElementById("username_comments").innerHTML="Sorry that username is not available.";
   	} else if(mygetrequest.responseText==1)  {
   		document.getElementById("username_comments").innerHTML="That username is available!";
    } else {
    	document.getElementById("username_comments").innerHTML="Sorry an error has occurred checking for that...";
    }
  }
  else{
   alert("An error has occured making the request")
  }
 }
}

mygetrequest.open("GET", "ajax/check_available.php?username="+username, true)
mygetrequest.send(null)

}

function login_ok(username) {
// Either login, or create new account was successful.  We are now logged in
// Just clean up the housekeeping stuff.

   		close_login();
   		document.getElementById("login_link").innerHTML=username + " <a href=\"logout.php\" onClick=\"js_logout();return false;\">(logout)</a>";
   		var caption = document.getElementById("comment_form_caption")
		if(caption) {
			caption.innerHTML=caption.innerHTML+" - " + username;
		}
   		logged_in=1;
}

function js_logout() {

var mygetrequest=new ajaxRequest();

mygetrequest.open("GET", "ajax/js_logout.php", true);

mygetrequest.onreadystatechange=function(){
 if (mygetrequest.readyState==4){
  if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
   //Should check for valid logout
   document.getElementById("login_link").innerHTML = "<a href=\"login.php\" onClick=\"login_prompt();return false;\">login</a>";

		FB_RequireFeatures(["XFBML"], function(){
					FB.Connect.logout();
					var fb_div=document.getElementById('fb_profile');
		 			fb_div.innerHTML= "";
		});

   		//FB.init({ apiKey: 'dbe91729a28e20c139811e3b07356f84' });
   		//FB.logout(handleSessionResponse);
// 		FB.logout(function(response) {
// 			// Everything here runs after FB logs out.
// 			alert(response);
// 			var fb_div=document.getElementById('fb_profile');
// 			fb_div.innerHTML= "";
// 			alert('fb logged out');
// 		});
   

  }
  else{
   alert("An error has occured making the request")
  }
 }
}


mygetrequest.send(null);


}

function handleSessionResponse(response) {
alert('FB logged lout');
alert(response);

}

function completed_logout() {

alert('done?');
}
