var http 		= khoitao_ajax();
var lastUrl 	= '';
var current_url = '';
var interval = '';

// NDK Loading
var ie45,ns6,ns4,dom;
if (navigator.appName=="Microsoft Internet Explorer") ie45=parseInt(navigator.appVersion)>=4;
else if (navigator.appName=="Netscape"){  ns6=parseInt(navigator.appVersion)>=5;  ns4=parseInt(navigator.appVersion)<5;}
dom=ie45 || ns6;

// AJAX INIT

function $(id) {
	return document.all ? document.all[id] :   dom ? document.getElementById(id) :   document.layers[id];
}

function khoitao_ajax()
{
	var x;
	try 
	{
		x	=	new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
    	try 
		{
			x	=	new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(f) { x	=	null; }
  	}
	if	((!x)&&(typeof XMLHttpRequest!="undefined"))
	{
		x=new XMLHttpRequest();
  	}
	return  x;
}

function show_Loading() {
	obj = $('LoadingDiv');
	obj.style.display = 'block';
}

function hide_Loading() {
	obj = $('LoadingDiv');
	obj.style.display = 'none';
}
function show_Sending() {
	obj = $('SendingDiv');
	obj.style.display = 'block';
}

function hide_Sending() {
	obj = $('SendingDiv');
	obj.style.display = 'none';
}
// Function for MainFrame
function main_loader(current_url,act)
{
	ifr.location = 'f.php?act='+current_url;
	zplayer = 'MainFrame';
	if (act == "Music_Nghe") 	zplayer = 'MusicFrame';
	try
	{
		//if (document.all && MainFrame.filters)	$('MainFrame').filters.blendTrans.apply();
		show_Loading();
		
		current_url = encodeURIComponent(current_url);
		http.open("POST", "process.php");
		// http.setRequestHeader("Cache-control","no-cache");
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				hide_Loading();
				//if (document.all && MainFrame.filters)	$('MainFrame').filters.blendTrans.play();
				
				if (http.status == 200)
				{
					el = $(zplayer);
					el.style.display = 'block';
					el.innerHTML = http.responseText;
					
					if (act == "Gallery_Xem") initLightbox();
				}
			}
		}
		http.send('act='+act+'&url='+current_url);
	}
	catch(e){}
	finally{}
}
// URL LOADING
function startLoad() {
	interval = setInterval('urlCheck()',100);
}
function urlCheck()
{
	document.title = "Đoàn TNCS Hồ Chí Minh - Thành phố Đà Nẵng";
	
	url	=	window.location.href;
	if (url.indexOf('?') != -1 ) {
		url_temp = url.split('?');
		clearInterval(interval);
		href = url_temp[0];
		
		url=url+'#/';
		url=url.split('#/');
		if (url[1]) window.location.href = href+'#/'+url[1];
		return;
	}
	if (url != '' && url!=lastUrl)
	{
		loadPage();
		lastUrl=url;
	}
}
function loadPage() {
	act = getVar(window.location.href,0);
	if (act) {
		current_url=window.location.href;
		current_url=current_url+'#/';
		current_url=current_url.split('#/');
		current_url=current_url[1];
		if (current_url) main_loader(current_url,act);
	}
}
function getVar(url,cnt)
{
	url=url+'#/';
	url=url.split('#/');
	if (!url[1]) window.location.href = '#/Home/';
	url=url[1];
	url=url+'/';
	url=url.split('/');
	// if (url[0] == 'Logout')		window.location.href = '?refresh=1';
	if (cnt != -1) {
		url=url[cnt];
		if (!url) return '';
	}
	return url;
}

// Lien He act
function lienhe_submit()
{
	var abcform = document.getElementById('frmLienHe');
    var elements = abcform.elements;
    var query = "act=LienHe&";
	
    for ( i = 0; i < elements.length-1; i++ )
    {
		query = query + elements.item(i).name + "=" + elements.item(i).value + "&";
    }
    query = query + elements.item(i).name + "=" + elements.item(i).value;

	var http = khoitao_ajax();
	try
	{
		show_Sending();
		http.open("POST", "process.php");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Cache-control", "no-cache");		
    	http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				hide_Sending();
				if (http.status == 200)
				{
					el = $('LienHe_Loader');
					el.style.display = 'block';
					el.innerHTML = http.responseText;
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	return false;
}