
               /*  .................................................  */
               /*  file:                                              */
               /*  author:Ted O'Hara                                  */
               /*  module:                                            */
               /*  purpose:                                           */                       
               /*  Created: 01/31/2006                                */
               /*  Copyright (C) 2006 bx.com, Inc.                    */
               /*  .................................................  */ 
               
               
function toggleEmail(bool)
{       
  if (typeof bool != 'boolean') 
    {
      bool = (emaildiv.style.display == 'none')
     }
  emaildiv.style.display = (bool) ? '': 'none';
  return false;
}




/*function to make an XMLHttpRequest. Must supply a URL (with variables, if necessary,
  and a reference to a function to handle the response.*/
function makeRequest(url,fnReference) {

  http_request = false;

  if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest(); 
      if (http_request.overrideMimeType) 
        {
          http_request.overrideMimeType('text/xml'); 
        }         
  } else if (window.ActiveXObject) { // IE
      try {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
          try {
              http_request = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {}
      }
  }

  if (!http_request) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
  }
  http_request.onreadystatechange = fnReference;        
  http_request.open('GET', url, true);
  http_request.send(null);
}


var globalEmailError = "";

//Function to validate an email address
function isvalidemail(val)
{
   var periodpos = val.indexOf("."); 
   var atpos=val.indexOf("@");
   globalEmailError = "";
   if(val.length<5) { globalEmailError+="Email address is too short."; }
   if(atpos<1 || atpos>periodpos) { globalEmailError+="The AT '@' symbol is missing or in the wrong position."; }
   if(periodpos<3 || periodpos+1==val.length || periodpos==atpos+1) { globalEmailError+="A period '.' is missing or in wrong place."; }
   if(globalEmailError != "") { globalEmailError += " Please correct the problem(s) and try again."; return false; }
   return true;
}


//Function to prep for the XMLHttpRequest. Sets up url, checks the email, then calls the request function
function submitViaAjax()
  {
   var hostname = window.location.hostname;
   //var hostname = 'irbt.lnxstaging6.gsipartners.com';
   var urlroot = '/custom/newsletterSignupAJX.cfm';
   //var urlroot = '/include/entry.jsp';
   var thisaddress = this.email_address.value;
   var validAddress = isvalidemail(thisaddress);
   
   if (!validAddress)
    {
      textContainer.firstChild.nodeValue = globalEmailError;
      return false;
    }
   
   
   var fullurl = 'http:\/\/' + hostname + urlroot + '?emailAddress=' + thisaddress + '&contest=irbt_nocontest&emailPref=Y'
   //if (confirm(fullurl)) {makeRequest(fullurl,handleResponse)}
   makeRequest(fullurl,handleResponse)
   return false;
  }
  
  
  
//Function to handle what we get back from the AJAX response.
function handleResponse() {

  if (http_request.readyState == 4) {//we're ready, Freddy.
      if (http_request.status == 200) {
      //pick apart the response   
      //alert(http_request.responseText)
      var status = http_request.responseText.split('|') [0]  
      var message = http_request.responseText.split('|') [1]
      
      var goodToGo = (status == 'Yes')
      //alert(goodToGo)
      if (goodToGo)
        {
          textContainer.firstChild.nodeValue = message
          textContainer.className = 'response';
          formBody.removeChild(maillabel)
          formBody.removeChild(mailfield)
          var x = document.createTextNode('Your email will not be shared with anyone outside iRobot. You may unsubscribe at any time.')
          formBody.appendChild(x)
          sButton.src ='/images/irobothp/hdr_email_close.gif' 
          sButton.onclick =  toggleEmail;       
        }
      
      } 
      else { //non 200 code
          alert('There was a problem with the request.');
          var tmp = window.open('errorwindow')
          tmp.document.open()
          tmp.document.write(http_request.responseText)
          tmp.document.close()
      }
  }

}
  
function emailOpen(emailslider) {
	toggleEmail();
}



if (document.getElementById)
{
  var emaildiv = document.getElementById('emailcontainer');
  emaildiv.style.display = 'none';
  var toggleButton = document.getElementById('emailbutton');
  var toggleLink = document.getElementById('emailLink');
  //var closeIcon =  document.getElementById('closeicon');
  //var textContainer = document.getElementById('emailtext');
  //var maillabel = document.getElementById('maillabel');
  //var mailfield = document.getElementById('email_address');
  //var formBody = document.getElementById('formbody');
  //var sButton = document.getElementById('submitter');
  
  //var emailform = document.emailform
  //emailform.onsubmit = submitViaAjax
  
  toggleButton.onclick = toggleEmail;
  toggleLink.onclick = toggleEmail;
  //closeIcon.onclick = function () {toggleEmail(false)};      
}



