/* ************************************************************************************* *\
 * The MIT License
 * Copyleft (c) 2007 Horacio R. Valdez  - http://www.hvaldez.com.ar
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
 * software and associated documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
\* ************************************************************************************* */
var iToolkit = new Class
({ 
	initialize: function() 
	{ 
		
	},
	
	SoloIngresarNumeros: function(filedName)
	{
		var special = ['enter', 'up', 'down', 'left', 'right', 'pace', 'backspace', 'delete', 'esc'];
		var numeros = '0123456789';
		$(filedName).onkeydown = function(event){ 
			var event = new Event(event);  
			if (numeros.indexOf(event.key) == -1 && special.indexOf(event.key) == -1)  

				event.stop();
		};
	},
	
	MedidorDePassword: function(fieldName)
	{
		obj = $(fieldName);
		target = obj.getParent();
				
		obj.addEvent('keyup', function () { this._MedirPassword(obj.value); }.bind(this) );	
		
		var divTxt =  new Element('div');
		divTxt.setStyles('float: left; position:relative;');

		obj.injectInside(divTxt);
		divTxt.injectInside(target);
		var spaceDiv =  new Element('div');
		spaceDiv.setStyles('float: left; width:5px; position:relative;');
		spaceDiv.innerHTML = "&nbsp;";
		spaceDiv.injectInside(target);
		
		
		obj.addEvent('focus', function () {  
			var container = new Element('div');
			container.id = 'iToolkitPMContainer';
				
			var meter = new Element('div');
			meter.id = 'iToolkitPMBorder';
			
			var strength = new Element('div');
			strength.id = 'FrmTkPasswdStrenght';
			strength.addClass('iToolkitPMLevel1');
			strength.injectInside(meter);
			
			meter.injectInside(container);
			
			var status = new Element('div');
			status.id = 'iToolkitPMStatus';
			status.injectInside(container);
					
			container.injectInside(target);
			
			this._MedirPassword(obj.value);
		}.bind(this) );	
		
		obj.addEvent('blur', function () {  
			$('iToolkitPMContainer').remove();
			
		}.bind(this) );	
		
	},
	
	EfectoDestacado: function()
	{
		var objTypes = ['text', 'password','textarea'];
		var inputs = $ES('input'); 
		var textAreas = $ES('textarea'); 
		inputs.merge(textAreas);
		inputs.each(
			function(obj, index)
			{
				if(objTypes.indexOf(obj.type) != -1)
				{
					obj.currentstyle = obj.getProperty('class'); 
					obj.addEvent('focus', function () { this._SetFocus(obj, true); }.bind(this) );	
					obj.addEvent('blur', function () { this._LostFocus(obj, true); }.bind(this) );	
					obj.addEvent('mouseover', function () {  this._SetFocus(obj, false); }.bind(this) );	
					obj.addEvent('mouseout', function () {   this._LostFocus(obj, false); }.bind(this) );	
				}
			}.bind(this));
	},

	_MedirPassword: function(passwd)
	{
		var intScore   = 0;
		var strVerdict = 1;
		var strText = "";
		
		// PASSWORD LENGTH
		if (passwd.length<5) intScore = (intScore+3);
		else if (passwd.length>4 && passwd.length<8) intScore = (intScore+6);
		else if (passwd.length>7 && passwd.length<16) intScore = (intScore+16);
		else if (passwd.length>15)  intScore = (intScore+18);

		// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
		if (passwd.match(/[a-z]/)) 	intScore = (intScore+1);
		if (passwd.match(/[A-Z]/)) 	intScore = (intScore+5);
		
		// NUMBERS
		if (passwd.match(/\d+/)) intScore = (intScore+5);
		if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))  intScore = (intScore+5);
			
		// SPECIAL CHAR
		if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)) intScore = (intScore+5);
		if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) intScore = (intScore+5);
			
		// COMBOS
		if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) 	intScore = (intScore+2);
		if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) intScore = (intScore+2);
		if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)) intScore = (intScore+2)

	
		if(intScore < 4) { strVerdict = 1;  strText = "Seguridad"; }
		else if(intScore > 3 && intScore < 16) { strVerdict = 2;  strText = "Muy simple"; }
		else if (intScore > 15 && intScore < 25)  { strVerdict = 2;  strText = "Simple"; }
		else if (intScore > 24 && intScore < 35)  { strVerdict = 3;  strText = "Medianamente Segura"; }
		else if (intScore > 34 && intScore < 45)  { strVerdict = 4;  strText = "Muy Segura"; }
		else  { strVerdict = 5;  strText = "Terriblemente Segura";}
		
		$('FrmTkPasswdStrenght').removeProperty('class');
		$('FrmTkPasswdStrenght').setProperty('class','iToolkitPMLevel'+strVerdict);
		
		$('iToolkitPMStatus').innerHTML = strText;
	},
	
	_SetFocus: function(obj, val)
	{
		if(obj.getProperty('class') !='iToolkitFocus')
			obj.currentstyle = obj.getProperty('class');
			
		if(val)
		{
			obj.focused = true;
		}
		obj.setProperty('class','iToolkitFocus');
	},

	_LostFocus: function(obj, val)
	{
		if(val) { obj.setProperty('class',obj.currentstyle); obj.focused = false; }
		if(!obj.focused) obj.setProperty('class',obj.currentstyle);
	}	
});