

// JavaScript Document

/* testing jquery library */
$(document).ready(function(e) {
 
  /*  $('#comp_client_form').bootstrapValidator({
        message: 'This value is not valid',
        excluded: [':disabled'],
        
    });

	$('#clientdiv').on('shown.bs.modal', function() {
    $('#comp_client_form').bootstrapValidator('resetForm', true);
});
    /* ============ add new client ==============*/
    $("#comp_client").change(function() {
        var comp_client_val = $(this).val();
        if (comp_client_val == -2) {
            var elemposition = $(this).data('position');
            $("#myModal_client").find(".savemodel_client").attr('data-position', elemposition);
            $('#myModal_client').modal('show');
        }
    });
    $(".savemodel_client_close").click(function() {
        $("#comp_client").select2("val", "");
    });
    /* ============ add new supplier ==============*/
    /*$(".client_supplier").live('change', function() {
     var comp_client_val = $(this).val();
     var data_no_supplier = $(this).attr("data-no");
     //alert(data_no_supplier);
     if (comp_client_val == -2) {
     var elemposition = $(this).data('position');
     $("#myModal_supplier").find(".savemodel_client").attr('data-position', elemposition);
     $("#myModal_supplier").find("#datanosupplier").val(data_no_supplier);
     $('#myModal_supplier').modal('show');
     }
     });*/
    $(".savemodel_supplier_close").click(function() {
        var data_no_supplier = $("#myModal_supplier").find("#datanosupplier").val();
        $("#client_supplier" + data_no_supplier).select2("val", "");
    });
    /* ============ validation form ==============*/
    //$("form").validate();
	$("#comp_client_form").validate();
    /* calcute client_insure intinal value */
    var caluteinsuratotal = function() {
        var client_insura_total = 0;
        $(".intinal_value").each(function() {
            client_insura_total += Number($(this).val());
        });
        $("#client_total_intial").html(client_insura_total);
		$("#client_total_intialx").val(client_insura_total);
    }
    caluteinsuratotal();
    $(".intinal_value").live('keyup', function() {
        var client_insura_total = 0;
        $(".intinal_value").each(function() {
            client_insura_total += Number($(this).val());
        });
        $("#client_total_intial").html(client_insura_total);
		$("#client_total_intialx").val(client_insura_total);
        caculcateshiprecept();
        caculcateinitialrecept();
    });
    /* calcute client_insure ship value */

    var caluteshiptotal = function() {
        var client_ship_total = 0;
        $(".ship_value").each(function() {
            client_ship_total += Number($(this).val());
        });
        $("#client_total_ship").html(client_ship_total);
		$("#client_total_shipx").val(client_ship_total);
    };
    caluteshiptotal();
    $(".ship_value").live('keyup', function() {
        var client_ship_total = 0;
        $(".ship_value").each(function() {
            client_ship_total += Number($(this).val());
        });
        $("#client_total_ship").html(client_ship_total);
		$("#client_total_shipx").val(client_ship_total);
        caculcateshiprecept();
        caculcateinitialrecept();
    });

	
    //

    /* add new row in clinet insurce */
    var clint_insure_no = Number($("#clint_insure_no").val()) + 1;
    var clint_tabindex_no = Number($("#clint_tabindex_no").val()) + 1;
    $("#client_add_new").click(function() {
        $("#client_insure_container").append('<tr>' +
                '<td>' +
                '<input type="hidden" class="select2ajax select2-offscreen" name="product' + clint_insure_no + '" id="product' + clint_insure_no + '" tabindex="' + clint_tabindex_no + '" data-placeholder="اختر منتج" >' +
                '<label id="selectfirst' + clint_insure_no + '" style="display:none" > من فضلك اختر المنتج اولا</label>'+
				'</td>' +
                '<td >' +
                '<input type="text" class="form-control client_serial" name="client_serial' + clint_insure_no + '" id="client_serial' + clint_insure_no + '" tabindex="' + clint_tabindex_no + '" placeholder="السيريل" data-no="' + clint_insure_no + '" required="required">' +
			    '</td>' +
                '<td >' +
				 '<input type="hidden" class="form-control  pull-right" tabindex="5" name="client_sellbill_type' + clint_insure_no + '" id="client_sellbill_type' + clint_insure_no + '" placeholder="نوع الفاتوره" />'+
                '<input type="text" class="form-control  pull-right" name="client_sellbill_serial' + clint_insure_no + '" id="client_sellbill_serial' + clint_insure_no + '" tabindex="' + clint_tabindex_no + '" placeholder="رقم الفاتوره"/>' +
                '<div class="clearfix"></div>' +
                '<input type="text" class=" pull-right awdatepicker" autocomplete="off"  data-date-format="yyyy-mm-dd" value="" placeholder="تاريخ الفاتورة"  tabindex="' + clint_tabindex_no + '" name="client_sellbill_date' + clint_insure_no + '" id="client_sellbill_date' + clint_insure_no + '"/>' +
                
				'</td>' +
                '<td >' +
				'<input type="hidden" class="form-control  pull-right" tabindex="5" name="client_buybill_type' + clint_tabindex_no + '" id="client_buybill_type' + clint_tabindex_no + '" placeholder="نوع الفاتوره" />'+
                '<input type="text" class="form-control  pull-right" tabindex="' + clint_tabindex_no + '" name="client_buybill_serial' + clint_insure_no + '" id="client_buybill_serial' + clint_insure_no + '" placeholder="رقم الفاتوره" /> ' +
                '<div class="clearfix"></div>' +
                '<input type="text" class=" pull-right awdatepicker" autocomplete="off"  data-date-format="yyyy-mm-dd" value="" placeholder="تاريخ الفاتورة"  tabindex="' + clint_tabindex_no + '" name="client_buybill_date' + clint_insure_no + '" id="client_buybill_date' + clint_insure_no + '" />' +
                
				'</td>' +
                '<td >' +
                '<input type="text" class="form-control"  placeholder="مده الضمان" tabindex="' + clint_tabindex_no + '" name="client_insurce_duration' + clint_insure_no + '" id="client_insurce_duration' + clint_insure_no + '" value="0">' +
                '</td>' +
                '<td  nowrap>' +
                '<div class="radio">' +
                '<label>' +
				' <input type="hidden" name="optionsRadiostype' + clint_insure_no + '" id="optionsRadiostype' + clint_insure_no + '"  tabindex="' + clint_tabindex_no + '" >'+
                '<input type="radio" name="optionsRadios' + clint_insure_no + '" id="optionsRadios' + clint_insure_no + '" value="0" tabindex="' + clint_tabindex_no + '">' +
                'خاضع' +
                '</label>' +
                '</div>' +
                '<div class="radio">' +
                '<label>' +
                '<input type="radio" name="optionsRadios' + clint_insure_no + '" id="optionsRadiosx' + clint_insure_no + '" value="1" tabindex="' + clint_tabindex_no + '"  checked>' +
                'غير خاضع' +
                '</label>' +
                '</div>' +
                '</td>' +
                '<td >' +
                '<textarea class="form-control" name="clinet_error' + clint_insure_no + '" id="clinet_error' + clint_insure_no + '" tabindex="' + clint_tabindex_no + '"></textarea>' +
                '</td>' +
                '<td >' +
                '<textarea class="form-control" name="client_note' + clint_insure_no + '" id="client_note' + clint_insure_no + '" tabindex="' + clint_tabindex_no + '"></textarea>' +
                '</td>' +
                '<td >' +
                '<textarea class="form-control" name="client_attch' + clint_insure_no + '" id="client_attch' + clint_insure_no + '" tabindex="' + clint_tabindex_no + '"></textarea>' +
                '</td>' +
                '<td >' +
                '<input type="text" class="form-control numberinput intinal_value" value="" name="client_intinal_cost' + clint_insure_no + '" id="client_intinal_cost' + clint_insure_no + '" tabindex="' + clint_tabindex_no + '" placeholder="تكلفه مبدئيه"  required="required">' +
                '</td>' +
                '<td >' +
                '<input type="text" class="form-control numberinput ship_value" value="" name="client_ship_cost' + clint_insure_no + '" id="client_ship_cost' + clint_insure_no + '" tabindex="' + clint_tabindex_no + '" placeholder="تكلفه الشحن" required="required" >' +
                
				'</td>' +
                '<td >' +
                /*'  <div id="supplierIdDiv' + clint_insure_no + '">'+
                 '<select data-placeholder="اختر المورد" data-no="' + clint_insure_no + '" class="select2 pull-left client_supplier"  tabindex="5" name="client_supplier' + clint_insure_no + '"  id="client_supplier' + clint_insure_no + '" required>'+
                 '    <option value=""></option>'+
                 '  <option value="-2" class="newoption">إضافة مورد جديد</option>'+
                 $("#suppliersdiv").html()+
                 ' </select>'+

                 '</div>'+*/
				 '  <div id="supplierIdDiv' + clint_insure_no + '">'+
                '<input type="hidden" class="select2ajax select2-offscreen client_supplier" name="client_supplier' + clint_insure_no + '" id="client_supplier' + clint_insure_no + '" tabindex="' + clint_tabindex_no + '" data-placeholder="اختر مورد" data-no="' + clint_insure_no + '">' +
               	'<input type="hidden" id="addsupplierId' + clint_insure_no + '" name="addsupplierId' + clint_insure_no + '" value=""  />'+
                 '<input type="hidden" id="addsuppliernam' + clint_insure_no + '" name="addsuppliernam' + clint_insure_no + '"  value=""  />'+
			    '</div>'+
				'</td>' +
                '<td >' +
                '<button type="button" class="close flt-none removeicon client_removeproduct" data-type="' + clint_insure_no + '" aria-hidden="true">×</button>' +
                '</td>' +
                '</tr>');
        $('select').select2();

        //assign select2 properties
        $("#product" + clint_insure_no).select2({
            placeholder: "إسم المنتج",
            //minimumInputLength: 3,
            minimumInputLength: document.getElementById("searchvalue").value,
            //	  maximumSelectionSize:3,
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "sellbillAjaxController.php?do=getProducts2",
                dataType: 'json',
                data: function(term, page) {
                    return {
                        term: term, //search term
                        page_limit: 10 // page size
                    };
                },
                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);
            }
        });

		 
        $("#client_supplier" + clint_insure_no).select2({
            placeholder: "إسم المنتج",
            //minimumInputLength: 3,
            minimumInputLength: document.getElementById("searchvalue").value,
            //	  maximumSelectionSize:3,
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "billreceiptAjaxController.php?do=getallsupplier",
                dataType: 'json',
                data: function(term, page) {
                    return {
                        term: term, //search term
                        page_limit: 10 // page size
                    };
                },
                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);
            }
        });
		
        var formatResult = function(data) {
			
            return  data.text;
        };
        caluteshiptotal();
        caluteinsuratotal();
        caculcateshiprecept();
        caculcateinitialrecept();
        clint_insure_no++;
        clint_tabindex_no++;
        $("#clint_insure_no").val(clint_insure_no);
    });

    //remove from client insure tbl
    $(".client_removeproduct").live('click', function() {
      
		
	  $(this).parent().parent().remove();
        caluteshiptotal();
        caluteinsuratotal();
        caculcateshiprecept();
        caculcateinitialrecept();
    });

	$(".client_removeproductx").live('click', function() {
      
		var no = $(this).attr('data-type');
	//$(this).parent().parent().css('background-color','#096');
	$(this).parent().parent().hide();
	//$(this).css('color','#903');
	  $("#client_intinal_cost"+no).remove();
	  $("#client_ship_cost"+no).remove();
	  $("#del"+no).val(1);
	  
	 
        caluteshiptotal();
        caluteinsuratotal();
        caculcateshiprecept();
        caculcateinitialrecept();
    });
	
	
$("#client").click(function(e) {
   
  	 var e = document.getElementById('clientdiv');
  	
       if(e.style.display == 'block')
	   {
		    $(this).css('color','#000');
			
          	e.style.display = 'none';
			//$("#client_serial1").attr("parsley-required","false");
		 
		 }
       else
	   {
          e.style.display = 'block'; 
		  // $("#comp_client_form").find('div[id=clientdiv]').validate();
		  $(this).css('color','#939');
	   }
	    caluteshiptotal();
        caluteinsuratotal();
        caculcateshiprecept();
        caculcateinitialrecept();
	//$(this).parent('h6').parent('div').hide();
});


    /***** =================================== company insure ========================================== *****/

    /* add new row in clinet insurce */
   var company_insure_no = Number($("#company_insure_no").val()) + 1;
    var company_tabindex_no = Number($("#company_tabindex_no").val()) + 1;
//	alert(company_insure_no);
    $("#company_add_new").click(function() {
		
        $("#company_insure_container").append('<tr>' +
                '<td>' +
                '<input type="hidden" class="select2ajax select2-offscreen" name="productcompany' + company_insure_no + '" id="productcompany' + company_insure_no + '" tabindex="' + company_tabindex_no + '" data-placeholder="اختر منتج" >' +
               	'<label id="selectfirstcompany' + company_insure_no + '" style="display:none" > من فضلك اختر المنتج اولا</label>'+
			    '</td>' +
                '<td >' +
                '<input type="text" class="form-control company_serial" name="company_serial' + company_insure_no + '" id="company_serial' + company_insure_no + '" tabindex="' + company_tabindex_no + '" placeholder="السيريل" data-no="' + company_insure_no + '"  required="required">' +
                '</td>' +
                '<td >' +
				'<input type="hidden" class="form-control  pull-right" tabindex="' + company_tabindex_no + '" name="company_buybill_type' + company_insure_no + '" id="company_buybill_type' + company_insure_no + '" placeholder="نوع الفاتوره" />'+
                '<input type="text" class="form-control  pull-right" tabindex="' + company_tabindex_no + '" name="company_buybill_serial' + company_insure_no + '" id="company_buybill_serial' + company_insure_no + '" placeholder="رقم الفاتوره"  /> ' +
                '<div class="clearfix"></div>' +
                '<input type="text" class=" pull-right awdatepicker" autocomplete="off"  data-date-format="yyyy-mm-dd" value="2014-09-06" placeholder="تاريخ الفاتورة"  tabindex="' + company_tabindex_no + '" name="company_buybill_date' + company_insure_no + '" id="company_buybill_date' + company_insure_no + '" />' +
                '</td>' +
                '<td >' +
                '<input type="text" class="form-control"  placeholder="مده الضمان" tabindex="' + company_tabindex_no + '" name="company_insurce_duration' + company_insure_no + '" id="company_insurce_duration' + company_insure_no + '" value="0" >' +
                '</td>' +
                '<td  nowrap>' +
                '<div class="radio">' +
                '<label>' +
				'<input type="hidden" name="company_optionsRadiostype' + company_insure_no + '" id="company_optionsRadiostype' + company_insure_no + '"  tabindex="' + company_tabindex_no + '" >'+
                '<input type="radio" name="company_optionsRadios' + company_insure_no + '" id="company_optionsRadios' + company_insure_no + '" value="0" tabindex="' + company_tabindex_no + '" >' +
                'خاضع' +
                '</label>' +
                '</div>' +
                '<div class="radio">' +
                '<label>' +
                '<input type="radio" name="company_optionsRadios' + company_insure_no + '" id="company_optionsRadiosx' + company_insure_no + '" value="1" tabindex="' + company_tabindex_no + '" checked>' +
                'غير خاضع' +
                '</label>' +
                '</div>' +
                '</td>' +
                '<td >' +
                '<textarea class="form-control" name="clinet_error' + company_insure_no + '" id="clinet_error' + company_insure_no + '" tabindex="' + company_tabindex_no + '"></textarea>' +
                '</td>' +
                '<td >' +
                '<textarea class="form-control" name="company_note' + company_insure_no + '" id="company_note' + company_insure_no + '" tabindex="' + company_tabindex_no + '"></textarea>' +
                '</td>' +
                '<td >' +
                '<textarea class="form-control" name="company_attch' + company_insure_no + '" id="company_attch' + company_insure_no + '" tabindex="' + company_tabindex_no + '"></textarea>' +
                '</td>' +
				'<td >'+
                  '<input type="text" class="form-control numberinput company_intinal_value"  value=""  name="company_intinal_cost' + company_insure_no + '" id="company_intinal_cost' + company_insure_no + '" tabindex="' + company_tabindex_no + '" placeholder="تكلفه مبدئيه"  required="required">'+
                   ' </td>'+
                '<td >' +
                '<input type="text" class="form-control numberinput company_ship_value" value="" name="company_ship_cost' + company_insure_no + '" id="company_ship_cost' + company_insure_no + '" tabindex="' + company_tabindex_no + '" placeholder="تكلفه الشحن"  required="required">' +
                '</td>' +
                '<td >' +
                 '  <div id="supplierIdDivxx' + company_insure_no + '">'+
                ' <input type="hidden" class="select2ajax select2-offscreen company_supplier" name="company_supplier' + company_insure_no + '" id="company_supplier' + company_insure_no + '" tabindex="' + company_tabindex_no + '" data-placeholder="اختر مورد" data-no="' + company_insure_no + '"  required="required">' +
               		'<input type="hidden" id="addsupplierIdx' + company_insure_no + '" name="addsupplierIdx' + company_insure_no + '" value=""  />'+
                 '<input type="hidden" id="addsuppliernamx' + company_insure_no + '" name="addsuppliernamx' + company_insure_no + '"  value=""  />'+
			   '</div>'+
                '</td>' +
                '<td >' +
                '<button type="button" class="close flt-none removeicon company_removeproduct" data-type="' + company_insure_no + '" aria-hidden="true">×</button>' +
                '</td>' +
                '</tr>');
        $('select').select2();
        //assign select2 properties
        $("#productcompany" + company_insure_no).select2({
            placeholder: "إسم المنتج",
            //minimumInputLength: 3,
            minimumInputLength: document.getElementById("searchvalue").value,
            //	  maximumSelectionSize:3,
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "sellbillAjaxController.php?do=getProducts2",
                dataType: 'json',
                data: function(term, page) {
                    return {
                        term: term, //search term
                        page_limit: 10 // page size
                    };
                },
                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);
            }
        });
		
		 $("#company_supplier" + company_insure_no).select2({
            placeholder: " اسم المورد",
            //minimumInputLength: 3,
            minimumInputLength: document.getElementById("searchvalue").value,
            //	  maximumSelectionSize:3,
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "billreceiptAjaxController.php?do=getallsupplier",
                dataType: 'json',
                data: function(term, page) {
                    return {
                        term: term, //search term
                        page_limit: 10 // page size
                    };
                },
                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);
            }
        });
        var formatResult = function(data) {
            return  data.text;
        };
        calutecompanyshiptotal();
		calutecompanyinsuratotal();
        caculcateshiprecept();
        caculcateinitialrecept();
        company_insure_no++;
        company_tabindex_no++;
        $("#company_insure_no").val(company_insure_no);
    });
    /* calcute company_insure ship value */

    var calutecompanyshiptotal = function() {
        var company_ship_total = 0;
        $(".company_ship_value").each(function() {
            company_ship_total += Number($(this).val());
        });
        $("#company_total_ship").html(company_ship_total);
		$("#company_total_shipx").val(company_ship_total);
    };
    calutecompanyshiptotal();
    $(".company_ship_value").live('keyup', function() {
        var company_ship_total = 0;
        $(".company_ship_value").each(function() {
            company_ship_total += Number($(this).val());
        });
		 $("#company_total_ship").html(company_ship_total);
		$("#company_total_shipx").val(company_ship_total);
        caculcateshiprecept();
        caculcateinitialrecept();
    });
	/* calcute client_insure intinal value */
    var calutecompanyinsuratotal = function() {
        var company_insura_total = 0;
        $(".company_intinal_value").each(function() {
            company_insura_total += Number($(this).val());
        });
        $("#company_total_intial").html(company_insura_total);
		$("#company_total_intialx").val(company_insura_total);
    }
    calutecompanyinsuratotal();
    $(".company_intinal_value").live('keyup', function() {
        var company_insura_total = 0;
        $(".company_intinal_value").each(function() {
            company_insura_total += Number($(this).val());
        });
        $("#company_total_intial").html(company_insura_total);
		$("#company_total_intialx").val(company_insura_total);
        caculcateshiprecept();
        caculcateinitialrecept();
    });
    //remove from company insure tbl
    $(".company_removeproduct").live('click', function() {
		
		/*var no = $(this).attr('data-type');
		var ourserial =$("#productname"+no).val();
		if(ourserial !="")
		{
			$.ajax({
				type: 'POST',
				url: 'billreceiptAjaxController.php?do=getserial&ourserial='+ourserial,
				
				
				success: function(data) {
					
				}
				});
		}*/
        $(this).parent().parent().remove();
        calutecompanyshiptotal();
		calutecompanyinsuratotal();
		
        caculcateshiprecept();
        caculcateinitialrecept();
		
    });
	
	 $(".company_removeproductx").live('click', function() {
		
		var no = $(this).attr('data-type');
		
        $(this).parent().parent().hide();
		$("#delcompany"+no).val(1);
        $("#company_intinal_cost"+no).remove();
	 	 $("#company_ship_cost"+no).remove();
		calutecompanyshiptotal();
		calutecompanyinsuratotal();
		
        caculcateshiprecept();
        caculcateinitialrecept();
		
    });
	
	
	$("#company").click(function(e) {
   
   
    var ex = document.getElementById('companydiv');
  			
			 if(ex.style.display == 'block')
			 {
				 	 ex.style.display = 'none';
					 $(this).css('color','#000');
				 		  
			}
			 else
			{
				
				  ex.style.display = 'block';
				 
			}
			
		calutecompanyshiptotal();
		calutecompanyinsuratotal();
        caculcateshiprecept();
        caculcateinitialrecept();
});
    /* ============ add new supplier ==============*/
    /*$(".company_supplier").live('change', function() {
        var comp_company_val = $(this).val();
        var data_no_supplier = $(this).attr("data-no");
        if (comp_company_val == -2) {
            var elemposition = $(this).data('position');
            $("#myModal_supplier").find(".savemodel_company").attr('data-position', elemposition);
            $("#myModal_supplier").find("#datanocompanysupplier").val(data_no_supplier);
            $('#myModal_supplier').modal('show');
        }
    });*/
    $(".savemodel_supplier_close").click(function() {
        var data_no_supplier = $("#myModal_supplierx").find("#datanocompanysupplierx").val();
        $("#company_supplier" + data_no_supplier).select2("val", "");
    });
    /***** =================================== mantance insure ========================================== *****/

    /* add new row in clinet insurce */
    var mantance_insure_no = Number($("#mantance_insure_no").val()) + 1;
    var mantance_tabindex_no = Number($("#mantance_tabindex_no").val()) + 1;
    $("#mantance_add_new").click(function() {
        $("#mantance_insure_container").append('<tr>' +
                '<td>' +
                '<input type="hidden" class="select2ajax select2-offscreen" name="productmantance' + mantance_insure_no + '" id="productmantance' + mantance_insure_no + '" tabindex="' + mantance_tabindex_no + '" data-placeholder="اختر منتج" >' +
                '</td>' +
                '<td >' +
                '<input type="text" class="form-control" name="mantance_serial' + mantance_insure_no + '" id="mantance_serial' + mantance_insure_no + '" tabindex="' + mantance_tabindex_no + '" placeholder="السيريل"  required="required" >' +
                '</td>' +
                '<td class="text-center hidden">' +
                '<textarea class="form-control" name="mantance_description' + mantance_insure_no + '" id="mantance_description' + mantance_insure_no + '" tabindex="' + mantance_tabindex_no + '"></textarea>' +
                '</td>' +
                '<td >' +
                '<textarea class="form-control" name="clinet_error' + mantance_insure_no + '" id="clinet_error' + mantance_insure_no + '" tabindex="' + mantance_tabindex_no + '"></textarea>' +
                '</td>' +
                '<td >' +
                '<textarea class="form-control" name="mantance_note' + mantance_insure_no + '" id="mantance_note' + mantance_insure_no + '" tabindex="' + mantance_tabindex_no + '"></textarea>' +
                '</td>' +
                '<td >' +
                '<textarea class="form-control" name="mantance_attch' + mantance_insure_no + '" id="mantance_attch' + mantance_insure_no + '" tabindex="' + mantance_tabindex_no + '"></textarea>' +
                '</td>' +
                '<td >' +
                '<input type="text" class="form-control mantance_intinal_value" value="" name="mantance_intinal_cost' + mantance_insure_no + '" id="mantance_intinal_cost' + mantance_insure_no + '" tabindex="' + mantance_tabindex_no + '" placeholder="تكلفه مبدئيه" required="required" >' +
                '</td>' +
                '<td >' +
                '<button type="button" class="close flt-none removeicon mantance_removeproduct" data-type="' + company_insure_no + '" aria-hidden="true">×</button>' +
                '</td>' +
                '</tr>');
        $('select').select2();
        //assign select2 properties
        $("#productmantance" + mantance_insure_no).select2({
            placeholder: "إسم المنتج",
            //minimumInputLength: 3,
            minimumInputLength: document.getElementById("searchvalue").value,
            //	  maximumSelectionSize:3,
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "sellbillAjaxController.php?do=getProducts2",
                dataType: 'json',
                data: function(term, page) {
                    return {
                        term: term, //search term
                        page_limit: 10 // page size
                    };
                },
                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);
            }
        });
        var formatResult = function(data) {
            return  data.text;
        };
        calutemantanceinsuratotal();
        caculcateshiprecept();
        caculcateinitialrecept();
        mantance_insure_no++;
        mantance_tabindex_no++;
        $("#mantance_insure_no").val(mantance_insure_no);
    });
    /* calcute mantance_insure intinal value */
    var calutemantanceinsuratotal = function() {
        var mantance_insura_total = 0;
        $(".mantance_intinal_value").each(function() {
            mantance_insura_total += Number($(this).val());
        });
        $("#mantance_total_intial").html(mantance_insura_total);
		$("#mantance_total_intialx").val(mantance_insura_total);
    };
    calutemantanceinsuratotal();
    $(".mantance_intinal_value").live('keyup', function() {
        var mantance_insura_total = 0;
        $(".mantance_intinal_value").each(function() {
            mantance_insura_total += Number($(this).val());
        });
        $("#mantance_total_intial").html(mantance_insura_total);
		$("#mantance_total_intialx").val(mantance_insura_total);
        caculcateshiprecept();
        caculcateinitialrecept();
    });
    //remove from mantance insure tbl
    $(".mantance_removeproduct").live('click', function() {
		
        $(this).parent().parent().remove();
        calutemantanceinsuratotal();
        caculcateshiprecept();
        caculcateinitialrecept();
    });
	
	$(".mantance_removeproductx").live('click', function() {
		
		var no = $(this).attr('data-type');
		
        $(this).parent().parent().hide();
		$("#delmantance"+no).val(1);
	 	 $("#mantance_intinal_cost"+no).remove();
        calutemantanceinsuratotal();
        caculcateshiprecept();
        caculcateinitialrecept();
    });
	
$("#mantance").click(function(e) {
   
   var e = document.getElementById('mantancediv');
  	
       if(e.style.display == 'block')
	   {
         
		  e.style.display = 'none';
		  $(this).css('color','#000');

		 }
       else
	   {
          e.style.display = 'block';
		  $(this).css('color','#939');
		  calutemantanceinsuratotal();
        caculcateshiprecept();
        caculcateinitialrecept();
		 
	   }
	//$(this).parent('h6').parent('div').hide();
});
    /**************************** total ****************************************/
    var caculcateshiprecept = function() {
        var caculcateshiprecept_total = 0;
        $(".ship_value").each(function() {
            caculcateshiprecept_total += Number($(this).val());
        });
        $(".company_ship_value").each(function() {
			var ex = document.getElementById('companydiv');
			 if(ex.style.display == 'none')
			   {
				  caculcateshiprecept_total +=0;
			 	}
			   else
			   {
				   caculcateshiprecept_total += Number($(this).val());
			   }
        });
        $("#recept_total_shop").html(caculcateshiprecept_total);
		 $("#recept_total_shopx").val(caculcateshiprecept_total);
    };
    var caculcateinitialrecept = function() {
        var caculcateinitialrecept_total = 0;
        $(".intinal_value").each(function() {
            caculcateinitialrecept_total += Number($(this).val());
        });
		$(".company_intinal_value").each(function() {
			
			var ex = document.getElementById('companydiv');
			 if(ex.style.display == 'none')
			   {
				  caculcateinitialrecept_total +=0;
			 	}
			   else
			   {
				   caculcateinitialrecept_total += Number($(this).val());
			   }
            
        });
        $(".mantance_intinal_value").each(function() {
            
			
			var e = document.getElementById('mantancediv');
			   if(e.style.display == 'none')
			   {
		 			caculcateinitialrecept_total +=0;
					
			   }
			   else
			   {
				  
					caculcateinitialrecept_total += Number($(this).val());
			   }
        });
        $("#recept_total_intinal").html(caculcateinitialrecept_total);
		 $("#recept_total_intinalx").val(caculcateinitialrecept_total);
    };
    caculcateshiprecept();
    caculcateinitialrecept();


    /******** send form with ajax ********/
    //callback handler for form submit
	//var validatorSettings = $.data($('#comp_client_form'), 'validator').settings;
//			validatorSettings.ignore = "";
   $("#btnready").click(function(e) {
    
        e.preventDefault(); //STOP default action
	
   // do the validation here
   

        if ($('#comp_client_form').validate().form()) {
			
			//ignore : "#clientdiv"
			
			 receptnumber = document.getElementById('receptid').value;
					 
					
			//noradiv
			 $('#noradiv').html2canvas({
				
            onrendered: function (canvas) {
             
				 $('#img_val1').val(canvas.toDataURL("image/png"));
				 	 mydata=document.getElementById('img_val1').value;
					
	
					$.ajax({
						  url: "saveimage.php?",
						  type: "post",
						  data: {name: mydata},
						  success: function(data){
							//alert(data);  
							$("#img_val").val(data);
							
						$.ajax({
						 url: 'billreceiptAjaxController.php?do=insertimage&imageval='+data+'&receptnumber='+receptnumber,
						  type: "post",
						  data: {name: mydata},
						   success: function(data){
							//alert(data);  
						   }
							});
						return true;	
							
					  }  
					}); 
		
            }
        });
			
           var postData = $("#comp_client_form").serializeArray();
            var formURL = $("#comp_client_form").attr("action");
            $('#allbillresult').html('<div class="ajaxsty"><img src="../views/default/assets/img/loading.gif" class="ajaximgs"/></div>');
            $.ajax({
                url: formURL,
                type: "POST",
                data: postData,
                success: function(data, textStatus, jqXHR)
                {
					//window.location = "?do=sucess";
                    if (data == '1') {
                      // $('#allbillresult').load("billreceiptController.php?do=sucess");
						                    //window.location = "?do=sucess";

                    } else {
                       // alert(data);
					  window.location = "?do=sucess";
                    }
                },
                error: function(jqXHR, textStatus, errorThrown)
                {
                    //if fails
                    alert(0);
                    window.location = "?do=error";
                }
			
            });
			
        
		
		
		// submit once return true
		$(this).click(function(e) {
            $(this).hide();
			
        });
	
		}
        e.unbind(); //unbind. to stop multiple form submit.
        return false;
		
    });
	
	
// submit more than once return false

    /*********** submit form ***************/
	
	$("#btnready1").click(function(e) {
 
        e.preventDefault(); //STOP default action
	
   // do the validation here
   

        if ($('#comp_client_form').validate().form()) {
			
			//ignore : "#clientdiv"
			
			 receptnumber = document.getElementById('receptid').value;
					 
				
			//noradiv
			 $('#noradiv').html2canvas({
				
            onrendered: function (canvas) {
             
				 $('#img_val1').val(canvas.toDataURL("image/png"));
				 	 mydata=document.getElementById('img_val1').value;
					
	
					$.ajax({
						  url: "saveimage.php?",
						  type: "post",
						  data: {name: mydata},
						  success: function(data){
							//alert(data);  
							$("#img_val").val(data);
							
						$.ajax({
						 url: 'billreceiptAjaxController.php?do=insertimage&imageval='+data+'&receptnumber='+receptnumber,
						  type: "post",
						  data: {name: mydata},
						   success: function(data){
							//alert(data);  
						   }
							});
						return true;	
							
					  }  
					}); 
		
            }
        });
			
           var postData = $("#comp_client_form").serializeArray();
            var formURL = $("#comp_client_form").attr("action");
            $('#allbillresult').html('<div class="ajaxsty"><img src="../views/default/assets/img/loading.gif" class="ajaximgs"/></div>');
            $.ajax({
                url: formURL,
                type: "POST",
                data: postData,
                success: function(data, textStatus, jqXHR)
                {
					//window.location = "?do=sucess";
                    if (data == '1') {
                      // $('#allbillresult').load("billreceiptController.php?do=sucess");
						                    //window.location = "?do=sucess";

                    } else {
                       //alert(data);
					
					 window.location.replace("receiptController.php?do=addrecept&receptid="+receptnumber);
                    }
                },
                error: function(jqXHR, textStatus, errorThrown)
                {
                    //if fails
                    alert(0);
                    window.location = "?do=error";
                }
			
            });
			
        
		
		
		// submit once return true
		$(this).click(function(e) {
            $(this).hide();
			
        });
		
        e.unbind(); //unbind. to stop multiple form submit.
        return false;
		}
		
    });
});


