

window.addEvent('domready', function() {

  // Setup the help icons:
  $$('a[class$=help_icon]').each(function(el,idx) {
    el.onclick = function() {
      if( this.parentNode.id + "" == "" )
        this.parentNode.id = "ID." + Math.random();
      var me = $(this.parentNode.id);
      
      this.parentNode.getElements('div[class$=help_content]').each(function(help) {
        help.style.display = help.style.display == "" ? "none" : help.style.display;
        help.style.display = help.style.display == "none" ? "block" : "none";
      });
      return false;
    };
  });
  
  $$('input[class$=url]').each(function(el,idx) {
    el.onfocus = function() {
      this.select();
    };
  });
  
  $('generator').onsubmit = validate_form;
  $('code').onfocus = function() { this.select(); };
  
//  showPreview();
});

function validate_form()
{
  var form = this;
  
  if( form.cbid.value.trim() == "" )
  {
    alert("ClickBank ID is Required");
    form.cbid.focus();
    return false;
  }// end if()
  
  if( form.cbid.value.trim().length < 5 || form.cbid.value.trim().length > 10 )
  {
    alert("ClickBank ID must be between 5 and 10 characters.");
    form.cbid.focus();
    return false;
  }// end if()
  
  if( form.search_type.selectedIndex == 0 )
  {
    alert("Please select a Search Type");
    form.search_type.focus();
    return false;
  }// end if()
  
  showPreview();
  
  // Finally:
  return false;
}// end validate_form()


function showPreview()
{
  var form = $('generator');
  
  var search_type = form.search_type.options[ form.search_type.selectedIndex ].value;
  if( search_type == "" )
    search_type = form.search_type.options[1].value;
  
  var code = $('code_template_' + search_type ).value.toString();
  code = code.replace("nickname", form.cbid.value.trim());
  code = code.replace("%tid%", form.tid.value.trim());
  $('code').value = code;
  
  $('preview_iframe').style.width = "100%";
  $('preview_iframe').style.height = "100%";
  $('submitter_form').code.value = code;
  $('submitter_form').submit();
}// end showPreview()



