/* var */
var allmail = 0;
var completed = 0;

/* ajax function */
function handleHttpRequestSendMail(xmlHttp, index) {
    setSending(xmlHttp.readyState, index);
    
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            var xmlDoc = xmlHttp.responseXML;
            if (xmlDoc.documentElement) {
                var getData = xmlDoc.documentElement.childNodes;
                setSend(getData, index);
            }
        }
    }
}

function handleHttpRequestGetMember(xmlHttp) {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            var xmlDoc = xmlHttp.responseXML;
            if (xmlDoc.documentElement) {
                var getData = xmlDoc.documentElement.childNodes;
                setMember(getData);
            }
        }
    } else {
        setMemberLoading();
    }
}

function setSending(status, index) {
    var msg_div = "msg" + index;
    var pan_div = "pan" + index;
    var width = 33 * status + "px";
    if (document.getElementById(pan_div)){
        document.getElementById(pan_div).style.border = "1px solid #999";
        
    }
    if (document.getElementById(msg_div)){
        document.getElementById(msg_div).style.width = width;
    }
    //if (document.getElementById(msg_div)){
    //    document.getElementById(msg_div).innerHTML = "正在发送邮件";
    //}
    if (document.getElementById('send')){
        document.getElementById('send').disabled = "disabled";
    }
    if (document.getElementById('close')) {
        document.getElementById('close').disabled = "disabled";
    }
}

function setSend(data, index) {
    var pan_div = "pan" + index;
    if (data[0].nodeValue == 'OK') {
        completed += 1;
        if (allmail == completed) {
            setClose(3);
        }
    } else {
        if (document.getElementById(pan_div)){
            document.getElementById(pan_div).innerHTML = '<span style="color:#FF0000;font-size:12px;"></span>';
        }
        if (document.getElementById('send')){
            document.getElementById('send').disabled = "";
            document.getElementById("send").value = "马上发送页面";
        }
        if (document.getElementById('close')) {
            document.getElementById('close').disabled = "";
        }
    }
}

function setClose(remain) {
    if (remain < 0) {
        closeTell();
    } else {
        if (document.getElementById("close")) {
            document.getElementById("close").disabled = "";
            document.getElementById("close").value = "关闭窗口" + " (" + remain + ")";
        }
        //document.getElementById("daily").innerHTML = remain + "秒后自动关闭";
        remain = remain - 1;
        setTimeout("setClose("+remain+");", 1000);
    }
}

function setMember(data) {
    if (data[0].childNodes[0].firstChild.nodeValue != "null") {
        if (document.getElementById('nickname')){
            document.getElementById('nickname').value = data[0].childNodes[0].firstChild.nodeValue;
        }
    } else {
        if (document.getElementById('nickname')){
            document.getElementById('nickname').value = "";
        }
    }
    if (data[0].childNodes[1].firstChild.nodeValue != "null") {
        if (document.getElementById('from')){
            document.getElementById('from').value = data[0].childNodes[1].firstChild.nodeValue;
        }
    } else {
        if (document.getElementById('from')){
            document.getElementById('from').value = "";
        }
    }
    
}

function setMemberLoading() {
    if (document.getElementById('nickname')){
    document.getElementById('nickname').value = "正在获取数据……";
    }
    if (document.getElementById('from')){
    document.getElementById('from').value = "正在获取数据……";
    }
}

function sendHttpRequestSendMail(xmlHttp, url, nickname, from, to, message, page_url, template, index) {
    var postContent = "nickname=" + encodeURIComponent(nickname) + "&from=" + encodeURIComponent(from) + "&to=" + encodeURIComponent(to) + "&message=" + encodeURIComponent(message) + "&page_url=" + encodeURIComponent(page_url) + "&template=" + encodeURIComponent(template);
    xmlHttp.open('POST', url, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange = function() {
        handleHttpRequestSendMail(xmlHttp, index)
    }
    xmlHttp.send(postContent);
}

function sendHttpRequestGetMember(xmlHttp, url) {
    xmlHttp.open('POST', url, true);
    xmlHttp.onreadystatechange = function() {
        handleHttpRequestGetMember(xmlHttp)
    }
    xmlHttp.send(null);
}

function sendMail(nickname, from, to ,message, page_url, template, index) {
    var xmlHttp = getXmlHttpObject();
    sendHttpRequestSendMail(xmlHttp, '/tell_friend/mail.html', nickname, from, to ,message, page_url, template, index);
}

function getMember() {
    var xmlHttp = getXmlHttpObject();
    sendHttpRequestGetMember(xmlHttp, '/tell_friend/get_member.html');
}

/* page function */
function dispTell() {
   // var tel = document.getElementById('telldiv');
    //if (!tel) {
        var str = document.createElement("div");
        str.id = "telldiv";
        str.style.position = "absolute";
        str.style.setAttribute("width","500px");
        str.style.setAttribute("height","200px");
        str.style.setAttribute("border","2 solid #D765A0");
        str.style.setAttribute("background","#FFFFFF");
        str.style.setAttribute("padding","15px");
        str.style.setAttribute("z-index","100");
        document.body.appendChild(str);
        html = '';
        html += '<form id="form" name="form" method="post">';
        html += '<table border="0" cellspacing="0" cellpadding="0" style="margin-bottom:10px; font-family:Arial;">';
        html += '<tr>';
        html += '<td width="130" align="right" style="font-size:12px; font-family:Arial; color:#DB669D;">您的昵称&nbsp;&nbsp;</td>';
        html += '<td align="center"><img src="/image/tell_friend/txt_2.gif" height="14" />&nbsp;</td>';
        html += '<td><input type="text" name="nickname" id="nickname" size="30" style="border:solid 1px #E6ADD0;color:#AAAAAA; font-family:Arial;"/></td>';
        html += '</tr>';
        html += '<tr>';
        html += '<td width="130" align="right" style="font-size:12px; font-family:Arial; color:#DB669D;">您的e-mail&nbsp;&nbsp;</td>';
        html += '<td align="center"><img src="/image/tell_friend/txt_3.gif" height="13" />&nbsp;</td>';
        html += '<td><input type="text" name="from" id="from" size="30" style="border:solid 1px #E6ADD0;color:#AAAAAA; font-family:Arial;"/></td>';
        html += '</tr>';
        html += '<tr>';
        html += '<td width="130" align="right" style="font-size:12px; font-family:Arial; color:#DB669D;">朋友的e-mail&nbsp;&nbsp;</td>';
        html += '<td align="center"><span style="color:DB669D;">1</span></td>';
        html += '<td>';
        html += '<table border="0" cellspacing="0" cellpadding="0">';
        html += '<tr>';
        html += '<td><input type="text" name="to" id="to1" size="30" style="border:solid 1px #E6ADD0;color:#AAAAAA; font-family:Arial;"/></td>';
        html += '<td><div id="pan1" style="width:132px;margin-left:4px;"><div id="msg1" style="width:0px; height:8px; font-size:1px; background:#6FE447;"></div></div></td>';
        html += '</tr>';
        html += '</table>';
        html += '</td>';
        html += '</tr>';
        html += '<tr>';
        html += '<td width="130" align="right" style="font-size:12px; font-family:Arial; color:#B47052;"> </td>';
        html += '<td align="center"><span style="color:DB669D;">2</span></td>';
        html += '<td>';
        html += '<table border="0" cellspacing="0" cellpadding="0">';
        html += '<tr>';
        html += '<td><input type="text" name="to" id="to2" size="30" style="border:solid 1px #E6ADD0;color:#AAAAAA; font-family:Arial;"/></td>';
        html += '<td><div id="pan2" style="width:132px;margin-left:4px;"><div id="msg2" style="width:0px; height:8px; font-size:1px; line-height:0px; background:#6FE447;"></div></div></td>';
        html += '</tr>';
        html += '</table>';
        html += '</td>';
        html += '</tr>';
        html += '<tr>';
    	html += '<td width="130" align="right" style="font-size:12px; font-family:Arial; color:#B47052;"> </td>';
        html += '<td align="center"><span style="color:DB669D;">3</span></td>';
        html += '<td>';
        html += '<table border="0" cellspacing="0" cellpadding="0">';
        html += '<tr>';
        html += '<td><input type="text" name="to" id="to3" size="30" style="border:solid 1px #E6ADD0;color:#AAAAAA; font-family:Arial;"/></td>';
        html += '<td><div id="pan3" style="width:132px;margin-left:4px;"><div id="msg3" style="width:0px; height:8px; font-size:1px; line-height:0px; background:#6FE447;"></div></div></td>';
        html += '</tr>';
        html += '</table>';
        html += '</td>';
        html += '</tr>';
        html += '<tr>';
        html += '<td width="130" align="right" style="font-size:12px; font-family:Arial; color:#DB669D;">您给朋友的留言&nbsp;&nbsp;</td>';
        html += '<td align="center"><img src="/image/tell_friend/txt_5.gif" height="14" />&nbsp;</td>';
        html += '<td><textarea name="message" id="message" cols="55" rows="5" style="border:solid 1px #E6ADD0;color:#AAAAAA; font-family:Arial;"></textarea></td>';
        html += '</tr>';
        html += '</table>';
        html += '<input type="button" id="send" value="马上发送页面" style="width:120px; height:20px; border:solid 1px #C05D9D; background:#C05D9D; color:#FFF9FF; padding:2px; font-size:12px; font-weight:bold;" onclick="send_mail();" />';
        html += '<input type="button" id="close" value="关闭窗口" style="width:120px; height:20px; border:solid 1px #C05D9D; background:#C05D9D; color:#FFF9FF; padding:2px; font-size:12px; font-weight:bold; margin-left:80px;" onclick="closeTell();" />';
        html += '<span id="daily"></span>';
        html += '<input type="hidden" name="page_url" id="page_url" value="' + window.location.href + '" />';
        html += '<input type="hidden" name="template" id="template" value="tell_friend" /><br>';
        html += '</form>';
        str.innerHTML = html;
        var mousex = mousePosX();
        var mousey = mousePosY();
    	//str.style.top = (document.body.scrollTop + (document.body.clientHeight - str.offsetHeight) / 2) + "px";
    	str.style.left = (document.body.scrollLeft + (document.body.clientWidth - str.offsetWidth) / 2) + 180 + "px";
    	str.style.top = ((mousey - str.offsetHeight/2)) + "px";
    	//str.style.left = (document.body.scrollLeft + (mousex - str.offsetWidth/2)) + "px";
        getMember();
    //}
}

function readyTell() {
    // load images
	var the_images = new Array(
    '/image/tell_friend/txt_1.gif',
    '/image/tell_friend/txt_2.gif',
    '/image/tell_friend/txt_3.gif',
    '/image/tell_friend/txt_4.gif',
    '/image/tell_friend/txt_5.gif');
    preloadImages(the_images);
    
    var tell_link = document.getElementById('tel');
    if (tell_link) {
        tell_link.style.margin = "10px 0 0 10px";
        //tell_link.innerHTML = '<a href="javascript:void 0;" onclick="dispTell();" style="color:#E786AD; font-size:12px; text-decoration:underline;">我要和朋友分享此页面</a>';
    }
}

function closeTell() {
    var tel = document.getElementById('telldiv');
    if (tel) {
        tel.style.display = "none";
        tel.removeNode(true);
    }
}

function send_mail() {
    allmail = 0;
    completed = 0;
    
    
    nickname = document.getElementById('nickname').value;
    from = document.getElementById('from').value;
    to1 = document.getElementById('to1').value;
    to2 = document.getElementById('to2').value;
    to3 = document.getElementById('to3').value;
    message = document.getElementById('message').value;
    page_url = document.getElementById('page_url').value;
    template = document.getElementById('template').value;
    
    
    if(from == "") {
        alert('请输入您的e-mail地址');
        document.getElementById('from').focus();
        return;
    } else {
        if(!/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(from)) {
            alert('请检查您的e-mail地址');
            document.getElementById('from').focus();
            return;
        }
    }
    
    if(to1 == "" && to2 == "" && to3 == "") {
        alert('请输入您朋友的e-mail地址');
        document.getElementById('to1').focus();
        return;
    }
    if (to1 != "") {
        if(!/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(to1)) {
            alert('请检查您朋友的e-mail地址');
            document.getElementById('to1').focus();
            return;
        }
        if(to1 == to2 || to1 == to3) {
            alert('e-mail地址不能重复');
            document.getElementById('to1').focus();
            return;
        }
        allmail += 1;
        var check1 = 1;
    } else {
        var check1 = 0;
    }
    if (to2 != "") {
        if(!/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(to2)) {
            alert('请检查您朋友的e-mail地址');
            document.getElementById('to2').focus();
            return;
        }
        if(to2 == to1 || to2 == to3) {
            alert('e-mail地址不能重复');
            document.getElementById('to2').focus();
            return;
        }
        allmail += 1;
        var check2 = 1;
    } else {
        var check2 = 0;
    }
    if (to3 != "") {
        if(!/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(to3)) {
            alert('请检查您朋友的e-mail地址');
            document.getElementById('to3').focus();
            return;
        }
        if(to3 == to1 || to3 == to2) {
            alert('e-mail地址不能重复');
            document.getElementById('to3').focus();
            return;
        }
        allmail += 1;
        var check3 = 1;
    } else {
        var check3 = 0;
    }
    if (check1) sendMail(nickname, from, to1 ,message, page_url, template, "1");
    if (check2) sendMail(nickname, from, to2 ,message, page_url, template, "2");
    if (check3) sendMail(nickname, from, to3 ,message, page_url, template, "3");
    //sendMail(nickname, from, to ,message, page_url, template);
}

if (window.addEventListener) {
    //window.addEventListener("load", readyTell, false);
} else if (window.attachEvent) {
    //window.attachEvent("onload", readyTell);
}