
// Global to keep track of whether all the fields were set via cookie
var allFieldsSet = true;

///////////////////////////////////////////////////////////////////////////////
// FUNC: ValidateLeadForm
// DESC: Validate that the form is filled in correctly.
// ARGS: leadFrom - The form to validate
// RETURNS: true if the form is filled in correctly, otherwise false.
///////////////////////////////////////////////////////////////////////////////
function ValidateLeadForm(leadForm, returnPage) {
    var phone = leadForm.phAreaCode.value + leadForm.phPrefix.value + leadForm.phSuffix.value;
    leadForm.phone.value = '(' + leadForm.phAreaCode.value + ') ' + leadForm.phPrefix.value + '-' + leadForm.phSuffix.value;
    
    var why = "";
    
    var firstname = document.getElementById("firstname").value;
    var lastname = document.getElementById("lastname").value;
    var emailAddress = document.getElementById("email").value;
    var company = document.getElementById("company").value;
    var timing = document.getElementById("timing").value;
    
    why += checkFirstName(firstname);
    why += checkLastName(lastname);
    why += checkCompany(company);
    why += checkTitle(leadForm.title.value);
    why += checkPhone(phone);
    why += checkEmail(emailAddress);
    why += checkEmployees(leadForm.employees.value);

    if (why != "") {
       alert(why);
       return false;
    }
    
    // The form is valid so save it to a cookie
    SaveLeadForm(leadForm); 
    
	// Add the user's email address for Manticore tracking
	if (leadForm.email != null) {
		var queryString = location.search;
		// Change the “?” to a “&” so it will work after “mtcEmail”
		queryString = queryString.replace("?", "&");
		var returnURL = leadForm.retURL.value + "?mtcEmail=" + leadForm.email.value + queryString;
		leadForm.retURL.value = returnURL;                    
	}
	return true;
}

///////////////////////////////////////////////////////////////////////////////
// FUNC: InitializeLeadForm
// DESC: Initializes the lead form if existing data is found in the cookie.
// ARGS: leadForm - The form to populate
///////////////////////////////////////////////////////////////////////////////
function InitializeLeadForm(leadForm)
{
    allFieldsSet = true;
    
    var firstName = ReadCookie("LeadFormFirstName");
    var lastName = ReadCookie("LeadFormLastName");
    var phAreaCode = ReadCookie("LeadFormPhoneAreaCode");
    var phPrefix = ReadCookie("LeadFormPhonePrefix");
    var phSuffix = ReadCookie("LeadFormPhoneSuffix");
    var phExtension = ReadCookie("LeadFormPhoneExtension");
    var companyName = ReadCookie("LeadFormCompanyName");
    var email = ReadCookie("LeadFormEmail");
    var numberEmployees = ReadCookie("LeadFormNumberEmployees");
    var titleIndex = ReadCookie("LeadFormTitleIndex");
    var timingIndex = ReadCookie("LeadFormTimingIndex");
    var decisionCheck = ReadCookie("LeadFormDecisionCheck");
    var leadFormObject = document.getElementById("leadForm");

    _SetInput(leadFormObject.firstname, firstName);
    _SetInput(leadFormObject.lastname, lastName);
    _SetInput(leadFormObject.company, companyName);
    _SetInput(leadFormObject.email, email);   
    _SetInput(leadFormObject.phAreaCode, phAreaCode);
    _SetInput(leadFormObject.phPrefix, phPrefix);
    _SetInput(leadFormObject.phSuffix, phSuffix);
    _SetInput(leadFormObject.employees, numberEmployees);

    _SetDropDownInput(leadFormObject.title, titleIndex);
    _SetDropDownInput(leadFormObject.timing, timingIndex);

    _SetCheckBoxInput(leadFormObject.decision, decisionCheck);

    if (allFieldsSet)
    {
        _SetDownloadInstuctionToConfirm();
    }
    
    // Extension is optional
    try
    {
        var phExtension = ReadCookie("LeadFormPhoneExtension");
        _SetInput(leadFormObject.phExtension, phExtension);
    }
    catch (err)
    {
        ;
    }
} 

///////////////////////////////////////////////////////////////////////////////
// FUNC: SaveLeadForm
// DESC: Saves the lead form information to a cookie
// ARGS: leadForm - The form to save
///////////////////////////////////////////////////////////////////////////////
function SaveLeadForm(leadForm)
{
    _CreateCookie("LeadFormFirstName", leadForm.firstname.value);
    _CreateCookie("LeadFormLastName", leadForm.lastname.value);
    _CreateCookie("LeadFormPhoneAreaCode", leadForm.phAreaCode.value);
    _CreateCookie("LeadFormPhonePrefix", leadForm.phPrefix.value);
    _CreateCookie("LeadFormPhoneSuffix", leadForm.phSuffix.value);
    _CreateCookie("LeadFormPhoneExtension", leadForm.phExtension.value);
    _CreateCookie("LeadFormCompanyName", leadForm.company.value);   
    _CreateCookie("LeadFormEmail", leadForm.email.value);
    _CreateCookie("LeadFormNumberEmployees", leadForm.employees.value);
    
    _CreateCookie("LeadFormTitleIndex", leadForm.title.selectedIndex);
    _CreateCookie("LeadFormTimingIndex", leadForm.timing.selectedIndex);
     
    if (leadForm.decision.checked)
    {
        _CreateCookie("LeadFormDecisionCheck", "true");    
    }
    else
    {
        _CreateCookie("LeadFormDecisionCheck", "false");    
    }
 }

///////////////////////////////////////////////////////////////////////////////
// FUNC: _CreateCookie
// DESC: Creates a cookie with the specified name and value
// ARGS: cookieName - The name of the cookie
//       cookieValue - The value of the cookie
//		days (value) - The days cookie persists
// RETURNS: nothing
///////////////////////////////////////////////////////////////////////////////
function _CreateCookie(cookieName, cookieValue)
{
    //90 is the numbe
    CreateCookie(cookieName, cookieValue, 90);
}

///////////////////////////////////////////////////////////////////////////////
// FUNC: _SetDownloadInstuctionToConfirm
// DESC: Sets the text of the download instructions to the cookied version
// ARGS: None
// RETURNS: nothing
///////////////////////////////////////////////////////////////////////////////
function _SetDownloadInstuctionToConfirm()
{
    // Change the text
    var messageText = "To download the guide, confirm your information in the form at left.";

    if (location.href.indexOf('demo') > 0)
    {
        messageText = "To access the Demo, confirm your information in the form at left.";
    }
    
    var downloadInstruction = document.getElementById("downloadInstruction");   
    downloadInstruction.firstChild.nodeValue = messageText;
    
    // Change the button
    var buttonObject = document.getElementById("button");   
    buttonObject.setAttribute('src', 'images/confdownloadbutton.png');   
}

///////////////////////////////////////////////////////////////////////////////
// FUNC: _SetCheckBoxInput
// DESC: Set the specified HTML checkbox control to the specified value
// ARGS: input - The name of the HTML control
//       value - The value to set
// RETURNS: nothing
///////////////////////////////////////////////////////////////////////////////
function _SetCheckBoxInput(input, value)
{
    if (value == null)
    {
        allFieldsSet = false;
        return;
    }
    
    if (value == 'true')
    {
        input.checked = true;
        return;
    }
    
    if (value == 'false')
    {
        input.checked = false;
        return;
    }
    
    allFieldsSet = false;
    return;   
}

///////////////////////////////////////////////////////////////////////////////
// FUNC: _SetDropDownInput
// DESC: Set the specified HTML drop down control to the specified value
// ARGS: input - The name of the HTML control
//       value - The value to set
// RETURNS: nothing
///////////////////////////////////////////////////////////////////////////////
function _SetDropDownInput(input, value)
{
    if (value == null ||
        value == '')
    {
        allFieldsSet = false;
        return;
    }
    
    var optionName = input.id + 'Option' + value;
    var optionObject =  document.getElementById(optionName);
    optionObject.selected = true;
}

///////////////////////////////////////////////////////////////////////////////
// FUNC: _SetInput
// DESC: Set the specified HTML control to the specified value
// ARGS: input - The name of the HTML control
//       value - The value to set
// RETURNS: nothing
///////////////////////////////////////////////////////////////////////////////
function _SetInput(input, value)
{
    if (value == null ||
        value == '')
    {
        allFieldsSet = false;
        return;
    }
    
    input.value = value;
}

///////////////////////////////////////////////////////////////////////////////
// FUNC: OpenWindow
// DESC: Opens the specified URL in a new window
// ARGS: url - The URL to open
// RETURNS: nothing
///////////////////////////////////////////////////////////////////////////////
function OpenWindow(url) 
{	
    var displayWindow = open(url, "", "location=yes,scrollbars=yes,toolbar=yes,menubar=yes,resizable=yes,status=yes,directories=yes");
}

///////////////////////////////////////////////////////////////////////////////
// FUNC: OutputCommissionJunctionCode
// DESC: Outputs the Commision Junction tracking code. A random number is used
//       for the OID value. OID = Order ID. Since we don't have orders we can
//       just use a random number here.
// ARGS: none
// RETURNS: nothing
///////////////////////////////////////////////////////////////////////////////
function OutputCommissionJunctionCode() 
{
    var cjCode1 = '<img src="https://www.emjcd.com/u?AMOUNT=0&CID=1501149&OID=';
    var cjCode2 = '&TYPE=304574&CURRENCY=USD&METHOD=IMG" height="1" width="20" />';    
    var randomNumber = Math.floor(Math.random()*100000000);
    
    document.write(cjCode1 + randomNumber + cjCode2);
}
