﻿// JScript 文件

function $(obj){return document.getElementById(obj);}


//类似于VB中的Trim，去掉空格。调用：obj.Trim()
//类似于VB中的Trim，去掉空格。调用：obj.Trim()
String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function()
{
    return this.replace(/(^\s*)/g, "");
}
String.prototype.Rtrim = function()
{
    return this.replace(/(\s*$)/g, "");
}
String.prototype.UrlEncode = function()
{
	var str = this;
	str = str.replace(/./g,function(sHex)
	{
		window.EnCodeStr = "";
		window.sHex = sHex;
		window.execScript('window.EnCodeStr=Hex(Asc(window.sHex))',"vbscript");
		return window.EnCodeStr.replace(/../g,"%$&");
	});
	return str;
}

function isEmail(strEmail) {
    if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}
function gourl(sUrl){window.location.href=""+sUrl+"";}


var sStr='<span style="color:#F00;"><img src=../img/error.gif border=0> ';
var eStr='</span>';
var okStr='<img src=../img/ok.gif border=0> ';
var vReg=true;

function ajaxData(m,posturl,data,callerror,callok)
{
	var req=(window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
	//data=data+"&m="+Math.random();
	if (m=="1"){
		req.open("POST",posturl,true);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.setRequestHeader("CharSet","GB2312");
		req.send(data);
	}else{
		req.open("GET",posturl+"?"+data,true);
		req.send(null);
	}
	req.onreadystatechange = function() {
		if (req.readyState == 4 && req.status == 200) 
		{
			var reqtext=req.responseText;
			if(isNaN(reqtext))
			{
				altGoto(reqtext,"");
				$("userCenter").focus();
				vReg=false;
			}
			else
			{
				if(reqtext=="0")
				{
					eval(callerror);
					vReg=false;
				}
				else
				{
					eval(callok);
				}
			}
		}
	}
}

function chkUser()
{
	if($("vipname").value.Trim()=="")
	{
		$("reg_1").innerHTML=sStr+"请输入用户名！"+eStr;vReg=false;
	}
	else
	{
		if($("vipname").value.Trim().length>15 || $("vipname").value.Trim().length<6)
		{
			$("reg_1").innerHTML=sStr+"用户名长度在6-15位之间！"+eStr;vReg=false;
			
		}
		else
		{
			if($("vipname").value.search(/^\w+$/) != -1)
			{
				ajaxData(1,"CheckUserName.aspx","Uname="+escape($("vipname").value),"$(\"reg_1\").innerHTML='"+sStr+$("vipname").value+" 已经被注册！"+eStr+"';","$(\"reg_1\").innerHTML='"+okStr+"';");
			}
			else
			{
				$("reg_1").innerHTML=sStr+"用户名由字母、数字、下划线组成！"+eStr;vReg=false;
			}
		}
	}
}

function chkPwd()
{
	if($("vippassword").value=="")
	{
		$("reg_2").innerHTML=sStr+"请输入密码！"+eStr;vReg=false;
	}else{
		if($("vippassword").value.length<6 || $("vippassword").value.length>20){
			$("reg_2").innerHTML=sStr+"密码长度在6-20位之间"+eStr;vReg=false;
		}else{
			$("reg_2").innerHTML=okStr;
			if($("vippasswordb").value!=$("vippassword").value)
			{
				$("reg_3").innerHTML=sStr+"两次输入的密码不相同！"+eStr;vReg=false;
			}else{
				$("reg_3").innerHTML=okStr;
			}
		}
	}	
}


function chkEmail()
{
	if(!isEmail($("vipemail").value.Trim()))
	{
		$("reg_9").innerHTML=sStr+"电子邮件格式不正确！"+eStr;vReg=false;
	}else{
		$("reg_9").innerHTML=okStr;
		//ajaxData(1,"/chk.asp","Act=chkIsReg&c=rEmail&w="+escape($("rEmail").value),"$(\"reg_9\").innerHTML='"+sStr+"电子邮件已经被使用，请换一个！"+eStr+"';","$(\"reg_9\").innerHTML='"+okStr+"';");
	}
}

function chkAnswer()
{
   if($("vipanswer").value=="")
   {
     $("reg_8").innerHTML=sStr+"请输入问题答案！"+eStr;vReg=false;
   }
   else
   {
		$("reg_8").innerHTML=okStr;
	}
}

function chkAddr()
{
  if($("vipaddr").value=="")
  {
     $("reg_5").innerHTML=sStr+"请输入详细地址！"+eStr;vReg=false;
  }
  else
  {
     if($("vipaddr").value.length<5)
     {
       $("reg_5").innerHTML=sStr+"地址太简单！"+eStr;vReg=false;
     }
     else
     {
      $("reg_5").innerHTML=okStr;
     }
  }
}





function chkTel()
{
		if($("vipmobile").value.Trim()!="")
		{
		  if($("vipmobile").value.Trim().length=11)
		  {
			if($("vipmobile").value.search(/^0{0,1}(13[0-9]|15[7-9]|15[0-2]|18[7-8])[0-9]{8}$/) != -1){
				$("reg_7").innerHTML=okStr;
			}
			else
			{
				$("reg_7").innerHTML=sStr+"手机格式不正确！格式: 13000000000"+eStr;vReg=false;
			}
          }
          else
          {$("reg_7").innerHTML="格式: 13000000000";}
		}
		else
		{$("reg_7").innerHTML=sStr+"请输入手机号"+eStr;vReg=false;}

	
}

function chkQQ()
{
   if($("vipqq").value!="")
   {
      if(!isNaN($("vipqq").value))
      {
        if($("vipqq").value.length>4&&$("vipqq").value.length<12)
        {
          
          $("reg_5").innerHTML=okStr;
        }
        else
        {
           $("reg_5").innerHTML=sStr+"请输入正确的QQ号码！"+eStr;vReg=false;
        }
      }
      else
      {
           $("reg_5").innerHTML=sStr+"请输入数字！"+eStr;vReg=false;
      }
   }

}


function chkReg()
{
	vReg=true;
	chkUser();chkPwd();chkAnswer();chkEmail();chkAddr();chkTel();chkQQ();
	
	if(vReg==true){
		$("reg_submit").disabled=true;
		var req=(window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
		var data="rUser="+escape($("vipname").value)+"&rPwd="+escape($("vippassword").value)+"&rQuest="+escape($("vipquestion").value)+"&rAnsw="+escape($("vipanswer").value)+"&rEmail="+escape($("vipemail").value)+"&rAddr="+escape($("vipaddr").value)+"&rTel="+escape($("vipmobile").value)+"&rQQ="+escape($("vipqq").value);
		
		req.open("POST","ureg.aspx",true);
	
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.setRequestHeader("CharSet","GB2312");
		req.send(data);
			
		req.onreadystatechange = function() { 
			//$("testMsg").value=unescape(req.responseText);
			if (req.readyState == 4 ) {
				var reqtext=req.responseText;
				 
				if(reqtext=="注册成功"){
					altGoto("注册成功，点击“<a href=\"/Vlogin.aspx\">确认</a>”立即登陆。<br>注：你可以尽情的享受网上购物的乐趣！","/Vlogin.aspx");
					$("tmpfocus").focus();
				}else{
					altGoto(reqtext,"");
					$("tmpfocus").focus();
					$("reg_submit").disabled=false;
				}
			}
		}
	}
}

function chkLogin(gType,bUrl)
{
	var rUser;
	var rPwd;
	var backStr;
	if(gType=="1"){
		rUser=$("lo_user").value;
		rPwd=$("lo_pwd").value;
		backStr="尊贵的会员<font color=white>"+escape(rUser)+"</font>您好！&nbsp;&nbsp;<a href=Basket.aspx?mode=view title=查看我的购物车 target=_blank>我的购物车</a>&nbsp;&nbsp;<a href=user/index.aspx >会员中心</a>　<a href=# onclick=LoginOut()>退出登录</a> ";
		ajaxData(1,"/ulogin.aspx","act=login&rUser="+escape(rUser)+"&rPwd="+escape(rPwd),"","$('userCenter').innerHTML='"+backStr+"';");
	}else{
		rUser=$("login_user").value;
		rPwd=$("login_pwd").value;
		rCode=$("login_code").value;
		if(bUrl!=""){backStr="gourl(\""+bUrl+"\");";}else{backStr="gourl(\"/user/\");";}
		ajaxData(1,"/ulogin.aspx","rUser="+escape(rUser)+"&rPwd="+escape(rPwd),"",backStr);
	}
}

function LoginOut()
{
	var backStr="用户名:<input type=text name=lo_user id=lo_user class=l_input maxlength=15> 密码:<input type=password name=lo_pwd id=lo_pwd class=l_input maxlength=20> <input type=submit value=登陆 class=btn41 onclick=chkLogin(1,1)> <a href=/user/reg.aspx>免费注册</a>";
	ajaxData(1,"ulogin.aspx","act=LoginOut","","$('userCenter').innerHTML='"+backStr+"';");
}



var isCreate=false;
function altGoto(msg,mUrl){
	if(isCreate==true){return;}
	//var eSrc=(document.all)?window.event.srcElement:arguments[1];
	var shield = document.createElement("DIV");
	shield.id = "shield";
	shield.style.position = "absolute";
	shield.style.left = "0px";
	shield.style.top = "0px";
	shield.style.width = "100%";
	shield.style.height = ((document.documentElement.clientHeight>document.documentElement.scrollHeight)?document.documentElement.clientHeight:document.documentElement.scrollHeight)+"px";
	shield.style.background = "#333";
	shield.style.textAlign = "center";
	shield.style.zIndex = "10000";
	shield.style.filter = "alpha(opacity=0)";
	shield.style.opacity = 0;
	var alertFram = document.createElement("DIV");
	alertFram.id="alertFram";
	alertFram.style.position = "absolute";
	alertFram.style.left = "50%";
	alertFram.style.top = "50%";
	alertFram.style.marginLeft = "-225px" ;
	alertFram.style.marginTop = -75+document.documentElement.scrollTop+"px";
	alertFram.style.width = "450px";
	alertFram.style.height = "120px";
	alertFram.style.background = "#ccc";
	alertFram.style.textAlign = "center";
	alertFram.style.lineHeight = "120px";
	alertFram.style.zIndex = "10001";
	
	strHtml = "<ul style=\"list-style:none;margin:0px;padding:0px;width:100%\">\n";
	strHtml += " <li style=\"background:#DD828D;text-align:left;padding-left:20px;font-size:14px;font-weight:bold;height:25px;line-height:25px;border:1px solid #F9CADE;color:#5E0000;\">[系统提示]</li>\n";
	strHtml += " <li style=\"background:#fff;text-align:center;font-size:14px;line-height:24px;padding:20px 0;border-left:1px solid #F9CADE;border-right:1px solid #F9CADE;color:#F00;\">"+msg+"</li>\n";
	strHtml += " <li style=\"background:#FDEEF4;text-align:center;font-weight:bold;height:35px;line-height:35px; border:1px solid #F9CADE;\"> <input type=\"button\" value=\"确 认\" id=\"do_OK\" style=\"padding:5px;\" onclick=\"doOk()\" /> </li>\n";
	strHtml += "</ul>\n";
	alertFram.innerHTML = strHtml;
	document.body.appendChild(alertFram);
	document.body.appendChild(shield);
	isCreate=true;
	this.setOpacity = function(obj,opacity){
		if(opacity>=1)opacity=opacity/100;
		try{ obj.style.opacity=opacity; }catch(e){}
		try{
			if(obj.filters.length>0&&obj.filters("alpha")){
			obj.filters("alpha").opacity=opacity*100;
		}else{
			obj.style.filter="alpha(opacity=\""+(opacity*100)+"\")";
		}
		}catch(e){}
	}
	var c = 0;
	this.doAlpha = function(){
		if (++c > 20){clearInterval(ad);return 0;}
		setOpacity(shield,c);
	}
	var ad = setInterval("doAlpha()",1);
	this.doOk = function(){
		document.body.removeChild(alertFram);
		document.body.removeChild(shield);
		//eSrc.focus();
		isCreate=false;
		document.body.onselectstart = function(){return true;}
		document.body.oncontextmenu = function(){return true;}
		if(mUrl!=""){gourl(mUrl);}
	}
}
