submitForm = function (event) {

    // Stop form from submitting
    event.preventDefault();
	
	$('#keyresult').html('<p><img src="/media/ajax-loader.gif" width="220" height="19" /></p>');
	

    $.ajax({
        type: "POST",
        url: "http://www.soapmedia.co.uk/roi.php",
        data: { "adspend": $("#adspend").val(), "cpc": $("#cpc").val(), "conversion": $("#conversion").val(), "rps": $("#rps").val(), "pps": $("#pps").val(),"manfee": $("#manfee").val(), "customers": $("#customers").val() },
        dataType: 'text',
        success: function(data, textStatus, XMLHttpRequest){
           // alert(data['error']);
			data = eval('('+data+')');

			if(data['error'] == "yes") {
				//move to top
				//window.scrollTo(0,0);
				location.href= "#keyresult";
				//spit out errors
				$('#keyresult').html(data['errors']);
			} else {
			
				$('#numclicks').html(data['numclick']);
				$('#numsales').html(data['numsales']);
				$('#totalrevenue').html(data['totalrevenue']);
				$('#totalprofit').html(data['totalprofit']);

				$('#roi').html(data['roi']);
				$('#poi').html(data['profit']);
				$('#cps').html(data['costpersale']);

				$('#adspend_result').html(data['adspend']);
				$('#managementfee').html(data['management_fee']);
				$('#costofsales').html(data['costofsales']);
				$('#avglv').html(data['avglv']);

			}

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.error('Error', XMLHttpRequest, textStatus, errorThrown);
        }
    });

}

// Connect submitForm function to the actual form.
$(document).ready(function(){
    $("#form1").submit(submitForm);
});
