//function OnRequestPermission(){
//
//  var myPermissions = "publish_stream"; // permissions your app needs
//
//FB.Connect.showPermissionDialog(myPermissions , function(perms) {
//  if (!perms)
//  {
//    // handles if the user rejects the request for permissions. This is a good place to log off from Facebook connect
//  }
//  else
//  {
//      // finish up here if the user has accepted permission request
//      really_refresh();
//  };
//});
//}




function OnRequestPermission_no_refresh(fb_permission){

FB.Connect.requireSession(req_perms_no_refresh);

}

function OnRequestPermission_manage(fb_permission){

FB.Connect.requireSession(req_perms_manage);

}

function OnRequestPermission_business(fb_permission){

FB.Connect.requireSession(req_perms_business);

}

function OnRequestPermission_step_2(fb_permission){

FB.Connect.requireSession(req_perms_step_2);

}

function OnRequestPermission_list_item(fb_permission){

FB.Connect.requireSession(req_perms_list_item);

}

function req_perms_no_refresh() {

  var myPermissions = "publish_stream"; // permissions your app needs

  //check is user already granted for this permission or not
  FB.Facebook.apiClient.users_hasAppPermission(myPermissions,
  function(result) { // prompt offline permission if (result == 0)
  { // render the permission dialog
  if (result == 0) {
  FB.Connect.showPermissionDialog(myPermissions, go_get_fb_to_connect);
  } else {
  go_get_fb_to_connect();
  }
   } });

}

function req_perms_manage() {


  var myPermissions = "publish_stream"; // permissions your app needs

  //check is user already granted for this permission or not
  FB.Facebook.apiClient.users_hasAppPermission(myPermissions,
  function(result) { // prompt offline permission if (result == 0)
  { // render the permission dialog
  if (result == 0) {
  FB.Connect.showPermissionDialog(myPermissions, direct_to_posting_page_manage);
  } else {
    window.location = 'http://www.moombo.com/manage.php?facebook_connect=yes';
  }
   } });

}

function req_perms_business() {


  var myPermissions = "publish_stream"; // permissions your app needs

  //check is user already granted for this permission or not
  FB.Facebook.apiClient.users_hasAppPermission(myPermissions,
  function(result) { // prompt offline permission if (result == 0)
  { // render the permission dialog
  if (result == 0) {
  FB.Connect.showPermissionDialog(myPermissions, direct_to_posting_page_business);
  } else {
    window.location = 'http://www.moombo.com/business.php?facebook_connect=yes';
  }
   } });

}

function req_perms_step_2() {


  var myPermissions = "publish_stream"; // permissions your app needs

  //check is user already granted for this permission or not
  FB.Facebook.apiClient.users_hasAppPermission(myPermissions,
  function(result) { // prompt offline permission if (result == 0)
  { // render the permission dialog
  if (result == 0) {
  FB.Connect.showPermissionDialog(myPermissions, direct_to_posting_page_step2);
  } else {
    window.location = 'http://www.moombo.com/step2.php?facebook_connect=yes';
  }
   } });

}

function req_perms_list_item() {


  var myPermissions = "publish_stream"; // permissions your app needs

  //check is user already granted for this permission or not
  FB.Facebook.apiClient.users_hasAppPermission(myPermissions,
  function(result) { // prompt offline permission if (result == 0)
  { // render the permission dialog
  if (result == 0) {
  FB.Connect.showPermissionDialog(myPermissions, direct_to_posting_page);
  } else {
    window.location = 'http://www.moombo.com/account.php?page=list_an_item';
  }
   } });

}


function go_get_fb_to_connect() {
enable_facebook('get_new_box');
}



function OnRequestPermission(fb_permission){



FB.Connect.requireSession(req_perms);


}

function req_perms() {

  var myPermissions = "publish_stream"; // permissions your app needs

  //check is user already granted for this permission or not
  FB.Facebook.apiClient.users_hasAppPermission(myPermissions,
  function(result) { // prompt offline permission if (result == 0)
  { // render the permission dialog
  if (result == 0) {
  FB.Connect.showPermissionDialog(myPermissions, direct_to_account);
  } else {
  really_refresh();  
  }
   } });

}


function direct_to_account() {
window.location = 'http://www.moombo.com/account.php';
}

function direct_to_posting_page() {
window.location = 'http://www.moombo.com/account.php?page=list_an_item';
}

function direct_to_posting_page_step2() {
window.location = 'http://www.moombo.com/step2.php?facebook_connect=yes';
}

function direct_to_posting_page_manage() {
window.location = 'http://www.moombo.com/manage.php?facebook_connect=yes';
}

function direct_to_posting_page_business() {
window.location = 'http://www.moombo.com/business.php?facebook_connect=yes';
}


function login_complete(passed_value) {
 alert ('test');
}

function on_page_load(error_msg) {

error_msg = error_msg.replace(/_/gi, " ");
if(error_msg) { alert(error_msg); }

}

function refresh_page() {
         FB.Connect.showPermissionDialog('publish_stream', really_refresh());

}

function really_refresh() {
         var current_page = document.location.href;
         // if log out command exists, remove so user can log in directly from a page that includes this log out code
         current_page = current_page.replace("log_out=1", "");
         current_page = current_page.replace("other_window=1", "");
         window.location = current_page;
}

function use_moombo_account() {
         var current_page = document.location.href;
         document.getElementById('log_in_top_span').innerHTML = '<form name="login" method="post" action="javascript: moombo_login_verification();"><input name="username" type="text" class="login_field" value="email"> <input name="password" type="password" class="login_field" value="password"> <input name="login" type="submit" class="login_submit" value="Log In"></form>';
         document.getElementById('log_in_bottom_span').innerHTML = '<a href="new_account.php">Create an Account</a> | <a href="#" onclick="FB.Connect.requireSession(refresh_page); return_false;">Log in using your Facebook Account</a>';
}

function createRequestObject() {
    var tmpXmlHttpObject;
    
    //depending on what the browser supports, use the right way to create the XMLHttpRequest object
    if (window.XMLHttpRequest) {
        // Mozilla, Safari would use this method ...
        tmpXmlHttpObject = new XMLHttpRequest();
	
    } else if (window.ActiveXObject) { 
        // IE would use this method ...
        tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    return tmpXmlHttpObject;
}

//call the above function to create the XMLHttpRequest object
var http = createRequestObject();

function moombo_login_verification() {
         var moombo_username = document.login['username'].value;
         var moombo_password = document.login['password'].value;
         var current_page = document.location.pathname;

         var postdata = 'moombo_username=' + moombo_username + '&moombo_password=' + moombo_password + '&current_page=' + current_page;

         http.open('POST', 'moombo_login_verification.php', true);
         http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
         http.setRequestHeader('Content-length', postdata.length);
         http.setRequestHeader("Connection", "close");
         http.onreadystatechange = processResponse;
         http.send(postdata);

}

function processResponse(test) {
         if(http.readyState == 4){

         var response = http.responseText;
         window.location=response;

         }
}



function log_out_complete() {
         if(http.readyState == 4){

         var randomnumber=Math.floor(Math.random()*11)
         var new_url = document.location.href;
         var new_url_array = new_url.split('#');
         new_url = new_url_array[0];
         new_url = new_url + '?reload=' + randomnumber;

             // if it's a search page that's not the home page, get the data to return to same page
             if(location.pathname == '/search.php' && document.getElementById('placeholder_price')) {
             new_url = new_url + '#price=' + document.getElementById('placeholder_price').innerHTML + '&image=' + document.getElementById('placeholder_image').innerHTML + '&video=' + document.getElementById('placeholder_video').innerHTML + '&range=' + document.getElementById('placeholder_range').innerHTML + '&page=' + document.getElementById('placeholder_page').innerHTML + '&order=' + document.getElementById('placeholder_order').innerHTML;
             }

         window.location = new_url;

         }
}



function log_out() {

         var current_page = document.location.href;

         var postdata = 'current_page=' + current_page;

         http.open('POST', 'log_out.php', true);
         http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
         http.setRequestHeader('Content-length', postdata.length);
         http.setRequestHeader("Connection", "close");
         http.onreadystatechange = log_out_complete;
         http.send(postdata);


}

function log_out_both() {

         var current_page = document.location.pathname;

         var postdata = 'current_page=' + current_page;

         http.open('POST', 'log_out.php', true);
         http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
         http.setRequestHeader('Content-length', postdata.length);
         http.setRequestHeader("Connection", "close");
         http.onreadystatechange = processResponse;
         http.send(postdata);
}


function advanced_dow_perm() {

    FB.Connect.showPermissionDialog('publish_stream');
    }

