
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();
    };
  });
  
  // Setup the form:
  $('generator').onsubmit = validate_generator_form;
  $('generator').landing_page.onchange = function() {
    $('first_name_hint').innerHTML =
      $('last_name_hint').innerHTML = 
        $('state_hint').innerHTML = this.options[ this.selectedIndex ].value == "processing_name" ? "(Required)" : "(Optional)";
    $('email_hint').innerHTML =
      this.options[ this.selectedIndex ].value == "processing_email" ? "(Required)" : "(Optional)";
  };
});


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 = [ ];
  
  switch( form.landing_page.options[ form.landing_page.selectedIndex ].value ) {
    case "":
      args.push("efc=1");
      break;
    case "processing_name":
      if( form.first_name.value.trim() == "" )
      {
        alert("First Name is required");
        form.first_name.focus();
        return false;
      }// end if()
      
      if( form.last_name.value.trim() == "" )
      {
        alert("Last Name is required");
        form.last_name.focus();
        return false;
      }// end if()
      
      if( form.state.selectedIndex == 0 )
      {
        alert("State is required");
        form.state.focus();
        return false;
      }// end if()
      
      args.push("ef_searching=1");
      break;
    case "processing_email":
      if( form.email.value.trim() == "" )
      {
        alert("Email is required");
        form.email.focus();
        return false;
      }// end if()
      
      args.push("ef_searching=1");
      break;
    default:
      break;
  };
  
  if( form.first_name.value.trim() != "" )
    args.push( "firstname=" + form.first_name.value.trim() );
  
  if( form.last_name.value.trim() != "" )
    args.push( "lastname=" + form.last_name.value.trim() );
  
  if( form.state.selectedIndex > 0 )
    args.push( "location=" + form.state.options[ form.state.selectedIndex ].value );
  
  if( form.email.value.trim() != "" )
    args.push( "email=" + form.email.value.trim() );
  
  var url = "http://nickname.phonesrch.hop.clickbank.net/?".replace("nickname", form.cbid.value.trim());
  if( form.tid.value.trim().length > 0 )
    args.push( "tid=" + escape( form.tid.value.trim() ) );
  if( form.trackingType.selectedIndex > 0 )
  {
    var ppcvar = form.trackingType.options[ form.trackingType.selectedIndex ].value;
    args.push( ppcvar + "=" + escape( form.ppctid.value.trim() ) );
  }// end if()
  
  form.link.value = url + ( args.length > 0 ? args.join("&") : "" );

  return false;
}// end validate_generator_form( form )



