/********************************************************************************************/
/* AJAX Simple Tabs by developersnippets, This code is intended for practice purposes.      */
/* You may use these functions as you wish, for commercial or non-commercial applications,  */
/* but please note that the author offers no guarantees to their usefulness, suitability or */
/* correctness, and accepts no liability for any losses caused by their use.                */
/********************************************************************************************/

var xmlhttp;
var xmlhttp1;
var xmlhttp2;
var xmlhttp3;
var xmlhttp4;
var sitepath;
var posNews=Math.floor(Math.random()*10);
var posImgRandom=Math.floor(Math.random()*10);
sitepath='/';
function newAjax()
{
	var obj;
	 try {
	 obj = new XMLHttpRequest(); /* e.g. Firefox */
	 } catch(e) {
	   try {
	   obj = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
	   } catch (e) {
		 try {
		 obj = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
		 } catch (E) {
		  obj = false;
		 } 
	   } 
	 }
	 return obj;
}

function getPrice(id,quantity,elQtyID,element_id) {
	xmlhttp=newAjax();
	pageUrl=sitepath+'mods/cart/updateCart.php?id='+id+'&qty='+quantity;
	xmlhttp.onreadystatechange = function() {dataResult(xmlhttp,element_id);};
	xmlhttp.open("GET",pageUrl,true);
	xmlhttp.send(null);
	if(quantity>99) document.getElementById(elQtyID).value=99;
	getTotalPrice();
	//cap nhat lai cart
	setCookie('_CARTLISTITEM','',3600);
}


function getTotalPrice() {
//     document.getElementById(divElementId).innerHTML = loadinglMessage;
	xmlhttp1=newAjax();
	pageUrl=sitepath+'mods/cart/updateCart.php?sum_price=1';
	sum_price_id='sum_price';	 
	xmlhttp1.onreadystatechange = function() {dataResult(xmlhttp1,sum_price_id);};
	xmlhttp1.open("GET",pageUrl,true);
	xmlhttp1.send(null);
  }

function dataResult(ajaxObj,divElementId) {
   var output = '';
   if(ajaxObj.readyState == 4) {
      if(ajaxObj.status == 200) {
         output = ajaxObj.responseText;
		 //alert(output);
         document.getElementById(divElementId).innerHTML = output;
         }
      }
  }
  
function getMailSaleOff()
{
	var inputVal = document.getElementById('mailSaleOff').value;
	//alert(inputVal);
	
	xmlhttp1=newAjax();
	pageUrl=sitepath+'updateMail.php?email='+inputVal;
	xmlhttp1.onreadystatechange = function() {
	   if(xmlhttp1.readyState == 4) {
		  if(xmlhttp1.status == 200) {
			 output = xmlhttp1.responseText;
			 //alert(output);
			 if(output=='ok')
			 	alert('Đăng ký mail thành công!');
			 else if(output=='err_1')
			 	alert('Email này đã có người đăng ký!');	
			 else
				alert('Email không hợp lệ!'+output);							 
			 }
		  }
		};
	xmlhttp1.open("GET",pageUrl,true);
	xmlhttp1.send(null);
}

function addCart(itemID)
{
	var lang=getLang();
	msg=(lang=='vn') ? 'Đã thêm sản phẩm vào giỏ hàng!' : 'Add item to cart successfully!';
	var show_total_cart = document.getElementById('total_cart');
	//alert(itemID);
	
	xmlhttp2=newAjax();
	pageUrl=sitepath+'mods/cart/addCart.php?id='+itemID;
	xmlhttp2.onreadystatechange = function() {
	   if(xmlhttp2.readyState == 4) {
		  if(xmlhttp2.status == 200) {
			 output = xmlhttp2.responseText;
			 	//alert(output);
				alert(msg);	
				//show_total_cart.innerHTML=output;
				window.location.href='dat-hang.html';
			 }
		  }
		};
	xmlhttp2.open("GET",pageUrl,true);
	xmlhttp2.send(null);
}

function delCart(itemID)
{
	var lang=getLang();
	msg=(lang=='vn') ? 'Đã lấy sản phẩm vào giỏ hàng!' : 'Remove an item successfully!';
	
	var show_total_cart = document.getElementById('cart_wrap');
	//alert(itemID);
	
	xmlhttp2=newAjax();
	pageUrl=sitepath+'mods/cart/delCart.php?id='+itemID;
	xmlhttp2.onreadystatechange = function() {
	   if(xmlhttp2.readyState == 4) {
		  if(xmlhttp2.status == 200) {
			 output = xmlhttp2.responseText;
			 	//alert(output);
				alert(msg);	
				show_total_cart.innerHTML=output;
			 }
		  }
		};
	xmlhttp2.open("GET",pageUrl,true);
	xmlhttp2.send(null);
}

function checkCart(url)
{
	//alert(checkCart);
	var lang=getLang();
	msg=(lang=='vn') ? 'Giỏ hàng chưa có gì! Mời bạn chọn mua hàng trước khi gửi yêu cầu thanh toán!' : 'Cart is empty! Please choose one or more product item before send payment request!';
	xmlhttp2=newAjax();
	pageUrl=sitepath+'mods/cart/checkCart.php';
	xmlhttp2.onreadystatechange = function() {
	   if(xmlhttp2.readyState == 4) {
		  if(xmlhttp2.status == 200) {
				output = xmlhttp2.responseText;
			 	//alert(output);
				if(output!=0)
				{
					//window.location.href=sitepath+url+'.html';
					window.location.href=sitepath+'?mod=UserInfo&p='+url;
				}else{
					alert(msg);
					window.location.href=sitepath+'?mod=Main';
				}
			 }
		  }
		};
	xmlhttp2.open("GET",pageUrl,true);
	xmlhttp2.send(null);
	
}


/*function showLastestNewsAjax()
{
	posNews++;
	if(posNews==10) posNews=0;
	getLastestNewsData();	
}
function showRandomImgProductAjax()
{
	posImgRandom++;
	if(posImgRandom==10) posImgRandom=0;
	
	getRandomImgProduct();	
	$("#showRandomImgProduct").hide();
	$("#showRandomImgProduct").fadeIn(500);
}

function getLastestNewsData()
{
	showLastestNewsElement=document.getElementById('showLastestNews');
	xmlhttp3=newAjax();
	pageUrl=sitepath+'getLastestNews.php?pos='+posNews;
	xmlhttp3.onreadystatechange = function() {
	   if(xmlhttp3.readyState == 4) {
		  if(xmlhttp3.status == 200) {
				output = xmlhttp3.responseText;				
				if(output==0) posNews=0; else
				showLastestNewsElement.innerHTML=output;
				//alert(posNews);
			 }
		  }
		};
	xmlhttp3.open("GET",pageUrl,true);
	xmlhttp3.send(null);
}*/

/*function getRandomImgProduct()
{
	showRandomImgProductElement=document.getElementById('showRandomImgProduct');
	xmlhttp4=newAjax();
	pageUrl=sitepath+'getRandomImgProduct.php?pos='+posImgRandom;
	xmlhttp4.onreadystatechange = function() {
	   if(xmlhttp4.readyState == 4) {
		  if(xmlhttp4.status == 200) {
				output = xmlhttp4.responseText;	
				if(output==0) posNews=0; else
				showRandomImgProductElement.innerHTML=output;
			 }
		  }
		};
	xmlhttp4.open("GET",pageUrl,true);
	xmlhttp4.send(null);
}*/
