
///////////////////////////////////////
// Khối right
///////////////////////////////////////

function cart(){
	window.location.href='?xm=cart';
}
function delete_cart(msg){ 
	if (confirm(msg)){
		$.ajax({
			type : "GET",
			url : "widget.php",
			data : {
				xw : "delete_cart"
			}, 				
			beforeSend : function(xhr){
				$('#right_cart').html('<img src="theme/images/loader.gif">');	
			},
			success : function(response,status){
				alert('Giỏ hàng đã được xóa!');
				window.location.href='?';
				//$('#right_cart').html(response);				
			}	
		});	
	}  
}
function right_adv(){// Box quảng cáo
	$.ajax({
		type : "POST",
		url : "widget.php",
		dataType: "html", 
		data : {
			xw : "right_adv"
		}, 
		timeout: 6000,		
		beforeSend : function(xhr){
			$('#right_adv').html('<img src="theme/images/loader.gif">');				
		},
		error:function (request, error){
			right_adv();
		},		
		success : function(response,status){	
			$('#right_adv').html(response);
		}	
	});	  
}
function right_support_online(){// Box hỗ trợ trực tuyến
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=right_support_online", 				
		beforeSend : function(xhr){},
		success : function(response,status){	
			$('#right_support_online').html(response);
		},
		error:function (xhr, ajaxOptions, thrownError){
			right_support_online();
		}	
	});	  
}
function right_newsletter(){// Box thư mời
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=right_newsletter", 				
		beforeSend : function(xhr){},
		success : function(response,status){	
			$('#right_newsletter').html(response);
		},
		error:function (xhr, ajaxOptions, thrownError){
			right_newsletter();
		}	
	});	  
}
function right_cart(id, quantity){// Box giỏ hàng
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "right_cart",
			id : id,
			quantity : quantity
		}, 				
		beforeSend : function(xhr){
			//$('#right_cart').html('<img src="theme/images/loader.gif">');		
		},
		success : function(response,status){
			$('#right_cart').html(response);
		},
		error:function (xhr, ajaxOptions, thrownError){
			right_cart(0,0);
		}	
	});	  
}
function right_news(){ // Box tin tức mới
	$.ajax({
		type : "POST",
		url : "widget.php",	
		data : "xw=right_news",
		dataType: "html",
		beforeSend : function(xhr){
			$('#right_news').html('<img src="theme/images/loader.gif">');},
		timeout: 6000,
		error:function (request, error){
			right_news();
		},	
		success : function(response,status){	
			$('#right_news').html(response);
		}		
	});	  
}
function right_promotion_news(){ // Box tin tức khuyến mại
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=right_promotion_news", 				
		dataType: "html",
		beforeSend : function(xhr){
			$('#right_promotion_news').html('<img src="theme/images/loader.gif">');},
		timeout: 6000,
		error:function (request, error){
			right_promotion_news();
		},	
		success : function(response,status){	
			$('#right_promotion_news').html(response);
		}	
	});	  
}

function right_relax_news(){// Box tin tức thư giãn
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=right_relax_news", 				
		dataType: "html",
		beforeSend : function(xhr){
			$('#right_relax_news').html('<img src="theme/images/loader.gif">');},
		timeout: 6000,
		error:function (request, error){
			right_relax_news();
		},	
		success : function(response,status){	
			$('#right_relax_news').html(response);
		}	
	});	  
}
function right_website_link(){// Box liên kết website
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=right_website_link", 				
		dataType: "html",
		beforeSend : function(xhr){
			$('#right_website_link').html('<img src="theme/images/loader.gif">');},
		timeout: 6000,
		error:function (XMLHttpRequest, textStatus, errorThrown){
			right_website_link();
		},	
		success : function(response,status){	
			$('#right_website_link').html(response);
		}	
	});	  
}

///////////////////////////////////
// Trang chủ
///////////////////////////////////

function list_category(code){ // Danh mục sản phẩm
	$.ajax({
		type : "POST",
		url : "widget.php",			
		data : "xw=list_category&code=" + code, 
		dataType: "html",			
		beforeSend : function(xhr){
			//$('#' + code).html('<img src="theme/images/loader.gif">');				
		},
		timeout: 6000,
		error:function (request, error){
			list_category(code);
		},			
		success : function(response,status){	
			$('#' + code).html(response);			
		}	
	});	  
}
function list_category_special(code, limit){ // Danh mục sản phẩm đặc biệt
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=list_category_special&code=" + code + "&limit=" + limit, 	
		dataType: "html",			
		beforeSend : function(xhr){
			//$('#' + code).html('<img src="theme/images/loader.gif">');				
		},
		timeout: 6000,
		error:function (request, error){
			list_category_special('linh-kien-may-tinh', 0);
		},			
		success : function(response,status){	
			$('#' + code).html(response);			
		}	
	});	  
}
function promotion_news(limit){ // Chương trình khuyến mại
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=promotion_news&limit=" + limit, 				
		beforeSend : function(xhr){},
		success : function(response,status){			
			$('#promotion_news').html(response);
		}	
	});	  
}
function lastest_product(limit){// Sản phẩm mới nhất
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=lastest_product&limit=" + limit, 
		dataType: "html",		
		beforeSend : function(xhr){
			$('#lastest_product').html('<img src="theme/images/loader.gif">');				
		},
		timeout: 6000,
		error:function (request, error){
			lastest_product();
		},				
		success : function(response,status){			
			$('#lastest_product').html(response);
		}	
	});	  
}
function bestseller_product(limit){// Sản phẩm bán chạy
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=bestseller_product&limit=" + limit, 				
		dataType: "html",
		beforeSend : function(xhr){
			$('#bestseller_product').html('<img src="theme/images/loader.gif">');
		},
		timeout: 6000,
		error:function (request, error){
			bestseller_product();
		},		
		success : function(response,status){			
			$('#bestseller_product').html(response);
		}	
	});	  
}

///////////////////////////////////
// Trang sản phẩm
///////////////////////////////////

function list_main_product_category(){ // Danh mục chính của sản phẩm
	$.ajax({
		type : "POST",
		url : "widget.php",		
		data : "xw=list_main_product_category", 				
		beforeSend : function(xhr){
			$('#list_main_product_category').html('<img src="theme/images/loader.gif">');				
		},
		success : function(response,status){	
			$('#list_main_product_category').html(response);
		}	
	});	  
}
function list_sub_product_category(code){// Danh mục con của sản phẩm
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=list_sub_product_category&code=" + code, 				
		beforeSend : function(xhr){
			$('#list_sub_product_category').html('<img src="theme/images/loader.gif">');				
		},
		success : function(response,status){	
			$('#list_sub_product_category').html(response);
		}	
	});	  
}
function list_manufacturer(code){// Danh sách nhà sản xuất
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=list_manufacturer&code=" +code, 				
		beforeSend : function(xhr){
			$('#list_manufacturer').html('<img src="theme/images/loader.gif">');			
		},
		success : function(response,status){	
			$('#list_manufacturer').html(response);
		}	
	});	  
}
function product_news(){// Tin mới nhất
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=product_news", 				
		beforeSend : function(xhr){
			//$('#product_news').html('<img src="theme/images/loader.gif">');			
		},
		success : function(response,status){			
			$('#product_news').html(response);
		}	
	});	  
}
function list_product_by_category(page, param){// Danh sách sản phẩm
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : "xw=list_product_by_category&page=" +page+"&param="+param,//"&code=" + code + "&mf=" + manufacturer, 				
		beforeSend : function(xhr){
			$('#list_product_by_category').html('<img src="theme/images/loader.gif">');				
		},
		success : function(response,status){			
			$('#list_product_by_category').html(response);
		}	
	});	  
}

///////////////////////////////////
// Trang chi tiết sản phẩm
///////////////////////////////////

function view_product_detail(id){// Xem thông tin chi tiết sản phẩm
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=view_product_detail&id=" + id, 				
		beforeSend : function(xhr){
			$('#view_product_detail').html('<img src="theme/images/loader.gif">');			
		},
		success : function(response,status){	
			$('#view_product_detail').html(response);
		}	
	});	  
}
function top_bestseller(code){// Danh sản phẩm bán chạy nhất
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=top_bestseller&code=" + code, 				
		beforeSend : function(xhr){
			$('#top_bestseller').html('<img src="theme/images/loader.gif">');			
		},
		success : function(response,status){	
			$('#top_bestseller').html(response);
		}	
	});	  
}

///////////////////////////////////
// Trang xây dựng cấu hình máy tính
///////////////////////////////////

function list_component(){// Danh sách các thành phần để cấu hình máy tính
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "list_component"					
		}, 				
		beforeSend : function(xhr){
			$('#list_component').html('<img src="theme/images/loader.gif">');							
		},
		success : function(response,status){	
			$('#list_component').html(response);												
		}	
	});	  
}

function show_config_computer(){// Danh mục chính của sản phẩm		
	var a = [];
	for (var i = 0; i < $("select").size(); i++) {
		if ($("select")[i].value>0) a.push($("select")[i].value);
	}		
	$.ajax({
		type : "GET",
		url : "widget.php",			
		data : {
			xw : "show_config_computer",
			product_id: a.join(',') 
		}, 				
		beforeSend : function(xhr){
			//$('#show_config_computer').html('<img src="theme/images/loader.gif">');				
		},
		success : function(response,status){	
			$('#show_config_computer').html(response);
		}	
	});	  
}

function download_config_computer(){// Danh mục chính của sản phẩm		
	var a = [];
	for (var i = 0; i < $("select").size(); i++) {
		if ($("select")[i].value>0) a.push($("select")[i].value);
	}
	window.location.href='export_config.php?product_id='+ a.join(',');  
}	

///////////////////////////////////
// Trang chủ tin tức
///////////////////////////////////

function list_news_category(code){// Danh mục tin tức
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=list_news_category&code=" + code, 				
		beforeSend : function(xhr){
			$('#' + code).html('<img src="theme/images/loader.gif">');
		},
		success : function(response,status){	
			$('#' + code).html(response);			
		}	
	});	  
}
function lastest_news(limit, block){// Tin mới nhất
	$.ajax({
		type : "POST",
		url : "widget.php",
		data : "xw=lastest_news&limit=" + limit + "&block=" + block, 				
		beforeSend : function(xhr){
			$('#'+block).html('<img src="theme/images/loader.gif">');				
		},
		success : function(response,status){			
			$('#'+block).html(response);
		}	
	});	  
}

function list_viewest_news(){// Danh sách tin đọc nhiều nhất trong tuần
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "list_viewest_news"
		}, 				
		beforeSend : function(xhr){
			$('#list_viewest_news').html('<img src="theme/images/loader.gif">');				
		},
		success : function(response,status){	
			$('#list_viewest_news').html(response);
		}	
	});	  
}

function bottom_adv(){// Box quảng cáo phía dưới
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "bottom_adv"
		},
		dataType: "html",
		timeout: 6000,
		beforeSend : function(xhr){
			$('#bottom_adv').html('<img src="theme/images/loader.gif">');				
		},		
		error: function(XMLHttpRequest, textStatus, errorThrown){
			bottom_adv();
		},
		success : function(response,status){	
			$('#bottom_adv').html(response);
		}	
	});	  
}

function middle_adv(){// Box quảng cáo ở giữa
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "middle_adv"
		}, 				
		beforeSend : function(xhr){
			$('#middle_adv').html('<img src="theme/images/loader.gif">');				
		},
		success : function(response,status){	
			$('#middle_adv').html(response);
		}	
	});	  
}

///////////////////////////////////
// Trang đăng ký tài khoản
///////////////////////////////////

function register(){
	if ($('#email').val()=='') {
		alert('Bạn chưa nhập địa chỉ email!');
		$('#email').focus();
		return false;
	}
	if ($('#password').val()=='') {
		alert('Bạn chưa nhập mật khẩu!');
		$('#password').focus();
		return false;
	}
	if ($('#confirm_password').val()=='') {
		alert('Bạn chưa xác nhận mật khẩu!');
		$('#confirm_password').focus();
		return false;
	}
	if ($('#confirm_password').val()!=$('#password').val()) {
		alert('Mật khẩu xác nhận chưa đúng!');
		$('#confirm_password').focus();
		return false;
	}									
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "register",
			email : $('#email').val(),
			password : $('#password').val(),
			confirm_password : $('#confirm_password').val(),
			firstname : $('#firstname').val(),
			lastname : $('#lastname').val(),					
			birthday : $('#birthday').val(),
			telephone : $('#telephone').val(),
			fax : $('#fax').val(),																									
			handphone : $('#handphone').val(),
			company : $('#company').val(),																									
			address_1 : $('#address_1').val(),
			address_2 : $('#address_2').val(),																									
			city : $('#city').val()
		}, 				
		beforeSend : function(xhr){},
		success : function(response,status){	
			if(response==1) {
				alert('Tài khoản đã được đăng ký thành công!');
				window.location.href='?';
			}else if(response==0){
				alert('Đăng ký tài khoản bị lỗi! Bãn hãy thử lại');
				window.location.href='?';						 
			}else{
				alert(response);
			}
		}	
	});	  
}

///////////////////////////////////
// Trang đăng nhập hệ thống
///////////////////////////////////

function login(){
	if($('#email').val()==''){
		alert('Bạn chưa nhập tài khoản!');
		$('#email').focus();
		return false;
	}
	if($('#pass').val()==''){
		alert('Bạn chưa nhập mật khẩu!');
		$('#pass').focus();
		return false;
	}		
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "login",
			email:$('#email').val(),
			pass:$('#pass').val()				
		}, 				
		beforeSend : function(xhr){},
		success : function(response,status){	
			if (response==1) window.location.href='?xm=profile';
			else if (response==2) window.location.href='?xm=cart';
			else alert(response);
		}	
	});	  
}

///////////////////////////////////
// Trang lấy mật khẩu
///////////////////////////////////

function lose_password(){
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "lose_password",
			email : $('#email').val()
		}, 				
		beforeSend : function(xhr){},
		success : function(response,status){	
			if(response=='SUCCESS') {
				alert('Mật khẩu mới đã được gửi tới hòm mail của bạn!');
				window.location.href='?xm=login';
			}else if(response=='ERROR'){
				alert('Lấy thông tin mật khẩu bị lỗi!');
				window.location.href='?xm=lose_password';						 
			}else{
				alert(response);
			}
		}	
	});	  
}

/////////////////////////////////////
// Trang sửa thông tin cá nhân
/////////////////////////////////////

function edit_profile(){
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "edit_profile",
			firstname : $('#firstname').val(),
			lastname : $('#lastname').val(),					
			birthday : $('#birthday').val(),
			telephone : $('#telephone').val(),
			fax : $('#fax').val(),																									
			handphone : $('#handphone').val()
		}, 				
		beforeSend : function(xhr){},
		success : function(response,status){	
			if(response==1) {
				alert('Thông tin cá nhân đã được thay đổi!');
				//window.location.href='?xm=profile';
			}else if(response==0){
				alert('Sửa thông tin cá nhân bị lỗi!');
				//window.location.href='?xm=edit_profile';						 
			}else{
				alert(response);
			}
		}	
	});	  
}

function edit_profile_form(){
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "edit_profile_form"
		}, 				
		beforeSend : function(xhr){},
		success : function(response,status){	
			$('#profile').html(response);
		}	
	});	  
}

/////////////////////////////////////
// Box thay đổi mật khẩu
/////////////////////////////////////

function change_password(){
	if ($('#old_password').val()=='') {
		alert('Bạn chưa nhập mật khẩu cũ!');
		$('#old_password').focus();
		return false;
	}
	if ($('#new_password').val()=='') {
		alert('Bạn chưa nhập mật khẩu mới!');
		$('#new_password').focus();
		return false;
	}			
	if ($('#confirm_password').val()=='') {
		alert('Bạn chưa xác nhận mật khẩu!');
		$('#confirm_password').focus();
		return false;
	}
	if ($('#confirm_password').val()!=$('#new_password').val()) {
		alert('Mật khẩu xác nhận chưa đúng!');
		$('#confirm_password').focus();
		return false;
	}									
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "change_password",
			old_password : $('#old_password').val(),
			new_password : $('#new_password').val(),					
			confirm_password : $('#confirm_password').val()
		}, 				
		beforeSend : function(xhr){},
		success : function(response,status){	
			if(response==1) {
				alert('Mật khẩu đã được thay đổi. Bạn cần đăng nhập lại!');
				window.location.href='?xm=login';
			}else if(response==0){
				alert('Đổi mật khẩu bị lỗi! Bãn hãy thử lại');
				window.location.href='?xm=change_password';						 
			}else{
				alert(response);
			}
		}	
	});	  
}

function change_password_form(){
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "change_password_form"
		}, 				
		beforeSend : function(xhr){},
		success : function(response,status){	
			$('#profile').html(response);
		}	
	});	  
}

/////////////////////////////////////
// Box sửa thông tin địa chỉ
/////////////////////////////////////

function edit_address(){
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "edit_address",
			company : $('#company').val(),																									
			address_1 : $('#address_1').val(),
			address_2 : $('#address_2').val(),																									
			city : $('#city').val()
		}, 				
		beforeSend : function(xhr){},
		success : function(response,status){	
			if(response==1) {
				alert('Thông tin địa chỉ đã được thay đổi!');
				//window.location.href='?xm=profile';
			}else if(response==0){
				alert('Thay đổi thông tin địa chỉ bị lỗi. Bãn hãy thử lại!');
				//window.location.href='?xm=edit_address';						 
			}else{
				alert(response);
			}
		}	
	});	  
}

function edit_address_form(){
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "edit_address_form"
		}, 				
		beforeSend : function(xhr){
			$('#profile').html('<img src="theme/images/loader.gif">');				
		},
		success : function(response,status){	
			$('#profile').html(response);
		}	
	});	  
}

/////////////////////////////////////
// Box xem lịch sử giao dịch
/////////////////////////////////////

function list_order_form(page){
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "list_order_form",
			page : page			
		}, 				
		beforeSend : function(xhr){
			$('#profile').html('<img src="theme/images/loader.gif">');				
		},
		success : function(response,status){	
			$('#profile').html(response);
		}	
	});	  
}

function view_order_detail(id, page){
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "view_order_detail",
			id : id,
			page : page
		}, 				
		beforeSend : function(xhr){
			$('#profile').html('<img src="theme/images/loader.gif">');				
		},
		success : function(response,status){	
			$('#profile').html(response);
		}	
	});	  
}
/////////////////////////////////////
// Box cấu hình nhận thư mời
/////////////////////////////////////

function config_newsletter_form(){
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "config_newsletter_form"
		}, 				
		beforeSend : function(xhr){
			$('#profile').html('<img src="theme/images/loader.gif">');				
		},
		success : function(response,status){	
			$('#profile').html(response);
		}	
	});	  
}

function logout(){
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "logout"
		}, 				
		beforeSend : function(xhr){},
		success : function(response,status){	
			window.location.href='?';
		}	
	});	  
}

function _search(){
	window.location.href='?xm=search&keyword='+$('#search').val(); 
}


function register_newsletter(){
	var email_newsletter = $('#email_newsletter').val();
	if (email_newsletter==''){
		alert("Bạn chưa nhập địa chỉ email nhận thư mời!");
		$('#email_newsletter').focus();
		return false;				
	}else if(!checkEmail(email_newsletter)){
		alert("Địa chỉ email bạn nhập không hợp lệ!");
		$('#email_newsletter').focus();
		return false;		
	}else{
		$.ajax({
			type : "GET",
			url : "widget.php",
			data : {
				xw : "register_newsletter",
				email_newsletter: email_newsletter
			}, 				
			beforeSend : function(xhr){
				$('#newsletter').html('<img src="theme/images/loader.gif">');				
			},
			success : function(response,status){	
				$('#newsletter').html(response);
			}	
		});					
	}
}
<!--Tìm kiếm sản phẩm-->
function search_product(page){
	$.ajax({
		type : "GET",
		url : "widget.php",
		data : {
			xw : "search_product",
			keyword : $('#keyword').val(),
			category_id : $('#category_id').val(),
			manufacturer_id : $('#manufacturer_id').val(),
			price_from : $('#price_from').val(),
			price_to : $('#price_to').val(),
			page : page
		}, 				
		beforeSend : function(xhr){},
		success : function(response,status){	
			$('#search_product').html(response);
		}	
	});	  
} 

