function refeshCaptcha(id)
{
	var e = document.getElementById(id);
	if(e)
	{
		var u = HOST + '/captcha.jpg?p=' + Math.random();
		e.src = u;
	}
}

function displayError(v, id)
{
	hideBlockUI();
	$('span.errMsg').hide();
	var spanId = (v==1) ? ('err_'+id) : ('exists_'+id);
	$('span#'+spanId).show();
	$('#'+id).select();
	$('#'+id).focus();
}

function hideBlockUI()
{
	$.unblockUI();
	$('embed').show();
}

function setValueCartHeader(v)
{
	$('#totalCart').html(v);
}

function showWaiting()
{
	var html = '<div class="caobox" id="caoboxPop"><table cellspacing="0" cellpadding="0">	<tbody>		<tr>			<td class="t_l"/>			<td class="t_c"/>			<td class="t_r"/>		</tr>		<tr>			<td class="c_l"/>			<td class="c_c">				<div class="caoboxIn">					<h1>Đang xử lý</h1>					<p><img src="images/ajax-loader-bar.gif" /></p>					<p>Hệ thống đang xử lý, vui lòng chờ trong giây lát.</p>					<!--					<a title="close" class="piro_close png" href="javascript:hideBlockUI();" style="visibility: visible; display: block;"></a>					-->				</div>			</td>			<td class="c_r"/>		</tr>		<tr>			<td class="b_l"/>			<td class="b_c"/>			<td class="b_r"/>		</tr>	</tbody></table></div>';
	$.blockUI
	({
		message : html ,
		css: 
		{
			border: 'none',
			cursor: 'pointer',
			top:  ($(window).height() - 300)/2 + 'px',
			left: ($(window).width() - 500)/2 + 'px',
			color : 'none'
		}
	});
}

function showLogin()
{
	$.getJSON
	(
		HOST + '/lightbox/login/?callback=?',
		{},
		function(data)
		{
			$.blockUI
			({
				message : data.html ,
				css: 
				{
					border: 'none',
					cursor: 'pointer',
					top:  ($(window).height() - 300)/2 + 'px',
					left: ($(window).width() - 500)/2 + 'px',
					color : 'none'
				}
			});
		}
	);
}

function saveCart(i, q, a)
{
	showWaiting();
	$.getJSON
	(
		HOST + '/cart/save/?callback=?',
		{
			'i'	: i,
			'q'	: q,
			'a'	: a
		},
		function(data)
		{
			var result = data.result;
			if(result=='1')
			{
				alertCart(i, a);
				setValueCartHeader(data.total_cart);
			}
			else if(result=='0')
			{
				alert('Sản phẩm đã hết hàng. Vui lòng chọn sản phẩm khác.');
			}
			else if(result=='-1')
			{
				alert('Bạn chỉ có thể chọn tối đa ['+data.limit+'] sản phẩm vào giỏ hàng của mình.');
			}
		}
	);
}

function alertCart(i, a)
{
	$.getJSON
	(
		HOST + '/lightbox/alert-cart/?callback=?',
		{'i':i},
		function(data)
		{
			$.blockUI
			({
				message : data.html ,
				css: 
				{
					border: 'none',
					cursor: 'pointer',
					top:  ($(window).height() - 300)/2 + 'px',
					left: ($(window).width() - 500)/2 + 'px',
					color : 'none'
				}
			});
			if(a=='0' || a==0)
			{
				setTimeout("window.location.href=window.location.href", 2*1000);
			}
			else
			{
				setTimeout("hideBlockUI()", 3*1000);
			}
		}
	);
}

function checkLengthContent(id, maxlength)
{
	var content = document.getElementById(id).value;
	if (content.length<=maxlength){
		return true;
	}
	return false;
}

function subContentByLength(id, maxlength)
{
	var e = document.getElementById(id);
	if (e)
	{
		e.value = e.value.substring(0, maxlength);
	}
}
