﻿
function chkShoppingCart_Login()
{
	this.passed = false;
	this.checkItems = [ "Account", "Password" , "Code" ];

	this.checkAll = function ()
	{
		for ( var i = 0; i < this.checkItems.length; i ++ )
		{
			var check = eval ( "this.check" + this.checkItems[i] + "()" );
			if(!check)
			    return false;
		}
		return true;
	}

	this.showTip = function ( message )
	{
		var obj;
		if ( obj = document.getElementById ( 'tipSummary' ) )
		{
			obj.style.visibility = "visible";
			obj.innerHTML = message;
		}
	}
	
	this.hideTip = function ()
	{
		var obj;
		if ( obj = document.getElementById ( 'tipSummary' ) )
		{
			obj.style.visibility = "hidden";
		}
	}

	this.checkAccount = function()
	{
		var message = '';
		var textbox = document.getElementById("Step2_txtAccount");
		if ( textbox.value.length == 0 )
		{
			message = "请填写用户名";
		}
		else if ( textbox.value.length < 4 || textbox.value.length > 20 )
		{
			message = '用户名长度必须为 4 - 20 字符';
		}
		else if ( textbox.value.indexOf (' ') >= 0 || textbox.value.indexOf ('\\') >= 0 || textbox.value.indexOf ('\'') >= 0 || textbox.value.indexOf ('\"') >= 0 )
		{
			message = '用户名包含非法字符';
		}		
		if ( message != '' )
		{
			this.showTip ( message );
			return false;
		}
		return true;
	}
	
	this.checkPassword = function()
	{
		var message = '';
		var account = document.getElementById("Step2_txtAccount");
		var textbox = document.getElementById("Step2_txtPassword");
		if ( textbox.value.length == 0 )
		{
			message = "请填写密码";
		}
		else if ( textbox.value.length < 6 || textbox.value.length > 20 )
		{
			message = '密码长度必须为 6 - 20 字符';
		}

		if ( message != '' )
		{
			this.showTip ( message );
			return false;
		}
		return true;
	}

	this.checkCode = function()
	{
		var textbox = document.getElementById("Step2_txtCode");
		if ( textbox.value.length == 0 )
		{
			this.showTip ( "请填写验证码" );
			return false;
		}
		else
		{
		    var result = Member.GetVerifyCode().value;
            if (textbox.value != result )
            {
	            this.showTip ( '验证码不正确, 请检查并重新填写' );
	            return false;
            }
	        return true;
		}
	}
	
	this.changeVerifyCode = function()
    {
	    var obj = document.getElementById ( 'Step2_imgVerifyCode' );
	    if ( obj )
	    {
		    obj.style.visibility = 'visible';
		    obj.src = 'Resources/VerifyCode.aspx';
	    }
    }
}

var chkLogin = new chkShoppingCart_Login();


function chkShoppingCart_Regsiter()
{
	this.passed = false;

	this.className = [ 'boxNormal', 'boxError', 'boxOK', 'boxWaiting' ];
	this.checkItems = [ "Account", "Nickname", "Password", "PasswordConfirm", "Email" ];
	this.messages = {
		'Account'				: '使用汉字，英文、数字，总字符数在 4 - 20 之间。',
		'Nickname'				: '使用汉字，英文、数字，总字符数在 4 - 20 之间。',
		'Password'				: '使用数字和字母，长度为 6 - 20 个字符。',
		'PasswordConfirm'	    : '再次输入密码以保证密码无误。',
		'Email'					: '请认真填写您的真实邮箱，以便我们提供更好的服务！',
		'Code'					: '输入上图中的数字，若无法辨别请点击图片更换。'
	};
	// 提交注册信息时验证
	this.checkAll = function ()
	{
	    if (!document.getElementById ("cbAgree").checked ) 
	    {
	        alert('您还没有阅读并同意《用户注册服务协议》'); 
	        return false; 
	    }
		this.passed = true;
		for ( var i = 0; i < this.checkItems.length; i ++ )
		{
			obj = document.getElementById ( "tip" + this.checkItems[i] );
			if ( obj.className != this.className[2] )
			{
				var check = eval ( "this.check" + this.checkItems[i] + "()" );
				if ( check == 'checking' )
				{
					this.passed = false;
				}
				else
				{
					this.passed = this.passed && check;
				}
			}
		}
		return this.passed;
	}

	// 显示所有提示
	this.showAllTips = function ()
	{
		for ( var i in this.checkItems )
		{
			var obj;
			if ( obj = document.getElementById( 'tip' + this.checkItems[i] ) )
			{
				obj.innerHTML = eval ( "this.messages." + this.checkItems[i] );
			}
		}
	}

	this.showTip = function ( chkItem, tipType, message )
	{
		var obj;
		if ( obj = document.getElementById ( 'tip' + chkItem ) )
		{
			if ( tipType == null ) tipType = 0;
			if ( message == null ) message = eval ( "this.messages." + chkItem );
			obj.style.display = "block";
			if ( tipType > 0 || obj.className != this.className[2] || obj.className == '' )
			{
				obj.innerHTML = message;
				obj.className = this.className[tipType];
			}
		}
	}
	
	this.hideTip = function ( chkItem )
	{
		var obj;
		if ( obj = document.getElementById ( 'tip' + chkItem ) )
		{
			if ( obj.className != this.className[2] )
			{
				obj.style.display = "none";
			}
		}
	}
	
	this.checkAccount = function(checkSystem)
	{
		var message = '';
		var textbox = document.getElementById("Step2_txtAccount1");
		if ( checkSystem == null ) checkSystem = true;
		if ( textbox.value.length == 0 )
		{
			message = "请填写用户名";
		}
		else if ( textbox.value.length < 4 || textbox.value.length > 20 )
		{
			message = '用户名长度必须为 4 - 20 字符';
		}
		else if ( textbox.value.indexOf (' ') >= 0 || textbox.value.indexOf ('\\') >= 0 || textbox.value.indexOf ('\'') >= 0 || textbox.value.indexOf ('\"') >= 0 )
		{
			message = '用户名包含非法字符';
		}		
		if ( message != '' )
		{
			this.showTip ( 'Account', 1, message );
			return false;
		}
		else if ( checkSystem )
		{
		    this.showTip ( 'Account', 3, '正在检测用户名...' );
	        // Ajax 检查用户名
		    var result = Member.ExistAccount(textbox.value).value;
            if(!result)
            {
	            this.showTip ( 'Account', 2, '您填写的用户名可以成功注册' );
                return true;
            }
            else
            {
                this.showTip ( 'Account', 1, '用户名 '+textbox.value+' 已被使用, 请重新指定' );
                return false;
            }
		}
		return true;
	}
	
	this.checkNickname = function()
	{
		var message = '';
		var textbox = document.getElementById("Step2_txtNickname1");
		if ( textbox.value.length == 0 )
		{
			message = "请填写昵称";
		}
		else if ( textbox.value.length < 4 || textbox.value.length > 20 )
		{
			message = '昵称长度必须为 4 - 20 字符';
		}
		else if ( textbox.value.indexOf (' ') >= 0 || textbox.value.indexOf ('\\') >= 0 || textbox.value.indexOf ('\'') >= 0 || textbox.value.indexOf ('\"') >= 0 )
		{
			message = '昵称包含非法字符';
		}		
		if ( message != '' )
		{
			this.showTip ( 'Nickname', 1, message );
			return false;
		}
		else
		{
			this.showTip ( 'Nickname', 2, '填写正确' );
			return true;
		}	
	}
	
	this.checkPassword = function()
	{
		var message = '';
		var account = document.getElementById("Step2_txtAccount1");
		var textbox = document.getElementById("Step2_txtPassword1");
		if ( textbox.value.length == 0 )
		{
			message = "请填写密码";
		}
		else if ( textbox.value.length < 6 || textbox.value.length > 20 )
		{
			message = '密码长度必须为 6 - 20 字符';
		}
		else if ( textbox.value != '' && textbox.value == account.value )
		{
			message = '为了您的账号安全，密码请不要与用户名相同';
		}

		if ( message != '' )
		{
			this.showTip ( 'Password', 1, message );
			return false;
		}
		else
		{
			this.showTip ( 'Password', 2, '填写正确' );
			return true;
		}	
	}
	
	this.checkPasswordConfirm = function()
	{
		var message = '';
		var password = document.getElementById("Step2_txtPassword1");
		var textbox = document.getElementById("Step2_txtPasswordConfirm1");
		if ( textbox.value.length == 0 )
		{
			message = "请填写确认密码";
		}
		else if ( textbox.value != password.value )
		{
			message = '两次填写的密码不一致, 请检查并重新填写';
		}
		else if ( password.value == '' )
		{
			message = '请填写密码';
		}
		
		if ( message != '' )
		{
			this.showTip ( 'PasswordConfirm', 1, message );
			return false;
		}
		else
		{
			this.showTip ( 'PasswordConfirm', 2, '填写正确' );
			return true;
		}
	}
	
	this.checkEmail = function(checkSystem)
	{
		var message = '';
		var textbox = document.getElementById("Step2_txtEmail1");
		checkSystem = checkSystem != null ? checkSystem : true;
		if ( textbox.value.length == 0 )
		{
			message = "请填写您的邮箱地址";
		}
		else if(!Edward.Web.Validator.IsEmail(textbox.value))
		{
			message = "E-mail 地址无效, 请填写真实有效的 E-mail 地址";
		}
		if ( message != '' )
		{
			this.showTip ( 'Email', 1, message );
			return false;
		}
		else if ( checkSystem )
		{
		    this.showTip ( 'Email', 3, '正在检测邮箱地址...' );
        	// Ajax 检查邮箱地址
		    var result = Member.ExistEmail(textbox.value).value;
            if(!result)
            {
                this.showTip ( 'Email', 2, '您填写的邮箱地址可以成功注册' );
                return true;
            }
            else
            {
                this.showTip ( 'Email', 1, '邮箱地址 '+textbox.value+' 已被使用, 请重新指定' );
                return false;
            }
		}
		return true;
	}
	
	this.checkCode = function()
	{
		var textbox = document.getElementById("Step2_txtCode1");
		if ( textbox.value.length == 0 )
		{
			this.showTip ( 'Code', 1, "请填写验证码" );
			return false;
		}
		else
		{
		    var result = Member.GetVerifyCode().value;
            if ( textbox.value != result )
            {
	            this.showTip ( 'Code', 1, '验证码不正确, 请检查并重新填写' );
                return false;
            }
            else
            {
	            this.showTip ( 'Code', 2, '填写正确' );
                return true;
            }
		}
	}
	
	this.changeVerifyCode = function()
    {
	    var obj = document.getElementById ( 'Step2_imgVerifyCode1' );
	    if ( obj )
	    {
		    obj.style.visibility = 'visible';
		    obj.src = 'Resources/VerifyCode.aspx';
	    }
    }
}

var chkRegsiter = new chkShoppingCart_Regsiter();


