{* Smarty *}

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

$(function() {
  AltButtons();
});
  
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 PoGet(){
  $.ajax({
    async: false,
    timeout: 3000,
    type: 'GET',
    url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/PoGet.ajax.php{literal}",
    success: function(answ){
      DialogStandartOpen('PoFrm', answ, "{/literal}{$MS_SP->Sp_LangMessage.message.MessSelectRecord}{literal}", 740);
      $('#GridPo').setGridWidth(720, false);
    }
  });
}

function PoSelect(){
  var VarCountSelectedRows = $("#GridPo").jqGrid('getGridParam','selarrrow').length;
  if(1 == VarCountSelectedRows){
    var VarId = $("#GridPo").jqGrid('getGridParam','selrow');
    var VarText = $("#GridPo").jqGrid('getRowData',VarId).po_name;
    if(!$("#FrmAgreePoCheckboxes"+VarId).length){
      $("#FormAdvPoContent").html('<div id="FrmAgreePoCheckboxes'+VarId+'"><input type="checkbox" id="FrmAgreePoCheckboxes'+VarId+'" name="FrmAgreePoCheckboxes[]" 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=\'$("#FrmAgreePoCheckboxes'+VarId+'").remove();\'></div>');
    }
    $("#PoFrm").dialog("destroy");
  }
  else{
    DialogStandartMessageOpen('error', "{/literal}{$MS_SP->Sp_LangMessage.warning.WarnShouldSelectOnlyOneRecord}{literal}");
  }
}

function showResponseFormPoLicenceAdd(responseText){
  if(typeof(responseText['error'])=='undefined'){
    //alert(responseText['message']);
    $("#GridPoLicence").trigger("reloadGrid");
    $("#PoLicenceEditFrm").dialog("destroy");
    DialogStandartMessageOpen("message", responseText['message']);
  }
  else{
    DialogStandartMessageOpen("error", responseText['error']);
  }
}

$(function() {
  $("input[name='policence_name']").css("width","400px");
  $("input[name='po_name']").css("width","400px");
  $("input[name='organiz_name']").css("width","400px");
  $("input[name='policence_number']").css("width","400px");
  $("textarea").css("width","400px");
  $("textarea").css("height","40px");
});

$(function() {
  var options = {
    success: showResponseFormPoLicenceAdd,
    timeout: 3000
  };
  $("#FormPoLicenceAdd").validate({
    submitHandler: function(form) {
      $(form).ajaxSubmit(options);
    },
    ignore: "",
    rules : {
      policence_name : {required : true},
      policence_number : {required : true, number: true},
      policence_dateb : {required : true},
      policence_datee : {required : true},
      'FrmAgreeOrganizHidden' : {
        required: {
          depends: function(element) {
            if($(":checkbox[id^='FrmAgreeOrganizCheckboxes']:checked").length){
              return false;
            }
            else{
              return true;
            }
          }
        }
      },
      'FrmAgreePoHidden' : {
        required: {
          depends: function(element) {
            if($(":checkbox[id^='FrmAgreePoCheckboxes']:checked").length){
              return false;
            }
            else{
              return true;
            }
          }
        }
      }
    },
    messages: {
      'FrmAgreeOrganizHidden': {
        required: "Выберите Организацию"
      },
      'FrmAgreePoHidden': {
        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;
  $("#policence_dateb").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("" == $("#policence_id").val()){
    $("#policence_dateb").datepicker( "setDate", '0d');
  }
  var VarYearE = (new Date).getFullYear()+50;
  $("#policence_datee").datepicker($.extend({yearRange: '1970:2038', 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("" == $("#policence_id").val()){
    $("#policence_datee").datepicker( "setDate", '+1y');
  }
});
//]]>
</script>
{/literal}

<form action="{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/PoLicenceSave.ajax.php" method="post" name="FormPoLicenceAdd" id="FormPoLicenceAdd">
  <input id="policence_id" name="policence_id" type="hidden" value="{if isset($ArrPoLicence.policence_name)}{$ArrPoLicence.policence_id}{/if}" />
  <table>
    <tr>
      <td><label for="policence_name">Наименование <span class="ui-state-error">* </span>:&nbsp;</label><span></span></td>
      <td colspan="2"><input type="text" id="policence_name" name="policence_name" value="{if isset($ArrPoLicence.policence_name)}{$ArrPoLicence.policence_name}{/if}" /></td>
    </tr>
    <tr>
<!--    <tr>
      <td><label for="po_id">Наименование ПО <span class="ui-state-error"></span>:&nbsp;</label><span></span></td>
      <td>
        <input id="po_name" name="po_name" value="{$ArrPoLicence.po_name|escape:'html'}" type="text" onclick="PoView();" readonly /><img title="Выбрать ПО" alt="Выбрать ПО" src="{$MS_SP->Sp_PathTop}/images/application_form_magnify.png" class="MsPseudoAT" onclick="PoView();" />
        <input id="po_id" name="po_id" type="hidden" value="{$ArrPoLicence.po_id}" />
      </td>
    </tr>-->
    <tr>
      <td><label for="FrmAgreePoHidden">ПО <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='PoGet();' /><input type="hidden" id="FrmAgreePoHidden" name="FrmAgreePoHidden" value="" /></td>
      <td id="FormAdvPoContent">
        {if isset($ArrPoLicence.po_id)}
          <div id="FrmAgreePoDivCheckboxes{$ArrPoLicence.po_id}"><input id="FrmAgreePoCheckboxes{$ArrPoLicence.po_id}" name="FrmAgreePoCheckboxes[]" value="{$ArrPoLicence.po_id}" checked="checked" type="checkbox" /> {$ArrPoLicence.po_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='$("#FrmAgreePoDivCheckboxes{$ArrPoLicence.po_id}").remove();' /></div>
        {/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($ArrPoLicence.organiz_id)}
          <div id="FrmAgreeOrganizDivCheckboxes{$ArrPoLicence.organiz_id}"><input id="FrmAgreeOrganizCheckboxes{$ArrPoLicence.organiz_id}" name="FrmAgreeOrganizCheckboxes[]" value="{$ArrPoLicence.organiz_id}" checked="checked" type="checkbox" /> {$ArrPoLicence.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{$ArrPoLicence.organiz_id}").remove();' /></div>
        {/if}
      </td>
    </tr>
    <tr>
      <td><label for="policence_number">Количество лицензий <span class="ui-state-error">* </span>:&nbsp;</label><span></span></td>
      <td colspan="2"><input type="text" id="policence_number" name="policence_number" value="{if isset($ArrPoLicence.policence_number)}{$ArrPoLicence.policence_number}{/if}" /></td>
    </tr>
    <tr>
      <td><label for="policence_note">Примечание :&nbsp;</label><span></span></td>
      <td colspan="2"><textarea cols="50" rows="2" id="policence_note" name="policence_note">{if isset($ArrPoLicence.policence_note)}{$ArrPoLicence.policence_note}{/if}</textarea></td>
    </tr>
    <tr>
      <td><label for="policence_dateb">Дата с <span class="ui-state-error">* </span>:&nbsp;</label><span></span></td>
      <td colspan="2">
        <input type="text" id="policence_dateb" name="policence_dateb" value="{if isset($ArrPoLicence.policence_dateb)}{$ArrPoLicence.policence_dateb|date_format:"%d.%m.%Y"}{/if}" />
      </td>
    </tr>
    <tr>
      <td><label for="policence_datee">Дата по <span class="ui-state-error">* </span>:&nbsp;</label><span></span></td>
      <td colspan="2">
        <input type="text" id="policence_datee" name="policence_datee" value="{if isset($ArrPoLicence.policence_datee)}{$ArrPoLicence.policence_datee|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>