﻿
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);
	}
}

HubSpotFormSpamCheck_LeadGen_ContactForm_68803_m0 = function() {
    var key = document.getElementById('LeadGen_ContactForm_68803_m0spam_check_key').value;
    var sig = '';
    for (var x = 0; x < key.length; x++) {
        sig += key.charCodeAt(x) + 13;
    }
    document.getElementById('LeadGen_ContactForm_68803_m0spam_check_sig').value = sig;
    /* Set the hidden field to contain the user token */
    var results = document.cookie.match('(^|;) ?hubspotutk=([^;]*)(;|$)');
    if (results && results[2]) {
        document.getElementById('LeadGen_ContactForm_68803_m0submitter_user_token').value = results[2];
    } else if (window['hsut']) {
        document.getElementById('LeadGen_ContactForm_68803_m0submitter_user_token').value = window['hsut'];
    }
    validateCalculator();
    return true;
};


function validateCalculator(){
   
    /* Set the hidden field to contain the user token */

    var sendSaleForm = 0;

    var name = document.getElementById("LeadGen_ContactForm_68803_m0_FirstName").value;
    var company = document.getElementById("LeadGen_ContactForm_68803_m0_Company").value;

    if (document.getElementById("LeadGen_ContactForm_68803_m0_FirstName").value == "") {
        document.getElementById("LeadGen_ContactForm_68803_m0_FirstName").focus();
        alert("Please enter your name");
        sendSaleForm = 1;
        return (false);
    }

    if (document.getElementById("LeadGen_ContactForm_68803_m0_Company").value == "") {
        document.getElementById("LeadGen_ContactForm_68803_m0_Company").focus();
        alert("Please enter your dealership name");
        sendSaleForm = 1;
        return (false);
    }

    if (document.getElementById("LeadGen_ContactForm_68803_m0_Phone").value == "") {
        document.getElementById("LeadGen_ContactForm_68803_m0_Phone").focus();
        alert("Please enter your phone number");
        sendSaleForm = 1;
        return (false);
    }

    if (document.getElementById("LeadGen_ContactForm_68803_m0_Email").value == "") {
        document.getElementById("LeadGen_ContactForm_68803_m0_Email").focus();
        alert("Please enter your email address");
        sendSaleForm = 1;
        return (false);
    }

    if (document.getElementById("LeadGen_ContactForm_68803_m0_Quoted_Monthly__c").value == "") {
        document.getElementById("LeadGen_ContactForm_68803_m0_Quoted_Monthly__c").focus();
        alert("Please enter your monthly website visits");
        sendSaleForm = 1;
        return (false);
    }

    webVistorsValid = isNumbersOnly(document.getElementById("LeadGen_ContactForm_68803_m0_Quoted_Monthly__c").value);

    if (webVistorsValid == false) {
        document.getElementById("LeadGen_ContactForm_68803_m0_Quoted_Monthly__c").focus();
        alert("Please enter a number for monthly website visits");
        sendSaleForm = 1;
        return (false);
    }

    if (document.getElementById("LeadGen_ContactForm_68803_m0_of_Stores_in_Group__c").value == "") {
        document.getElementById("LeadGen_ContactForm_68803_m0_of_Stores_in_Group__c").focus();
        alert("Please enter your monthly website leads");
        sendSaleForm = 1;
        return (false);
    }

    webLeadsValid = isNumbersOnly(document.getElementById("LeadGen_ContactForm_68803_m0_of_Stores_in_Group__c").value);

    if (webLeadsValid == false) {
        document.getElementById("LeadGen_ContactForm_68803_m0_of_Stores_in_Group__c").focus();
        alert("Please enter a number for monthly website leads");
        sendSaleForm = 1;
        return (false);
    }

    if (sendSaleForm == 0) {

        // Send Sales Form
        var nameSplit = document.getElementById("LeadGen_ContactForm_68803_m0_FirstName").value;
        var nameSplitResult = nameSplit.split(" ");

        /*window.SaleFrame.document.getElementById("LeadGen_ContactForm_53921_m0_FirstName").value = nameSplitResult[0];

        if (nameSplitResult[1] == null) {
        window.SaleFrame.document.getElementById("LeadGen_ContactForm_53921_m0_LastName").value = "None Given";
        }
        else {
        window.SaleFrame.document.getElementById("LeadGen_ContactForm_53921_m0_LastName").value = nameSplitResult[1];
        }

        window.SaleFrame.document.getElementById("LeadGen_ContactForm_53921_m0_Company").value = document.getElementById("LeadGen_ContactForm_68803_m0_Company").value;
        window.SaleFrame.document.getElementById("LeadGen_ContactForm_53921_m0_Phone").value = document.getElementById("LeadGen_ContactForm_68803_m0_Phone").value;
        window.SaleFrame.document.getElementById("LeadGen_ContactForm_53921_m0_Email").value = document.getElementById("LeadGen_ContactForm_68803_m0_Email").value;
        window.SaleFrame.document.getElementById("LeadGen_ContactForm_53921_m0_AE_Site_Form__c").value = "LeadGen_ContactForm_68803_m0_Quoted_Monthly__c";
        window.SaleFrame.document.getElementById("00N40000001uGrO").value = "Calculator";
        window.frames['SaleFrame'].document.forms[0].submit();*/


        // Process Calculator
        dealerShipName = document.getElementById("LeadGen_ContactForm_68803_m0_Company").value;
        visitsPerMonth = document.getElementById("LeadGen_ContactForm_68803_m0_Quoted_Monthly__c").value;
        visitLeads = document.getElementById("LeadGen_ContactForm_68803_m0_Quoted_Monthly__c").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;
}
