{* Smarty *}

{literal}
<script type="text/javascript">
//<![CDATA[
$(function() {
  AltButtons();
});

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

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

//function DemandsGet(){
//  $.ajax({
//    async: false,
//    timeout: 3000,
//    type: 'GET',
//    url: "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/DemandsGet.ajax.php{literal}",
//    success: function(answ){
//      DialogStandartOpen('DemandsFrm', answ, 'Выбрать заявку.');
//    }
//  });
//}

function DemandsSelectionSelect(com,grid){
  $('.trSelected',grid).each(function(){
    var VarStr = $(this).attr("id").replace("row","");
    if(!$("#FrmAgreeDemandsCheckboxes"+VarStr).length){
      var VarText = $("#"+$(this).attr("id")+" td[abbr='demands_id']",grid).text();
      $("#FormAdvDemandsContent").html('<div id="FrmAgreeDemandsCheckboxes'+VarStr+'"><input type="checkbox" id="FrmAgreeDemandsCheckboxes'+VarStr+'" name="FrmAgreeDemandsCheckboxes[]" value="'+VarStr+'" 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=\'$("#FrmAgreeDemandsCheckboxes'+VarStr+'").remove();\'></div>');
    }
  });
  $("#DemandsFrm").dialog("destroy");
}

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

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

$(function() {
  $("input[name='repair_summa']").css("width","400px");
  $("input[name='repair_numdoc']").css("width","400px");
  $("textarea").css("width","400px");
  $("textarea").css("height","40px");
});

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

$(function(){
  var options = {
    success: FormRepairAddShowResponse,
    timeout: 3000
  };
  $("#FormRepairAdd").validate({
    submitHandler: function(form) {
      $(form).ajaxSubmit(options);
    },
    ignore: "",
    rules : {
      'repair_summa' : {number : true},
      'FrmAgreeOrganizHidden' : {
        required: {
          depends: function(element) {
            if($(":checkbox[id^='FrmAgreeOrganizCheckboxes']:checked").length || '' != $(":hidden[id^='FrmAgreeOrganizHidden']").val()){
              return false;
            }
            else{
              return true;
            }
          }
        }
      },
      'FrmAgreeRepairStatusHidden' : {
        required: {
          depends: function(element) {
            if($(":checkbox[id^='FrmAgreeRepairStatusCheckboxes']:checked").length || '' != $(":hidden[id^='FrmAgreeRepairStatusHidden']").val()){
              return false;
            }
            else{
              return true;
            }
          }
        }
      }
    },
    messages: {
      'FrmAgreeOrganizHidden': {
        required: "Выберите Поставщика услуг"
      },
      'FrmAgreeRepairStatusHidden': {
        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']);
  $("#repair_numdocdate").datepicker($.extend({yearRange: '1978:'+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}'}));
  $("#repair_date_b").datepicker($.extend({yearRange: '1978:'+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}'}));
  $("#repair_date_e").datepicker($.extend({yearRange: '1978:'+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/RepairSave.ajax.php" method="post" name="FormRepairAdd" id="FormRepairAdd">
  <input id="repair_id" name="repair_id" type="hidden" value="{if isset($ArrRepair.repair_id)}{$ArrRepair.repair_id}{/if}" />
    {if isset($smarty.get.demands_id) && is_numeric($smarty.get.demands_id)}
  <input id="FrmAgreeDemandsCheckboxes{$smarty.get.demands_id}" name="FrmAgreeDemandsCheckboxes[]" type="hidden" value="{$smarty.get.demands_id}" />
  <table>
    {else}
  <table>
    <tr>
      <td><label for="FrmAgreeDemandsHidden">На основании заявки :&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='DemandsGet();' /><input type="hidden" id="FrmAgreeDemandsHidden" name="FrmAgreeDemandsHidden" value="" /></td>
      <td id="FormAdvDemandsContent">
        {if isset($ArrRepair.demands_id)}
          <div id="FrmAgreeDemandsDivCheckboxes{$ArrRepair.demands_id}"><input id="FrmAgreeDemandsCheckboxes{$ArrRepair.demands_id}" name="FrmAgreeDemandsCheckboxes[]" value="{$ArrRepair.demands_id}" checked="checked" type="checkbox" /> {$ArrRepair.demands_id}&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='$("#FrmAgreeDemandsDivCheckboxes{$ArrRepair.demands_id}").remove();' /></div>
        {/if}
      </td>
    </tr>
    {/if}
    <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($ArrRepair.organiz_id)}
          <div id="FrmAgreeOrganizDivCheckboxes{$ArrRepair.organiz_id}"><input id="FrmAgreeOrganizCheckboxes{$ArrRepair.organiz_id}" name="FrmAgreeOrganizCheckboxes[]" value="{$ArrRepair.organiz_id}" checked="checked" type="checkbox" /> {$ArrRepair.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{$ArrRepair.organiz_id}").remove();' /></div>
        {/if}
      </td>
    </tr>
    <tr>
      <td><label for="FrmAgreeRepairStatusHidden">Статус <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='RepairStatusGet();' /><input type="hidden" id="FrmAgreeRepairStatusHidden" name="FrmAgreeRepairStatusHidden" value="" /></td>
      <td id="FormAdvRepairStatusContent">
        {if isset($ArrRepair.repairstatus_id)}
          <div id="FrmAgreeRepairStatusDivCheckboxes{$ArrRepair.repairstatus_id}"><input id="FrmAgreeRepairStatusCheckboxes{$ArrRepair.repairstatus_id}" name="FrmAgreeRepairStatusCheckboxes[]" value="{$ArrRepair.repairstatus_id}" checked="checked" type="checkbox" /> {$ArrRepair.repairstatus_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='$("#FrmAgreeRepairStatusDivCheckboxes{$ArrRepair.repairstatus_id}").remove();' /></div>
        {/if}
      </td>
    </tr>
    <tr>
      <td><label for="repair_summa">Сумма :&nbsp;</label><span></span></td>
      <td colspan="2"><input type="text" id="repair_summa" name="repair_summa" value="{if isset($ArrRepair.repair_summa)}{$ArrRepair.repair_summa}{/if}" /></td>
    </tr>
    <tr>
      <td><label for="repair_content">Вид работ :&nbsp;</label><span></span></td>
      <td colspan="2"><textarea cols="50" rows="2" id="repair_content" name="repair_content">{if isset($ArrRepair.repair_content)}{$ArrRepair.repair_content}{/if}</textarea></td>
    </tr>
    <tr>
      <td><label for="repair_numdoc">№ платежного документа :&nbsp;</label><span></span></td>
      <td colspan="2"><input type="text" id="repair_numdoc" name="repair_numdoc" value="{if isset($ArrRepair.repair_numdoc)}{$ArrRepair.repair_numdoc}{/if}" /></td>
    </tr>
    <tr>
      <td><label for="repair_numdocdate">Дата платежного документа :&nbsp;</label><span></span></td>
      <td colspan="2">
        <input type="text" id="repair_numdocdate" name="repair_numdocdate" value='{if isset($ArrRepair.repair_numdocdate)}{$ArrRepair.repair_numdocdate|date_format:"%d.%m.%Y"}{/if}' />
      </td>
    </tr>
    <tr>
      <td><label for="FrmAgreeUsersCheckboxes[]">ФИО сист. инженера :&nbsp;</label><span></span></td>
      <td colspan="2">
        {foreach $ArrUsers as $val}
          {if $val@first}
            <select id="FrmAgreeUsersCheckboxes" name="FrmAgreeUsersCheckboxes[]">
              <option value="">---выберите исполнителя---</option>
          {/if}
              <option value="{$val.users_id}" {if isset($ArrRepair.users_id) && $ArrRepair.users_id eq $val.users_id}selected="selected"{/if}>{$val.users_fio}</option>
          {if $val@last}
            </select>
          {/if}
        {/foreach}
      </td>
    </tr>
    <tr>
      <td><label for="repair_date_b">Дата начала ремонта :&nbsp;</label><span></span></td>
      <td colspan="2">
        <input type="text" id="repair_date_b" name="repair_date_b" value='{if isset($ArrRepair.repair_date_b)}{$ArrRepair.repair_date_b|date_format:"%d.%m.%Y"}{/if}' />
      </td>
    </tr>
    <tr>
      <td><label for="repair_date_e">Дата окончания ремонта :&nbsp;</label><span></span></td>
      <td colspan="2">
        <input type="text" id="repair_date_e" name="repair_date_e" value='{if isset($ArrRepair.repair_date_e)}{$ArrRepair.repair_date_e|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>