
$(document).ready(function() {
	
	
  $("input:text,textarea").focus(function(){
   // $(this).css("background-color","#cccccc");
  });
  $("input:text,textarea").blur(function(){
  //  $(this).css("background-color","#ffffff");
  });
$("#ex_excel").hide();
$("#ex_pdf").hide();
$( "#from" ).datepick();
$( "#to" ).datepick();

 
$("#New").click(function() {

  positionedPopup("jobentry.php",1200,700)

}); 


 
 $("#search").click(function()  {
fetch_result(0,null)
		   

    });
	
	
	$('#ex_pdf').click(function () {
	$('#transperent').show();
	$.post('book_export_pdf.php', $('#form_bookdetails table  tr[filtermatch!="false"]').find("select, textarea, input").serialize(),function () {
			$('#transperent').hide();
				window.open("booking_det.pdf","mywindow","menubar=1,resizable=1,width=870,height="+640+",left=250,top=80");

	});
});	

					$("#ex_excel").click(function() {
						//$('.filters').hide();			
						$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='book_export_excel.php?type=book_det' style='display:none' id='ReportTableData'><input type='text' name='tableData' value='"+data+"' ></form>");
 $('#ReportTableData').submit().remove();
$('#excel_interstep').html('');
	});



$('.sidemenu_hide').click(function() {

$( ".left-panel" ).hide( "slow", function() {
$(".right-panel").animate({width:'100%'},100);});
$(this).hide('slow');
$('.sidemenu_show').show()
 });


$('.sidemenu_show').click(function() { 
$(".right-panel").animate({width:'80%'},100,function(){
$( ".left-panel" ).show( "slow");});
$(this).hide('slow');
$('.sidemenu_hide').show()
 });



$("#pendingRawMatList .dragDown,#penrmlist").click(function()
  {
    var divHeight = 0;
    var div = $("#pendingRawMatList .dataDiv");
    if (div.hasClass('show'))
    {
      div.html('');
      divHeight = 0;
      div.animate(
      {
        height: divHeight
      }, 1000, function()
      {
		  if (!div.hasClass('show'))
		  {
			div.addClass('show');
		  }
		  else
		  {
			div.removeClass('show');
		  }
      });
    }
    else
    {
      $.post('pendingjob.php',
      {
        'type': 'pendingproduct','agent':$('#agentcode').val()
      }, function(html)
      {
        if (html == 0)
        {
          divHeight = 20;
          div.html('No data.');
        }
        else
        {
          divHeight = 300;
          div.html(html);
		   $(".dataDiv table").tablesorter({
			theme: 'default',
			widthFixed: false,
			showProcessing: true,
			//headerTemplate : '{content} {icon}', // Add icon for various themes

			widgets: ['filter',  'stickyHeaders'  ],
    		widgetOptions : {
			filter_searchDelay: 300,
			stickyHeaders_attachTo : '.dataDiv'
			}
			});
        }
        div.animate(
        {
          height: divHeight
        }, 1000, function()
        {
          if (!div.hasClass('show'))
          {
            div.addClass('show');
          }
          else
          {
            div.removeClass('show');
          }
        });
      });
    }
  });

});//document ends




function fetch_result(type,next_rec)
{
		// next_rec=0;	 
        var from_date    = $("#from").val();
		        var to_date    = $("#to").val();
				var sortby    = $("#sortby").val();
				var searchtype = $("#searchtype").val();
		        var bookingtype    = $("#bookingtype").val();
				var cmp_key=$("#cmpsort").val();

        // forming the queryString
		if(type==0)
		  var data = 'from_date='+ from_date+'&to_date='+to_date+'&bookingtype='+bookingtype+'&cmp_key='+cmp_key+'&sortby='+sortby+'&searchtype='+searchtype;
		else if(type==1)
        var data = 'from_date='+ from_date+'&to_date='+to_date+'&bookingtype='+bookingtype+'&cmp_key='+cmp_key+'&next_rec='+next_rec+'&sortby='+sortby+'&searchtype='+searchtype+'&tot_rec='+$("#tot_rec").text();
      
       
            // ajax call
			$('#transperent').show();
            $.ajax({
                type: "POST",
                url: "jobreg_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>col1</th><th>col1</th><th>col4</th><th>col5</th><th>col6</th><th>col7</th><th>col6</th><th>col7</th><th>col8</th><th filter-type='ddl' >col9</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;
	}
var winPop;

function positionedPopup(url,width,height){
	
	 if(winPop&&!winPop.closed&&winPop.document.location.href!=url){  //checks to see if window is open  
    winPop.close();  
  } 
 winPop =window.open (url,"mywindow","menubar=1,resizable=1,width="+width+",height="+height+",left=250,top=0");
  if(winPop&&winPop.document.location.href==url)
winPop.focus();

}

	
/*	function getCSVData(){
		 $("#form_bookdetails table tr:eq(1)").hide();
 var csv_value=$('#form_bookdetails table').table2CSV({delivery:'value'});
 $("#csv_text").val(csv_value);	
 
  $("#form_bookdetails table tr:eq(1)").show();
}*/

function show_next_rec()
{
	$('.linkspages').click(function () {
		$('.linkspages').css('color','black');
		$(this).css('color','red');
	fetch_result(1,$(this).attr('id'))
	});
	
	}
function search_bybookid(event,val){
if(event.keyCode==13){
positionedPopup('carrental_entry.php?bookid='+val,660);
return false;
}
	
}

/*function sticky_relocate() {
    var window_top = $(window).scrollTop();
    var div_top = $('#sticky-anchor').offset().top;
    if (window_top > div_top) {
        $('#sticky').addClass('stick');
    } else {
        $('#sticky').removeClass('stick');
    }
}

$(function () {
    $(window).scroll(sticky_relocate);
    sticky_relocate();
});*/




	
