$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};

$('#sendorder').live("click", function() {
	var error = false;
	$('.required').each(function() { 
		if($(this).val() == '') {
			$(this).css('border', '1px solid red');
			error = true;
		} else if($(this).val() == 'HERBALIFE-Berater' && $('#berater').val() == '' || $('#berater').val() == 'Name des Beraters') {
			$('#berater').css('border', '1px solid red');
			error = true;
		}
	});
	if(error == false) {
		$.ajax({
			type: 'POST',
			url: 'send.php',
			cache: false,
			data: $('#orderherbabook').serialize(),
			success: function(html) {
				if(html == 'OK') {
					$('#orderherbabook').clearForm();
					alert('Wir haben Ihre Bestellung erhalten\r\nVielen Dank!');
				}
			}
		});
	}
});

$('#sendcontact').live("click", function() {
	var error = false;
	$('.required').each(function() { 
		if($(this).val() == '') {
			$(this).css('border', '1px solid red');
			error = true;
		}
	});
	if(error == false) {
		$.ajax({
			type: 'POST',
			url: 'send_contact.php',
			cache: false,
			data: $('#sendmessage').serialize(),
			success: function(html) {
				if(html == 'OK') {
					$('#sendmessage').clearForm();
					alert('Wir haben Ihre Nachricht erhalten\r\nVielen Dank!');
				}
			}
		});
	}
});

$(document).ready(function() { 
	$('#ref').live('change', function() { 
		if($(this).val() == 'HERBALIFE-Berater') {
			$('#berater').fadeIn('slow');
			$('#berater').addClass('required');
		} else {
			$('#berater').fadeOut('slow');
			$('#berater').removeClass('required');
		}
	});
});