var panel_otherinformation =
{
	initialize: function()
	{
		$('.selectbox').selectbox();
		$('.selectbox').change(panel_otherinformation.update);
		$('#coinsInfo').click(panel_otherinformation.coinsInfo);
        $('#passe_partoutsInfo').click(panel_otherinformation.passe_partoutsInfo);
		$('#reservation_ticketsInfo').click(panel_otherinformation.reservation_ticketsInfo);
		$('#registrationByInfo').click(panel_otherinformation.registrationByInfo);
		$('#teamTraining').change(panel_otherinformation.teamTrainingChange);
		$('img.btn_next').click(panel_otherinformation.nextStepClicked);
		$('img.btn_previous').click(panel_otherinformation.previousStepClicked);		
		
		
		$('#otherInfoForm input').keyup(panel_otherinformation.inputDataChanged);



		// run the update function 
		panel_otherinformation.update();
		
		return true;
	},
	
	inputDataChanged:function(event)
	{
		clearInterval(panel_otherinformation.waitInterval);
		panel_otherinformation.waitInterval = setInterval(panel_otherinformation.update, 400);
	},

	update: function()
	{
        clearInterval(panel_otherinformation.waitInterval);

        // SJONGE JONGE.. hier een mooie hack voor IE mensen, serialize werkt niet goed als er alleen een
        // selectbox in de lijst staat ofzo.... man man ma nman....
        // Bij deze dus een hack to make live less miserable
        if($('#otherInfoForm').serialize() == '' && $('#otherReservationTickets').length == 1)
        {
            data = {otherReservationTickets : $('#otherReservationTickets').val()};
        }else{
            data = $('#otherInfoForm').serialize();
        }

		// save the form to the database
		$.getJSON(	page.SERVERPATH+'/ajax/otherinformation/eventID/'+page.eventID+'/registrationkey/'+page.registrationKey, 
					data
				 );	
		clearInterval(panel_otherinformation.waitInterval);
	},
	
	coinsInfo: function()
	{
        page.activePopup = 'step2InformationPopup';
		page.loadPopup(page.SERVERPATH+'/popup/coins');
	
	},
	
	passe_partoutsInfo: function()
	{
        page.activePopup = 'step2InformationPopup';
		page.loadPopup(page.SERVERPATH+'/popup/passepartouts');
	
	},
	
	reservation_ticketsInfo: function()
	{
        page.activePopup = 'step2InformationPopup';
		page.loadPopup(page.SERVERPATH+'/popup/reservationtickets');	
	},

    registrationByInfo:function()
    {
      page.activePopup = 'step2InformationPopup';
	  page.loadPopup(page.SERVERPATH+'/popup/registrationby');
    },
	
	
	otherInfoErrorsRecieved:function(data)
	{
		$('#otherInfoForm input').removeClass('error');
		$('.selectboxError').removeClass('selectboxError');		

		if(data.message == 'error')
		{
			$.each(data.errors, function(i,item)
								{	
									element = $('[name="'+i+'"]');
									if(element.length > 0)
									{
										switch(element.attr('tagName').toLowerCase())
										{
											case 'select':
												$('#'+i+'_input').addClass('selectboxError');
												break;
											case 'input':
                                                
												switch(element.attr('type').toLowerCase())
												{
													case 'text':
													case 'password':
														$('input[name="'+i+'"]').addClass('error');	
													break;
													default:
														alert('nog niet');
													break;
												}
											break;
										}
									}
								}
				);
            if(data.errors.teamName_used == true)
            {
                $('input[name="teamName"]').addClass('error');
                $('#teamName_error').html('De opgegeven teamnaam is helaas al ingebruik.');
            }else{
                $('#teamName_error').html('');
            }
			page.menuItemEnable('btn_check',false);			
			page.menuItemEnable('btn_payment',false);			
		}else{
			page.menuItemEnable('btn_check',true);							
		}
	},	
	
	nextStepClicked: function(event)
	{

        if($('#teamTraining').val() == 'Ja' && $('[id^="teamTrainingDay"]:checked').length == 0)
        {
            alert('Je dient minstens 1 van de '+$('[id^="teamTrainingDay"]').length+' trainingsdagen aan te geven');
        }else{
            page.validating(true);
            // SJONGE JONGE.. hier een mooie hack voor IE mensen, serialize werkt niet goed als er alleen een
            // selectbox in de lijst staat ofzo.... man man ma nman....
            // Bij deze dus een hack to make live less miserable
            if($('#otherInfoForm').serialize() == '' && $('#otherReservationTickets').length == 1)
            {
                data = {otherReservationTickets : $('#otherReservationTickets').val()};
            }else{
                data = $('#otherInfoForm').serialize();
            }

            // save the form to the database
            $.getJSON(	page.SERVERPATH+'/ajax/otherinformation/eventID/'+page.eventID+'/registrationkey/'+page.registrationKey,
                        data,
                        panel_otherinformation.nextStepValidated
                     );
        }



	},

    nextStepValidated:function(data)
    {
       page.validating(false);
       panel_otherinformation.otherInfoErrorsRecieved(data);
       if($('#otherInfoForm .error').length > 0 || $('#otherInfoForm .selectboxError').length > 0)
       {
			alert('Je hebt nog niet alle gegevens correct ingevult, de punten in het rood dien je nog correct in te vullen.');
	   }else{
			$('#btn_check').trigger('click');
        }
    },


    teamTrainingChange:function(event)
    {
        if(this.value.toLowerCase() == 'ja')
        {
            $('#teamTrainigDays').show();
        }else{
            $('#teamTrainigDays').hide();
            $('[id^="teamTrainingDay"]').removeAttr('checked');
        }
    },
	
	previousStepClicked:function(event)
	{
		$('#btn_contestant').trigger('click');
	}
}