﻿ var PasswordStrength ={
            Level : ["极佳","一般","较弱","太短"],
            LevelValue : [15,10,5,0],//强度值
            Factor : [1,2,5],//字符加数,分别为字母，数字，其它
            KindFactor : [0,0,10,20],//密码含几种组成的加数 
            Regex : [/[a-zA-Z]/g,/\d/g,/[^a-zA-Z0-9]/g] //字符正则数字正则其它正则
            }
            
        PasswordStrength.StrengthValue = function(pwd)
        {
            var strengthValue = 0;
            var ComposedKind = 0;
            for(var i = 0 ; i < this.Regex.length;i++)
            {
                var chars = pwd.match(this.Regex[i]);
                if(chars != null)
                {
                    strengthValue += chars.length * this.Factor[i];
                    ComposedKind ++;
                }
            }
            strengthValue += this.KindFactor[ComposedKind];
            return strengthValue;
        } 
        
        PasswordStrength.StrengthLevel = function(pwd)
        {
            var value = this.StrengthValue(pwd);
            for(var i = 0 ; i < this.LevelValue.length ; i ++)
            {
                if(value >= this.LevelValue[i] )
                    return this.Level[i];
            }
        }
     
		function loadinputcontext(o)
		{
		   var showmsg=PasswordStrength.StrengthLevel(o.value);
		   switch(showmsg)
		   {
			  case "太短": showmsg+=" <img src='/image/level/1.gif' width='88' height='11' />";break;
			  case "较弱": showmsg+=" <img src='/image/level/2.gif' width='88' height='11' />";break;
			  case "一般": showmsg+=" <img src='/image/level/3.gif' width='88' height='11' />";break;
			  case "极佳": showmsg+=" <img src='/image/level/4.gif' width='88' height='11' />";break;
		   }
		   
		   document.getElementById('showmsg').innerHTML = showmsg;
			
		}

		function htmlEncode(source, display, tabs)
		{
			function special(source)
			{
				var result = '';
				for (var i = 0; i < source.length; i++)
				{
					var c = source.charAt(i);
					if (c < ' ' || c > '~')
					{
						c = '&#' + c.charCodeAt() + ';';
					}
					result += c;
				}
				return result;
			}
			
			function format(source)
			{
				// Use only integer part of tabs, and default to 4
				tabs = (tabs >= 0) ? Math.floor(tabs) : 4;
				
				// split along line breaks
				var lines = source.split(/\r\n|\r|\n/);
				
				// expand tabs
				for (var i = 0; i < lines.length; i++)
				{
					var line = lines[i];
					var newLine = '';
					for (var p = 0; p < line.length; p++)
					{
						var c = line.charAt(p);
						if (c === '\t')
						{
							var spaces = tabs - (newLine.length % tabs);
							for (var s = 0; s < spaces; s++)
							{
								newLine += ' ';
							}
						}
						else
						{
							newLine += c;
						}
					}
					// If a line starts or ends with a space, it evaporates in html
					// unless it's an nbsp.
					newLine = newLine.replace(/(^ )|( $)/g, '&nbsp;');
					lines[i] = newLine;
				}
				
				// re-join lines
				var result = lines.join('<br />');
				
				// break up contiguous blocks of spaces with non-breaking spaces
				result = result.replace(/  /g, ' &nbsp;');
				
				// tada!
				return result;
			}

			var result = source;
			
			// ampersands (&)
			result = result.replace(/\&/g,'&amp;');

			// less-thans (<)
			result = result.replace(/\</g,'&lt;');

			// greater-thans (>)
			result = result.replace(/\>/g,'&gt;');
			
			if (display)
			{
				// format for display
				result = format(result);
			}
			else
			{
				// Replace quotes if it isn't for display,
				// since it's probably going in an html attribute.
				result = result.replace(new RegExp('"','g'), '&quot;');
			}

			// special characters
			result = special(result);
			
			// tada!
			return result;
		}

		function checkSetting()
		{
			if ($('receiveuser').checked)
			{
				$('showhint').disabled = false;
			}
			else
			{			
				$('showhint').checked = false;
				$('showhint').disabled = true;
			}
		}