﻿
function Comma(number) {
	number = '' + number;
	if (number.length > 3) {
		var mod = number.length % 3;
		var output = (mod > 0 ? (number.substring(0, mod)) : '');
		for (i = 0; i < Math.floor(number.length / 3); i++) {
			if ((mod == 0) && (i == 0)) {
				output += number.substring(mod + 3 * i, mod + 3 * i + 3);
			}
			else {
				output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
			}
		}
		return (output);
	}
	else return number;
}


function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g, '');
	if (isNaN(num)) {
		num = "0";
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10) {
		cents = "0" + cents;
	}
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
		num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
	}
	return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}


function isNumbersOnly(strValue) {
	var checkOK = "0123456789";
	var checkStr = strValue;
	var allValid = true;
	var allNum = "";
	for (i = 0; i < checkStr.length; i++) {
		ch = checkStr.charAt(i);
		for (j = 0; j < checkOK.length; j++) {
			if (ch == checkOK.charAt(j))
				break;
		}
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
		if (ch != ",") {
			allNum += ch;
		}
	}
	if (!allValid) {
		return (false);
	}
}


function validateCalculator() {

	var sendSaleForm = 0;

	if (document.getElementById("name").value == "") {
		document.getElementById("name").focus();
		alert("Please enter your name");
		sendSaleForm = 1;
		return (false);
	}

	if (document.getElementById("your_company").value == "") {
		document.getElementById("your_company").focus();
		alert("Please enter your dealership name");
		sendSaleForm = 1;
		return (false);
	}

	if (document.getElementById("phone").value == "") {
		document.getElementById("phone").focus();
		alert("Please enter your phone number");
		sendSaleForm = 1;
		return (false);
	}

	if (document.getElementById("email").value == "") {
		document.getElementById("email").focus();
		alert("Please enter your email address");
		sendSaleForm = 1;
		return (false);
	}

	if (document.getElementById("webVisits").value == "") {
		document.getElementById("webVisits").focus();
		alert("Please enter your monthly website visits");
		sendSaleForm = 1;
		return (false);
	}

	webVistorsValid = isNumbersOnly(document.getElementById("webVisits").value);

	if (webVistorsValid == false) {
		document.getElementById("webVisits").focus();
		alert("Please enter a number for monthly website visits");
		sendSaleForm = 1;
		return (false);
	}

	if (document.getElementById("visitsToLeads").value == "") {
		document.getElementById("visitsToLeads").focus();
		alert("Please enter your monthly website leads");
		sendSaleForm = 1;
		return (false);
	}

	webLeadsValid = isNumbersOnly(document.getElementById("visitsToLeads").value);

	if (webLeadsValid == false) {
		document.getElementById("visitsToLeads").focus();
		alert("Please enter a number for monthly website leads");
		sendSaleForm = 1;
		return (false);
	}


	if (sendSaleForm == 0) {

		// Send Sales Form
		var nameSplit = document.getElementById("name").value;
		var nameSplitResult = nameSplit.split(" ");

		window.SaleFrame.document.getElementById('first_name').value = nameSplitResult[0];

		if (nameSplitResult[1] == null) {
			window.SaleFrame.document.getElementById("last_name").value = "None Given";
		}
		else {
			window.SaleFrame.document.getElementById("last_name").value = nameSplitResult[1];
		}

		window.SaleFrame.document.getElementById("company").value = document.getElementById("your_company").value;
		window.SaleFrame.document.getElementById("phone").value = document.getElementById("phone").value;
		window.SaleFrame.document.getElementById("email").value = document.getElementById("email").value;
		window.SaleFrame.document.getElementById("lead_source").value = "AE Website";
		window.SaleFrame.document.getElementById("00N40000001uGrO").value = "Calculator";
		window.frames['SaleFrame'].document.forms[0].submit();


		// Process Calculator
		dealerShipName = document.getElementById("your_company").value;
		visitsPerMonth = document.getElementById("webVisits").value;
		visitLeads = document.getElementById("visitsToLeads").value;

		document.getElementById("CalculatorForm").style.display = "none";
		document.getElementById("CalculatorResults").style.display = "block";

		processCalculator(dealerShipName, visitsPerMonth, visitLeads, false);
		
	}
}


function processCalculator(varDealershipName, varWebVisits, varWebVisitLeads, boolIsPrintPage) {

	// Display Calculator Results
	

	calculations = performCalculations(varWebVisits, varWebVisitLeads);

	if (boolIsPrintPage == false) {
		// Fill in Results Table
		document.getElementById("ResultsDealerShipName").innerHTML = varDealershipName;
		document.getElementById("ResultsAnonVisitorCount").innerHTML = calculations.anonVisits;
		document.getElementById("ResultsPotLeadCount").innerHTML = calculations.potLiveChats;
		document.getElementById("ResultsEstSalesCount").innerHTML = calculations.potProfit;
		document.getElementById("ResultsBtnRow").innerHTML = "<button type='submit' class='ResultsPrintBtn' id='printBtn' onclick=\"processCalculator('" + varDealershipName + "','" + varWebVisits + "','" + varWebVisitLeads + "','true');\"" + "></button>";
	}
	else {
		window.open("inc/LeadCalculator/PrintResults.html?dealership=" + varDealershipName + "&webVisits=" + varWebVisits + "&webVisitsToLeads=" + varWebVisitLeads);

	}
}

function performCalculations(varWebVisits, varWebVisitLeads) {

	// Perform Calculations
	anonVisits = varWebVisits - varWebVisitLeads;

	potLiveChats = anonVisits * .03;
	potLiveChats = Math.round(potLiveChats);

	potChatsToLeads = potLiveChats * .84;
	potChatsToLeads = Math.round(potChatsToLeads);

	potLeadsToSales = potChatsToLeads * .11;
	potLeadsToSales = Math.round(potLeadsToSales);

	potProfit = potLeadsToSales * 1800;

	// Format Calculations
	anonVisits = Comma(anonVisits);
	potLiveChats = Comma(potLiveChats);
	potChatsToLeads = Comma(potChatsToLeads);
	potLeadsToSales = Comma(potLeadsToSales);
	potProfit = formatCurrency(potProfit);

	return { anonVisits: anonVisits, potLiveChats: potLiveChats, potChatsToLeads: potChatsToLeads, potLeadsToSales: potLeadsToSales, potProfit: potProfit };
	
}


function printResults(varDealershipName, varWebVisits, varWebVisitLeads) {

	calculations = performCalculations(varWebVisits, varWebVisitLeads);

	document.getElementById("PrintDealershipName").innerHTML = unescape(varDealershipName);
	document.getElementById("printTotalVisits").innerHTML = varWebVisits;
	document.getElementById("printVisitsToLeads").innerHTML = varWebVisitLeads;
	document.getElementById("printAnonVisits").innerHTML = calculations.anonVisits;
	document.getElementById("printAnonLeadsToChats").innerHTML = calculations.potLiveChats;
	document.getElementById("printPotChatToLeads").innerHTML = calculations.potChatsToLeads;
	document.getElementById("printLeadsToSales").innerHTML = calculations.potLeadsToSales;
	document.getElementById("printEstIncome").innerHTML = calculations.potProfit;
}