var requestTimeout='';

function addToShopcard(ajUrl,id,masp,tensp,name,price)
{//alert(id+'-'+name+'-'+captcha+'-'+contend);
	var xmlhttp = creatHTTPreq();	
	if (xmlhttp) 
	{
		var num=document.forms["frmProducts"].elements["text_quantity_"+id].value;
		var			poststr = "kind=ats";
		poststr = 	poststr + "&pd_id="  	+encodeURI(id);
		poststr = 	poststr + "&pd_masp="  	+encodeURI(masp);
		poststr = 	poststr + "&pd_tensp="  	+encodeURI(tensp);
		poststr = 	poststr + "&pd_name="  	+encodeURI(name);
		poststr = 	poststr + "&pd_price=" 	+encodeURI(price);
		poststr = 	poststr + "&pd_num=" 	+encodeURI(num);
		//alert(poststr);
		url = ajUrl+"include/ajax_shopcard.php";
		xmlhttp.open('POST', url, true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.onreadystatechange = function() 
		{
				parseResponse(xmlhttp);
		};
		xmlhttp.setRequestHeader("Content-length", poststr.length);
		xmlhttp.send(poststr);
		//error_mess = document.getElementById('ajax_error').innerHTML; 
		requestTimeout= setTimeout(function (){	
											 //commentError(error_mess,'0');
											 },10000);
		
	}
}
function updateItemInShopcard(ajUrl,id)
{//alert(id+'-'+name+'-'+captcha+'-'+contend);
	var xmlhttp = creatHTTPreq();	
	if (xmlhttp) 
	{
		var num=document.forms["frmProducts"].elements["text_quantity_"+id].value;
		if(isNumber(num))
		{
			document.getElementById("text_quantity_"+id).style.backgroundColor = 'white';
			var			poststr = "kind=uds";
			poststr = 	poststr + "&pd_id="  	+encodeURI(id);
			poststr = 	poststr + "&pd_num=" 	+encodeURI(num);
			//alert(poststr);
			url = ajUrl+"include/ajax_shopcard.php";
			xmlhttp.open('POST', url, true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function() 
			{
					parseResponse(xmlhttp);
			};
			xmlhttp.setRequestHeader("Content-length", poststr.length);
			xmlhttp.send(poststr);
			//error_mess = document.getElementById('ajax_error').innerHTML; 
			requestTimeout= setTimeout(function (){	 resetTimeout();	 },10000);
		}
		else
		{
			document.getElementById("text_quantity_"+id).style.backgroundColor = '#f2da6b';
			return false;
		}
		
	}
}
function parseResponse(request,prefix) 
{
	//alert(request.responseText);
	if (request.readyState == 4) 
	{
		if (request.status == 200 || request.status == 304) 
		{
			clearTimeout(requestTimeout);
			var arrRes= new Array();
			arrRes=request.responseText.split('[<!!!>]');
			arrRes[0]= parseInt(arrRes[0],10);//alert(arrRes[0]);
			if(arrRes[0]==1)
			{
				document.getElementById('soluongsanpham').innerHTML = formatCurrency(arrRes[1]);
				document.getElementById('tonggia').innerHTML = formatCurrency(arrRes[2]);
				if (arrRes[1]>0)
				{
					if (document.getElementById("img_cart"))
					{
						document.getElementById("img_cart").style.background = "url(images/layout/icon_cart_full.png)";
					}
				}
				else
				{
					if (document.getElementById("img_cart_full"))
					{
						document.getElementById("img_cart_full").style.background = "url(images/layout/icon_cart.png)";
					}
				}
			}
			if(arrRes[0]==2)
			{
				alert(arrRes[1]);
			}
			if(arrRes[0]==3)
			{
				document.getElementById("text_quantity_"+arrRes[1]).value = arrRes[2];
				document.getElementById("itemprice_"+arrRes[1]).innerHTML = formatCurrency(arrRes[5]);
				document.getElementById("tongiashopcard").innerHTML = formatCurrency(arrRes[4]);
				updateShopcard(arrRes[3],arrRes[4])
			}
		}
	}
}
function confirmShopcard(mess)
{
	if(checkNumItem())
	{
		if (confirm(mess))
		{
			document.forms["frmProducts"].submit();
		}
	}
	return false;
}
function checkCusform()
{
	var flag=true;
	var ctm_name=document.forms["frmCustomer"].elements["ctm_name"].value;
	var ctm_add=document.forms["frmCustomer"].elements["ctm_add"].value;
	var ctm_phone=document.forms["frmCustomer"].elements["ctm_phone"].value;
	var ctm_mail=document.forms["frmCustomer"].elements["ctm_mail"].value;
	//alert(ctm_name+'-'+ctm_add+'-'+ctm_phone+'-'+ctm_mail);
	
	if (ctm_name=='')
	{	document.getElementById("ctm_name").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_name").style.backgroundColor = 'white';	}
	if (ctm_add=='')
	{	document.getElementById("ctm_add").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_add").style.backgroundColor = 'white';	}
	if (!checkPhone(ctm_phone) || ctm_phone=='')
	{	document.getElementById("ctm_phone").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_phone").style.backgroundColor = 'white';	}
	if (!checkMail(ctm_mail) || ctm_mail=='')
	{	document.getElementById("ctm_mail").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_mail").style.backgroundColor = 'white';	}
	if (flag)
	{	document.forms["frmCustomer"].submit();		}
	return false;
}
function checkNumItem()
{
	var arr = new Array();
	var flag= true;
	var str= document.getElementById("arrID").innerHTML;
	arr = str.split('[!!!]');
	for (var i=0;i<arr.length;i++)
	{
		var id= "text_quantity_"+arr[i];
		var val = document.getElementById(id).value;
		if (isNumber(val) && val>=0)
		{	document.getElementById(id).style.backgroundColor = 'white';		}
		else
		{	document.getElementById(id).style.backgroundColor = '#f2da6b';	flag=false;		}
	}
	return flag;
}
function checkPhone(str) 
{
	var phone = /^[0-9]{9,15}$/;
	if (str.match(phone)) {
   		return true;
 	} else {
 		return false;
 	}
}
function isNumber(str) 
{
	if(!isNaN(str))
	{
		return true;
	}
	else
	{
		return false;
	}
}
function checkMail(str) 
{
	var mail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,3}$/;
	if (str.match(mail)) {
   		return true;
 	} else {
 		return false;
 	}
}
function updateShopcard(num,ttp)
{
	if (document.getElementById("soluongsanpham") && document.getElementById("tonggia"))
	{
		document.getElementById("soluongsanpham").innerHTML=num;
		document.getElementById("tonggia").innerHTML=formatCurrency(ttp);
	}
}
function resetTimeout()
{
	clearTimeout(requestTimeout);
}
/* phan customer */
function cuslogin(ajUrl)
{
	var flag = true;
	var username=document.getElementById("login_username").value;
	var pass=document.getElementById("login_pass").value;
	if (username=='')
	{	document.getElementById("login_username").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("login_username").style.backgroundColor = 'white';	}
	if (pass=='')
	{	document.getElementById("login_pass").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("login_pass").style.backgroundColor = 'white';	}
	if (flag)
	{
		var xmlhttp = creatHTTPreq();	
		if (xmlhttp) 
		{
			var			poststr = "kind=lgi";
			poststr = 	poststr + "&cus_username="  	+encodeURI(username);
			poststr = 	poststr + "&cus_pass=" 	+encodeURI(pass);
			//alert(poststr);
			url = ajUrl+"include/ajax_customer.php";
			xmlhttp.open('POST', url, true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function() 
			{
					cusparseResponse(xmlhttp);
			};
			xmlhttp.setRequestHeader("Content-length", poststr.length);
			xmlhttp.send(poststr);
			//requestTimeout= setTimeout(function (){	 resetTimeout();	 },10000);
		}
	}
	return false;
}
function cusLogout(ajUrl)
{
	
	var xmlhttp = creatHTTPreq();	
	if (xmlhttp) 
	{
		var			poststr = "kind=lgo";
		//alert(poststr);
		url = ajUrl+"include/ajax_customer.php";
		xmlhttp.open('POST', url, true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.onreadystatechange = function() 
		{
				cusparseResponse(xmlhttp);
		};
		xmlhttp.setRequestHeader("Content-length", poststr.length);
		xmlhttp.send(poststr);
		//requestTimeout= setTimeout(function (){	 resetTimeout();	 },10000);
	}
	return false;
}
function cusparseResponse(request) 
{
	//alert(request.responseText);
	if (request.readyState == 4) 
	{
		if (request.status == 200 || request.status == 304) 
		{//alert(request.responseText);
			clearTimeout(requestTimeout);
			var arrRes= new Array();
			arrRes=request.responseText.split('[<!!!>]');
			arrRes[0]= parseInt(arrRes[0],10);//alert(arrRes[0]);
			if(arrRes[0]==1)
			{
				document.getElementById('cus_logedname').innerHTML = arrRes[1];
				document.getElementById('cuslogedin').innerHTML = '1';
				hidediv('log_in');
				showdiv('logedincus');
			}
			if(arrRes[0]==2)
			{
				document.getElementById('login_mess').innerHTML = arrRes[1];
			}
			if(arrRes[0]==3)
			{
				document.getElementById('cus_logedname').innerHTML = '';
				document.getElementById('cuslogedin').innerHTML = '0';
				hidediv('logedincus');
				showdiv('log_in');
			}
		}
	}
}
// Phan khach hang
function checkformRC()
{
	var flag=true;
	var ctm_username=document.forms["frmCustomer"].elements["ctm_username"].value;
	var ctm_pass=document.forms["frmCustomer"].elements["ctm_pass"].value;
	var ctm_repass=document.forms["frmCustomer"].elements["ctm_repass"].value;
	var ctm_name=document.forms["frmCustomer"].elements["ctm_name"].value;
	var ctm_add=document.forms["frmCustomer"].elements["ctm_add"].value;
	var ctm_phone=document.forms["frmCustomer"].elements["ctm_phone"].value;
	var ctm_mail=document.forms["frmCustomer"].elements["ctm_mail"].value;
	//alert(ctm_username+'-'+ctm_pass+'-'+ctm_repass+'-'+ctm_name+'-'+ctm_add+'-'+ctm_phone+'-'+ctm_mail);
	if (ctm_username=='')
	{	document.getElementById("ctm_username").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_username").style.backgroundColor = 'white';	}
	if (ctm_pass=='')
	{	document.getElementById("ctm_pass").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{
		if (ctm_repass!=ctm_pass)
		{
			document.getElementById("ctm_pass").style.backgroundColor = '#f2da6b';
			document.getElementById("ctm_repass").style.backgroundColor = '#f2da6b';
			flag=false;	
		}
		else
		{
			document.getElementById("ctm_pass").style.backgroundColor = 'white';
			document.getElementById("ctm_repass").style.backgroundColor = 'white';	
		}
	}
	if (ctm_repass=='')
	{	document.getElementById("ctm_repass").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{
		if (ctm_repass!=ctm_pass)
		{
			document.getElementById("ctm_pass").style.backgroundColor = '#f2da6b';
			document.getElementById("ctm_repass").style.backgroundColor = '#f2da6b';
			flag=false;	
		}
		else
		{
			document.getElementById("ctm_pass").style.backgroundColor = 'white';
			document.getElementById("ctm_repass").style.backgroundColor = 'white';	
		}
	}
	if (ctm_name=='')
	{	document.getElementById("ctm_name").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_name").style.backgroundColor = 'white';	}
	if (ctm_add=='')
	{	document.getElementById("ctm_add").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_add").style.backgroundColor = 'white';	}
	if (!checkPhone(ctm_phone) || ctm_phone=='')
	{	document.getElementById("ctm_phone").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_phone").style.backgroundColor = 'white';	}
	if (!checkMail(ctm_mail) || ctm_mail=='')
	{	document.getElementById("ctm_mail").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_mail").style.backgroundColor = 'white';	}
	if (flag)
	{	document.forms["frmCustomer"].submit();		}
	return false;
}
function checkformEC()
{
	var flag=true;
	var ctm_username=document.forms["frmCustomer"].elements["ctm_username"].value;
	var ctm_pass=document.forms["frmCustomer"].elements["ctm_pass"].value;
	var ctm_repass=document.forms["frmCustomer"].elements["ctm_repass"].value;
	var ctm_name=document.forms["frmCustomer"].elements["ctm_name"].value;
	var ctm_add=document.forms["frmCustomer"].elements["ctm_add"].value;
	var ctm_phone=document.forms["frmCustomer"].elements["ctm_phone"].value;
	var ctm_mail=document.forms["frmCustomer"].elements["ctm_mail"].value;
	//alert(ctm_username+'-'+ctm_pass+'-'+ctm_repass+'-'+ctm_name+'-'+ctm_add+'-'+ctm_phone+'-'+ctm_mail);
	if (ctm_username=='')
	{	document.getElementById("ctm_username").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_username").style.backgroundColor = 'white';	}
	if (ctm_pass!='' || ctm_repass!='')
	{
		if (ctm_pass=='')
		{	document.getElementById("ctm_pass").style.backgroundColor = '#f2da6b';	flag=false;	}
		else
		{
			if (ctm_repass!=ctm_pass)
			{
				document.getElementById("ctm_pass").style.backgroundColor = '#f2da6b';
				document.getElementById("ctm_repass").style.backgroundColor = '#f2da6b';
				flag=false;	
			}
			else
			{
				document.getElementById("ctm_pass").style.backgroundColor = 'white';
				document.getElementById("ctm_repass").style.backgroundColor = 'white';	
			}
		}
		if (ctm_repass=='')
		{	document.getElementById("ctm_repass").style.backgroundColor = '#f2da6b';	flag=false;	}
		else
		{
			if (ctm_repass!=ctm_pass)
			{
				document.getElementById("ctm_pass").style.backgroundColor = '#f2da6b';
				document.getElementById("ctm_repass").style.backgroundColor = '#f2da6b';
				flag=false;	
			}
			else
			{
				document.getElementById("ctm_pass").style.backgroundColor = 'white';
				document.getElementById("ctm_repass").style.backgroundColor = 'white';	
			}
		}
	}
	if (ctm_name=='')
	{	document.getElementById("ctm_name").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_name").style.backgroundColor = 'white';	}
	if (ctm_add=='')
	{	document.getElementById("ctm_add").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_add").style.backgroundColor = 'white';	}
	if (!checkPhone(ctm_phone) || ctm_phone=='')
	{	document.getElementById("ctm_phone").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_phone").style.backgroundColor = 'white';	}
	if (!checkMail(ctm_mail) || ctm_mail=='')
	{	document.getElementById("ctm_mail").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_mail").style.backgroundColor = 'white';	}
	if (flag)
	{	document.forms["frmCustomer"].submit();		}
	return false;
}
function checkformCT()
{
	var flag=true;
	var ctm_name=document.forms["frmCustomer"].elements["ctm_name"].value;
	var ctm_mail=document.forms["frmCustomer"].elements["ctm_mail"].value;
	var ctm_noidung=document.forms["frmCustomer"].elements["ctm_noidung"].value;
	//alert(ctm_username+'-'+ctm_pass+'-'+ctm_repass+'-'+ctm_name+'-'+ctm_add+'-'+ctm_phone+'-'+ctm_mail);
	if (ctm_name=='')
	{	document.getElementById("ctm_name").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_name").style.backgroundColor = 'white';	}
	if (!checkMail(ctm_mail) || ctm_mail=='')
	{	document.getElementById("ctm_mail").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_mail").style.backgroundColor = 'white';	}
	if (ctm_noidung=='')
	{	document.getElementById("ctm_noidung").style.backgroundColor = '#f2da6b';	flag=false;	}
	else
	{	document.getElementById("ctm_noidung").style.backgroundColor = 'white';	}
	if (flag)
	{	document.forms["frmCustomer"].submit();		}
	return false;
}
/* phan Onepay*/
function checkformOP(mess1,mess2)
{
	var flag=true;
	if (document.getElementById('vpc_tenchuthe').value=='')
	{
		document.getElementById("vpc_tenchuthe").style.backgroundColor = '#f2da6b';
		document.getElementById('chuthe_error').innerHTML = mess1;
		flag=false;
	}
	else
	{
		document.getElementById("vpc_tenchuthe").style.backgroundColor = 'white';
		document.getElementById('chuthe_error').innerHTML='';
	}
	if (document.getElementById('vpc_sotaikhoan').value=='')
	{
		document.getElementById("vpc_sotaikhoan").style.backgroundColor = '#f2da6b';
		document.getElementById('taikhoan_error').innerHTML = mess2;
		flag=false;
	}
	else
	{
		document.getElementById("vpc_sotaikhoan").style.backgroundColor = 'white';
		document.getElementById('taikhoan_error').innerHTML='';
	}
	if(flag) 
	{
		document.forms['OPForm'].submit();
	}
	return false;
}
