$(document).ready(function() { 
	$('#regForm input[value="EMAIL"],#regForm input[value="POST"]').bind("click", function(e){
		$('#regForm #userdaten').hide();
	});
	$('#regForm input[value="WEB"]').bind("click", function(e){
		$('#regForm #userdaten').show();
	});
	$('#regForm input[name="werbung_andere"]').bind("focus", function(e){
		$('#regForm input[value="Andere"]').attr('checked',true);
	});	
	$('#regForm input[name="bestandskunde"][value="laufendes Abo"]').bind("click", function(e){
		$('#regForm input[value="WEB"]').attr('checked',true);
		$('#regForm input[value="EMAIL"]').attr('disabled',true);
		$('#regForm input[value="POST"]').attr('disabled',true);
		$('#regForm #userdaten').show();
	});
	$('#regForm input[name="bestandskunde"][value="neue Abo"]').bind("click", function(e){
		$('#regForm input[value="WEB"]').attr('checked',true);
		$('#regForm input[value="EMAIL"]').attr('disabled',false);
		$('#regForm input[value="POST"]').attr('disabled',false);
	});	
	$.getScript("/cms/js/jquery.form.js", function(){
        // bind 'myForm' and provide a simple callback function 
		var options = { 
		        //target:        '#output1',   // target element(s) to be updated with server response 
		        //beforeSubmit:  showRequest,  // pre-submit callback 
		        success:       showResponse,  // post-submit callback
		 
		        // other available options: 
		        //url:       url         // override for form's 'action' attribute 
		        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
		        dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type) 
		        //clearForm: true        // clear all form fields after successful submit 
		        //resetForm: true        // reset the form after successful submit 
		 
		        // $.ajax options can be used here too, for example: 
		        //timeout:   3000 
		    }; 
		$('#regForm').ajaxForm(options); 
	});
}); 

//post-submit callback 
function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
	if (responseText.bSuccess) {
		$("#regForm").replaceWith(responseText.sText);
	} else {
		$("#regForm .form_row .errortext").remove();
		$("#regForm input").each(function(){this.style.borderColor = "";});
		for ( var int = 0; int < responseText.aError.length; int++) {
			$("#regForm input[name='"+ responseText.aError[int].name +"']").css("border-color","red");
			$("#regForm .form_row:has(input[name='"+ responseText.aError[int].name +"'])").prepend('<div class="errortext">'+ responseText.aError[int].value +'</div>');
		}
		var errorTextDiv = $("#regForm .form_row:has(.errortext) input");
		var offset = errorTextDiv.offset();
		$("html,body").animate({scrollTop: offset.top-100}, 1000);
	}
} 
