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

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

$(function() {
  $("input[name='demands_intdocnum']").css("width","400px");
  $("#FrmAgreeMasterCheckboxes").css("width","400px");
  $("#FrmAgreeSprStatusCheckboxes").css("width","400px");
  $("#FrmAgreeDemandsClassifCheckboxes").css("width","400px");
  $("textarea").css("width","400px");
  $("textarea").css("height","120px");
  $("textarea").resizable();
});

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

$(function(){
  var options = {
    success: showResponseFormDemandsAdd,
    timeout: 3000
  };
  $("#FormDemandsAdd").validate({
    submitHandler: function(form) {
      $(form).ajaxSubmit(options);
    },
    ignore: "",
    rules : {
      "FrmAgreeUsersCheckboxes[]": {required : true},
      "FrmAgreeSprStatusCheckboxes[]": {required : true},
//      "FrmAgreeDemandsClassifCheckboxes[]": {required : true},
      'demandscomments_comment' : {required : true},
      'FrmSvtHidden' : {
        required: {
          depends: function(element) {
            //alert($(":hidden[id^='FrmAgreeSvtCheckboxes']").val());
            if($(":checkbox[id^='FrmAgreeSvtCheckboxes']:checked").length || '' != $(":hidden[id^='FrmAgreeSvtCheckboxes']").val()){
              return false;
            }
            else{
              return true;
            }
          }
        }
      },
      'FrmSprStatusHidden' : {
        required: {
          depends: function(element) {
            if($(":checkbox[id^='FrmAgreeSprStatusCheckboxes']:checked").length || '' != $(":hidden[id^='FrmAgreeSprStatusCheckboxes']").val()){
              return false;
            }
            else{
              return true;
            }
          }
        }
      }
    },
    messages: {
      'FrmSvtHidden': {
        required: "Выберите СВТ"
      },
      'FrmSprStatusHidden': {
        required: "Выберите Статус"
      }
    },
    errorPlacement: function(error, element) {
      var er = element.attr("name");
      error.appendTo($("label[for='" + er + "']").next() );
    }
  });
});

$(function(){
  var VarYearE = (new Date).getFullYear()+2;
  $.datepicker.setDefaults($.datepicker.regional['ru']);
  $("#demands_intdocdate").datepicker($.extend({yearRange: '2011:'+VarYearE, 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}'}));  
});
//]]>
</script>
{/literal}

<form action="{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/DemandsSave.ajax.php" method="post" name="FormDemandsAdd" id="FormDemandsAdd">
  <input id="demands_id" name="demands_id" type="hidden" value="{if isset($ArrDemands.demands_id)}{$ArrDemands.demands_id}{/if}" />
  <table>
    {if isset($smarty.session.SessionAuth.SvtAdmin.reg) || isset($smarty.session.SessionAuth.SvtOperatorAdv.reg) || isset($smarty.session.SessionAuth.SvtOperator.reg)}
    <tr>
      <td><label for="FrmUsersHidden">Заявка от <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='UsersGet("Users");' /><input type="hidden" id="FrmUsersHidden" name="FrmUsersHidden" value="" /></td>
      <td id="FormAdvUsersContent">
        {if isset($ArrDemands.users_id)}
          <div id="FrmAgreeUsersDivCheckboxes{$ArrDemands.users_id}"><input id="FrmAgreeUsersCheckboxes{$ArrDemands.users_id}" name="FrmAgreeUsersCheckboxes[]" value="{$ArrDemands.users_id}" checked="checked" type="checkbox" /> {$ArrDemands.users_fio}&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='$("#FrmAgreeUsersDivCheckboxes{$ArrDemands.users_id}").remove();' /></div>
        {/if}
      </td>
    </tr>
    {else}
    <tr>
      <td><label for="FrmAgreeUsersCheckboxes[]">Заявка от <span class="ui-state-error">* </span>:&nbsp;</label><span></span></td>
      <td colspan="2">
        {foreach $ArrUsers as $val}
          {if $val@first}
            <select id="FrmAgreeUsersCheckboxes" name="FrmAgreeUsersCheckboxes[]">
          {/if}
              <option value="{$val.users_id}" {if isset($ArrDemands.users_id) && $ArrDemands.users_id eq $val.users_id}selected="selected"{/if}>{$val.users_fio}</option>
          {if $val@last}
            </select>
          {/if}
        {/foreach}
      </td>
    </tr>
    {/if}
    {if isset($smarty.session.SessionAuth.SvtAdmin.reg) || isset($smarty.session.SessionAuth.SvtOperatorAdv.reg) || isset($smarty.session.SessionAuth.SvtOperator.reg)}
    <tr>
      <td><label for="FrmSvtHidden">СВТ <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="FrmSvtHidden" name="FrmSvtHidden" value="" /></td>
      <td id="FormAdvSvtContent">
        {if isset($ArrDemands.svt_id)}
          <div id="FrmAgreeSvtDivCheckboxes{$ArrDemands.svt_id}"><input id="FrmAgreeSvtCheckboxes{$ArrDemands.svt_id}" name="FrmAgreeSvtCheckboxes[]" value="{$ArrDemands.svt_id}" checked="checked" type="checkbox" /> {if $ArrDemands.svt_name}({$ArrDemands.svt_name}) {/if}{$ArrDemands.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{$ArrDemands.svt_id}").remove();' /></div>
        {/if}
      </td>
    </tr>
    <tr>
      <td><label for="FrmAgreeMasterCheckboxes[]">Исполнитель :&nbsp;</label><span></span></td>
      <td colspan="2">
        {foreach $ArrUsersMaster as $val}
          {if $val@first}
            <select id="FrmAgreeMasterCheckboxes" name="FrmAgreeMasterCheckboxes[]">
              <option value="">---выберите исполнителя---</option>
          {/if}
              <option value="{$val.users_id}" {if isset($ArrDemands.demands_masterid) && $ArrDemands.demands_masterid eq $val.users_id}selected="selected"{/if}>{$val.users_fio}</option>
          {if $val@last}
            </select>
          {/if}
        {/foreach}
      </td>
    </tr>
    <tr>
      <td><label for="FrmAgreeSprStatusCheckboxes[]">Статус <span class="ui-state-error">* </span>:&nbsp;</label><span></span></td>
      <td colspan="2">
        {foreach $ArrSprStatus as $val}
          {if $val@first}
            <select id="FrmAgreeSprStatusCheckboxes" name="FrmAgreeSprStatusCheckboxes[]">
              <option value="">---выберите статус---</option>
          {/if}
              <option value="{$val.sprstatus_id}" {if isset($ArrDemands.sprstatus_id) && $ArrDemands.sprstatus_id eq $val.sprstatus_id}selected="selected"{/if}>{$val.sprstatus_name}</option>
          {if $val@last}
            </select>
          {/if}
        {/foreach}
      </td>
    </tr>
    <tr>
      <td><label for="FrmAgreeDemandsClassifCheckboxes[]">Классификация :&nbsp;</label><span></span></td>
      <td colspan="2">
        {foreach $ArrDemandsClassif as $val}
          {if $val@first}
            <select id="FrmAgreeDemandsClassifCheckboxes" name="FrmAgreeDemandsClassifCheckboxes[]">
              <option value="">---выберите классификацию---</option>
          {/if}
              <option value="{$val.demandsclassif_id}" {if isset($ArrDemands.demandsclassif_id) && $ArrDemands.demandsclassif_id eq $val.demandsclassif_id}selected="selected"{/if}>{$val.demandsclassif_name}</option>
          {if $val@last}
            </select>
          {/if}
        {/foreach}
      </td>
    </tr>
    {else}
     <tr>
       <td colspan="3">
      {if isset($smarty.get.UrlId)}
        <input type="hidden" id="FrmSvtHidden" name="FrmSvtHidden" value="" />
        <input id="FrmAgreeSvtCheckboxes{$smarty.get.UrlId}" name="FrmAgreeSvtCheckboxes[]" value="{$smarty.get.UrlId}" type="hidden" />
      {/if}
      <input type="hidden" id="FrmSprStatusHidden" name="FrmSprStatusHidden" value="" />
      <input id="FrmAgreeSprStatusCheckboxes1" name="FrmAgreeSprStatusCheckboxes[]" value="1" type="hidden" />
      </td>
    </tr>
    {/if}
    <tr>
      <td><label for="demands_intdocnum">№ служ. записки :&nbsp;</label><span></span></td>
      <td colspan="2"><input type="text" id="demands_intdocnum" name="demands_intdocnum" value="{if isset($ArrDemands.demands_intdocnum)}{$ArrDemands.demands_intdocnum}{/if}" /></td>
    </tr>
    <tr>
      <td><label for="demands_intdocdate">Дата служ. записки :&nbsp;</label><span></span></td>
      <td colspan="2">
        <input type="text" id="demands_intdocdate" name="demands_intdocdate" value="{if isset($ArrDemands.demands_intdocdate)}{$ArrDemands.demands_intdocdate|date_format:'%d.%m.%Y'}{/if}" />
      </td>
    </tr>
    <tr>
      <td><label for="demandscomments_comment">Содержание заявки <span class="ui-state-error">* </span>:&nbsp;</label><span></span></td>
      <td colspan="2">
        <textarea cols="50" rows="2" id="demandscomments_comment" name="demandscomments_comment">{if isset($ArrDemandsComments.demandscomments_comment)}{$ArrDemandsComments.demandscomments_comment}{/if}</textarea>
      </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>