var formName='WeeeRS_Registration_form_';

$(document).ready(function(){
	aform_creation();
	aform_select_additional();
	aform_functionality();
	$('.aform_buttons input[value="Continue"]').click(aform_submit);
 });

function aform_creation() {
/*
	$('#aform .aform_section').each(function(){
		$(this).find('dd').each(function(i){
	
			var $mainid = $(this).parents('.aform_section').attr("id");
			$(this).find('input[type=radio]').replaceWith("<input type='radio' name="+$mainid+"_optionset"+i+" />");
			
		});
		$(this).find('input[type=radio]').each(function(i){
			var section = $(this).parents(".aform_section").attr("id");
			var thisid = $(this).attr("id",section+"_radio"+i);
			$(this).next('label').attr("for",$(thisid).attr("id"));
			var labelval = $(this).next('label').text();
			$(this).attr("value",labelval);
		});
	});	*/
	$('.aform .aform_section input').each(function() {
		
		var input_name = $(this).attr("name");
		var clear = $('<div class="aform_clear"></div>');
		var radiorow = $(this).parent();
	
		if ($(radiorow).parent().attr("class") != "aform_section_radioboxes" && input_name != formName+'regstep2_agree') {
			$(clear).insertAfter($(this));
		}
	});
	$('#aform_section5 .aform_section_checkboxes input[type=checkbox]').each(function(i) {
		var parentid = $(this).parents(".aform_section").attr("id");
		var e = i+1;
		var thisid = $(this).attr("id",parentid+"_cat"+e);
		var labelfor = $(this).attr("id");
		$("<label for="+labelfor+"></label>").insertAfter($(this)).text("Category "+e).hover(
	      	function () {
				$('#category'+e+'_info').show();
			}
			, 
	      	function () {
				$('#category'+e+'_info').hide();
			}
		);
	});
	// changing description for section6 depending on W/B/WB choice of step1
	s6desc=$('#aform_section6 .aform_section_content div[id=desc]');
	switch($('#'+formName+'regstep2_registration_type').val()){
		case 'B':
			s6desc.html('Your registration fee is based on your annual turnover of Batteries &/or Accumulators.');
			break;
		case 'W':
			s6desc.html('Your registration fee is based on your annual turnover of Electrical and Electronic Equipment.');
			break;
		case 'WB':
			s6desc.html('Your registration fee is based on your annual turnover of Electrical and Electronic Equipment and Batteries &/or Accumulators.');
			break;
	}
}

function aform_select_additional() {
	$('<div class="aform_clear"></div>').insertAfter($('#'+formName+'regstep1_country1'));
	/*for (var i=1;i<=1;i++) {
		$('#aform_section2_countryadd').click(function(){
			$cloned = $('#'+formName+'regstep1_country1').clone().attr("id",formName+"regstep1_country"+i).attr("name",formName+"regstep1_country"+i++).insertAfter($('#aform_section2 .aform_clear:last'));
			$remove = $('<span class="aform_section2_countryremove">remove</span>').insertAfter($cloned);
			$('<div class="aform_clear"></div>').insertAfter($remove);
			$remove.click(function(){
				$(this).next().remove();
				$(this).prev('span').remove();
				$(this).prev().remove();
				$(this).remove();
			});
		});
	}*/
}

function aform_functionality() {
	$('input').each(function(){
	var iid = $(this).attr("id");
		if( $(this).attr('type') == "text" ) {
			$(this).prev('label').attr("id","label_"+iid);
		} else if ( $(this).attr('type') == "radio") {
			$(this).prev('span').attr("id","span_"+iid);
		}
	});
	//$('#span_'+formName+'regstep1_wmp')
	$('.aform_section_radioboxes').find("dd:last").css("marginBottom","0px");
	$('.aform_section_radioboxes dd').find("span:first").attr("class","aform_section_radioboxes_heading");
	$('#aform_section6 .aform_section_radioboxes span').each(function(){ $(this).attr("class","aform_section_radioboxes_heading")});
	$('input[name='+formName+'regstep1_cs_name]').parent().hide();
	$('#aform_section3 #aform_section3_div_schemenumber').hide();
	$('input[name='+formName+'regstep1_cs_in_progress]').parent().hide();
	$('input[name='+formName+'regstep1_cs_name2]').parent().hide();

	$('#aform_section3 #cs_member').hide();

	$('#aform_section2').hide();
	//$('#'+formName+'regstep1_weee_number').next('.aform_clear').hide();
	//$('#'+formName+'regstep1_weee_number').prev('label').hide();
	//$('#'+formName+'regstep1_weee_number').hide();
	var wmp = $('#span_'+formName+'regstep1_wmp').parent();
	$(wmp).hide();
	$('#wmp_accept').hide();
	$('#wmp_creation').hide();

	$('#cs_member').click(controlWarnings);
	$('#cs_in_progress').click(controlWarnings);
	$('#wmp_creation').click(controlWarnings);
	$('#wmp').click(controlWarnings);
	$('#wmp_accept').click(controlWarnings);
	$('#bmp_creation').click(controlWarnings);
	$('#bmp').click(controlWarnings);
	$('#bmp_accept').click(controlWarnings);

	
	$('#payment_invoice input').attr("checked" , false);
	$('input#WeeeRS_Registration_form_regstep2_same_details').attr('checked','');
	//$('input#WeeeRS_Registration_form_regstep1_same_details').attr('checked','')
	
	
	$('input[name='+formName+'regstep1_service_type]').click(function(){
		
		//clear warnings
		$('#all_no').remove();

		//any change lets clear the radio buttons
		$("#aform_section3 #cs_member :input").each(function(){ $(this).attr("checked", "") } );
		$("#aform_section3 #cs_name :input").each(function(){ $(this).attr("checked", "") } );
		$("#aform_section3 #cs_name2 :input").each(function(){ $(this).attr("checked", "") } );
		$("#aform_section3 #cs_in_progress :input").each(function(){ $(this).attr("checked", "") } );
		$("#wmp :input").each(function(){ $(this).attr("checked", "") } );
		$("#bmp :input").each(function(){ $(this).attr("checked", "") } );
		
		if($(this).val() == "B2B"){
			$('#aform_section3 #cs_member').hide();
			$('#aform_section3 #cs_name').hide();
			$('#aform_section3 #cs_name2').hide();
			$('#aform_section3 #aform_section3_div_schemenumber').hide();
			$('#aform_section3 #cs_in_progress').hide();
		} else {
			$('#aform_section3 #cs_member').show();
			if($('#aform_section3 #cs_member input[checked]').val()=='N')$('#aform_section3 #cs_in_progress').show();
			else $('#aform_section3 #cs_in_progress').hide();
		}
		
		if ($(this).val() == "B2B" || $(this).val() == "B2C/B2B" ) {
			$('#aform_section3 #cs_member').show();
			
			
			$(wmp).show();
			$('#wmp_accept').show();
			$('#wmp_creation').show();
			
			if($('#aform_section3 #cs_member input[checked]').val()=='N')$('#aform_section3 #cs_in_progress').show();
			else $('#aform_section3 #cs_in_progress').hide();
			/*
			$(wmp).hide();
			$('#wmp_accept').hide();
			$('#wmp_creation').hide();
			*/
		} else {
			
			$(wmp).hide();
			$('#wmp_accept').hide();
			$('#wmp_creation').hide();
		}
		
		//wmpStateSwitch();
	});

	
	
	$('input[name='+formName+'regstep1_already_registered]').click(function(){
	
		if($(this).val() == "Y") {
			$('#'+formName+'regstep1_weee_number')
				.next().css("display","").end()
				.prev().css("display","").end()
				.css("display","");
		} else {
			$('#'+formName+'regstep1_weee_number')
				.next().hide().end()
				.prev().hide().end()
				.hide();	
		}
	
	});
	$('input[name='+formName+'regstep1_cs_member]').click(function(){
		if ($(this).val() == "Y" ) {
			$('input[name='+formName+'regstep1_cs_name]').parent().css("display","block");
			$('#aform_section3 #aform_section3_div_schemenumber').css("display","block");
			$('input[name='+formName+'regstep1_cs_in_progress]').parent().hide();
			$('input[name='+formName+'regstep1_cs_name2]').parent().hide();
		}
		else {
			$('input[name='+formName+'regstep1_cs_in_progress]').parent().css("display","block");	
			$('input[name='+formName+'regstep1_cs_name]').parent().hide();
			$('#aform_section3 #aform_section3_div_schemenumber').hide();			
		}
	});
	$('input[name='+formName+'regstep1_cs_in_progress]').click(function(){
		if ($(this).val() == "Y") {
			$('input[name='+formName+'regstep1_cs_name2]').parent().css("display","block");
		}
		else {
			$('input[name='+formName+'regstep1_cs_name2]').parent().hide();
		}
	});
	$('input[name='+formName+'regstep1_distance_sell]').click(function(){
		if($(this).val() == "Y") {
			$('#aform_section2').show();
		} else {
			$('#aform_section2').hide();
		}
	});
	$('#aform_section6 .aform_section_radioboxes input[type=radio]').click(function(){
		if ($(this).attr("value") == "150") {
			$('#aform_section6_sum span').text("150");
			$('#aform_section6_sum').css("display","inline");
		} else if ($(this).attr("value") == "250") {
			$('#aform_section6_sum span').text("250");
			$('#aform_section6_sum').css("display","inline");
		} else if ($(this).attr("value") == "500") {
			$('#aform_section6_sum span').text("500");
			$('#aform_section6_sum').css("display","inline");
		} else if ($(this).attr("value") == "1000") {
			$('#aform_section6_sum span').text("1000");
			$('#aform_section6_sum').css("display","inline");
		} else if ($(this).attr("value") == "2000") {
			$('#aform_section6_sum span').text("2000");
			$('#aform_section6_sum').css("display","inline");
		}
	});
	if ($('#aform_section6 .aform_section_radioboxes input[type=radio]').attr("checked") != "checked") {
		$('#aform_section6_sum').hide();
	}
	$('#payment_options input[type=radio]').click(function(){
		if($(this).val() == "Cheque"){
			$('#payment_cheque').show();
			$('#payment_ebt').hide();
		}
		else{
			$('#payment_cheque').hide();
			$('#payment_ebt').show();
		}
	});
	

	//page 2
	$('#aform_section8 input[type=radio]').click(function(){
		
		if($("#aform_section8 :input[checked]").length>3){
			
			displayWarning=true;
			$("#aform_section8 :input[checked]").each(function(){ 
				if($(this).attr('value')!='N') {
					displayWarning=false;
				}
			});
			
			if(displayWarning){
				dd=$('#aform_section8 .aform_section_radioboxes dd').get(2);
				$('<div id="all_no"><img src="templates/images/icon_notice.gif" />You have indicated NO to all Battery Types. '+
				'Please review your choices or contact the WEEE Register Society for guidance</div>').insertAfter(dd);
				$('#'+formName+'regstep2_Continue').attr('disabled','disabled');
			}
			else{
				$('#all_no').remove();
				if(!$("img[src='templates/images/icon_notice.gif']").is(':visible')){
					$('#'+formName+'regstep1_Continue').attr('disabled','');
				}
			}
		}
		
		
	});




	// Are contact details the same as producer details?
	$('input[name='+formName+'regstep2_same_details]').click(function(){
		if($(this).val()=='Y'){
			contactDetailsSwitch(true);
		}else{
			contactDetailsSwitch(false);
		}
		
	});
	// defaults
	contactDetailsSwitch(false);
	// if all 6 radiobuttons on step1 set to NO, showing a warning
	$('#aform_section1 input[type=radio]').click(function(){
		buttons=$('#aform_section1 input[type=radio][value=N][checked]');
		if(buttons.length==6){
			dd=$('#aform_section1 .aform_section_radioboxes dd').get(5);
			if($('#all_no').length==0)$('<div class="warning_msg" id="all_no"><img src="templates/images/icon_notice.gif" />You have indicated NO to all questions. '+
				'Please review your choices or contact the WEEE Register Society for guidance</div>').insertAfter(dd);
			$('#'+formName+'regstep1_Continue').attr('disabled','disabled');
		}else{
			$('#all_no').remove();
			if(!$(".warning_msg").is(':visible')){
				$('#'+formName+'regstep1_Continue').attr('disabled','');
			}
		}
	});
	
	// switching registration type radio will switch controls
	$('#span_'+formName+'regstep1_registration_type').nextAll('input[type=radio]').click(function(){
		batteryFormSwitch();
	});

	// switching registration type radio will switch controls
	$('#span_'+formName+'regstep1_service_type').nextAll('input[type=radio]').click(function(){
		batteryFormSwitch();
	});


	// defaults
	batteryFormSwitch();
	// batteries deal is not so easy, there is another question regarding it
	$('#span_'+formName+'regstep1_battery_import').nextAll('input[type=radio]').click(function(){
		// on YES showing batteries categories, on NO showing a warning
		if($(this).attr('value')=='N'){
			$('#aform_section8').hide();
			$('<div class="warning_msg" id="no_import"><img src="templates/images/icon_notice.gif" />Please contact the WEEE Register '+
				'Society for guidance on registering as a Battery Producer</div>').insertAfter($(this).next());
			$('#'+formName+'regstep1_Continue').attr('disabled','disabled');
		}else{
			$('#no_import').remove();
			$('#aform_section8').show();
			if(!$(".warning_msg").is(':visible')){
				$('#'+formName+'regstep1_Continue').attr('disabled','');
			}
		}
	});
	// if battery only was selected, hiding checkboxes on step2
	if($('#'+formName+'regstep2_registration_type').attr('value')=='B'){
		$('#aform_section5').hide();
	}else{
		$('#aform_section5').show();
	}
}
function wmpStateSwitch(){
	// Waste Management Plan: if all answers NO, displaying warning
	// answers considered differs depending on service type
	// depending on the service type we consider different radios
	messageb2b=$('<div id="wmp_b2b_all_no"><img src="templates/images/icon_notice.gif" />You have indicated NO one of the EEE Waste Management Plan questions. '+
				'Please review your choice or contact the WEEE Register Society for guidance</div>');
	messageb2c=$('<div id="wmp_b2c_all_no"><img src="templates/images/icon_notice.gif" />You have indicated NO to all compliance scheme questions. '+
				'Please review your choices or contact the WEEE Register Society for guidance</div>');
	$('div#wmp_b2b_all_no').remove();
	$('div#wmp_b2c_all_no').remove();
	
	funcb2b=function(){
		if($('input[name='+formName+'regstep1_wmp_creation][checked]').val()=='N'){
			messageb2b.insertAfter($('dd#wmp_creation'));
			// preventing wizard processing
			//$('#'+formName+'regstep1_Continue').attr('disabled','disabled');
		}else{
			$('div#wmp_b2b_all_no').remove();
			//$('#'+formName+'regstep1_Continue').attr('disabled','');
			$('#wmp_accept').show();
			$('#wmp_creation').show();

			if($('input[name='+formName+'regstep1_wmp][checked]').val()=='N'){
			messageb2b.insertAfter($('dd#wmp'));
			// preventing wizard processing
			//$('#'+formName+'regstep1_Continue').attr('disabled','disabled');
			}else{
				$('div#wmp_b2b_all_no').remove();
				//$('#'+formName+'regstep1_Continue').attr('disabled','');
				$('#wmp_accept').show();
				$('#wmp_creation').show();
				
				if($('input[name='+formName+'regstep1_wmp_accept][checked]').val()=='N'){
					messageb2b.insertAfter($('dd#wmp_accept'));
					// preventing wizard processing
					//$('#'+formName+'regstep1_Continue').attr('disabled','disabled');
				}else{
					$('div#wmp_b2b_all_no').remove();
					//$('#'+formName+'regstep1_Continue').attr('disabled','');
					$('#wmp_accept').show();
					$('#wmp_creation').show();
				}
			}
		}
	}

	funcb2c=function(){
		if($('input[name='+formName+'regstep1_cs_member][checked]').val()=='N' &&
			$('input[name='+formName+'regstep1_cs_in_progress][checked]').val()=='N' &&
			$('input[name='+formName+'regstep1_service_type][checked]').val()!='B2B'){
			messageb2c.insertAfter($('dd#cs_in_progress'));
			// preventing wizard processing
			//$('#'+formName+'regstep1_Continue').attr('disabled','disabled');
		}else{
			$('div#wmp_b2c_all_no').remove();
			$('#'+formName+'regstep1_Continue').attr('disabled','');
		}
	}

	funcb2bb=function(){
		if($('input[name='+formName+'regstep1_cs_member][checked]').val()=='N' &&
			   $('input[name='+formName+'regstep1_cs_in_progress][checked]').val()=='N' &&
			   $('input[name='+formName+'regstep1_wmp_creation][checked]').val()=='N' &&
			   $('input[name='+formName+'regstep1_wmp][checked]').val()=='N' &&
			   $('input[name='+formName+'regstep1_wmp_accept][checked]').val()=='N'){
				
				messageb2c.insertAfter($('dd#cs_in_progress'));
				// preventing wizard processing
				//$('#'+formName+'regstep1_Continue').attr('disabled','disabled');
				funcb2b();
			}else{
				$('div#wmp_b2c_all_no').remove();
				$('#'+formName+'regstep1_Continue').attr('disabled','');
			}

	}

	switch($('input[name='+formName+'regstep1_service_type][checked]').val()){
		case 'B2B':
			
			// user clicks no to one question: wmp field
			$('input[name='+formName+'regstep1_wmp_creation]').click(funcb2bb);
			$('input[name='+formName+'regstep1_wmp]').click(funcb2bb);
			$('input[name='+formName+'regstep1_wmp_accept]').click(funcb2bb);
			// unbinding others
			$('input[name='+formName+'regstep1_cs_member]').unbind('click',funcb2c);
			$('input[name='+formName+'regstep1_cs_in_progress]').unbind('click',funcb2c);
			funcb2b();
			
			
			
			break;
		case 'B2C':
			
			// user clicks no to both questions: cs_member and cs_in_progress
			$('input[name='+formName+'regstep1_cs_member]').click(funcb2c);
			$('input[name='+formName+'regstep1_cs_in_progress]').click(funcb2c);
			// unbinding others
			$('input[name='+formName+'regstep1_wmp_creation]').unbind('click',funcb2b);
			$('input[name='+formName+'regstep1_wmp]').unbind('click',funcb2b);
			$('input[name='+formName+'regstep1_wmp_accept]').unbind('click',funcb2b);
			funcb2c();
			
			break;

		case 'B2C/B2B':
			// both warning should appear
			$('input[name='+formName+'regstep1_wmp_creation]').unbind('click',funcb2b);
			$('input[name='+formName+'regstep1_wmp]').unbind('click',funcb2b);
			$('input[name='+formName+'regstep1_wmp_accept]').unbind('click',funcb2b);

			$('input[name='+formName+'regstep1_cs_member]').click(funcb2c);
			$('input[name='+formName+'regstep1_cs_in_progress]').click(funcb2c);
			funcb2b();
			funcb2c();
			break;
	}
}

funcbmp=function(){
	
	if($('input[name='+formName+'regstep1_bmp][checked]').val()=='N' ||
		$('input[name='+formName+'regstep1_bmp][checked]').val()=='NA'){
		//messagebwp.insertAfter($('dd#bmp'));
		$('#bmp_accept').hide();
		// preventing wizard processing
		//$('#'+formName+'regstep1_Continue').attr('disabled','disabled');
	}else{
		$('#bmp_accept').show();
		$('div#bmp_no').remove();
		$('#'+formName+'regstep1_Continue').attr('disabled','');
	}
	
}


function batteryFormSwitch(value){
	messagebwp=$('<div id="bmp_no"><img src="templates/images/icon_notice.gif" />As you have indicated NO to the self-complying Battery producer Waste Management plan question. '+
				'Please review your choice or contact the WEEE Register Society for guidance</div>');
	$('div#bmp_no').remove();
	$("#bmp :input").each(function(){ $(this).attr("checked", "") } );
	
	
	
	var service_type_checked=$('#service_type [checked]').attr('value');

	// this one is called on both pages
	// value on the first step...
	if(!value)value=$('#span_'+formName+'regstep1_registration_type').nextAll('input[type=radio][checked]').attr('value');
	// .. or on the second
	if(!value)value=$('#'+formName+'regstep2_registration_type').attr('value');
	if(value=='W'){
		// Compliance Scheme and Waste Management Plans should be visible
		// question "Please indicate what types of Batteries ..." should NOT be visible
		//  "Compliance Scheme and Waste Management Plans" should be visible
		$('#aform_section1 .aform_section_radioboxes dd').show().prev('hr').show();
		
		$('#distance_selling_w').show();
		$('#distance_selling_wb').hide();
		$('#distance_selling_b').hide();
		
		
		$('#aform_section8').hide();
		$('#aform_section8_1').hide();
		//$('#aform_section3').show();

		// showing/hiding coutries
		if($('#'+formName+'regstep1_distance_sell[checked]').val()=='Y')$('#aform_section2').show();
		else $('#aform_section2').hide();
		
		//service type section
		
		$('#aform_section3 #service_type').show();

		service_types_hide();
		
		if(service_type_checked=='B2C'){
			$('#cs_member').show();	
		}else if(service_type_checked=='B2B'){
			$('#wmp').show();
			$('#wmp_accept').show();
		}else if(service_type_checked=='B2C/B2B'){
			$('#cs_member').show();	
			$('#wmp').show();
			$('#wmp_accept').show();
		}

		$('input[name='+formName+'regstep1_bmp]').click(function(){
			if($(this).val()=='Y'){
				$('#bmp_accept').show();
			}else{
				$('#bmp_accept').hide();
			}
		});
		
	}
	else if(value=='B'){
		// Compliance Scheme and Waste Management Plans should NOT be visible
		// question "Please indicate what types of Batteries your company places on the Irish Market" should be visible
		//  "Compliance Scheme and Waste Management Plans" should be hidden
		
		$('#aform_section1 .aform_section_radioboxes dd').hide().prev('hr').hide();

		$('#distance_selling_w').hide();
		$('#distance_selling_wb').hide();
		$('#distance_selling_b').show();
		
		if(
			// first step condition
			$('#aform_section8_1').attr('value')=='Y' ||
			// second step condition
			$('#'+formName+'regstep2_battery_import').attr('value')=='Y'
			)$('#aform_section8').show();
		else $('#aform_section8').hide();
		$('#aform_section8_1').show();
		
		$('#aform_section3 #service_type').hide();
		
		//service type section
		
		service_types_hide();
		
		$('#cs_member').show();	
		
		$('input[name='+formName+'regstep1_bmp]').click(function(){
			if($(this).val()=='Y'){
				$('#bmp_accept').show();
			}else{
				$('#bmp_accept').hide();
			}
		});
		
		$('#aform_section2').hide();
	}
	else if(value=='WB'){
		// ALL of
		// Compliance Scheme and Waste Management Plans 
		//  "Compliance Scheme and Waste Management Plans" 
		// AND
		// question "Please indicate what types of Batteries your company places on the Irish Market" 
		// should be visible
		
		$('#aform_section1 .aform_section_radioboxes dd').show().prev('hr').show();

		$('#distance_selling_w').hide();
		$('#distance_selling_wb').show();
		$('#distance_selling_b').hide();
		
		if(
			// first step condition
			$('#aform_section8_1').attr('value')=='Y' ||
			// second step condition
			$('#'+formName+'regstep2_battery_import').attr('value')=='Y'
			)$('#aform_section8').show();
		else $('#aform_section8').hide();
		$('#aform_section8_1').show();

		$('#aform_section3 #service_type').show();
		
		service_types_hide();
		
		if(service_type_checked=='B2C'){
			$('#cs_member').show();
			$("#bmp :input").each(function(){ $(this).attr("checked", "checked") } );
			$("#bmp_accept :input").each(function(){ $(this).attr("checked", "checked") } );
		}else if(service_type_checked=='B2B'){
			$('#wmp').show();
			$('#wmp_accept').show();
			$('#bmp').show();
			$('#bmp_accept').show();
		}else if(service_type_checked=='B2C/B2B'){
			$('#cs_member').show();	
			$('#wmp').show();
			$('#wmp_accept').show();
			$('#bmp').show();
			$('#bmp_accept').show();
		}
	
		$('input[name='+formName+'regstep1_bmp]').click(function(){
			if($(this).val()=='Y'){
				$('#bmp_accept').show();
			}else{
				$('#bmp_accept').hide();
			}
		});
		
		
		
		// showing/hiding coutries
		if($('#'+formName+'regstep1_distance_sell[checked]').val()=='Y')$('#aform_section2').show();
		else $('#aform_section2').hide();
	}
}
function contactDetailsSwitch(disabled){
	if(disabled==undefined)disabled=$('input[name='+formName+'regstep2_contact_address1]').attr('disabled')?false:true;
	//$('input[name='+formName+'regstep2_contact_name]').attr('disabled',disabled);
	$('input[name='+formName+'regstep2_contact_address1]').attr('disabled',disabled);
	$('input[name='+formName+'regstep2_contact_address2]').attr('disabled',disabled);
	$('input[name='+formName+'regstep2_contact_address3]').attr('disabled',disabled);
	$('input[name='+formName+'regstep2_contact_address4]').attr('disabled',disabled);
	$('input[name='+formName+'regstep2_contact_phone]').attr('disabled',disabled);
	$('input[name='+formName+'regstep2_contact_fax]').attr('disabled',disabled);
	$('input[name='+formName+'regstep2_contact_email]').attr('disabled',disabled);
	// when disabled, values should be set from company
	if(disabled==true){
		//$('input[name='+formName+'regstep2_contact_name]').attr('value',$('input[name='+formName+'regstep2_company_name]').attr('value'));
		$('input[name='+formName+'regstep2_contact_address1]').attr('value',$('input[name='+formName+'regstep2_address1]').attr('value'));
		$('input[name='+formName+'regstep2_contact_address2]').attr('value',$('input[name='+formName+'regstep2_address2]').attr('value'));
		$('input[name='+formName+'regstep2_contact_address3]').attr('value',$('input[name='+formName+'regstep2_address3]').attr('value'));
		$('input[name='+formName+'regstep2_contact_address4]').attr('value',$('input[name='+formName+'regstep2_address4]').attr('value'));
		$('input[name='+formName+'regstep2_contact_phone]').attr('value',$('input[name='+formName+'regstep2_phone]').attr('value'));
		$('input[name='+formName+'regstep2_contact_fax]').attr('value',$('input[name='+formName+'regstep2_fax]').attr('value'));
		$('input[name='+formName+'regstep2_contact_email]').attr('value',$('input[name='+formName+'regstep2_email]').attr('value'));
	}else{
		// when enabled they should be all cleared
		//$('input[name='+formName+'regstep2_contact_name]').attr('value','');
		$('input[name='+formName+'regstep2_contact_address1]').attr('value','');
		$('input[name='+formName+'regstep2_contact_address2]').attr('value','');
		$('input[name='+formName+'regstep2_contact_address3]').attr('value','');
		$('input[name='+formName+'regstep2_contact_address4]').attr('value','');
		$('input[name='+formName+'regstep2_contact_phone]').attr('value','');
		$('input[name='+formName+'regstep2_contact_fax]').attr('value','');
		$('input[name='+formName+'regstep2_contact_email]').attr('value','');
	}
}

function service_types_hide(){
	$('#cs_member').hide();
	$('#cs_name').hide();
	$('#cs_number').hide();
	$('#cs_in_progress').hide();
	$('#cs_name2').hide();
	$('#wmp').hide();
	$('#wmp_accept').hide();
	$('#bmp').hide();
	$('#bmp_accept').hide();
}

function aform_submit() {
	$('.aform_error').remove();
	$('label').css("color","#333");
	$('.aform_section_radioboxes_heading').css("color","#333");
	$('.aform_section8_portable_span').css("color","#333");
	return;
	$('.aform input').each(function(){
		if ($(this).val() == "" && $(this).attr("type") != "radio" && $(this).parent().is(":visible") == true && $(this).attr("class") != "aform_optional") {
			$("<span class='aform_error'>This field is not filled out properly</span>").insertAfter($(this));
			$(this).prev('label').css("color","red");
		} else if (($(this).attr("type") == "radio" && $(this).attr("checked") == false && $(this).nextAll('input').attr("checked") == false && $(this).parent().is(":visible") == true) && $(this).attr("name") != "'+formName+'regstep2_reg_fee") {
				$("<span class='aform_error'>This field is not filled out properly</span>").appendTo($(this).parent());
				$(this).prevAll('span').css("color","red");
		} 
	});
	$('.aform select').each(function(){
		var def = $(this).find('option:first').attr("value","Select Country");
		if ( $(def).is(":selected") == true && $(this).attr("id") != formName+"regstep1_country1") {
			$("<span class='aform_error'>This field is not filled out properly</span>").insertAfter($(this).next());
		}
		else if ($(def).is(":selected") == true && $(this).attr("id") == formName+"regstep1_country1") {
			$("<span class='aform_error'>This field is not filled out properly</span>").insertAfter($(this));
		}

	});
	$('.aform_section_checkboxes').each(function(){
		if ($(this).find('input[type=checkbox]').is(":checked") == false ) {
			$("<span class='aform_error'>This fields are not filled out properly</span>").appendTo($(this));
			$(this).find("label").css("color","red");
		}
	});
	if ($('#aform_section6 .aform_section_radioboxes input').is(":checked") == false ) {
		$("<span class='aform_error'>Please select one of the options.</span>").appendTo($('#aform_section6 .aform_section_radioboxes'));
		$('#aform_section6 .aform_section_radioboxes span').css("color","red");
	}
}

controlWarnings = function(){
	$('#all_no').remove();
	//checkProducer
	switch($('#aform_registration_type input[checked]').val()){
		case 'W':
			// W
			switch($('input[name='+formName+'regstep1_service_type][checked]').val()){
				case 'B2C':
					if(warningCS()){
						$('<div id="all_no"><img src="templates/images/icon_notice.gif" />You have indicated NO to all compliance scheme questions. '+
						'Please review your choices or contact the WEEE Register Society for guidance</div>').insertAfter($('dd#cs_in_progress'));
					}
					break;
				case 'B2C/B2B':
					if(warningCS()){
						$('<div id="all_no"><img src="templates/images/icon_notice.gif" />You have indicated NO to all compliance scheme questions. '+
						'Please review your choices or contact the WEEE Register Society for guidance</div>').insertAfter($('dd#cs_in_progress'));
					}
					break;
				case 'B2B':
					if(warningCS()){
						if(warningWMP()){
							$('<div id="all_no"><img src="templates/images/icon_notice.gif" />You have indicated NO to all compliance scheme questions and all Waste Management Plan questions. '+
							'Please review your choices or contact the WEEE Register Society for guidance</div>').insertAfter($('dd#wmp_accept'));
						}
					}
					break;
			}
			break;
		case 'WB':
			//WB
			switch($('input[name='+formName+'regstep1_service_type][checked]').val()){
				case 'B2C':
					if(warningCS()){
						$('<div id="all_no"><img src="templates/images/icon_notice.gif" />You have indicated NO to all compliance scheme questions and all Battery Waste Management Plan questions. '+
						'Please review your choices or contact the WEEE Register Society for guidance</div>').insertAfter($('dd#cs_in_progress'));
						
					}
					break;
				case 'B2C/B2B':
					if(warningCS()){
						if(warningBMP()){
								$('<div id="all_no"><img src="templates/images/icon_notice.gif" />You have indicated NO to all compliance scheme questions and all EEE/Battery Waste Management Plan questions. '+
								'Please review your choices or contact the WEEE Register Society for guidance</div>').insertAfter($('dd#bmp_accept'));
						}
					}
					break;
			}
			break;
		case 'B':
			//B
			if(warningCS()){
				$('#bmp').show();
				if(warningBMP()){
					$('<div id="all_no"><img src="templates/images/icon_notice.gif" />You have indicated NO to all compliance scheme questions and all Battery Waste Management Plan questions. '+
					'Please review your choices or contact the WEEE Register Society for guidance</div>').insertAfter($('dd#bmp_accept'));
				}
			}else{
				$('#bmp').hide();
				$('#bmp_accept').hide();
			}
			break;
	
	}
}


warningCS =function(){
	if($('input[name='+formName+'regstep1_cs_member][checked]').val()=='N' &&
	$('input[name='+formName+'regstep1_cs_in_progress][checked]').val()=='N'){
		return true;
	} else {
		return false;
	}
}



warningWMP =function(){
	/*
	check
	wmp_creation
	wmp
	wmp_accept
	*/
	if($('input[name='+formName+'regstep1_wmp_accept][checked]').val()=='N' &&
	$('input[name='+formName+'regstep1_wmp][checked]').val()=='N'){
		return true;
	} else {
		return false;
	}
}

warningBMP =function(){
	/*
	check
	bmp_creation
	bmp
	bmp_accept
	
	if($('input[name='+formName+'regstep1_bmp_accept][checked]').val()=='N' ||
	($('input[name='+formName+'regstep1_bmp][checked]').val()=='N' || $('input[name='+formName+'regstep1_bmp][checked]').val()=='NA')){
		return true;
	} else {
		return false;
	}
	*/
	if($('input[name='+formName+'regstep1_bmp_accept][checked]').val()=='N'){
		return true;
	} else {
		return false;
	}
}

