﻿/*$('#ctl00_MainContent_uxLogin_UserName').keyup(
 function() {
  var result = "";
  $.each($(this).val().split(""), function(i, c) {
    if ( c == 'a') {
      result += c;
    }  
  });

    alert("keyup");
  $(this).val(result);
}); */


$().ready(function() {

  initValidation();
  enforceType('decimal', '.decimal');
  enforceType('integer', '.integer');
  addInputMask('zip', '.zipcode');
  addInputMask('phone', '.phone');
  addInputMask('url', '.url');
  //  *********  Validation Start ***********
  // do the following when the credentials section is visible
  if ($(_uxLoginCredentialsSection).length == 1) {
    // validate user name on txt change
    $(_uxUsername).change(
        function() {  //rule to see if username is already used, does ajax post back to server
          $(_uxUsername).rules("remove"); // removes all added rules.  will not fail if none added yet.
          if ($(_uxUsername).val().length > 0)
            $(_uxUsername).rules("add",
          {
            remote:
            {
              url: "/VendorAccount.aspx/isUserNameAvailable",
              dataType: "json",
              type: "post",
              contentType: "application/json; charset=utf-8",
              data: "{'username':'" + $(_uxUsername).val() + "'}",
              dataFilter: function(data, type) { return JSON.parse(data).d; } //for some reason ms web forms always wraps json responses in {d:[value]} ...
            }
          });
          if ($(_uxPassword).val().length > 0)
            $(_uxPasswordConfirm).rules("add", { equalTo: $(_uxPassword).val() });  // rule to make sure confirm password matches password
        });
  }
  $(_uxRefGridRowCount).rules("add", { min: 1 });  //rule for minimum # of references

  //validate that there is at least 1 customer reference
  $(_uxRegisterAcct1).click(function() {
    if ($('input.error').length == 1 && $(_uxRefGridRowCount).hasClass("error")) {
      popupDialog("uxErrorDialog", 300, "Validation Error", "At least 1 customer reference is required.");
    }
  });
  //  *********  Validation End ***********   

  $(_uxFindVendor1Results).change(function() { $("form:first").submit(); }); //post back to get search data

  checkboxEnabled($(_uxUseOtherSvcProvider), $(_uxOtherSvcProviders));
  checkboxEnabled($(_uxMWBE), $(_uxMWBEReg));

  $(_uxAddRefDialog).dialog({
    bgiframe: true,
    autoOpen: false,
    width: 600,
    modal: false,
    title: "Add Customer Reference",
    dialogClass: "popupDialog",
    beforeclose: function() {
      while ($(_uxAddRefDialog).parent().parent().is('form'))
        $(_uxAddRefDialog).parent().parent().zap();
    },
    buttons: { "Add Reference":
            function(evt) {
              $(_uxAddRefDialog).parent().wrap('<form></form>'); //wrap with form tags to meet validation pluggin requirements.
              var theForm = $(this).closest('form');
              if (theForm != null) {
                $(theForm).validate({ errorPlacement: function(error, element) { } });

                if ($(theForm).valid() == true) {
                  while ($(_uxAddRefDialog).parent().parent().is('form'))
                    $(_uxAddRefDialog).parent().parent().zap();

                  AddUserRef();
                  $(_uxAddRefDialog).dialog('close');
                }
              }
            }
    }
  });

  $("#uxAddRef").click(function() { $(_uxAddRefDialog).dialog('open'); });
});

    function getRefPrm() {
      return "{'company':' " + $(_uxRefCompany).val() +
               "','refContact':' " + $(_uxRefContact).val() +
               "','refPhone':' " + $(_uxRefPhone).val() +
               "','refEmail':' " + $(_uxRefEmail).val() +
               "','refYearsSvcd':' " + $(_uxRefYearsSvcd).val() + "'}";
    }

    function DeleteUserRef(btn) {
      //src code forces company to be unique so we can delete a record.  Need to do this because refs are stored in session state and only committed to sql on page submit.
      var _company = $(btn).closest('tr').children(':eq(1)').text();
      $.ajax({
        url: "/VendorAccount.aspx/DeleteUserReference",
        type: "POST",
        dataType: "json",
        data: "{'company' : '" + _company + "' }",
        contentType: "application/json; charset=utf-8",
        complete:
          function(jsondata, stat) {
            if (stat == "success") {
              if (JSON.parse(jsondata.responseText).d == false)
                popupDialog("AddRefError", 300, "Submission Failed", "Reference was not deleted.  Please contact Springwise support.");
              else
                __doPostBack(_upRefPnl, '');
            }  //trigger postback of update panel
          }
      });
    }

    function AddUserRef() {
      $.ajax({
        url: "/VendorAccount.aspx/AddUserReference",
        type: "POST",
        dataType: "json",
        data: getRefPrm(),
        contentType: "application/json; charset=utf-8",
        complete:
          function(jsondata, stat) {
            if (stat == "success") {
              if (JSON.parse(jsondata.responseText).d == false)
                popupDialog("AddRefError", 300, "Submission Failed","Reference was not saved.  Please verify the data you entered is valid and try again.");
              else
                __doPostBack(_uxRefPnl, ''); 
            }  //trigger postback of update panel
          }
      })
    };
