//Store location from where request come
//and set the Id of password field into passId variable
var url=location.href;

if(url.match('addUser.php') || url.match('registerUser.php'))
	var passId='pass1_ele';
else if(url.match('editUser.php') || url.match('editDealer.php') || url.match('reset_password.php'))
	var passId='newpass1_ele';

// Ads eventhandelr to given object
function addEvent(obj, evType, fn)
{
	if (obj.addEventListener)
	{
   		obj.addEventListener(evType, fn, false);
   		return true;
	}
	else if (obj.attachEvent)
	{
   		var r = obj.attachEvent('on'+evType, fn);
   		return r;
 	}
 	else
 	{
   		return false;
 	}
}

// Sets focus to first input with class "focus"
function initFocus()
{
	if (findFocus('input'))
		return;

	if (findFocus('select'))
		return;

	if (findFocus('textarea'))
		return;
}

function findFocus(elementName)
{
	var inputs = document.getElementsByTagName(elementName);
	for (var i = 0; i < inputs.length; i++)
	{
		if (inputs[i].className.indexOf('focus') != -1 )
		{
			inputs[i].focus();
			return true;
		}
	}
	return false;
}

//init
function initQualityMeter()
{
	var _pwd = document.getElementById(passId);
	if (_pwd)
	{
		addEvent(_pwd, 'keyup', updateQualityMeter);
		initProgressBar();
		updateQualityMeter();
	}
}


function updateQualityMeter()
{
	var quality = getPasswordStrength();
	setProgressBarValue(quality);
}

// Function taken from Mozilla Code:
// http://lxr.mozilla.org/seamonkey/source/security/manager/pki/resources/content/password.js
function getPasswordStrength()
{
	// Here is how we weigh the quality of the password
	// number of characters
	// numbers
	// non-alpha-numeric chars
	// upper and lower case characters

	var pw = document.getElementById(passId).value;
	//  alert("password='" + pw +"'");

	//length of the password
	var pwlength=(pw.length);
	if (pwlength>5)
		pwlength=5;


	//use of numbers in the password
	var numnumeric = pw.replace (/[0-9]/g, '');
	var numeric=(pw.length - numnumeric.length);
	if (numeric>3)
		numeric=3;

	//use of symbols in the password
	var symbols = pw.replace (/\W/g, '');
	var numsymbols=(pw.length - symbols.length);
	if (numsymbols>3)
		numsymbols=3;

	//use of uppercase in the password
	var numupper = pw.replace (/[A-Z]/g, '');
	var upper=(pw.length - numupper.length);
	if (upper>3)
		upper=3;


	var pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);

	// make sure we're give a value between 0 and 100
	if ( pwstrength < 0 ) {
		pwstrength = 0;
	}

	if ( pwstrength > 100 ) {
		pwstrength = 100;
	}

	return pwstrength;
}

function initProgressBar()
{
	var _container = document.getElementById('the_progressbar');
	if (_container)
	{
		_currentStyle = _container.style;
		if (_currentStyle != null)
		{
			_currentStyle.display = 'block';
		}

		//Create The Left progress part
		//Looks like <span class='right' style="display: none; padding: 0 50px; height: 1.2em; background-color: green;">
		var _progressL = document.createElement('span');
		_progressL.id = 'progressl';

		var _style = _progressL.style;
		_style.backgroundColor = '';
		_style.fontWeight='bold';
 		_style.padding = '3px 0px';
		_style.height = '1.2em';
 		_style.width = '145px';
 		_style.color = '#000';
		_style.display = 'block';
		_style.textAlign = 'center';
		_style.zIndex = 100;

		_container.appendChild(_progressL);
	}
}

function setProgressBarValue(value)
{
	var pw = document.getElementById(passId).value;

	var _value = parseInt(value);
	if (_value == NaN)
		_value = 0;

	if(_value == 0 && pw.length > 0)
		_value = 1;

	if (_value > 100)
		_value = 100;

	_progressL = document.getElementById('progressl');
	if (_progressL )
	{
		if(_value == 0)
			_progressL.style.display='none';
		else
			_progressL.style.display='block';

		if(_value > 0 && _value <= 20){
			_progressL.innerHTML = short;
			_progressL.style.backgroundColor = '#fb5656';
		}
		else if(_value > 20 && _value <= 40){
 			_progressL.innerHTML = weak;
			_progressL.style.backgroundColor = '#ffb062';
		}
		else if(_value > 40 && _value <= 60){
 			_progressL.innerHTML = fair;
			_progressL.style.backgroundColor = '#fee30c';
		}
		else if(_value > 60 && _value <= 80){
 			_progressL.innerHTML = strong;
			_progressL.style.backgroundColor = '#cfeb8e';
		}
		else if(_value > 80 && _value <=100){
 			_progressL.innerHTML = v_strong;
			_progressL.style.backgroundColor = '#afd161';
		}
		else{
 			_progressL.innerHTML = '';
			_progressL.style.backgroundColor = '';
		}
	}
}