var a_obj, emailAddy, subjectPos, subject, emailbodyPos, emailbody;

function mailToFriend(a_obj) {
    if (a_obj.href.match('mailto')) {
        emailAddy = unescape(a_obj.href.replace('mailto:', ''));
        emailAddy = emailAddy.replace('?', '&');
        if (emailAddy.match('&subject=')) {
            subjectPos = emailAddy.indexOf('&subject=');
            subject = emailAddy.substring(subjectPos).replace('&subject=', '');
            emailAddy = emailAddy.replace(emailAddy.substring(subjectPos), '');
            if (subject.match('&body=')) {
                emailbodyPos = subject.indexOf('&body=');
                emailbody = subject.substring(emailbodyPos).replace('&body=', '');
                subject = subject.replace(subject.substring(emailbodyPos), '')
            }
        }
        if (emailAddy.match('&body=')) {
            emailbodyPos = emailAddy.indexOf('&body=');
            emailbody = emailAddy.substring(emailbodyPos).replace('&body=', '');
            emailAddy = emailAddy.replace(emailAddy.substring(emailbodyPos), '');
            if (emailbody.match('&subject=')) {
                subjectPos = emailbody.indexOf('&subject=');
                subject = emailbody.substring(subjectPos).replace('&subject=', '');
                emailbody = emailbody.replace(emailbody.substring(emailbodyPos), '')
            }
        }
        popSendToFriend(emailAddy, subject, emailbody, a_obj)
    } else {
        alert('Failed to send email')
    }
}
function urlencode(str) {
    return escape(str).replace(/\+/g, '%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40')
}
function urldecode(str) {
    return unescape(str.replace(/\+/g, ' '))
}
var makeEmailInputEditable, makeSubjectInputEditable, makeBodyInputEditable, mainEmailOverallTitle, addInExtraFieldName, sendOnToThanksPage, sendOnToThanksPageText;

function popSendToFriend(emailAddy, subject, emailbody, a_obj, makeEmailInputEditable, makeSubjectInputEditable, makeBodyInputEditable, mainEmailOverallTitle, addInExtraFieldName, sendOnToThanksPage) {
    subject = urldecode(subject);
    if (a_obj == undefined) a_obj = '';
    if (isRunningIE6OrBelow) {
        $$('select').each(function (el) {
            el.style.display = "none"
        })
    }
    if (mainEmailOverallTitle == undefined) mainEmailOverallTitle = 'Send To A Friend.';
    if (mainEmailOverallTitle == '') mainEmailOverallTitle = 'Send To A Friend.';
    if (makeEmailInputEditable == undefined) makeEmailInputEditable = true;
    if (makeSubjectInputEditable == undefined) makeSubjectInputEditable = true;
    if (makeBodyInputEditable == undefined) makeBodyInputEditable = true;
    var oDiv = document.createElement('div');
    var randomnumber = Math.floor(Math.random() * 11);
    var oinner_part_content = '';
    if (addInExtraFieldName != undefined) {
        oinner_part_content += ' <span> <strong>' + addInExtraFieldName + ':</strong>           <input type="text" name="email_extra_part" id="email_extra_part" value="" />            <input type="hidden" name="email_extra_part" id="email_extra_part_name" value="' + addInExtraFieldName + '" />          </span> <br /> '
    }
    if (sendOnToThanksPage) sendOnToThanksPageText = "true";
    else sendOnToThanksPageText = "false";
    if (!makeEmailInputEditable) {
        oinner_part_content += ' <span> <strong>Send to:</strong> ' + emailAddy + ' </span> <br />          <input type="hidden" name="email_input_part" id="email_input_part" value="' + emailAddy + '" />'
    } else {
        oinner_part_content += ' <span> <strong>Send to:</strong>           <input type="text" name="email_input_part" id="email_input_part" value="' + emailAddy + '" />           </span> <br /> '
    }
    if (!makeSubjectInputEditable) {
        oinner_part_content += ' <span> <strong>Subject:</strong> ' + subject + ' </span> <br />            <input type="hidden" name="subject_input_part" id="subject_input_part" value="' + subject + '" />'
    } else {
        oinner_part_content += ' <span> <strong>Subject:</strong>           <input type="text" name="subject_input_part" id="subject_input_part" value="' + subject + '" />             </span> <br /> '
    }
    if (!makeBodyInputEditable) {
        oinner_part_content += ' <span> <strong>Body:</strong> ' + emailbody + ' </span> <br />             <input type="hidden" name="body_input_part" id="body_input_part" value="' + subject + '" />'
    } else {
        oinner_part_content += ' <span> <strong>Body:</strong>          <textarea name="body_input_part" id="body_input_part">' + emailbody + '</textarea>          </span> <br /> '
    }
    oDiv.id = 'email_popup_box_' + randomnumber;
    var addedLineAboutMultiSend = '';
    if (makeEmailInputEditable) {
        addedLineAboutMultiSend = '<p> <em> You can send to multiple emails by seperating each one with a semi-colon followed by a space E.g."email@address.com; another@email.com" </em> </p>'
    }
    oDiv.innerHTML = '<div id=\'email_popup_box_main\'>                           <form onsubmit="void(0); return false;">                            <span class="email_close_button" onclick="popSendToFriendClose(\'' + oDiv.id + '\');"> <img src=\"images/glossy_icons/cancel.png\" width=\"20\" height=\"20\" alt=\"Close\" /> </span>                                                      <h3> ' + mainEmailOverallTitle + ' </h3>                                                        ' + addedLineAboutMultiSend + '                                                         <span> <strong>From:</strong>          <input type="text" name="email_input_from" id="email_input_from" value="YOUR EMAIL" />          </span> <br /> ' + oinner_part_content + ' <input type="submit" value="Send" onclick="sendTheToFriendemail(\'' + oDiv.id + '\', ' + sendOnToThanksPageText + '); return false;" />          </form> </div> </div> ';
    
    // Init New Modal Window
    this.modaltoppart = new ModalWindow({
        content     : oDiv.innerHTML,
        scrollTo    : true
    });
    this.modaltoppart.open();
}
function sendTheToFriendemail(sendBoxId, sendOnToThanksPage) {
    var sendEmailUrl = "pages/send_to_friend.php";
    var email_input_part = $('email_input_part').value;
    var subject_input_part = $('subject_input_part').value;
    var body_input_part = $('body_input_part').value;
    var email_input_from = $('email_input_from').value;
    var email_extra_part = ($('email_extra_part')) ? $('email_extra_part_name').value : '';
    var email_extra_part_name = ($('email_extra_part_name')) ? $('email_extra_part_name').value : '';
    new Ajax.Request(sendEmailUrl, {
        method: 'POST',
        parameters: {
            email_part: email_input_part,
            subject_part: subject_input_part,
            body_part: body_input_part,
            email_from: email_input_from,
            email_extra_part: email_extra_part,
            email_extra_part_name: email_extra_part_name
        },
        onSuccess: function (transport) {
            var response = transport.responseText || "no response text";
            alert(response);
            if (sendOnToThanksPage) window.location = "thanks.php"
        },
        onFailure: function () {
            alert('Sorry, but there was an error with your request? Please try again.')
        }
    });
    popSendToFriendClose(sendBoxId);
}
function insertAfter(newChild, refChild) {
    refChild.parentNode.insertBefore(newChild, refChild.nextSibling)
}
var sendBoxId;

function popSendToFriendClose(sendBoxId) {

    this.modaltoppart.close();
    
    setTimeout(function () {
        //document.body.removeChild(document.getElementById(sendBoxId));
        if (isRunningIE6OrBelow) {
            $$('select').each(function (el) {
                el.style.display = "inline"
            })
        }
    }, 600)
}
