/********************************************************************************************/
/* 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 rewritepath;
rewritepath='/';
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=rewritepath+'updateCart.php?id='+id+'&qty='+quantity;
	//alert(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=rewritepath+'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) {
   if(ajaxObj.readyState == 4) {
      if(ajaxObj.status == 200) {
         output = ajaxObj.responseText;
         document.getElementById(divElementId).innerHTML = output;
         }
      }
  }
  
function getMailSaleOff(inputVal)
{
	var inputVal = document.getElementById('mailSaleOff').value;
	//alert(inputVal);
	
	xmlhttp1=newAjax();
	pageUrl=rewritepath+'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ệ!');							 
			 }
		  }
		};
	xmlhttp1.open("GET",pageUrl,true);
	xmlhttp1.send(null);
}

function addCart(itemID)
{
	var show_total_cart = document.getElementById('total_cart');
	//alert(itemID);
	
	xmlhttp2=newAjax();
	pageUrl=rewritepath+'addCart.php?id='+itemID;
	xmlhttp2.onreadystatechange = function() {
	   if(xmlhttp2.readyState == 4) {
		  if(xmlhttp2.status == 200) {
			 output = xmlhttp2.responseText;
			 	//alert(output);
				alert('Đã thêm sản phẩm vào giỏ hàng!');	
				show_total_cart.innerHTML=output;
			 }
		  }
		};
	xmlhttp2.open("GET",pageUrl,true);
	xmlhttp2.send(null);
}

function delCart(itemID)
{
	var show_total_cart = document.getElementById('cart_wrap');
	//alert(itemID);
	
	xmlhttp2=newAjax();
	pageUrl=rewritepath+'delCart.php?id='+itemID;
	xmlhttp2.onreadystatechange = function() {
	   if(xmlhttp2.readyState == 4) {
		  if(xmlhttp2.status == 200) {
			 output = xmlhttp2.responseText;
			 	//alert(output);
				alert('Đã lấy sản phẩm ra khỏi giỏ hàng!');	
				show_total_cart.innerHTML=output;
			 }
		  }
		};
	xmlhttp2.open("GET",pageUrl,true);
	xmlhttp2.send(null);
}

function checkCart(url)
{
	//alert(checkCart);
	xmlhttp2=newAjax();
	pageUrl=rewritepath+'checkCart.php';
	xmlhttp2.onreadystatechange = function() {
	   if(xmlhttp2.readyState == 4) {
		  if(xmlhttp2.status == 200) {
				output = xmlhttp2.responseText;
			 	//alert(output);
				if(output!=0)
				{
					window.location.href=rewritepath+'thong-tin-khach-hang/'+url+'.html';
				}else{
					alert('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!');
					window.location.href=rewritepath+'thong-tin-khach-hang/gio-hang.html';
				}
			 }
		  }
		};
	xmlhttp2.open("GET",pageUrl,true);
	xmlhttp2.send(null);
	
}

function getBackgroundSite(val,new_w,h)
{
	var output='';
	var d=document;
	xmlhttp3=newAjax();
	pageUrl=rewritepath+'getBackgroundSite.php?screensize='+val;
	xmlhttp3.onreadystatechange = function() {
	   if(xmlhttp3.readyState == 4) {
		  if(xmlhttp3.status == 200) {
				output = xmlhttp3.responseText;
				//alert(output);
				d.getElementById('float_left').style.backgroundImage ="url("+output+")";
				d.getElementById('float_left').style.backgroundPosition ="top left";
				d.getElementById('float_right').style.backgroundImage ="url("+output+")";
				d.getElementById('float_right').style.backgroundPosition ="top right";
				
				d.getElementById('float_left').style.width=new_w+'px';
				d.getElementById('float_left').style.height=h+'px';
				d.getElementById('float_right').style.width=new_w+'px';
				d.getElementById('float_right').style.height=h+'px';		
			 }
		  }
		};
	xmlhttp3.open("GET",pageUrl,true);
	xmlhttp3.send(null);
}
