/* finish.js */

function onFlashDone(tab)
{
    if (tab.toLowerCase() == "bebo") {
        showBebo(1);
    }
    else {
        showBebo(0);
    }
    recordAdoptionCompletion(tab, "copy");
};

function showBebo(panelIndex)
{
  var panelIds = [ "gobebo", "beboquickhelp", "gobebo2" ];
  //  hide the other one
  var e = document.getElementById(panelIds[1 - panelIndex]);
  if (e) {
    e.style.display = "none";
  }
  e = document.getElementById(panelIds[panelIndex]);
  if (e) {
    e.style.display = "block";
    e = document.getElementById('scrollpoint');
    bytefx.scroll(e, 5);
  }
};

function closeBebo(divId)
{
  e = document.getElementById(divId);
  if (e) {
    e.style.display = "none";
  }
};


function onPostProfileHandler(data)
{
    showBebo(2);
    recordAdoptionCompletion(data.network, "gigya");
};

function onPostProfileFromFlashHandler(network)
{
    showBebo(2);
    recordAdoptionCompletion(network, "gigya");
};

function recordAdoptionCompletion(network, meth)
{
    //  track this as a goal completion
    pageTracker._trackPageview('/completion/' + encodeURIComponent(network));
    //alert('recordAdoptionCompletion ' + network);
    //  we need to get the pet params from the query string, whee
    var animal = null;
    var query = window.location.search;
    if (!query.length)
        return;
    //alert('query = ' + query);
    var parmPairs = query.substring(1).split('&');
    //  search for animal... usually at the end, so go backwards
    for (var i=parmPairs.length-1; i>=0; --i) {
        var nameValue = parmPairs[i].split('=');
        //alert("nameValue[0] = " + nameValue[0]);
        if (nameValue[0]=='mc') {
            animal = decodeURIComponent(nameValue[1]);
            //  strip off .extension if there
            animal = (animal.split('.'))[0];
            break;
        }
    }
    if (!animal)
        return;

    var xhr = null;
    if (window.XMLHttpRequest) {
        xhr = new window.XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject('Microsoft.XMLHTTP');
    }
    if (!xhr)
        return;
    //alert('ready to go');
    xhr.open('GET', 'recordcompletion.php?mc=' + encodeURIComponent(animal)
                    + '&net='+encodeURIComponent(network)
                    + '&meth='+encodeURIComponent(meth));
    xhr.send(null);
    //  that's it, no need to wait for return value.
}
