function format_number(total){
	total = total.toString();
	total = total.replace("$", "");
	total = total.replace(",", "");
	total = total.replace("x", "");
	total = total.replace("X", "");
	total = total.replace("%", "");
	var wd = "w"
	var tempnum = total.toString();
	for (i=0;i<tempnum.length;i++){
		if (tempnum.charAt(i) == "."){ wd = "d"; break; }
	}
	if (wd=="w"){
		total = tempnum + ".00";
		return comma_number(total);
	} else {
		if (tempnum.charAt(tempnum.length-1) == "."){
			total = tempnum + "00";
			return comma_number(total);
		}
		if (tempnum.charAt(tempnum.length-2) == "."){
			total = tempnum + "0";
			return comma_number(total);
		}
		if (tempnum.charAt(tempnum.length-3) == "."){
			return comma_number(total);
		} 
		tempnum = Math.round(tempnum*100)/100;
		total = format_number(tempnum);
		return total;
	}
}
function comma_number(total){
	total = total.toString();
	if (total.length > 6){ total = total.substring(0, total.length-6) + "," + total.substring(total.length-6); }
	if (total.length > 10){ total = total.substring(0, total.length-10) + "," + total.substring(total.length-10); }
	return total;
}
function checkform(){
	var missing = false;
	if (document.calculator['monthly_traffic'].value.length == 0){ missing = true; }
	if (document.calculator['conversion_rate'].value.length == 0){ missing = true; }
	if (document.calculator['new_conversion_rate'].value.length == 0){ missing = true; }
	if (document.calculator['average_sale'].value.length == 0){ missing = true; }
	if (document.calculator['seo_cost'].value.length == 0){ missing = true; }
	if (missing != false){
		alert("Sorry, you must complete all the fields."); return false;
	} else {
		calculate();
		showdiv('results');
	}
}
function clean_number(total){
	total = total.toString();
	total = total.replace("$", "");
	total = total.replace(",", "");
	total = total.replace("x", "");
	total = total.replace("X", "");
	total = total.replace("%", "");
	return total;
}
function calculate(){
	var monthly_traffic = document.calculator['monthly_traffic'].value;
	var conversion_rate = document.calculator['conversion_rate'].value;
	var new_conversion_rate = document.calculator['new_conversion_rate'].value;
	var average_sale = document.calculator['average_sale'].value;
	var seo_cost = document.calculator['seo_cost'].value;
	var sales_conversion = 0;
	
	var full_results = "Thank you for using the Metamend SEO-ROI calculator.  Below are your requested results.  If you have any further questions or require more information, please visit our contact page,\n\nhttp://www.metamend.com/contactus.html\n\nor call us toll free at 1.866.381.6382 and speak with one of our sales representatives.\n\n----\n\n";
	full_results = full_results + "Business 2 Consumer SEO-ROI Calculator Results\n\nhttp://www.metamend.com/seo-roi-b2c.html\n\n";
	
	document.getElementById('value_monthly_traffic').innerHTML = monthly_traffic;
	full_results += "Baseline Monthly Traffic - Average Visitors: " + (monthly_traffic) + "\n\n";
	
	document.getElementById('value_conversion_rate').innerHTML = conversion_rate;
	full_results += "Conversion Rate: " + (conversion_rate) + "\n\n";
	
	document.getElementById('value_new_conversion_rate').innerHTML = new_conversion_rate;
	full_results += "New Conversion Rate: " + (new_conversion_rate) + "\n\n";

	var value_new_clients = (clean_number(monthly_traffic) * clean_number(new_conversion_rate)) - (clean_number(monthly_traffic) * clean_number(conversion_rate));
	document.getElementById('value_new_clients').innerHTML = Math.ceil(value_new_clients / 100);
	full_results += "Monthly Net New Customers: " + format_number(value_new_clients) + "\n\n";	
	
	document.getElementById('value_avg_sale').innerHTML = average_sale;
	full_results += "Average Sale: " + (average_sale) + "\n\n";
	
	var value_avg_monthly_revenue = clean_number(average_sale) * value_new_clients / 100.00;
	document.getElementById('value_avg_monthly_revenue').innerHTML = '$' + format_number(value_avg_monthly_revenue);
	full_results += "Average Monthly Revenue: " + (value_avg_monthly_revenue) + "\n\n";
	
	var value_annual_new_clients = 12 * (value_new_clients / 100);
	document.getElementById('value_new_clients_year1').innerHTML = Math.ceil(value_annual_new_clients);
	full_results += "Annual Net New Clients: " + format_number(value_annual_new_clients) + "\n\n";	
	
	var value_annual_revenue = 12 * (value_avg_monthly_revenue);
	document.getElementById('value_new_revenue_year1').innerHTML = '$' + format_number(value_annual_revenue);
	full_results += "Annual Net New Revenue: " + format_number(value_annual_revenue) + "\n\n";	
	
	var value_aquisition = clean_number(seo_cost) / value_annual_new_clients;
	document.getElementById('value_acquisition_cost_year1').innerHTML = '$' + format_number(value_aquisition);
	full_results += "Customer Acquisition Cost: " + format_number(value_aquisition) + "\n\n";
	
	document.getElementById('value_seo_cost').innerHTML = seo_cost;
	full_results += "Outsourced SEO Annual Cost: " + (seo_cost) + "\n\n";
	
	document.emailform['full_results'].value = full_results;
}

function send_email(e){
	// check that all the info is present
	var missing = false;
	if (document.emailform['recipient'].value.length == 0){ missing = true; }
	if (missing == true){ alert("Sorry, please enter your email address!"); return false; }
	
	document.emailform['email_results'].value = "Please wait...";
	document.emailform['email_results'].disabled = true;
	
	var target = window.event ? window.event.srcElement : e ? e.target : null;
	if (target.ajaxInProgress){ return; }
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('POST', '/ajax-form2email.html', true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4){
			if (xmlhttp.status == 200){
				// returned ok
				show_email_results(xmlhttp.responseText, target);
			} else {
				// error
				target.ajaxInProgress = false;
				document.emailform['email_results'].value = "Email";
				document.emailform['email_results'].disabled = false;
				alert("Sorry, there was an error emailing your results.  Please try again.");
			}
		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
	xmlhttp.send('&recipient=' + document.emailform['recipient'].value + '&subject=Your SEO-ROI Results' + '&sender_name=Metamend' + '&sender_email=seo-roi@metamend.com' + '&bcc_sender=1' + '&message_body=' + document.emailform['full_results'].value);
	target.ajaxInProgress = true;
}

function show_email_results(responseText, target){
	target.ajaxInProgress = false;
	document.emailform['email_results'].value = "Email";
	document.emailform['email_results'].disabled = false;
	alert("Your results have been emailed!");
	//alert(responseText);
}