function validate_twicture_url (template_id, feed_url, width, height) {
  if (!template_id.match(/^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}$/))
    template_id = null;

  if ((width = parseInt(width)) && width < 50)
    width = null;

  if ((height = parseInt(height)) && height < 50)
    height = null;

  if (!feed_url.match(/^https?\:\/\//))
    feed_url = 'http://' + feed_url;

  if (!(feed_url.match(/^https?\:\/\/(www\.)?twitter\.com\/statuses\/user_timeline\/d{8}\.rss$/) ||
        feed_url.match(/^http\:\/\/search\.twitter\.com\/search\.(atom|rss)\?q=.*$/) ||
        feed_url.match(/^https?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}(\/.*)?$/)))
    feed_url = null;

  return { template: template_id, width: width, height: height, url: feed_url };
}

function generator (template_input_id, feed_input_id, output_id, width_input_id, height_input_id) {
  var template_id = document.getElementById(template_input_id);

    if (template_id.value.length == 0) {
      elements = document.getElementsByName(template_input_id);
      if (elements)
        for(i = 0; i < elements.length; i++)
          if (elements[i].checked)
            template_id = elements[i];
    }

  var feed_url = document.getElementById(feed_input_id);
  var output = document.getElementById(output_id);

  var width = null;
  var width_value = ''
  if (width_input_id) {
    width = document.getElementById(width_input_id);
    if (width)
      width_value = width.value
  }

  var height = null;
  var height_value = ''
  if (height_input_id) {
    height = document.getElementById(height_input_id);
    if (height)
      height_value = height.value
  }

  if (!(template_id && feed_url && output)) return false; 

  var result = validate_twicture_url(template_id.value, feed_url.value, width_value, height_value);
  var has_error = false;

  output_value = 'http://tw.zetaprints.com/';
  if (template_id.value)
    if (result.template) {
      output_value += result.template;
      template_id.className = template_id.className.replace(/\berror\b/g, '');
    }
    else {
      if (!template_id.className)
        template_id.className = 'error';
      else if (template_id.className.indexOf('error') == -1)
        template_id.className += ' error';

      has_error = true;
    }
  else
    has_error = true;

  var width_value = '';
  if (width_input_id) {
    var width = document.getElementById(width_input_id);
    if (width)
      if (width.value)
        if (result.width) {
          width_value = result.width;
          width.className = width.className.replace(/\berror\b/g, '');
        }
      else
        if (!width.className)
          width.className = 'error';
        else if (width.className.indexOf('error') == -1)
          width.className += ' error';
  }

  var height_value = '';
  if (height_input_id) {
    var height = document.getElementById(height_input_id);
    if (height)
      if (height.value)
        if (result.height) {
          height_value = result.height;
          height.className = height.className.replace(/\berror\b/g, '');
        }
      else
        if (!height.className)
          height.className = 'error';
        else if (height.className.indexOf('error') == -1)
          height.className += ' error';
  }

  if (width_value || height_value)
    output_value += '/' + width_value + 'x' + height_value;

  if (feed_url.value)
    if (result.url) {
      output_value += '/?' + result.url;
      feed_url.className = feed_url.className.replace(/\berror\b/g, '');
    }
    else {
      if (!feed_url.className)
        feed_url.className = 'error';
      else if (feed_url.className.indexOf('error') == -1)
        feed_url.className += ' error';

      has_error = true;
    }
  else
    has_error = true;

  if (!has_error) {
    output.value = output_value;
    return true;
  }

  output.value = '';
  return false;
};

function deselect_template (template_input_id) {
  elements = document.getElementsByName(template_input_id);
  if (elements)
    for(i = 0; i < elements.length; i++)
      if (elements[i].checked)
        elements[i].checked = false;
}

function gen_with_clean (template_input_id, feed_input_id, output_id, width_input_id, height_input_id) {
  document.getElementById(template_input_id).value = '';
  return generator(template_input_id, feed_input_id, output_id, width_input_id, height_input_id);
}

