$(document).ready(function() {
  $('a[rel=formSend]').click(function() {
    $('#fade').remove();
    $('#formSend').remove();
    $('body').append('<div id="fade"></div>');
    $('body').append('<div id="formSend" class="form_send">'+
      '<div class="button_close"><a href="#" class="close"><img src="./images/button_close.gif" title="Close" alt="Close" border="0"/></a></div>'+
      '<div id="start_page">'+
        '<img class="title" src="./images/title_B.gif" alt="Tell your friend about Olay" />'+
        '<table cellpadding="0" cellspacing="0">'+
          '<tr><td class="left"><p>Friend name</p></td><td><input type="text" class="text" name="fname" id="fname" value="" /></td></tr>'+
          '<tr><td class="left"><p>Friend e-mail</p></td><td><input type="text" class="text" name="femail" id="femail"/></td></tr>'+
          '<tr><td class="left"><p>Your name</p></td><td><input type="text" class="text" name="mname" id="mname"/></td></tr>'+
          '<tr><td class="left"><p>Your e-mail</p></td><td><input type="text" class="text" name="memail" id="memail" /></td></tr>'+
          '<tr><td class="left"><p>Message</p></td><td><textarea name="description" id="description">Join me and SWAP for Total Effects!\nOn Jan 21st, exchange your old cream for a FREE bottle of Total Effects.</textarea></td></tr>'+
          '<tr><td class="left">&nbsp;</td><td><span class="">* All fields are required</span></td></tr>'+
          '<tr><td class="left">&nbsp;</td><td><input type="image" src="./images/button_send.gif" id="button_send" class="button_send" /></td></tr>'+
        '</table>'+
        '<div class="conf">E-mail telling a friends / friends, you hereby acknowledge that they agree to use their email address in order to obtain the information from this website. We will not store or use personal data of your girlfriends / friends for any other purpose than to send this notice, using your email address. Your email address will give them a direct link to the page olayarabia.com. Please do not include email addresses of their girlfriends / friends if they had not yet turned 18 years old</div>'+
		'</div>' + 
      '<div id="end_page">'+
        '<div class="message_sent">Your message has been sent!</div>'+
      '</div>'+
    '</div>');
    $('#start_page').show();
    $('#formSend').fadeIn();
    $('#end_page').hide();

    $('#formSend').css({
      'margin-top' : -(($('#formSend').height() + 80) / 2),
      'margin-left' : -(($('#formSend').width() + 80) / 2)
    });
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
    $('body').scrollTop(0);
    return false;
  });
  
  $('a.close, #fade').live('click', function() {
    $('#fade, .form_send').fadeOut();
    $('#fade').remove();
    $('#formSend').remove();
    return false;
  });
  
  $('#button_send').live('click', function() {
    var fname = $('#fname').val();
    if (fname.length == 0) {
      jAlert('Your friend&#8217;s name can&#8217;t be empty', 'Your data input is incorrect!');
      return false;
    }

    var femail = $('#femail').val();
    if (femail.length == 0) {
      jAlert('Your friend&#8217;s email field can&#8217;t be empty', 'Your data input is incorrect!');
      return false;
    }

    var re = /^\w+([\.-]?\w+)*@(((([a-z0-9]{2,})|([a-z0-9][-][a-z0-9]+))[\.][a-z0-9])|([a-z0-9]+[-]?))+[a-z0-9]+\.([a-z]{2,}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/i;
    if(!re.test(femail)) {
      jAlert('Friend&#8217;s email is invalid', 'Your data input is incorrect!');
      return false;
    }

    var mname = $('#mname').val();
    if (mname.length == 0) {
      jAlert('Your name can&#8217;t be empty', 'Your data input is incorrect!');
      return false;
    }

    var memail = $('#memail').val();
    if (memail.length == 0) {
      jAlert('Your email  field can&#8217;t be empty', 'Your data input is incorrect!');
      return false;
    }

    if(!re.test(memail)) {
      jAlert('Your email is invalid', 'Your data input is incorrect!');
      return false;
    }

    var descr = $('#description').val();
    if (descr.length == 0) {
      jAlert('The message box can&#8217;t be empty', 'Your data input is incorrect!');
      return false;
    }
    $.post("/sendfriend.php", { fname: $('#fname').val(), femail: $('#femail').val(), mname: $('#mname').val(), memail: $('#memail').val(), description: $('#description').val(), url: window.location.href}, 
      function(data){
        //console.log(data);
      }, "text");

    $('#start_page').hide();
    $('#end_page').show();
    return false;
  });
});
