jQuery(document).ready(function($) {
	'use strict';
	window.id = 0;
	$('.repeater').repeater({
		defaultValues: {
			'id': window.id,              
		},
		show: function () {
			$(this).slideDown();
			$('#cat-id').val(window.id);
		},
		hide: function (deleteElement) {
			if( confirm('Are you sure you want to delete?') ) {
				window.id--;
				$('#cat-id').val(window.id);
				$(this).slideUp(deleteElement);
			}
		},
		ready: function (setIndexes) {}
	});
    if( $(window).width() > 910) {        
        $( '#wpcsr-findrate .data-repeater:nth-child(1)' ).append( $('.dashicons-plus') );        
    }else {
        $( '#wpcsr-create-order' ).append( $('.submit-wrapper') );
        $( '#parcel-rate' ).insertAfter( '#parcel-info' );
    }
    var $window = $(window);
    function checkWidth() {
        var windowsize = $window.width();
        if (windowsize < 910) {
            $( '#parcel-rate' ).insertAfter( '#parcel-info' );
        }else {
            $( '#parcel-rate' ).insertAfter( '#parcel-content' );
        }
        if( windowsize < 640 ) {
            $( '.parcel-value' ).append( $( '.parcel-value .rate-unit' ) );
        }
    }
    // Execute on load
    checkWidth();
    // Bind event listener
    $(window).resize(checkWidth);
    
    $( '#toggleID .data-repeater .quantity' ).val(1);
    $( '#toggleID .rate-qty .rate-label' ).addClass( 'active' );
    $(' #no-parcel ').click( function(){
        $( '#toggleID' ).css( 'display', 'block');  
    });
    $( '#toggleID' ).click( function(){
        calculate();
        
    });
    $( '#toggleID .dashicons' ).click( function(){
        calculate();
    });
    $( '.dashicons-plus' ).click( function(){
        $( '#toggleID .data-repeater:last-child .rate-qty .rate-label' ).addClass( 'active' );
    });
    function calculate(){
        var sum = 0;
        $('.quantity').each(function(){
            sum += parseInt(this.value);
        });        
        if($.isNumeric(sum)){
            $( '#no-parcel' ).val(sum);
        }
        $( '.quantity' ).each(function(){
            if( !this.value ) {
                jQuery(this).val(1);
            }
        });
    }
    $( '#no-parcel' ).keyup( function(){
        var qty = $( '.quantity' ).length;
        if (qty>1) {
            $( '.data-repeater:first-child .quantity' ).val(1);
            return;
        }
        var parcel = $( '#no-parcel' ).val();
        $( '.data-repeater:first-child .quantity' ).val(parcel);
    });
	
    // Rate Info Modal
    $( '.protection-info-btn' ).click(function(){
		var rateID = $(this).attr( 'data-value' );
		$( '.protection-'+rateID ).toggleClass( 'hide-element' );
	});
    //$( '.protection-info-btn' ).on( 'click', protection );
    $( '.find-more-button' ).on( 'click', protection );
    function protection() {
        $( '#wpcsr-protection-info' ).toggleClass( 'hide-element' );
    }
    // Protection Insurance Create Order
    $( '#wpcsr-parcels .parcel-value-input' ).keyup( function() {
		var parcel = 0;
		$( '#wpcsr-parcels .parcel-value-input' ).each(function(){
			if( $(this).val() == '' ){
				var value = 0;
			}else{
				var value = $(this).val();
			}
			parcel += parseInt(value);
		});
        if( parcel > 20 ){
            $( '#protection-header' ).removeClass( 'hide-element' );
            $( '#protection-content' ).removeClass( 'hide-element' );
        }else {
            $( '#protection-header' ).addClass( 'hide-element' );
            $( '#protection-content' ).addClass( 'hide-element' );
        }
    });
    
    $( '.exit-modal' ).click( function(){
        $( this ).parent().parent().toggleClass( 'hide-element' );
    });
    $( '#wpcsr-result-categorize' ).on( 'click', '.more-info-btn', function(){
        var parent = $( this ).closest( '.rate-content-details' );
        var modal = $( parent ).find( '#wpcsr-rate-info' );
        $( modal ).toggleClass( 'hide-element' );
    });
	
    // MUlti Parcel
    // Add Repeater in Create Order
	$( '#add-parcel-primary' ).click( function(){
		$( '#parcel-container .wpcargo-table-wrapper' ).removeClass( 'hide-element' );
		$( this ).remove();
		$( '#add-parcel' ).removeClass( 'hide-element' );
	});
	
    // Search Box dimension popup
    $( '.single-search #wpcsr-destination' ).on( 'change', function(){
        if( $( '#wpcsr-destination option' ).length > 1 ){
            $( '.single-search #toggleID' ).removeClass( 'hide-element' );
        }
    });
	
    // Confirmation Before Submitting of form
    $( '#wpcsrcommerce-create-order, #wpcsr-create-order' ).on( 'submit', function(){
		if( $('input[name="agree"]#form-agreement').length > 0 ){
			if( !$('input[name="agree"]#form-agreement').is(":checked") ){
				$( '#disagree-alert' ).css( 'display', 'block' );
				return false;
			}
		}
    });
    $( '#prohibited-button' ).click( function(){
        $( '#wpcsr-prohibited-docs' ).removeClass('hide-element');
    });
});
