﻿var emailerr = false;
var section = window.location.href.split("/");
var emailscript = "/" + section[3] + "/email-friend.aspx";
var senturl = "";
for (var i = 3; i < section.length; i++) {
    senturl += "/" + section[i];
}
$(document).ready(function() {
  $('#ex2').jqm({
    trigger: 'a#email-friend',
    onShow: function(h) {
        /* callback executed when a trigger click. Show notice */
        h.w.slideDown("normal");
    },
    onHide: function(h) {
        /* callback executed on window hide. Hide notice, overlay. */
        h.o.fadeOut("normal");
        h.w.slideUp("normal",function() { 
          if(h.o) h.o.remove(); 
          $("#mail-friend-form").attr("style", "display:block;");
          $("#confirmation").attr("style", "display:none;");
        });
    }
    });
   $("#btnSend").click(function() {
      validateForm();
      if (emailerr == false) {
        //sendEmail("Name", "Email", "Friend's Email", "Comment");
        $("#mail-friend-form").attr("style", "display:none;");
        $("#confirmation").attr("style", "display:block;");
        $("#confirmation").html('<p class="loading"><img src="/images/ajax-loader.gif" alt="Sending Message" />Sending Message...</p>');
        sendEmail($("#txtYourName").val(), $("#txtYourEmail").val(), $("#txtFriendEmail").val(), $("#txtComments").val());
      }
   });
});
function closeModal() {
  $('#ex2').jqmHide();
}
function sendEmail(yourname, youremail, friendemail, comment) {
    var title = $("#content-column h1:first").html();
    $.ajax({
       type: "POST",
       url: emailscript,
       data: 'yourname=' + yourname.replace('&', '--and--') + '&youremail=' + youremail + '&friendemail=' + friendemail + '&comment=' + comment.replace('&', '--and--') + '&title=' + title.replace('&', '--and--') + '&link=' + window.location.href,
       success: function(html){
         pageTracker._trackPageview(emailscript + "?page=" + senturl);
         $("#confirmation").html(html);
       }
   });
}
function showform(clear) {
  $("#mail-friend-form").attr("style", "display:block;");
  $("#confirmation").attr("style", "display:none;");
  if (clear == "clear") {
    $("#txtFriendEmail").val("");
    $("#txtComments").val("");
  }
}
function validateForm() {
  emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
  var youremail = $("#txtYourEmail").val();
  var friendemail = $("#txtFriendEmail").val();
  var yourerr = false;
  var frienderr = false;
  if (youremail != "") {
    if( !emailpat.test(youremail) ) {
       $("#youremailerr").html("(Enter a <em>valid</em> email address)");
       yourerr = true;
    } else {
       $("#youremailerr").html("");
       yourerr = false;
    }
  } else {
    $("#youremailerr").html("(Enter your email address)");
    yourerr = true;
  }
  if (friendemail != "") {
    if( !emailpat.test(friendemail) ) {
       $("#friendemailerr").html("(Enter a <em>valid</em> email address)");
       frienderr = true;
    } else {
       $("#friendemailerr").html("");
       frienderr = false;
    }
  } else {
    $("#friendemailerr").html("(Enter your friend's email address)");
    frienderr = true;
  }
  if (yourerr == true || frienderr == true) {
    emailerr = true;
  } else {
    emailerr = false;
  }

}
