
var originalButtonsHTML;

function onLoad()
{
  originalButtonsHTML = document.getElementById('generated_buttons').innerHTML;
}

function validate()
{
  for (i = 0; i < urls.length; i++) {
    if (urls[i]) {
      window.open("http://feedvalidator.org/check.cgi?url=" + escape(urls[i]))
    }
  }
}

// FIXME: we can test more than this
function validateurls() {
 for (i=0; i < urls.length; i++) {
   if (urls[i].substring(0,7) != 'http://' &&
       urls[i].substring(0,8) != 'https://')
     return 'There was a problem with one or more of your urls: <br /><blockquote>' + urls[i] + '</blockquote>Please only submit http:// or https:// urls<br />';
 }
 return null;
}

function generateButtons()
{
  var buttonHTML = '';
  var i;
  var errorString = '';
  var urlInput;
  var subscriptionUrl = "http://subscribe.getMiro.com/?";
  
  // add new buttons here
  // add url to button img
  // (make sure to add comma to current last img)
  var buttons = new Array(
    'http://subscribe.getmiro.com/img/buttons/one-click-subscribe-88X34.png',
    'http://subscribe.getmiro.com/img/buttons/one-click-subscribe-109X34.png'
 );
  
  
  urlInput = document.getElementById('urls').value;

  if (urlInput == null || urlInput == '')
  {
    errorString = "You need to enter at least one valid URL.  Please try again in the above field.";
  }
  else
  {
    urlInput = urlInput.replace(/^\s+/g, '').replace(/\s+$/g,'');
    urls = urlInput.split(/\s+/);
    errorString = validateurls();
    for (i = 0, j = 1; i < urls.length; i++)
    {
      if (urls[i])
      {
	  	  if (j != 1)
		    {
		      subscriptionUrl += "&";
		    }
		  subscriptionUrl += "url" + j + "=" + escape(urls[i]);
		  j ++;
       }
     }
  }
  
  buttonHTML+='<form name="buttoncode"><div id="content-left">';
  for (i=0; i < buttons.length; i++)
  {
    buttonHTML += '<div class="button"><div class="image"><img src="' + buttons[i] +
      '" alt="" /></div><div class="code"><textarea name="btn' + i + 
      '" onClick="document.buttoncode.btn' + i + '.select();" cols="40" rows="4" style="background-color:#EEEEEE;"><a href="' + 
	subscriptionUrl + '" title="Miro: Internet TV"><img src="'+ buttons[i] + '" alt="Miro Video Player" border="0" /></a></textarea></div></div>';
    if (i == Math.floor((buttons.length - 1) / 2)) {
      buttonHTML+='</div><div id="content-right">';
    }
  }
  buttonHTML+='</div></form><br clear="all" />'; 
  
  if (errorString != null && errorString != '') {
    document.getElementById('generated_buttons').innerHTML = 
   '<p><strong>Error!</strong></p><p>' + errorString + '</p>';
  } else {
    document.getElementById('generated_buttons').innerHTML = originalButtonsHTML;
  
    document.getElementById('subscription_url_link').innerHTML = 
      '<a href="' + subscriptionUrl + '">' + subscriptionUrl + '</a>';
    document.getElementById('buttons').innerHTML = buttonHTML;
  }
  
  document.getElementById('generated_buttons').style.display = "block"
}