$(document).ready(function()
{
	
	$('#cmd_send').click(function()
	{
		$.ajax(
        {
            type: "POST",
            data: "subject=" + $('#subject').val() + "&dummy=" +  $('#email').val() + "&name=" +  $('#polaris2').val() + "&mail=" +  $('#polaris1').val() + "&telefon=" +  $('#polaris3').val() + "&nachricht=" +  $('#polaris4').val(),
            url: "ajax-angebote.php",
            success: function(result)
            {
                if (result.substr(0, 2) == 'OK')
                {
                    result = result.substr(3);
                    if (result) // Fehler
                    {
                    	result = result.split("|:|");
                    	var fields = result[0].split('|');
                    	for (i=0; i<fields.length; i++)
                    	{
                    		$('#' + fields[i]).addClass('error');
						}
						alert("Bitte korrigieren Sie folgende Fehler und senden das Formular erneut ab:\n\n" + result[1] + " ");
					}
					else
					{
						$('.angebot-more').html('<p class="erfolg">Vielen Dank! Ihre Anfrage wurde erfolgreich übermittelt.<br>Wir werden uns schnellstmöglich bei Ihnen melden.</p>');
					}
                }
                else
                {
                    alert(result);
                }
            }
        });
        
	});
	
	
	$('#cmd_reset').click(function()
	{
		$('#polaris1').val('Ihre E-Mail Adresse');
		$('#polaris1').removeClass('error');
		$('#polaris1').addClass('label');
		$('#polaris2').val('Ihr Name');
		$('#polaris2').removeClass('error');
		$('#polaris2').addClass('label');
		$('#polaris3').val('Ihre Durchwahl');
		$('#polaris3').removeClass('error');
		$('#polaris3').addClass('label');
		$('#polaris4').val('Ihr Anliegen');
		$('#polaris4').removeClass('error');
		$('#polaris4').addClass('label');
	});
	
	
	$('#polaris1').focus(function()
	{
		$('#polaris1').removeClass('error');
		if ($('#polaris1').val() == 'Ihre E-Mail Adresse')
		{ 
			$('#polaris1').val('');
			$('#polaris1').removeClass('label');
		}
	});
	
	$('#polaris1').blur(function()
	{
		if ($('#polaris1').val() == '')
		{ 
			$('#polaris1').val('Ihre E-Mail Adresse');
			$('#polaris1').addClass('label');
		}
	});
	
	
	$('#polaris2').focus(function()
	{
		$('#polaris2').removeClass('error');
		if ($('#polaris2').val() == 'Ihr Name')
		{ 
			$('#polaris2').val('');
			$('#polaris2').removeClass('label');
		}
	});
	
	$('#polaris2').blur(function()
	{
		if ($('#polaris2').val() == '')
		{ 
			$('#polaris2').val('Ihr Name');
			$('#polaris2').addClass('label');
		}
	});
	
	
	$('#polaris3').focus(function()
	{
		$('#polaris3').removeClass('error');
		if ($('#polaris3').val() == 'Ihre Durchwahl')
		{ 
			$('#polaris3').val('');
			$('#polaris3').removeClass('label');
		}
	});
	
	$('#polaris3').blur(function()
	{
		if ($('#polaris3').val() == '')
		{ 
			$('#polaris3').val('Ihre Durchwahl');
			$('#polaris3').addClass('label');
		}
	});
	
	
	$('#polaris4').focus(function()
	{
		$('#polaris4').removeClass('error');
		if ($('#polaris4').val() == 'Ihr Anliegen')
		{ 
			$('#polaris4').val('');
			$('#polaris4').removeClass('label');
		}
	});
	
	$('#polaris4').blur(function()
	{
		if ($('#polaris4').val() == '')
		{ 
			$('#polaris4').val('Ihr Anliegen');
			$('#polaris4').addClass('label');
		}
	});
	
});



