// JavaScript Document


/* IMAGE ROLLOVERS */
var images = "/marketmax/epislon/graphics/org/";
function button_action(sender, path, action, ext)
{
    if (ext == null) ext = "jpg";
    sender.src = images + path + ((action == "over") ? "-rollover" : "") + "." + ext;
}



/* CLIENT DROP DOWN */

var clientDropEffect;
var clientDropClosedY = -247;
var clientDropOpenY = 0;
var clientDropContainerId = "clientDrop";
window.addEvent('domready', function()
{
    //setup client drop
    clientDropEffect = new Fx.Style(clientDropContainerId, 'top', {
        duration: 500, 
        transition: Fx.Transitions.Sine.easeInOut
    });
    
    
    $(clientDropContainerId).addEvent('mouseenter', clientDrop_mouseover);
    $(clientDropContainerId).addEvent('mouseleave', clientDrop_mouseout);
    
    
    
    // nav menu 
    
    
    // setup drop downs
    new DropdownMenu($('byServiceMenu'))
    
    // show on rollover
    var myTimer;
    $('servicesNav').addEvent('mouseenter', function()
    {
        myTimer = $clear(myTimer);
        $('mainNavDrop').setStyle('display', 'block');
        $('servicesNav').addClass('active');
        
    });
    $('servicesNav').addEvent('mouseleave', function()
    {
        myTimer = (function() { $('mainNavDrop').hide(); $('servicesNav').removeClass('active'); } ).delay(500);
    });
    
    // keep sublist items highlighted when mouse goes over to sublist
    
    $$('.outerLink').each(function(instance) {  
        instance.addEvent('mouseenter', function()
        {
            instance.addClass('active');
        });
        instance.addEvent('mouseleave', function()
        {
            instance.removeClass('active');
        });
    }); 
    
    
    $('servicesNav').addEvent('mouseenter', function()
    {
        
    });
    $('servicesNav').addEvent('mouseleave', function()
    {
        
    });
    
    
    
});

function clientDrop_mouseover()
{
    if ($(clientDropContainerId).getPosition().y == clientDropClosedY)
    {
        clientDropEffect.start(clientDropOpenY);
    }
}
function clientDrop_mouseout()
{
    if ($(clientDropContainerId).getPosition().y == clientDropOpenY)
    {
        clientDropEffect.start(clientDropClosedY);
    }

}


function input_focus(sender) { if (sender.value == sender.defaultValue) sender.value = ""; }
function input_blur(sender) { if (sender.value == "") sender.value = sender.defaultValue; }

/* services left nav */

function makeNav() {
  var urlParts = window.location.pathname.split('/');
  var level1 = urlParts[3] // industry or solutions
  var currentURL = '';
  for (var i = 2; i < urlParts.length; i++) { currentURL += '/'+urlParts[i]; }
  var leftNavNodes = {
        industry : {
          text : '',
          url : '/services/industry',
          children : {
            'cpg.html' : {
              text : 'Consumer Packaged Goods',
              url : '/services/industry/cpg.html',
              children : false
            },
            'financial.html' : {
              text : 'Financial',
              url : '/services/industry/financial.html',
              children : false
            },
            'insurance.html' : {
              text : 'Insurance',
              url : '/services/industry/insurance.html',
              children : false
            },
            'pharma.html' : {
              text : 'Pharmaceutical',
              url : '/services/industry/pharma.html',
              children : false
            },
            'retail.html' : {
              text : 'Retail',
              url : '/services/industry/retail.html',
              children : false
            }
          }
        },
        solutions : {
          text : '',
          url : '/services/solutions',
          children : {
            abacus : {
              text : 'Abacus',
              url: '/services/solutions/abacus',
              children : false
            },
            'marketing_technology' : {
              text : 'Marketing Technology',
              url: '/services/solutions/marketing_technology',
              children : {
                'email.html' : {
                  text : 'Email',
                  url : '/services/solutions/marketing_technology/email.html',
                  children : false
                },
                'hygiene.html' : {
                  text : 'Hygiene Services',
                  url : '/services/solutions/marketing_technology/hygiene.html',
                  children : false
                },
                'ice.html' : {
                  text : 'Integrated Communications Engine',
                  url : '/services/solutions/marketing_technology/ice.html',
                  children : false
                },
                'integration.html' : {
                  text : 'Customer Data Integration',
                  url : '/services/solutions/marketing_technology/integration.html',
                  children : false
                },
                'loyalty.html' : {
                  text : 'Loyalty',
                  url : '/services/solutions/marketing_technology/loyalty.html',
                  children : false
                },
                'sdb.html' : {
                  text : 'Stategic Database Services',
                  url : '/services/solutions/marketing_technology/sdb.html',
                  children : false
                },
                'web_direct.html' : {
                  text : 'WebDirect',
                  url : '/services/solutions/marketing_technology/web_direct.html',
                  children : false
                }
              }
            },
            purple : {
              text : 'Purple@Epsilon',
              url : '/services/solutions/purple',
              children : {
                'acquisition.html' : {
                  text : 'Acquisition Services',
                  url : '/services/solutions/purple/acquisition.html',
                  children : false
                },
                'brand_creation.html' : {
                  text : 'Brand Creation',
                  url : '/services/solutions/purple/brand_creation.html',
                  children : false
                },
                'creative_implementation.html' : {
                  text : 'Creative Implementation',
                  url : '/services/solutions/purple/creative_implementation.html',
                  children : false
                },
                'deliverability.html' : {
                  text : 'Industry and Deliverability',
                  url : '/services/solutions/purple/deliverability.html',
                  children : false
                },
                'direct_mail.html' : {
                  text : 'Direct Mail',
                  url : '/services/solutions/purple/direct_mail.html',
                  children : false
                },
                'strategic.html' : {
                  text : 'Market Strategics',
                  url : '/services/solutions/purple/strategic.html',
                  children : false
                },
                'targeted_media.html' : {
                  text : 'Targeted Media',
                  url : '/services/solutions/purple/targeted_media.html',
                  children : false
                },
                'web_development.html' : {
                  text : 'Web Development',
                  url : '/services/solutions/purple/web_development.html',
                  children : false
                }
              }
            },
            'analytics.html' : {
              text : 'Analytics',
              url : '/services/solutions/analytics.html',
              children : false
            },
            'data_services.html' : {
              text : 'Data Services',
              url : '/services/solutions/data_services.html',
              children : false
            },
            'strategic_consulting.html' : {
              text : 'Strategic Consulting',
              url : '/services/solutions/strategic_consulting.html',
              children : false
            }
          }
        }
  };
  if (!leftNavNodes[level1]) { return; }
  if (!leftNavNodes[level1].children) { return; }
  var leftNav = leftNavNodes[level1].children;
  var html = '<ul>';
  for (var attr in leftNav) {
    html += createNavItem(leftNav[attr], true);
  }
  html += '</ul>';
  document.getElementById('leftNav').innerHTML = html;
  function createNavItem(node, topLevel) {
    var active = (currentURL.indexOf(node.url) == 0) ? ' class="active"' : '';
    if (currentURL == node.url) {
      if (topLevel) {
        var navItem = '<li'+active+'><a href="/epsilonstatic'+node.url+'"'+active+'>'+node.text+"</a>";
      } else {
        var navItem = '<li><a href="/epsilonstatic'+node.url+'"'+active+'>'+node.text+"</a>";
      }
    } else {
      var navItem = '<li'+active+'><a href="/epsilonstatic'+node.url+'"'+active+'>'+node.text+"</a>";
    }
    if (active) {
      if (node.children) {
        navItem += '<ul>';
        for (var attr in node.children) {
          navItem += createNavItem(node.children[attr], false);
        }
        navItem += '</ul>';
      }
    }
    navItem += '</li>';
    return navItem;
  }
}

/* contact us form ajax submission and validation */

if (typeof(EPSILON) == "undefined") {
  var EPSILON = {};
}
EPSILON.CONTACT_DESCR_MAXLEN = 500;
EPSILON.contactSend = function () {
  var req;
  var url = "/epsilonstatic/contactsubmit.aspx";
  var name = document.getElementById('epsilon_contact_name').value;
  var email = document.getElementById('epsilon_contact_email').value;
  var phone = document.getElementById('epsilon_contact_phone').value;
  var descr = document.getElementById('epsilon_contact_descr').value;
  var message;
  var params;
  var errorText = '';
  var emailRE = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

  //
  // VALIDATE
  //
  if (!emailRE.test(email)) {
    errorText += '\nPlease enter a valid email address.';
  }
  if (!name.length) {
    errorText += '\nPlease enter your name.';
  }
  if (!phone.length) {
    errorText += '\nPlease enter a phone number.';
  }
  if (!descr.length) {
    errorText += '\nPlease enter a message.';
  }
  if (errorText.length) {
    alert(errorText);
    return false;
  }

  //
  // CREATE PARAMETER LIST
  //
  message = descr;
  params = "name="+encodeURI(name)+"&phone="+encodeURI(phone)+"&email="+encodeURI(email)+"&message="+encodeURI(message);

  //
  // SET UP AJAX CALL
  //
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch(e) {
      req = false;
    }
  } else if(window.ActiveXObject) {
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e1) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e2) {
        req = false;
      }
    }
  }
  if (req) {
    try {

  //
  // PROCESS AJAX CALL
  //
      req.onreadystatechange=function () {
        var ready=req.readyState;
        if (ready==4) {
          var httpStatus = req.status;
          if (httpStatus == 200 || httpStatus === 0) {
            document.getElementById('epsilon_contact').innerHTML = req.responseText;
          } else {
            alert("XMLHttpRequest call failed");
          }
        }
      };
      req.open('POST',url,true);
      req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      req.setRequestHeader("Content-length", params.length);
      req.setRequestHeader("Connection", "close");
      req.send(params);
    } catch (e3) {
      alert("Error installing AJAX response handler");
    }
  } else {
    alert("Couldn't instantiate XMLHttpRequest object");
  }
};
EPSILON.contactFullSend = function () {
  var req;
  var url = "/epsilonstatic/contactsubmit.aspx";
  var name = document.getElementById('epsilon_contact_full_name').value;
  var email = document.getElementById('epsilon_contact_full_email').value;
  var phone = document.getElementById('epsilon_contact_full_phone').value;
  var descr = document.getElementById('epsilon_contact_full_descr').value;
  var city = document.getElementById('epsilon_contact_full_city').value;
  var country = document.getElementById('epsilon_contact_full_country').options[document.getElementById('epsilon_contact_full_country').selectedIndex].text;
  var message;
  var params;
  var errorText = '';
  var emailRE = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

  //
  // VALIDATE
  //
  if (!emailRE.test(email)) {
    errorText += '\nPlease enter a valid email address.';
  }
  if (!name.length) {
    errorText += '\nPlease enter your name.';
  }
  if (!phone.length) {
    errorText += '\nPlease enter a phone number.';
  }
  if (!city.length) {
    errorText += '\nPlease enter a city.';
  }
  if (!descr.length) {
    errorText += '\nPlease enter a message.';
  }
  if (errorText.length) {
    alert(errorText);
    return false;
  }

  //
  // CREATE PARAMETER LIST
  //
  message = descr+"\n\n"+city+", "+country;
  params = "name="+encodeURI(name)+"&phone="+encodeURI(phone)+"&email="+encodeURI(email)+"&message="+encodeURI(message);

  //
  // SET UP AJAX CALL
  //
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch(e) {
      req = false;
    }
  } else if(window.ActiveXObject) {
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e1) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e2) {
        req = false;
      }
    }
  }
  if (req) {
    try {

  //
  // PROCESS AJAX CALL
  //
      req.onreadystatechange=function () {
        var ready=req.readyState;
        if (ready==4) {
          var httpStatus = req.status;
          if (httpStatus == 200 || httpStatus === 0) {
            document.getElementById('epsilon_contact_full').innerHTML = req.responseText;
          } else {
            alert("XMLHttpRequest call failed");
          }
        }
      };
      req.open('POST',url,true);
      req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      req.setRequestHeader("Content-length", params.length);
      req.setRequestHeader("Connection", "close");
      req.send(params);
    } catch (e3) {
      alert("Error installing AJAX response handler");
    }
  } else {
    alert("Couldn't instantiate XMLHttpRequest object");
  }
};
EPSILON.newsletterSend = function () {
  var req;
  var url = "/epsilonstatic/newslettersubmit.aspx";
  var name = "== newsletter request ==";
  var email = document.getElementById('epsilon_newsletter_email').value;
  var phone = '';
  var descr = '';
  var message;
  var params;
  var errorText = '';
  var emailRE = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

  //
  // VALIDATE
  //
  if (!emailRE.test(email)) {
    errorText += '\nPlease enter a valid email address.';
  }
  if (errorText.length) {
    alert(errorText);
    return false;
  }

  //
  // CREATE PARAMETER LIST
  //
  message = descr;
  params = "name="+encodeURI(name)+"&phone="+encodeURI(phone)+"&email="+encodeURI(email)+"&message="+encodeURI(message);

  //
  // SET UP AJAX CALL
  //
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch(e) {
      req = false;
    }
  } else if(window.ActiveXObject) {
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e1) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e2) {
        req = false;
      }
    }
  }
  if (req) {
    try {

  //
  // PROCESS AJAX CALL
  //
      req.onreadystatechange=function () {
        var ready=req.readyState;
        if (ready==4) {
          var httpStatus = req.status;
          if (httpStatus == 200 || httpStatus === 0) {
            document.getElementById('newsletterContent').innerHTML = req.responseText;
          } else {
            alert("XMLHttpRequest call failed");
          }
        }
      };
      req.open('POST',url,true);
      req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      req.setRequestHeader("Content-length", params.length);
      req.setRequestHeader("Connection", "close");
      req.send(params);
    } catch (e3) {
      alert("Error installing AJAX response handler");
    }
  } else {
    alert("Couldn't instantiate XMLHttpRequest object");
  }
  return false;
};
EPSILON.checkLen = function () {
  var elem = document.getElementById('epsilon_contact_descr');
  if (elem.value.length > EPSILON.CONTACT_DESCR_MAXLEN) {
    elem.value = elem.value.substr(0,EPSILON.CONTACT_DESCR_MAXLEN);
    return false;
  }
  return true;
};
