var friend_collapse_div;

function close_friend_status()
{
   var status_div = document.getElementById('status_div');
   status_div.style.display = 'none';
}

function display_friend_status(title,msg,onclose)
{
   var friend_email_div = document.getElementById('friend_email_div');
   var divX = friend_email_div.offsetLeft;
   var divY = friend_email_div.offsetTop;
   var parent = friend_email_div.offsetParent;
   while (parent) {
      if (parent.offsetLeft) divX += parent.offsetLeft;
      if (parent.offsetTop) divY += parent.offsetTop;
      parent = parent.offsetParent;
   }
   divX += -205;   divY += 80;
   var status_div = document.getElementById('status_div');
   var html = "<div class='status_header'><h1 class='status_h1'>"+title+"</h1><p>" +
      msg+"</p></div><div class='status_footer'><a href=\"\" onclick=\"close_friend_status();";
   if (onclose) html += onclose + " ";
   html += " return false;\" ><img src=\"/images/pop-up-close.jpg\" alt=\"Close Window\" border='0' /></a></div>";
   status_div.innerHTML = html;
   status_div.style.left = divX+"px";
   status_div.style.top = divY+"px";
   status_div.style.display = '';
}

function friend_onload()
{
   friend_collapse_div = new animatedcollapse("friend_div",1000,325,false);
   friend_collapse_div.isExpanded = 'no';
}

function reset_friend_captcha()
{
   var captcha_image = document.getElementById('friend_captcha_image');
   var date_value = new Date();
   captcha_image.src = '/CaptchaSecurityImages.php?characters=5&height=25&width=70&bgr=0&bgg=105&bgb=171&CacheTime=' +
                       date_value.getTime();
}

function finish_display_friend()
{
   if (friend_collapse_div.runtimer) setTimeout(finish_display_friend,1);
   else document.Friend.FriendEMails.focus();
}

function display_friend()
{
   var comodo_seal = document.getElementById('cot_tl_fixed');
   if (comodo_seal) comodo_seal.style.display = 'none';
   var friend_button = document.getElementById('friend_button');
   friend_button.style.display = 'none';
   reset_friend_captcha();
   var div = document.getElementById('friend_div');
   div.style.height = "1px";
   var slide_div = document.getElementById('friend_slide_div');
   slide_div.style.display = '';
   friend_collapse_div.slidedown();
   finish_display_friend();
}

function hide_friend_div()
{
   if (friend_collapse_div.runtimer) setTimeout(hide_friend_div,1);
   else {
      var slide_div = document.getElementById('friend_slide_div');
      slide_div.style.display = 'none';
      document.Friend.FriendEMails.value = '';
      document.Friend.EMail.value = '';
      document.Friend.FirstName.value = '';
      document.Friend.LastName.value = '';
      document.Friend.ValidationCode.value = '';
      var friend_button = document.getElementById('friend_button');
      friend_button.style.display = '';
      var comodo_seal = document.getElementById('cot_tl_fixed');
      if (comodo_seal) comodo_seal.style.display = '';
   }
}

function close_friend_div()
{
   friend_collapse_div.slideup();
   hide_friend_div();
}

function get_friend_field_value(field_name,field_title)
{
   var field = document.Friend[field_name];
   if (field.value == '') {
      display_friend_status('Missing Field',field_title + ' is required',
                            'document.Friend.' + field_name + '.focus();');
      field.focus();   return null;
   }
   return field.value;
}

function check_friend_email(str)
{
    var at = "@";
    var dot = ".";
    var lat = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);

    if (str.indexOf(at) == -1) return false;
    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) return false;
    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) return false;
    if (str.indexOf(at,(lat+1)) != -1) return false;
    if (str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot) return false;
    if (str.indexOf(dot,(lat+2)) == -1) return false;
    if (str.indexOf(" ") != -1) return false;
    return true;
}

var friend_request;

function cancel_friend_request()
{
   friend_request.abort();
}

function finish_friend_request(status,response)
{
   if (status == 200) {
      display_friend_status('Email Sent',
                          'Thank you for sending this page to a friend!');
      friend_collapse_div.slideup();
      hide_friend_div();
   }
   else if (status == 406) {
      display_friend_status('Invalid Code','Invalid Validation Code',
                          'document.Friend.ValidationCode.focus();');
      document.Friend.ValidationCode.focus();
   }
   else if (status == 410) close_friend_status();
   else display_friend_status('Friend Error',response);
}

function send_friend_request(fields)
{
   display_friend_status('Sending Request','Please wait...','cancel_friend_request();');
   friend_request = new XMLHttpRequest();
   friend_request.open('POST','/friend.php',true);
   friend_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
   friend_request.setRequestHeader('Content-length',fields.length);
   friend_request.setRequestHeader('Connection','close');
   friend_request.onreadystatechange = function() {
      if (friend_request.readyState == 4)
         finish_friend_request(friend_request.status,friend_request.responseText);
   }
   friend_request.send(fields);
}

function email_a_friend()
{
   if (friend_collapse_div.isExpanded == 'no') {
      display_friend();   return;
   }

   var friend_emails = get_friend_field_value('FriendEMails','Friend E-mails');
   if (! friend_emails) return;
   var email_array = friend_emails.split(',');
   for (var loop = 0;  loop < email_array.length;  loop++) {
      if (! check_friend_email(email_array[loop])) {
         display_friend_status('Invalid Friend E-mail Addresses',
                               'Please enter one or more correct e-mail addresses',
                               'document.Friend.FriendEMails.focus();');
         document.Friend.FriendEMails.focus();   return;
      }
   }
   var email = get_friend_field_value('EMail','E-mail address');
   if (! email) return;
   if (! check_friend_email(email)) {
      display_friend_status('Invalid E-mail','Please enter a correct e-mail address',
                          'document.Friend.EMail.focus();');
      document.Friend.EMail.focus();   return;
   }
   var confirm_email = get_friend_field_value('ConfirmEMail','Confirm E-mail address');
   if (! confirm_email) return;
   if (confirm_email != email) {
      display_friend_status('Invalid Confirm E-mail','E-mail and Confirm E-mail don\'t match',
                          'document.Friend.ConfirmEMail.focus();');
      document.Friend.ConfirmEMail.focus();   return;
   }
   var first_name = get_friend_field_value('FirstName','First Name');
   if (! first_name) return;
   var last_name = get_friend_field_value('LastName','Last Name');
   if (! last_name) return;
   var validation_code = get_friend_field_value('ValidationCode','Validation Code');
   if (! validation_code) return;

   var fields = 'friend_emails='+friend_emails+'&email='+email+'&first_name='+first_name +
                '&last_name='+last_name+'&ValidationCode='+validation_code;
   send_friend_request(fields);
}

function friend_keypress(evt,next_field)
{
   var key_code = null;
   if (evt.which) key_code = evt.which;
   else if (evt.keyCode) key_code = evt.keyCode;
   if (key_code == 13) {
      if (next_field) document.Friend[next_field].focus();
      else email_a_friend();
      return false;
   }
   return true;
}

