function init_forms(name,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9) {
	inputs = (name) ? document.forms[name].getElementsByTagName('input') : document.getElementsByTagName('input');
	i = 0;
	while(eval('a'+i)) {
		if(eval('a'+i) > 0) {
			inputs[i].setAttribute('minlength',eval('a'+i));
		}
		i++;
	}
	for(c = 0; c < inputs.length; c++) {
		if(inputs[c].getAttribute('minlength') || inputs[c].getAttribute('require')) {
			bg_validate(inputs[c]);
			inputs[c].setAttribute('onkeyup','bg_validate(this)');
			inputs[c].setAttribute('onchange','bg_validate(this)');
			if(document.all) {
				inputs[c].onkeyup = bg_validate_ie;
				inputs[c].onchange = bg_validate_ie;
			}
		}
	}
}
function bg_validate(obj) {
	valid = _validate_field(obj)
	submitElement = get_submit_element(obj.form);
	if(valid && submitElement.disabled) {
		elems = obj.form.elements;
		var c = 0;
		form_ok = true;
		while(c < elems.length && form_ok) {
			form_ok = _validate_field(elems[c]);
			c++;
		}
		if(form_ok) {
			submitElement.disabled = false;
			submitElement.className = 'enabled';
			submitElement.title = '';
			_errormsg(obj, false);
		}
		else {
			_errormsg(elems[c-1], true);
		}
	}
	else if(!valid && submitElement.disabled == false) {
		submitElement.disabled = true;
		submitElement.title = submitElement.getAttribute('errormsg');
		submitElement.className = 'disabled';
		_errormsg(obj, true)
	}
	if(!valid) {
		_errormsg(obj, true)
	}
	if((obj.type=="text") || (obj.type=="password")) {
		obj.className = (valid) ? 'valid' : 'invalid';
	}
}
function get_submit_element(form) {
	if(form.submitElement) {
		return form.submitElement;
	}
	else {
		if(form.elements['submit']) {
			form.submitElement = form.elements['submit'];
		}
		else {
			for(var i=0;i<form.elements.length;i++) {
				if(form.elements[i].type == 'submit') {
					form.submitElement = form.elements[i]
				}
			}
		}
		return form.submitElement;
	}
}
function bg_validate_ie() {
	bg_validate(event.srcElement)
}
function _errormsg(obj, error) {
	id = obj.form.name + '_' + 'status';
	if(!document.getElementById(id)) {
		embryo = document.createElement('div');
		embryo.id = id;
		obj.form.appendChild(embryo)
	}
	document.getElementById(id).textContent = (error) ? obj.getAttribute('errormsg') : '';
}
function _validate_field(obj) {
	valid_length = (obj.getAttribute('minlength') && (obj.value.length < obj.getAttribute('minlength'))) ? false : true;
	if(obj.getAttribute('require') == 'email') {
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		valid_context = filter.test(obj.value);
	}
	else if(obj.getAttribute('require') == 'check') {
		valid_context = (obj.checked);
	}
	else if(obj.type=="password") {
		score = passwordStrength(obj.value, obj.form.user.value)
		window.status = score;
		valid_context = (score >= 23);
		if((score > 0) && (score < 23)) {
			obj.setAttribute('errormsg','Password too simple');
		}
		else {
			obj.setAttribute('errormsg','Password too short');
		}
	}
	else {
		valid_context = true;
	}
	return (valid_length && valid_context);
}
function passwordStrength(password,username)
{
    score = 0 
    
    //password < 4
    if (password.length < 4 ) { return 0 }
    
    //password == username
    if (password.toLowerCase()==username.toLowerCase()) { return 0 }
    
    //password length
    score += password.length * 4
    score += ( checkRepetition(1,password).length - password.length ) * 1
    score += ( checkRepetition(2,password).length - password.length ) * 1
    score += ( checkRepetition(3,password).length - password.length ) * 1
    score += ( checkRepetition(4,password).length - password.length ) * 1

	score -= keyPattern(password) * 3;
	
	//password has 3 numbers
    if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5 
    
    //password has 2 symbols
    if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5 
    
    //password has Upper and Lower chars
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10 
    
    //password has number and chars
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15 
    //
    //password has number and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15 
    
    //password has char and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15 
    
    //password is just a numbers or chars
    if (password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10 
    
    //verifing 0 < score < 100
    if ( score < 0 )  score = 0 
    if ( score > 100 )  score = 100 
    
    return score;
}

function checkRepetition(pLen,str) {
    res = ""
    for ( i=0; i<str.length ; i++ ) {
        repeated=true
        for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
            repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
        if (j<pLen) repeated=false
        if (repeated) {
            i+=pLen-1
            repeated=false
        }
        else {
            res+=str.charAt(i)
        }
    }
    return res
}
function keyPattern(str) {
    score = 0; // high score = bad
	kb1 = keyboardString('qwertyuiop');
	kb2 = keyboardString('asdfghjkl');
	kb3 = keyboardString('zxcvbnm');
	for (i=1; i<str.length ; i++ ) {
		if(Math.abs(str.charCodeAt(i) - str.charCodeAt(i-1)) == 1) {
			score++;
		}
		else if( (kb1[str.charAt(i)]) && (kb1[str.charAt(i-1)]) && (Math.abs(kb1[str.charAt(i)] - kb1[str.charAt(i-1)]) == 1))  {
			score++;
		}
		else if( (kb2[str.charAt(i)]) && (kb2[str.charAt(i-1)]) && (Math.abs(kb2[str.charAt(i)] - kb2[str.charAt(i-1)]) == 1))  {
			score++;
		}		
		else if( (kb3[str.charAt(i)]) && (kb3[str.charAt(i-1)]) && (Math.abs(kb3[str.charAt(i)] - kb3[str.charAt(i-1)]) == 1))  {
			score++;
		}
	}
	return score;
}

function keyboardString(str) {
	arr = new Array();
	for(i=0;i<str.length;i++) {
		arr[str.charAt(i)] = i;
	}
	return arr;
}