var mod_passport = {
	text: {
		login: {
			empty_email: '',
			empty_password: ''
		}
	},
	login_form_sent: false,
	loginFormIsCorrect: function(form)
	{
		if(this.login_form_sent)
		return false;

		if (isEmpty(form.email.value))
		{
			alert('Вы не указали E-Mail!');
			form.email.focus();
			return false;
		}

		if (!isEmail(form.email.value))
		{
			alert('Вы указали неверный E-Mail!');
			form.email.focus();
			return false;
		}

		if (isEmpty(form.password.value))
		{
			alert('Вы не указали пароль!');
			form.password.focus();
			return false;
		}

		form.send_button.disabed = true;

		this.login_form_sent = true;
		return true;
	},

	legal_form_sent: false,
	legalSubmit: function(form)
	{
		if(this.legal_form_sent)
			return false;

			this.legal_form_sent = true;
			form.send_button.disabed = true;

			return true;
	},

	changeQuestion: function()
	{
		if ($('#question option:selected').val() == 100)
		{
			$('#q_text_block').css('display', '');
			$('#question_text').focus();
		}
		else
		{
			$('#q_text_block').hide();
			$('#question_text').val('');
		}
	},

	setView: function(obj, arrobj)
	{
		if(obj != null)
			document.getElementById(obj).style.display = '';

		for(var i = 0; i < arrobj.length; i++)
			if(arrobj[i] != null)
				document.getElementById(arrobj[i]).style.display = 'none';
	},

	setEMail: function(login, domain, post)
	{
		if(typeof(post) == 'undefined')
			post = '';
		var uobj = document.getElementById('username'+post);
		if(uobj != null)
			uobj.value = login;
		var dobj = document.getElementById('domain'+post);
		if(dobj != null)
		{
			for(var i=0;i<dobj.options.length;i++)
			{
				if(dobj.options[i].text == domain)
				{
					dobj.selectedIndex = i;
					break;
				}
			}
		}
		return false;
	},

	setName: function(name)
	{
		var uobj = document.getElementById('firstname');
		if(uobj != null)
		uobj.value = name;
		return false;
	},

	remindSent: false,
	remind1Submit: function()
	{
		if(this.remindSent == true)
		return false;

		var email = document.getElementById('forgot_email');
		if (isEmpty(email.value))
		{
			alert('Вы не указали E-Mail!');
			email.focus();
			return false;
		}

		if (!isEmail(email.value))
		{
			//alert('Вы указали неверный E-Mail!');
			//email.focus();
			//return false;
		}

		document.getElementById('send_button').disabled = true;

		this.remindSent = true;

		return true;
	},

	remind2Submit: function()
	{
		if(this.remindSent == true)
		return false;

		var answer = document.getElementById('answer');
		if (isEmpty(answer.value))
		{
			alert('Вы не указали ответ!');
			answer.focus();
			return false;
		}

		document.getElementById('send_button').disabled = true;

		this.remindSent = true;

		return true;
	},

	remind3Submit: function()
	{
		if(this.remindSent == true)
		return false;

		var password = document.getElementById('new_pass');
		var password2 = document.getElementById('new_pass2');

		if (isEmpty(password.value))
		{
			alert('Вы не указали пароль!');
			password.focus();
			return false;
		}

		if (isEmpty(password2.value))
		{
			alert('Вы не указали пароль!');
			password2.focus();
			return false;
		}

		if (password.value != password2.value)
		{
			alert('Пароли не равны!');
			password.focus();
			return false;
		}

		document.getElementById('send_button').disabled = true;

		this.remindSent = true;

		return true;
	},

	load_success: function(to, data)
	{
		obj = $('#'+to).get(0);

		obj.options.length = 1;
		for(var i in data)
			obj.options[obj.options.length] = new Option(data[i].name, i, false, false);
		obj.selectedIndex = 0;
		obj.disabled = false;
	},

	load_failed: function(to)
	{
		obj = $('#'+to);
		obj.get(0).disabled = true;
	},

	changeViewSH: function(type) {

		if (type == 1) {
			var label = document.getElementById('lrsh1');
			var firstName = document.getElementById('@fn').value.replace(/\s/,'');
			var lastName = document.getElementById('@ln').value.replace(/\s/,'');

			if (firstName != '' && lastName != '') {
				label.innerHTML = firstName + ' ' + lastName;
			} else
				label.innerHTML = 'Имя Фамилия';
		} else if (type == 2) {
			var label = document.getElementById('lrsh1');
			var firstName = document.getElementById('@fn').value.replace(/\s/,'');
			var lastName = document.getElementById('@ln').value.replace(/\s/,'');

			if (firstName != '' && lastName != '') {
				label.innerHTML = firstName + ' ' + lastName;
			} else
				label.innerHTML = 'Имя Фамилия';

			var label = document.getElementById('lrsh0');
			var nickname = document.getElementById('nickname').value.replace(/\s/,'');

			if (nickname != '') {
				label.innerHTML = nickname;
			} else
				label.innerHTML = 'Псевдоним';
		}

	},

	/*getPlaceForm: function(motive, type, append, place) {

		if ( !place )
			place = 0;

		$.ajax({
			url: '/place/?nocache=3453',
			dataType: 'json',
			data: {
				action: 'place_form',
				motive: motive,
				type: type,
				place: place,
				rand:Math.random()},
			success: function(data){

				$(append).before(data.form);
				$('#suggest'+data.fix).suggest("/place/", {
						maxCacheSize: -1,
						minchars: 3,
						action: 'search',
						onSelect: function(value, key) {
							$('#place'+data.fix).get(0).value = key;
						},
						onSuggest: function() {
							var city = $('#city'+data.fix).get(0);
							var city_text = $('#city_text'+data.fix);

							city = city.options[city.selectedIndex].value > 0 ? city.options[city.selectedIndex].text : city_text.val();
							this.data.city = '';
							this.data.type = type;

							if ( city != '' )
								this.data.city = city;
							else
								alert('Пожалуйста укажите город!');
						}
					}
				);
			},
			type: 'POST'});
	},*/

	addPlace: function(type, place) {
		$.ajax({
			url: '/service/source/db.place',
			dataType: 'json',
			data: {
				action: 'adduser',
				type: type,
				id: place
			},
			success: function(data){
				$("#place_id_" + place).hide();
			},
			type: 'POST'
		});
	},

	getPlaceFormNew: function(type, append, place) {

		if ( !place )
			place = 0;

		$.ajax({
			url: '.',
			dataType: 'json',
			data: {
				action: 'place',
				type: type,
				place: place,
				rand:Math.random()},
			success: function(data){

				$(append).before(data.form);
				$('#suggest'+data.fix).suggest("/service/source/db.place", {
						maxCacheSize: -1,
						minchars: 2,
						delay: 450,
						action: 'search',
						onSelect: function(value, key) {
							$('#place'+data.fix).get(0).value = key;
						},
						onSuggest: function() {
							var city = $('#city'+data.fix).get(0);
							var city_text = $('#city_text'+data.fix);

							city = city.options[city.selectedIndex].value > 0 ? city.options[city.selectedIndex].text : city_text.val();
							this.data.city = '';
							this.data.type = type;

							if ( city != '' ) {
								this.data.city = city;
							} else
								alert('Пожалуйста укажите город!');
						}
					}
				);
				$('#faculty_suggest'+data.fix).suggest("/service/source/db.place", {
						maxCacheSize: -1,
						minchars: 2,
						action: 'search_faculty',
						delay: 450,
						onSelect: function(value, key) {
							$('#faculty'+data.fix).get(0).value = key;
						},
						onSuggest: function() {
							var place = $("#place"+data.fix).get(0).value;

							this.data.place = '';

							if ( place != '' )
								this.data.place = place;
							/*else
								alert('Пожалуйста, укажите ВУЗ!');*/
						}
					}
				);
				$('#chair_suggest'+data.fix).suggest("/service/source/db.place", {
						maxCacheSize: -1,
						minchars: 2,
						action: 'search_chair',
						delay: 450,
						onSelect: function(value, key) {
							$('#chair'+data.fix).get(0).value = key;
						},
						onSuggest: function() {
							var faculty = $("#faculty"+data.fix).get(0).value;

							this.data.faculty = '';

							if ( faculty != '' )
								this.data.faculty = faculty;
							/*else
								alert('Пожалуйста, укажите Факультет!');*/
						}
					}
				);

				$('#spec_suggest'+data.fix).suggest("/service/source/db.place", {
						maxCacheSize: -1,
						minchars: 2,
						action: 'search_spec',
						delay: 450,
						onSelect: function(value, key) {
							$('#spec'+data.fix).get(0).value = key;
						}
					}
				);
			},
			type: 'POST'});
	},

	getPlaceForm: function(type, append, place) {

		if ( !place )
			place = 0;

		$.ajax({
			url: '.',
			dataType: 'json',
			data: {
				action: 'place',
				type: type,
				place: place,
				rand:Math.random()
			},
			success: function(data){

				$(append).append(data.form);

				$('#suggest'+data.fix).autocomplete("/service/source/db.place", {
					extraParams: {
						action: 'search_place',
						type: type,
						code: ''
					},
					dataType: 'json',
					parse: function(json) {
						var parsed = [];

						if ( !json || !json.length )
							return parsed;

						for (var i in json)
							parsed[parsed.length] = {
								data: json[i].Name,
								value: json[i].PlaceID,
								result: json[i].Name
							};

						return parsed;
					},
					formatItem: function(text, i, max, value) {
						return text;
					},
					max: 20
				}).result(
					function(event, Name, Code) {
						$('#place'+data.fix).val(Code);

						$('#faculty_suggest'+data.fix).val('');
						$('#faculty'+data.fix).val('');

						$('#chair'+data.fix).val('');
						$('#chair_suggest'+data.fix).val('');

						$('#faculty_suggest'+data.fix).setOptions({
							extraParams: {
								action: 'search_faculty_json',
								place: Code
							}
						});

						$('#chair_suggest'+data.fix).setOptions({
							extraParams: {
								action: 'search_chair_json',
								place: Code
							}
						});
					}
				);


				if ($('#city'+data.fix).size())
					$('#city'+data.fix).change();
				else {
					$('#suggest'+data.fix).setOptions({
						extraParams: {
							'code': $('div.location-inline input:eq(0)', append).val(),
							'action': 'search_place',
							'type': type
						}
					});
				}

				$('#faculty_suggest'+data.fix).autocomplete("/service/source/db.place",{
					extraParams: {
						action: 'search_faculty_json',
						place: place
					},
					dataType: 'json',
					parse: function(json) {
						var parsed = [];

						if ( !json || !json.length )
							return parsed;

						for (var i in json)
							parsed[parsed.length] = {
								data: json[i].Name,
								value: json[i].FacultyID,
								result: json[i].Name
							};

						return parsed;
					},
					formatItem: function(text, i, max, value) {
						return text;
					},
					max: 20
				}).result(
					function(event, Name, Code) {

						$('#faculty'+data.fix).val(Code);
						$('#chair'+data.fix).val('');
						$('#chair_suggest'+data.fix).val('');

						$('#chair_suggest'+data.fix).setOptions({
							extraParams: {
								action: 'search_chair_json',
								faculty: Code,
								place: $('#place'+data.fix).val()
							}
						});
					}
				);

				$('#chair_suggest'+data.fix).autocomplete("/service/source/db.place",{
					extraParams: {
						action: 'search_chair_json',
						place: place,
						faculty: 0
					},
					dataType: 'json',
					parse: function(json) {
						var parsed = [];

						if ( !json || !json.length )
							return parsed;

						for (var i in json)
							parsed[parsed.length] = {
								data: json[i].Name,
								value: json[i].KafedraID,
								result: json[i].Name
							};

						return parsed;
					},
					formatItem: function(text, i, max, value) {
						return text;
					},
					max: 20
				}).result(
					function(event, Name, Code) {
						$('#chair'+data.fix).val(Code);
					}
				);

				$('#spec_suggest'+data.fix).autocomplete("/service/source/db.place",{
					extraParams: {
						action: 'search_spec_json',
						place: place,
						faculty: 0
					},
					dataType: 'json',
					parse: function(json) {
						var parsed = [];

						if ( !json || !json.length )
							return parsed;

						for (var i in json)
							parsed[parsed.length] = {
								data: json[i].Name,
								value: json[i].ChairID,
								result: json[i].Name
							};

						return parsed;
					},
					formatItem: function(text, i, max, value) {
						return text;
					},
					max: 20
				}).result(
					function(event, Name, Code) {

					}
				);
			},
			type: 'POST'
		});
	},

	removePlace: function(place, type) {
		// temp
	}
};

