// 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);
}


datHang = function(frm)
{
	if (frm.txtName.value == '')
	{
		 alert('Bạn chưa nhập Họ & Tên');
		 frm.txtName.focus();
		 return false;
	}
	
	email = frm.txtEmail.value;

	if (email == '' || !email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/)){
		alert('Địa chỉ email không hợp lệ');
		frm.txtEmail.focus();
		return false;
	}
	if (frm.txtContent.value == ''){
		alert('Bạn chưa nhập nội dung đặt hàng');
		frm.txtContent.focus();
		return false;
	}
	
	if (!confirm('Gởi đi đơn đặt hàng ?'))
	{
		return false;
	}
	
	txtName			=	encodeURIComponent(frm.txtName.value);
	txtTel			=	encodeURIComponent(frm.txtTel.value);
	txtEmail		=	encodeURIComponent(frm.txtEmail.value);
	txtContent		=	encodeURIComponent(frm.txtContent.value);
	
	var	query	=	"txtName="+txtName+"&txtTel="+txtTel+"&txtEmail="+txtEmail+"&txtAddress="+txtAddress+"&txtContent="+txtContent;
	var http 	=	khoitao_ajax();
	try
	{
		http.open("POST", "/dat_hang.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")
					{
						alert(x);
					}
					else
					{
						alert("Đã thực hiện đặt hàng thành công.");
						document.frmContact.btnSend.disabled = "disabled";
					}
				}
				else
				{
					alert('Có lỗi hệ thống. Xin vui lòng thử lại sau');
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	return false;
}


isNumber = function(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	
	return !(charCode > 31 && (charCode < 48 || charCode > 57));
}


addToCart = function(itemId)
{
	$.ajax({
		type	:	'POST',
		url		:	'/cart.php',
		data	:	{ id : itemId },
		success	:	function(msg)
		{
			if (msg.length)
			{
				$("#totalAmount").html(msg + ' <sup>đ</sup>');
				alert("Đã thêm vào giỏ hàng");
			}
		}
	});
}



