$(document).ready(function() {
   
	
	var modal_div = document.getElementById('meta_content_popup_div');
	var modal_show = document.getElementById('add_btn_new');
	var modal_close = document.getElementsByClassName("close")[0];
	modal_close.onclick = function() {
		modal_div.style.display = "none";
	}
	modal_show.onclick = function() {
		modal_div.style.display = "block";	
	}
	
	window.onclick = function(event){
		if(event.target == modal_div){			
			modal_div.style.display = "none";
		}
	}
	
	var modal_div_file = document.getElementById('file_content_popup_div');
	var modal_show_file = document.getElementById('file_btn_new');
	var modal_close_file = document.getElementById("file_close");
	modal_close_file.onclick = function() {
		modal_div_file.style.display = "none";
	}
	modal_show_file.onclick = function() {
		modal_div_file.style.display = "block";	
	}
	
	window.onclick = function(event){
		if(event.target == modal_div_file){			
			modal_div_file.style.display = "none";
		}
	}
		
}); 


$(document).on('click', '#meta_submitBtn_cancel', function () {     
	var modal_div = document.getElementById('meta_content_popup_div');
	modal_div.style.display = "none";
});
$(document).on('click', '#file_submitBtn_cancel', function () {     
	var modal_div_file = document.getElementById('file_content_popup_div');
	modal_div_file.style.display = "none";
});

$(document).on('focus', 'input[type=text],input[type=email],input[type=file],textarea', function () {     
	$(this).removeClass('error_m');		
});
 
$(document).on('click', '#meta_submitBtn', function () {
	var error=false;
	$('#meta_content_popup_div').find('input[type=text],input[type=email],textarea').each(function () {
		$(this).removeClass('error_m');		
		if($(this).val()==""){	
			$(this).addClass('error_m');
			error=true;			
		} 
    });
	
	if(error==true){
		return false;
	}else{
		var formData = new FormData();
		formData.append('center_file', $('#centerImage')[0].files[0]);
		formData.append('app_file', $('#appImage')[0].files[0]);
		formData.append('android_link',$('#android_link').val());	
		formData.append('ios_link',$('#ios_link').val());
		formData.append('app_text',$('#app_text').val());
		
		document.getElementById("meta_submitBtn").disabled = true;
	    $('#message_div').html(http_loader);
		$.ajax({
			url: HTTP_PATH+"download_center/actions",
			data: formData,
			type: "POST",
			dataType : "JSON",
			processData: false,  // tell jQuery not to process the data
			contentType: false, 			
			success: function(msg) {
				if(msg.success==true){
					$('#message_div').html(msg.text);
					setTimeout(function(){					
						document.location.href=HTTP_PATH+'download_center';
					},2000);
				}else{
					document.getElementById("meta_submitBtn").disabled = false;
					if(msg.img){
						$('#message_div').html(msg.img);
					}else{
						$('#message_div').html(msg.text);
					}
				}
			}
		});
	}
});

function deletesFiles(txt){
	$('#file_'+txt).remove();
	$.ajax({
		url: HTTP_PATH+"download_center/deleteFiles",
		data: {deleteId: txt},
		type: "POST",
		dataType : "JSON", 
		success: function(msg) {
			/* if(msg.success==true){
				alert(msg.text);
				setTimeout(function(){					
					document.location.href=HTTP_PATH+'download_center';
				},2000);
			}else{
				alert(msg.text);				
			} */
		}
	});
}

$("#file_form_upload").on('submit',(function(e) {
	e.preventDefault();
	document.getElementById("file_submitBtn").disabled = true;
	$('#info_message_div').html(http_loader);
	$.ajax({
		url: "download_center/upload_file",
		type: "POST",
		data:  new FormData(this),
		contentType: false,
		dataType : "JSON",
		processData:false, 
		success: function(msg) {			 
			 
			$('#info_message_div').html(msg.text);
			setTimeout(function(){					
				document.location.href=HTTP_PATH+'download_center';
			},1000);
			 
		}	        
	});
}));

$(document).on('click','.editSave',function(){
	 
	var id=$(this).data('id');
	var title=$('#title_'+id).val();
	if(title!=""){	
		var formData = new FormData();
		formData.append('file_name', title);
		formData.append('update_id', id);		
		 
		$.ajax({
			url: HTTP_PATH+"download_center/fileEdit",
			data: formData,
			type: "POST",
			dataType : "JSON",
			processData: false,  // tell jQuery not to process the data
			contentType: false, 			
			success: function(msg) {
				if(msg.success==true){
					alert(msg.text);
					setTimeout(function(){					
						document.location.href=HTTP_PATH+'download_center';
					},500);
				}else{
					alert(msg.text);					 
				}
			}
		});
	}
});

$(document).on('click','.editCancel',function(){
	 
	var id=$(this).data('id');
	$('#edit_'+id).remove();
});

$(document).on('click','.fileNameEdit',function(){
	var title=$(this).data('title');
	var id=$(this).data('id');
	var div='<div id="edit_'+id+'" class="form-group"><input value="'+title+'" type="text" id="title_'+id+'" class="form-control"><br><input type="button" data-id="'+id+'" class="editSave btn btn-primary" value="Save"/> <input type="button" class="btn editCancel" data-id="'+id+'" value="Cancel"/></div>';
	$('#file_'+id).append(div);


});

/* $(document).on('click', '#file_submitBtn', function () {
	var error=false;
	$('#meta_content_popup_div').find('iinput[type=file]').each(function () {
		$(this).removeClass('error_m');		
		if($(this).val()==""){	
			$(this).addClass('error_m');
			error=true;			
		} 
    });
	
	if(error==true){
		return false;
	}else{
		var formData = new FormData();
		//formData.append('upload_file', document.getElementById('upload_file').files);
		 
		 
		$.ajax({
			url: HTTP_PATH+"download_center/upload_file",
			data: formData,
			type: "POST",
			dataType : "JSON",
			processData: false,  // tell jQuery not to process the data
			contentType: false, 			
			success: function(msg) {
				if(msg.success==true){
					$('#message_div').html(msg.text);
					setTimeout(function(){					
						document.location.href=HTTP_PATH+'download_center';
					},2000);
				}else{
					if(msg.img){
						$('#message_div').html(msg.img);
					}else{
						$('#message_div').html(msg.text);
					}
				}
			}
		});
	}
});
 */
