/**
 * @author   Tijs
 * --------------
 * @by       Johan
 * @at       2008.12.03
 * @what     cartridge nieuwe object
 * --------------
 * @by       Yves
 * @at       2009.01.07
 * @what     check on selectedindex >=0 in choicelist
 * --------------
 * @by       slem
 * @at       2009.01.08
 * @what     change login script
 * --------------------
 * @by       Johan
 * @at       2009.01.09
 * @what     Atalanta implementation.
 * --------------------
 * @by       Johan
 * @at       2009.01.15
 * @what     checklogin first var removed.
 * --------------------
 * @by       Slem
 * @at       2009.03.25
 * @what     leeding 0's an . delte from numeric login
 * --------------------
 * @by       tvin
 * @at       2009.09.05
 * @what     added devis functionality in checkLogin
 * --------------------
*/

function checkLogin(login,pass,vdc,vdcno,remlog,rempass)
{
	if(document.getElementById(login).tagName == 'INPUT')
	{
		login = document.getElementById(login).value;
	}
	else
	{
		if(document.getElementById(login).selectedIndex >= 0)
		{
			login = document.getElementById(login).options[document.getElementById(login).selectedIndex].value;
		}
	}
	pass = document.getElementById(pass).value;
	if(vdc)
	{
		vdc = (document.getElementById(vdc).value != vdcno ?  document.getElementById(vdc).value : '');
	}
	else
	{
		vdc = '';
	}
	remlog = (remlog == true ? '1' : '0');
	rempass = (rempass == true ? '1' : '0');
	var emailReg = /^(("[\w-\s\-]+")|([\w\-]+(?:\.[\w\-]+)*)|("[\w\s\-]+")([\w\-]+(?:\.[\w\-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	var uidReg = /[T0-9][0-9]{4,5}/i;
	var uidLeedingZero = /^0+/i;
	var type = false;
	if(emailReg.test(login))
	{
		type = "email";
	}
	else if(uidReg.test(login.replace(uidLeedingZero, '')))
	{
		type = "uid";
	}
	else
	{
		type = "devis";
	}
	loadFile("post","/includes/logincheck.php","type="+type+"&uid="+login+"&pass="+encodeURIComponent(pass)+"&code="+vdc+"&rem="+remlog+"&rempass="+rempass, 'text');
}

/**
 * Calculate the strength of a password on a scale of 0-100, inclusive.
 * @param string password The password to check.
 * @return int
 * @todo Unfinished and not in use. Finish and put to use, or delete it.
 */
function getPasswordStrength(password)
{
	// Each test returns a percentage with 0 being total failure and 100 being total success.
	var tests = {
		'isLongEnough': function () {
			return password.length >= 8
				? 100
				: 0;
		},
		'hasNumbers': function () {
			return password.split(/[0-9]/).length > 1
				? 100
				: 0;
		},
		'hasSymbols': function () {
			// Can you say "biased towards ASCII, verging on scriptaculously racist"? :-)
			return password.split(/[^0-9a-zA-Z]/).length
				? 100
				: 0;
		},
		'hasMixedCase': function () {
			return /[a-z]/.test(password) && /[A-Z]/.test(password)
				? 100
				: 0;
		}
	};
	var score = 0, legend = [];
	for (var test in tests) {
		var result = tests[test]();
		legend[legend.length] = test + ': ' + result;
		score += result;
	}
	score = score / tests.length;
	return parseInt(Math.min(100, score), 10);
}