/*---------------------------+
| phan add cart 			 |
------------------------------*/
function addCart(id){
    var productcart = $("#product"+id).val();
    if(productcart > 99){
        jAlert('Số lượng mua nhập vào phải nhỏ hơn 99','Thông báo');
    }else if(productcart == '-SL-'){
        jAlert('Xin vui lòng nhập số lượng cần mua.Chỉ chấp nhận số','Thông báo');
    }else if(isNumeric(productcart) == false){
        jAlert('Số lượng nhập vào phải là số. Ví dụ: 1,2,3,4,5...','Thông báo');
    }else{
        $.post(base_url+"checkout/addCart",{'productcart':productcart,'id':id},function(data)
        { 
        	$("#cart").html(data.cart);
           // $.modal($('#box').html(data.box),{minHeight:180,minWidth: 400})                                                 
        });    
    }
}

/*******************
* Check isNumber
*/
function isNumeric(string){
    var numericExpression = /^[0-9]+$/;
    if(string.match(numericExpression)) {
        return true;

    } else {
        return false;
    }
}
/*-------------------------------------------+
 * radio * 
 --------------------------------------------*/
$(document).ready(function(){         
	change_payment();
	 $("#div_freeship").show("fast");
});
function change_payment(){
	
    checked = $('input[name=choice_payment]:checked').val();
   
    if(checked==1){
        $(".add_elm").hide();  
        $("#div_visa").show("fast");
    }else if(checked==2){
        $(".add_elm").hide();
        $("#div_gold").show("fast");
    }else if(checked==3){
        $(".add_elm").hide(); 
        $("#div_freeship").show("fast");
    }else if(checked==4){
        $(".add_elm").hide(); 
        $("#div_bank").show("fast");
    }
}

/*---------------------------------------------+
 * 											  *
 ----------------------------------------------*/
function pay_step_one(){
    var checked = $('input[name=choice_payment]:checked').val();
    var checkout_type = $('input[name=checkout_type]:checked').val();
    if(checked >=1 && checked <=4){
        //$("#ajax-load").css('display','block');
        var grb_choice_payment = $("#choice_payment").val();
        var grb_choice_bank = $("#choice_bank").val();
        if(checked == 4){
            if(grb_choice_bank ==''){
                jAlert('Vui lòng chọn ngân hàng.','Thông báo');
                return false;
            }
        }
         dataString = 'grb_choice_payment='+checked+'&grb_choice_bank='+grb_choice_bank+'&grb_next_step=2&checkout_type='+checkout_type;
         $.ajax({          
             type: "POST",
             url: base_url+"checkout/pay/"+$("#grb_id").val(),
             data: dataString,
             async:true,
             //dataType: "json",
             success: function(data) {
                $("#left").html(data);
                //$("#ajax-load").css('display','none');   
             }
         });
                
    }else{
        jAlert('Vui lòng chọn phương thức thanh toán','Thông báo');
    }
}

/*----------------------------------------------------
 * 
 * 
 ----------------------------------------------------*/

function fn_submit_step_two(){
	$("#form_step_two").validate({
            rules: {
        	 	fullName:{
                    required: true
                },
                phone:{
                    required: true
                },            
                address:{
                    required: true
                }
                                                                  
            },
            messages: {
            	fullName: {
                    required: "Vui lòng điền Họ và tên"
                },
                phone: {
                    required: "Vui lòng điền số điện thoại"
                },            
                address: {
                    required: "Vui lòng điền Số nhà, đường"
                }        
                              
            },
            submitHandler: function(form) {
                    dataString = $("#form_step_two").serialize();
                    $.ajax({
                    type: "POST",
                    url: base_url+"checkout/checkoutsteps",
                    data: dataString,
                    //dataType: "json",
                    success: function(data) {
                        $("#checkout_steps").html(data);
                        reloadpage(); 
                    }
                    });

                    return false;            

            }                
         });
             
 
}
/*---------------------------------------------------------+
 * 
 ---------------------------------------------------------*/

function choice_bank(bank){
    var checked = $('input[name=choice_payment]:checked').val();
    if(checked == 4){
        
         dataString = 'bank='+bank;
         $.ajax({
             type: "POST",
             url: url+"payment/choicebank",
             data: dataString,
             async:true,
             dataType: "json",
             success: function(data) {
                     $("#bank_info").html(data.info).show('fast');
                     $(".list-bank").hide('fast');
                     $("#choice_bank").val(data.bankid);
             }
         });            
       
        return false;
    }else{
        jAlert('Bạn phải chọn phương thức Chuyển khoản mới chọn được ngân hàng '+bank,'Thông báo');
        return false;
    }
}

// check login
function login_payment(){
    $("#login_payment").validate({
       rules: {
           emailLogin:{
    			required: true,
    			email:true
    	   },
           passLogin: "required"
       },
       messages: {
           emailLogin:{
    	   		required: "Vui lòng nhập địa chỉ Email",
    	   		email:	"Email không đúng định dạng"
       		},
           passLogin: "Vui lòng nhập mật khẩu"
       }
       ,submitHandler: function(form) {
           dataString = $("#login_payment").serialize();
           //$("#ajax-load").css('display','block');
           $.ajax({
               type: "POST",
               url: base_url+"account/ajaxLogin",
               data: dataString,
               dataType: "json",
               success: function(data) {
        	   		//alert(data)
                   if(data.message !=''){
                       jAlert(data.message,'Thông báo');
                   }else{
                       location.reload(); 
                   }    
                   //$("#ajax-load").css('display','none'); 
               }
           });             
           }
   });
}

/*----------------------------------------|
 * function fn_submit_step_three
 -----------------------------------------*/

function fn_submit_step_three(){
    $("#form_step_three").validate({
        rules:{
    		choice_payment:"required"
        },
        messages:{
        	choice_payment :"Vui lòng chọn phương thức vận chuyển"
        },
        submitHandler: function(form) {
            dataString = $("#form_step_three").serialize();
            $.ajax({
                type: "POST",
                url: base_url+"checkout/checkoutsteps",
                data: dataString,
                success: function(data){
                    $("#checkout_steps").html(data);
                    reloadpage();
                    
                }
            });

            return false; 
        }
    });
}


/*--------------------------------------------+

+---------------------------------------------*/
function step_con(id){
    $("#"+id).validate({
        rules: {
            email_customer:{
                required: true,
                email: true
            }    
        },
        messages: {
            email_customer:{
                required: "Vui lòng điền địa chỉ email",
                email: "Vui lòng điền đúng định dạng email"
            }
        },
        submitHandler: function(form) {
                dataString = $("#"+id).serialize();
                $.ajax({
                type: "POST",
                url: base_url+"checkout/checkoutsteps",
                data: dataString,
                //dataType: "json",
                success: function(data) {
                    $("#checkout_steps").html(data);
                    reloadpage();  
                }
                });

                //return false;            

          
        }
    });
}


/*------------------------------------+
 * 
 -------------------------------------*/
function reloadpage(){
    var browser;
    if(jQuery.browser.msie)
      browser = "Internet Explorer";
    if(browser =="Internet Explorer"){
         location.reload();
    }
}
