/*********************************************************************************************\
*       COPYRIGHT © 2009 ENVISION INFORMATION TECHNOLOGIES, LLC.    ALL RIGHTS RESERVED       *
*       DISTRIBUTION, UNAUTHORIZED USE AND MODIFICATION IS STRICTLY PROHIBITED                *
*       ENVISION IT, MADISON, WI    http://www.envisionitllc.com   info@envisionitllc.com     *
\*********************************************************************************************/

function displayEmailAFriend(pageId){
  displayEmailAFriendSpecifyHeight(pageId, "550px");
}

function displayEmailAFriendSpecifyHeight(pageId, height)
{
  var bg = document.createElement("div");
  bg.className = "BGCover";
  bg.id = "bg";
  document.body.appendChild(bg);
  
  var container = document.createElement("div");
  container.style.position = "absolute";
  container.style.width = "750px";
  container.style.height = height;
  container.style.left = "50%";
  container.style.top = "70px";
  container.style.overflow="hidden";
  container.style.marginLeft="-375px";
  container.style.border = "1px solid black";
  container.style.background = "white";
  container.id = "emailContainer";
  
  var email = document.createElement("iframe");
  email.style.width = "750px";
  email.style.height = "550px";
  email.style.border = "0px solid black";
  email.src = "/email_friend/emailfriend_send.php?pageViewId=" + pageId;
  container.appendChild(email);
  
  document.body.appendChild(container);
}

function displayFriendConnect()
{
  var fConnect = document.createElement("iframe");
  fConnect.id = "connectContainer";
  fConnect.style.width = "745px";
  fConnect.style.height = "495px";
  fConnect.style.left="0px";
  fConnect.style.top="0px";
  fConnect.style.position="absolute";
  fConnect.src = "/email_friend/emailfriend_getcontacts.php";
  
  document.body.appendChild(fConnect);
}

function hideEmailAFriend()
{
  var container = document.getElementById("emailContainer");
  if(container != undefined)
    document.body.removeChild(container);

  var bg = document.getElementById("bg");
  if(bg != undefined)
    document.body.removeChild(bg);
}

function hideFriendConnect()
{
  var container = document.getElementById("connectContainer");

  if(container != undefined)
    document.body.removeChild(container);
}

function addAddress(name, address)
{
  var row = document.getElementById('selectedAddresses').insertRow(-1);
  var cell = row.insertCell(-1);
  cell.innerHTML='<a href="#" onclick="deleteAddress(this.parentNode.parentNode);return false;">delete</a>';
  
  cell = row.insertCell(-1);
  cell.innerHTML = name;
  
  cell = row.insertCell(-1);
  cell.innerHTML = address;
}

function addAddressFromInput()
{
  var name = document.getElementById("friendsname");
  var address = document.getElementById("friendsemail");
  
  if(name.value == '' || address.value == '')
  {
    alert('You must enter a valid name and email address');
    return;  
  }
  
  addAddress(name.value,address.value);

  name.value = '';
  address.value = '';
}

function deleteAddress(row)
{
  row.parentNode.removeChild(row);
}

function error(msg)
{
  alert(msg);
  return false;
}

function sendEmail()
{
  var table = document.getElementById("selectedAddresses");
  
  var postData = "";
  for(i=1; i < table.rows.length; i++)
  {
    postData += table.rows[i].cells[1].innerHTML + "&FIELD&" + table.rows[i].cells[2].innerHTML + "\n";
  }
  
  var fromName = document.getElementById('sendersname').value;
  var fromEmail = document.getElementById('sendersemail').value;
  var comments = document.getElementById('comments').value;
  var captcha = document.getElementById('captchacode').value;
  
  if(fromName == '')
    return error("Please enter your name");

  if(fromEmail == '')
    return error("Please enter your e-mail address");
  
  if (table.rows.length == 1)
    return error("Please enter at least one friend to e-mail.");
  
  if (captcha.length != 6)
    return error("Please enter the code in the image (all 6 characters)");

  var xmlHttp = createHTTPRequest();
   
  xmlHttp.onreadystatechange = function()
  {
    // xmlHttp.readyState
    // 0 The request is not initialized, 1 The request has been set up, 2 The request has been sent, 3 The request is in process, 4 The request is complete
    
    if (xmlHttp.readyState == 4)
    {
      var text = xmlHttp.responseText;
      
      if (text == 'captcha')
      {
        alert('The security image code is not correct.');
        parent.hideWait();
      }
      else if (text == 'TRUE')
      {
        parent.hideWait();
        alert('Your e-mail has been sent.  Thank you!');
        parent.hideEmailAFriend();
      }
      else
      {
        parent.hideWait();
        alert('Your e-mail was not able to be sent.  The following message occurred: ' + text);
      }
    }
  }
  
  var params = "data=" + escape(postData) + "&pageViewId=" + pageViewId + "&fromName=" + escape(fromName) + "&fromEmail=" + escape(fromEmail) + "&comments=" + escape(comments) + "&captcha=" + escape(captcha);

  xmlHttp.open("POST","emailfriend_sendparse.php",true);

  //Send the proper header information along with the request
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  
  xmlHttp.send(params);
  
  parent.showWait();  
}
