function checkStr(str, len)
 {	var alowed_str="AĄBCČDEĘĖFGHIĮJKLMNOPRSŠTUŲŪVYZŽaąbcčdeęėfghiįjklmnoprsštuųūvyzž0123456789QWXqwx \"'-*";
            var clear_str="AĄBCČDEĘĖFGHIĮJKLMNOPRSŠTUŲŪVYZŽaąbcčdeęėfghiįjklmnoprsštuųūvyzž0123456789QWXqwx";
	var strLen = str.length;
	if (strLen < len) return 1;
	var clearLen = 0;
	for (i = 0; i <= str.length-1; i++)	{
		if(alowed_str.indexOf(str.substring(i, i+1)) == -1) return 2;
		if(clear_str.indexOf(str.substring(i, i+1)) != -1) clearLen = clearLen + 1;
	}
	if (clearLen < len) return 1;
	return 0;
}

function mod(x,y) {
	return (x - Math.floor(x / y) * y);
}

function isnotNumber(str)
{
	if (str == "") return true
	var num = "0123456789";
	for (var intLoop = 0; intLoop < str.length; intLoop++)
		if (-1 == num.indexOf(str.charAt(intLoop)))
			return true;
	return false;
}

function checkPersonCode(Str) {
	if (Str=='') return 1;
	if (11 != Str.length)	return 1;
	if (isnotNumber(Str)) return 1;
	var i_Sum = 0;
	var num = "0123456789";
	for (var intLoop = 0; intLoop < 10; intLoop++) {
		i_Sum = i_Sum + (num.indexOf(Str.charAt(intLoop)) * ((intLoop%9) + 1));
	}
	var i_Key = (i_Sum%11);
	if (i_Key == 10)
	{
		i_Sum = 0;
		for (intLoop =0; intLoop < 10; intLoop++) {
		 i_Sum = i_Sum + (num.indexOf(Str.charAt(intLoop)) * (((intLoop+2)%9) + 1));
		}
		i_Key = (i_Sum%11);
		if (i_Key == 10) i_Key = 0;
 	}
 	if (i_Key != num.indexOf(Str.charAt(10))) return 1;
	return 0;
}

function checkCompanyCode(code) {
	code = parseInt(code);
	if ((code >= 100000000) && (code <= 999999999)) return 0;
	if ((code < 1000000) || (code > 9999999)) return 1;
	sum = 0;
	for(i=2; i <= 9; i++) sum = sum + mod(Math.floor(code / Math.pow(10, i - 1)), 10) * (8 - i);
	sum = 11 - mod(sum, 11);
	if (sum != mod(code, 10)) return 2
	return 0;
}

function checkPVMCode(code, item) {
	if (code == '') return 1;
	if ((14 != code.length) && (11 != code.length)){
		if ((code.length == 9)||(code.length == 12)) {
			code = 'LT'+code;
			if (item) item.value = code;
		}
		else  return 1;
	} 
	var code1= code.substring(0,2).toUpperCase();
	if (code1 != 'LT') return 2;
	var code2= code.substring(2,14);
	var num = "0123456789";
	for (var intLoop = 0; intLoop < code2.length; intLoop++) if (-1 == num.indexOf(code2.charAt(intLoop))) return 2;
	code = code1+code2;
	return 0;
}


function validateCode(code) {
	result = (checkPersonCode(code));
	if (result == 1) result = checkCompanyCode(code);
	return result;
}

function validateDealNum(code) {
	var str="AĄBCČDEĘĖFGHIĮJKLMNOPRSŠTUŲŪVYZŽ0123456789QWX-/";
	if (code.length < 4) return false;	if (code.length > 32) return false;
	for (i = 0; i <= code.length-1; i++) if(str.indexOf(code.substring(i, i+1)) == -1) return false;
	return true;
}

function checkNum(number, min, max) {
	var str="0123456789";
	for (i = 0; i <= number.length-1; i++) if(str.indexOf(number.substring(i, i+1)) == -1) return false;
	if (parseInt(number) < min) return false;	if (parseInt(number) > max) return false;	return true;
}

function checkNumLen(number, min, max) {
	var str="0123456789";
	for (i = 0; i <= number.length-1; i++) if(str.indexOf(number.substring(i, i+1)) == -1) return false;
	if (number.length < min) return false;
	if (number.length > max) return false;
	return true;
}

function checkPassword(password, fname, lname, code) {
	var chars="AĄBCČDEĘĖFGHIĮJKLMNOPRSŠTUŲŪVYZŽaąbcčdeęėfghiįjklmnoprsštuųūvyzžQWXqwx \"'-&*";
	var nums = "0123456789";
	char_c = 0;
	nums_c = 0;
	for (i = 0; i <= password.length-1; i++) {	
	if(chars.indexOf(password.substring(i, i+1)) == -1) {
			if(nums.indexOf(password.substring(i, i+1)) == -1) return 1;
			else nums_c = nums_c + 1;
		} else char_c = char_c + 1;
	}	if ((char_c < 2) || (nums_c < 4)) return 2;
	if (code.length == 11) {
		year = code.substring(1, 3);
		month = code.substring(3, 5);
		if (month == "00") {
			month = "01";
			day = "01";
		} else {	
			day = code.substring(5, 7);
			if (day == "00") day == "01";
		}
		password = password.toUpperCase();
		fnm = fname.substring(0, 1);
		fnm = fnm.toUpperCase();
		lnm = lname.substring(0, 1);
		if ((password.indexOf(year) != -1) || (password.indexOf(month) != -1) || (password.indexOf(day) != -1)) return 3;
		if ((password.indexOf(fnm) != -1) || (password.indexOf(lnm) != -1)) return 4;
	}
	return 0;
}
