﻿var email,pass,emailcount,emailarr,emailsel;
function checkfriendemail()
{
    email = $F("emailtb").strip();
    pass = $F("passtb").strip();
    
    if(email == "" || pass == "")
    {
        alert("Please Fill All!");
        return;
    }
    
    email = email + $("email_sel").options[$("email_sel").selectedIndex].text;;
    
    var res = /^[0-9a-zA-Z_\-\.]+@[0-9a-zA-Z_\-]+(\.[0-9a-zA-Z_\-]+)*$/;  
    var re = new RegExp(res);
    if(!re.test(email))
    {
        alert("Please check Email");
        return;
    } 
    
    var url = "ashx/friend_email.ashx";
    var par = "email=" + email + "&pass=" + pass + "&emailtype=" + $F("email_sel");
    var myAjax = new Ajax.Request(
                                    url,
                                    {
                                        method: "post",
                                        postBody: par,
                                        onComplete: getlogemail,
                                        onLoading: loading,
                                        onLoaded: loaded
                                    }
                                );
}

function getlogemail(xmlhttp)
{
    var result = xmlhttp.responseText;
    
    if(result == "error")
    {
        alert("Please check your email and password!");
        return;
    }
    if(result == "")
    {
        alert("There is no one in the contact list!");
        return;
    }
    
    $("div1").style.display = "none";
    $("div2").style.display = "none";
    $("div3").style.display = "";
    
    //alert(result);
    
    var tbycount = $("emailtby").rows.length;
    
    for(var h = 1; h < tbycount; h++)
    {
        $("emailtby").removeChild($("emailtby").rows.item(1));
    }
    
    var emailarr = result.split("|");
    emailarr.sort(function(a,b){return b<a});
    emailcount = emailarr.length;
    var tbyhtml = "";
    var row;
    var cell; 
    
    for(var i = 0; i < emailcount; i++)
    { 
        var cssnum = i % 2 + 2 + "";
        var arr = emailarr[i].split(" ");

        row = document.createElement("tr");  
        row.className = "emaillist" + cssnum;
        
        cell = document.createElement("td");
        cell.className = "emailbb";
        cell.innerHTML = "<input type='checkbox' id='cbemail" + i + "' value='" + arr[1] + "' />";
        row.appendChild(cell);
        
        cell = document.createElement("td");
        cell.className = "c_w fwb fs12 emailbb";
        cell.setAttribute("height", "30"); 
        cell.innerHTML = emailarr[i];
        row.appendChild(cell);
        
        $("emailtby").appendChild(row);
    }

//    $("emailtby").innerHTML +=  tbyhtml;
   
}
function loading()
{
    $("emailbtn").style.display = "none";
    $("loading").style.display = "";
}
function loaded()
{
    $("emailbtn").style.display = "";
    $("loading").style.display = "none";
}




//msn
function checkfriendmsn()
{
    email = $F("emailtb").strip();
    pass = $F("passtb").strip();
    
    if(email == "" || pass == "")
    {
        alert("Please Fill All!");
        return;
    }
    
    var res = /^[0-9a-zA-Z_\-\.]+@[0-9a-zA-Z_\-]+(\.[0-9a-zA-Z_\-]+)*$/;  
    var re = new RegExp(res);
    if(!re.test(email))
    {
        alert("Please check Email");
        return;
    } 
    
    var url = "ashx/friend_msn.ashx";
    var par = "email=" + email + "&pass=" + pass;
    var myAjax = new Ajax.Request(
                                    url,
                                    {
                                        method: "post",
                                        postBody: par,
                                        onComplete: getlogemail,
                                        onLoading: loading,
                                        onLoaded: loaded
                                    }
                                );
}



function cball()
{
    var check = $("allcb").checked;
    
    for(var i = 0; i< emailcount; i++)
    {
        $("cbemail" + i).checked = check;
    }
    
    
}

function getfriendlink()
{
    var uinfo = getCookie("customerinfo");
    
    if(uinfo != null)
    {
        var arr = uinfo.split("|||");
        var linkemail = arr[1];
        
        var res = /^[0-9a-zA-Z_\-\.]+@[0-9a-zA-Z_\-]+(\.[0-9a-zA-Z_\-]+)*$/;  
        var re = new RegExp(res);
        if(!re.test(linkemail))
        {
            alert("Please check Email");
            return;
        } 
        
        var url = "ashx/linkemail.ashx";
        var par = "linkemail=" + linkemail;
        var myAjax = new Ajax.Request(
                                        url,
                                        {
                                            method: "post",
                                            postBody: par,
                                            onComplete: getlinkemail
                                        }
                                    );
    }
}

function getlinkemail(xmlhttp)
{
    var deslink = xmlhttp.responseText;
    if(deslink.length < 10 )
    {
        alert("Data error");
        return;
    }
    var url = document.location.href;
    var arr = url.split("/");
    $("linktb").value = "http://" + arr[2] + "/fromfriend.aspx?friend=" + deslink;
}




function linkbtnshow()
{
    if("\v"=="v")
    //if(document.all)
    {
        $("linkbtn").style.display = "";
    }
}


function copylink()
{
    window.clipboardData.setData("Text",$F("linktb"));
    alert("Copy OK!"); 
}

function checkemail()
{
    $("sendbtn").style.display = "none";
    emailsel = "";
    
    for(var i=0; i<emailcount; i++)
    {
        if($("cbemail" + i).checked)
        {
            emailsel += $("cbemail" + i).value + "|";
        }
    }
    
    if(emailsel == "")
    {
        alert("Please select email!");
        $("sendbtn").style.display = "";
        return;
    }
    
    var uinfo = getCookie("customerinfo");
    
    if(uinfo != null)
    {
        var arr = uinfo.split("|||");
        var linkemail = arr[1];
        
        var url = "ashx/sendemail.ashx";
        var par = "email=" + email + 
                  "&pass=" + pass +
                  //"&emailtype=" + "gmail" +
                  "&emailtype=" + $F("email_sel") +
                  "&emaillist=" + emailsel +
                  "&linkemail=" + linkemail;
        var myAjax = new Ajax.Request(
                                        url,
                                        {
                                            method: "post",
                                            postBody: par,
                                            onComplete: sendemail
                                        }
                                    );
                                
    }
    else
    {
        return;
    }
    
    
}

function sendemail(xmlhttp)
{
    var result = xmlhttp.responseText;

    if(result == "ok")
    {
        alert("The success of e-mail! ");
    }
    else
    {
        alert("E-mail failure! ");
    }
}


function cgemail()
{
    if($F("email_sel") == "yahoo")
    {
        $("yahooinfo").style.display = "";
    }
    else
    {
        $("yahooinfo").style.display = "none";
    }
}

function getsnsemail()
{
    var uinfo = getCookie("customerinfo");
    if(uinfo != null)
    {
        var arr = uinfo.split("|||");
        var linkemail = arr[1];
        
        var res = /^[0-9a-zA-Z_\-\.]+@[0-9a-zA-Z_\-]+(\.[0-9a-zA-Z_\-]+)*$/;  
        var re = new RegExp(res);
        if(!re.test(linkemail))
        {
            alert("Please check Email");
            return;
        } 
        var url = "ashx/getsnsemail.ashx?linkemail=" + linkemail;
        var myAjax = new Ajax.Request(
                                        url,
                                        {
                                            method: "get",
                                            onComplete: getsnsemaildo
                                        }
                                    );
    }
}

function getsnsemaildo(xmlhttp)
{
    var result = xmlhttp.responseText;
    var arr = result.split("|||||");
    
    if(arr.length == 2)
    {
        $("tbtitle").value = arr[0];
        
        $("tbcontent").innerHTML = arr[1];
    }
}

function selmsn()
{
    $("tbmsn").value = "";
    emailsel = "";
    
    for(var i=0; i<emailcount; i++)
    {
        if($("cbemail" + i).checked)
        {
            emailsel += $("cbemail" + i).value + ";";
        }
    }
    
    $("tbmsn").value = emailsel;
    
    if(emailsel != "")
    {
        $("div3").style.display = "none";
        $("div4").style.display = "";
    }
    else
    {
        alert("Please select friend!");
    }
}


function showstmp()
{
    window.open("smtpyahoo.htm","smtp","height=700, width=600, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no");
}

function msnemailsend()
{
    $("sendbtn").style.display = "none";
    emailsel = "";
    
    emailsel = $F("tbmsn");
    email = $F("emailtb");
    pass = $F("passtb");
    
    if(emailsel == "")
    {
        alert("Please select email!");
        $("sendbtn").style.display = "";
        return;
    }
    
    var uinfo = getCookie("customerinfo");
    
    if(uinfo != null)
    {
        var arr = uinfo.split("|||");
        var linkemail = arr[1];
        
        var url = "ashx/sendemail.ashx";
        var par = "email=" + email + 
                  "&pass=" + pass +
                  "&emailtype=hotmail" +
                  "&emaillist=" + emailsel +
                  "&linkemail=" + linkemail;
        var myAjax = new Ajax.Request(
                                        url,
                                        {
                                            method: "post",
                                            postBody: par,
                                            onComplete: sendemail
                                        }
                                    );
                                
    }
    else
    {
        return;
    }
}

function getcode()
{
     var uid=getuid();
     if(uid=="" || getuid()==0)
     {
        return;
     }
     
     var xml="<NOED>";
     xml+="<uid>"+escape(uid)+"</uid>";
     xml+="</NOED>";
     var url="ashx/friendcode.ashx";
     var myAjax = new Ajax.Request(
                                        url,
                                        {
                                            method: "post",
                                            postBody: xml,
                                            onComplete: setcode
                                        }
                                    );
     
} 
function setcode(xmlhttp)
{
     var result = xmlhttp.responseText;
     $("codevalue").innerHTML=result;
}

function codeclass()
{
    var code=$F("txtcode");
    if(code=="")
    {
        alert("Please enter the verification code!");
        return;
    }
    else
    {
         $("txtcode").readonly="true";
         if($("fbtn") != null)
         {
            $("fbtn").style.display="none";
         }
         var xml="<NOED>";
         xml+="<code>"+escape(code)+"</code>";
         xml+="</NOED>";
         var url="ashx/code.ashx";
         var myAjax = new Ajax.Request(
                                        url,
                                        {
                                            method: "post",
                                            postBody: xml,
                                            onComplete: codelook
                                        }
                                    );
    }
    
}

function codelook(xmlhttp)
{
    var result = xmlhttp.responseText; 
    if(result=="")
    {
        alert("Error code, does not exist!");
        $("txtcode").readonly="false";
        $("fbtn").style.display="block";
        return;
    }
    else
    {
        $("ondiv").style.display="none";
        $("indiv").style.display="block";
        $("uname").innerHTML=result.split("|||")[0];
        var cEmail=result.split("|||")[1];
        setCookie("friend",cEmail,3)
    }
}
