//$('div.info').html("<p>loading with ajax ...</p>");
function activateDeleteConfirmation(selector, question) {
	$(document).ready(function() {
		$(selector).click(
	  	function()
	  	{
			var answer = confirm(question);
			if (answer) return true;
			return false;
 		});  
	
	});
}

function activateRegistrationFormChooser() {
	$(document).ready(function()
	{
	$('form[id=chooser-form] input[type="submit"]').hide();
	if($('#customer_customerType').val() == 'naturalPerson') {
		$('form[id=chooser-form] #natural-person').attr('checked', 'checked');
	}
	else if($('#customer_customerType').val() == 'naturalPersonCompany') {
		$('form[id=chooser-form] #natural-person-company').attr('checked', 'checked');
	}
	else {
		$('form[id=chooser-form] #company').attr('checked', 'checked');
	}
  	$('form[id=chooser-form] #natural-person').click(
	  	function()
	  	{	
			loadNaturalPersonForm();
 		});  
  	$('form[id=chooser-form] #natural-person-company').click(
	  	function()
	  	{
			loadNaturalPersonCompanyForm();
 		});  
  	$('form[id=chooser-form] #company').click(
	  	function()
	  	{
			loadCompanyForm();
 		});
	});
}


function activateRequestFormChooser() {
    $(document).ready(function()
    {
        $('form[id=chooser-form] input[type="submit"]').hide();
        if($('#customer_formType').val() == 'question') {
            $('form[id=chooser-form] #question-form').attr('checked', 'checked');
        }
        else if($('#customer_formType').val() == 'complaint') {
            $('form[id=chooser-form] #complaint-form').attr('checked', 'checked');
        }
        
        $('form[id=chooser-form] #question-form').click(
           function(){    
            $('div.cust-form').load(
                $('form[id=chooser-form]').attr('action')+' div.content', { formType: 'question' },
                function() { 
                     $(document).ready(); }
            );
 
             }
        );  
        $('form[id=chooser-form] #complaint-form').click(
              function()
              {
                $('div.cust-form').load(
                    $('form[id=chooser-form]').attr('action')+' div.content', { formType: 'complaint' },
                    function() { 
                         $(document).ready(); 
                    }
                ); 
             }
        );  

    });
}


function activateEroticNumbersToogle() {
    $(document).ready(function()
    {
        $('form[id=ero-chooser-form] input[type="submit"]').hide();
        if($('#new_businessTypeCheckbox').val() == 'erotic') {
            $('form[id=ero-chooser-form] #erotic-form').attr('checked', 'checked');
        }
        else if($('#new_businessTypeCheckbox').val() == 'non-erotic') {
            $('form[id=ero-chooser-form] #non-erotic-form').attr('checked', 'checked');
        }  //*/
        
        $('form[id=ero-chooser-form] #erotic-form').click(
           function(){ 
                $('div.new_service').load( 
                                    $('form[id=ero-chooser-form]').attr('action') +' div.tarrifs' , { businessTypeCheckbox: 'erotic' },
                                    function() { $(document).ready(); }
                                  );
                 }
            
        );  
        $('form[id=ero-chooser-form] #non-erotic-form').click(
              function(){                
                $('div.new_service').load(
                                    $('form[id=ero-chooser-form]').attr('action') +' div.tarrifs' , { businessTypeCheckbox: 'non-erotic' },
                                    function() { $(document).ready(); }
                                    );                
                }
        );  

    });

}

function activateMoreServicesToogle() {
	$(document).ready(function()
	{
  		if($('#customer_service_more_services').attr('checked') == true)
			$('#more_services').show();
		else
			$('#more_services').hide();
		$('#customer_service_more_services').click(
		function()
	  	{
	  		if($('#customer_service_more_services').attr('checked') == true)
				$('#more_services').show();
			else
				$('#more_services').hide();
		});
	});
}

function getAllElements(selector) {	
  	var array = new Array();
  	$(selector).each(
		function()
		{
			//$('div.info').append('<p>SET: <strong>'+this.name+'</strong>: '+this.checked+'</p>');
			switch(this.type) {
				case 'checkbox':
					array[this.name] = this.checked;
					break;
				default:
					array[this.name] = this.value;
			}
		}
	);
	return array;
}

function setAllElements(selector, array) {
	$(selector).each(
		function()
		{
			if(array[this.name]) {
				switch(this.type) {
					case 'checkbox':
						this.checked = array[this.name];
						break;
					default:
						this.value = array[this.name];
				}
			}
		}
	);
}

function loadNaturalPersonForm() {
	var arr = getAllElements('form:last input[type="text"], input[type="checkbox"]');
  	//$('div.customer').slideUp();
	$('div.customer').load(
		$('form[id=chooser-form]').attr('action')+' div.content', { customerType: 'naturalPerson' },
		function() { 
			setAllElements('form:last input[type="text"], input[type="checkbox"]', arr);
			//$('div.customer').slideDown();
			activateContactAddressToogle();
			activateMoreServicesToogle();
		}
	);
}

/**
 * Laduje de facto prawie taki sam formularz jak w przypadku osoby fizycznej.
 */
function loadNaturalPersonCompanyForm() {
	var arr = getAllElements('form:last input[type="text"], input[type="checkbox"]');
  	//$('div.customer').slideUp();
	$('div.customer').load(
		$('form[id=chooser-form]').attr('action')+' div.content', { customerType: 'naturalPersonCompany' },
		function() { 
			setAllElements('form:last input[type="text"], input[type="checkbox"]', arr);
			//$('div.customer').slideDown();
			activateContactAddressToogle();
			activateMoreServicesToogle();
		}
	);
}

function loadCompanyForm() {
	var arr = getAllElements('form:last input[type="text"], input[type="checkbox"]');
  	//$('div.customer').slideUp();
	$('div.customer').load(
		$('form[id=chooser-form]').attr('action')+' div.content', { customerType: 'company' },
		function() { 
			setAllElements('form:last input[type="text"], input[type="checkbox"]', arr);
			//$('div.customer').slideDown();
			activateContactAddressToogle();
			activateMoreServicesToogle();
		}
	);
}
