var abc = 0; //Declaring and defining global increement variable 
var inc_var1 =0;	
$(document).ready(function() {

//To add new input file field dynamically, on click of "Add More Files" button below function will be executed
    
 

//following function will executes on change event of file input to select different file	 
 var inc_var =0;	

    $('body').on('change', 'input:file', function()
    {
        console.log(inc_var);
        if(inc_var < 15)
        {
    		var fileName = $(this).val();
    		var iSize = $(this)[0].files[0].size/1024; 
    		mb_size = iSize/1024;   
    		
    		if(mb_size <= 2) 
    		{   	
    			if (this.files && this.files[0]) 
    			{
    				 abc += 1; //increementing global variable by 1
    				 inc_var++;
    				
    				var z = abc - 1;
    				var x = $(this).parent().find('#previewimg' + z).remove();
    				$(this).before("<div id='abcd"+ abc +"' class='abcd upload_js_img'><img id='previewimg" + abc + "' src=''  /></div></div>");
    			   
    				var reader = new FileReader();
    				reader.onload = imageIsLoaded;
    				reader.readAsDataURL(this.files[0]);    
    				$(this).hide();   
    				res = $(this).attr('id').split("add_more"); 
    				$("#image_preview"+res[1]).hide();
					$("#CarLb"+res[1]).hide();  
    				$("#ShowMyImages").show(400); 
    				
    				$("#abcd"+ abc).append($("<img/>", {id: 'img', src: 'x.png', alt: 'delete'}).click(function() 
    				{
    					if(inc_var > 1)
    					{	 
    						inc_var--;
    						$(this).parent().remove();   
    					}else
    						alert("You must upload atlist one image!");   
    					
    				}));   
    			}
    		}else 
    		{
    			$(this).val('');
    			alert("Up to 2 MB file size is allow!");
    		}
        }
        else 
        {
            $(this).val('');
    		alert("You must upload upto 15 photo");
    		$(this).hide(); 
        }
    });

//To preview image     
    function imageIsLoaded(e) {
        $('#previewimg' + abc).attr('src', e.target.result);
    };

    $('#upload').click(function(e) {
        var name = $(":file").val();
        if (!name)
        {
            alert("First Image Must Be Selected");
            e.preventDefault();
        }
    });
});    