$('#upload').change(function(){

		if($('.sticker').length + this.files.length > 30)
		{
			ShowError("Total 30 Srickers are allowed")
			$('#upload').val('');
			return false;
		}

	   var _totalSelectedImages=this.files.length;
	   var _currentFSIndex=0;
	   var names = [];
	   for (var i = 0; i < $(this).get(0).files.length; ++i)
	   {
	   		var FR= new FileReader();
	   		var IsOutOfSize=false;
	   		FR.addEventListener("load", function(e) {
						  var strim="";
						  var _className="preview";
						  IsOutOfSize=false;

						  if(e.loaded / 1024 >= 100 )
						  {
							//IsOutOfSize=true;
							//_className="";
						  }
						  strim +='<div data-toggle="tooltip" data-placement="top" title="File size : '+ parseFloat(e.loaded / 1024).toFixed(1) +' kb "  IsOutOfSize="'+ IsOutOfSize +'" class="card sticker " style="width: 120px;margin:5px;float:left;">';
						  strim +='	<img class="'+_className+' card-img-top" style="padding:5px;margin:5px;width:100px" src="'+ e.target.result  +'" alt="Card image cap" />';
						  strim +='<button  type="button" onclick="_removeSingle(this)" class="btn btn-danger btn-sm">Remove</button>';
						  strim +='</div>';

						  $('#imgArea').append(strim);
						  
						  _currentFSIndex=_currentFSIndex+1;
						  if(_currentFSIndex==_totalSelectedImages)
						  {
						  	$('[isoutofsize="true"]').css('border','1px solid red');
						  	$('[data-toggle="tooltip"]').tooltip();	
						  	_IssueWithStickrs();
						  	setStickerCount();
						  }
			}); 			

			FR.readAsDataURL( this.files[i]);
	   }
	   $('#upload').val('');
	   
	});	


	function _IssueWithStickrs()
	{
		var  IsProblem=false;
		$('.sticker').each(function(){
			if( $(this).attr('IsOutOfSize')=="true" )
			{
				IsProblem=true;
			}
		});

		if(IsProblem==true)
		{
			ShowError("There is problem with some sticker. Please remove problematic sticker and try again.");
		}
		if($('.sticker').length==0)
		{
			ShowError("Please Select Stickers");
			IsProblem=true;
		}

		return IsProblem;
	}

	function _clearStickers()
	{
		$('#imgArea').html('');
		setStickerCount();		
	}
	function _removeSingle(element)
	{
		$(element).parent().remove();
		setStickerCount();
	}

	function setStickerCount()
	{
		$('#stickerCount').text( $('.sticker').length );
	}




	var _currentI=0;

	function Valdate()
	{
		if($("#StickerPackName").val()=="")
		{
			ShowError("Please enter Sticker Pack Name");
			return false;
		}
		if($("#publisher").val()=="")
		{
			ShowError("Please enter publisher");
			return false;
		}
		if( $('.preview').length < 3 )
		{
			ShowError("Minimum 3 Stickers are required in one Sticker Pack");
			return false;	
		}
		if( $('.preview').length > 30 )
		{
			ShowError("Maxximum 30 Stickers are Allowed in one Sticker Pack");
			return false;	
		}
		return true;
	}

	function _SaveData()
	{
		if(Valdate()==true)
		{
			HoldOn.open({
	                                    theme:'sk-rect',
	                                    message:"Uploading.. <span id='CurrentLoadingCnt'>0</span> /  " + $('.preview').length
	                                });
			 uploadAll();	
		}
	}

	function postSingle()
	{
		$('#CurrentLoadingCnt').text(_currentI+1);

		$.ajax({
            type: "POST",
            data: {upload64:$($('.preview')[_currentI]).attr('src')},
            url: "apis/uploadSingleFile.php",
            success: function(data){
               console.log(data.trim())
			   $('#hiddenArea').append('<input type="hidden" name="upload64[]" value='+  data.trim() +'>');
			   $($('._row')[_currentI]).find('.status').text('Done');
			   
			   _currentI=_currentI+1;
			   	uploadAll();
                }
            });
	}

	function uploadAll()
	{
		if($('.preview').length >_currentI )
			{
				postSingle();
			}
			else
			{
				postForm()
			}
	}


function postForm()
{
	$.ajax({
					url:'apis/savenewpack.php',
					method:'post',
					data:$('#newpackForm').serialize(),
					success:function()
					{
						ShowSuccess("Pack Created Successfully");
						HoldOn.close();
						$('#btnSave').attr('disabled','disabled')
						setTimeout(function(){
							window.location.href="stickerpack.php"
						},3000);
					}
					});
}
