$().ready(function() {

   
    $('#coupon_code').click(function(){
        $('#coupon_error').hide()
    });
              
   
    $('#applycoupon').click(function()
    {////
       
        if(   $('#coupon_code').val() !=""){
            $.ajax({
                            
                type: 'POST',
                url: 'rpc.php',
                data:'action=sign_up&mode=apply_coupon_code&coupon_code='+$.URLEncode($('#coupon_code').val()),
                success:function(data){
                              
                    if($.trim(data) != 'false')
                    {
                        $('#coupon_error').hide();
                        window.location.reload(true);

                        //$("table#tbl_invoice tr:last").before('<tr><td>Row 4</td></tr>');
                    }
                    else
                    {  
                        $('#coupon_error').show();
                        $('#coupon_error').html("Invalid coupon code");         
                    }
                }
            });
        }else{
            $('#coupon_error').show();
            $('#coupon_error').html("Enter coupon code");  
        }          
       
       
       
    });////     
    
   
    $('#checkoutSubmit').click(function(){
                               
         
         
        if(   $('#discodeapplied').val() !=""){
            $.ajax({
                            
                type: 'POST',
                url: 'rpc.php',
                data:'action=sign_up&mode=get_coupon_code_discount&coupon_code='+$.URLEncode($('#discodeapplied').val()),
                success:function(data){
                          
                    if($.trim(data) == 'true')
                    {
                        $('#coupon_error').hide();
                        document.location.href = 'pay-form.html';
                    }
                    else if($.trim(data) == '100') //100% discount will bypass payment page 
                    {
                        $('#coupon_error').hide();
                        document.location.href = 'thank-you.html';
                    }
                    else
                    {  
                              
                        $('#coupon_error').show();
                        $('#coupon_error').html("Invalid coupon code");         
                             
                    }


                }

            });
        }else{
              
            document.location.href = 'pay-form.html';   
              
        }      
          
    }) ; 
   
   
   

});
