/**************** 有关上载用户的所有js函数 *********************************/
//仅允许输入数字
function checkNumber() {
    //判断输入字符的keyCode，数字在48到57之间，超出部分返回false
    if ((event.keyCode >= 48) && (event.keyCode <= 57)) {
        event.returnValue = true;
    } else {
        event.returnValue = false;
    }
}
/*
* 上载用户注册的验证、提交js函数
*/
function reg()
{
	//主要字段的验证
	var username=document.getElementById("username").value;
	if(username==null || username.length==0 || username.length>30 || username.length<2){
		document.getElementById("usernameInfo").innerHTML="<font color='red'>用户名不合法！</font>";
		document.getElementById("username").focus();
		return;
	}else if(falg==1){
		document.getElementById("usernameInfo").innerHTML="<font color='red'>该用户名已经存在，请输入其他用户名！</font>";
		document.getElementById("username").focus();
		return;
	}
	var pwd=document.getElementById("password").value;
	if(pwd==null || pwd.length==0 || pwd.length>10 || pwd.length<4){
		document.getElementById("pwdInfo").innerHTML="<font color='red'>密码不合法！</font>";
		document.getElementById("password").focus();
		return;
	}
	var pwd2=document.getElementById("pwd2").value;
	if(pwd2==null || pwd2.length==0){
		document.getElementById("pwd2Info").innerHTML="<font color='red'>确认密码不能为空！</font>";
		document.getElementById("pwd2").focus();
		return;
	}
	if(pwd2!=pwd){
		document.getElementById("pwd2Info").innerHTML="<font color='red'>两次输入的密码不一致！</font>";
		return;
	}
	var email=document.getElementById("email").value;
	//正则表达式验证邮箱的合法性
	if(email==null || email.length==0){
		document.getElementById("emailInfo").innerHTML="<font color='red'>电子邮箱不能为空！</font>";
		document.getElementById("email").focus();
		return;
	}else{
		 var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
		 if(reg.test(email)!=true)
		 {
		 	document.getElementById("emailInfo").innerHTML="<font color='red'>电子邮箱不合法！</font>";
		 	document.getElementById("email").focus();
			return;
		 }
	}
	var checkNum=document.getElementById("checkNum").value;
	if(checkNum==null || checkNum.length==0){
		document.getElementById("checkNumInfo").innerHTML="<font color='red'>验证码不能为空！</font>";
		document.getElementById("checkNum").focus();
		return;
	}
	//检查上传头像的类型是否符合图像格式
	var logopath=document.getElementById("logopath").value;
	if(logopath!=null && logopath!="" && logopath.length!=0)
	{
		var endNameArr=logopath.split(".");
		var endName=endNameArr[endNameArr.length-1];
		//alert(endName);
		if(endName!="x-png" && endName!="bmp" && endName!="png" && endName!="gif" && endName!="jpeg" && endName!="jpg" && endName!="pjpeg"&& endName!="BMP")
		{
			document.getElementById("logopathInfo").innerHTML="<font color='red'>头像文件类型不兼容！</font>";
			document.getElementById("logopath").focus();
			return;
		}else{
			document.getElementById("logopathInfo").innerHTML="";
		}
		//验证是否同意协议
		var remarks=document.getElementsByName("remark");
		if(remarks[0].checked==false)
		{
			alert("请阅读帐号相关协议并同意后再注册！");
			return;
		}
		//验证通过后，提交注册信息到服务器处理
		document.regForm.submit();
	}else{
		encodeURIComponent(username);
		var truename=document.getElementById("truename").value;
		if(truename==null)truename="";
		encodeURIComponent(truename);
		var mobile=document.getElementById("mobile").value;
		if(mobile==null)mobile="";
		var phone=document.getElementById("phone").value;
		if(phone==null)phone="";
		var qq=document.getElementById("qq").value;
		if(qq==null)qq="";
		var address=document.getElementById("address").value;
		if(address==null)address="";
		encodeURIComponent(address);
		var code=document.getElementById("code").value;
		var url="/uploadRigester.action?username="+username+"&truename="+truename+"&password="+pwd+"&email="+email+"&mobile="+mobile+
				"&phone="+phone+"&qq="+qq+"&address="+address+"&code="+code+"&checkNum="+checkNum;
		//验证是否同意协议
		var remarks=document.getElementsByName("remark");
		if(remarks[0].checked==false)
		{
			alert("请阅读《帐号相关协议》并同意后再注册！");
			return;
		}
		window.location.href=url;	
	}
}

/**
*  注册时检查用户名是否已经存在的方法
*/
function checkName()
{
	var username=document.getElementById("username").value;
	if(username==null || username.length==0 || username.length>30 || username.length<2){
		document.getElementById("usernameInfo").innerHTML="<font color='red'>用户名不合法！</font>";
		document.getElementById("username").focus();
		return;
	}
	var url="/checkName.action?";
	var value="username="+username;
	doPost(url,value,checkNameResp);
}

/**
*  上载视频时的验证的方法
*/
function uploadSubmit()
{
	//检查非空
	var name=document.getElementById("name").value;
	if(name==null || name.length==0 || name.length>30 || name.length<2){
		document.getElementById("nameInfo").innerHTML="<font color='red'>视频名称不能为空！</font>";
		document.getElementById("name").focus();
		return;
	}else{
		document.getElementById("nameInfo").innerHTML="";
	}
	var videoFile=document.getElementById("videoFile").value;
	if(videoFile==null || videoFile.length==0){
		document.getElementById("videoFileInfo").innerHTML="<font color='red'>请选择视频文件路径！</font>";
		document.getElementById("videoFile").focus();
		return;
	}
	//检查上传文件的类型是否符合
	var endNameArr=videoFile.split(".");
	var endName=endNameArr[endNameArr.length-1];
	//alert(endName);
	if(endName!="WMV" && endName!="wmv" && endName!="AVI" && endName!="avi" && endName!="MPG" && endName!="mpg" && endName!="WMA"&& endName!="wma")
	{
		document.getElementById("videoFileInfo").innerHTML="<font color='red'>视频文件类型不兼容！</font>";
		return;
	}else{
		document.getElementById("videoFileInfo").innerHTML="";
	}
	//提交
	var name=document.uploadVideo.name.value;
	var content=document.uploadVideo.content.value;
	var size=document.uploadVideo.size.value;
	var time=document.uploadVideo.time.value;
	var source=document.uploadVideo.source.value;
	var sortId=document.uploadVideo.sortId.value;
	document.uploadVideo.submit();
}

//订单验证、提交方法
function orderSubmit()
{
	//主要字段验证
	var num=document.getElementById("num").value;
	if(num==null || num.length==0){
		document.getElementById("numInfo").innerHTML="<font color='red'>订购数量不能为空！</font>";
		document.orderForm.num.focus();
		return;
	}else if(isNaN(num)==true)
	{
		document.getElementById("numInfo").innerHTML="<font color='red'>订购数量只能输入正整数！</font>";
		document.orderForm.num.focus();
		return;
	}
	var clientname=document.getElementById("clientname").value;
	if(clientname==null || clientname.length==0 || clientname.length>30 || clientname.length<1){
		document.getElementById("clientnameInfo").innerHTML="<font color='red'>客户姓名不合法！</font>";
		document.orderForm.clientname.focus();
		return;
	}
	var phone=document.getElementById("phone").value;
	var tel=document.getElementById("tel").value;
	var address=document.getElementById("address").value;
	if(phone.length==0 && tel.length==0 && address.length==0)
	{
		document.getElementById("phoneInfo").innerHTML="<font color='red'>请留下你的任意一种联系方式，移动、固定电话或地址！</font>";
		document.orderForm.phone.focus();
		return;
	}
	if(address.length>255){
		document.getElementById("addressInfo").innerHTML="<font color='red'>地址不能超过255个字符！</font>";
		document.orderForm.address.focus();
		return;
	}
	//提交处理
	//var price=document.getElementById("price").value;
	//var productId=document.getElementById("productId").value;
	//document.orderForm.action="/product/proOrder.action?productId="+productId+"&price="+price;
	document.orderForm.submit();
}
/*
* 找回密码的提交方法
*/
function getPwd()
{
	//先验证邮箱、验证码是否为空
	var email=document.getElementById("email").value;
	//正则表达式验证邮箱的合法性
	if(email==null || email.length==0){
		document.getElementById("emailInfo").innerHTML="<font color='red'>电子邮箱不能为空！</font>";
		document.getElementById("email").focus();
		return;
	}else{
		 var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
		 if(reg.test(email)!=true)
		 {
		 	document.getElementById("emailInfo").innerHTML="<font color='red'>电子邮箱不合法！</font>";
		 	document.getElementById("email").focus();
			return;
		 }
	}
	var checkNum=document.getElementById("checkNum").value;
	if(checkNum==null || checkNum.length==0)
	{
		document.getElementById("checkNum").focus();
		document.getElementById("checkNumInfo").innerHTML="<font color='red'>验证码不能为空！</font>";
		return;
	}
	//提交
	window.location.href="/getPwd.action?falg=1&email="+email+"&checkNum="+checkNum;
}

/*
* 首页找回密码的提交方法
*/
function firstPageGetPwd()
{
	var username=document.getElementById("username").value;
	if(username=="" || username==null)
	{
		document.getElementById("info").innerHTML="<font color='red'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;用户名和密码不能为空！</font>";
		return;
	}
	var url="/getPwd.action?";
	var value="falg=0&username="+username;
	doPost(url,value,getPwdResp);
}
/*
* 登陆失败后找回密码的提交方法
*/
function loginErrorGetPwd()
{
	var url="/getPwd.action?falg=1";
	doPost(url,null,getPwdResp);
}
/************************************************ 以下为Ajax方法 *************************************************/
var xmlHttp = null;
var falg=0;
function createXMLHttp(){
	try{
		xmlHttp = new XMLHttpRequest();//非ie浏览器
	}catch(e){
		try{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//ie5
		}catch(e){
			try{
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");//ie6
			}catch(e){
				xmlHttp = null;
				alert("不支持异步的浏览器！");
			}
		}
	}
}

function doPost(url,value,responseM)
{
	//alert("doPost()");
    try
    {
        createXMLHttp();
        if(xmlHttp!=null)
        {
            xmlHttp.onreadystatechange=responseM;
            xmlHttp.open("POST",url,false);
            //http_request.setRequestHeader("Content-Length",value.length); 
            xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlHttp.send(value);
        }
    }
    catch(e)
    {
        return false;
    }   
}

function checkNameResp()
{
	//alert("处理函数");
	if(xmlHttp.readyState==4)
	{
		//alert("---"+xmlHttp.responseText+"---");
		if(xmlHttp.responseText=="true")
		{
			//给全局变量设定用户名已存在的值
		    falg=1;
			document.getElementById("usernameInfo").innerHTML="<font color='red'>该用户名已经存在，请输入其他用户名！</font>";
		}else{
			falg=0;
			document.getElementById("usernameInfo").innerHTML="<font color='green'>恭喜！该用户名可以使用！</font>";
		}
	}
}

function getPwdResp()
{
	//alert("处理函数");
	if(xmlHttp.readyState==4)
	{
		//alert("---"+xmlHttp.responseText+"---");
		document.getElementById("info").innerHTML=xmlHttp.responseText;
	}
}

