jQuery(document).ready(function() {

	// animated contact form
	jQuery('#contact_button').bind('click', toggleContactForm);

	jQuery('#contact_button').bind('keydown', function() {
		
		if(jQuery('#contact_form').css("display") == "none")
		{
			jQuery("#contact_form").show('slide', {direction: 'right'}, 'slow');
		}		
		else
		{
			jQuery("#contact_form").hide('slide', {direction: 'right'}, 'slow');
		}
	});
	
	// end animated contact form
	
	
	// styled form button
	//jQuery('#contact_form_submit').css("border", "none");
	//jQuery('#contact_form_submit').css("cursor", "pointer");
	//jQuery('#contact_form_submit').css("background", "transparent url(fileadmin/templates/lsb/media/images/buttons/contact_form_send.png) no-repeat scroll 0px 0px");
	jQuery('#contact_form_submit').val("");
	
	if (window.location.host == 'www.lsb-nrw.de')
	{
	
		jQuery('#contact_form_submit').bind('click', function() {
			if(validateForm()) {
				jQuery('html').css("cursor","wait");
				jQuery.ajax({
					type: 'POST',
					//url: 'index.php?id=kontakt',
					url: 'http://www.lsb-nrw.de/lsb-nrw/service-navigation/kontakt0/',
					data: jQuery("#form-contact").serialize(),
					cache: false,
					success: function(data) {
						jQuery('#form-contact').hide();
						jQuery('#contact-result').show();
						jQuery('#contact-result-button').bind('click', function() {
							toggleContactForm();
							jQuery('#form-contact').show();
							jQuery('#contact-result').hide();
						});
						
						jQuery.ajax({
							type: 'GET',
							url: 'index.php',
							data: {id: 'kontaktantwort',type: '5001'},
							dataType: "html",
						    async:false,
							success: function(htmldata) {
								jQuery('#contact-result-text').html(htmldata);
								jQuery('html').css("cursor","default");
							}
						});
						
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						jQuery('#form-contact').hide();
						jQuery('#contact-result').show();
						jQuery('#contact-result-button').bind('click', function() {
							toggleContactForm();
							jQuery('#form-contact').show();
							jQuery('#contact-result').hide();
						});
						jQuery('#contact-result-text').html("Beim versenden der Kontaktanfrage ist ein Fehler aufgetreten.");
						jQuery('html').css("cursor","default");
					}
				});
			}
			return false;
		});
	}
	else if (window.location.host == 'www.sportjugend-nrw.de') 
	{
		jQuery('#contact_form_submit').bind('click', function() {
			if(validateForm()) {
				jQuery('html').css("cursor","wait");
				jQuery.ajax({
					type: 'POST',
					//url: 'index.php?id=kontakt',
					url: 'http://www.sportjugend-nrw.de/index.php?id=4497&MP=4497-5445',
					data: jQuery("#form-contact").serialize(),
					cache: false,
					success: function(data) {
						jQuery('#form-contact').hide();
						jQuery('#contact-result').show();
						jQuery('#contact-result-button').bind('click', function() {
							toggleContactForm();
							jQuery('#form-contact').show();
							jQuery('#contact-result').hide();
						});
						
						jQuery.ajax({
							type: 'GET',
							url: 'index.php',
							data: {id: 'kontaktantwort',type: '5001'},
							dataType: "html",
						    async:false,
							success: function(htmldata) {
								jQuery('#contact-result-text').html(htmldata);
								jQuery('html').css("cursor","default");
							}
						});
						
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						jQuery('#form-contact').hide();
						jQuery('#contact-result').show();
						jQuery('#contact-result-button').bind('click', function() {
							toggleContactForm();
							jQuery('#form-contact').show();
							jQuery('#contact-result').hide();
						});
						jQuery('#contact-result-text').html("Beim versenden der Kontaktanfrage ist ein Fehler aufgetreten.");
						jQuery('html').css("cursor","default");
					}
				});
			}
			return false;
		});
	}
	
	
});

function toggleContactForm() {
	if(jQuery('#contact_form').css("display") == "none")
	{
		
		jQuery("#contact_form").show('slide', {direction: 'right'}, 0);
		// styled dropdown boxes
		jQuery('#request_select').msDropDown({showIcon:false});
		jQuery('#salutation_select').msDropDown({showIcon:false});
		// styled checkbox
		jQuery('#newsletter').checkbox();
	}		
	else
	{
		jQuery("#contact_form").hide('slide', {direction: 'right'}, 0);
	}
	return false;
}

function validateForm() {
	var error = false;
	var question = false;
	
	var input_fields = new Array('#contact_person', '#contact_street', '#contact_house_number', '#contact_zip', '#contact_city', '#contact_telephone', '#contact_email', '#question');
	
	jQuery.each(input_fields,function(i,value) {
		if(jQuery(value).val() == "")
		{
			jQuery(value).attr("style", "border: solid 0.2em red !important");
			if(value == "#question")
				question = true
			else
				error = true;
		}
		else
		{
			jQuery(value).attr("style", "border: none");
		}
	});
	
	
	if(jQuery("#request_select").val() == "0")
	{
		error = true;
		jQuery("#request_select_msdd .ddTitle").attr("style", "border: solid 0.2em red !important");		
	}
	else
	{
		jQuery("#request_select_msdd .ddTitle").attr("style", "none");	
	}
	
	if(jQuery("#salutation_select").val() == "0")
	{
		error = true;
		jQuery("#salutation_select_msdd .ddTitle").attr("style", "border: solid 0.2em red !important");		
	}
	else
	{
		jQuery("#salutation_select_msdd .ddTitle").attr("style", "border: none");	
	}

	
	if(error && question)
	{
		jQuery('#contact_error').html('Bitte f&uuml;llen Sie alle Pflichtfelder aus und geben Sie eine Anfrage ein.');
	}
	else if(error)
	{
		jQuery('#contact_error').html('Bitte f&uuml;llen Sie alle Pflichtfelder aus.');
	}
	else if(question)
	{
		jQuery('#contact_error').html('Bitte geben Sie eine Anfrage ein.');
	}
	else
	{
		jQuery('#contact_error').html('');
		return true;
	}
	return false;
}
