$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

    $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
    $(".button").hide();

		
    var email = $("input#email").val();
    if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      $(".button").show();
      return false;
    }

      var etest = validate_reg_email(email);
      if (etest == false) {
         $("label#email_error").show();
         $(".button").show();
         $("input#email").focus();
         return false;
      }


    var firstname = $("input#firstName").val();
    if (firstname == "") {
      $("label#email_error").show();
      $("input#email").focus();
      $(".button").show();
      return false;
    }

    var companyname = $("input#companyName").val();
    if (companyname == "") {
      $("label#email_error").show();
      $("input#email").focus();
      $(".button").show();
      return false;
    }

    var lastname = $("input#lastName").val();
    if (lastname == "") {
      $("label#email_error").show();
      $("input#email").focus();
      $(".button").show();
      return false;
    }

    var apitype = $("input[type=hidden][name$=apitype]").val();
    var nltype = $("input[name='nltype']:checked").val(); 
    var sourceid = $("input[type=hidden][name$=sourceid]").val();
    var newsletterid = $("input[type=hidden][name$=newsletterid]").val();

    var dataString = 'companyName=' + companyname + '&firstName='+ firstname + '&lastName=' + lastname  + '&nltype=' + nltype + '&apitype=' + apitype + '&sourceid=' + sourceid + '&email=' + email +  '&newsletterid=' + newsletterid;
   // alert (dataString);return false;

      $(".button2").show()
		
		$.ajax({
      type: "POST",
      url: "/cls.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("")
        .append("<img src=\"/images/ci_newsletter_thanks.png\" />")
        .hide()
        .fadeIn(1500, function() {
          
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});

function validate_reg_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = email;
   if(reg.test(address) == false) {
      return false;
   } else {
      return true;
   }
}

