function isMaxLength(maxLength,s)
{	
	if(s!=null && s!="" && s.length>maxLength)
	{
		alert("Maximum length is " + maxLength + " characters!");		
		return false;
	}	
	return true;
}
function randCaptCha(img,text){		
		var strCaptCha = CyloCaptcha.genRan(6);
		var HTMLCode = CyloCaptcha.display(strCaptCha);	
		$(text).val(strCaptCha);
		$(img).html(HTMLCode);
}
function trimSpace(s)
{	
	if(s!=null && s.length>0){
		for(i=0;i<s.length;i++)
			s = s.replace("&nbsp;"," ");
		while(s.charCodeAt(0)==32 && s.length>1)
		{
			s=s.substr(1,s.length)
		}		
		//-------------------------
		if(s.charCodeAt(0)==32 && s.length==1)
			return "";
		else
			return s;	
	}else return "";
}
function showTab(i){		
		if(i==0){
			$('#MostRead').removeClass('hidden');
			$('#MostRead').addClass('show');
			
			$('#MostEmail').removeClass('show');
			$('#MostEmail').addClass('hidden');
			$('#MostEmailShow').hide();
			$('#MostReadShow').show();
		}else{		
			$('#MostRead').removeClass('show');
			$('#MostRead').addClass('hidden');
			
			$('#MostEmail').removeClass('hidden');
			$('#MostEmail').addClass('show');
			$('#MostReadShow').hide();
			$('#MostEmailShow').show();
		}
	}
function chkSubmit(txt,i){		
	if($('#'+txt).val()==""){
		alert("Please fill keywords!");
		$('#'+txt).focus();
		return false;
	}else{
		if(i==0){				
			$('input[id*=rdHmeNews]').each(function(){					
				if(this.checked && $(this).val()=="google"){
					text = $('#'+txt).val();
					f=document.forms[0];						
					f.action ="gsearch.php?cx=008750855576469573486%3Atncrbga5_n4&cof=FORID%3A9&ie=UTF-8&q="
								+ text.replace(" ","+") + "&sa=Search";
					f.method = "post";
				}
			});												
		}
		return true;			
	}
}
function download(id){
	l = screen.availWidth/2 - 100;
	t = screen.availHeight/2 - 100;
	window.open("download.php?id="+id,"","top="+t+",left="+l+",width=300,height=200,scrollbar=no");
	}
function page(id,p,a){		
		url = "video/show_comments.php";
		if(a==1){
			url = "incs/show_article_comments.php";
		}
		$('#vComments').load(url+"?id="+id+"&start="+p);	
	}
function isEmail(strEmail) {
	var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
	var matchArray = strEmail.match(emailPat);
	if (matchArray == null) {
		return false;
	}
	return true;
}
function submitComment(id,a){
		url_comment = "video/show_comments.php";
		if(a==1){
			url_comment = "incs/show_article_comments.php";
		}
		if(trimSpace($("#txtName").val())==""){
			alert('Please input your name!');
			$("#txtName").focus();
			return;
		}else{
			if(!isMaxLength(100,trimSpace($("#txtName").val()))){
				$("#txtName").focus();
				return;
			}
		}
		// dung neu nhap toan rong
		if(trimSpace($("#txtEmail").val()).length>0 && !isEmail(trimSpace($("#txtEmail").val()))){
			alert('Email is invalid!');
			$("#txtEmail").focus();
			return;
		}		
		if(trimSpace($("#txtComment").val())==""){
			alert('Please input your comment!');
			$("#txtComment").focus();
			return;
		}else{
			if(!isMaxLength(500,trimSpace($("#txtComment").val()))){
				$("#txtComment").focus();
				return;
			}
		}		
		//secutity code		
		if(trimSpace($("#hidCaptCha").val())!=trimSpace($("#txtCaptCha").val())){
			alert('Incorrect security code!!');
			$("#txtCaptCha").focus();
			return;
		}
		//------------------------------------------------		
		$.ajax({
			    type: "POST",
				url:url_comment,
				data:{'id':id,'poster':$("#txtName").val(),'comment':$("#txtComment").val(),
						'email':$("#txtEmail").val(),'title':$("#txtTitle").val()},
				success:function(data){					
					//alert(data);
					$('#vComments').html('');
					$('#vComments').load(url_comment+"?id="+id+"&start=0");						
				},
				error:function(){
					alert('Error!');
				}
		});
	}	
function otherVideo(id,p,gid){
	//$('#vOther').html("");
	$('#vOther').load("video/other.php?id="+id+"&start="+p+"&v_id="+gid);
	}
function showStocks(){		
	$('#loadStocks').html('<img src="admin/images/loading.gif"> loading...');	
	$.ajax({		   
		   type:"GET",
		   url:"incs/display_stocks.php",
		   data:{},
		   success:function(data){			  			   			   
			  $('#loadStocks').html("");
			   },
		   error:function(){
			   //alert('Connection is limited');
			   }
	});
}
function editPrefrences(){	
	$('#showStock').load("incs/display_stocks_tbl.php");
	}
function viewCom(div_){						
	//vComments +div_
	$('#vComments').toggle();		
}

function searchVideo(){	
	if($('#txtVideoKey').val()==""){
		alert("Please fill keyword into the Textbox!");
		$('#txtVideoKey').focus();
	}else{
		$('#txtKey').val($('#txtVideoKey').val());
		$('#divSearch').html('<img src="images/loading.gif">Loading, please wait!');		
		$.ajax({		   
		   type:"POST",
		   url:"video/search.php",
		   data:{'key':$('#txtVideoKey').val()},
		   success:function(data){			  
			  $('#divSearch').html(data);
			   },
		   error:function(){
			  $('#divSearch').load(data);
			   }
		});
	}
}
function pageSearchVideo(p){		
		$('#divSearch').html('<img src="images/loading.gif">Loading, please wait!');				
		$.ajax({		   
		   type:"POST",
		   url:"video/search.php",
		   data:{'key':$('#txtKey').val(),'start':p},
		   success:function(data){			  
			  $('#divSearch').html(data);
			   },
		   error:function(){
			  $('#divSearch').load(data);
			   }
		});	
}
