﻿var container, dataItem, reg,
cityId, citySelectorId, birthDateId, loginId, passwordId, confirmPasswordId,
birthdateId, birthdateRequiredId, birthdateFormatId,
customCityId, countrySelectorId, firstnameId, lastnameId, emailId, maleId, femaleId;

function init_registration
(value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11, value12, value13, value14, value15, value16, value17)
{
	cityId = value1;
	citySelectorId = value2;
	birthDateId = value3;
	loginId = value4;
	passwordId = value5;
	confirmPasswordId = value6;
	birthdateId = value7;
	birthdateRequiredId = value8;
	birthdateFormatId = value9;
	customCityId = value10;
	reg = value11 == "True";
	countrySelectorId = value12;
	firstnameId = value13;
	lastnameId = value14;
	emailId = value15;
	maleId = value16;
	femaleId = value17;
}

function ValidateFirstName(source, clientside_arguments) { clientside_arguments.IsValid = CheckValid(firstnameId); }
function ValidateLastName(source, clientside_arguments) { clientside_arguments.IsValid = CheckValid(lastnameId); }
function ValidateConfirmPassword(source, clientside_arguments) { clientside_arguments.IsValid = CheckValid(confirmPasswordId); }
function EmailRequired(source, clientside_arguments) { clientside_arguments.IsValid = EmailValidationNonEmpty(emailId); }
function ValidateEmail(source, clientside_arguments) { clientside_arguments.IsValid = EmailValidationFormat(emailId); }
function ValidateGender(source, clientside_arguments) { clientside_arguments.IsValid = $(maleId + ':checked').val() != undefined || $(femaleId + ':checked').val() != undefined; }

function ValidateCity(source, clientside_arguments)
{
	if ($(citySelectorId).val() > 0 || ($(citySelectorId).val() == -1 && $('#<%= CustomCity.ClientID %>').val() != ""))
	{
		clientside_arguments.IsValid = true;
		$('#CityDropDown').removeClass('err');
		$(customCityId).removeClass('err');
	}
	else
	{
		clientside_arguments.IsValid = false;
		if ($(citySelectorId).val() == 0)
		{
			$('#CityDropDown').addClass('err');
			$(customCityId).removeClass('err');
		}
		else
		{
			$(customCityId).addClass('err');
			$('#CityDropDown').removeClass('err');
		}
	}
}

function ValidatePasswordConfirmation(source, clientside_arguments)
{
	var tmp = $('#' + confirmPasswordId).val();
	clientside_arguments.IsValid = StringIsEmpty(tmp) || ($('#' + passwordId).val() == tmp);
}

$(document).ready(function() {
	$(birthDateId)
		.datepicker({ changeMonth: true, changeYear: true, firstDay: 1, dateFormat: 'dd-mm-yy', minDate: '-100Y', maxDate: '', showMonthAfterYear: true })
		.datepicker('option', 'yearRange', '-70:+30')
		.datepicker('option', 'dayNamesMin', ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'])
		.datepicker('option', 'monthNamesShort', ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь']);
});

function OnLoadCitiesCallback(data)
{
	if (data)
	{
		container = document.getElementById("CityDropDown");
		if (container)
		{
			container.options.length = reg ? 1 : 0;

			data = data.split(',');
			
			for (var iterator in data)
			{
				dataItem = data[iterator].split('=');

				option = document.createElement("OPTION");
				option.value = dataItem[0];
				option.text = dataItem[1];
				option.selected = (dataItem[0] == cityId);

				container.options.add(option);
			}
			option = document.createElement("OPTION");
			option.value = -1;
			option.text = "Другой город";
			option.selected = cityId == "-1";
			container.options.add(option);

			passValue(container.value);

			container.disabled = false;

			if(reg) container.focus();
		}
	}
}

function passValue(value)
{
	$(citySelectorId).val(value);

	cityId = value;

	if (value == -1)
		$(customCityId).removeClass('ui-tabs-hide');
	else
		$(customCityId).addClass('ui-tabs-hide');
}

function InitialLoadCities(countryControl)
{
	var country = document.getElementById(countryControl);
	if (country != null)
	{
		country = parseInt(country.value);
		if (country > 0)
		{
			LoadCities(country);
		}
	}
}

function LoadCities(CountryId)
{
	if (CountryId != 0)
	{
		new AJAXConnector('/GetCities.ashx?CountryId=' + CountryId, OnLoadCitiesCallback).doGet();
	}
	else
	{
		container = document.getElementById("CityDropDown");
		container.selectedIndex = 0;
		container.disabled = true;
		$(citySelectorId).val(0);
	}
}

function ValidateLogin(source, args)
{
	var minLoginLength = 4;
	var maxLoginLength = 12;
	var userLogin = $("#" + loginId).val();

	var errMsg = checkLogin(userLogin, minLoginLength, maxLoginLength);

	$(source).text(errMsg);
	if (errMsg.length > 0)
	{
		SetValidCssClass(loginId, false);
		args.IsValid = false;
	}
	else
	{
		SetValidCssClass(loginId, true);
		args.IsValid = true;
	}
}

function ValidatePassword(source, args)
{
	var minPassLength = 5;
	var maxPassLength = 15;
	var userLogin = $("#" + loginId).val();
	var userPassword = $("#" + passwordId).val();

	var errMsg = checkPassword(userPassword, userLogin, minPassLength, maxPassLength);

	$(source).text(errMsg);
	if (errMsg.length > 0)
	{
		SetValidCssClass(passwordId, false);
		args.IsValid = false;
	}
	else
	{
		SetValidCssClass(passwordId, true);
		args.IsValid = true;
	}
}

function ValidateBirthDate()
{
	var value = $(birthdateId).val();

	var dateRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

	if (StringIsEmpty(value))
		$(birthdateRequiredId).css('display', 'inline');
	else
		$(birthdateRequiredId).css('display', 'none');
	if (StringIsEmpty(value) | dateRegExp.test(value))
		$(birthdateFormatId).css('display', 'none');
	else
		$(birthdateFormatId).css('display', 'inline');

	if (!StringIsEmpty(value) & dateRegExp.test(value))
	{
		$(birthdateId).removeClass("err");
		return true;
	}
	else {
		$(birthdateId).addClass("err");
		return false;
	}
}

function ValidateCountry(source, clientside_arguments) {
	if ($(countrySelectorId).val() == 0) {
		clientside_arguments.IsValid = false;
		$(countrySelectorId).addClass('err');
	}
	else {
		clientside_arguments.IsValid = true;
		$(countrySelectorId).removeClass('err');
	}
}