var imgAry = new Array(); 
var inc_var = 0;
var MtVal = 0;
var x=0;
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.png)$/;
var ct = 0;

$(document).ready(function() 
{
    /*ct= 16 - parseInt($("#HiddenImagesVal").val());
    if(MtVal==0)
    {
        inc_var= parseInt($("#HiddenImagesVal").val());
    }
    else
    {
       inc_var = ct; 
    }
    
    ///for(var i=1;i<16;i++)
    for(var i=1;i<ct;i++)*/
    
    for(var i=1;i<16;i++)
    {
        $("#OutrImg"+i).hide();
    }

});



function ImgCalled(i)
{ 
    if(inc_var > 1)
    {
        $("#PreImg"+i).hide(); 
        str = "PreImg"+i;
        res = str.split("PreImg");   
        imgAry.push(parseInt(res[1]));  
        ImgHidenVal = imgAry.toString(); 
        $("#ImgHidenVal").val(ImgHidenVal);
        inc_var--;
    }
    else
    {
        alert("Minimum one image has to upload");
        return false;
    }
}


function preview_image() 
{ 
    var tmp=0;
    var tmp1=0;
    var x1=0;
    var x2=0;
    if(MtVal==0)
    {
        inc_var= parseInt($("#HiddenImagesVal").val());
    }
    
    var total_file=document.getElementById("add_more"+MtVal).files.length; 
    for(var i=0;i<total_file;i++)
    {
        var iSize = (event.target.files[i].size)/1024; 
        mb_size = iSize/1024; 
        
        if(mb_size <= 6) 
        {
            if (regex.test(event.target.files[i].name.toLowerCase())) 
            {
                $("#OutrImgNxt"+MtVal).before("<div  class='col-xs-6 col-md-12 col-lg-12 POP' id='PreImg"+x+"'> <div  class='pip' style='min-width:120px;min-height:145px;border:1px solid #002f34;margin-bottom:10px;'> <div  class='abcd upload_js_img' >" +"<img src='"+URL.createObjectURL(event.target.files[i])+"'><a href='javascript:void(0);' class='remove' onclick='ImgCalled("+x+");' >Remove</a>  " +"</div></div><div class='clear'></div></div>");
            }
            else 
            {
                tmp=1;
                if(x1 < 1)
    		        alert("It's not a valid image file. Only jpg,jpeg,png image formats are allowed");  
    		        
    		    imgAry.push(parseInt(x));  
                ImgHidenVal = imgAry.toString();
                $("#ImgHidenVal").val(ImgHidenVal);  
    		    x1++;
            }
        }
        else
        {
            tmp1=1;
            if(x2 < 1)
		        alert("Up to 6 MB file size is allow!");  
		        
		    imgAry.push(parseInt(x));  
            ImgHidenVal = imgAry.toString(); 
            $("#ImgHidenVal").val(ImgHidenVal);   
            
		    x2++;
        }
        x++;
    }
    
    if(total_file > 1)
    {
        inc_var+=total_file;
    }
    else 
    {
        inc_var++;
    }
    
    if(tmp==1)
    {
        inc_var=inc_var - x1;
    }
    
    if(tmp1==1)
    {
        inc_var=inc_var - x2;
    }
    
    $("#OutrImg"+MtVal).hide();
    ShowMyImages = (MtVal+1); 
    
    
    if(inc_var < 15)
    {
        $("#OutrImg"+ShowMyImages).show(400); 
    }
        
    MtVal++; 
    
    
    
    
}