﻿function HideAsterisk(elWhich, sId) {
	if (elWhich.value == '')
		document.images[sId].src = '/f/1/required.gif'
	else
		document.images[sId].src = '/f/1/0.gif'
}

function SetDisabled(bDisabled, sId) {
	elOnWhich = document.getElementById(sId)

	if (elOnWhich) {
		if (bDisabled)
			elOnWhich.checked = false
		elOnWhich.disabled = bDisabled
	}

	elOnWhichLabel = document.getElementById('l_' + sId)

	if (elOnWhichLabel) {
		elOnWhichLabel.disabled = bDisabled
	}

	if (document.images['i_' + sId])
		if (bDisabled)
			document.images['i_' + sId].src = '/f/1/required-d.gif'
		else
			document.images['i_' + sId].src = '/f/1/required.gif'
}


function IfFiledEnable(sWhichId, sId) {
	if (document.getElementById(sWhichId).value != '')
		SetDisabled(false, sId)
}


function IfFiledDisable(sWhichId, sId) {
	if (document.getElementById(sWhichId).value != '')
		SetDisabled(true, sId)
}


function IfNotFiledEnable(sWhichId, sId) {
	if (document.getElementById(sWhichId).value == '')
		SetDisabled(true, sId)
}


function IfNotFiledDisable(sWhichId, sId) {
	if (document.getElementById(sWhichId).value == '')
		SetDisabled(true, sId)
}


function IfCheckedEnable(sWhichId, sId) {
	if (document.getElementById(sWhichId).checked)
		SetDisabled(false, sId)
}


function IfCheckedDisable(sWhichId, sId) {
	if (document.getElementById(sWhichId).checked)
		SetDisabled(true, sId)
}


function IfNotCheckedDisable(sWhichId, sId) {
	if (!document.getElementById(sWhichId).checked)
		SetDisabled(true, sId)
}


function IfNotCheckedEnable(sWhichId, sId) {
	if (!document.getElementById(sWhichId).checked)
		SetDisabled(false, sId)
}


function FilterAlphaSymbols(sTarget)
{
	return sTarget.substring(0, sTarget.length - 1)
}

function CheckDayTruth(oEvent, oElement)
{
	if (
			oEvent && oEvent.type == 'keyup' &&
			(
					isNaN(oElement.value) ||
					( oElement.value < 1 || oElement.value > 31 )
					)
			)
	{
		oElement.value = FilterAlphaSymbols(oElement.value)
	}
	return true
}

function CheckRussainTruth(oEvent, oElement)
{
	if (oEvent && oEvent.type == 'keyup')
	{
		oElement.value = oElement.value.replace(/[a-zієї]/gi, '')
	}

	/*	)
	{
		oElement.value = FilterAlphaSymbols( oElement.value )
	}*/
	return true
}


function CheckMonthTruth(evt)
{
	oEvent = window.event ? window.event : evt
	oElem = window.event ? oEvent.srcElement : oEvent.currentTarget
	if (
			oEvent && oEvent.type == 'keyup' &&
			(
					isNaN(oElem.value) ||
					( oElem.value < 1 || oElem.value > 12 )
					)
			)
	{
		oElem.value = FilterAlphaSymbols(oElem.value)
	}
	return true
}

function CheckYearTruth(evt)
{
	oEvent = window.event ? window.event : evt
	oElem = window.event ? oEvent.srcElement : oEvent.currentTarget

	if (oEvent && oEvent.type == 'keyup' && isNaN(oElem.value))
	{
		oElem.value = FilterAlphaSymbols(oElem.value)
	}

	if (oEvent && oEvent.type == 'blur' && !isNaN(oElem.value))
	{
		if (oElem.value.length == 2 && oElem.value > 10) oElem.value = '19' + oElem.value;
		if (( oElem.value.length == 2 ) && oElem.value < 11) oElem.value = '20' + oElem.value;
		if (( oElem.value.length == 1 ) && oElem.value < 11) oElem.value = '200' + oElem.value;
		if (oElem.value.length != 2 && oElem.value.length != 4) oElem.value = '';
	}
	return true;
}


function ValidateProfileForm(f)
{

	f = f.form
	var isNewUser = !( (f.DocKey && f.DocKey.value) || (f.PersonKey && f.PersonKey.value) )
	if (isNewUser && f.Login.value)
	{
		var xml = LoadXml('/_getlogin.html?login=' + escape(f.Login.value))
		/*alert('/_getlogin.html?login=' + encodeURI(f.Login.value))*/
		if (xml.documentElement.firstChild.nodeValue == '1')
		{
			StepEnable(StepArray[0])
			f.Login.focus()
			alert('ОШИБКА!\nПользователь с логином \"' + f.Login.value + '\" уже зарегистрирован в системе.')
			return false
		}
	}

	if (isNewUser && ( !f.Passwd.value || ( f.Passwd.value != f.PasswdDbl.value ) ))
	{
		StepEnable(StepArray[0])
		f.Passwd.focus()
		alert('ОШИБКА!\nНе заполнено поле \"Пароль\" \nили или значение поля \"Пароль\" не совпадает\nсо значением поля \"Повтор пароля\"')
		return false
	}

	// все поля заполнены правильно (ставим 2)
	//(единица ставится если такого логина еще нет в базе и пароль введен 2 раза одинаково)

	if (bCheckValidateState != 2)
	{
		bCheckValidateState = 0
	}

	bCheckValidateState = bCheckValidateState + 1;
	CheckValidateStateAndDisableSubmitButton();
	return true;
}

function CheckValidateStateAndDisableSubmitButton()
{
	if (bCheckValidateState & 1 && bCheckValidateState & 2) {
		document.getElementById('DoSubmit').disabled = true;
		return true;
	}
}
