var $j = jQuery.noConflict();

$j(document).ready(function() { 
    $j('#resource_form_wrapper').css('display', 'block');
    $j('#form_request').attr('action', '/web2lead'); 
    $j('#mail_list_wrapper').css('display', 'block');
    $j('#form_mail_list').attr('action', '/web2lead'); 
});
    
function SubmitResourceForm() {
    return TestRequiredFields();
}

// This function validates an email address
function TestRequiredFields_Email() {
  var str = document.form_request.email.value;
  var atSym = str.lastIndexOf("@");
  if (atSym < 1) { return false; } // no local-part
  if (atSym == str.length - 1) { return false; } // no domain
  if (atSym > 64) { return false; } // there may only be 64 octets in the local-part
  if (str.length - atSym > 255) { return false; } // there may only be 255 octets in the domain

  // Is the domain plausible?
  var lastDot = str.lastIndexOf(".");
  // Check if it is a dot-atom such as example.com
  if (lastDot > atSym + 1 && lastDot < str.length - 1) { return true; }
  //  Check if could be a domain-literal.
  if (str.charAt(atSym + 1) == '[' &&  str.charAt(str.length - 1) == ']') { return true; }
  return false;
}

// This function validates any of our forms
function TestRequiredFields() {
  if (document.getElementById("first_name")) {
    if (document.getElementById("first_name").value.length < 1) {
      alert("Please enter your first name.");
      document.getElementById("first_name").focus();
      return false;
    }
  }
  
  if (document.getElementById("last_name")) {
    if (document.getElementById("last_name").value.length < 1) {
      alert("Please enter your last name.");
      document.getElementById("last_name").focus();
      return false;
    }
  }
  
  if (document.getElementById("title")) {
    if (document.getElementById("title").value.length < 1) {
      alert("Please enter your title.");
      document.getElementById("title").focus();
      return false;
    }
  }

  if (document.getElementById("company")) {
    if (document.getElementById("company").value.length < 1) {
      alert("Please enter your company's name.");
      document.getElementById("company").focus();
      return false;
    }
  }
  
  if (document.getElementById("phone")) {
    if (document.getElementById("phone").value.length < 1) {
      alert("Please enter your phone number.");
      document.getElementById("phone").focus();
      return false;
    }
  }

  if (document.getElementById("email").value.length < 1) {
    alert("Please enter your email address so we can fulfill your request.");
    document.getElementById("email").focus();
    return false;
  }

  if (!TestRequiredFields_Email()) {
    alert("Please enter a valid email address so we can fulfill your request.");
    document.getElementById("email").focus();
    return false;
  }

  if (document.getElementById("country")) {
    if (document.getElementById("country").value.length < 1) {
      alert("Please select your country.");
      document.getElementById("country").focus();
      return false;
    }

    if (document.getElementById("state")) {
      if (document.getElementById("country").value == "USA" && document.getElementById("state").value.length < 1) {
        alert("Please select a state.");
        document.getElementById("state").focus();
        return false;
      }

      if (document.getElementById("country").value == "Canada" && document.getElementById("state").value.length < 1) {
        alert("Please select a province.");
        document.getElementById("state").focus();
        return false;
      }
    }
  }

  if (document.getElementById("most_interesting")) {
    if (document.getElementById("most_interesting").value.length < 1) {
      alert("Please indicate which solution interests you the most.");
      document.getElementById("most_interesting").focus();
      return false;
    }
  }

  if (document.getElementById("num_mac")) {
    if (document.getElementById("num_mac").value.length < 1) {
      alert("Please indicate the number of Macs in your organization.");
      document.getElementById("num_mac").focus();
      return false;
    }
  }
  
  if (document.getElementById("num_win")) {
    if (document.getElementById("num_win").value.length < 1) {
      alert("Please indicate the number of Windows Servers in your organization.");
      document.getElementById("num_win").focus();
      return false;
    }
  }
  
  return true;
}

function mto(addr, domain, suffix, /* optional */ innertext, cssclass) {
    if (domain == 'eda') { domain = 'enterprisedesktopalliance'; }
    if (!innertext) innertext = addr + '@' + domain + '.' + suffix;
    cssclass = (!cssclass) ? ' ' : ' class="' + cssclass + '" ';
    document.write('<a' + cssclass + 'hr' + 'ef="' + 'mai' + 'lto:' + addr + '@' + domain + '.' + suffix + '">' + innertext + '</a>');
}
