function upload_file() {
	if(!checkRequiredField('category','Please choose a category for the video.')){
		return false;
	}
	if(!checkExtension('fileToUpload', '.avi-.mpeg-.mpg-.mov-.wmv', 'Please upload the video file in AVI or MPEG format.')){
		return false;	
	}
	ajaxFileUpload();
}

function ajaxFileUpload()
{
	if(!checkRequiredField('fileToUpload', 'Please select a file to upload.'))
		return false;
	
	var category = document.getElementById('category').value;
	var title = document.getElementById('title').value;
	var description = document.getElementById('the_description').value;
	
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});

	$("#uploading_text")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});

	$.ajaxFileUpload
	(
		{
			url: "/inc/ajaxfuncs.php",
			data: "category="+category+"&title="+title+"&description="+description+"",
			secureuri:false,
			fileElementId:'fileToUpload',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.error);
					}else
					{
						alert(data.msg);
						document.getElementById('category').value = '';
						document.getElementById('title').value = '';
						document.getElementById('description').value = '';
						window.location.href='myvideo.php';
					}
				}
			},
			error: function (data, status, e)
			{
				if(data.msg)
					alert(data.msg);
			}
		}
	)
		
	return false;

}
function delete_file(fileid) {
	if(confirm('Are you sure you want to delete the video file?')){
		if(fileid != ''){
			$.ajax({
				type: "POST",
				url: "/inc/ajaxfuncs.php",
				data: "func=updatethefile&data_id="+fileid+"&cmd=delete_file",
				success: function(msg){
					if(msg != ""){
						window.location.reload();
					}
					else{
						alert('Cannot delete the file.');
					}
				}
			});	
		}
		else{
			return false;	
		}
	}
}

function rate_video(rate){
	var fileid = document.getElementById('id').value;
	$.ajax({
		type: "POST",
		url: "/inc/ajaxfuncs.php",
		data: "func=ratevideo&data_id="+fileid+"&rate="+rate+"",
		success: function(msg){
			if(msg != ""){
				alert('Thank you for rating.');
				//window.location.reload();
			}
			else{
				alert('Cannot rate the video.');
			}
		}
	});	
}

function comment_video(){
	var fileid = document.getElementById('id').value;
	var comment = document.getElementById('comment').value;
	$.ajax({
		type: "POST",
		url: "/inc/ajaxfuncs.php",
		data: "func=commentvideo&data_id="+fileid+"&comment="+comment+"",
		success: function(msg){
			if(msg != ""){
				alert('Thank you for your comment.');
				//document.getElementById('comment').value = '';
				window.location.reload();
			}
			else{
				alert('Cannot comment the video.');
			}
		}
	});	
}
function comment_photo(){
	var fileid = document.getElementById('photoid').value;
	var comment = document.getElementById('comment').value;
	$.ajax({
		type: "POST",
		url: "/inc/ajaxfuncs.php",
		data: "func=commentphoto&data_id="+fileid+"&comment="+comment+"",
		success: function(msg){
			if(msg != ""){
				alert('Thank you for your comment.');
				//document.getElementById('comment').value = '';
				frames['photocomments'].window.location.href  = 'view-photo-comments.php?photoid='+fileid;
				document.getElementById('commentbox').style.display = 'none';
				document.getElementById('comment').value = '';
			}
			else{
				alert('Cannot comment the video.');
				document.getElementById('commentbox').style.display = 'none';
				document.getElementById('comment').value = '';
			}
		}
	});	
}

function save_title(rate){
	var photoid = document.getElementById('id').value;
	var title = document.getElementById('title').value;
	$.ajax({
		type: "POST",
		url: "/inc/ajaxfuncs.php",
		data: "func=savetitle&data_id="+photoid+"&title="+title+"",
		success: function(msg){
			if(msg != ""){
				alert('Title Updated.');
				document.getElementById('edittitle').style.display='none';
			}
			else{
				alert('Cannot update the title.');
			}
		}
	});	
}
