
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;
    };
  });
  
  $$('tr[class$=odd]').each(function(el,idx) {
    el.getElements('td')[1].style.width = "220px";
    try {
      el.getElements('td')[2].style.width = "150px";
      el.getElements('td')[3].style.width = "150px";
    }
    catch(e) {
      // Do nothing:
    };
  });
  
  // Setup the form:
  $('generator').onsubmit = validate_generator_form;
  $('generator').onreset = function() {
    $('download_instructions').style.display = "none";
    $('click_instruction').style.display = "inline";
    $('please_wait').style.display = "none";
    return true;
  };
});


function validate_generator_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("ClickBankID must be between 5 and 10 characters.");
    form.cbid.focus();
    return false;
  }// end if()
  
  if( form.trackingType.selectedIndex > 0 && form.ppctid.value.trim() == "" )
  {
    alert("Please enter a PPC Tracking ID.");
    form.ppctid.focus();
    return false;
  }// end if()
  
  var args = ["cbid=" + form.cbid.value.trim() ];
  if( form.tid.value.trim().length > 0 )
    args.push( "tid=" + escape( form.tid.value.trim() ) );
  args.push( "trackingType=" + form.trackingType.options[ form.trackingType.selectedIndex ].value );
  args.push( "ppctid=" + form.ppctid.value.trim() );
  var ppcvar = form.trackingType.options[ form.trackingType.selectedIndex ].value;
  args.push( ppcvar + "=" + escape( form.ppctid.value.trim() ) );
  
  window.setTimeout(function() {
    $('please_wait').style.display = "none";
    $('download_instructions').style.display = "inline";
    $('download_link').onclick = function() {
      document.location = "/handlers/psa.email_finder.landing_page_generator.default?" + args.join("&") + "&r=" + Math.random();
      return false;
    };
  }, 750);
  $('download_instructions').style.display = "none";
  $('click_instruction').style.display = "none";
  $('please_wait').style.display = "inline";

  return false;
}// end validate_generator_form( form )



