function setBookmark(url,str)
{
  if(str=='') str=url;
  if (document.all) window.external.AddFavorite(url,str);
  else alert('Press CTRL and D to add a bookmark to:\n"'+url+'".');
}

function random_int() 
{
 return Math.floor(Math.random()*100000);
}

var cc;
var name;
var rand;
var tmout = 0;
var pin = 0;

function submitBlogFormAndRefresh(divid, uri, form, timeInMS)
{
  //alert('1');
  cc = $('countryCode').value;
  name = $('name').value;
  rand = $('rand').value;
  if(cc != 'US')
  {
    window.location.href = "/blog_voice/blog_voice.php?cc="+cc;
    return;
  }
  if(tmout)
  {
    clearTimeout(tmout);
    tmout = 0;
  }

  var response = submit_form(uri, form);
  if (response) 
  {
    if(response.match('resubmit')) 
    {
      alert(response);
      window.location.reload(true);
    }  
    else if(response.match('Info missing')) 
    {
      alert(response);
    }        
    else if (response.match('SUCCESS')) 
    {
      //alert(response);
      if (response.match(/<strong>(\d+)<\/strong>/)) 
      {
          pin = parseInt(RegExp.$1);
          //alert(pin);
      }
      tmout = setTimeout('pollResponse("'+divid+'")', 15000);
      $(divid).innerHTML = response;
    } 
    else if (response.match('COMPLETED')) 
    {
      //alert(response);
      $(divid).innerHTML = response;
      $('subscribeInvite').style.display='block';
      $('morelinks').style.display='block';
    }
    else if (response.match('Error')) 
    {
      alert(response);
      $('subscribeInvite').style.display='block';
      $('morelinks').style.display='block';
    }      
    else
    {
      pollResponse(divid);
    }       
  }
}

var failureCount = 0;
var totalRequests = 0;

function pollResponse(divId) 
{
  if(++totalRequests > 200)
  {
    alert('Request timed out... Please try again');
    window.location.reload(true); 
    return;
  }
  new Ajax.Request('./my_voice.php',
    {
      method:'get',
      parameters: { pin:pin, countryCode: cc, rand:rand, random: random_int() },
      onSuccess: function(transport)
      {
        var response = transport.responseText || "no response text";
        //alert(response);
        if (response.match('WAITING')) 
        {
          //alert("We are waiting for you to call");
          tmout = setTimeout('pollResponse("'+divId+'")', 7000);
        }         
        else if (response.match('RECORDING')) 
        {
          tmout = setTimeout('pollResponse("'+divId+'")', 3000);
          $(divId).innerHTML = response;
        }
        else if (response.match('resubmit')) 
        {
          alert(response);
          window.location.reload(true);
        }        
        else if (response.match('SUCCESS')) 
        {
          tmout = setTimeout('pollResponse("'+divId+'")', 10000);
          $(divId).innerHTML = response;
        }
        else if (response.match('COMPLETED')) 
        {
          $(divId).innerHTML = response;
          $('subscribeInvite').style.display='block';
          $('morelinks').style.display='block';
        } 
        else if (response.match('Error')) 
        {
          alert(response);
          $('subscribeInvite').style.display='block';
          $('morelinks').style.display='block';
        }         
        else
        {
          alert('Some problem occured... Please try again');
          window.location.reload(true);          
        } 
      },
      onFailure: function()
      { 
        if(++failureCount>3)
        {
          alert('Operation Failure, Some problem occured... Please try again');
          window.location.reload(true);
        }
        else
        {
          tmout = setTimeout('pollResponse("'+divId+'")', 2000);
        }
      }
    });
}

function setSkin(soundFile, imgFile, x, y, ptxt)
{
  var str = getPlayer1Obj(soundFile, imgFile, x, y);
  $('flashTextContainer').value = str + ptxt;
  $('flashDisplay').innerHTML = str;
}


function getPlayer1Obj(soundFile, imgFile, x, y)
{
  serverUrl = "http://top-callingcards.com";
  soundFile = serverUrl + "/blog_voice/playFile.php?filename="+soundFile;
  playerUrl = serverUrl + "/blog_voice/player/mp3-player.swf";
  homeURL = serverUrl + "/blog_voice/my_voice.php";
  img = serverUrl + imgFile;
  var str = " <object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"300\" height=\"200\" align=\"middle\"> " +
     "<param name=\"allowScriptAccess\" value=\"sameDomain\" /> " +
     "<param name=\"movie\" value=\""+playerUrl+"?backgroundImage="+img+"&introLink="+soundFile+"&musicXPosition="+x+"&musicYPosition="+y+"&homeURL="+homeURL+"\"/> " +
     "<param name=\"quality\" value=\"high\" /> " +
     "<param name=\"wmode\" value=\"transparent\" /> " +
     "<param name=\"bgcolor\" value=\"#F7C15C\" /> " +
     "<embed src=\""+playerUrl+"?backgroundImage="+img+"&introLink="+soundFile+"&musicXPosition="+x+"&musicYPosition="+y+"&homeURL="+homeURL+"\" width=\"300\" height=\"200\" ></embed> " +
  "</object> ";

return str;
}
