
<div class="row">
    <div class="container {if $lang_code == 'en'} gt4-ltr{/if}">
        <ol class="breadcrumb">
            <li><a href="#">{$smarty.const.EXPENSES_REVENUE}</a></li>
            <li><a href="#">{$smarty.const.EXPENSES}</a></li>
            <li class="active">{$smarty.const.ADD_EXPENSES}</li>
        </ol>
        <div class="panel panel-default panel-m">
            <!-- Default panel contents -->
            <div class="panel-heading">
                <h3 class="panel-title">{$smarty.const.ADD_EXPENSES}</h3>
            </div>
            <div class="panel-body">
                <!-- tree >
                <div class="col-lg-4 ">
                    <div class="row">
                        <div class="container">
                            <div class="panel panel-default panel-m">
                                <div class="panel-heading">
                                    <div class="caption"><i class="icon-comments"></i>شجرة المصروفات</div>
                                    <div class="actions"> <a href="javascript:;" id="tree_expense_collapse" class="btn btn-success"> اغلاق الكل</a> <a href="javascript:;" id="tree_expense_expand" class="btn btn-warning"> فتح الكل</a> </div>
                                </div>
                                <div class="panel-body" style="overflow-x:auto;  overflow-y:auto; height:400px;">
                                    <div class=" portlet-body fuelux">
                                        <ul class="tree" id="tree_expense">
                                            <li> <a href="#" data-role="branch" class="tree-toggle" data-toggle="branch" data-value="التصنيفات"> التصنيفات </a>
                                                <ul class="branch in">
                                                    {*$treeHTML*}
                                                </ul>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                </div><!----col-lg-4-->
                <!-- tree end -->

                <div class="col-lg-12">
                    <input name="selectedId" id="selectedId" type="hidden" value="" />
                    <input name="selectedClass" id="selectedClass" type="hidden" value="" />
                    <input name="addParent" id="addParent" type="button"  onclick="addParent();" style="display:none;" class="btn btn-primary conformdel"  value="{$smarty.const.ADD_EXPENSES}"/>&nbsp;&nbsp;
                    <input name="addExpense" id="addExpense" type="button" onclick="addExpense();" style="display:none;" class="btn btn-primary" value="{$smarty.const.ADD_EXPENSES}"  />
                    <!------------    Start  expensesTypeformDiv  --------------------------->
                    <div id="expensesTypeformDiv" style="display:none">

                    </div>
                    <!------------    End  expensesTypeformDiv  --------------------------->

                    <div class="clearfix"></div>

                    <div id="expensesEdit"></div>
                    <div class="clearfix"></div>

                    <!------------    Start  expensesformDiv  --------------------------->
                    <div id="expensesformDiv" >
                        <form  action="expensesController.php?do=add"  method="post" class=" form-horizontal" id="submitnew" name="myExpenseForm" enctype="multipart/form-data">


                            <div class="form-group">
                                <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_PAYMENT_METHOD}</label>
                                <div class="col-lg-4">
                                    <div class="radio-inline">
                                        <label>
                                            <input value="0"  name="paymethod" id="PMSave" class="PMethod" checked="checked"   type="radio" />

                                            {$smarty.const.EXPENSES_SAFE} </label>
                                    </div>
                                    <div class="radio-inline">
                                        <label>
                                            <input value="1" name="paymethod" id="PMBank" class="PMethod" type="radio" />
                                            {$smarty.const.EXPENSES_BANK} </label>
                                    </div>
                                    <div class="radio-inline">
                                        <label>
                                            <input value="2" name="paymethod" id="PMSupplier" class="PMethod" type="radio" />
                                            مورد</label>
                                    </div>

                                </div>
                            </div>


                            <div id="save-div">

                                {if $searchinonesave == 1}
                                <input type="hidden" name="saveid" id="saveid" value="{$allSave}" />
                                {else}
                                <div class="form-group">
                                    <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_SAFE}</label>
                                    <div class="col-lg-4">
                                        <select name="saveid" id="saveid" class="select2">
                                            <option value="-1">{$smarty.const.EXPENSES_CHOOSE_SAFE}</option>
                                            {foreach $allSave as $data}
                                            <option value="{$data->saveid}" {if $saveid == $data->saveid}selected{/if}>{$data->savename}</option>
                                            {/foreach}
                                        </select>
                                    </div>
                                </div>
                                {/if}

                                <!-- {if $userData->searchinonesave eq 0}
                                <div class="form-group">
                                    <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_SAFE}</label>
                                    <div class="col-lg-4">
                                        <span id="spryselect3">
                                            <select name="saveid"  id="saveid" class="select2">
                                                <option value="-1">{$smarty.const.EXPENSES_CHOOSE_SAFE}</option>
                                                {foreach $allSave as $data}
                                                <option value="{$data->saveid}" {if $userData->saveid == $data->saveid}selected{/if}>{$data->savename}</option>
                                                {/foreach}
                                            </select>
                                            <span class="selectInvalidMsg">{$smarty.const.EXPENSES_CHOOSE_SAFE}</span></span>
                                    </div>
                                </div>
                                {else}
                                <div class="form-group">
                                    <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_SAFE}</label>
                                    <div class="col-lg-4">
                                        {foreach $allSave as $data}
                                        {if $userData->saveid == $data->saveid}{$data->savename}{/if}
                                        <input type="hidden" name="saveid" id="saveid" value="{$userData->saveid}"/>
                                        {/foreach}
                                    </div>
                                </div>
                                {/if} -->

                            </div>

                            <div id="bank-div" style="display: none">
                                <div class="form-group">
                                    <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_ELBANK}</label>
                                    <div class="col-lg-4">
                                        <span id="spryselect4">
                                            <select name="bankid"  id="bankid" class="select2">
                                                <option value="-1">{$smarty.const.EXPENSES_CHOOSE_ELBANK}</option>
                                                {foreach $bank as $data}
                                                <option value="{$data->bankid}">{$data->bankname}</option>
                                                {/foreach}
                                            </select>
                                            <span class="selectInvalidMsg">{$smarty.const.EXPENSES_CHOOSE_ELBANK}</span></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_ACCOUNT}</label>
                                    <div class="col-lg-4">
                                        <span id="spryselect5">
                                            <select name="bankaccountid"  id="bankaccountid" class="">

                                            </select>
                                            <span class="selectInvalidMsg">{$smarty.const.EXPENSES_CHOOSE_ACCOUNT}</span></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_CHECK_NUMBER}</label>
                                    <div class="col-lg-4">
                                        <span id="sprytextfield3">
                                            <input name="checkNo" id="checkNo" type="text"   class="form-control" />
                                            <span class="textfieldRequiredMsg">*</span></span>
                                    </div>
                                </div>
                            </div>



                            <div class="form-group">
                                <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_TYPE}</label>
                                <div class="col-lg-4">
                                    <input type="text" name="expensetype"  class="wdt100" id="expensetype" data-placeholder="{$smarty.const.EXPENSES_CHOOSE_TYPE} " />
                                </div>
                            </div>

                            <!--   <div class="form-group"   >
                                  <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_TYPE}</label>
                                  <div class="col-lg-4">
                                      <select class="select2 wdt100" required="" data-placeholder="{$smarty.const.EXPENSES_CHOOSE_TYPE}"  name="expensetype" id="expensetype" >
                                          <option value=""></option>
                                          {foreach $allParents as $data}
                                          <option value="{$data->expensestypeid}">{$data->expensestypename}</option>
                                          {/foreach}
                                      </select>
                                      <div id="expensetypeError" style="display:none;"></div>
                                  </div>
                              </div> -->
                            <div class="form-group"   {if $Programsettingdata->expensesproduct eq 0} {else} style="display:none" {/if}>
                                <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_PRODUCTS}</label>
                                <div class="col-lg-4">
                                    <input type="hidden" name="searchvalue" id="searchvalue" value="{$Programsettingdata->searchvalue}"/>
                                    <input type="text" name="productid" id="productid" class="select2product wdt100 clas_offerproduct" data-itr='1'  data-placeholder="{$smarty.const.EXPENSES_CHOOSE_PRODUCT}" tabindex="301"/>
                                </div>
                            </div>
                            <div  {if $Programsettingdata->costcenter neq 0 && $Programsettingdata->realestatedev neq 0} style="display:none" {/if}>
                                <div class="form-group"   >
                                    <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_COST_CENTER}</label>
                                    <div class="col-lg-4">
                                        <select class="select2 wdt100" name="Costcenterid" id="Costcenterid" >
                                            <option value="-1">{$smarty.const.EXPENSES_CHOOSE_COST_CENTER}</option>
                                            {foreach $allCostcenter as $myallCostcenter}
                                            <option value="{$myallCostcenter->id}">{$myallCostcenter->name}</option>
                                            {/foreach}
                                        </select>
                                    </div>
                                </div>
                            </div>



                            <div class="form-group">
                                <label for="inputEmail1" class="col-lg-2 control-label">{$smarty.const.EXPENSES_NAME}</label>
                                <div class="col-lg-4">
                                    <!--<input type="text" name="expensesname" id="expensesname"   ocnhange="checkExpenseName(this.value);"class="form-control" placeholder="إسم المصروف">-->
                                    <input type="text" name="expensesname" id="expensesname" class="form-control" placeholder="{$smarty.const.EXPENSES_NAME}" required="">

                                    <div id="nameError" style="display:none;"></div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="inputEmail1" class="col-lg-2 control-label">{$smarty.const.EXPENSES_VALUE}</label>
                                <div class="col-lg-4">
                                    <input type="text" name="expensevalue" id="expensevalue"   class="form-control positivefloat calcExpensevalueAfterTax"  placeholder="{$smarty.const.EXPENSES_VALUE}">
                                    <div id="valueError" style="display:none;"></div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputEmail1" class="col-lg-2 control-label">يوجد قيمة مضافة</label>
                                <div class="col-lg-4">
                                    <input type="hidden" id="vatValue_Setting_h" value="{$Programsettingdata->vatValue}">
                                    <input type="checkbox" name="taxExist" class="calcExpensevalueAfterTax" id="taxExist" value="1">
                                </div>
                            </div>
                            <div class="form-group vatDiv" style="display: none">
                                <label for="inputEmail1" class="col-lg-2 control-label">  ضريبه القيمة المضافة</label>
                                <div class="col-lg-4">
                                    <div class="input-group">
                                        <input type="text" name="tax" class="form-control numberinput positiveNo calcExpensevalueAfterTax" id="tax" value="0">
                                        <span class="input-group-addon">%</span>
                                    </div>
                                    <div class="input-group" style="padding-top: 1px;">
                                        <input type="text" name="taxVal" class="form-control numberinput positivefloat" id="taxVal" value="0" readonly="">
                                        <span class="input-group-addon">جنيه</span>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group vatDiv" style="display: none">
                                <label for="inputEmail1" class="col-lg-2 control-label">قيمة المصروف بعد الضريبة</label>
                                <div class="col-lg-4">
                                    <div class="input-group" style="padding-top: 1px;">
                                        <input type="text" name="expensevalueAfterTax" id="expensevalueAfterTax"   class="form-control positivefloat"  value="0" readonly="">
                                        <span class="input-group-addon">جنيه</span>
                                    </div>
                                </div>
                            </div>

                            {if $Programsettingdata->expensesupplier eq 1}
                            <div class="form-group">
                                <label class="col-lg-2 control-label">المورد</label>
                                <div class="col-lg-4">
                                    <select class="select2 wdt100" name="supplier" id="supplier" >
                                        <option value="0"></option>
                                        {foreach $suppliersData as $data}
                                        <option value="{$data->supplierid}">{$data->suppliername}</option>
                                        {/foreach}
                                    </select>
                                    <div id="supplierError" style="display:none;"></div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputEmail1" class="col-lg-2 control-label">المتبقى على المورد</label>
                                <div class="col-lg-4">
                                    <input type="text" name="suppRemainingVal" id="suppRemainingVal"   class="form-control positivefloat">
                                    <div id="valueError" style="display:none;"></div>
                                </div>
                            </div>
                            {/if}
                            {if $Programsettingdata->specialexpenses eq 0}
                            <div class="form-group">
                                <label for="inputEmail1" class="col-lg-2 control-label">{$smarty.const.EXPENSES_NUMBER}</label>
                                <div class="col-lg-4">
                                    <input type="text" name="expensenumbrt" id="expensenumbrt"   class="form-control positivefloat"  placeholder="{$smarty.const.EXPENSES_NUMBER}">
                                    <div id="valueError" style="display:none;"></div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputEmail1" class="col-lg-2 control-label">{$smarty.const.EXPENSES_PRICE}</label>
                                <div class="col-lg-4">
                                    <input type="text" name="expenseprice" id="expenseprice"   class="form-control positivefloat"  placeholder="{$smarty.const.EXPENSES_PRICE}">
                                    <div id="valueError" style="display:none;"></div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputEmail1" class="col-lg-2 control-label">{$smarty.const.EXPENSES_TOTALN}</label>
                                <div class="col-lg-4">
                                    <input type="text" name="expensetotaln" id="expensetotaln"  readonly class="form-control positivefloat"  placeholder="{$smarty.const.EXPENSES_TOTALN}">
                                    <div id="valueError" style="display:none;"></div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputEmail1" class="col-lg-2 control-label">{$smarty.const.EXPENSES_SHORTFALLS}</label>
                                <div class="col-lg-4">
                                    <input type="text" name="expenseshortfalls"  id="expenseshortfalls"   class="form-control positivefloat"  placeholder="{$smarty.const.EXPENSES_SHORTFALLS}">
                                    <div id="valueError" style="display:none;"></div>
                                </div>
                            </div>
                            {/if}
                            {if $Programsettingdata->clientsell eq 1}
                            <div class="form-group">
                                <label for="txtName" class="col-lg-2 control-label">العملاء</label>
                                <div class="col-lg-4">
                                    <input type="text" name="clientid" class="wdt100" id="select2client" data-placeholder="اختر " />
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="txtName" class="col-lg-2 control-label">الفواتير </label>
                                <div class="col-lg-4">
                                    <input type="text" name="sellbillid" class="wdt100" id="select2sellbill" data-placeholder="اختر " />
                                </div>
                            </div>
                            {/if}

                            {if $Programsettingdata->realestateunits eq 1}
                            <div class="form-group">
                                <label for="txtName" class="col-lg-2 control-label">العقارات</label>
                                <div class="col-lg-4">
                                    <input type="text" name="realestateid" class="wdt100" id="select2realestates" data-placeholder="اختر " />
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="txtName" class="col-lg-2 control-label">الوحدات </label>
                                <div class="col-lg-4">
                                    <input type="text" name="realestateunitid" class="wdt100" id="select2realestatesunits" data-placeholder="اختر " />
                                </div>
                            </div>
                            {/if}



                            <div class="form-group">
                                <label for="inputEmail1" class="col-lg-2 control-label">{$smarty.const.EXPENSES_COMMENT}</label>
                                <div class="col-lg-4">
                                    <textarea name="expensecomment" class="form-control "></textarea>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-lg-2 control-label">الملف</label>
                                <div class="col-lg-4">
                                    <input type="file" name="file" id="file"/>
                                </div>
                            </div>
                            <input type="hidden" name="add_print" id="add_print" value="0">
                            <div class="form-group">
                                <div class="col-lg-offset-2 col-lg-4">
                                    <input type="button" id="add_print_btn" name="sp" class="btn btn-success pull-left submitnew" value="حفظ وطباعة"/>
                                    <input type="button" id="" name="ss" class="btn btn-primary pull-left submitnew" style="margin-left: 3px;" value="{$smarty.const.EXPENSES_SAVE}"/>
                                </div>
                            </div>
                        </form>
                    </div>
                    <!------------    End  expensesformDiv  --------------------------->
                    <div class="clearfix"></div>

                </div><!----col-lg-8-->
                <div class="clearfix"></div>


                <!-- Modal -->
                <div class="modal fade" id="catModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content bg-modal">
                            <div class="modal-header">
                                <button type="button" class="close pull-left" data-dismiss="modal" aria-hidden="true">&times;</button>
                                <h4 class="modal-title">{$smarty.const.EXPENSES_ADD_TYPE}</h4>
                            </div>
                            <div class="modal-body">

                                <form class="nameToBeChecked2 form-horizontal" action="expensesControllerajex.php?do=addCat"  id="catForm" name="catForm" method="post" enctype="multipart/form-data">


                                    <input name="parent" id="parent" type="hidden" value="" />
                                    <div class="form-group">
                                        <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_TYPE_NAME}</label>
                                        <div class="col-lg-4">
                                            <input type="text" name="name" id="name" required="required" class="form-control" />
                                            <div id="nameError" class="alert alert-block alert-danger fade in" style="display:none">
                                                <button type="button" class="close pull-left" data-dismiss="alert" aria-hidden="true">&times;</button>
                                                <h4>{$smarty.const.EXPENSES_WRONG_MESSAGE}</h4>
                                                <p>{$smarty.const.EXPENSES_NAME_ALREADY_TAKEN}</p>
                                                <div class="clearfix"></div>
                                            </div>
                                        </div>
                                    </div>

                                    <!--==============================-->

                                    <div class="form-group">
                                        <label class="col-lg-2 control-label">{$smarty.const.EXPENSES_OTHER_DETAILS}</label>
                                        <div class="col-lg-4">

                                            <textarea   name="descripe" id="descripe" class="form-control" cols="45" rows="5" ></textarea>
                                        </div>
                                    </div>

                                    <!--==============================-->

                                    <div class="form-group">
                                        <div class="col-lg-offset-2 col-lg-4">
                                            {literal}
                                            <input type="button"  id="ww" name="ww" class="btn btn-primary pull-left" value="{$smarty.const.EXPENSES_ADD}" onclick="if (Spry.Widget.Form.validate(this)) {
                                                        checkCatName(event, 'expensestype', 'name');
                                                        return true;
                                                    } else {
                                                        return false;
                                                    }"/>
                                            {/literal}
                                            <!--validate the form before checking name-->
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <!--              <button type="reset" class="btn btn-default" data-dismiss="modal">اغلاق</button>
                                -->              <!--<button type="submit" class="btn btn-primary savemodel">حفظ</button>-->
                            </div>
                        </div>

                    </div><!-- /.modal-content -->
                </div><!-- /.modal-dialog -->
            </div><!-- /.modal -->
            <!--------=====================================================================---->

        </div>
        <div class="clearfix"></div>
    </div>
</div>

{literal}
<script type="text/javascript">
    var spryselect3 = new Spry.Widget.ValidationSelect("spryselect3", {invalidValue: "-1", isRequired: false});
    var spryselect4 = '';
    var spryselect5 = '';
    var sprytextfield3 = '';

    $(".PMethod").live('change', function () {
        var whatis = $(this).val();
        if (whatis == 0) {
            $('#save-div').show();
            $('#bank-div').hide();
            spryselect3 = new Spry.Widget.ValidationSelect("spryselect3", {invalidValue: "-1", isRequired: false});
            spryselect4.destroy();
            spryselect5.destroy();
            sprytextfield3.destroy();
        } else if (whatis == 1) {
            $('#save-div').hide();
            $('#bank-div').show();
            spryselect3.destroy();
            spryselect4 = new Spry.Widget.ValidationSelect("spryselect4", {invalidValue: "-1", isRequired: false});
            spryselect5 = new Spry.Widget.ValidationSelect("spryselect5");
            sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3");
        }

    });
    $("#bankid").live('change', function () {
        var id = $(this).val();
        $.post("projectControllerAjax.php?do=getbankaccount", {id: id}, function (data) {
            $("#bankaccountid").html(data);
        });
    });

</script>
<script type="text/javascript">

    $(document).ready(function () {


        var formatResult;
        formatResult = function (data) {
            return  data.text;
        }


        $("#expensetype").select2({
            placeholder: "إسم  المشروع",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "expensesControllerajex.php?do=expensetypesave",
                type: "post",
                dataType: 'json',
                data: function (term, page) {
                    var saveid = Number($('#saveid').val());
                    if (!saveid) {
                        alert('يجب اختيار الخزنه اولا');
                        return false;
                    }
                    return {
                        searchTerm: term,
                        saveid: saveid,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });


        $("#select2client").select2({
            placeholder: "إسم  المشروع",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "executors.php?do=select2client",
                type: "post",
                dataType: 'json',
                data: function (term, page) {
                    return {
                        searchTerm: term,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });



        $("#select2realestates").select2({
            placeholder: "العقار",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "realestateunits.php?do=select2realestates",
                type: "post",
                dataType: 'json',
                data: function (term, page) {
                    $('#select2realestatesunits').val("").trigger("change");
                    return {
                        searchTerm: term,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });



        $("#select2realestatesunits").select2({
            placeholder: "الوحده",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "realestateunits.php?do=select2realestatesunits",
                type: "post",
                dataType: 'json',
                data: function (term, page) {
                    var select2realestates = Number($('#select2realestates').val());
                    if (!select2realestates) {
                        alert('يجب اختيار العقار اولا');
                        return false;
                    }
                    return {
                        searchTerm: term,
                        realestateid: select2realestates,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });



        $('body').on('change', '#select2realestates', function () {
            var id = $(this).val();
            var thisinput = $(this);
            $.post('realestateunits.php?do=getexpenstype', {id: id}, function (result) {
                if (result) {
                    $('#expensetype').val(result.trim()).trigger('change');
                }
            });

        });




        $("#select2sellbill").select2({
            placeholder: "إسم  المشروع",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "executors.php?do=select2sellbill",
                type: "post",
                dataType: 'json',
                data: function (term, page) {
                    var select2client = Number($('#select2client').val());
                    if (!select2client) {
                        alert('يجب اختيار العميل اولا');
                        return false;
                    }
                    return {
                        searchTerm: term,
                        clientid: select2client,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });

        $("#expenseprice,#expensenumbrt").live('change', function () {
            var expensenumbrt = $('#expensenumbrt').val();
            var expenseprice = $('#expenseprice').val();
            $('#expensetotaln').val(expensenumbrt * expenseprice);
        });

        $(".positiveNo").forcepositiveNum();
        var formatResult;
        formatResult = function (data) {

            return  data.text;
        }
        $("#productid").select2({
            placeholder: "إسم المنتج",
            width: '100%',
            minimumInputLength: document.getElementById("searchvalue").value,
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "sellbillAjaxController.php?do=getProductsexpenses",
                dataType: 'json',
                data: function (term, page) {
                    return {
                        term: term, //search term
                        page_limit: 10,

                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });

    });
</script>
{/literal}