﻿	var url1=document.location.href;
	var url2=url1.substring(7,url1.indexOf('/',9));	//获取当前主机地址
function validate(){ 	
	
	var urlFrom=document.location.href.split('?')[0];
	var num=0;
	var action1=0;
	if(url1.indexOf('action')!=-1){             //判断地址包含action参数字串 并得到起始位置
		num=url1.indexOf('action');
	  num+=7;	 																	//起始位置+7  得到action=后的位置 
 		action1=url1.substring(num,num+1); 	    	//得到action参数值
	}
   
 
 
 	 //action 1为体验用户 2 注册用户
			var email = document.form1.email.value;  
			
			document.form1.email.value=trim(email);
			 var isemail=(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
			 			 if(isEmpty(email)){
						    	alert("电子邮件不能空");
		             document.form1.email.focus();
		             return false;

				}		 
			 if(checkEmail(document.form1.email.value)){
								alert("邮箱地址非法");
								document.form1.email.focus();
								return false;
				}
		   if(!isemail.test(email)){
		   					alert("邮箱地址格式错误，请重新输入");
		   					document.form1.email.focus();
		   					return false;
		    }		 		
			document.form1.action="http://211.157.114.227/InitStudy/servlet/com.init.free.service.UserRegisterServlet?action="+action1+"&email"+email+"&urlFrom="+urlFrom;
			document.getElementById("next").style.display="none";
			document.getElementById("reg").style.display="";
			document.form1.submit();		 
	 		
}

 
function registerCheck(){	 
	 var isURL  = this.location.href;	
   var result = isURL.split('=');	     
    //Emil已经存在 
 		if(result[0].split('?')[1]=="error"&&result[1].split('&')[0]=="0"){			 	//判断error参数 等于0 则EMAIL存在	 
							document.getElementById("next").style.display="";
							document.getElementById("reg").style.display="none";
							document.getElementById("zq").style.display="none";		
							document.getElementById("cw").style.display="";
							document.getElementById("cw").innerHTML="邮箱已经达到5个，不可以再注册！";
							return;
		}
		//注册体验用户成功 判断user=1 体验用户
		if(result[0].split('?')[1]=="user"&&result[1].split('&')[0]=="1"){
			 				var username2=result[2].split('&')[0];
				 			var  pass2 =result[3].split('&')[0];
				 			var email2=result[4];
							document.getElementById("count1").style.display="none";		
							document.getElementById("count2").style.display="block";
					  	document.getElementById("username2").innerHTML=username2;
					 		document.getElementById("pass2").innerHTML=pass2;
					 		document.getElementById("email2").innerHTML=email2;
 		 					return;
 		 }
 		 //注册用户成功 user=2 注册用户
 		 if(result[0].split('?')[1]=="user"&&result[1].split('&')[0]=="2"){
			 				var username1=result[2].split('&')[0];
				 			var  pass1 =result[3].split('&')[0];
				 			var email1=result[4];
							document.getElementById("count1").style.display="none";		
							document.getElementById("count3").style.display="block";
					  	document.getElementById("username1").innerHTML=username1;
					 		document.getElementById("pass1").innerHTML=pass1;
					 		document.getElementById("email1").innerHTML=email1;
 							return;
 		}
			 //注册用户成功 user=3  友情网站链接
 		 if(result[0].split('?')[1]=="user"&&result[1].split('&')[0]=="3"){
			 				var username1=result[2].split('&')[0];
				 			var  pass1 =result[3].split('&')[0];
				 			var email1=result[4];
							document.getElementById("count1").style.display="none";		
							document.getElementById("count4").style.display="block";
					  	    document.getElementById("username3").innerHTML=username1;
					 		document.getElementById("pass3").innerHTML=pass1;
					 		document.getElementById("email3").innerHTML=email1;
 							return;
 		}
 				 
	 
	} 


	
 

	 	

//测试提交数据不为空
function isEmpty(str) {
    var flag = true;
    if ((str == null) || (str.length == 0 )) {
        return true;
    } else {
        return false;
    }
}

//检测EMail合法性
function checkEmail(str) {
  if (!(str == "")) {
    var atIndex = str.indexOf('@');
    var dotIndex = str.indexOf('.', atIndex);
    var flag = false;
    theSub = str.substring(0, dotIndex + 1);
    if ((atIndex < 1) || (atIndex != str.lastIndexOf('@')) ||
        (dotIndex < atIndex + 2) || (str.length <= theSub.length)) {
        flag = true;
    } else {
        flag = false;
    }
    return flag;
  }
}
function experience() {  //体验用户登录
   
   var isURL1  = this.location.href;	
   var result = isURL1.split('=');	  
   var username=result[2].split('&')[0];
	 var pass =result[3].split('&')[0];   
	  
	   var goURL="http://211.157.114.227/InitStudy/chinese/login.jsp?username="+username+"&password="+pass+"&act=logon&from="+getIP(username)+"&user=1";		 
   document.location=goURL;		
}
  
function card() {//学习卡激活
  
   var isURL1  = this.location.href;	
   var result = isURL1.split('=');	  
   var username=result[2].split('&')[0];
	 var pass =result[3].split('&')[0];   
	 
	   var goURL="http://211.157.114.227/InitStudy/chinese/login.jsp?username="+username+"&password="+pass+"&act=logon&from="+getIP(username)+"&user=2";		 
   document.location=goURL;		
}

function buy_program() {//购买课程
  
   var isURL1  = this.location.href;	
   var result = isURL1.split('=');	  
   var username=result[2].split('&')[0];
   var pass =result[3].split('&')[0];   
	 
	   var goURL="http://211.157.114.227/InitStudy/chinese/login.jsp?username="+username+"&password="+pass+"&act=logon&from="+getIP(username)+"&user=3";		 
   document.location=goURL;		
}

function getPassword(){//取回密码
 	var url1=document.location.href;
	var url2=url1.substring(7,url1.indexOf('/',9));	
  	  	var email = document.form1.email_text.value;  
  	  	var username= document.form1.user_id_text.value ;
  	  	if(isEmpty(username)){
						    	alert("用户名不能为空");
		             document.form1.user_id_text.focus();
		             return false;

				}		 
			 if(isEmpty(email)){
						    	alert("电子邮件不能空");
		             document.form1.email_text.focus();
		             return false;

				}		 
			 if(checkEmail(document.form1.email_text.value)){
								alert("邮箱地址非法");
								document.form1.email_text.focus();
								return false;
				}
				
 
				document.form1.action="http://"+getIP(username)+"/InitStudy/servlet/com.init.web.NewGetPassword?user_id="+username+"&email="+email; 
				document.form1.submit();
  			document.getElementById("next_click").style.display="none";
		 
}

function getUserMsg(){ 
		var url = window.location.href;
		var msgArray = url.split("=");
		var email = msgArray[1];
		
		if (email==1||email==2||email==4){
			if(email==1){
			document.getElementById("cw").style.display="";
			document.getElementById("cw").innerHTML="密码取回失败";
			document.getElementById("Email").innerHTML= "用户不存在";
			}
			if(email==2){
				document.getElementById("cw").style.display="";
				document.getElementById("cw").innerHTML="密码取回失败";
			document.getElementById("Email").innerHTML= "邮箱和用户名不匹配";
			}
			if(email==4){
				document.getElementById("cw").style.display="";
				document.getElementById("cw").innerHTML="密码取回失败";
			document.getElementById("Email").innerHTML= "查找失败";
			}
		}else{	
			if(email.split('&')[0]==0){
				document.getElementById("zq").style.display="";
				document.getElementById("zq").innerHTML="密码取回成功";
			document.getElementById("Email").innerHTML= "系统已将您的密码发送到您的电子邮箱  "+msgArray[2]+"中，请查收。";
			}
		}
}
 


function getIP(username){	
			 //分配ip
			 var user_name=username;
				var coverCode = user_name.substring(0,2); 
				var url_jsp = "";//外网ip
				for(var i=0;i<serverList.length;i++) {
				  var object = serverList[i];	  
				  if(coverCode.toUpperCase()==object.code.toUpperCase()) {		   
					  url_jsp = object.url_jsp;//外网ip	 
					  break;
				   }
				  if(i==serverList.length-1){		
					url_jsp=defaultUrl_jsp;//外网ip
				  }
				}	
			 
	return url_jsp;
}


/**   
 * 校验所有输入域是否含有特殊符号   
 * 所要过滤的符号写入正则表达式中，注意，一些符号要用'\'转义.   
 * 要转义的字符包括：1， 点号 .   
 *                   2,  中括号 []   
 *                   3,  大括号 {}   
 *                   4,  加号   +   
 *                   5,  星号   *   
 *                   6,  减号   -   
 *                   7,  斜杠   \   
 *                   8,  竖线   |   
 *                   9,  尖号   ^   
 *                   10, 钱币   $   
 *                   11, 问号   ？   
 * 试例：   
 * if(checkAllTextValid(document.forms[0]))   
 *  alert("表单中所有文本框通过校验！");   
 */   
function checkAllTextValid(form)    
{    
    //记录不含引号的文本框数量    
 var resultTag = 0;    
    //记录所有text文本框数量    
    var flag = 0;    
 for(var i = 0; i < form.elements.length; i ++)    
 {    
  if(form.elements[i].type=="text")    
  {    
            flag = flag + 1;    
   //此处填写所要过滤的特殊符号    
   //注意：修改####处的字符，其它部分不许修改.    
   //if(/^[^####]*$/.test(form.elements[i].value))     
  
  if(/^[^\|"'<>^~`!#$%&*{}()?]*$/.test(form.elements[i].value)){
                resultTag = resultTag+1;   
  }else   	 
     form.elements[i].select();     
  }   
 }   
  
 
    /**   
     * 如果含引号的文本框等于全部文本框的值，则校验通过   
     */   
 if(resultTag == flag)   
  return true;   
 else   
 {   
  alert("请不要输入特殊字符，请确认后重新注册");    
  return false;    
 }    
}    
function trim(str1){
  var str2=str1.replace(/(^\s*)|(\s*$)/g,""); 
 	while((str2>0)&&(x.charAt(0)==' ')){
 	str2=str2.substring(1,str2.length);
 	}
 	while((str2.length>0)&&(str2.charAt(str2.length)==' ')){
 	str2=str2.substring(0,str2.length-1);
 	}
 
  return str2;
}