// Send to friend
jQuery(document).ready(function() {
    jQuery(".iMail").click(function() {

        //this needs to be first or the positioning isn't right in firefox, yes I know it is absolute, but still doesn't work correctly. doctype?
        jQuery(".emailForm").show();
        jQuery("#NewSendToFriendDiv").fadeIn("fast");

        //get parents pos
        var parentTop;
        var box = document.getElementById('NewSendToFriendDiv').offsetParent;
        parentTop = 0;
        while (box) {
            parentTop += box.offsetTop;
            box = box.offsetParent;
        }

        var l = 0, t = 0;  //get the scroll position
        if (typeof (window.pageYOffset) == 'number') {  // Netscape
            //l = window.pageXOffset;
            t = window.pageYOffset;
        } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {  // DOM
            //l = document.body.scrollLeft;
            t = document.body.scrollTop;
        } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {// IE6 standards compliant mode
            //l = document.documentElement.scrollLeft;
            t = document.documentElement.scrollTop;
        }

        var objPosCheck, offTop;  //try and guess which mail icon was clicked. If the top, show box just under it, if bottom, put it above.

        offTop = 0;
        objPosCheck = document.getElementById('iMailTop');
        while (objPosCheck) {
            offTop += objPosCheck.offsetTop;
            objPosCheck = objPosCheck.offsetParent;
        }
        if (t + 30 <= offTop) { //use top
            t = offTop;
        } else { //use bottom
            offTop = 0;
            objPosCheck = document.getElementById('iMailBottom');
            while (objPosCheck) {
                offTop += objPosCheck.offsetTop;
                objPosCheck = objPosCheck.offsetParent;
            }
            t = offTop - 200;  //200 is the height of the box
        }
       
        t -= parentTop;
        l = 300;  //middle'ish

        jQuery("#NewSendToFriendDiv").css("left", l);
        jQuery("#NewSendToFriendDiv").css("top", t);
    });
});



///////////////////Send to friend new method
function CloseSendToFriend() {
    jQuery("#NewSendToFriendDiv").fadeOut("slow");
    //jQuery(".emailForm").show();
    jQuery("#emailsent").hide();
    jQuery("#yourName").val("");
    jQuery("#yourEmail").val("");
    jQuery("#friendsName").val("");
    jQuery("#friendsEmail").val("");
    jQuery("#emailStatus").html("");
}

function NewSendToFriends() {
    var a = window.location.search.split("newsId");
    if (a[1])
        var b = a[1].split("&");
    var newsId = b ? b[0] : a[1] ? a[1] : "none";
    jQuery("#emailStatus").html("Sending email");
    jQuery("#emailStatus").removeClass("errorDiv");
    jQuery("#emailStatus").addClass("msgDiv");

    if (jQuery("#yourName").val().trim().length > 0) {
        if (validateEmails(jQuery("#yourEmail").val().trim())) {
            if (jQuery("#friendsName").val().trim().length > 0) {
                if (validateEmails(jQuery("#friendsEmail").val().trim())) {
                    var postUrl;
                    if (window.location.host.toLowerCase() == 'localhost')
                        postUrl = "http://localhost/DNN/_data/feedpages/newssendtofriend.aspx";
                    else
                        postUrl = "/_data/FeedPages/NewsSendToFriend.aspx";

                    var params = "uname=" + escape(jQuery("#yourName").val().trim()) + "&uemail=" + escape(jQuery("#yourEmail").val().trim())
						+ "&fname=" + escape(jQuery("#friendsName").val().trim()) + "&femail=" + escape(jQuery("#friendsEmail").val().trim())
						+ "&newsURL=" + escape(window.location.protocol + "//" + window.location.host + window.location.pathname)
						+ "&headline=" + escape(document.getElementById('articleHeadline').innerHTML)
						+ "&date=" + escape(document.getElementById('articleDate').innerHTML);
                    $.ajax({
                        type: "POST",
                        url: postUrl,
                        data: params,
                        error: function(msg, status) {
                            NewSendEmailComplete(status, msg);
                        },
                        success: function(msg, status) {
                            NewSendEmailComplete(status, msg);
                        }
                    });
                }
                else {
                    jQuery("#emailStatus").html("Your friend's e-mail is in wrong format");
                    jQuery("#emailStatus").addClass("errorDiv");
                    jQuery("#emailStatus").show();
                }
            }
            else {
                jQuery("#emailStatus").html("Please enter your friend's name");
                jQuery("#emailStatus").addClass("errorDiv");
                jQuery("#emailStatus").show();
            }
        }
        else {
            jQuery("#emailStatus").html("Your e-mail is in wrong format");
            jQuery("#emailStatus").addClass("errorDiv");
            jQuery("#emailStatus").show();
        }
    }
    else {
        jQuery("#emailStatus").html("Please enter your name");
        jQuery("#emailStatus").addClass("errorDiv");
    }
}

function NewSendEmailComplete(responseText, msg) {
    if (responseText == "success") {
        jQuery(".emailForm").hide();
        jQuery("#emailsent").show();
    }
    else {
        jQuery("#emailStatus").html("Error sending email, try again later");
        jQuery("#emailStatus").addClass("errorDiv");
    }
    ;
}



function validateEmails(value) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return filter.test(value);

}

function trim(str) {
    var s = str.replace(/^(\s)*/, '');
    var ret = s.replace(/(\s)*$/, '');
    return ret;
}



/*
I don't think this is used in horse racing (only bigpond sport)
function FixCategory(cat){
cat = jQuery.trim(cat).toLowerCase();
if (cat == "motor"){
cat = "motorsport";
}
else if (cat == "racing"){
cat = "horse racing";
}
else if (cat == "surfing"){
cat = "more";
}
else if (cat == "tour de france"){
cat = "more";
}
return cat;
}
*/