﻿// JavaScript Document

function checkmail(e){
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
	var returnval=emailfilter.test(e.value)
	/*if (returnval==false){
		alert("Please enter a valid email address.")
		e.select()
	}
	*/
	return returnval
}

function goMenu(module){	
	var myurl=getSiteAddress();
	var url=myurl+'/'+cur_lang+'/'+module;
	document.location.href=url;		
}

function getLoginCookie(){
	
	if(getCookie('loginvalue') && getCookie('passwdvalue')){
		var theForm = document.frm_login;
		theForm.email_txt.value = getCookie('loginvalue');
		theForm.pass_txt.value = getCookie('passwdvalue');
		return true;
	}
}

function FormatYear(str){
	var validChars = '0123456789';

	for(var i = 0; i < val.length; i++) {
		if(validChars.indexOf(val.charAt(i)) == -1)
			return false;
	}
	
	if(strTemp.length != 4){
		return false;
	}
	
	return true;	
}

function isNumeric(val) {
	var validChars = '0123456789.,';

	for(var i = 0; i < val.length; i++) {
		if(validChars.indexOf(val.charAt(i)) == -1)
			return false;
	}

	return true;
}

function FormatNumber(str){
	if(!isNumeric(str)){
		alert("Vui long nhap so (0-9)!");
		return "";
	}
	return str;
	/*
	var strTemp = GetNumber(str);
	
	if(strTemp.length <= 3)
		return strTemp;
	strResult = "";
	for(var i =0; i< strTemp.length; i++)
		strTemp = strTemp.replace(",", "");
	for(var i = strTemp.length; i>=0; i--)
	{
		if(strResult.length >0 && (strTemp.length - i -1) % 3 == 0)
			strResult = "," + strResult;
		strResult = strTemp.substring(i, i + 1) + strResult;
	}
	return strResult;
	*/
}
function GetNumber(str)
{
	for(var i = 0; i < str.length; i++)
	{
		var temp = str.substring(i, i + 1);
		if(!(temp == "," || temp == "." || (temp >= 0 && temp <=9)))
		{
			alert("Vui long nhap so (0-9)!");
			return str.substring(0, i);
		}
		if(temp == " ")
			return str.substring(0, i);
	}
	return str;
}

function checkIsImage(img_name){
	
	var mess="không hợp lệ." + "\nBạn chỉ được chọn file ảnh: *.jpg, *.gif, *.png";
	
	var spl=img_name.split('.');
	var ext=spl[spl.length-1].toLowerCase();
	if(!(ext=='jpg' || ext=='gif' || ext=='png')){				
		var spl2=img_name.split('\\');
		alert("File '"+spl2[(spl2.length)-1]+"' : "+mess);		
		return false;
	}	
	
	return true;
}


function PrintPage(ID){
	var w=700;
	var h=600;
	var left=(screen.width-w)/2;
	var top=(screen.height-h)/2;
	
	var myurl=getSiteAddress();
	var url=myurl+'/'+cur_lang+'/print-preview/'+ID;
	
	window.open(url,'PrintPage','resizable,scrollbars=yes,top='+top+',left='+left+',height='+h+',width='+w+'');
}

function ShowPopUp(name){
	var div = document.getElementById(name);
	div.style.display="block";		
}
function HidePopUp(name){
	var div = document.getElementById(name);
	div.style.display="none";		
}
function SendEmail(formname){
	var theForm=document.frm_search;
	
	if(formname=="guibanbe"){
		if(theForm.nguoigui_f1.value==""){
			alert("Hãy nhập tên người gửi");
			theForm.nguoigui_f1.focus();
			return false;
		}
		if(theForm.email_nguoigui_f1.value==""){
			alert("Hãy nhập email người gửi");
			theForm.email_nguoigui_f1.focus();
			return false;
		}
		if(!checkmail(theForm.email_nguoigui_f1)){
			alert("'Địa chỉ email' không hợp lệ");
			theForm.email_nguoigui_f1.focus();
			return false;
		}
		
		if(theForm.nguoinhan_f1.value==""){
			alert("Hãy nhập tên người nhận");
			theForm.nguoinhan_f1.focus();
			return false;
		}
		if(theForm.email_nguoinhan_f1.value==""){
			alert("Hãy nhập email người nhận");
			theForm.email_nguoinhan_f1.focus();
			return false;
		}
		if(!checkmail(theForm.email_nguoinhan_f1)){
			alert("'Địa chỉ email' không hợp lệ");
			theForm.email_nguoinhan_f1.focus();
			return false;
		}
		if(theForm.noidunggui_f1.value==""){
			alert("Hãy nhập nội dung");
			theForm.noidunggui_f1.focus();
			return false;
		}
	}else{
		if(theForm.nguoigui_f2.value==""){
			alert("Hãy nhập tên người gửi");
			theForm.nguoigui_f2.focus();
			return false;
		}
		if(theForm.email_nguoigui_f2.value==""){
			alert("Hãy nhập email người gửi");
			theForm.email_nguoigui_f2.focus();
			return false;
		}
		if(!checkmail(theForm.email_nguoigui_f2)){
			alert("'Địa chỉ email' không hợp lệ");
			theForm.email_nguoigui_f2.focus();
			return false;
		}
		if(theForm.noidunggui_f2.value==""){
			alert("Hãy nhập nội dung");
			theForm.noidunggui_f2.focus();
			return false;
		}
	}
	theForm.act.value=formname;
	theForm.submit();
}


function add_if_filter(theForm){
	var f_params='';
	if(theForm.filter){
		var filter=theForm.filter.value;
		if(filter==1){
			
			f_params='0/f:1';
			
			if(theForm.matindang){if(theForm.matindang.value){f_params+='/matin:'+theForm.matindang.value;}}
			if(theForm.FK_loaitin){if(theForm.FK_loaitin.value && theForm.FK_loaitin.value!=0){f_params+='/loaitin:'+theForm.FK_loaitin.value;}}
			if(theForm.FK_loaibds){if(theForm.FK_loaibds.value && theForm.FK_loaibds.value!=0){f_params+='/loaibds:'+theForm.FK_loaibds.value;}}
			if(theForm.FK_loaibds2){if(theForm.FK_loaibds2.value && theForm.FK_loaibds2.value!=0){f_params+='/loaibds2:'+theForm.FK_loaibds2.value;}}
			if(theForm.FK_province){if(theForm.FK_province.value && theForm.FK_province.value!=0){f_params+='/tinh:'+theForm.FK_province.value;}}
			if(theForm.FK_district){if(theForm.FK_district.value && theForm.FK_district.value!=0){f_params+='/quan:'+theForm.FK_district.value;}}
			if(theForm.FK_tinhtrangphaply){if(theForm.FK_tinhtrangphaply.value && theForm.FK_tinhtrangphaply.value!=0){f_params+='/tinhtrangphaply:'+theForm.FK_tinhtrangphaply.value;}}
			if(theForm.FK_khunggia){if(theForm.FK_khunggia.value && theForm.FK_khunggia.value!=0){f_params+='/khunggia:'+theForm.FK_khunggia.value;}}
			if(theForm.textsearch){if(theForm.textsearch.value){f_params+='/s:'+theForm.textsearch.value;}}
			if(theForm.order){if(theForm.order.value && theForm.order.value!=1){f_params+='/order:'+theForm.order.value;}}
			
			if(f_params!='0/f:1'){
				f_params+='/';
			}else{
				f_params='';	
			}
			
			/*
			var o_url=document.location.href;
			var spl = o_url.split('/f:1/');
			var f=spl[1];
			if(f){
				var spl2 = f.split('/');
				if(spl2.length>1){
					//alert(spl2.length+", "+spl2[0]+", "+spl2[spl2.length-1]);
					url+='0/f:1/';
					for(var i=0; i<spl2.length; i++){
						var v=spl2[i];
						//alert(v.substring((v.length)-4,(v.length)));
						if(spl2[i]!='' && v.substring((v.length)-4,(v.length))!='html'){
							url+=spl2[i]+'/';	
						}
					}
					
				}
			}
			*/
		}
	}
	
	return f_params;
}

function checkFormSearch(theForm){
	var s=theForm.s.value;
	if(s==''){
		alert("Nhập từ khóa cần tìm");
		theForm.s.focus();
		return false;	
	}
	
	var cat_s=theForm.cat_s.value;
	var myurl=getSiteAddress()+'/'+cur_lang+'/';
	if(cat_s=="tatca" || cat_s=="tintuc"){
		myurl+='timkiem/1/'+cat_s+'/s:'+s+'/';
	}else if(cat_s=="sangiaodich" || cat_s=="chobds"){
		myurl+=cat_s+'/1/0/f:1/s:'+s+'/';
	}
	
	document.location.href=myurl;
	
	return false;
	
}

function changePage(n){
	var theForm=document.my_frm;
	changeOption(theForm,n);	
}

function changeOption(theForm,page){
	if(!page)page=1;
	if(!theForm)theForm=document.my_frm;
	
	var module=theForm.module.value;
	var myurl=getSiteAddress();
	var url=myurl+'/'+cur_lang+'/'+module+'/'+page+'/';
	
	url+=add_if_filter(theForm);
	
	if(vcho=="1"){
		var d=new Date();
		url+="/0/r:"+d.getTime()+"/";
	}
	//alert(vcho);
	document.location.href=url;
	
}

function getSiteAddress(){
	var u=location.href;
	//alert(u);
	var myurlParts = u.split('/');
	var myurl = myurlParts[0]+"//"+myurlParts[2]; 
	if(myurlParts[2]=="localhost"){
		myurl+="/"+myurlParts[3];
	}
	
	return myurl;
}

function viewDetail(ID, module_fix){
	var theForm=document.my_frm;
	//theForm.id.value=ID;
	
	var myurl=getSiteAddress();
	
	var module="";
	if(module_fix){
		module=module_fix;
	}else{
		if(theForm){
			module=theForm.module.value;
		}
	}
	var page=0;
	if(theForm){
		page=theForm.page.value;
	}
	var url=myurl+'/'+cur_lang+'/'+module;
	if(page)url+='/'+page;
	if(ID)url+='/'+ID;
	document.location.href=url;
	//theForm.submit();
}

function print_friendly(){
	u=location.href;
	t=document.title;window.open("http://www.printfriendly.com/print?url="+encodeURIComponent(u))}
function share_twitter(){
	u=location.href;
	t=document.title;window.open("http://twitter.com/home?status="+encodeURIComponent(u))}
function share_facebook(){
	u=location.href;
	t=document.title;window.open("http://www.facebook.com/share.php?u="+encodeURIComponent(u)+"&t="+encodeURIComponent(t))}
function share_google(){
	u=location.href;
	t=document.title;window.open("http://www.google.com/bookmarks/mark?op=edit&bkmk="+encodeURIComponent(u)+"&title="+t+"&annotation="+t)}
function share_googlebuzz(){
	u=location.href;
	t=document.title;window.open("http://www.google.com/buzz/post?url="+encodeURIComponent(u))}
function share_buzz(){
	u=location.href;
	t=document.title;window.open("http://buzz.yahoo.com/buzz?publisherurn=HaiPhatLand&targetUrl="+encodeURIComponent(u))}