var popup_company = {
	waitInterval:0,
	changed: false,
	errors: 0,

	initialize: function()
	{
		if($('#form_company').length > 0)
		{
			popup_company.changed = false;
            
            // We only validate if the company is not new!
            if($('#form_company input:text[value!=""][value!="geen"]').length > 0)
            {
                popup_company.companyValidate();
            }
            
            $('select').selectbox();
			
			$('#form_company').change(popup_company.companyDataChanged);
			$('#form_company input').keyup(popup_company.companyDataChanged);
			$('select').change(popup_company.companyDataChanged);
		
			
			$('input[title]').bt({
				  	trigger: ['focus', 'blur'],
				  	positions: ['right'],				  
				  	fill: 'rgba(255, 255, 255, .8)',
				  	cornerRadius: 10,
				   	strokeWidth: 2,
					strokeStyle: '#a0d0ff'
				});
		}
		
		$('#btn_popup_save').click(popup_company.popupSave);
		$('#btn_popup_cancel').click(popup_company.popupCancel);

		return true;
	},
    
	
	companyDataChanged:function(event)
	{
        // From now on we only validate when the user wishes to save the form
		// clearInterval(popup_company.waitInterval);
		// popup_company.waitInterval = setInterval(popup_company.companyValidate,400);
		popup_company.changed = true;
	},

    companyValidate:function()
	{
		clearInterval(popup_company.waitInterval);
		$.getJSON(	page.SERVERPATH+'/ajax/validatecompany/eventID/'+page.eventID+'/registrationkey/'+page.registrationKey,
					$('#form_company').serialize(),
					popup_company.contestantErrorsRecieved
				);

	},

	companySave:function()
	{
		clearInterval(popup_company.waitInterval);
		$.getJSON(	page.SERVERPATH+'/ajax/saveCompany/eventID/'+page.eventID+'/registrationkey/'+page.registrationKey, 
					$('#form_company').serialize(),
					popup_company.contestantErrorsRecieved
				);
			
	},
	
	contestantErrorsRecieved:function(data)
	{
		$('#form_company input').removeClass('error')
		$('.selectboxError').removeClass('selectboxError');
        popup_company.errors = 0;
		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;
												}
											break;
										}
                                        popup_company.errors++;
									}
								}
				);
		}
		
	},
	
	popupSave:function()
	{
        clearInterval(popup_company.waitInterval);
		$.getJSON(	page.SERVERPATH+'/ajax/saveCompany/eventID/'+page.eventID+'/registrationkey/'+page.registrationKey,
					$('#form_company').serialize(),
					popup_company.popupSaveValidated
				);
        page.validating(true);

	},
    

    popupSaveValidated:function(data)
    {
        popup_company.contestantErrorsRecieved(data);
        page.validating(false);
        if(popup_company.errors > 0)
        {
            response = confirm('Je hebt nog niet alle gegevens correct ingevult, controleer de in het rood aangegeven velden. Het venster kan gesloten worden maar je dient deze informatie wel volledig in te vullen voor je door kan gaan naar de volgende stap.\r\n\r\nWeet je zeker dat je het venster wilt sluiten?');

        }else{
            response = true;
        }

        if(response == true)
        {
            //   panel_contestant.quickadd($('#companyKey').val());
            popup_company.companySave();
            panel_contestant.contestantPanelRefresh();
            if($('#companyID').val() >= 100000 && page.activePopup == 'companyNew')
            {
               panel_contestant.quickadd($('#companyKey').val());
            }else{
                page.hidePopup();
            }
        }
    },
    

	loadContactPanel:function()
	{
		page.activePopup = 'contact';
		page.loadPopup(page.SERVERPATH+'/popup/contact/contactKey/'+panel_contestant.nextPopupContactKey);
		panel_contestant.nextPopupContactKey = 0;
	},

    popupCancel:function()
	{
		if(popup_company.changed == false && $('#new').length > 0 || page.activePopup == 'companyNew'){
			panel_contestant.companyDeleteAction($('#companyKey').val());
		}
		panel_contestant.contestantPanelRefresh();
		 page.hidePopup();
	}
}