{* Smarty *}

{literal}
<script type="text/javascript">
//<![CDATA[

$(function() {
  AltButtons();
});

function SvtGet(){
  $.ajax({
    async: false,
    timeout: 3000,
    type: 'GET',
    url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/SvtGet.ajax.php{literal}",
    success: function(answ){
      DialogStandartOpen('SvtFrm', answ, "{/literal}{$MS_SP->Sp_LangMessage.message.MessSelectRecord}{literal}", 740);
      $('#GridSvt').setGridWidth(720, false);
    }
  });
}

function SvtSelect(){
  var VarCountSelectedRows = $("#GridSvt").jqGrid('getGridParam','selarrrow').length;
  if(1 == VarCountSelectedRows){
    var VarId = $("#GridSvt").jqGrid('getGridParam','selrow');
    var VarText = '';
    if ($("#GridSvt").jqGrid('getRowData',VarId).svt_name.length){
      VarText = '('+ $("#GridSvt").jqGrid('getRowData',VarId).svt_name +') ';
    }
    VarText = VarText + $("#GridSvt").jqGrid('getRowData',VarId).svt_buxname;
    if(!$("#FrmAgreeSvtCheckboxes"+VarId).length){
      $("#FormAdvSvtContent").html('<div id="FrmAgreeSvtCheckboxes'+VarId+'"><input type="checkbox" id="FrmAgreeSvtCheckboxes'+VarId+'" name="FrmAgreeSvtCheckboxes[]" value="'+VarId+'" checked /> '+VarText+'&nbsp;<img style="vertical-align:middle;" title="Удалить" alt="Удалить" src="{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSmarty}/{$MS_SP->Sp_TemplateName}{literal}/css/images/delete.png" class="MsPseudoAT" onclick=\'$("#FrmAgreeSvtCheckboxes'+VarId+'").remove();\'></div>');
    }
    $("#SvtFrm").dialog("destroy");
  }
  else{
    DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.warning.WarnShouldSelectOnlyOneRecord}{literal}");
  }
}

function OrganizGet(){
  $.ajax({
    async: false,
    timeout: 3000,
    type: 'GET',
    url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/OrganizGet.ajax.php{literal}",
    success: function(answ){
      DialogStandartOpen('OrganizFrm', answ, "{/literal}{$MS_SP->Sp_LangMessage.message.MessSelectRecord}{literal}", 740);
      $('#GridOrganiz').setGridWidth(720, false);
    }
  });
}

function OrganizSelect(){
  var VarCountSelectedRows = $("#GridOrganiz").jqGrid('getGridParam','selarrrow').length;
  if(1 == VarCountSelectedRows){
    var VarId = $("#GridOrganiz").jqGrid('getGridParam','selrow');
    var VarText = $("#GridOrganiz").jqGrid('getRowData',VarId).organiz_name;
    if(!$("#FrmAgreeOrganizCheckboxes"+VarId).length){
      $("#FormAdvOrganizContent").html('<div id="FrmAgreeOrganizCheckboxes'+VarId+'"><input type="checkbox" id="FrmAgreeOrganizCheckboxes'+VarId+'" name="FrmAgreeOrganizCheckboxes[]" value="'+VarId+'" checked /> '+VarText+'&nbsp;<img style="vertical-align:middle;" title="Удалить" alt="Удалить" src="{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSmarty}/{$MS_SP->Sp_TemplateName}{literal}/css/images/delete.png" class="MsPseudoAT" onclick=\'$("#FrmAgreeOrganizCheckboxes'+VarId+'").remove();\'></div>');
    }
    $("#OrganizFrm").dialog("destroy");
  }
  else{
    DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.warning.WarnShouldSelectOnlyOneRecord}{literal}");
  }
}
  
function CartridgeTypeGet(){
  $.ajax({
    async: false,
    timeout: 3000,
    type: 'GET',
    url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/CartridgeTypeGet.ajax.php{literal}",
    success: function(answ){
      DialogStandartOpen('CartridgeTypeFrm', answ, "{/literal}{$MS_SP->Sp_LangMessage.message.MessSelectRecord}{literal}", 740);
      $('#GridCartridgeType').setGridWidth(720, false);
    }
  });
}

function CartridgeTypeSelect(){
  var VarCountSelectedRows = $("#GridCartridgeType").jqGrid('getGridParam','selarrrow').length;
  if(1 == VarCountSelectedRows){
    var VarId = $("#GridCartridgeType").jqGrid('getGridParam','selrow');
    var VarText = $("#GridCartridgeType").jqGrid('getRowData',VarId).cartridgetype_name;
    if(!$("#FrmAgreeCartridgeTypeCheckboxes"+VarId).length){
      $("#FormAdvCartridgeTypeContent").html('<div id="FrmAgreeCartridgeTypeCheckboxes'+VarId+'"><input type="checkbox" id="FrmAgreeCartridgeTypeCheckboxes'+VarId+'" name="FrmAgreeCartridgeTypeCheckboxes[]" value="'+VarId+'" checked /> '+VarText+'&nbsp;<img style="vertical-align:middle;" title="Удалить" alt="Удалить" src="{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSmarty}/{$MS_SP->Sp_TemplateName}{literal}/css/images/delete.png" class="MsPseudoAT" onclick=\'$("#FrmAgreeCartridgeTypeCheckboxes'+VarId+'").remove();\'></div>');
    }
    $("#CartridgeTypeFrm").dialog("destroy");
  }
  else{
    DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.warning.WarnShouldSelectOnlyOneRecord}{literal}");
  }
}


function showResponseCartridge(responseText){
  if(typeof(responseText['error'])=='undefined'){
    $("#GridCartridge").trigger("reloadGrid");
    $("#CartridgeEditFrm").dialog("destroy");
    DialogStandartMessageOpen("message", responseText['message']);
  }
  else{
    DialogStandartMessageOpen("error", responseText['error']);
  }
}

$(function() {
  $("input[name='cartridge_name']").css("width","400px");
  $("input[name='cartridge_serialnum']").css("width","400px");
  $("input[name='cartridgetype_name']").css("width","400px");
  $("input[name='cartridge_writeoff']").css("width","400px");
  $("textarea").css("width","400px");
  $("textarea").css("height","40px");
});

$(function() {
  var options = {
    success: showResponseCartridge,
    timeout: 3000
  };
  $("#FormCartridgeAdd").validate({
    submitHandler: function(form) {
      $(form).ajaxSubmit(options);
    },
    ignore: "",
    rules : {
      cartridge_name : {required : true},
      cartridge_serialnum : {required : true},
      cartridge_writeoff : {number : true},
      'FrmAgreeSvtHidden' : {
        required: {
          depends: function(element) {
            if($(":checkbox[id^='FrmAgreeSvtCheckboxes']:checked").length){
              return false;
            }
            else{
              return true;
            }
          }
        }
      },
      'FrmAgreeOrganizHidden' : {
        required: {
          depends: function(element) {
            if($(":checkbox[id^='FrmAgreeOrganizCheckboxes']:checked").length){
              return false;
            }
            else{
              return true;
            }
          }
        }
      },
      'FrmAgreeCartridgeTypeHidden' : {
        required: {
          depends: function(element) {
            if($(":checkbox[id^='FrmAgreeCartridgeTypeCheckboxes']:checked").length){
              return false;
              }
            else{
              return true;
            }
          }
        }
      }
    },
    messages: {
      'FrmAgreeSvtHidden': {
        required: "Выберите СВТ"
      },
      'FrmAgreeOrganizHidden': {
        required: "Выберите Продавца"
      },
      'FrmAgreeCartridgeTypeHidden': {
        required: "Выберите Тип картриджа"
      }
    },
    errorPlacement: function(error, element) {
      var er = element.attr("name");
      error.appendTo($("label[for='" + er + "']").next() );
    }
  });
});
  
$(function() {
  $.datepicker.setDefaults($.datepicker.regional['ru']);
  var VarYearE = (new Date).getFullYear()+1;
  $("#cartridge_loaddate").datepicker($.extend({yearRange: '1970:'+VarYearE, dateFormat: "dd.mm.yy", changeMonth: true, changeYear: true, closeText: 'X', showButtonPanel: true, showStatus: true,showOn: 'both', buttonText: 'Выбрать дату', buttonImageOnly: true, buttonImage: '{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSmarty}/{$MS_SP->Sp_TemplateName}/css/images/ms_check_calendar.png{literal}'}));
  if("" == $("#cartridge_id").val()){
    $("#cartridge_loaddate").datepicker( "setDate", '0d');
  }
  $("#cartridge_buydate").datepicker($.extend({yearRange: '1970:'+VarYearE, dateFormat: "dd.mm.yy", changeMonth: true, changeYear: true, closeText: 'X', showButtonPanel: true, showStatus: true,showOn: 'both', buttonText: 'Выбрать дату', buttonImageOnly: true, buttonImage: '{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSmarty}/{$MS_SP->Sp_TemplateName}/css/images/ms_check_calendar.png{literal}'}));
  if("" == $("#cartridge_id").val()){
    $("#cartridge_buydate").datepicker( "setDate", '0d');
  }
});
//]]>
</script>
{/literal}

<form action="{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/CartridgeSave.ajax.php" method="post" name="FormCartridgeAdd" id="FormCartridgeAdd">
  <input id="cartridge_id" name="cartridge_id" type="hidden" value="{if isset($ArrCartridge.cartridge_id)}{$ArrCartridge.cartridge_id}{/if}" />
  <table>
    <tr>
      <td><label for="FrmAgreeSvtHidden">Установлен в принтер <span class="ui-state-error">* </span>:&nbsp;</label><span></span></td>
      <td style="width:16px;"><img title="Выбрать СВТ" alt="Выбрать СВТ" src="/{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSmarty}/{$MS_SP->Sp_TemplateName}/css/images/application_form_magnify.png" class="MsPseudoAT" onclick='SvtGet();' /><input type="hidden" id="FrmAgreeSvtHidden" name="FrmAgreeSvtHidden" value="" /></td>
      <td id="FormAdvSvtContent">
        {if isset($ArrCartridge.svt_id)}
          <div id="FrmAgreeSvtDivCheckboxes{$ArrCartridge.svt_id}"><input id="FrmAgreeSvtCheckboxes{$ArrCartridge.svt_id}" name="FrmAgreeSvtCheckboxes[]" value="{$ArrCartridge.svt_id}" checked="checked" type="checkbox" /> {$ArrCartridge.svt_buxname}&nbsp;<img style="vertical-align: middle;" title="Удалить" alt="Удалить" src="/{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSmarty}/{$MS_SP->Sp_TemplateName}/css/images/delete.png" class="MsPseudoAT" onclick='$("#FrmAgreeSvtDivCheckboxes{$ArrCartridge.svt_id}").remove();' /></div>
        {/if}
      </td>
    </tr>
    <tr>
      <td><label for="cartridge_name">Наименование <span class="ui-state-error">* </span>:&nbsp;</label><span></span></td>
      <td colspan="2"><input type="text" id="cartridge_name" name="cartridge_name" value="{if isset($ArrCartridge.cartridge_name)}{$ArrCartridge.cartridge_name}{/if}" /></td>
    </tr>
    <tr>
      <td><label for="FrmAgreeOrganizHidden">Продавец <span class="ui-state-error">* </span>:&nbsp;</label><span></span></td>
      <td style="width:16px;"><img title="Выбрать Организацию" alt="Выбрать Организацию" src="/{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSmarty}/{$MS_SP->Sp_TemplateName}/css/images/application_form_magnify.png" class="MsPseudoAT" onclick='OrganizGet();' /><input type="hidden" id="FrmAgreeOrganizHidden" name="FrmAgreeOrganizHidden" value="" /></td>
      <td id="FormAdvOrganizContent">
        {if isset($ArrCartridge.organiz_id)}
          <div id="FrmAgreeOrganizDivCheckboxes{$ArrCartridge.organiz_id}"><input id="FrmAgreeOrganizCheckboxes{$ArrCartridge.organiz_id}" name="FrmAgreeOrganizCheckboxes[]" value="{$ArrCartridge.organiz_id}" checked="checked" type="checkbox" /> {$ArrCartridge.organiz_name}&nbsp;<img style="vertical-align: middle;" title="Удалить" alt="Удалить" src="/{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSmarty}/{$MS_SP->Sp_TemplateName}/css/images/delete.png" class="MsPseudoAT" onclick='$("#FrmAgreeOrganizDivCheckboxes{$ArrCartridge.organiz_id}").remove();' /></div>
        {/if}
      </td>
    </tr>
    <tr>
      <td><label for="cartridge_serialnum">Серийный № <span class="ui-state-error">* </span>:&nbsp;</label><span></span></td>
        <td colspan="2"><input type="text" id="cartridge_serialnum" name="cartridge_serialnum" value="{if isset($ArrCartridge.cartridge_serialnum)}{$ArrCartridge.cartridge_serialnum}{/if}" /></td>
    </tr>
    <tr>
      <td><label for="FrmAgreeCartridgeTypeHidden">Тип картриджа <span class="ui-state-error">* </span>:&nbsp;</label><span></span></td>
      <td style="width:16px;"><img title="Выбрать Тип картриджа" alt="Выбрать Тип картриджа" src="/{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSmarty}/{$MS_SP->Sp_TemplateName}/css/images/application_form_magnify.png" class="MsPseudoAT" onclick='CartridgeTypeGet();' /><input type="hidden" id="FrmAgreeCartridgeTypeHidden" name="FrmAgreeCartridgeTypeHidden" value="" /></td>
      <td id="FormAdvCartridgeTypeContent">
        {if isset($ArrCartridge.cartridgetype_id)}
          <div id="FrmAgreeCartridgeTypeDivCheckboxes{$ArrCartridge.cartridgetype_id}"><input id="FrmAgreeCartridgeTypeCheckboxes{$ArrCartridge.cartridgetype_id}" name="FrmAgreeCartridgeTypeCheckboxes[]" value="{$ArrCartridge.cartridgetype_id}" checked="checked" type="checkbox" /> {$ArrCartridge.cartridgetype_name}&nbsp;<img style="vertical-align: middle;" title="Удалить" alt="Удалить" src="/{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSmarty}/{$MS_SP->Sp_TemplateName}/css/images/delete.png" class="MsPseudoAT" onclick='$("#FrmAgreeCartridgeTypeDivCheckboxes{$ArrCartridge.cartridgetype_id}").remove();' /></div>
        {/if}
      </td>
    </tr>
    <tr>
      <td><label for="cartridge_writeoff">Количество заправок :&nbsp;</label><span></span></td>
      <td colspan="2"><input type="text" id="cartridge_writeoff" name="cartridge_writeoff" value="{if isset($ArrCartridge.cartridge_writeoff)}{$ArrCartridge.cartridge_writeoff}{/if}" /></td>
    </tr>
    <tr>
      <td><label for="cartridge_note">Примечание :&nbsp;</label><span></span></td>
      <td colspan="2"><textarea cols="50" rows="2" id="cartridge_note" name="cartridge_note">{if isset($ArrCartridge.cartridge_note)}{$ArrCartridge.cartridge_note}{/if}</textarea></td>
    </tr>
    <tr>
      <td><label for="cartridge_loaddate">Дата заправки :&nbsp;</label><span></span></td>
      <td colspan="2">
        <input type="text" id="cartridge_loaddate" name="cartridge_loaddate" value="{if isset($ArrCartridge.cartridge_loaddate)}{$ArrCartridge.cartridge_loaddate|date_format:"%d.%m.%Y"}{/if}" />
      </td>
    </tr>
    <tr>
      <td><label for="cartridge_buydate">Дата приобретения :&nbsp;</label><span></span></td>
      <td colspan="2">
        <input type="text" id="cartridge_buydate" name="cartridge_buydate" value="{if isset($ArrCartridge.cartridge_buydate)}{$ArrCartridge.cartridge_buydate|date_format:"%d.%m.%Y"}{/if}" />
      </td>
    </tr>
    <tr>
      <td colspan="3">
        <span class="ui-state-error"> * </span>поля обязательные для заполнения
      </td>
    </tr>
    <tr>
      <td colspan="3" class="MsFormSubmit"><input class="AltButtons" name="FormSubmit" value="сохранить" type="submit" /></td>
    </tr>
  </table>
</form>