jQuery(document).ready(function() {
    
    //var TicketPrice = parseFloat(jQuery('#amount-for-ticket').val().replace(',','.'));
    //var TotalPrice;    
    jQuery('[id^="tickets_count-"]').keyup(function() {
        jQuery(this).next('span').html('');
        this.value = this.value.replace(/[^0-9\.]/g,'1');
        var Tickets = jQuery(this).val();
        var regex = /^[\-]{0,1}[0-9]{1,8}$/;
        if(Tickets!=undefined && Tickets!='' && regex.test(Tickets)==true && Tickets!=0 && Tickets!='0') {
            //pobieram id eventu
            var IdEvent = jQuery(this).attr('id').split('-');
            RecountTickets(IdEvent[1]);
        }
        else {
            jQuery(this).val(1);
            //jQuery(this).next('span').html('Wpisz poprawną ilość biletów.');
        }
        
    });
    
    jQuery('[id^="ticketOptions-"] input[type=radio]').change(function() {
        var IdEvent = jQuery(this).attr('id').split('-');
        RecountTickets(IdEvent[1]);
    });
	
	function DotPaySubmit(IdEvent) {
		jQuery('.validtc', this).html('');
        RecountTickets(IdEvent); 
        var Regulation = jQuery('#regulations-'+IdEvent).is(':checked');
		alert(Regulation);
        if(Regulation!=true) {		
			jQuery('#validtc-'+IdEvent).html('Musisz zgodzić się z regulaminem.');
            return false;
        }
		//jQuery(this).submit();	
		return true;
	}
	jQuery('[id^="dotpay-"]').click(function() {	      
		jQuery('.validtc', this).html('');
        var IdEvent = jQuery(this).attr('id').split('-'); 
        RecountTickets(IdEvent[1]); 
        var Regulation = jQuery('#regulations-'+IdEvent[1]).is(':checked');
        if(Regulation!=true) {		
			jQuery('#validtc-'+IdEvent[1]).html('Musisz zgodzić się z regulaminem.');
            return false;
        }
		jQuery(this).parent('form').submit();
        //return true
             
    });
	
	/* Dla eventu */
	
	
    jQuery('#tickets_count').keyup(function() {
        jQuery(this).next('span').html('');
        this.value = this.value.replace(/[^0-9\.]/g,'1');
        var Tickets = jQuery(this).val();
        var regex = /^[\-]{0,1}[0-9]{1,8}$/;
        if(Tickets!=undefined && Tickets!='' && regex.test(Tickets)==true && Tickets!=0 && Tickets!='0') {
            //pobieram id eventu
            //var IdEvent = jQuery(this).attr('id').split('-');
            RecountTicketsForEvent();
        }
        else {
            jQuery(this).val(1);
            //jQuery(this).next('span').html('Wpisz poprawną ilość biletów.');
        }
        
    });

	jQuery('#ticketOptions input[type=radio]').change(function() {
        RecountTicketsForEvent();
    });
	
	jQuery('#dotpay').click(function() {	      
		jQuery('.validtc', this).html('');
        RecountTicketsForEvent();
        var Regulation = jQuery('#regulations').is(':checked');
        if(Regulation!=true) {		
			jQuery('#validtc').html('Musisz zgodzić się z regulaminem.');
            return false;
        }
		jQuery(this).parent('form').submit();
        //return true
             
    });

    /**
 * RecountTickets
 */
    function RecountTickets(IdEvent) {
        var TicketPrice = parseInt(parseFloat(jQuery('#amount-for-ticket-'+IdEvent).val().replace(',','.')) * 100);
        
        var TicketCount = parseInt(jQuery('#tickets_count-'+IdEvent).val());
        TotalPrice = TicketPrice * TicketCount;
        
        var Shipping = parseInt(jQuery('input[name=shipping-'+IdEvent+']:checked', '#ticketOptions-'+IdEvent).val());
        var TP = parseFloat((TotalPrice+Shipping)/100);
        jQuery('#price-test-'+IdEvent).html(TP.toFixed(2));
        jQuery('#formAmount-'+IdEvent).val(TP.toFixed(2));
		return true;
    }
	
	function RecountTicketsForEvent() {
        var TicketPrice = parseInt(parseFloat(jQuery('#amount-for-ticket').val().replace(',','.')) * 100);
        
        var TicketCount = parseInt(jQuery('#tickets_count').val());
        TotalPrice = TicketPrice * TicketCount;
        
        var Shipping = parseInt(jQuery('input[name=shipping]:checked', '#ticketOptions').val());
        var TP = parseFloat((TotalPrice+Shipping)/100);
        jQuery('#price-test').html(TP.toFixed(2));
        jQuery('#formAmount').val(TP.toFixed(2));
		return true;
    }
});
