
function addtocart(id,act){
	 total= 0;
	 if(act!='del'){
	    total = $('#txtSoLuong').val();
		_curl = '../../';
	 }else
	 	_curl = '';
	 url = $('#txtIndexUrl').val();	
	 arr_url = explode("/",url);
	 if(IsNumeric(total) || act=='del'){		 
		 $.ajax({
			type:"POST", url: "./"+_curl+"includes/addtocart.php",
			data:{'id':id,'total':total,'act':act},
			success:function(data){				
				
				if(act=='del')
					window.location = url;
				else
					window.location = url + "gio-hang/";
			},
			error:function(data){
				alert("Chua cap nhat duoc san pham vao gio hang.");
			}
		 });
	 }else{
		 alert("Ban hay nhap vao kieu so");
	 }
}

function addtocart_grid(id,act,view){		
	 total = $('#txtnum_'+id).val();
	 url = $('#txtIndexUrl').val();	
	 arr_url = explode("/",url);
	 if(view==1)
	 	_curl = '../';
	else	
		_curl = '';
	  if(IsNumeric(total)){
		 $.ajax({
			type:"POST", url: "../"+_curl+"../includes/addtocart.php",
			data:{'id':id,'total':total,'act':act},
			success:function(data){				
				if(act=='del')
					window.location = url;
				else
					window.location = url + "gio-hang/";
			},
			error:function(data){
				alert("Chua cap nhat duoc san pham vao gio hang.");
			}
		 });
	  }else{
	  	 alert("Ban hay nhap vao kieu so");
		 $('#txtnum_'+id).focus();
	  }
}

function view_products(msg){
	 url = $('#txtIndexUrl').val();	
	 arr_url = explode("/",url);
	 $.ajax({
		type:"POST", url: "../../includes/setviewpro.php",
		data:{'msg':msg},
		success:function(data){							
			window.location.reload()				
		},
		error:function(data){
			
		}
	 });
}

function UpdateBasket(){
	
}

function ViewOrder(){
	if(ValiCheckOutInfo()){
		hoten = $('#txtHoVaTen').val();
		sdthoai = $('#txtSoDienThoai').val();
		diachi = $('#txtDiaChi').val();
		 $.ajax({
			type:"POST", url: "../../includes/setnameorder.php",
			data:{'hoten':hoten,'sdthoai':sdthoai,'diachi':diachi},
			success:function(data){										
				//window.location = "./../../vieworder.php";
				window.open("../../vieworder.php",'blank');
			},
			error:function(data){
				
			}
		 }); 
	}
}

function SendOrder(){	
	if(ValiCheckOutInfo()){
		Hoten = $('#txtHoVaTen').val();
		Sodienthoai = $('#txtSoDienThoai').val();
		Email = $('#txtEmail').val();
		Diachi = $('#txtDiaChi').val();
		Yeucaukhac = $('#txtYeuCauKhac').val();
		//thank = $('#txtThankOrder').val();
		$.ajax({
			type:"POST", url: "../../includes/checkout.php",
			data:{'hoten':Hoten,'sodienthoai':Sodienthoai,'email':Email,'diachi':Diachi,'yeucaukhac':Yeucaukhac},
			success:function(data){
				//$('#tdOrderThank').html(thank);				
				$('#tdOrderThank').html(data);				
				$('#tdbasket').html("");
			},
			error:function(){
				alert("error");							
			}
		 });
	}
}


function BackToHome(){
	 url = $('#txtIndexUrl').val();	
	 window.location = url;
}

function BasketDelItems(){
	c_value = "";
	for (var i=0; i < document.basket_items.chkpro.length; i++)
	   {
	   if (document.basket_items.chkpro[i].checked)
		  {
		  c_value = c_value + document.basket_items.chkpro[i].value + ",";
		  }
	   }
	url = $('#txtIndexUrl').val();
	$.ajax({
			type:"POST", url: "./../../includes/deleteitems.php",
			data:{'idpro':c_value},
			success:function(data){				
				window.location = url+"gio-hang/";
			},
			error:function(){
				alert("Co loi xay ra.");
			}
	});
}

function ValiCheckOutInfo(){
		s = $.trim($('#txtHoVaTen').val()); 
		if(s=="" || s.length>50)
		{
			alert("Vui long nhap ho ten hoac ho ten phai nho hon 50 ky tu!");
			$('#txtHoVaTen').focus();
			return false;
		}
		s = $.trim($('#txtEmail').val());
		if(s!="" && !is_email(s)){
			alert("Ban chua nhap dia chi email\nHoac dia chi email khong lop le!");
			$('#txtEmail').focus();
			return false;
		}
		s = $.trim($('#txtSoDienThoai').val()); 
		if(s=="" || s.length>15)
		{
			alert("Vui long nhap vao so dien thoai!");
			$('#txtSoDienThoai').focus();
			return false;
		}
		s = $.trim($('#txtDiaChi').val()); 
		if(s=="" || s.length>100)
		{
			alert("Vui long nhap vao dia chi!");
			$('#txtDiaChi').focus();
			return false;
		}
		return true;
}

function IsNumeric(input){ 
    var RE = /^-{0,1}\d*\.{0,1}\d+$/; 
    return (RE.test(input)); 
}

function is_email(email)
{	
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email))	
		return false;
	else
		return true;	
}


function explode (delimiter, string, limit) {
    // Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.  
    // 
    // version: 1003.2411
    // discuss at: http://phpjs.org/functions/explode    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
     var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||        typeof arguments[1] == 'undefined' ) {
        return null;
    }
 
    if ( delimiter === '' ||        delimiter === false ||
        delimiter === null ) {
        return false;
    }
     if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' ) {
        return emptyArray;    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;    }
}

