/*
用户登陆，注销脚本
被 /inc/top.asp 引用
*/

//等待提示
var progress_timeout_obj=null;
function inProgress(state,msg)	//设置等待状态
{
	if(!msg){var msg='处理中,请稍候...';}
	document.getElementById('loadingtips').innerHTML=msg;
	var progressdiv=document.getElementById('progress_div');
	if(state)
	{
		progressdiv.style.top=130+document.documentElement.scrollTop;
		progressdiv.style.left=-200;
		progressdiv.style.display='';
		moveInProgress();
	}else{
		if(progress_timeout_obj){
			clearTimeout(progress_timeout_obj);
			progressdiv.style.left=document.body.clientWidth/2-100;
		}
		progress_timeout_obj=setTimeout("moveOutProgress()",3000);
	}
}

function moveInProgress(){	//等待提示出现
	var progressdiv=document.getElementById('progress_div');
	if(progressdiv.getBoundingClientRect().left<document.body.clientWidth/2-100){
		progressdiv.style.left=progressdiv.getBoundingClientRect().left+50;
		clearTimeout(progress_timeout_obj);
		progress_timeout_obj=setTimeout("moveInProgress()",10);
	}
}

function moveOutProgress(){//等待提示消失
	var progressdiv=document.getElementById('progress_div');
	if(progressdiv.getBoundingClientRect().left>0){
		progressdiv.style.left=progressdiv.getBoundingClientRect().left-50;
		progress_timeout_obj=setTimeout("moveOutProgress()",10);
	}else{
		progressdiv.style.display='none';
		clearTimeout(progress_timeout_obj);
	}
}

function loading_tips(isloading){
	var sys_loadingbar=document.getElementById('loaing_tip');
	if(isloading){
		sys_loadingbar.style.display="";
		sys_loadingbar.style.top=document.documentElement.scrollTop+100;
	}else{
		sys_loadingbar.style.display="none";	
	}
}

function ShowMessage(message,delay){	//预定义信息提示函数，便于将来统一修改
	var progressdiv=document.getElementById('progress_div');
	progressdiv.style.left=document.body.clientWidth/2-100;
	progressdiv.style.top=130+document.documentElement.scrollTop;
	progressdiv.style.display='';
	document.getElementById('loadingtips').innerHTML=message;
	if(delay>0){
		progress_timeout_obj=setTimeout("moveOutProgress()",delay);
	}else{
		progress_timeout_obj=setTimeout("moveOutProgress()",2000);
	}
}

function login(type)
{   
	//验证登陆信息是否有效
	var login_name=document.getElementById('login_name').value;
	var login_password=document.getElementById('login_password').value;
	
	if(login_name==''&&type!=0){
		alert('请填写会员名称！');
		return;
	}
	if(login_password==''&&type!=0){
		alert('请填写登陆密码！');
		return;
	}

	//提交登陆验证  
	var ajaxRequest = false;  
    if(window.XMLHttpRequest)   
    {   
        ajaxRequest = new XMLHttpRequest();   
    }   
    else if(window.ActiveXObject)   
    {   
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");   
    }   
    if (!ajaxRequest){
      alert("您的浏览器不支持AJAX，请更新浏览器到IE6以上版本!");
	  return;
	}
	
	inProgress(true,'正在验证用户信息，请稍后...');
	
	var params="type="+type+"&username="+escape(login_name)+"&password="+escape(login_password);
	//alert(params);
	ajaxRequest.open("POST","/system/userlogin.asp", true);
	ajaxRequest.onreadystatechange=function(){
		if(ajaxRequest.readyState != 4){return}
		if(ajaxRequest.status == 200){
			var login_result=ajaxRequest.responseText;
		}else{
			var login_result='登陆失败，请联系管理员！'+ajaxRequest.status;
		}
		switch(parseInt(login_result)){
		case 1:{	//成功
				if(type!=0){
					ShowUserStatus(login_name);
					inProgress(false,'登陆成功，欢迎您使用淘友吧！');
				}else{
					ShowLoginDiv();
					inProgress(false,'再见，欢迎您下次光临！');
				}
				break;
			}
		case 2:{	//登陆密码错误
				inProgress(false,'用户密码错误');
				break;
			}
		case 3:{	//用户名不存在
				inProgress(false,'用户名不存在');
				break;
			}
		default:{
				inProgress(false,login_result);
			}
		}
	}
	ajaxRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajaxRequest.send(params);
}

function login(type){
	var loginjsObj=document.getElementById('loginjs');
	if(!loginjsObj){
		alert('无法连接脚本登陆对象');
		return;
	}
	
	//验证登陆信息是否有效
	var login_name=document.getElementById('login_name').value;
	var login_password=document.getElementById('login_password').value;
	
	if(login_name==''&&type!=2){
		alert('请填写会员名称！');
		return;
	}
	if(login_password==''&&type!=2){
		alert('请填写登陆密码！');
		return;
	}
	loginjsObj.src='/system/UserLogin.asp?un='+escape(login_name)+'&pw='+escape(login_password)+'&type='+type;
	inProgress(true,'正在验证用户信息，请稍后...');

}

function logincallback(type,sta){
	switch(sta){
	case 1:{	//成功
			if(type!=2){
				ShowUserStatus(document.getElementById('login_name').value);
				inProgress(false,'登陆成功，欢迎您使用淘友吧！');
			}else{
				ShowLoginDiv();
				inProgress(false,'再见，欢迎您下次光临！');
			}
			break;
		}
	case 2:{	//登陆密码错误
			inProgress(false,'用户密码错误');
			break;
		}
	case 3:{	//用户名不存在
			inProgress(false,'用户名不存在');
			break;
		}
	default:{
			inProgress(false,sta);
		}
	}
}
function ShowUserStatus(username)
{
	var welcome ='欢迎光临!';
	var nowDate = new Date();
	var nowTime = nowDate.getHours();　
	if ((nowTime>6)&&(nowTime<=9)) welcome=	"早啊！今天生意一定很火！ ";
	if ((nowTime>9)&&(nowTime<=11)) welcome=	"上午好！新货都上架了吗？";
	if ((nowTime>11)&&(nowTime<=12)) welcome= 	"午餐想好吃什么了吗？。";
	if ((nowTime>12)&&(nowTime<=17)) welcome=	"下午最多买家在挖宝呢！。";
	if ((nowTime>17)&&(nowTime<=21)) welcome= 	"晚上好！今天的成绩还行吧？";
	if ((nowTime>21)&&(nowTime<=23)) welcome= 	"夜深了，要注意身体哦。";
	if ((nowTime>=0)&&(nowTime<=6)) welcome=	"早点睡，明天还要继续努力！";

	welcome+='<br><a href="/center/">[进入会员中心]</a>&nbsp;<a href="javascript:login(2)">[退出]</a>';
	document.getElementById('logined_div').innerHTML="<strong>"+username+"</strong>,"+welcome;
	document.getElementById('logined_div').style.display='';
	document.getElementById('login_div').style.display='none';
}

function ShowLoginDiv(){
	document.getElementById('logined_div').innerHTML="";
	document.getElementById('logined_div').style.display='none';
	document.getElementById('login_div').style.display='';
	document.getElementById('login_password').value="";
}

function top_bnt_tips(tips){
	document.getElementById('top_tips_div').innerHTML=tips;
}