$(function (){
   $("#contactContent").load("/content/canadalife/wegweiser_altersvorsorge/kontaktformular.jsp", initFormular);
})
function submitForm(){
	$('#contactFormular').submit();
}
function checkFieldGPNr(){
	if ( $('#isNoBrokerSwitch:checked').val() ){
		$('#gpnr').val("");
	}
}
function initFormular(){
	$('#isNoBrokerSwitch').click( checkFieldGPNr )
	$('#isNoBrokerSwitch').focus( checkFieldGPNr )
	$('#isNoBrokerSwitch').blur( checkFieldGPNr )
	$('#isNoBrokerSwitch').change( checkFieldGPNr )

	$("#contactFormular").validate({
		rules: {
			firma: "required",
			vorname: "required",
			name: "required",
			strasse: "required",
			nummer: "required",
			plz: "required",
			ort: "required"
		},
		errorPlacement: function(error, element) {
		},
		submitHandler: function(form) {
			checkFieldGPNr();
		
			var error = false;
			$("#errorBox").hide();
	
			// ----------------------------------------- bestellmöglichkeit
					
			if ( $('#bestellung:checked').length == 0 ){
				$('#errorBoxChoseType').show();
				error = true;
			} else {
  				$('#errorBoxChoseType').hide();
			}
	
			// ----------------------------------------- geschäftspartner

			if ( !($('#isBrokerSwitch:checked').val() || $('#isNoBrokerSwitch:checked').val() ) ){
				$('#errorBoxBroker').show();
				$('#gpnr').removeClass("error");
				error = true;
			} else if ( $('#isBrokerSwitch:checked').val() && !$('#gpnr').val() ){
				error = true;
  				$('#errorBoxBroker').hide();
				$('#gpnr').addClass("error");
			} else {
  				$('#errorBoxBroker').hide();
				$('#gpnr').removeClass("error");
			}
	
			// ----------------------------------------- data protection
			
			if ( $('#dataprotection:checked').length == 0 ){
				$('#errorBoxDataProtection').show();
				error = true;
			} else {
				$('#errorBoxDataProtection').hide();
			}
			
			if (!error){
                                                                                         $('.submitButton').html("Ihre Anfrage wird bearbeitet. Bitte warten Sie einen Moment...");
				var params = {};
				$(form).find("input:checked, input:text").each(function() {
					params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
				});
				$.post($(form).attr("action") + "?call=ajax", params, function(xml){
					$("#contactContent").load("/content/canadalife/wegweiser_altersvorsorge/kontaktformular_bestaetigung.jsp");
				});
			}
			return false;
		}
	});
}
function showKontaktPanel(){
 $("#actionKontakt").show();
}
function hideKontaktPanel(){
   $("#actionKontakt").hide();
   $("#contactContent").load("/content/canadalife/wegweiser_altersvorsorge/kontaktformular.jsp", initFormular);
}
function showSignPostPanel(){
 $("#actionSignPost").show();
}
function hideSignPostPanel(){
   $("#actionSignPost").hide();
}
