
$(document).ready(function(){
	
	$('#addnew').click(function(e) {
        var row = $('#barcode table tbody tr:eq(0)').clone();
		 $('#barcode table tbody').append(row);
		 	
	$(".productcode").autocomplete(
      "../productreg/product_code_fill.php?mode=barcode",
      {
  			delay:10,
  			minChars:1,
  			matchSubset:1,
  			matchContains:1,
			
  			cacheLength:10,
			sortResults :false,
  			onItemSelect:function(row){
					var rowtable = $(this).closest('tr');
				if(row.data[1]!='undefined')
				rowtable.find('.barcodevalue').val(row.data[1]);
				if(row.data[4]!='undefined')
				rowtable.find('.Size').val(row.data[4]);
				if(row.data[1]!='undefined')
				rowtable.find('.sku').val(row.data[1]);
				if(row.data[1]!='undefined')
				rowtable.find('.qty').val(row.data[1]);
								
			},
			showResult:function(row,data) {
    	return row+"|"+data[0]+"|"+data[4];
  },
  			autoFill:false
  		});
    });
	
	
	
	
	$("#ex_excel").click(function() {

$temp_search=$('#demotable_table_wrap').clone();

$('#excel_interstep').html($temp_search);

$temp_search=$('#excel_interstep div tr[filtermatch="false"],#excel_interstep div .filters').remove();				

var data=$('#excel_interstep div').html();

$('body').prepend("<form method='post' action='barcode_export_excel.php?type=barcode_det' style='display:none' id='ReportTableData'><input type='text' name='tableData' value='"+data+"' ></form>");

$('#ReportTableData').submit().remove();

$('#excel_interstep').html('');

});
	
	$(".productcode").autocomplete(
      "../productreg/product_code_fill.php?mode=barcode",
      {
  			delay:10,
  			minChars:1,
  			matchSubset:1,
  			matchContains:1,
			
  			cacheLength:10,
			sortResults :false,
  			onItemSelect:function(row){
			var tempRow=$('*:focus').closest('tr');
				if(row.data[0]!='undefined')
				tempRow.find('.barcodevalue').val(row.data[0]);
				if(row.data[1]!='undefined')
				tempRow.find('.sku').val(row.data[1]);
				if(row.data[2]!='undefined')
				tempRow.find('.qty').val(row.data[2]);
				if(row.data[3]!='undefined')
				tempRow.find('.Size').val(row.data[3]);
				if(row.data[4]!='undefined')
				tempRow.find('.MRP').val(row.data[4]);
				
								
			},
			showResult:function(row,data) {
    	return row+"|"+data[0]+"|"+data[3];
  		},
  			
			autoFill:false
  		});	
		
		
		
 $('#showsetting').click(function(){	
 $("#generator").animate({
      width:'toggle'
    });	
	 });
	 
	 
	$("#search").click(function()  {
fetch_result(0,null)
		   

    }); 
	
	$("#searchpt").click(function()  {
fetch_searchpt(0,null)
		   

    }); 
	
});

function fetch_result(type,next_rec)
{
		var fromtype = $("#fromtype").val();
		var valueid  = $("#valueid").val();
		
        // forming the queryString
		if(type==0)
		  var data = 'fromtype='+fromtype+'&valueid='+valueid;
		else if(type==1)
        var data = 'next_rec='+next_rec+'&fromtype='+fromtype+'&valueid='+valueid+'&tot_rec='+$("#tot_rec").text();
		
            // ajax call
			$('#transperent').show();
            $.ajax({
                type: "POST",
                url: "barcode_fill.php",
               data:data,
               success: function(html){ // this happens after we get results
			   if($.trim(html)=="")
			   jSuccess('No record found in the given date range!!\nClick New to enter new duty');
			   else
			   {
			   var str=html.split('###');
                    $('#form_bookdetails').html(str[0]);
					if(next_rec==null)
					{
						$('#div_page').html('');
						}
					if($.trim(str[1])!='')
					{
					$('#div_page').html(str[1]);
					}

					 $('#demotable tr:even td textarea').css({'background-color':'#a5c4d8'});
					
						$('#demotable textarea').keydown(function(){
							return false;
						});
						
					  $("#demotable").createScrollableTable({
						  width: '100%',
						  height: '450'
					  });
					
					 var options = {
				clearFiltersControls: [$('#search')],
				enableCookies :false       
			};
			
			var bookingtype    = $("#bookingtype").val();
			$("#demotable").prepend("<thead><tr><th >col0</th><th >col0</th><th>col1</th><th>col1</th><th>col4</th><th>col5</th><th>col6</th><th>col6</th><th>col6</th></tr></thead>");
			
			$('#demotable').tableFilter(options);

				$('#demotable thead tr:eq(0)').remove();
				//if(next_rec==null)
				show_next_rec()	;
				$(".add").show();
				$(".ex_excel").show();
				$("#ex_pdf").show();
				//$('#demotable .filters td input:text').val('');
				// $('#form_bookdetails table th').css({'width':'20px'});
				 $('#demotable thead td').css({'width':'20px'});

				} 
				$('#form_bookdetails').css('border-bottom', '4px solid #52A8AF');
				$('#transperent').hide();
			   
             }//success ends
			 
            }); 
        return false;
}


function fetch_searchpt(type,next_rec)
{
		var fromtype = $("#fromtype").val();
		var valueid  = $("#valueid").val();
		
        // forming the queryString
		if(type==0)
		  var data = 'fromtype='+fromtype+'&valueid='+valueid;
		else if(type==1)
        var data = 'next_rec='+next_rec+'&fromtype='+fromtype+'&valueid='+valueid+'&tot_rec='+$("#tot_rec").text();
		
            // ajax call
			$('#transperent').show();
            $.ajax({
                type: "POST",
                url: "barcode_fill.php?mode=ptview",
               data:data,
               success: function(html){ // this happens after we get results
			   if($.trim(html)=="")
			   jSuccess('No record found in the given date range!!\nClick New to enter new duty');
			   else
			   {
			   var str=html.split('###');
                    $('#form_bookdetails').html(str[0]);
					if(next_rec==null)
					{
						$('#div_page').html('');
						}
					if($.trim(str[1])!='')
					{
					$('#div_page').html(str[1]);
					}

					 $('#demotable tr:even td textarea').css({'background-color':'#a5c4d8'});
					
						$('#demotable textarea').keydown(function(){
							return false;
						});
						
					  $("#demotable").createScrollableTable({
						  width: '100%',
						  height: '450'
					  });
					
					 var options = {
				clearFiltersControls: [$('#search')],
				enableCookies :false       
			};
			
			var bookingtype    = $("#bookingtype").val();
			$("#demotable").prepend("<thead><tr><th >col0</th><th >col1</th><th>col2</th><th>col3</th><th>col4</th><th>col5</th><th>col6</th><th>col7</th><th>col8</th><th >col9</th><th >col10</th><th>col11</th><th>col12</th><th>col13</th><th>col14</th><th>col15</th><th>col16</th><th>col17</th><th >col18</th><th >col19</th><th>col20</th><th>col21</th><th>col22</th><th>col23</th><th>col24</th><th>col25</th><th>col26</th><th>col27</th></tr></thead>");
			
			$('#demotable').tableFilter(options);

				$('#demotable thead tr:eq(0)').remove();
				//if(next_rec==null)
				show_next_rec1()	;
				$(".add").show();
				$(".ex_excel").show();
				$("#ex_pdf").show();
				//$('#demotable .filters td input:text').val('');
				// $('#form_bookdetails table th').css({'width':'20px'});
				 $('#demotable thead td').css({'width':'20px'});

				} 
				$('#form_bookdetails').css('border-bottom', '4px solid #52A8AF');
				$('#transperent').hide();
			   
             }//success ends
			 
            }); 
        return false;
}


function show_next_rec()
{
	$('.linkspages').click(function () {
		$('.linkspages').css('color','black');
		$(this).css('color','red');
	fetch_result(1,$(this).attr('id'))
	});
	
	}
	
	
function show_next_rec1()
{
	$('.linkspages1').click(function () {
		$('.linkspages1').css('color','black');
		$(this).css('color','red');
	fetch_searchpt(1,$(this).attr('id'))
	});
	
	}

 function generateBarcode(){
	 
	 
	 $.post('barcodegenerator.php', $('#generator1 input:text,#barcode table input:text,#form_bookdetails #demotable textarea').serialize(),function (html) {
		 console.log(html);
			$('#transperent').hide();
				window.open("barcodeoutput.pdf","mywindow","menubar=1,resizable=1,width=870,height="+640+",left=250,top=80");

	});

       /* var value = $("#barcodeValue").val();
        var btype = $("input[name=btype]:checked").val();
        var renderer = $("input[name=renderer]:checked").val();
        
		var quietZone = false;
        if ($("#quietzone").is(':checked') || $("#quietzone").attr('checked')){
          quietZone = true;
        }
		
        var settings = {
          output:renderer,
          bgColor: $("#bgColor").val(),
          color: $("#color").val(),
          barWidth: $("#barWidth").val(),
          barHeight: $("#barHeight").val(),
          moduleSize: $("#moduleSize").val(),
          posX: $("#posX").val(),
          posY: $("#posY").val(),
          addQuietZone: $("#quietZoneSize").val()
        };
        if ($("#rectangular").is(':checked') || $("#rectangular").attr('checked')){
          value = {code:value, rect: true};
        }
        if (renderer == 'canvas'){
          clearCanvas();
          $("#barcodeTarget").hide();
          $("#canvasTarget").show().barcode(value, btype, settings);
        } else {
          $("#canvasTarget").hide();
          $("#barcodeTarget").html("").show().barcode(value, btype, settings);
        }
		
		addDetails();*/
		
      }
	  
	  function addDetails(){
		  var lastDiv ='';
		   var width ='';
		    var height ='';
		  lastDiv = $("#barcodeTarget div").last().clone().html('MRP: 299.00');
		  $("#barcodeTarget").append(lastDiv);
		  lastDiv = $("#barcodeTarget div").last().clone().html('Style: FD-25-DD');
		  $("#barcodeTarget").append(lastDiv);
		  
		  lastDiv = $("#barcodeTarget div").last().clone().html('FreeDom Design');
		  $("#barcodeTarget").prepend(lastDiv);
		  width = lastDiv.width();
		  //$('#generator1:not(#barcodeTarget)').hide();
		  // $("#barcodeTarget").show();
		 // $("#barcodeTarget").print();
		  // $('body').show();
		Popup($('#barcodeTarget').html(),1000,750);
	  }
	  
	   function Popup(data,width,height) 
    {
        var mywindow = window.open('', 'my div', 'height=400,width=600');
        mywindow.document.write('<html><head><title>my div</title>');
        /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
        mywindow.document.write("</head><body style='width:"+width+";height:"+height+"'>");
	  	mywindow.document.write(data);
	  	//mywindow.document.write(data);

        mywindow.document.write('</body></html>');

        mywindow.document.close(); // necessary for IE >= 10
        mywindow.focus(); // necessary for IE >= 10

        //mywindow.print();
       // mywindow.close();

        return true;
    }
          
      function showConfig1D(){
        $('.config .barcode1D').show();
        $('.config .barcode2D').hide();
      }
      
      function showConfig2D(){
        $('.config .barcode1D').hide();
        $('.config .barcode2D').show();
      }
      
      function clearCanvas(){
        var canvas = $('#canvasTarget').get(0);
        var ctx = canvas.getContext('2d');
        ctx.lineWidth = 1;
        ctx.lineCap = 'butt';
        ctx.fillStyle = '#FFFFFF';
        ctx.strokeStyle  = '#000000';
        ctx.clearRect (0, 0, canvas.width, canvas.height);
        ctx.strokeRect (0, 0, canvas.width, canvas.height);
      }
      
      $(function(){
        $('input[name=btype]').click(function(){
          if ($(this).attr('id') == 'datamatrix') showConfig2D(); else showConfig1D();
        });
        $('input[name=renderer]').click(function(){
          if ($(this).attr('id') == 'canvas') $('#miscCanvas').show(); else $('#miscCanvas').hide();
        });
        //generateBarcode();
      });