﻿//Ajax核心引擎

//本项目组版权所有

var xmlHttp; //声明XMLHttpRequest对象

function createXMLHTTP() {
    if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest(); //mozilla浏览器
    }
    else if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //IE老版本
        }
        catch (e)
        { }
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE新版本
        }
        catch (e)
        { }
        if (!xmlHttp) {
            window.alert("AJAX初始化失败");
            return false;
        }
    }
}

//-----------------------------------------------------------------------------------------------------------------------

function isemail(strg) {
    if (!(strg.indexOf("@") > 3 && strg.indexOf(".") > 1)) return false;
    if (strg.indexOf("@", strg.indexOf("@") + 1) > 0) return false;
    var strarray = new Array("@@", "@.", "..")
    for (i = 0; i < strarray.length; i++)
        if (strg.indexOf(strarray[i]) > 0) return false;
    for (i = 0; i < strg.length; i++)
        if (strg.substr(i, 1) <= "," || strg.substr(i, 1) == "/" || (strg.substr(i, 1) >= "[" && strg.substr(i, 1) <= "") || strg.substr(i, 1) == "`" || (strg.substr(i, 1) >= ":" && strg.substr(i, 1) <= "?") || strg.substr(i, 1) >= "{") return false;
    return true
}  


//登录
function Login() {
    if (document.getElementById("userName").value == "") {
        Growl.Smoke({ title: '请输入用户名', text: 'Please input the username.<br />' });
    return false;
    }
    if (document.getElementById("userPwd").value == "") {
        Growl.Smoke({ title: '请输入密码', text: 'Please input the password.<br />' });
    return false;
    }
    createXMLHTTP(); //创建XMLHttpRequest对象
    var url = "JSEtoClS.aspx?Username=" + document.getElementById("userName").value + "&Pwd=" + document.getElementById("userPwd").value + "&Event=Login";
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = userLogin;
    xmlHttp.send(null);
}
//登陆回调函数
function userLogin() {
    if (xmlHttp.readyState == 4)//判断对象状态
    {
        if (xmlHttp.status == 200)//信息成功返回，开始处理信息
        {
            if (xmlHttp.responseText == "true")//获取的Response.Write("true");里面的文本
            {

                mySlide.slideOut();

                Growl.Smoke({ title: '登录成功', text: 'Login Success!<br />' });
 
                
                //---------------------用JS向cookie写入里的name值---------------

                var e = new Date();
                e.setTime(e.getTime() +  1000 * 60 * 60 * 24 * 30);

                document.cookie = "NIPOONVOICE" + "=" + encodeURI(document.getElementById("userName").value)  + "; path=" + "/" + "; expires=" + e.toGMTString();
               

                //---------------------清空登录框---------------              
                //document.getElementById("userName").value = "";
                //document.getElementById("userPwd").value = "";

                self.location = "AllMusic.aspx";　
            }
            else {
                Growl.Smoke({ title: '对不起，用户名或者密码错误', text: 'The username or password <br /> is not correct.' });
                //document.getElementById("userName").value = "";
                document.getElementById("userPwd").value = "";
                
            }
        }
    }
}

//-----------------------------------------------------------------------------------------------------------------------


//注册新用户
function regUser() {


    if (document.getElementById("reg_userName").value == "") {
        Growl.Smoke({ title: '请输入Email', text: 'Please input your Email as the username.' });
        return false;
    }
    
    if (!isemail(document.getElementById("reg_userName").value)) {
        Growl.Smoke({ title: 'Email格式不正确', text: 'Illegal Email.' });
        return false;
    }

    if (document.getElementById("reg_userPwd").value == "") {
        Growl.Smoke({ title: '请输入密码', text: 'Please input the password.' });
        return false;
    }
 
    if (document.getElementById("reg_userPwd").value.length < 5 ) {
        Growl.Smoke({ title: '密码长度不能少于5位', text: 'Need your password length more then 5.' });
        return false;
    }
   
    createXMLHTTP(); //创建XMLHttpRequest对象
    var url = "JSEtoClS.aspx?Username=" + document.getElementById("reg_userName").value + "&Pwd=" + document.getElementById("reg_userPwd").value + "&Event=Reg";

    xmlHttp.open("get", url, true);
    xmlHttp.onreadystatechange = regUserInfo;
    xmlHttp.send(null);

}

//注册用户回调函数
function regUserInfo() {
    if (xmlHttp.readyState == 4)//判断对象状态
    {
        if (xmlHttp.status == 200)//信息成功返回，开始处理信息
        {
            if (xmlHttp.responseText == "true")//获取的Response.Write("true");里面的文本
            {
                TB_remove();
                Growl.Smoke({ title: 'Success', text: '恭喜，注册成功' });
                
            }
            else {
                Growl.Smoke({ title: 'Error', text: '对不起，用户名已存在' });
                document.getElementById("reg_userName").value = "";
                document.getElementById("reg_userPwd").value = "";
                document.getElementById("reg_Email").value = "";
            }
        }
    }
}

//-----------------------------------------------------------------------------------------------------------------------


//上传显示
function Upload_show() {

    if (document.getElementById('FileUpload1').value == "") {
        alert('上传文件不能为空！');
        return false;
    }
    else if (document.getElementById('TextBox_Name').value == "") {
        alert('音乐名称不能为空！');
        return false;
    }
    else if (Len(document.getElementById('TextBox_Name').value)>30) {
        alert('音乐名称不能超过30个字！');
        return false;
    }
    
    parent.document.getElementById('light').style.display = 'block';
    parent.document.getElementById('fade').style.display = 'block';
    
    return true;
}

function Len(str) {
    var i, sum;
    sum = 0;
    for (i = 0; i < str.length; i++) {
        if ((str.charCodeAt(i) >= 0) && (str.charCodeAt(i) <= 255))
            sum = sum + 1;
        else
            sum = sum + 2;
    }
    return sum;
}

//-----------------------------------------------------------------------------------------------------------------------

