$(document).ready(function(){
	$('.reason-div').hide();
});

$('#paarAvailable').change(function(){
	// alert("slkjsl");
	var paarAvailable = $('#paarAvailable').val();
	if (paarAvailable == "Yes") {
		$('#paarImage').removeAttr('disabled');
		$('#paarValue').removeAttr('disabled');
		$('#paarValue').attr('required', 'required');
		$('#paarNumber').removeAttr('readonly');
		$('#paarNumber').attr('required', 'required');
		$('#paarNumber').removeClass('disabled');
		
	}else{
		$('#paarImage').attr('disabled', 'disabled');
		$('#paarImage').addClass('disabled');
		
		$('#paarValue').attr('disabled', 'disabled');
		$('#paarValue').addClass('disabled', 'disabled');
		$('#paarNumber').attr('readonly', 'readonly');
		$('#paarNumber').addClass('disabled', 'disabled');
		$(input[name="paarImage"]).css('background', 'gray');
		
	}
});


$('#containerSize').change(function(){
	 //alert("slkjsl");
	var containerSize = $('#containerSize').val();
	if (containerSize == "20ft") {
		$('#container').removeAttr('readonly');
		$('#container').attr('required', 'required');
		$('#container').removeClass('disabled');
		$('#container').removeAttr('disabled');
		
 		$('#cont20').attr('required', 'required');
		$('#cont20').removeAttr('readonly');
		$('#cont20').removeClass('disabled');
		$('#cont20').removeAttr('disabled');
		
		$('#cont40').attr('disabled', 'disabled');
		$('#cont40').removeAttr('required');
		$('#cont40').addClass('disabled');
	}else if (containerSize == "40ft"){
		$('#container').removeAttr('readonly');
		$('#container').attr('required', 'required');
		$('#container').removeClass('disabled');
		$('#container').removeAttr('disabled');
		
 		$('#cont40').attr('required', 'required');
		$('#cont40').removeAttr('readonly');
		$('#cont40').removeClass('disabled');
		$('#cont40').removeAttr('disabled');
		
		
		$('#cont20').attr('disabled', 'disabled');
		$('#cont20').removeAttr('required');
		$('#cont20').addClass('disabled');
	}else if (containerSize == "20f & 40ft"){
		$('#container').removeAttr('readonly');
		$('#container').attr('required', 'required');
		$('#container').removeClass('disabled');
		$('#container').removeAttr('disabled');
		
 		$('#cont40').attr('required', 'required');
		$('#cont40').removeAttr('readonly');
		$('#cont40').removeClass('disabled');
		$('#cont40').removeAttr('disabled');
		
		$('#cont20').attr('required', 'required');
		$('#cont20').removeAttr('readonly');
		$('#cont20').removeClass('disabled');
		$('#cont20').removeAttr('disabled');
	}else if (containerSize == "No Container"){
		$('#container').attr('disabled', 'disabled');
		$('#container').removeAttr('required');
		$('#container').addClass('disabled');
		
 		$('#cont40').attr('disabled', 'disabled');
		$('#cont40').removeAttr('required');
		$('#cont40').addClass('disabled');
		
		$('#cont20').attr('disabled', 'disabled');
		$('#cont20').removeAttr('required');
		$('#cont20').addClass('disabled');
	}


});


$('#decision').change(function(){
	var decision = $('#decision').val();
	if (decision == "Rejected") {
		$('.reason-div').show();
		$('.reason').removeAttr('disabled');
	}else{
		$('.reason').attr('disabled', 'disabled');
		$('.reason-div').hide();
	}
});


// to update password
$('#update').click(function(){
	var oldpass = $('#oldpass').val();
	var newpass = $('#newpass').val();
	var newpass2 = $('#newpass2').val();
	if(newpass !== newpass2){
		$('#feed').html("<center><font color=red>Password do not Match</font></center>");
	}else{
		$('#feed').html("<center><font color=red>Processing</font></center>");
		$.post('backend.php', { oldpass: oldpass, newpass: newpass },function(data) //send id to updating page
				 {		
					$('#feed').html("<center>"+data+"</center>");
				 });
	}
});

$('#basicForm').submit(function (e) {   
    e.preventDefault();
    $('.loader').addClass('spinner-grow text-light spinner-border-sm spinner-grow-sm');
	$('.tab1-proceed').html("Processing");
    $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            enctype: 'multipart/form-data',
            data: new FormData(this),
            processData: false,
            contentType: false,
            success: function (data) {
                //console.log('Thank God it worked!');
				var billCheck = data.trim();
				//console.log(billCheck);
				if(billCheck=="bill exist"){
                	$('#tab1').click(); 
					$('.swalDefaultError').click();
                	alert("Bill of lading already exist");
					$('.loader').removeClass('spinner-grow text-light spinner-border-sm spinner-grow-sm');
					$('.tab1-proceed').html("Proceed <i class='fa fa-arrow-right'></i>");
					
				}else{
					$("#basicForm :input").prop("disabled", true);
					$('#tab2').click();
					$('.swalDefaultSuccess').click();
					$('.loader').removeClass('spinner-grow text-light spinner-border-sm spinner-grow-sm');
					$('.tab1-proceed').html("Proceed <i class='fa fa-arrow-right'></i>");
				}
                
            }
        }
    );
    // alert("slkjfsl");
});

 //NON STANDARD VIN
$('#addV').submit(function (e) {   
    e.preventDefault();
    $('.loader').addClass('spinner-grow text-warning spinner-border-sm spinner-grow-sm');
	$('.tab2-adding').html("Adding Vehicle");
    $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            enctype: 'multipart/form-data',
            data: new FormData(this),
            processData: false,
            contentType: false,
            success: function (data) {
                $('.loader').removeClass('spinner-grow text-warning spinner-border-sm spinner-grow-sm');
				$('.tab2-adding').html("Add Vehicle</span>");
            	$('#inserted').html(data).fadeIn().delay(2000);
                // console.log('Thank God it worked!');
                 $('.file-caption-name').attr('title', '');
                 $('.file-caption-name').text('');
                 $('.fileinput-remove').click();
                document.getElementById("addV").reset();
               
                var msg = $.trim(data);
                if (msg=="Invalid Application") {
                	$('.swalDefaultError').click();
                	// alert(data);
                	$('#tab1').click();
                }else{
                	$('.swalDefaultSuccess').click();
                	// alert(data);
                	$('#tab2').click();
                }
                $('.finalSubmission').removeAttr('disabled');
                $('.btn-preview').removeClass('disabled');
                $('.del').click(function(){

					var removeId = $(this).attr('data-del');
					// alert(removeId);
						$.post('backend.php', { del: removeId},function(data) //send id to updating page
								 {		
									$('.swalDefaultSuccess').click();
								 });
					var parent = $(this).parent().parent().remove();
					// alert(parent);
				});

            }
        }
    );
});

//STANDARD VIN
$('#addVStd').submit(function (e) {   
    e.preventDefault();
    $('.loader').addClass('spinner-grow text-warning spinner-border-sm spinner-grow-sm');
	$('.tab3-adding').html("Adding Vehicle");
    $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            enctype: 'multipart/form-data',
            data: new FormData(this),
            processData: false,
            contentType: false,
            success: function (data) {
                $('.loader').removeClass('spinner-grow text-warning spinner-border-sm spinner-grow-sm');
				$('.tab3-adding').html("Add Vehicle</span>");
            	$('#insertedStd').html(data).fadeIn().delay(2000);
                // console.log('Thank God it worked!');
                document.getElementById("addVStd").reset();
                var msg = $.trim(data);
                if (msg=="Invalid Application") {
                	$('.swalDefaultError').click();
                	// alert(data);
                	$('#tab1').click();
                }else{
                	$('.swalDefaultSuccess').click();
                	// alert(data);
                	$('#tab3').click();
                }
                
                $('.delStd').click(function(){

					var removeId = $(this).attr('data-del');
					// alert(removeId);
						$.post('backend.php', { delStd: removeId},function(data) //send id to updating page
								 {		
									$('.swalDefaultSuccess').click();
								 });
					var parent = $(this).parent().parent().remove();
					// alert(parent);
				});

            }
        }
    );
});

// OTHER ITEMS
$('#addOthers').submit(function (e) {   
    e.preventDefault();
    $('.loader').addClass('spinner-grow text-warning spinner-border-sm spinner-grow-sm');
	$('.tab4-adding').html("Adding Item");
    $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            enctype: 'multipart/form-data',
            data: new FormData(this),
            processData: false,
            contentType: false,
            success: function (data) {
                $('.loader').removeClass('spinner-grow text-warning spinner-border-sm spinner-grow-sm');
				$('.tab4-adding').html("Add Item</span>");
            	$('#insertedOthers').html(data).fadeIn().delay(2000);
                // console.log('Thank God it worked!');
                document.getElementById("addOthers").reset();
                $('#tab4').click();
                var msg = $.trim(data);
                if (msg=="Invalid Application") {
                	$('.swalDefaultError').click();
                	// alert(data);
                }else{
                	$('.swalDefaultSuccess').click();
                	// alert(data);
                }
                
                $('.delOthers').click(function(){

					var removeId = $(this).attr('data-del');
					// alert(removeId);
						$.post('backend.php', { delOthers: removeId},function(data) //send id to updating page
								 {		
									$('.swalDefaultSuccess').click();
     
								 });
					var parent = $(this).parent().parent().remove();
					// alert(parent);
				});

            }
        }
    );
});

//DELETING NON STANDARD FROM DRAFT ONLOAD
$('.del').click(function(){

	var removeId = $(this).attr('data-del');
	// alert(removeId);
		$.post('backend.php', { del: removeId},function(data) //send id to updating page
				 {		
					$('.swalDefaultSuccess').click();
				 });
	var parent = $(this).parent().parent().remove();
	// alert(parent);
});
//

//DELETING STANDARD FROM DRAFT ONLOAD
$('.delStd').click(function(){

	var removeId = $(this).attr('data-del');
	// alert(removeId);
		$.post('backend.php', { delStd: removeId},function(data) //send id to updating page
				 {		
					$('.swalDefaultSuccess').click();
				 });
	var parent = $(this).parent().parent().remove();
	// alert(parent);
});

// DELETING OTHERS FROM DRAFT
$('.delOthers').click(function(){

	var removeId = $(this).attr('data-del');
	// alert(removeId);
		$.post('backend.php', { delOthers: removeId},function(data) //send id to updating page
				 {		
					$('.swalDefaultSuccess').click();

				 });
	var parent = $(this).parent().parent().remove();
	// alert(parent);
});
// $('#swtalert').click(function(){
// 	swal("Good job!", "You clicked the button!", "success");
// });

//TERMINAL SELECTOR BASE ON COMMAND

$('#command').change(function(){
	var commandId = $('#command').val();
    $.post('backend.php', { commandId: commandId},function(data) //send id to updating page
				 {		
					$('#terminal').html(data);

				 });
	
});


// SUPER ADMIN SCRIPTS
$('#designation').change(function(){
	var designation = $('#designation').val();
// 	alert(designation);
	if (designation == "OC Valuation") {
	   $('.signature-div').show();
		$('#signature').removeAttr('disabled');
		$('#terminal').attr('disabled', 'disabled');
		$('.terminal-div').hide();
		
	}else if(designation == "DC Valuation"){
    	$('.signature-div').show();
		$('#signature').removeAttr('disabled');
		$('#terminal').attr('disabled', 'disabled');
		$('.terminal-div').hide();
		
	}else{ //comptroller
	    $('.signature-div').show();
	    $('#terminal').attr('disabled', 'disabled');
		$('.terminal-div').hide();
	}
});


//L&P SCRIPTS
$('#documentValidity').change(function(){
	var decision = $('#documentValidity').val();
	if (decision == "Expiring") {
		$('.expiryDate-div').show();
		$('.expiryDate').removeAttr('disabled');
	}else{
		$('.expiryDate').attr('disabled', 'disabled');
		$('.expiryDate-div').hide();
		$('.expiryDate').removeAttr('required');
		$('#expiryDate').removeAttr('required');
	}
});

$('#otherLP').change(function(){
	var decision = $('#otherLP').val();
	if (decision == "Yes") {
		$('.otherLPno-div').show();
		$('.otherLPno').removeAttr('disabled');
		$('otherLPno').removeAttr('required');
	}else{
		$('.otherLPno').attr('disabled', 'disabled');
		$('.otherLPno-div').hide();
	}
});

$('#lpForm').submit(function (e) {   
    e.preventDefault();
    $('.loader').addClass('spinner-grow text-light spinner-border-sm spinner-grow-sm');
	$('.tab1-proceed').html("Processing");
    $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            enctype: 'multipart/form-data',
            data: new FormData(this),
            processData: false,
            contentType: false,
            success: function (data) {
                //console.log('Thank God it worked!');
				var tinCheck = data.trim();
				//console.log(billCheck);
				if(tinCheck=="TIN or Email Exist"){
                	$('#tab1').click(); 
					$('.swalDefaultError').click();
                	alert("TIN or Email exist");
					$('.loader').removeClass('spinner-grow text-light spinner-border-sm spinner-grow-sm');
					$('.tab1-proceed').html("Proceed <i class='fa fa-arrow-right'></i>");
					
				}else{
					$("#lpForm :input").prop("disabled", true);
					$('#tab2').click();
					$('.swalDefaultSuccess').click();
					$('.loader').removeClass('spinner-grow text-light spinner-border-sm spinner-grow-sm');
					$('.tab1-proceed').html("Proceed <i class='fa fa-arrow-right'></i>");
				}
                
            }
        }
    );
    // alert("slkjfsl");
});

 //DOCUMENTS UPLOAD
$('#addDoc').submit(function (e) {   
    e.preventDefault();
    $('.loader').addClass('spinner-grow text-warning spinner-border-sm spinner-grow-sm');
	$('.tab2-adding').html("Uploading Document");
    $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            enctype: 'multipart/form-data',
            data: new FormData(this),
            processData: false,
            contentType: false,
            success: function (data) {
                $('.loader').removeClass('spinner-grow text-warning spinner-border-sm spinner-grow-sm');
				$('.tab2-adding').html("Upload Document</span>");
            	$('#inserted').html(data).fadeIn().delay(2000);
                // console.log('Thank God it worked!');
                 $('.file-caption-name').attr('title', '');
                 $('.file-caption-name').text('');
                 $('.fileinput-remove').click();
                document.getElementById("addDoc").reset();
               
                var msg = $.trim(data);
                if (msg=="Invalid Application") {
                	$('.swalDefaultError').click();
                	// alert(data);
                	$('#tab1').click();
                }else{
                	$('.swalDefaultSuccess').click();
                	// alert(data);
                	$('#tab2').click();
                }
                $('.finalSubmission').removeAttr('disabled');
                $('.btn-preview').removeClass('disabled');
                $('.del').click(function(){

					var removeId = $(this).attr('data-del');
					// alert(removeId);
						$.post('backend.php', { del: removeId},function(data) //send id to updating page
								 {		
									$('.swalDefaultSuccess').click();
								 });
					var parent = $(this).parent().parent().remove();
					// alert(parent);
				});

            }
        }
    );
});


 //Agent UPLOAD
$('#addAgent').submit(function (e) {   
    e.preventDefault();
    $('.loader').addClass('spinner-grow text-warning spinner-border-sm spinner-grow-sm');
	$('.tab3-adding').html("Adding Agent");
    $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            enctype: 'multipart/form-data',
            data: new FormData(this),
            processData: false,
            contentType: false,
            success: function (data) {
                $('.loader').removeClass('spinner-grow text-warning spinner-border-sm spinner-grow-sm');
				$('.tab3-adding').html("Adding Agent</span>");
            	$('#inserted').html(data).fadeIn().delay(2000);
                // console.log('Thank God it worked!');
                 $('.file-caption-name').attr('title', '');
                 $('.file-caption-name').text('');
                 $('.fileinput-remove').click();
                document.getElementById("addAgent").reset();
               
                var msg = $.trim(data);
                if (msg=="Invalid Application") {
                	$('.swalDefaultError').click();
                	// alert(data);
                	$('#tab2').click();
                }else{
                	$('.swalDefaultSuccess').click();
                	// alert(data);
                	$('#tab3').click();
                }
                $('.finalSubmission').removeAttr('disabled');
                $('.btn-preview').removeClass('disabled');
                $('.del').click(function(){

					var removeId = $(this).attr('data-del');
					// alert(removeId);
						$.post('backend.php', { del: removeId},function(data) //send id to updating page
								 {		
									$('.swalDefaultSuccess').click();
								 });
					var parent = $(this).parent().parent().remove();
					// alert(parent);
				});

            }
        }
    );
});