// AJAX INIT
function $$$(id) {
	return document.getElementById(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	Forward(url)
{
	window.location.href = url;
}
function	_postback()
{
	return void(1);
}
/*So sanh height cua 2 the div tgp_body_content & tgp_right*/
function EqualHeight(elements) { 
    //Xác định chiều cao của cột cao nhất 
    tallest = 0; 
    elements.each(function() { 
        elementHeight = jQuery(this).height(); 
        if(elementHeight > tallest) { 
            tallest = elementHeight; 
        } 
    }); 
    //Chỉnh chiều cao cho tất cả các cột 
    //theo chiều cao của cột cao nhất 
    elements.height(tallest); 
} 

function setHomepage()
{
	document.body.style.behavior='url(#default#homepage)';
  	document.body.setHomePage('http://www.flat.vn');
}


function kt_member(frm)
{

	txt_username = frm.txt_username.value;
	txt_password = frm.txt_password.value;
	
	login_form =	$$$('kt_member').innerHTML;
	$$$('kt_member').innerHTML = "<center><img src='/images/bok-loader.gif' style='margin-top:20px;' /></center>";
	
	if(!txt_username)
	{
		$.jGrowl("Vui lòng nhập tên đăng nhập!");
		frm.txt_username.focus();
		$$$('kt_member').innerHTML = login_form;
		return false;
	}
	if(!txt_password)
	{
		$.jGrowl("Vui lòng nhập mật khẩu!");
		frm.txt_password.focus();
		$$$('kt_member').innerHTML = login_form;
		return false;
	}
	else

	{
		var	query	=	"act=kt_member&txt_username="+txt_username+"&txt_password="+txt_password;
		var http 	=	khoitao_ajax();
		try
		{
			http.open("POST", "/action/the-gioi-phang.html");
			http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			http.setRequestHeader("Cache-control", "no-cache");		
			http.onreadystatechange = function()
			{
				if (http.readyState == 4)
				{
					if (http.status == 200)
					{
						
						x = http.responseText;
						if (x != "OK")
						{
							$.jGrowl(x);
							$$$('kt_member').innerHTML = login_form;
						}
						else
						{
							window.location.reload();
							// Forward('http://flat.vn');
						}
					}
					else
					{
						$$$('kt_member').innerHTML = login_form;
						return false;
					}
				}
			}
			http.send(query);
		}
		catch (e)
		{
		}
		return false;
	}
}
function send_res(frmContact)
{
	txtSubject = frmContact.txtSubject.value
	txtEmail = frmContact.txtEmail.value	
	txtNoidung = frmContact.txtNoidung.value
    txtcard=frmContact.txtcard.value
    txtlink=frmContact.txtlink.value
    if(!txtSubject)
	{
		
		alert("Vui lòng nhập tiêu đề !");
		frmContact.txtSubject.focus();
		return false;
	}
	if(!txtEmail)
	{
		alert("Vui lòng nhập email !");
		frmContact.txtEmail.focus();
		return false;
	}
	if(!txtNoidung)
	{
		alert("Vui lòng nhập nội dung !");
		frmContact.txtNoidung.focus();
		return false;
	}
	
	email=txtEmail
	if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/)){
		alert('Địa chỉ email không hợp lệ.');
		frmContact.txtEmail.focus();
		return false;
		
	}
	
	else
	{
	   $("#formlienhe").hide();
		var	query	=	"act=send_res&txtlink="+txtlink+"&txtcard="+txtcard+"&txtSubject="+txtSubject+"&txtEmail="+txtEmail+"&txtNoidung="+txtNoidung;
		var http 	=	khoitao_ajax();
		try
		{
			//$$$('inner').innerHTML = "<center style='margin-top:50px'><img src='/images/loading.gif' /></center>";
			http.open("POST", "/action.php");
			http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			http.setRequestHeader("Cache-control", "no-cache");		
			http.onreadystatechange = function()
			{
				if (http.readyState == 4)
				{
					if (http.status == 200)
					{
						x = http.responseText;
						if (x.substring(0,2) == "OK")
						{
							alert(x.substring(3));
                            $("#formlienhe").remove();
							//window.location.href = "/home/";
						}
						else
						{
							alert(x);
                            $("#formlienhe").show();
							//window.location.href = "/";
						}
					}
					else
					{
							return false;
					}
				}
			}
			http.send(query);
		}
		catch (e)
		{
		}
		return false;
	}
}
//cuongnt - sign out kt member
function kt_member_signout()
{
		
		var	query	=	"act=kt_member_signout";
		var http 	=	khoitao_ajax();
		try
		{
			$$$('kt_member').innerHTML = "<center><img src='/images/bok-loader.gif' style='margin-top:20px;' /></center>";
			http.open("POST", "/action/");
			http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			http.setRequestHeader("Cache-control", "no-cache");		
			http.onreadystatechange = function()
			{
				if (http.readyState == 4)
				{
					if (http.status == 200)
					{
						$$$('kt_member').innerHTML = http.responseText;
						window.location.href = "/";
					}
					else
					{
							return false;
					}
				}
			}
			http.send(query);
		}
		catch (e)
		{
		}
		
		return false;
}


function add_bookcards(card)
{
	
	var	query	=	"act=bookmark&card="+card;
	var http 	=	khoitao_ajax();
	try
	{	
		http.open("POST", "/action/the-gioi-phang.html");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Cache-control", "no-cache");		
    	http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200)
				{
					x = http.responseText;
					if (x != "OK")
					{
						$.jGrowl(x);

					}
					else
					{
						$.jGrowl("Thêm vào Sổ Danh Thiếp thành công!");
					}
			
				}
				else
				{
					$.jGrowl("Có lỗi hệ thống, xin vui lòng thử lại sau!");
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	return false;
}
function dang_ky(frm_res)
{

	
	var val = 0;
	username = frm_res.txt_username.value;
	password = frm_res.txt_password.value;
	password2 = frm_res.txt_password2.value;
	ten = frm_res.txt_ten.value;
	dia_chi = frm_res.txt_dia_chi.value;
	dien_thoai = frm_res.txt_dien_thoai.value;
	email = frm_res.txt_email.value;
	if (!username){
	$.jGrowl('Vui lòng nhập họ tên.');
	document.frm_res.txt_username.focus();
		 return false;
		}
	if (!password){
	 $.jGrowl('Vui lòng nhập mật khẩu.');
	document.frm_res.txt_password.focus();
	 return false;
	}
	if (!password2){
	 $.jGrowl('Vui lòng nhập xác nhận mật khẩu.');
	document.frm_res.txt_password2.focus();
	 return false;
	}
	if (!ten){
	 $.jGrowl('Vui lòng nhập họ và tên.');
	document.frm_res.txt_ten.focus();
	 return false;
	}
	if (!dia_chi){
	 $.jGrowl('Vui lòng nhập địa chỉ.');
document.frm_res.txt_dia_chi.focus();
	 return false;
	}
	if (!dien_thoai){
	 $.jGrowl('Vui lòng nhập điện thoại.');
	document.frm_res.txt_dien_thoai.focus();
	 return false;
	}
	if(password != password2)
	{
		$.jGrowl("Xác nhận mật khẩu không chính xác");	
	document.frm_res.txt_password2.focus();	
	 return false;
	}

	if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/)){
		$.jGrowl('Địa chỉ email không hợp lệ.'); 
	document.frm_res.txt_email.focus();	
		return false;
	}
	
	var	query	=	"act=tgp_register&txt_username="		+username+
										"&txt_password="	+password+
										"&txt_ten="			+ten+
										"&txt_dia_chi="		+dia_chi+
										"&txt_dien_thoai="	+dien_thoai+
										"&txt_thanh_vien="	+val+
										"&txt_email="		+email;
	var http 	=	khoitao_ajax();
	try
	{	
		http.open("POST", "/action/the-gioi-phang.html");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Cache-control", "no-cache");		
    	http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200)
				{
					x = http.responseText;
					if (x != "OK")
					{
						if (x == "ERROR")
						{
							$.jGrowl('Nhập sai mã xác nhận!');
						}
						else
						{
							$.jGrowl(x);
						}							
						
					}
					else
					{
						$.jGrowl('Đăng ký thành công.Tài khoản của bạn đã được kích hoạt - Hãy đăng nhập hệ thống');	
						window.location = "/home/";
					}
			
				}
				else
				{
					$.jGrowl('Có lỗi hệ thống. Xin vui lòng thử lại sau!');
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	return false;
}

function edit_tai_khoan(edit)
{
	var val = 0;
	password = edit.txt_password.value;
	password2 = edit.txt_password2.value;
	ten = edit.txt_ten.value;
	dia_chi = edit.txt_dia_chi.value;
	dien_thoai = edit.txt_dien_thoai.value;
	email = edit.txt_email.value;
	
	if (!ten){
	 $.jGrowl('Vui lòng nhập họ và tên.');
	 document.edit.txt_ten.focus();
	 return false;
	}
	
	if (!dia_chi){
	 $.jGrowl('Vui lòng nhập địa chỉ.');
	 document.edit.txt_password2.focus();
	 return false;
	}
	
	if (!dien_thoai){
	 $.jGrowl('Vui lòng nhập điện thoại.');
	 document.edit.txt_username.focus();
	 return false;
	}
	
	if(password != password2)
	{
		$.jGrowl("Xác nhận mật khẩu không chính xác");	
		document.edit.txt_password2.focus();
		return false;
	}
	
	if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/)){
		$.jGrowl('Địa chỉ email không hợp lệ.');
		document.edit.txt_email.focus();
		return false;
	}
	
	var	query	=	"act=edit_tai_khoan&txt_password="	+password+
										"&txt_ten="			+ten+
										"&txt_dia_chi="		+dia_chi+
										"&txt_dien_thoai="	+dien_thoai+
										"&txt_email="		+email;
										
	var http 	=	khoitao_ajax();
	try
	{	
		http.open("POST", "/action/the-gioi-phang.html");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Cache-control", "no-cache");		
    	http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200)
				{
					x = http.responseText;

					if (x != "OK")
					{
							$.jGrowl(x);
					}
					else
					{
						
						$.jGrowl('Thay đổi thông tin tài khoản thành công!');	
						window.location = "/thong-tin-tai-khoan/";
					}
			
				}
				else
				{
					$.jGrowl('Có lỗi hệ thống. Vui lòng thử lại sau!');
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	return false;
}


function re_email(frm)
{
	txt_email = frm.txt_email.value;
	email=txt_email
	if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/)){
		$.jGrowl('Địa chỉ email không hợp lệ.');
		frm.txt_email.focus();
		return false;
	}
	
	var	query	=	"act=tgp_forgot&txt_email="	+txt_email;
	var http 	=	khoitao_ajax();
	try
	{	
		http.open("POST", "/action.php");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Cache-control", "no-cache");		
    	http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200)
				{
					x = http.responseText;
					if (x != "OK")
					{
						$.jGrowl(x);
					}
					else
					{
						$.jGrowl('Password mới đã được gửi đến mail của bạn.Vui lòng check email!');						
					}
				}
				else
				{
					$.jGrowl('Có lỗi hệ thống. Vui lòng thử lại sau!');
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	return false;
}

function up2vip(vip)
{
	if ( vip.thoihan[0].checked == false && vip.thoihan[1].checked == false && vip.thoihan[2].checked == false && vip.thoihan[3].checked == false) 
	{
		$.jGrowl('Bạn chưa chọn thời hạn!');
		return false;
	}
	else
	{
		var a = confirm("Bạn có thật sự muốn đăng card này lên KHÁCH HÀNG VÀNG?");
		if (a)
		{
			txt_user = vip.user.value;
			txt_card = vip.card.value;
			if ( vip.thoihan[0].checked == true )  point = vip.thoihan[0].value;	
			if ( vip.thoihan[1].checked == true )  point = vip.thoihan[1].value;	
			if ( vip.thoihan[2].checked == true )  point = vip.thoihan[2].value;	
			if ( vip.thoihan[3].checked == true )  point = vip.thoihan[3].value;	
					
			
			var	query	=	"act=up2vip&point="+point+"&user="+txt_user+"&card="+txt_card;
			var http 	=	khoitao_ajax();
			try
			{	
				http.open("POST", "/action/the-gioi-phang.html");
				http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				http.setRequestHeader("Cache-control", "no-cache");		
				http.onreadystatechange = function()
				{
					if (http.readyState == 4)
					{
						if (http.status == 200)
						{
							x = http.responseText.split("|");
							if (x[0] != "OK")
							{
								$.jGrowl(x);
								return false;
							}
							else
							{
								$.jGrowl('Đã đăng card này lên<br> KHÁCH HÀNG VÀNG thành công!<br>Số điểm của bạn còn '+ x[1]);
								$$$('point1').innerHTML = x[1];	
								$$$('point2').innerHTML = x[1];
								return false;
							}
						}
						else
						{
							$.jGrowl('Có lỗi hệ thống. Vui lòng thử lại sau!');
						}
					}
				}
				http.send(query);
			}
			catch (e)
			{
			}
			return false;	
			}
		else 
		{
			return false;
		}
	}
}

function cms_friends(frm)
{
	email_list = encodeURIComponent(frm.email_list.value);
	id	= encodeURIComponent(frm.id.value);
	cat	= encodeURIComponent(frm.cat.value);
	
	xyz	=	$$$('send_loading').innerHTML;
	$$$('send_loading').innerHTML =	"<center><img src=\"/images/81.gif\" /></center>";
	
	var	query	=	"act=cms_friends&email_list="+email_list+"&id="+id+"&cat="+cat;
	var http 	=	khoitao_ajax();
	try
	{
		http.open("POST", "/action/the-gioi-phang.html");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Cache-control", "no-cache");		
    	http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200)
				{
					x = http.responseText;
					$$$('send_loading').innerHTML = x;
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	
	return false;
}



$(function () {
	$('.bubbleinfo').each(function () {
		var distance = 10;
		var time = 250;
		var hideDelay = 300;

		var hideDelayTimer = null;

		var beingShown = false;
		var shown = false;
		var trigger = $('.trigger', this);
		var info = $('.popup', this).css('opacity', 0);


		$([trigger.get(0), info.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				// reset position of info box
				beingShown = true;

				info.css({
					top: 20,
					left: 1,
					display: 'block'
				}).animate({
					top: '-=' + distance + 'px',
					opacity: 0.8
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
			}

			return false;
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				info.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					info.css('display', 'none');
				});

			}, hideDelay);
			return false;
		});
	});
});

