var http = createRequestObject();
var lastUrl = '';
var current_url = '';
var field = '';
var interval = '';
var loading=0;
var update = '001';
var loadingText = "<img src='images/loading.gif' align=absmiddle hspace=5><font color=red>Loading ...</font>";

function createRequestObject() {
	var xmlhttp;
	try {
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e) 
	{
		try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		catch(f) { xmlhttp=null; }
	}
	if(!xmlhttp&&typeof XMLHttpRequest!="undefined") 
	{
		xmlhttp=new XMLHttpRequest();
	}
	return  xmlhttp;
}

function request_DanhSach()
{
	var ng_lon = document.getElementById('ng_lon');
	if ( !isNumber(ng_lon) ) {
		alert("Vui lòng nhập vào kiểu số !");
		ng_lon.value = 0;
		return;
	}
	else	ng_lon = parseInt(ng_lon.value);
	var tre_em = document.getElementById('tre_em');
	if ( !isNumber(tre_em) ) {
		alert("Vui lòng nhập vào kiểu số !");
		tre_em.value = 0;
		return;
	}
	else	tre_em = parseInt(tre_em.value);
	var tre_nho = document.getElementById('tre_nho');
	if ( !isNumber(tre_nho) ) {
		alert("Vui lòng nhập vào kiểu số !");
		tre_nho.value = 0;
		return;
	}
	else	tre_nho = parseInt(tre_nho.value);
	var total = ng_lon + tre_em + tre_nho;
	document.getElementById('tongso').value = total;
	if( !isNaN(total) && total > 1 )
	{
		field = 'TotalGuest';
		try{
			document.getElementById(field).innerHTML = '<img src="images/loading3.gif" width="235" height="12">';
			http.open('POST', 'actions.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
			http.onreadystatechange = ConfirmCode_handleResponse;
			http.send('url=DANH_SACH_GUEST,'+ total);
		}
		catch(e){}
		finally{}
	}
	else	document.getElementById(field).innerHTML = '';
}
function request_DanhSach_en()
{
	var ng_lon = document.getElementById('ng_lon');
	if ( !isNumber(ng_lon) ) {
		alert("Please enter the numeric!");
		ng_lon.value = 0;
		return;
	}
	else	ng_lon = parseInt(ng_lon.value);
	var tre_em = document.getElementById('tre_em');
	if ( !isNumber(tre_em) ) {
		alert("Please enter the numeric!");
		tre_em.value = 0;
		return;
	}
	else	tre_em = parseInt(tre_em.value);
	var tre_nho = document.getElementById('tre_nho');
	if ( !isNumber(tre_nho) ) {
		alert("Please enter the numeric!");
		tre_nho.value = 0;
		return;
	}
	else	tre_nho = parseInt(tre_nho.value);
	var total = ng_lon + tre_em + tre_nho;
	document.getElementById('tongso').value = total;
	if( !isNaN(total) && total > 1 )
	{
		field = 'TotalGuest';
		try{
			document.getElementById(field).innerHTML = '<img src="images/loading3.gif" width="235" height="12">';
			http.open('POST', 'actions.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
			http.onreadystatechange = ConfirmCode_handleResponse;
			http.send('url=DANH_SACH_GUEST,'+ total);
		}
		catch(e){}
		finally{}
	}
	else	document.getElementById(field).innerHTML = '';
}

function request_Tong()
{
	var ng_lon = document.getElementById('ng_lon');
	if ( !isNumber(ng_lon) ) {
		alert("Vui lòng nhập vào kiểu số !");
		ng_lon.value = 0;
		return;
	}
	else	ng_lon = parseInt(ng_lon.value);
	var tre_em = document.getElementById('tre_em');
	if ( !isNumber(tre_em) ) {
		alert("Vui lòng nhập vào kiểu số !");
		tre_em.value = 0;
		return;
	}
	else	tre_em = parseInt(tre_em.value);
	var tre_nho = document.getElementById('tre_nho');
	if ( !isNumber(tre_nho) ) {
		alert("Vui lòng nhập vào kiểu số !");
		tre_nho.value = 0;
		return;
	}
	else	tre_nho = parseInt(tre_nho.value);
	var total = ng_lon + tre_em + tre_nho;
	document.getElementById('tongso').value = total;
}

function check_Number(ValueID)
{
	var giatri = document.getElementById(ValueID);
	if ( !isNumber(giatri) ) {
		alert("Vui lòng nhập vào kiểu số !");
		giatri.value = 0;
		return;
	}
}

function request_NgayVe(value)
{
	if( !parseInt(value) )
		document.getElementById('return_a').style.display = 'none';
	else
		document.getElementById('return_a').style.display = '';
}

function request_SendMail()
{
	field = 'SendMail';
	$y_name = document.getElementById('yname');
	if( $y_name.value == '' )
	{
		alert("Vui lòng cho biết tên của bạn !");
		$y_name.focus();
		return;
	}
	$y_mail = document.getElementById('ymail');
	if( !isEmail($y_mail.value) )
	{
		alert("Vui lòng cho biết địa chỉ e-mail của bạn !");
		$y_mail.focus();
		return;
	}
	$t_mail = document.getElementById('tmail');
	if( !isEmail($t_mail.value) )
	{
		alert("Vui lòng cho biết địa chỉ e-mail của người nhận !");
		$t_mail.focus();
		return;
	}
	$url = document.getElementById('url').value;
	current_url = encodeURIComponent($y_name.value +','+ $y_mail.value +','+ $t_mail.value +','+ $url);
	try{
		document.getElementById(field).innerHTML = '<p align=center><br><img src="images/loading2.gif" width="130" height="60"></p>';
		http.open('POST', 'actions.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = ConfirmCode_handleResponse;
		http.send('url=SEND_MAIL,'+ current_url);
	}
	catch(e){}
	finally{}
}

function reload_SelectDepart(value)
{
	if( !parseInt(value) )
		document.getElementById('OtherDepart').style.display = '';
	else
		document.getElementById('OtherDepart').style.display = 'none';
}

function reload_News( SiteID )
{
	field = 'indexNews';
	try{
		document.getElementById(field).innerHTML = '<p align=center><img src="images/loading3.gif" width="235" height="12"></p>';
		/*if (loading == 0) 
		{
			loading = 1;
			show_bOOm();
		}*/
		http.open('POST', 'actions.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
		http.onreadystatechange = ConfirmCode_handleResponse;
		http.send('url=SITE_NEWS,'+ SiteID);
	}
	catch(e){}
	finally{}
}

function reload_ConfirmCode() {
	field = 'Confirmation_Code';
	try{
		document.getElementById(field).innerHTML = loadingText;
		http.open('POST', 'actions.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = ConfirmCode_handleResponse;
		http.send('url='+ field);
	}
	catch(e){}
	finally{}
}

function request_NgayKhoiHanh(value, vnd_usd, usd, TourID)
{
	field = 'lichkhoihanh';
	if(value > 0)
	{
		//alert('url=LICH_KHOI_HANH,'+ value +','+ vnd_usd);
		try{
			document.getElementById(field).innerHTML = '<p align=center><img src="images/loading3.gif" width="235" height="12"></p>';
			http.open('POST', 'actions.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = ConfirmCode_handleResponse;
			http.send('url=LICH_KHOI_HANH,'+ value +','+ vnd_usd +','+ usd);
		}
		catch(e){}
		finally{}
	}
	else if( value == 0)
	{
		try{
			document.getElementById(field).innerHTML = '<p align=center><img src="images/loading3.gif" width="235" height="12"></p>';
			http.open('POST', 'actions.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = ConfirmCode_handleResponse;
			http.send('url=LIEN_HE,'+ TourID);
		}
		catch(e){}
		finally{}
	}
}

function reload_PT_ThanhToan(value, view_thoathuan)
{
	field = 'pttt';
	try{
		document.getElementById(field).innerHTML = '<p align=center><img src="images/loading3.gif" width="235" height="12"></p>';
		http.open('POST', 'actions.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = ConfirmCode_handleResponse;
		http.send('url=PT_THANH_TOAN,'+ value +','+ view_thoathuan);
	}
	catch(e){}
	finally{}
}

function handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			hide_bOOm();
			loading = 0;
			field.innerHTML = http.responseText;
			field.style.display = "";
		}
  	}
	catch(e){}
	finally{}
}

function ConfirmCode_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200))
			document.getElementById(field).innerHTML = http.responseText;
  	}
	catch(e){}
	finally{}
}

// bOOm Loading
//configure below variable for menu width, position on page
var offsetleft= 0;
var offsettop = 0;

var ns4 = document.layers ? 1 : 0;
var ie4 = document.all ? 1 : 0;
var ns6 = document.getElementById && !document.all ? 1 : 0;

function makeStatic() 
{
	if (ie4) bOOm_Loading.style.pixelTop = document.body.scrollTop + offsettop;
	else if (ns6) document.getElementById("bOOm_Loading").style.top = window.pageYOffset + offsettop;
	else if (ns4) eval(document.bOOm_Loading.top = eval(window.pageYOffset + offsettop));
	setTimeout("makeStatic()", 0);
}

function menu_3()
{
	if (ns6||ie4||ns4)
		makeStatic();
}

// bOOm Loading
function show_bOOm()
{
	document.getElementById('bOOm_Loading').style.display = 'inline';
	menu_3();
}

function hide_bOOm()
{
	document.getElementById('bOOm_Loading').style.display = 'none';
	window.clearTimeout(menu_3);
}

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] == 'Home') window.location.href = './';
	if (cnt != -1) {
		url = url[cnt];
		if (!url) return '';
	}
	return 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) sendRequest(current_url, act);
	}
}

function urlCheck()
{
	url = window.location.href;
	if (url != '' && url != lastUrl)
	{
		loadPage();
		lastUrl = url;
	}
}

function startLoad() 
{
	interval = setInterval('urlCheck()',100);
}
/*------------------------------------------------------*/

function Shockwave_Flash( strSource, fwidth, fheight )
{
	var width=fwidth;
	var height=fheight;
    var s;
	s='<object width="'+ width +'" height="'+ height +'" codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000>';
		s+='<param name="movie" value="Avertise">';
		s+='<PARAM NAME="Src" VALUE="'+ strSource +'">';
		s+='<PARAM NAME="WMode" VALUE="Window">';
		s+='<PARAM NAME="Quality" VALUE="High">';
		s+='<PARAM NAME="AllowScriptAccess" VALUE="always">';
		s+='<PARAM NAME="Scale" VALUE="ShowAll">';
		s+='<embed src="'+ strSource +'" width="'+ width +'" height="'+ height +'" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash"></embed>';
	s+='</object>';

	document.write(s);
}

function isEmail(s)
{
    if (s=="") return false;
    if(s.indexOf(" ")>0) return false;
    var i = 1;
    var sLength = s.length;
    if (s.indexOf(".")==sLength) return false;
    if (s.indexOf(".")<=0) return false;
    if (s.indexOf("@")!=s.lastIndexOf("@")) return false;
    while ((i < sLength) && (s.charAt(i) != "@"))  { i++  }
    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    while ((i < sLength) && (s.charAt(i) != "."))  { i++  }
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    var str="1234567890abcdefghikjlmnopqrstuvwxyzABCDEFGHIKJLMNOPQRSTUVWXYZ-@._";
    for(var j=0;j<s.length;j++)
        if(str.indexOf(s.charAt(j))==-1)
            return false;
    return true;
}

function checkNumber( NumberID )
{
	if (!isNumber(document.getElementById(NumberID))) {
		alert("Vui lòng nhập vào kiểu số!.");
		document.getElementById(NumberID).value = 0;
		document.getElementById(NumberID).focus();
		return;
	}
}
function isNumber(obj)
{
	var checkOK		= "0123456789.";
	var checkStr	= obj.value;
	var allValid	= true;
	var validGroups	= true;
	var decPoints	= 0;
	var allNum		= "";
	var i,ch;	
	for (i = 0; i < checkStr.length; i++)
	{
		ch = checkStr.charAt(i);
		var j;
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch == ".")
		{
			allNum += ".";
			decPoints++;
		}
		else if (ch == "," && decPoints != 0)
		{
			validGroups = false;
			break;
		}
		else if (ch != ",")
			allNum += ch;
	}
	if (!allValid)
	{
		obj.focus();
		return false;
	}
	//
	else if (decPoints > 1 || !validGroups)
	{
		obj.focus();
		return false;
	}
	else
		return true;
}
