﻿// _includes/activate-sales-forms.js
  // this function is included in incMetrics.asp - it activates the forms, there is a separate script that runs on the actual pages that get loaded into the modal ajax boxes

function initializeForm(formID, contentID, frameHTML, btnText, w, h) {
  
  //console.log("initializing the form...");

  // are we on production? if so -- do ssl.
  var pageURL = location.href;
  var prodSvr = pageURL.lastIndexOf("www.fpweb.net");
  var fpHost = (("-1" == prodSvr) ? "/" : "/");
  var product = '';
  /* if btnText is obviously about one product or another, automatically select for the user */
  if(btnText.lastIndexOf("2010 Server") != '-1') {
    product = 'radioServer';
  }
  if(btnText.lastIndexOf("2010 Foundation") != '-1') {
    product = 'radioFoundation';
  }
  
  var frameDir = "contact/sales/";
  var frameSrc = fpHost + frameDir + frameHTML + "?btnText=" + btnText + "&product=" + product; // format the url based on the type of form and button text selected.
  var frameWrap = "<iframe src='" + frameSrc + "' frameborder='0' width='" + w + "' height='" + h + "' scrolling='no' allowtransparency='true'></iframe>";

  $(contentID).html(frameWrap); // place the iframe code in the content wrapper

  $(formID).center({ vertical: false });
  $(overlayDiv).customShow("fast", function () { $(formID).customFadeIn(300, function () { }); });

  return false;

}

function trackModalFormSubmit(thisForm) {

  //console.log("trackModalFormSubmit(" + thisForm + ")");
  GAcustomVar(4, 'lead', 'submitted_form_success', 2);


  switch (thisForm) {
    // custom quote form (popup)
    case "CustomizeSuccessPanel":
      //GAtrack('/incCustomizeForm.asp?action=SUBMIT');
      GAevent('lead generation', 'submit-form', 'type=custom-quote');
      break;
    // basic contact form (popup)
    case "ContactUsSuccessPanel":
      //GAtrack('/incContactUs.asp?action=SUBMIT');
      GAevent('lead generation', 'submit-form', 'type=basic-contact');
      break;
    // sales email form (popup) 
    case "EmailSuccessPanel":
      //GAtrack('/incSalesEmail.asp?action=SUBMIT');
      GAevent('lead generation', 'submit-form', 'type=sales-email');
      break;
    // reseller form
    case "ResellerSuccessPanel":
      //GAtrack('/reseller/apply/?action=SUBMIT');
      GAevent('lead generation', 'submit-form', 'type=reseller-application');
      break;
    default:
      //GAtrack('/incUnknownForm.asp?action=SUBMIT');
      var type = "type=#" + thisForm;
      GAevent('lead generation error', 'unknown-form', type);


  } // END switch(whatForm)

} // END trackModalFormSubmit()

function bindModalFormClose(formStatus) {

  // make the previou binding die so we can ensure no previous global vars are effecting current bind
  $(".containerPlus:visible .buttonBar > a.close").die("click");

  var evType;
  var evConverted = 'false';

  switch (formStatus)
  {
    case 'initial':
      // the user has not submitted the form yet (failed conversion
      evConverted = 'false';
      break;
    case 'complete':
      evConverted = 'true';
      break;
    default:
      // TODO: log a hoptoad error here... something went wrong.
  }

  //console.log("bindModalFormClose(" + formStatus + ", " + evConverted + ")");
  
  // bind to the click of the close button
  $(".containerPlus:visible .buttonBar > a.close").live("click", function(event) {
    // if its a right click, show the context menu (required b/c of a firefox bug related to .live)
    if(event.which == 3) { return true; }
    // track form abandonment with Google Analytics.
    var whichForm = $(this).attr("id");

    // track form specific events

    switch(whichForm) {
      case 'customClose':
        evType = 'custom-quote';
        break;
      case 'leadClose':
        evType = 'custom-quote';
        break;
      case 'emailClose':
        evType = 'sales-email';
        break;
      default:
        evType = 'basic-contact';

    }

    if(evConverted == 'false') {
      // user did not convert, they are exiting before completion.
      GAcustomVar(4, 'lead', 'exited_before_submitting', 2);
      GAevent('lead generation', 'exit-form', 'type=' + evType);
    } else {
      if(evConverted == 'true') {
        // the lead converted, but the fact that it was successful is tracked in trackModalFormSubmit()
      } else {
        //TODO: log a hoptoad error here, something went wrong.
      }
    }
    

    // no matter what, when someone clicks the close button, close the form!
    $(this).closest(".containerPlus").customFadeOut(300, function() { $(".overlay").hide(); });
    return false;
  });

} // END bindModalFormClose(formStatus)

$(function() {
  overlayDiv = "#popupFormOverlay";
  // global containres available on every page
  var containers = "#basicSalesLead, #basicContactForm, #customize, #emailContactForm";

  // bind the form's close button to specific actions.
  bindModalFormClose('initial');

  // ACTIVATION OF SPECIFIC FORMS
  activateForms("#contentContainer");
});

function launchModalForm(cEv, cType, sRef, bTxt, bLoc) {

  //console.log(cEv);
  //return true;
  if(cEv != 'siteRef') {
    if(cEv.which == 3) { return true; }
  } 

//console.log("launchModalForm('siteRef', 'Quote', " + sRef + ", " + bTxt + ", " + bLoc + ")");

  // global default iframe dimensions (can specify local sizes per .bind function)
  frameWidth = "580";
  frameHeight = "600";

  var whatForm, whatContent, frameHTML, GAeventLabel;
  var cType_l = cType.toLowerCase();
  

  switch(cType) {

    case 'Quote':

      //GAtrack('/incCustomizeForm.asp?action=OPEN');
      GAeventLabel = 'type=custom-quote_&_btnText=' + bTxt;
      whatForm = "#customize";
      frameHTML = "quote.asp";

      break;

    case 'Email':

      //GAtrack('/incSalesEmail.asp?action=OPEN');
      GAeventLabel = 'type=sales-email_&_btnText=' + bTxt;
      whatForm = "#emailContactForm";
      frameHTML = "email-us.asp";

    break;
    
    default:

      //GAtrack('/incCustomizeForm.asp?action=OPEN');
      GAeventLabel = 'type=custom-quote_&_btnText=' + bTxt;
      whatForm = "#customize";
      frameHTML = "quote.asp";

      // TODO: log a hoptoad error... default reached

  } // END switch(cType)


  whatContent = whatForm + "-content";

  if(sRef != '') {
    bLoc = sRef + "_" + bLoc;
  }

  if(bLoc != '') {
    GAeventLabel = GAeventLabel + '_&_btnLoc=' + bLoc;
  }
  
  // track this user throughout the session so we'll know that they interacted with a lead-gen form
  GAcustomVar(4, 'lead', 'opened_the_form', 2);
  // TRIGGER GA EVENT
  GAevent( 'lead generation', 'open-form', GAeventLabel );

  initializeForm(whatForm, whatContent, frameHTML, bTxt, frameWidth, frameHeight);
  return false;


} // END launchModalForm(cEv, cThis)

function activateForms(where) {

  // unique URL QueryString can be used to trigger the form (useful when linking from the blog pages, etc...)
  var modalAction = $.getUrlVar('modalAction');
  var isModalActionNeeded = 'no';
  //console.log("quotePopupNeeded: " + isModalActionNeeded);
  if(modalAction) {
    isModalActionNeeded = 'yes';
  }

  if(isModalActionNeeded == 'yes') {

    var siteRef = $.getUrlVar('site');
      if(!siteRef){siteRef = ''; }
      var btnText = $.getUrlVar('btnText');
      if(!btnText){btnText = '';}
      var btnLoc = $.getUrlVar('btnLoc');
      if(!btnLoc){btnLoc = '';}

    switch(modalAction) {
      case 'quote':

        launchModalForm('siteRef', 'Quote', siteRef, btnText, btnLoc);

        break;
      case 'email':
        
        launchModalForm('siteRef', 'Email', siteRef, btnText, btnLoc);

        break;
      case 'chat':

        $("#salesChat").trigger("click");

        break;
      default:
      // TODO: log hoptoad error here... default action necessary
    } // END switch(modalAction)
  }

  // FREE QUOTE, CONSULTATION, CUSTOMIZE
  $(where).find("a.consult,a.custom,.salesQuote,a.quote").live("click", function(event) {
    event.preventDefault();
    btnLoc = '';
    var btnClass = $(this).attr('class');
    if(btnClass.lastIndexOf('mast') != -1) { btnLoc = 'mast'; }

    launchModalForm(event, 'Quote', '', $(this).text(), btnLoc);
  });

  // BASIC CONTACT
  $(where).find("a.contact,.salesEmail").live("click", function(event) {
    event.preventDefault();
    btnLoc = '';
    var btnClass = $(this).attr('class');
    if(btnClass.lastIndexOf('mast') != -1) { btnLoc = 'mast'; }

    launchModalForm(event, 'Quote', '', $(this).text(), btnLoc);
  });

} // END activateForms


