$(document).ready(function()
{
	//var url = '/kunder/byggpall.se/site_specific/modules/news_form/buysell_form_post.php';
  var url = '/site_specific/modules/news_form/buysell_form_post.php';

	$('#buySellFormSubmit').click(function()
	{
		
		$('#buySellForm').submit();
	});

	$('#buySellForm').submit(function()
	{
	  $('#buySellFormIndicator').show();
	
		var data = {
			subject: $('#subject').val(),
			namn: $('#namn').val(),
			foretag: $('#foretag').val(),
			adress: $('#adress').val(),
			ort: $('#ort').val(), 
			postnummer: $('#postnummer').val(),
			telefon: $('#telefon').val(),   
			epost: $('#epost').val(), 
			information: $('#information').val(),
			conf: $('#conf').val() 
		};
    sendNewsForm(url, data);
		return false;

	});
});

function sendNewsForm(url, data)
{  

	$.ajax({
		type: "POST",
		url: url,
		dataType: 'json',
		data: data,
		success: function(response) 
		{
			$('*').removeClass("errorField");
			$('*').removeClass("buySellFormError");
			$('*').removeClass("buySellFormSuccess"); 
      $('#buySellFormIndicator').hide();     			

			if(response.validation == 'failed')
			{
				$('#buySellFormMessage').addClass("buySellFormError");
				$('#buySellFormMessage').html(response.message).show('fast');

				
				$.each(response.error_fields, function(index, value) 
				{       
					$("#" + value).addClass("errorField");
				});           
			 
         
			}
			else
			{ 
				   
				$('#buySellFormMessage').addClass("buySellFormSuccess");
				$('#buySellFormMessage').html(response.message).show('fast');

        // console.log(response); 
				// Success lets cleanit
				$.each(data, function(index, value) 
				{ 
					//console.log(value)
					$("#" + index).val('');
				});

				$('#buySellFormMessage').delay(2000).hide('slow');
				 
				/*
				$("#buySellForm").delay(5000, function(){
					parent.$.fn.colorbox.close();
				});
				*/
				
				
			}
		}
  });
}

