var user_id;
var url;
var startTime,endTime;

var d=new Date();
startTime=d.getTime();

function IsLogin()
{
    if(user_id=="")
    {
        alert("登陆后才能进入购物车. . .");
        userlogin();
    }
    else
    {
        location.href='/user/mycart.aspx'; 
    }
}
function IsUserLogin()
{
    if(user_id=="")
    {
        alert("登陆后才能进入我的帐户. . .");
        userlogin();
    }
    else
    {
        location.href='/user/waitpay.aspx'; 
    }
}

function $(name) {
	return document.getElementById(name);
}
function $C(name) {
	return document.createElement(name);
}
function login()
{
    var email=$("email").value;
    var pass=$("pass").value;
    if(email=="")
    {
        $("msg").innerHTML="<font color='#c00000' font-size='1px'><b>×</b>请输入帐户名称！</font>";
    }
    else if(pass=="")
    {
        $("msg").innerHTML="<font color='#c00000' font-size='1px'><b>×</b>请输入密码！</font>";
    }
    else
    {
        $("msg").innerHTML="<font color='#3399ff' font-size='1px'>请稍候，系统正在验证密码 . . .</font>";
        window.setTimeout("CheckLogin()",1000);
    }
    
}

function CheckLogin()
{
    var email=$("email").value;
    var pass=$("pass").value;
    var str=AjaxProTop.CheckLogin(email,pass).value;
    switch(str)
    {
        case "0" : 
            $("msg").innerHTML="<font color='#c00000' font-size='1px'><b>×</b>帐户名称或密码错误！</font>";
            break;
        case "1" : 
            $("msg").innerHTML="<font color='#3399FF' font-size='1px'><b>√</b>登陆成功！</font>";
            GetUrl();
            break;
        case "2" : 
            $("msg").innerHTML="<font color='#c00000' font-size='1px'><b>×</b>帐户没有激活！请登陆邮箱:"+email+"激活帐户！</font>";
            break;
        case "3" :
            $("msg").innerHTML="<font color='#c00000' font-size='1px'><b>×</b>服务器配置错误！</font>";
            break;
        default : 
            $("msg").innerHTML="<font color='#c00000' font-size='1px'><b>×</b>未知错误！</font>";
            break;
    }
}

function GetUrl()
{
    window.setTimeout("window.location.href='"+url+"';",2000);
}

function regedit()
{
    var regeditemail=$("regeditemail").value;
    var regeditpass=$("regeditpass").value;
    var regeditpass1=$("regeditpass1").value;
    var sReg = /[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/;
    if(!sReg.test(regeditemail))
    {
        $("regeditmsg").innerHTML="<font color='#c00000' font-size='1px'><b>×</b>请使用邮箱作为帐户名称！</font>";
    }
    else
    {
        if(regeditpass!=regeditpass1)
        {
            $("regeditmsg").innerHTML="<font color='#c00000' font-size='1px'><b>×</b>两次输入的密码不相等！</font>";
        }
        else
        {
            $("regeditmsg").innerHTML="<font color='#3399ff' font-size='1px'>请稍候，系统准备完成注册 . . .</font>";
            window.setTimeout("finishRegedit()",0);
            
        }
    }
}

function finishRegedit()
{
    var regeditemail=$("regeditemail").value;
    var regeditpass=$("regeditpass").value;
    var str=AjaxProTop.UserRegeditOK(regeditemail,regeditpass).value;
    if(str=="-1")
    {
        $("regeditmsg").innerHTML="<font color='#c00000' font-size='1px'><b>×</b>邮箱已存在，请不要重复注册！</font><a href='/user/password.aspx' style='color:#00ff00' target='_self'>找回密码？</a>";
    }
    if(str=="0")
    {
        $("regeditmsg").innerHTML="<font color='#c00000' font-size='1px'><b>×</b>注册失败！</font>";
    }
    if(str=="1")
    {
        $("regeditmsg").innerHTML="<font color='#3399FF' font-size='1px'><b>√</b>注册成功!请登陆邮箱:"+regeditemail+"激活帐户！</font>";
    }
    if(str=="-2")
    {
        $("regeditmsg").innerHTML="<font color='#c00000' font-size='1px'><b>×</b>激活链接发送失败！</font>";
    }
}

function ChechEmailLogin()
{
    //document.getElementById("mail").innerHTML=AjaxProTop.Email(document.getElementById("email").value).value;
    var sReg = /[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/; 
    if (!sReg.test(document.getElementById("email").value))
    {
        document.getElementById("mail").innerHTML="<font color='#c00000'><b>×</b>邮箱格式错误</font>";
    }
    else
    {
        document.getElementById("mail").innerHTML="<font color='#66FF00'><b>√</b>邮箱格式正确</font>";
    }
}

function ChechEmailregedit()
{
    document.getElementById("regeditmail").innerHTML=AjaxProTop.Email(document.getElementById("regeditemail").value).value;
}

function userlogin(obj) 
{
    if(user_id=="")
    {
        var searchwin = $("searchwin");
        if (!searchwin) {
    	
            searchwin = $C("div");
            searchwin.id = "searchwin";
            document.body.appendChild(searchwin);
    		
            var searchbody = $C("div");
            searchbody.id = "searchbody";
            searchbody.style.position = "absolute";
            searchbody.style.top = document.documentElement.scrollTop + "px";
            searchwin.appendChild(searchbody);
    		
            var searchbg = $C("div");
            var ss = searchbg.style;
            ss.width = "486px";
            ss.height = "100%";
            ss.position = "absolute";
		    ss.backgroundColor="#ffffff";
            searchbody.appendChild(searchbg);
    		
            var searchform = $C("div");
            searchform.id = "searchform";
            searchform.style.backgroundColor="#ffffff";
            searchform.style.zIndex=1;
            var tableStr="<div style='width:500px; background-image:url(/images/top/login.gif); background-color:#fff;padding-top:15px; height:214px;-moz-opacity:0.95; filter:alpha(opacity=95);position:absolute;   margin:2px;'><div style='float:left;  width:200px; height:129px;'><p style='width:360px; text-align:center; height:40px;line-height:30px; margin-left:25px;'><span id='msg'></span></p><div style='width:235px; height:150px; margin-left:150px;'><p style='color:#006EC8; margin-bottom:20px;'>帐　户：<input id='email' style='width:140px; height:18px;' type='text' name='textfield' /><span id='mail'></span></p><p style=' color:#006EC8;'>密　码：<input id='pass' style='width:140px; height:18px;' type='password' name='textfield2' /></p><p style='margin:10px 0 0 10px;'><input type='button' onclick='login()' value='登&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;陆' style=' width:100px; height:27px; border:none; background-image:url(/images/top/menu.gif); color:#fff;  padding-left:5px; margin-left:35px; cursor:pointer; '/></p><p style='padding:10px 0 5px 40px;'><a href='/user/password.aspx' target='_self' style='font-size:12px; color:#006EC8;  margin-top:5px; margin-left:5px; '>找回密码？</a></p></div></div><div style=' float:right; width:108px; height:200px;'><p style='cursor:pointer; width:65px; height:25px;color:#fff;margin:8px 0 0 50px; ' ><a onclick='cancelsearch()'>退出</a></p><p style=' margin-top:100px; color:#006EC8; font-weight:bold;'><a style='cursor:pointer; ' onclick='userregedit()'> 您还没注册？</a></p></div></div>";
            searchform.innerHTML = tableStr;
            searchbody.appendChild(searchform);
    		
        } else {
            var ss = $("searchbody").style;
            ss.display = "block";
            ss.left = "";
            ss.top = document.documentElement.scrollTop + "px";
        }
        topno = 10;
        toptimer = window.setInterval(function(){setsearchtop(30,true);},10);
        return false;
    }    
    else
    {
        alert("您已经登陆了！");
    }
}

function userregedit()
{
    if(user_id=="")
    {
        var searchwin = $("regeditwin");
        if (!searchwin) {
    	
            searchwin = $C("div");
            searchwin.id = "regeditwin";
            document.body.appendChild(searchwin);
    		
            var searchbody = $C("div");
            searchbody.id = "regeditbody";
            searchbody.style.position = "absolute";
            searchbody.style.top = document.documentElement.scrollTop + "px";
            searchwin.appendChild(searchbody);
    		
            var searchbg = $C("div");
            var ss = searchbg.style;
            ss.width = "486px";
            ss.height = "100%";
            ss.position = "absolute";
		    ss.backgroundColor="#ffffff";
            searchbody.appendChild(searchbg);
    		
            var searchform = $C("div");
            searchform.id = "regeditform";
            searchform.style.backgroundColor="#ffffff";
            searchform.style.zIndex=2;
            searchform.innerHTML = "<div style='width:500px; background-image:url(/images/top/regedit.gif); background-color:#fff; height:230px;-moz-opacity:0.95; filter:alpha(opacity=95);position:absolute;   margin:2px;'><div style='width:353px; height:200px; padding-left:150px; margin-top:23px; '><div><div style='cursor:pointer;  width:65px; height:25px; float:right;color:#fff;' ><a onclick='cancelregedit()'>退出</a></div><div style='margin-top:50px;vertical-align:top;'><p style='color:#006EC8; margin-bottom:5px;'>帐户名称：<input id='regeditemail' onkeyup='ChechEmailregedit()' style='width:160px;height:16px;' type='text' name='textfield' /><span id='regeditmail'></span></p><p style='color:#006EC8; margin-bottom:5px;'>密　　码：<input id='regeditpass' style='width:160px; height:16px;' type='password' name='textfield2' /></p><p style='color:#006EC8;margin-bottom:5px; margin-left:0px;'>确认密码：<input id='regeditpass1' style='width:160px; height:16px;' type='password' name='textfield2' /></p><p style='height:20px;text-align:left;'><span id='regeditmsg'></span></p><p ><input type='button' value='注&nbsp;&nbsp;&nbsp;&nbsp;册' onclick='regedit();' style='width:100px; height:27px; background-image:url(/images/top/menu.gif); float:left; border:none;color:#fff; margin-left:65px; cursor:pointer; '></p></div></div></div>";
            searchbody.appendChild(searchform);
    		
        } else {
            var ss = $("regeditbody").style;
            ss.display = "block";
            ss.left = "";
            ss.top = document.documentElement.scrollTop + "px";
        }
        topno = 10;
        toptimer = window.setInterval(function(){setregedittop(30,true);},10);
        return false;
    }
    else
    {
        alert("请不要重复注册！")
    }
}

var msearch = false;
var sx,sy,ox,oy;
var topno = 0;
var toptimer;
var startf = true;
function cancelsearch() {
    topno = 10;
    startf = true;
    toptimer = window.setInterval(function(){setsearchtop(30,false);},10);
    return false;
}
function cancelregedit() {
    topno = 10;
    startf = true;
    toptimer = window.setInterval(function(){setregedittop(30,false);},10);
    return false;
}
function setsearchtop(addtop,dis) {
    var ss = $("searchbody").style;
    if (topno == 0) {
        window.clearInterval(toptimer);
        if (!dis)
            ss.display = "none";
        else
            ss.top = parseInt(ss.top) - addtop * 1.5 + "px";
    }
    if (!dis && startf) {
        ss.top = parseInt(ss.top) - addtop + "px";
        startf = false;
    } else {
        ss.top = parseInt(ss.top) + addtop + "px";
    }
    topno --;
}
function setregedittop(addtop,dis) {
    var ss = $("regeditbody").style;
    if (topno == 0) {
        window.clearInterval(toptimer);
        if (!dis)
            ss.display = "none";
        else
            ss.top = parseInt(ss.top) - addtop * 1.5 + "px";
    }
    if (!dis && startf) {
        ss.top = parseInt(ss.top) - addtop + "px";
        startf = false;
    } else {
        ss.top = parseInt(ss.top) + addtop + "px";
    }
    topno --;
}
function showManagerOver()
{
    if(user_id!="")
    {
        $("usermanager").innerHTML="<div style='width:180px;height:25px;background-image:url(/images/clallmenu.gif);margin-top:4px;'><div style='margin-top:6px;'>&nbsp;&nbsp;<a href='/user/myaddress.aspx'>收货地址</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='/user/alterpwd.aspx' target='_self'>修改密码</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='/user/logout.aspx' target='_self'>注销登陆</a></div></div>";
    }
}
function shoManagerOut()
{
    window.setTimeout("$('usermanager').innerHTML='';",3000);
}

//弹出层
function openLayer(objId,conId)
{
    var arrayPageSize = getPageSize();//调用getPageSize()函数
    var arrayPageScroll = getPageScroll();//调用getPageScroll()函数
    if (!document.getElementById("popupAddr"))
    {
        //创建弹出内容层
        var popupDiv = document.createElement("div");

        //给这个元素设置属性与样式
        popupDiv.setAttribute("id","popupAddr")
        popupDiv.style.position = "absolute";
        popupDiv.style.border = "1px solid #ccc";
        popupDiv.style.background = "#fff";
        popupDiv.style.zIndex = 99;



        //创建弹出背景层
        var bodyBack = document.createElement("div");
        bodyBack.setAttribute("id","bodybg")
        bodyBack.style.position = "absolute";
        bodyBack.style.width = arrayPageSize[0];
        bodyBack.style.height = (arrayPageSize[1] + 35 + 'px');
        bodyBack.style.zIndex = 98;
        bodyBack.style.top = 0;
        bodyBack.style.left = 0;

        //bodyBack.style.filter = "alpha(opacity=90)";
        //bodyBack.style.opacity = 0.5;
        //bodyBack.style.background = "#ddf";

        //实现弹出(插入到目标元素之后)
        var mybody = document.getElementById(objId);
        insertAfter(popupDiv,mybody);//执行函数insertAfter()
        insertAfter(bodyBack,mybody);//执行函数insertAfter()
    }

    //显示背景层
    document.getElementById("bodybg").style.display = "";
    //显示内容层
    var popObj=document.getElementById("popupAddr")
    popObj.innerHTML = document.getElementById(conId).innerHTML;
    popObj.style.display = "";
    //让弹出层在页面中垂直左右居中(统一)
    popObj.style.width = "370px";
    popObj.style.height = "185px";
    popObj.style.top = 200 + 'px';
    popObj.style.left = arrayPageSize[0] / 2-210 + 'px';
    //让弹出层在页面中垂直左右居中(个性)
//    var arrayConSize=getConSize(conId)
//    popObj.style.top = arrayPageScroll[1] + (arrayPageSize[3] - arrayConSize[1]) / 2-50 + 'px';
//    popObj.style.left = (arrayPageSize[0] - arrayConSize[0]) / 2 -30 + 'px';
}

//获取内容层内容原始尺寸
function getConSize(conId)
{
    var conObj=document.getElementById(conId)
    conObj.style.position = "absolute";
    conObj.style.left=-1000+"px";
    conObj.style.display="";
    var arrayConSize=[conObj.offsetWidth,conObj.offsetHeight]
    conObj.style.display="none";
    return arrayConSize;
}

function insertAfter(newElement,targetElement)
{//插入
    var parent = targetElement.parentNode;
    if(parent.lastChild == targetElement)
    {
        parent.appendChild(newElement);
    }
    else
    {
        parent.insertBefore(newElement,targetElement.nextSibling);
    }
}

//获取滚动条的高度
function getPageScroll()
{
    var yScroll;
    if (self.pageYOffset) 
    {
        yScroll = self.pageYOffset;
    } 
    else if (document.documentElement && document.documentElement.scrollTop)
    {
        yScroll = document.documentElement.scrollTop;
    } 
    else if (document.body) 
    {
        yScroll = document.body.scrollTop;
    }

    arrayPageScroll = new Array('',yScroll)
    return arrayPageScroll;
}

//获取页面实际大小
function getPageSize()
{
    var xScroll,yScroll;

    if (window.innerHeight && window.scrollMaxY)
    {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } 
    else if (document.body.scrollHeight > document.body.offsetHeight)
    {
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } 
    else 
    {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth,windowHeight;
    //var pageHeight,pageWidth; 
    if (self.innerHeight) 
    {
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } 
    else if (document.documentElement && document.documentElement.clientHeight) 
    {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } 
    else if (document.body) 
    {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    var pageWidth,pageHeight
    if(yScroll < windowHeight)
    {
        pageHeight = windowHeight;
    } 
    else 
    {
        pageHeight = yScroll;
    }
    if(xScroll < windowWidth) 
    {
        pageWidth = windowWidth;
    } 
    else 
    {
        pageWidth = xScroll;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
    return arrayPageSize;
}
//关闭弹出层
function closeLayer()
{
    document.getElementById("popupAddr").style.display = "none";
    document.getElementById("bodybg").style.display = "none";
    return false;
}

