var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  http = new XMLHttpRequest();
}

function validate(user, uname, fb_id, mp3, publish_to_facebook, publish_to_twit, date_day, date_year, genre) {


FB.Facebook.init("8729b07bb0fc1d57424e70165a155def", "xd_receiver.php");
if(uname ==''){FB.Connect.requireSession(validate(user, 'guest', fb_id, mp3, publish_to_facebook, publish_to_twit, date_day, date_year, genre)); return false;}
 
if ((user !== '') && (user !== 'COMMENT TO UNLOCK THE NEXT TRACK!')){ 
  http.abort();
  http.open("GET", "validate.php?name=" + user + "&uname=" + uname + "&fb_id=" + fb_id + "&mp3=" + mp3 + "&publish_to_facebook=" + publish_to_facebook + "&publish_to_twit=" + publish_to_twit + "&date_day=" + date_day + "&date_year=" + date_year + "&genre=" + genre , true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      
      if (http.responseText != 'denied'){
        document.getElementById('locker').style.display='none';
        document.getElementById('unlocker').style.display='inline';
        document.getElementById('showcom').style.display = 'none';
        document.getElementById('posted').style.display='inline';
        document.getElementById('thank').style.display='inline';
        
        //Animation(document.getElementById('unlocker')).to('background', '#1f3549').from('background', '#8c7f4d').duration(6000).go();
        //Animation(document.getElementById('posted')).to('background', '#000000').from('background', '#34689a').duration(1500).go(); return false;
      }
      
      if (http.responseText == 'denied'){
      FB.Connect.showPermissionDialog("publish_stream", function(x){granted(x);});
      }
    }
  }
  
  http.send(null);
}
function granted(x){
            if (x =="publish_stream"){
                  http.abort();
  http.open("GET", "validate.php?name=" + user + "&uname=" + uname + "&fb_id=" + fb_id + "&mp3=" + mp3 + "&publish_to_facebook=" + publish_to_facebook + "&publish_to_twit=" + publish_to_twit + "&date_day=" + date_day + "&date_year=" + date_year + "&genre=" + genre, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      
      if (http.responseText != 'denied'){
        document.getElementById('locker').style.display='none';
        document.getElementById('unlocker').style.display='inline';
        document.getElementById('showcom').style.display = 'none';
        document.getElementById('posted').style.display='inline';
        document.getElementById('thank').style.display='inline';
        
        //Animation(document.getElementById('unlocker')).to('background', '#1f3549').from('background', '#8c7f4d').duration(6000).go();
        //Animation(document.getElementById('posted')).to('background', '#000000').from('background', '#34689a').duration(6000).go(); return false;
      }
      
      if (http.responseText == 'denied'){
      FB.Connect.showPermissionDialog("publish_stream", function(x){granted(x);});
//      FB.Connect.showPermissionDialog("offline_access", function(x){granted(x);});
      }
    }
  }

  http.send(null);
            }
          }
}

function rate(user, uname, fb_id, mp3, publish_to_facebook, publish_to_twit, genre) {
  
  if(uname ==''){FB.Connect.requireSession();}
  
http.abort();
http.open("GET", "rate.php?name=" + user + "&uname=" + uname + "&fb_id=" + fb_id + "&mp3=" + mp3 + "&publish_to_facebook=" + publish_to_facebook + "&publish_to_twit=" + publish_to_twit + "&genre=" + genre, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      
      if ((http.responseText != 'denied') && (http.responseText.indexOf('error') == -1)){
        document.getElementById('rate').innerHTML = http.responseText;
        if (document.getElementById('tracknum').value != 1){
          document.getElementById('locker').style.display='none';
          document.getElementById('unlocker').style.display='inline';
        }
        
        
        //Animation(document.getElementById('unlocker')).to('background', '#1f3549').from('background', '#8c7f4d').duration(6000).go();
        //Animation(document.getElementById('posted')).to('background', '#000000').from('background', '#34689a').duration(1500).go(); return false;
      }
      
      if (http.responseText.indexOf('error') != -1){document.getElementById('rate').innerHTML = "Error.<br>Please reload.";}
      
      if (http.responseText == 'denied'){
      FB.Connect.showPermissionDialog("publish_stream", function(x){granted2(x);});
      }
    }
  }
  http.send(null);
  
function granted2(x){
            if (x =="publish_stream"){
                  http.abort();
  http.open("GET", "rate.php?name=" + user + "&uname=" + uname + "&fb_id=" + fb_id + "&mp3=" + mp3 + "&publish_to_facebook=" + publish_to_facebook + "&publish_to_twit=" + publish_to_twit + "&genre=" + genre, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      
      if ((http.responseText != 'denied') && (http.responseText.indexOf('error') == -1)){
        document.getElementById('rate').innerHTML = http.responseText;
        if (document.getElementById('tracknum').value != 1){
          document.getElementById('locker').style.display='none';
          document.getElementById('unlocker').style.display='inline';
          }
        
        //Animation(document.getElementById('unlocker')).to('background', '#1f3549').from('background', '#8c7f4d').duration(6000).go();
        //Animation(document.getElementById('posted')).to('background', '#000000').from('background', '#34689a').duration(6000).go(); return false;
      }
      
      if (http.responseText.indexOf('error') != -1){document.getElementById('rate').innerHTML = "Error.<br>Please reload.";}
      
      if (http.responseText == 'denied'){
      FB.Connect.showPermissionDialog("publish_stream", function(x){granted2(x);});
      }
    }
  }

  http.send(null);
            }
          }
}