

String.prototype.trim = function() {
	return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};

var Dictionary = Class.create({
	initialize : function() 
	{
		this.dict=new Array();
	},
	addAll : function(arr) 
	{
		for( i=0; i<arr.length; i++ )
		{
			var ss = arr[i].trim().toLowerCase();
			
			this.dict.push(ss);
		}
		
		this.dict.sort();	
	},
	check : function(ss)
	{
		var str=ss.trim().toLowerCase();
		
		var first=0;
		var last=this.dict.length-1;
		
		while(first<last)
		{
			var current=Math.floor((last+first)/2);
			
			if (str==this.dict[current])
				return true;
			else if (str > this.dict[current])
			{
				first=current+1;
			}
			else
				last=current-1;
		}
		
		/*
		for( i=0; i<this.dict.length; i++ )
		{
			var chk = this.dict[i];
			
			if (str==chk)
			{
				return true;
			}
		}
		*/

		return false;
	},
	length : function() { return this.dict.length; }
});

var dict=new Dictionary();

new Ajax.Request("/passwordCheck/password-sort.txt", {
	method: 'get',
  	onSuccess: function(transport) 
  	{
		var arr = transport.responseText.split(/[\n\r]+/);
  		
		dict.addAll(arr);
	},
	onFailure: function(transport) {
		alert('error');
	}
});

function DictionaryWord(pw)
{	
	return dict.check(pw);
}

function VariationOfDictionaryWord(pw)
{
	return DictionaryWord(pw);
}

function CharacterSet(pw)
{
	var cs=0;
	
	if (pw.search(/[a-z]/)!=-1) cs++;
	if (pw.search(/[A-Z]/)!=-1) cs++;
	if (pw.search(/[0-9]/)!=-1) cs++;
	if (pw.search(/[\!@#\$%\^&*\(\)]/)!=-1) cs++;
	//!@#$%^&*()_+-='\";:[{]}\|.>,</?`~-
	if (pw.search(/[^a-zA-Z0-9\!@#\$%\^&*\(\)]/)!=-1) cs++;
	
	return cs;
}

function ClientSideBestPassword(pw)
{
	return pw.length >= 13 && CharacterSet(pw) >= 3 && !VariationOfDictionaryWord(pw);
}

function ClientSideStrongPassword(pw)
{
	return pw.length >= 8 && CharacterSet(pw) >= 3 && !VariationOfDictionaryWord(pw);
}

function ClientSideMediumPassword(pw)
{
	return pw.length >= 8 && CharacterSet(pw) >= 2 && !DictionaryWord(pw);
}
