comp = navigator.appName;
vers = navigator.userAgent;

function CheckField(f_field, f_name, f_type, f_len) {
	var sError = '';
	if ( f_field.value.length == 0 )
		sError = 'The "' + f_name + '" field must not be empty!';
	else if ( f_type != 'anychars' && CheckInvalidField(f_field.value, f_type) )
		sError = 'The "' + f_name + '" field is invalid or contains unacceptable characters!\n' + sInvalidFieldWarn;
	else if ( f_len && (f_field.value.length != f_len) )
		sError = 'The "' + f_name + '" length must not exceed ' + f_len + ' characters.';
		
	if ( sError ) {
		alert(sError);
		f_field.focus();
		return false;
	}
	return true;
}

function CheckOptField(f_field, f_name, f_type) {
	if ( f_field.value.length > 0 )
		if ( CheckInvalidField(f_field.value, f_type) ) {
			alert('The "' + f_name + '" field is invalid or contains unacceptable characters!\n' + sInvalidFieldWarn);
			f_field.focus();
			return false;
		}
	return true;
}

function CheckInvalidField (s, sKind) {
	switch (sKind) {
		case 'login': //Логин, пароль
			sInvalidFieldWarn = "Only letters (a-z, A-Z), digits and underscope are allowed.";
			sPossibleChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_';
			break;
		case 'name': //Имя, фамилия, отчество
			sInvalidFieldWarn = "Only letters (a-z, A-Z), minus and space are allowed.";
			sPossibleChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- ';
			break;
		case 'city': //Город
			sInvalidFieldWarn = "Only letters (a-z, A-Z), digits, slash, mesh, minus and space are allowed.";
			sPossibleChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890#/- ';
			break;
		case 'phonecity': //Телефон
			sInvalidFieldWarn = "Only digits, space, plus and minus are allowed.";
			sPossibleChars = '1234567890- ';
			break;
		case 'phonecountry': //Телефон
			sInvalidFieldWarn = "Only digits, space, plus and minus are allowed.";
			sPossibleChars = '1234567890- ';
			break;
		case 'phone': //Телефон
			sInvalidFieldWarn = "Only digits, space, plus and minus are allowed.";
			sPossibleChars = '1234567890- ';
			break;
		case 'number': // Число
			sInvalidFieldWarn = "Only digits are allowed.";
			sPossibleChars = '1234567890';
			break;
		case 'email': //E-mail ***
			sInvalidFieldWarn = "Only letters (a-z, A-Z), digits, @, minus and underscore are allowed.";
			//Ищем собаку
			if ( (iDogPosition = s.indexOf('@')) == -1 || s.indexOf('@') == 0 || s.indexOf('@') == s.length-1 )
				return 1;
			//Ищем точку с позиции собаки до конца строки
			else if ( (iPointPosition = s.indexOf('.', iDogPosition+2)) == -1 || s.indexOf('.', iDogPosition+2) == 0 || s.indexOf('.', iDogPosition+2) == s.length-1 )
				return 1;
			else
				sPossibleChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_.@';
			break;
	}		
	for (var i = 0; i < s.length; i++) {
		if (sPossibleChars.indexOf(s.charAt(i)) == -1) {
			return 1; //Найден недопустимый символ
		}
	}
	return 0; //Все OK
}
