// JavaScript Document
/*********** Left Box *********************/
function getBlogBox(pathUrl, strUser)
{
	$.getJSON(pathUrl+"/apis/blog_mypage_blogmember/"+strUser+"/10/?jsoncallback=?",function(data)
	{
		var result = data.html;
		$("#spanBlog").html(result);                       
	});
}

function getMusicBox(pathUrl, strUser)
{
	$.getJSON(pathUrl+"/apis/getnewsongmypage/"+strUser+"/5/?jsoncallback=?",
	function(data)
	{
		var result = data.html;
		$("#spanMusic").html(result);                             
	});
}

function getPhotoBox(pathUrl, strUser)
{
	$.getJSON(pathUrl+"/apis/getnewphotomypage/"+strUser+"/9/video/?jsoncallback=?",
	function(data)
	{
		var result = data.html;
		$("#spanPhoto").html(result);                             
	});
}

function getBoxFriend(pathUrl, strUser, strProduct, strIdProduct)
{
	$.getJSON(pathUrl+"/apis/infofriend/"+strUser+"/"+strProduct+"/?jsoncallback=?",
	function(data)
	{
		var result = data.result;
		$("#span"+strIdProduct).html(result);                             
	});
}
/*****************************************/

/************************/
function getEditor(name)
{
	tinyMCE.init({
		mode : "exact",
		elements : "txt_write_comment",
		theme : "advanced",
		entities : "",
		plugins : "inlinepopups,emotions,eBizIcon",
		theme_advanced_buttons1 : "mybutton,bold,italic,underline,justifyleft,justifycenter,justifyright,justifyfull,forecolor,emotions,eBizIcon,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "",	    
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "none",
		theme_advanced_resizing : false,		
		content_css : "/js/tinymce/css/content.css",
		setup: function(ed) {        			
			ed.addButton('mybutton', {
				label : name,
				onclick : function() {
					
				}
			});
		}
	});	
	
}
/************************/

/******* Profile **************/
function getProfileBox(profileURL,containerId,callbackName)
{
	
	if (containerId == null)
	{
		containerId ='spanProfile';
	}
	if (callbackName == null)
	{
		callbackName ='callback';
	}
	$.ajax({
			  url: profileURL,
			  cache: false,
			  dataType: "jsonp",
			  jsonp:callbackName,
			  success: function(data) {
				showBoxProfile(data,containerId)
			}});
}

function showBoxProfile(data,containerId)
{
	if (data.result)
	{
		$('#'+containerId).html(data.result);
	}
}


/*********************/
function updateQuickList(video_id)
{
	$('#list_playlist_favorite').nt_updateQuickList(video_id);
}

function pagingVideoPlayListFavorite(strUser, intPage)
{
	$('#list_playlist_favorite').nt_loadFavoriteVideoPlayListList(strUser, intPage);
}

function loadMainVideoList(strUser)
{
	$('#ym_vid_videolist_inner').nt_loadMainVideoList(strUser, $('#shortListVideo').val());
}

function loadMainVideoFavorite(strUser)
{
	$('#list_favorite_video').nt_loadMainFavoriteVideoList(strUser, $('#shortLoveVideo').val());
}

function loadListVideoFavorite(intPage)
{
	$('#array_video_list').nt_loadVideoListLastNews(intPage, $('#limitPage').val(), $('#shortVideo').val());
}

function pagingListAllVideoFavorite(strUser, intPage)
{
	$('#array_video_list').nt_loadFavoriteVideoList(strUser, intPage, $('#shortVideo').val());
}


function pagingListVideoFavorite(strUser, intPage)
{
	$('#array_video_list').nt_loadVideoListLastNews(strUser, intPage, $('#shortVideo').val());
}

function pagingListVideoPlaylist(strUser, intPage)
{
	$('#video_playlist_list').nt_loadListVideoPlaylists(strUser, intPage); 
}

function showPopupVideo(idDiv, intVideoId, strUser, intMemberId, strVideoName)
{
	$('#'+idDiv).nt_showPopupVideo(idDiv, intVideoId, strUser, intMemberId, strVideoName);
}

function showPopupPlayList(idDiv, intVideoListId, strUser, intMemberId)
{
	$('#'+idDiv).nt_showPopupPlayList(idDiv, intVideoListId, strUser, intMemberId);
}

function refeshCaptcha()
{
	var e = document.getElementById("img_security");
	if(e)
	{
		intNumRand = parseInt(Math.random()*999999999999999);
		e.src = VIDEO+"/captcha.php?t=" + intNumRand;
	}
}


function checkEmail(strEmail,isArray)
{
	if(isArray==true)
	{
		arrEmail=strEmail.split(',');
		for(i=0;i<arrEmail.length;i++)
		{
			arrEmail[i]=arrEmail[i].replace(/^\s+|\s+$/g,"");
			if (arrEmail[i].search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1 && arrEmail[i]!="")
			{
				return false;
			}	
			else
			return true;
		}	
	}
	else
	{
		if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
		{
			return false;
		}
		else
		return true
	}
}
function checkFieldLength(element,limit)
{
	if(element.val().length >=limit)
	{
		strNew = element.val().substr(0,limit);
		element.val(strNew);
	}	
}
function errorSecurity(id)
{
	$('div#'+id).removeClass('normal_form');
	$('div#'+id).addClass('error_form');
	$('div#'+id).prepend('<div class="txt_error">Mã xác nhận không chính xác! </div>');
}
function sendToFriendVideo(itemId, ownerUsername, bolType)
{
	strSenderEmail 		= $("#txt_sender_email");
	strReceiverEmail 	= $("#txt_receiver_email");
	strSendedContent 	= $("#txt_content_sendmail");
	intSecurity 		= $("#txt_security_sendmail");
	if ( $("div#id_sender_email").length != 0 )
	{
		strSenderEmail = $("#txt_sender_email");
		if ( $.trim(strSenderEmail.val()) == '' || !checkEmail(strSenderEmail.val(),false) )
		{
			if(!$('div#id_sender_email').is('.error_form'))
			{
				$('div#id_sender_email').removeClass('normal_form');
				$('div#id_sender_email').addClass('error_form');
				$('div#id_sender_email').prepend('<div class="txt_error">Email người Gửi không hợp lệ </div>');
			}
			strSenderEmail.focus();
			return false;
		}
		else
		{
			$('div#id_sender_email').removeClass('error_form');
			$('div#id_sender_email').addClass('normal_form');
		}
		strSenderEmail = strSenderEmail.val();
	}
	else
	{
		strSenderEmail = '';
	}
	
	if ( $.trim(strReceiverEmail.val()) == '' || !checkEmail(strReceiverEmail.val(),true) )
	{
		if ( !$('div#id_receiver_email').is('.error_form') )
		{
			$('div#id_receiver_email').removeClass('normal_form');
			$('div#id_receiver_email').addClass('error_form');
			$('div#id_receiver_email').prepend('<div class="txt_error">Email người Nhận không hợp lệ </div>');
		}
		strReceiverEmail.focus();
		return false;
	}
	else
	{
		$('div#id_receiver_email').removeClass('error_form');
		$('div#id_receiver_email').addClass('normal_form');
	}
	
	if($.trim(intSecurity.val())=='')
	{
		if(!$('div#id_security_sendmail').is('.error_form'))
		{
			$('div#id_security_sendmail').removeClass('normal_form');
			$('div#id_security_sendmail').addClass('error_form');
			$('div#id_security_sendmail').prepend('<div class="txt_error" id="security">Bạn chưa nhập mã xác nhận!</div>');
		}
		intSecurity.focus();
		return false;
	}
	else
	{
		$('div#security').empty();
		$('div#id_security_sendmail').removeClass('error_form');
		$('div#id_security_sendmail').addClass('normal_form');
	}

	$('#ym_2cr').nt_sendToFriend(strSenderEmail, strReceiverEmail.val(), strSendedContent.val(), intSecurity.val(), itemId, ownerUsername, bolType);
}

function setFavouriteVideo(p_video_id)
{
	isFavourite = confirm('Bạn yêu thích Video này?');
	if (isFavourite)
	{
		$('#video_playlist_list').nt_setFavoriteVideo(p_video_id);		
	}
}

function setFavouriteVideoList(intVideoListId)
{
	isFavourite = confirm('Bạn yêu thích Video List này?');
	if (isFavourite)
	{
		$('#video_playlist_list').nt_setFavoriteVideoList(intVideoListId);		
	}
}

function setHotVideo(intVideoId)
{
	isHot = confirm('Bạn muốn chọn video này nổi bật?');
	if (isHot)
	{
		$('#video_playlist_list').nt_setHotVideo(intVideoId);		
	}	
}

function setHotVideoList(intVideoListId)
{
	isHot = confirm('Bạn muốn chọn video playlist này nổi bật?');
	if (isHot)
	{
		$('#video_playlist_list').nt_setHotVideoList(intVideoListId);		
	}	
}

function getDownload()
{
	isDownload = confirm('Hiện giờ đang nâng cấp chức năng này. Vui lòng quay lại sau.');
}


function sendComment()
{			
	contentComment 		= tinyMCE.get('txt_write_comment').getContent();

	if($.trim(contentComment) == "")
	{
		alert('Vui lòng nhập Bình Luận');
		$("#txt_write_comment").focus();
		return false;
	}
	else
	{
		$("#ym_vid_binhluan_inner").nt_insertVideoComment(contentComment, intVideoId, intMemberId);
	}
}

function sendDetailComment()
{			
	contentComment 		= tinyMCE.get('txt_write_comment').getContent();

	if($.trim(contentComment) == "")
	{
		alert('Vui lòng nhập Bình Luận');
		$("#txt_write_comment").focus();
		return false;
	}
	else
	{
		$("#ym_vid_binhluan_inner").nt_insertVideoDetailComment(contentComment, intVideoId, intMemberId);
	}
}

function sendCommentVideoList()
{			
	contentComment 		= tinyMCE.get('txt_write_comment').getContent();

	if($.trim(contentComment) == "")
	{
		alert('Vui lòng nhập Bình Luận');
		$("#txt_write_comment").focus();
		return false;
	}
	else
	{
		$("#ym_vid_binhluan_inner").nt_insertVideoListComment(contentComment, intVideoListId, intMemberId);
	}
}

function sendDetailCommentVideoList()
{			
	contentComment 		= tinyMCE.get('txt_write_comment').getContent();

	if($.trim(contentComment) == "")
	{
		alert('Vui lòng nhập Bình Luận');
		$("#txt_write_comment").focus();
		return false;
	}
	else
	{
		$("#videolist_detail_comment").nt_insertVideoListDetailComment(contentComment, intVideoListId, intMemberId);
	}
}

function autoPlayVideo(path, intVideo)
{
	if($('#autoplay').attr('checked') == true)
	{
		$("#blog").val('<embed src="'+ path +'flash_player/player_embed.swf" width="430" height="330" type="application/x-shockwave-flash" allowfullscreen="true"  flashvars="width=430&height=330&xmlPath='+ VIDEO +'/playlist/'+ intVideo +'.xml&mAuto=true&colorAux=0xe0e0e0&colorMain=0x3d78f6&colorBorder=0x5a5a5a"></embed>');
	}
	else
	{
		$("#blog").val('<embed src="'+ path +'flash_player/player_embed.swf" width="430" height="330" type="application/x-shockwave-flash" allowfullscreen="true" flashvars="width=430&height=330&xmlPath='+ VIDEO +'/playlist/'+ intVideo +'.xml&colorAux=0xe0e0e0&colorMain=0x3d78f6&colorBorder=0x5a5a5a"></embed>');
	}
}

function autoPlayVideoList(path, intVideoList)
{
	if($('#autoplay').attr('checked') == true)
	{
		$("#blog").val('<embed src="'+ path +'flash_player/player_embed_playlist.swf" width="429" height="380" type="application/x-shockwave-flash" allowfullscreen="true" flashvars="width=429&height=380&xmlPath='+ VIDEO +'/videoplaylist/'+ intVideoList +'.xml&mAuto=true&colorAux=0xe0e0e0&colorMain=0x3d78f6&colorBorder=0x5a5a5a"></embed>');
	}
	else
	{
		$("#blog").val('<embed src="'+ path +'flash_player/player_embed_playlist.swf" width="429" height="380" type="application/x-shockwave-flash" allowfullscreen="true" flashvars="width=429&height=380&xmlPath='+ VIDEO +'/videoplaylist/'+ intVideoList +'.xml&colorAux=0xe0e0e0&colorMain=0x3d78f6&colorBorder=0x5a5a5a"></embed>');
	}
}

function sendBadNote(strId, intMemberId)
{
	strOptionBadNoteContent = $(".rad:checked").attr('id');
	strBadNoteContent 		= $("#txt_content_badnote");
	intSecurity 			= $("#txt_security_badnote");
	if($.trim(intSecurity.val())=='')
	{
		if(!$('div#id_security_badnote').is('.error_form'))
		{
			$('div#id_security_badnote').removeClass('normal_form');
			$('div#id_security_badnote').addClass('error_form');
			$('div#id_security_badnote').prepend('<div class="txt_error" id="security">Bạn chưa nhập mã xác nhận!</div>');
		}
		intSecurity.focus();
		return false;
	}
	else
	{
		$('div#security').empty();
		$('div#id_security_badnote').removeClass('error_form');
		$('div#id_security_badnote').addClass('normal_form');
	}
	$('#ym_2cr').nt_sendVideoBadNote(strOptionBadNoteContent, strBadNoteContent.val(), intSecurity.val(), strId, intMemberId);
}
function sendVideoListBadNote(strId, intMemberId)
{
	strOptionBadNoteContent = $(".rad:checked").attr('id');
	strBadNoteContent 		= $("#txt_content_badnote");
	intSecurity 			= $("#txt_security_badnote");
	if($.trim(intSecurity.val())=='')
	{
		if(!$('div#id_security_badnote').is('.error_form'))
		{
			$('div#id_security_badnote').removeClass('normal_form');
			$('div#id_security_badnote').addClass('error_form');
			$('div#id_security_badnote').prepend('<div class="txt_error" id="security">Bạn chưa nhập mã xác nhận!</div>');
		}
		intSecurity.focus();
		return false;
	}
	else
	{
		$('div#security').empty();
		$('div#id_security_badnote').removeClass('error_form');
		$('div#id_security_badnote').addClass('normal_form');
	}

	$('#ym_2cr').nt_sendVideoListBadNote(strOptionBadNoteContent, strBadNoteContent.val(), intSecurity.val(), strId, intMemberId);
}
function addPlaylist(intVideoId)
{
	intVideoListId 		= $("#txt_playlist_id").val();
	strPlaylistTitle 	= $("#txt_playlist_title").val();
	strPlaylistContent 	= $("#txt_playlist_content").val();
	
	if ( document.getElementById("popup").style.display == 'block' && ( strPlaylistTitle == 'Tên video list' || strPlaylistTitle == '' ) )
	{
		new_playlist();
		if(!$('div#id_playlist_title').is('.error_form'))
		{
			$('div#id_playlist_title').removeClass('normal_form');
			$('div#id_playlist_title').addClass('error_form');
			$('div#id_playlist_title').prepend('<div class="txt_error" id="security">Bạn nhập tên video list</div>');	
		}
		return false;
	}
	else
	{
		if ( strPlaylistTitle.indexOf("<") != -1 || strPlaylistTitle.indexOf(">") != -1 || strPlaylistTitle.indexOf("&lt;") != -1 || strPlaylistTitle.indexOf("&gt;") != -1 )
		{
			$('div#id_playlist_title').removeClass('normal_form');
			$('div#id_playlist_title').addClass('error_form');
			$('div#id_playlist_title').prepend('<div class="txt_error" id="security">Vui lòng không nhập các kí tự đặc biệt.</div>');	
			return false;
		}
		else
		{
			$('div#security').empty();
			$('div#id_playlist_title').removeClass('error_form');
			$('div#id_playlist_title').addClass('normal_form');
		}
	}
	
	if ( strPlaylistTitle == 'Tên video list' ) strPlaylistTitle = '';
	else intVideoListId = '';
	
	$('#ym_2cr').nt_addPlaylist(intVideoId, intVideoListId, strPlaylistTitle, strPlaylistContent);
}

function new_playlist(opt)
{
	if (opt=="off") document.getElementById("popup").style.display='none';
	else document.getElementById("popup").style.display='block';
}

function pagingVideoPlaylists(intPage, strTab)
{
	$('#'+ strTab).nt_loadTabNewViewComVoteFav(strTab, intPage);
}

function pagingVideos(strTab, intCateId, intSubCateId, strTime, intPage, strVideo)
{
	$('#'+ strTab).nt_loadTabNewViewComVoteFav(strTab, intCateId, intSubCateId, strTime, intPage, strVideo);
}

function downloadVideo(intVideo)
{
	intSecurity = $("#txt_security_downloadvideo");
	if($.trim(intSecurity.val())=='')
	{
		if(!$('div#id_security_downloadvideo').is('.error_form'))
		{
			$('div#id_security_downloadvideo').removeClass('normal_form');
			$('div#id_security_downloadvideo').addClass('error_form');
			$('div#id_security_downloadvideo').prepend('<div class="txt_error" id="security">Bạn chưa nhập mã xác nhận!</div>');
		}
		intSecurity.focus();
		return false;
	}
	else
	{
		$('div#security').empty();
		$('div#id_security_downloadvideo').removeClass('error_form');
		$('div#id_security_downloadvideo').addClass('normal_form');
	}
	$('#ym_popup_download').nt_downloadVideo(intSecurity.val(), intVideo, intMemberId);
}
function checkSecurity(divId)
{
	if($('div#security2').length<=0)
	{
		$('div#' + divId).removeClass('normal_form');
		$('div#' + divId).addClass('error_form');
		$('div#' + divId).prepend('<div class="txt_error" id="security2">Mã xác nhận chưa hợp lệ!</div>');
		intSecurity.focus();
		return false;
	}
	else
	{
		$('div#' + divId).removeClass('error_form');
		$('div#' + divId).addClass('normal_form');
	}
}

//open popup win
function OpenWin(url, name, width, height, property)
{
	this.x = (screen.width - width) / 2; 
	this.y = (screen.height - height - 60) / 2; 
	property = 'top=' + this.y + ',left=' + this.x + ',width=' + width + ',height=' + height + ',' + property;
//	property = "scrollbars=1,resizable=1,menubar=1,status=1,location=1,toolbar=1"
	newwindow = window.open(url,name,property);
	if (window.focus) 
	{
		newwindow.focus();
	}
}