﻿//
// _uGC from the old urchin.js
//

function _uGC(l, n, s) {
    if (!l || l == "" || !n || n == "" || !s || s == "") return "-";
    var i, i2, i3, c = "-";
    i = l.indexOf(n);
    i3 = n.indexOf("=") + 1;
    if (i > -1) {
        i2 = l.indexOf(s, i); if (i2 < 0) { i2 = l.length; }
        c = l.substring((i + i3), i2);
    }
    return c;
}


// 
// Get the __utmz cookie value. This is the cookies that 
// stores all campaign information. 
// 
var z = _uGC(document.cookie, '__utmz=', ';'); 
// 
// The cookie has a number of name-value pairs. 
// Each identifies an aspect of the campaign. 
// 
// utmcsr  = campaign source
// utmcmd  = campaign medium 
// utmctr  = campaign term (keyword) 
// utmcct  = campaign content (used for A/B testing) 
// utmccn  = campaign name 
// utmgclid = unique identifier used when AdWords auto tagging is enabled 
// 
// This is very basic code. It separates the campaign-tracking cookie 
// and populates a variable with each piece of campaign info. 
// 
var source  = _uGC(z, 'utmcsr=', '|'); 
var medium  = _uGC(z, 'utmcmd=', '|'); 
var term    = _uGC(z, 'utmctr=', '|'); 
var content = _uGC(z, 'utmcct=', '|'); 
var campaign = _uGC(z, 'utmccn=', '|'); 
var gclid   = _uGC(z, 'utmgclid=', '|');

var f;

// Read the "salesforce campaing id"

var utmcid = _uGC(z, 'utmcid=', '|');

function run() {
    f = document.forms[0];

    if (utmcid == '-') {
        utmcid = f.Campaign_ID.value;
    }
    // must check if field Campaign_ID wil be overwritten

    // 
    // The gclid is ONLY present when auto tagging has been enabled. 
    // All other variables, except the term variable, will be '(not set)'. 
    // Because the gclid is only present for Google AdWords we can 
    // populate some other variables that would normally 
    // be left blank. 
    // 
    if (gclid != "-") {
        source = 'google';
        medium = 'cpc';
    }
    // Data from the custom segmentation cookie can also be passed 
    // back to your server via a hidden form field 
    var csegment = _uGC(document.cookie, '__utmv=', ';');
    if (csegment != '-') {
        var csegmentex = /[1-9]*?\.(.*)/;
        csegment = csegment.match(csegmentex);
        csegment = csegment[1];
    } else {
        csegment = '';
    }
}

function populateHiddenFields(f) { 
      f['00N200000019gWa'].value  = source;
      f['00N200000019gWV'].value  = medium;
      f['00N20000001ABjS'].value = term;
      f['00N200000019gW6'].value = content;
      f['00N200000019gWL'].value = campaign;
      f.Campaign_ID.value = utmcid;
      // f.segment.value = csegment;
      
      // Check if first name is entered
      if (document.getElementById('first_name').value == '') {
          alert(pp_message('direct_login_alert'));
          document.getElementById('first_name').focus();
          return false;
      }
      
      // Check if last name is entered
      if (document.getElementById('last_name').value == '') {
          alert(pp_message('direct_login_alert'));
          document.getElementById('last_name').focus();
          return false;
      }      
      
      // Check if email is entered
      if (document.getElementById('email').value == '') {
          alert(pp_message('direct_login_alert'));
          document.getElementById('email').focus();
          return false;
      }
      
      // Check if email is in the correct format
      if (!addCheck(document.getElementById('email').value)) {
          alert(pp_message('direct_login_alert_email_not_valid'));
          return false;
      }

      // Check if company is entered
      if (document.getElementById('company').value == '') {
          alert(pp_message('direct_login_alert'));
          document.getElementById('company').focus();
          return false;
      }
      
      // Check if phone is entered
      if (document.getElementById('phone').value == '') {
          alert(pp_message('direct_login_alert'));
          document.getElementById('phone').focus();
          return false;
      }
      
      //_gaq.push(['_linkByPost', document.getElementById('livedemoform')]);
      _gaq.push(['_trackPageview', 'onlinedemo_ok.html']);
      document.livedemoform.submit()
      return true; 
}

// utmcsr  = campaign source
// media source = 00N200000019gWa
//
// utmcmd  = campaign medium
// media channel = 00N200000019gWV
//
// utmctr  = campaign term (keyword)
// search frace = 00N20000001ABjS
//
// utmcct  = campaign content (used for A/B testing) 
// ad variation = 00N200000019gW6
//
// utmccn  = campaign name
// google campaign = 00N200000019gWL 
//
// utmgclid = unique identifier used when AdWords auto tagging is enabled
// campain id 

function pp_message(message)
{ //returns a message in the correct language. Error messages are specified directly in each signup file.
  if (!window.pp_messages)
    return '';
  var ms = pp_messages[message]
  if (!ms)
    alert('No message with key "' + message + '" !!!')
  return ms;
}

function setAffiliateTrackingTag() {
    var orderid = Math.round((new Date().getTime() * Math.random()));
    document.write('<img src="https://www.emjcd.com/u?AMOUNT=0&CID=1517156&OID=' + orderid + '&TYPE=335438&METHOD=IMG" width="1" height="20" />');

    //alert('in setAffiliateTrackingTag');

}


function notEmptyCheck(value) {
    if (value == '') alert(pp_message('direct_login_alert'))
    return (value != '')
}

function addCheck(value) {
    var re = /^[a-zA-Z0-9\-\.\_\']+\@[a-zA-Z0-9\-\.\_\']+\.[a-zA-Z]{2,}$/

    var m = re.exec(value);
    if (!m || value != m[0]) {
        //alert(pp_message('direct_login_alert_email_not_valid')) // A message
        return false
    }
    return true
}
