//Doan code Save va Get Cookie
var expDays = 1;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {  

	var endstr = document.cookie.indexOf (";", offset);  

	if (endstr == -1) { endstr = document.cookie.length; }

	return unescape(document.cookie.substring(offset, endstr));

}

function GetCookie (name) {  

	var arg = name + "=";  

	var alen = arg.length;  

	var clen = document.cookie.length;  

	var i = 0;  

	while (i < clen) {    

		var j = i + alen;    

		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    

		i = document.cookie.indexOf(" ", i) + 1;    

		if (i == 0) break;   

	}  

	return null;

}

function SetCookie (name, value) {  

	var argv = SetCookie.arguments;  

	var argc = SetCookie.arguments.length;  

	var expires = (argc > 2) ? argv[2] : null;  

	var path = (argc > 3) ? argv[3] : null;  

	var domain = (argc > 4) ? argv[4] : null;  

	var secure = (argc > 5) ? argv[5] : false;  

	document.cookie = name + "=" + escape (value) + 

	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 

	((path == null) ? "" : ("; path=" + path)) +  

	((domain == null) ? "" : ("; domain=" + domain)) +    

	((secure == true) ? "; secure" : "");
}

//Ket thuc Doan code Save va Get Cookie

function CatExpandMenu(spanId, target, align, boxId) {
	var catId = spanId;
	
	var cat_ex_cookie = GetCookie('catExpanding');
	var selectedCatCookie = GetCookie('selectedCat');
	var selectedAlignCookie = GetCookie('boxAlign');
	var box_ex_cookie = GetCookie('boxExpanding');
	
	if(cat_ex_cookie != null) {
		if(document.getElementById(cat_ex_cookie) != null) {
			var expandingMenu = document.getElementById(cat_ex_cookie);
			if(spanId == cat_ex_cookie) {							
				if (expandingMenu.style.display == 'none') {
					expandingMenu.style.display = 'block';
				}
				else {
					expandingMenu.style.display = 'none';
				}
			}
			else {
				expandingMenu.style.display = 'none';
			}
		}
	}
	
	if(selectedCatCookie != null) {
		if(document.getElementById(selectedCatCookie) != null) {
			var selectedCat = document.getElementById(selectedCatCookie);
			if(selectedAlignCookie==1)
				selectedCat.className='catalogue_top';
			if(selectedAlignCookie==2)
				selectedCat.className='catalogue_left';
			if(selectedAlignCookie==3)
				selectedCat.className='catalogue_right';
			if(selectedAlignCookie==4)
				selectedCat.className='catalogue_bottom';
		}
	}
	
	if(box_ex_cookie != null) {
		if(box_ex_cookie != boxId){
			if(document.getElementById(box_ex_cookie) != null) {
				var expandingMenu = document.getElementById(box_ex_cookie);
				expandingMenu.style.display='none';
			}
			if(document.getElementById(boxId) != null) {
				var expandingMenu = document.getElementById(boxId);
				expandingMenu.style.display='block';
			}
		}				
	}	
	
	SetCookie('catExpanding',spanId);
	SetCookie('selectedCat',"cat" + catId);
	SetCookie('boxAlign', align);
	SetCookie('boxExpanding',boxId);	
	
	if (target != "") {
		window.location = target;
	}
	else {
		if(spanId != cat_ex_cookie) {
			if(document.getElementById(spanId) != null) {
				var expandingMenu = document.getElementById(spanId);
				expandingMenu.style.display='block';
			}
		}
		if(document.getElementById("cat" + catId) != null) {
			var selectedCat = document.getElementById("cat" + catId);
			//selectedCat.className='menu_selected';
			if(align==1)
				selectedCat.className='menu_selected_top';
			if(align==2)
				selectedCat.className='menu_selected_left';
			if(align==3)
				selectedCat.className='menu_selected_right';
			if(align==4)
				selectedCat.className='menu_selected_bottom';
		}
	}	
}

function BoxExpandMenu(spanId, target) {
	var box_ex_cookie = GetCookie('boxExpanding');
	if(spanId=='')
	{
		//document.getElementById(box_ex_cookie).style.display='none';
		//SetCookie('boxExpanding',"");
	}
	if(box_ex_cookie != null) {
		if(document.getElementById(box_ex_cookie) != null) {
			var expandingMenu = document.getElementById(box_ex_cookie);
			if(spanId == box_ex_cookie) {							
				if (expandingMenu.style.display == 'none') {
					expandingMenu.style.display = 'block';
					SetCookie('boxExpanding',spanId);	
				}
				else {
					expandingMenu.style.display = 'none';
					SetCookie('boxExpanding',"");	
				}
			}
			else {
				expandingMenu.style.display = 'none';
				SetCookie('boxExpanding',"");
				if(document.getElementById(spanId) != null){
					document.getElementById(spanId).style.display = 'block';
					SetCookie('boxExpanding',spanId);	
				}
			}
		}
		else {
			if(document.getElementById(spanId) != null){
				document.getElementById(spanId).style.display = 'block';
				SetCookie('boxExpanding',spanId);					
			}
		}
	}
	else {
		if(document.getElementById(spanId) != null){
			document.getElementById(spanId).style.display = 'block';
			SetCookie('boxExpanding',spanId);	
		}
	}	
	
	
	if(target!='') window.location = target;
}

function CatLink(catId, target, align) 
{
	SetCookie('selectedCat',"cat" + catId);
	SetCookie('boxAlign', align);
	//alert(target);
	BoxExpandMenu(catId, target);
	/*var box_ex_cookie = GetCookie('boxExpanding');	
	if(box_ex_cookie != null) 
	{
		if()
	}
	window.location = target;*/
}
/*
function CatLink(catId, target, align) {
	SetCookie('selectedCat',"cat" + catId);
	SetCookie('boxAlign', align);
	window.location = target;
}
*/

function loadPage() {
	var cat_ex_cookie = GetCookie('catExpanding');
	var box_ex_cookie = GetCookie('boxExpanding');
	var cat_selected_cookie = GetCookie('selectedCat');
	//var selectedAlignCookie = GetCookie('boxAlign');
	
	if(cat_ex_cookie != null) {
		if(document.getElementById(cat_ex_cookie) != null) {
			var expandingMenu = document.getElementById(cat_ex_cookie);
			expandingMenu.style.display='block';
		}		
	}
		
	if(box_ex_cookie != null) {
		if(document.getElementById(box_ex_cookie) != null) {			
			var expandingMenu = document.getElementById(box_ex_cookie);
			expandingMenu.style.display='block';
		}		
	}
	
	/*if(cat_selected_cookie != null) {
		if(document.getElementById(cat_selected_cookie) != null) {
			var selectedCat = document.getElementById(cat_selected_cookie);
			//selectedCat.className='menu_selected';
			if(selectedAlignCookie==1)
				selectedCat.className='menu_selected_top';
			if(selectedAlignCookie==2)
				selectedCat.className='menu_selected_left';
			if(selectedAlignCookie==3)
				selectedCat.className='menu_selected_right';
			if(selectedAlignCookie==4)
				selectedCat.className='menu_selected_bottom';
		}
	}*/	
}

function expMenu(id)
{	
	//alert(id);
	var obj=document.getElementById(id);
	
	if(obj.style.display="none")
	{
		obj.style.display="block";
	}
	else
	{
		obj.style.display="none";
	}
	
}
function openWithSelfMain(url,name,width,height) {
	var options = "width=" + width + ",height=" + height + "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no";
	new_window = window.open(url, name, options);
	window.self.name = "main";
	new_window.focus();
}

//Ham chuyen doi huong tim kiem tren tran va tim tren internet (Google)

function search_google() {
	document.frm_search_google.q.value = document.getElementById("query").value;
	//document.frm_search_google.cof.value=unescape(document.frm_search_google.cof.value);
	document.frm_search_google.submit();
}

function search_portal() {
	var str = document.getElementById("query").value;	
	if(str.length >= 3){
		document.frm_search_portal.submit();
	}	
}

function switchSearch(){
	var obj = document.getElementById("rdo_search");
	if(obj.checked){
		search_portal(); 
	}
	else {
		search_google();
	}
	
	return false;
}
//Ham khoi tao anh cho menu
function initMenu(lang) 
{
	var menu_cookie = GetCookie('menuCookie');
	//alert(menu_cookie);
	var obj;
	if(lang == 'vietnamese')
	{
		obj = document.getElementById('trangchu');
		if( obj != null) {
		if(menu_cookie == 'trangchu') {					
				obj.className = 'Menu-trangchu-Over';			
			}
			else
			{
				obj.className = 'Menu-trangchu-Out';			
			}
		}
		
		obj = document.getElementById('tintuc');
		if( obj != null) {
		if(menu_cookie == 'tintuc') {					
				obj.className = 'Menu-tintuc-Over';			
			}
			else
			{
				obj.className = 'Menu-tintuc-Out';			
			}
		}
		
		obj = document.getElementById('diendan');
		if( obj != null) {
		if(menu_cookie == 'diendan') {					
				obj.className = 'Menu-diendan-Over';			
			}
			else
			{
				obj.className = 'Menu-diendan-Out';			
			}
		}
		
		obj = document.getElementById('taifile');
		if( obj != null) {
		if(menu_cookie == 'taifile') {					
				obj.className = 'Menu-taifile-Over';			
			}
			else
			{
				obj.className = 'Menu-taifile-Out';			
			}
		}
		
		obj = document.getElementById('lienhe');
		if( obj != null) {
		if(menu_cookie == 'lienhe') {					
				obj.className = 'Menu-lienhe-Over';			
			}
			else
			{
				obj.className = 'Menu-lienhe-Out';			
			}
		}
		
		obj = document.getElementById('english');
		if( obj != null) {
		if(menu_cookie == 'english') {					
				Over(obj, 'english');
			}
			else
			{
				Out(obj, 'english');			
			}
		}
	}
	else // init for menu english
	{
		
		obj = document.getElementById('home');

		if( obj != null) {
		if(menu_cookie == 'home') {					
				obj.className = 'Menu-home-Over';			
			}
			else
			{
				obj.className = 'Menu-home-Out';			
			}
		}
	//	alert(obj.className);
		obj = document.getElementById('news');
		if( obj != null) {
		if(menu_cookie == 'news') {					
				obj.className = 'Menu-news-Over';			
			}
			else
			{
				obj.className = 'Menu-news-Out';			
			}
		}
		
		obj = document.getElementById('forum');
		if( obj != null) {
		if(menu_cookie == 'forum') {					
				obj.className = 'Menu-forum-Over';			
			}
			else
			{
				obj.className = 'Menu-forum-Out';			
			}
		}
		
		obj = document.getElementById('download');
		if( obj != null) {
		if(menu_cookie == 'download') {					
				obj.className = 'Menu-download-Over';			
			}
			else
			{
				obj.className = 'Menu-download-Out';			
			}
		}
		
		obj = document.getElementById('contact');
		if( obj != null) {
		if(menu_cookie == 'contact') {					
				obj.className = 'Menu-contact-Over';			
			}
			else
			{
				obj.className = 'Menu-contact-Out';			
			}
		}
		
		obj = document.getElementById('vietnamese');
		if( obj != null) {
		if(menu_cookie == 'vietnamese') {					
				Over(obj, 'vietnamese');
			}
			else
			{
				Out(obj, 'vietnamese');			
			}
		}
	}
	SetCookie('menuCookieTemp',menu_cookie);
	SetCookie('menuCookie',null);
}
	
	function Over(obj, img)
	{
		obj.className='Menu-'+img+'-Over';}
	function Out(obj, img)
	{
		var menu_cookie = GetCookie('menuCookieTemp');
		if(menu_cookie != img)
		{
			obj.className='Menu-'+img+'-Out';
		}
	}
		
	function action_Link(url, name, target){
		//alert(name);
		SetCookie('menuCookie', name);
		if(target =='')
		{
			document.location=url;
		}
		else
		{
			window.open(url,'name');
		}
		
	}
	
	function openAdv(url) {
	var options = "width=800,height=600";	
	new_window = window.open(url, "Adv", options);
	new_window.focus();
}

