function media_main_validation()
{
   var in_jobcard_no_media = document.getElementById("in_jobcard_no_media").value;
	if(in_jobcard_no_media=='')
	{
		document.getElementById("in_jobcard_no_media_validation_div").innerHTML="* This field is Required.";
		document.getElementById("in_jobcard_no_media").focus();
		return false;
	}
	 var total_net = document.getElementById("total_net").value;
	if(total_net==0.00)
	{
	alert("Please fill the sublist");
		document.getElementById("roll_name").focus();
		return false;
	}
}

function media_entry_sub_validation()
{
   var in_jobcard_no_media = document.getElementById("in_jobcard_no_media").value;
	if(in_jobcard_no_media=='')
	{
		document.getElementById("in_jobcard_no_media_validation_div").innerHTML="* This field is Required.";
		document.getElementById("in_jobcard_no_media").focus();
		return false;
	}
	
	 var media_particular = document.getElementById("media_particular").value;
	if(media_particular=='')
	{
		document.getElementById("media_particular_validation_div22").innerHTML="* This field is Required.";
		document.getElementById("media_particular").focus();
		return false;
	}
	 var width1 = document.getElementById("width1").value;
	if(width1=='')
	{
		document.getElementById("media_width_validation_div").innerHTML="* This field is Required.";
		document.getElementById("width1").focus();
		return false;
	}
	 var height1 = document.getElementById("height1").value;
	if(height1=='')
	{
		document.getElementById("media_height_validation_div").innerHTML="* This field is Required.";
		document.getElementById("height1").focus();
		return false;
	}
	 var plan_qty = document.getElementById("plan_qty").value;
	if(plan_qty=='')
	{
		document.getElementById("media_paln_qty_validation_div").innerHTML="* This field is Required.";
		document.getElementById("plan_qty").focus();
		return false;
	}
	var plan_qty = document.getElementById("plan_qty").value;
		var order_qty = document.getElementById("order_qty").value;
	if((parseFloat(plan_qty)>parseFloat(order_qty)))
	{
		alert("Check The quantity");

		document.getElementById("plan_qty").focus();
		return false;
	}
	 var roll_name = document.getElementById("roll_name").value;
	
	if(roll_name=='')
	{
		document.getElementById("in_jobcard_roll_media_validation_div").innerHTML="* This field is Required.";
		document.getElementById("roll_name").focus();
		return false;
	}
	
	
	 var req_by = document.getElementById("req_by").value;
	if(req_by=='')
	{
		document.getElementById("media_req_by_validation_div").innerHTML="* This field is Required.";
		document.getElementById("req_by").focus();
		return false;
	}
	var steps = document.getElementById("steps").value;
	if(steps=='')
	{
		document.getElementById("media_steps_validation_div").innerHTML="* This field is Required.";
		document.getElementById("steps").focus();
		return false;
	}
	
		var gap = document.getElementById("gap").value;
	if(gap=='')
	{
		document.getElementById("media_gap_validation_div").innerHTML="* This field is Required.";
		document.getElementById("gap").focus();
		return false;
	}
// 	else 
// 	{
// 		document.getElementById("media_gap_validation_div").innerHTML="*Enter the Correct Gap Value.";
// 		document.getElementById("gap").focus();
// 		return false;
// 	}
	
	

	
}
function media_entry_qty_validation(order_qty,plan_qty)
{
  // alert(order_qty);
  // alert(plan_qty);
	var plan_qty = document.getElementById("plan_qty").value;
		var order_qty = document.getElementById("order_qty").value;
	if((parseFloat(plan_qty)>parseFloat(order_qty)))
	{
		alert("Check The Order Quantity..!");

		$('#plan_qty').val('');
		return false;
	}
}
function get_roll_size(roll_name)
{
var matches = roll_name.replace(/[^0-9]+/ig,"");
document.getElementById("roll_size").value=matches;
}
function get_mtrs_width(width)
{ var mtr=width*2.54;
var tot_mtr=(mtr/100);
document.getElementById("width_hid").value=tot_mtr;
}
function get_mtrs_height(height)
{ var hmtr=height*2.54;
var htot_mtr=(hmtr/100);
document.getElementById("height_hid").value=htot_mtr;
}
function get_tot_steps(plan_qty,steps,tot_qty_hid,tot_qty_hid1)
{ 
	//alert(tot_qty_hid+plan_qty+steps);
//if(plan_qty!='' && steps!='' && tot_qty_hid!=''){
if(plan_qty!='' && steps!=''){
var tot_step=(plan_qty/steps);
}
else{
var tot_step=0;	
}
//alert(tot_step);
var tot_qty_hid_float = parseFloat(tot_qty_hid);
var tot_qty_hid_float = parseFloat(tot_qty_hid1);
// if (!isNaN(tot_qty_hid_float)) {
//     if (tot_step <= tot_qty_hid_float) {
//      //   alert(tot_step.toFixed(2));
//     } else {
//       alert("Plan Qty should less then actual qty is "+tot_qty_hid_float);
//     }
// } else {
//     console.error('tot_qty_hid is not a valid number:', tot_qty_hid);
// }
var t = tot_step.toFixed(2);
//alert(t);
document.getElementById("total_steps").value=t;
//}
}
function get_gap_mtr(total_steps,gap)
{ 
var mtr=(total_steps*gap);
var gtot_mtr=(mtr/39.37);
var ts=gtot_mtr.toFixed(2);
document.getElementById("gap_mtr").value=ts;
}

// function get_total_steps_used1(req_by,roll_name,width,height)
// {
// 	alert(roll_name);
// 	if(req_by=='Width'){
// 	 var a1=Number(width);
	 
// 	 var a=(roll_name/a1);
// 	}
// 	else if(req_by=='Height'){
// 		 var a1=Number(height);
	 
// 	 var a=(roll_name/a1);
// 	}
// 	else{
// 		var a=0;
// 	}
	
// 	var e=a.toFixed(2);
	
// 	document.getElementById("steps_used").value=e;

	
// }
function get_total_steps_used(req_by, roll_name, width, height) {
   // alert(roll_name);
    // Extracting the part before hyphen
    var parts = roll_name.split("-");
    var roll_number = parts[0];
    // alert(roll_name);
    if (req_by === 'Width') {
        var a1 = Number(width);
        var a = (roll_number / a1);
    } else if (req_by === 'Height') {
        var a1 = Number(height);
        var a = (roll_number / a1);
    } else {
        var a = 0;
    }
    var e = a.toFixed(2);
    document.getElementById("steps_used").value = e;
    
    var batch_no = $("#batch_no").val();
   // alert(batch_no);
    jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/tot_qty_hid.php",
		data: "roll_name="+roll_name+"&batch_no="+batch_no,
		success: function(msg){ 
		    //alert(msg);
		    jQuery("#tot_qty_hid").val(msg); 
	    }
	});

	jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/tot_qty_hid1.php",
		data: "roll_name="+roll_name+"&batch_no="+batch_no,
		success: function(msg){ 
		    //alert(msg);
		    jQuery("#tot_qty_hid1").val(msg); 
	    }
	});

}

function get_total_steps(req_by, roll_name, width, height) {
    var parts = roll_name.split("-");
    var roll_number = Number(parts[0]);

    var a = 0;
    if (req_by === 'Width') {
        var a1 = Number(width);
        a = roll_number / a1;
    } else if (req_by === 'Height') {
        var a1 = Number(height);
        a = roll_number / a1;
    }

    var roundedValue = Math.floor(a); // 👈 this will take only the integer part, no decimals
    document.getElementById("steps").value = roundedValue;

    // Immediately call your dependent function now that 'steps' is updated
    get_tot_steps(
        document.getElementById("plan_qty").value,
        roundedValue
        // document.getElementById("tot_qty_hid").value,
        // document.getElementById("tot_qty_hid1").value
    );
}
	





function get_batch_no(roll_name,jobcard_no)
{ //alert(roll_name+jobcard_no);
    jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/batch_no.php",
		data: "roll_name="+roll_name+"&jobcard_no="+jobcard_no,
		success: function(msg){
		    jQuery("#batch_no_divs").html(msg); 
	    }
	});
}
function get_tot_req(req_by,width_hid,height_hid,total_steps,gap_mtr)
{
	
	if(req_by=='Width')
	{ 
	var a=((total_steps*height_hid)+Number(gap_mtr));
	
	
	
	}
	else if(req_by=='Height')
	{ 
	var a=((total_steps*width_hid)+Number(gap_mtr));
	
	}
	else{
		var a=0;
	}
	
	var d=a.toFixed(2);
	document.getElementById("total_req").value=d;

	
}

function get_gap_no(gap_no)
{
	
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/gapinterval.php",
		data: "gap_no="+gap_no,
		success: function(msg){
		
			if (msg==1){
				alert(" Enter the correct gap value");
		
		document.getElementById("gap").focus();
			
		$('#gap').val('');
	
		return false;
			}
			
			
			
	}
	});
}
function calculate_total_steps_and_update(req_by, roll_name, width, height, gap) {
    var parts = roll_name.split("-");
    var roll_number = Number(parts[0]);

    var a = 0;
    if (req_by === 'Width') {
        var a1 = Number(width);
        a = roll_number / a1;
    } else if (req_by === 'Height') {
        var a1 = Number(height);
        a = roll_number / a1;
    }

    var roundedValue = Math.round(a);
    document.getElementById("total_steps").value = roundedValue;

    // Now trigger dependent calculations
    get_gap_mtr(roundedValue, gap);
    get_tot_req(
        document.getElementById("req_by").value,
        document.getElementById("width_hid").value,
        document.getElementById("height_hid").value,
        roundedValue,
        document.getElementById("gap_mtr").value
    );
}


function media_main_clear_validation(str)
{
	if(str!='')
	{
		document.getElementById("in_jobcard_no_media_validation_div").innerHTML="";
	}
}

function media_sub_clear_validation(str)
{
	if(str!='')
	{
		document.getElementById("in_jobcard_no_media_validation_div").innerHTML="";
	}
	if(str!='')
	{
		document.getElementById("media_particular_validation_div22").innerHTML="";
	}
	if(str!='')
	{
		document.getElementById("media_width_validation_div").innerHTML="";
	}
	if(str!='')
	{
		document.getElementById("media_height_validation_div").innerHTML="";
	}
	if(str!='')
	{
		document.getElementById("media_paln_qty_validation_div").innerHTML="";
	}
	if(str!='')
	{
		document.getElementById("in_jobcard_roll_media_validation_div").innerHTML="";
	}
			if(str!='')
	{
		document.getElementById("media_req_by_validation_div").innerHTML="";
	}
	if(str!='')
	{
		document.getElementById("media_steps_validation_div").innerHTML="";
	}
	if(str!='')
	{
		document.getElementById("media_gap_validation_div").innerHTML="";
	}
	
	
}



function media_sublist(jobcard_no,in_cust_name,media_date,roll_name,batch_no,media_particular,order_qty,plan_qty,gap,total_steps,req_by,total_req,width,fabric_type,created_by,job_types,entrydates,height,width_hid,height_hid,edit_id,action,start,end,steps_used,steps,gap_mtr,tot_qty_hid,tot_qty_hid1)
{
	var t1 = parseFloat(tot_qty_hid1);
	console.log(t1);
	var t2 = parseFloat(total_req);
	console.log(t2);
    var total_meeter_run = t1+t2;
// alert(batch_no+'tot'+total_req+'bath'+parseFloat(tot_qty_hid)+" "+tot_qty_hid1);
// alert(total_meeter_run);
    
	if((roll_name!='')&&(width!='')&&(height!='')&&(plan_qty!='')&&(gap!='')&&(steps!='')&&(req_by!='')&&(parseFloat(plan_qty)<=parseFloat(order_qty))&&((Number(start)<=gap)&&(Number(end) >= gap)))
{
	if(parseFloat(total_meeter_run)<=parseFloat(tot_qty_hid)){
 jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/media_entry_db.php",
		data: "jobcard_no="+jobcard_no+"&in_cust_name="+in_cust_name+"&media_date="+media_date+"&roll_name="+roll_name+"&batch_no="+batch_no+"&media_particular="+media_particular+"&order_qty="+order_qty+"&plan_qty="+plan_qty+"&gap="+gap+"&total_steps="+total_steps+"&steps_used="+steps_used+"&steps="+steps+"&gap_mtr="+gap_mtr+"&req_by="+req_by+"&total_req="+total_req+"&width="+width+"&fabric_type="+fabric_type+"&created_by="+created_by+"&job_types="+job_types+"&entrydates="+entrydates+"&height="+height+"&width_hid="+width_hid+"&height_hid="+height_hid+"&edit_id="+edit_id+"&action="+action,
		success: function(msg){
			jQuery("#media_sub_list_div").html(msg);
		document.getElementById("roll_name").value='';
		document.getElementById("batch_no").value='';
		document.getElementById("order_qty").value='';
		document.getElementById("plan_qty").value='';
		document.getElementById("gap").value='';
		document.getElementById("steps").value='';
		document.getElementById("total_steps").value='';
		document.getElementById("req_by").value='';
		document.getElementById("total_req").value='';
		
		document.getElementById("width1").value='';
	    document.getElementById("height1").value='';
		document.getElementById("width_hid").value='';
		document.getElementById("height_hid").value='';

		document.getElementById('add').value='ADD';
		
		document.getElementById("in_jobcard_no_media").focus();	
		document.getElementById("entrydates").focus();	
 }
		});

	}
	else{
		alert("The available stock for this batch number is low!!! " +(" with only "  + tot_qty_hid)+ " units in stock");
   // document.getElementById("media_paln_qty_validation_div").innerHTML="";
}
}
else
{
	media_entry_sub_validation();
} 
     

}

function mediaent_sublist_edit(edit_id,jobcard_no)
{ 
  jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/media_sublist.php",
		data: "edit_id="+edit_id+"&jobcard_no="+jobcard_no,
		success: function(msg){jQuery("#media_sub_list_div").html(msg);

 }
		});
}


function delete_sublist_media(id,jobcard_no)
{
	if(confirm('Are You Sure! You Want To Delete?'))
	{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/media_entry_db.php",
		data: "id="+id+"&jobcard_no="+jobcard_no+"&action=del_media_sublist",
		success: function(msg){jQuery("#media_sub_list_div").html(msg);
		document.getElementById("in_jobcard_no_media").focus();	
		document.getElementById("entrydates").focus();	
	}
	});
    }
}



function media_main_list(jobcard_no,entry_time,customer_name,style_no,created_by,style_name,cust_po_no,combo,jobtype,fabric_type,edit_id,action)
{
	if((jobcard_no!='')&&(total_net!=0.00))
{
	

if(confirm('Are You Sure! You Want To Confirm?'))
	{
   jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/media_entry_db.php",
		data: "jobcard_no="+jobcard_no+"&entry_time="+entry_time+"&customer_name="+customer_name+"&style_no="+style_no+"&created_by="+created_by+"&style_name="+style_name +"&cust_po_no="+cust_po_no+"&combo="+combo+"&jobtype="+jobtype+"&fabric_type="+fabric_type+"&main_edit_id="+edit_id+"&action="+action,
		success: function(msg){jQuery("#media_main_list_div").html(msg);
		alert(msg);
			if(edit_id!='')
			{
			window.location.href="main_fram.php?file=media_entry";
			}
			else
			{
		window.location.reload(true);
		    }
 }
		});
	}
}
else
{media_main_validation();
}
}

function get_customer_medias(in_jobcard_no_media)
{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/customer_name.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media,
		success: function(msg){jQuery("#media_customer").html(msg);
	}
	});
}


function get_created_by_media(in_jobcard_no_media)
{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/created_by.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media,
		success: function(msg){jQuery("#created_by_div").html(msg);
	}
	});
}

function get_style_no_media(in_jobcard_no_media)
{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/style_no.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media,
		success: function(msg){jQuery("#style_no_div").html(msg);
	}
	});
}

function get_style_name_media(in_jobcard_no_media)
{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/style_name.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media,
		success: function(msg){jQuery("#style_name_div").html(msg);
	}
	});
}

function get_customer_po_media(in_jobcard_no_media)
{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/cust_po_no.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media,
		success: function(msg){jQuery("#customer_po_div").html(msg);
	}
	});
}


function get_combo_media(in_jobcard_no_media)
{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/combo.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media,
		success: function(msg){jQuery("#combo_type_div").html(msg);
	}
	});
}


function get_job_type_media(in_jobcard_no_media)
{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/job_type.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media,
		success: function(msg){jQuery("#job_types_div").html(msg);
	}
	});
}


function get_fabric_types_media(in_jobcard_no_media)
{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/fabric_type.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media,
		success: function(msg){jQuery("#fabric_type_div").html(msg);
	}
	});
}

function getmediajobcard(in_jobcard_no_media)
{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/media_sublist.php",
		data: "jobcard_no="+in_jobcard_no_media,
		success: function(msg){jQuery("#media_sub_list_div").html(msg);
	}
	});
}



function get_particulars_media(in_jobcard_no_media)
{
  //  alert(in_jobcard_no_media);
//   document.getElementById("in_jobcard_no_media").removeAttribute("onclick");
//   console.log(in_jobcard_no_media);
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/particulars.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media,
		success: function(msg){
		  //  alert(msg);
		  //  console.log("msg");
		   jQuery("#media_partic_divs").html(msg);
	}
	});
}

function get_rolls_sub_media(in_jobcard_no_media,media_particular)
{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/rolls.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media+"&media_particular="+media_particular,
		success: function(msg){jQuery("#rolls_div").html(msg);
	}
	});
}


function get_height_sub_media(in_jobcard_no_media,media_particular)
{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/meters.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media+"&media_particular="+media_particular,
		success: function(msg){jQuery("#kg_height_div").html(msg);
	}
	});
}


function get_qty_sub_media(in_jobcard_no_media,media_particular)
{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/qty.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media+"&media_particular="+media_particular,
		success: function(msg){jQuery("#qty_sub_divs").html(msg);
	
	}
	});
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/rolls.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media+"&media_particular="+media_particular,
		success: function(msg){jQuery("#width1_sub_divs").html(msg);
	
	}
	});
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/roll_height.php",
		data: "in_jobcard_no_media="+in_jobcard_no_media+"&media_particular="+media_particular,
		success: function(msg){jQuery("#height1_sub_divs").html(msg);
	
	}
	});
}


function delete_media_main(jobcard_no)
{
	// alert(jobcard_no);
	if(confirm('Are You Sure! You Want To Delete?'))
	{
		jQuery.ajax({
		type: "POST",
		url: "consumable/media_entry/media_entry_db.php",
		data: "jobcard_no="+jobcard_no+"&action=delete_media",
		success: function(msg){jQuery("#media_main_list_div").html(msg);
		window.location.reload(true);
	}
	});
    }
}



