/* submit data to filter */
var do_select;

function saveCompanyData()
{
	 var formfields = new Object();
		
		$("#company-information input[type=text], #billing-information input[type=text], .end-registration input[type=hidden]").each(function()
		{	
			formfields[this.name] = this.value;
		});
		$("#billing_info_same_as_firm_info").each(function(){
			if ( this.checked )
			{
				formfields[this.name] = 1;
			}
			else
			{
				formfields[this.name] = 0;
			}
		});
		
		$.ajax({
			  type: 'post', 
			  url: '/coursesignup/save_company_data',
			  data: formfields
		});
}
function saveparticipants(ajax_filter_url, select)
{
	// for each checkbox
	var formfields = new Object();
	
	$("#participants input[type=text]").each(function()
	{	
		formfields[this.name] = this.value;
	});
	$("#participants input[type=radio]").each(function()
	{	
		if ( this.checked )
		{
			formfields[this.name]=this.value;
		}
	});
	/* for each hidden form field */
	$("#participants input[type=hidden]").each(function()
	{
		formfields[this.name] = this.value;
	});
	
	$("#participants textarea").each(function()
	{
		formfields[this.name] = this.value;
	});
	$("#participants select").each(function()
	{
		formfields[this.name] = this.value;
	});
	
	$("#participants input[type=checkbox]").each(function()
	{
		if  ( this.name == 'signupdata[signup][newsletter]' || jQuery(this).attr('class') == 'extra_product_box')
		{
			if ( this.checked )
			{
				formfields[this.name]=this.value;
			}
		}
		else
		{
			if(this.checked)
			{
				formfields[this.name] = '1';
			}
			else
			{
				formfields[this.name] = '0';
			}
		}
	});
	
	do_select = select;
	$.ajax({
	  type: 'post', 
	  url: ajax_filter_url,
	  data: formfields,
	  success: handleFilterResponseCourse
	});
	
}
function deleteparticipant( url )
{
	var formfields = new Object();

	/* for each hidden form field */
	$("#participants input[type=hidden]").each(function()
	{
		formfields[this.name] = this.value;
	});
	$.ajax({
		  type: 'post', 
		  url: url,
		  data: formfields,
		  success: handleFilterResponseCourse
		});
}

/* handle filter response */
function handleFilterResponseCourse(data)
{
	$('#participants_list').html(data);
	if ( do_select )
	{
		setTimeout("document.getElementById(do_select).focus()", 100 );
	}
	var length = $('#deltakere').height();
	$('#deltakere').animate({scrollTop: 2000}, 1000);
	
}
$(document).ready(function () {
	   $("form#company").submit(function() {   
			   if (!$('#check-box-2').attr('checked'))
			   {
				   jAlert( $( "#forgot_condition" ).html(), $( "#forgot_condition_heading" ).html());
				   return false;
			   }
		});
	   
	   $("#company-information input[type=text], #billing-information input[type=text]").blur(function(){
		   saveCompanyData();
	   });
	   $("#billing_info_same_as_firm_info").click(function(){
		   saveCompanyData();
	   });
	   $("span#cancel_link").live("click", function(){
		   $.alerts.okButton = $( "#cancel_ok_button" ).html();
		   $.alerts.cancelButton = $( "#cancel_cancel_button" ).html();
		   jConfirm( $( "#cancel_dialog" ).html(), $( "#cancel_heading" ).html(), function(r) {
			    if ( r )
			    {
			    	window.location.href = $("#cancel_link").attr("title");
			    }
			});
	   });
	   
		//$('.deltakere .extra-products').hide();
	   $('.toggle-link').live('click', function(){
			var idfield = $(this).attr('id');
			var id		= idfield.split('_');
			var clicked = $(this);
			if (clicked.hasClass('toggled')) {
				clicked.html('+').removeClass('toggled');
				$('#hide_extra_product_list_' + id[1] ).val('1');
				$('#extra_product_' + id[1] ).hide();
			} else {
				clicked.html('-').addClass('toggled');
				$('#hide_extra_product_list_' + id[1] ).val('0');
				$('#extra_product_' + id[1] ).show();
			}
	   });
	

	
	   
	   /* list for clicks on the newsletter signup */
	 /*  $("#newsletter").click(function() {
		   if(this.checked)
		   {
			   $("#newsletter-categories").css("display", "block");
			   $("#newsletter-categories").show('blind');
		   }
		   else
		   {
			   $("#newsletter-categories").hide('blind');
		   }
	   });
	   
	   
		*/
	   $("#newsletter").live("click", function(){
			if(this.checked)
			   {
				   $("#newsletter-categories").css("display", "block");
				   $("#newsletter-categories").show('blind');
			   }
			   else
			   {
				   $("#newsletter-categories").hide('blind');
			   }
		});
	   $("#newsletter-categories").hide();
	
	   
			checkInvoiceAsDelivery();
			
			// when the user edits the delivery address
			$("#company-information input").keyup(function() {
				// if invoice address should be the same as delivery
		      	if($("#billing_info_same_as_firm_info").attr('checked'))
		      	{
		      		copyInvoiceData();
		      	}
			});
			
			// when the "invoice as delivery" check box is clicked
			$("#billing_info_same_as_firm_info").live("click", function(event) {
			
				checkInvoiceAsDelivery();
			});
			
			
			function checkInvoiceAsDelivery()
			{
				// if the "invoice as delivery" checkbox is checked
				if($("#billing_info_same_as_firm_info").attr('checked'))
				{
					copyInvoiceData();
					
					// disable invoice fields
					$("#billing-information input.text").each(function( intIndex ){
						$(this).attr("readonly", true);
						$(this).addClass("readonly");
						$(this).attr("tabindex", "-1");
					});			
				}
				
				else
				{
					// enable invoice fields
					$("#billing-information input.text").each(function( intIndex ){
						$(this).attr("readonly", false);
						$(this).removeClass("readonly");
						$(this).attr("tabindex", false);
					});	
				}
			}
			
			function copyInvoiceData()
			{
				// copy data from delivery to invoice fields
		  		$("#firm_invoice").attr("value", $("#firm").attr('value'));
		  		$("#department_invoice").attr("value", $("#department").attr('value'));
		  		$("#phone_invoice").attr("value", $("#phone").attr('value'));
		  		$("#address_invoice").attr("value", $("#address").attr('value'));
		  		$("#zip_invoice").attr("value", $("#zip").attr('value'));
		  		$("#city_invoice").attr("value", $("#city").attr('value'));
			}

});


