﻿// JScript File
function SetButtonStyle(Obj, bOver)
{
	var cls = Obj.className;
	var re = new RegExp(bOver ? "Out" : "Over", "g");
	Obj.className = cls.replace(re, bOver ? "Over" : "Out");
}

function HighlightButton(Obj, bOver)
{
	var img = Obj.src;
	var re = new RegExp(bOver ? "out" : "over", "g");
	Obj.src = img.replace(re, bOver ? "over" : "out");
}

function CalcCtrlLeftOffset(ctrl, start)
{
	var res = start;
	if (ctrl)
	{
		res += ctrl.offsetLeft;
		if (ctrl.offsetParent) res = CalcCtrlLeftOffset(ctrl.offsetParent, res);
	}
	return res;
}

function CalcCtrlTopOffset(ctrl, start)
{
	var res = start;
	if (ctrl)
	{
		res += ctrl.offsetTop;
		if (ctrl.offsetParent) res = CalcCtrlTopOffset(ctrl.offsetParent, res);
	}
	return res;
}

function SelectUsers(page, title, showUsers, showTeachers, showStaff, showRoles, showGRoles, singleSel, onSelComplete)
{
	var w = 600;
	var h = 400;
	var l = (window.screen.width - w) / 2;
	var t = (window.screen.height - h) / 2;
	var f = 0;
	if (showUsers) f |= 0x01;
	if (showTeachers) f |= 0x02;
	if (showStaff) f |= 0x04;
	if (showRoles) f |= 0x10;
	if (showGRoles) f |= 0x20;
	if (singleSel) f |= 0x80;
	var wnd = window.open(page + "?op=" + title + "&width=" +
		w + "&height=" + h + "&flags=" + f + "&callback=" + onSelComplete + "", 
		"_blank", "width=" + w + ", height=" + h + ", left=" + l + ", top=" + t + 
		", directories=no, fullscreen=no, location=no, menubar=no, resizable=no, status=no, toolbar=no, scrollbars=yes");
	if (wnd)
	{
		wnd.title = title;
		wnd.focus();
	}
}

function SelectPage(page, title, onSelComplete)
{
	var w = 600;
	var h = 400;
	var l = (window.screen.width - w) / 2;
	var t = (window.screen.height - h) / 2;
	var wnd = window.open(page + "?width=" + w + "&height=" + h + "&callback=" + 
		onSelComplete + "", "_blank", 
		"width=" + w + ", height=" + h + ", left=" + l + ", top=" + t + 
		", directories=no, fullscreen=no, location=no, menubar=no, resizable=no, status=no, toolbar=no, scrollbars=yes");
	if (wnd)
	{
		wnd.title = title;
		wnd.focus();
	}
}

function SelectSite(page, title, onSelComplete)
{
	var w = 600;
	var h = 400;
	var l = (window.screen.width - w) / 2;
	var t = (window.screen.height - h) / 2;
	var wnd = window.open(page + "?width=" + w + "&height=" + h + "&callback=" + 
		onSelComplete + "", "_blank", 
		"width=" + w + ", height=" + h + ", left=" + l + ", top=" + t + 
		", directories=no, fullscreen=no, location=no, menubar=no, resizable=no, status=no, toolbar=no, scrollbars=yes");
	if (wnd)
	{
		wnd.title = title;
		wnd.focus();
	}
}

function HighlightRequiredField(Fld, MinLen)
{
	if (Fld)
	{
		if (Fld.value.length < MinLen)
		{
			Fld.style.backgroundColor = "#FF6666";
		} else
		{
			Fld.style.backgroundColor = "";
		}
	}
}

function Number2String(Value, Len, Def)
{
	var Out = "";
	var Num = new Number(Value);
	if (isNaN(Num)) 
		Out = "" + Def;
	else
		Out = "" + Value;
	if (Out.length > Len)
	{
		Out = Out.substring(0, Len);
	} else if (Out.length < Len)
	{
		var a = Out.length;
		for (var i = 0; i < (Len - a); i++) Out = "0" + Out;
	}
	return Out;
}

function FilterDateInput(evt, Field)
// В поля даты можно вводить только символы 0-9 и .
{
	var code = 0;
	if (evt.keyCode) code = evt.keyCode;
	else if (evt.which) code = evt.which;
	else if (evt["keyCode"]) code = evt["keyCode"];
	else return false;
	if (!document.all)
	{
		if ((code < 32) || (code == 45) || (code == 46) || 
			((code >= 33) && (code <= 40)))	return true;
	}
	var AllowedChars = "0123456789.-";
	var PrevText = Field.value;
	var KeyCode = String.fromCharCode(code);
	if (AllowedChars.indexOf(KeyCode) < 0)
	{
		evt.cancelBubble = true;
		evt.returnValue = false;
		return false;
	}
	if (KeyCode == "-") 
	{
		if (evt.keyCode) evt.keyCode = AllowedChars.charCodeAt(10);
		else if (evt.which) evt.which = AllowedChars.charCodeAt(10);
		else evt["keyCode"] = AllowedChars.charCodeAt(10);
	}
	return true;
}

function ClearDateString(Val)
{
	var out = "";
	for (var i = 0; i < Val.length; i++)
	{
		var c = Val.charAt(i);
		if (c == "-") c = "."
		out += c;
	}
	return out;
}

function CheckDateInput(Field)
{
	var ItemText = "" + Field.value;
	if (ItemText.length < 1) return false;
	var DateText = ClearDateString(ItemText);
	var a = DateText.indexOf(".");
	var y = "";
	var m = "";
	var d = "";
	if (a > 0)
	{
		d = Number2String(DateText.substring(0, a), 2, 1);
		m = DateText.substring(a + 1, DateText.length, 1);
		a = m.indexOf(".");
		if (a > 0)
		{
			y = Number2String(m.substring(a + 1, m.length), 4, 5);
			m = Number2String(m.substring(0, a), 2, 1);
		} else
		{
			m = Number2String(m.substring(0, 2), 2, 1);
			y = Number2String(5, 4, 5);
		}
	} else
	{
		d = Number2String(DateText.substring(0, 2), 2, 1);
		m = Number2String(1, 2, 1);
		y = Number2String(5, 4, 5);
	}
	if ((1 * y) < 100) y = Number2String(2000 + 1 * y, 4, 5);
	if (d < 0)
	{
		alert("Число не может быть меньше 1!");
		d = "01";
	}
	if (d > 31)
	{
		alert("В месяце может быть не более 31 дня!");
		d = 30;
	}
	if (m < 0)
	{
		alert("Месяц не может быть меньше 1!");
		m = "01";
	}
	if (m > 12)
	{
		alert("В году всего 12 месяцев!");
		m = 12;
	}
	if ((y > 2035) || (y < 1900))
	{
		alert("Недопустимый год!");
		y = 2000;
	}
	Field.value = d + "." + m + "." + y;
	return true;
}

function ConvertString2Date(DateText)
{
	if (DateText.length < 1) return null;
	var a = DateText.indexOf(".");
	var y = "";
	var m = "";
	var d = "";
	if (a > 0)
	{
		d = Number2String(DateText.substring(0, a), 2, 1);
		m = DateText.substring(a + 1, DateText.length, 1);
		a = m.indexOf(".");
		if (a > 0)
		{
			y = Number2String(m.substring(a + 1, m.length), 4, 5);
			m = Number2String(m.substring(0, a), 2, 1);
		} else
		{
			m = Number2String(m.substring(0, 2), 2, 1);
			y = Number2String(5, 4, 5);
		}
	} else
	{
		d = Number2String(DateText.substring(0, 2), 2, 1);
		m = Number2String(1, 2, 1);
		y = Number2String(5, 4, 5);
	}
	if ((1 * y) < 100) y = Number2String(2000 + 1 * y, 4, 5);
	if (d < 0) d = 1;
	if (d > 31) d = 31;
	if (m < 0) m = 1;
	if (m > 12) m = 12;
	if ((y > 2035) || (y < 1900)) y = 2000;
	return new Date(y, m, d, 0, 0, 0, 0);
}

function FilterIPInput(evt, Field)
// В поля IP-адреса можно вводить только символы 0-9 и .
{
	var code = 0;
	if (evt.keyCode) code = evt.keyCode;
	else if (evt.which) code = evt.which;
	else if (evt["keyCode"]) code = evt["keyCode"];
	else return false;
	if (!document.all)
	{
		if ((code < 32) || (code == 45) ||  
			((code >= 33) && (code <= 40)) ||
			((code >= 112) && (code <= 123)))	return true;
	}
	var AllowedChars = "0123456789.";
	var PrevText = Field.value;
	var KeyCode = String.fromCharCode(code);
	if (AllowedChars.indexOf(KeyCode) < 0)
	{
		evt.cancelBubble = true;
		evt.returnValue = false;
		return false;
	}
	return true;
}

function CheckIPInput(Field)
{
	var ItemText = "" + Field.value;
	if (ItemText.length < 1) return false;
	var a = ItemText.indexOf(".");
	var p1 = "";
	var p2 = "";
	var p3 = "";
	var p4 = "";
	if (a > 0)
	{
		p1 = Number2String(ItemText.substring(0, a), 3, 0);
		var p = ItemText.substring(a + 1, ItemText.length, 1);
		a = p.indexOf(".");
		if (a > 0)
		{
			p2 = Number2String(p.substring(0, a), 3, 0);
			p = p.substring(a + 1, p.length, 1);
			a = p.indexOf(".");
			if (a > 0)
			{
				p4 = Number2String(p.substring(a + 1, p.length), 3, 0);
				p3 = Number2String(p.substring(0, a), 3, 0);
			} else
			{
				p3 = Number2String(p.substring(0, 3), 3, 0);
				p4 = "000";
			}
		} else
		{
			p2 = Number2String(p.substring(0, 3), 3, 0);
			p3 = "000";
		}
	} else
	{
		p1 = Number2String(ItemText.substring(0, 3), 3, 0);
		p2 = "000";
		p3 = "000";
		p4 = "000";
	}
	if ((p1 < 0) || (p1 > 255))
	{
		alert("Ни один из компонентов IP-адреса не может быть более 255 или менее 0!");
		p1 = "255";
	}
	if ((p2 < 0) || (p2 > 255))
	{
		alert("Ни один из компонентов IP-адреса не может быть более 255 или менее 0!");
		p2 = "255";
	}
	if ((p3 < 0) || (p3 > 255))
	{
		alert("Ни один из компонентов IP-адреса не может быть более 255 или менее 0!");
		p3 = "255";
	}
	if ((p4 < 0) || (p4 > 255))
	{
		alert("Ни один из компонентов IP-адреса не может быть более 255 или менее 0!");
		p4 = "255";
	}
	Field.value = "" + (1 * p1) + "." + (1 * p2) + "." + (1 * p3) + "." + (1 * p4);
	return true;
}

function ConvertString2IP(ItemText)
{
	if (ItemText.length < 1) return false;
	var a = ItemText.indexOf(".");
	var p1 = "";
	var p2 = "";
	var p3 = "";
	var p4 = "";
	if (a > 0)
	{
		p1 = Number2String(ItemText.substring(0, a), 3, 0);
		var p = ItemText.substring(a + 1, ItemText.length, 1);
		a = p.indexOf(".");
		if (a > 0)
		{
			p2 = Number2String(p.substring(0, a), 3, 0);
			p = p.substring(a + 1, p.length, 1);
			a = p.indexOf(".");
			if (a > 0)
			{
				p4 = Number2String(p.substring(a + 1, p.length), 3, 0);
				p3 = Number2String(p.substring(0, a), 3, 0);
			} else
			{
				p3 = Number2String(p.substring(0, 3), 3, 0);
				p4 = "000";
			}
		} else
		{
			p2 = Number2String(p.substring(0, 3), 3, 0);
			p3 = "000";
		}
	} else
	{
		p1 = Number2String(ItemText.substring(0, 3), 3, 0);
		p2 = "000";
		p3 = "000";
		p4 = "000";
	}
	if ((p1 < 0) || (p1 > 255)) p1 = "255";
	if ((p2 < 0) || (p2 > 255)) p2 = "255";
	if ((p3 < 0) || (p3 > 255)) p3 = "255";
	if ((p4 < 0) || (p4 > 255)) p4 = "255";
	return (1 * p4) + (256 * p3) + (65536 * p2) + (16777216 * p1);
}

function FilterAnyInput(evt, Field, AllowedChars)
{
	var code = 0;
	if (evt.keyCode) code = evt.keyCode;
	else if (evt.which) code = evt.which;
	else if (evt["keyCode"]) code = evt["keyCode"];
	else return false;
	if (!document.all)
	{
		if ((code < 32) || (code == 45) || (code == 46) || 
			((code >= 33) && (code <= 40)))	return true;
	}
	var PrevText = Field.value;
	var KeyCode = String.fromCharCode(code);
	if (AllowedChars.indexOf(KeyCode) < 0)
	{
		evt.cancelBubble = true;
		evt.returnValue = false;
		return false;
	}
	return true;
}

function FilterIntegerInput(evt, Field)
// В целочисленные поля можно вводить только символы 0-9.
{
	var code = 0;
	if (evt.keyCode) code = evt.keyCode;
	else if (evt.which) code = evt.which;
	else if (evt["keyCode"]) code = evt["keyCode"];
	else return false;
	if (!document.all)
	{
		if ((code < 32) || (code == 45) ||  
			((code >= 33) && (code <= 40)) ||
			((code >= 112) && (code <= 123)))	return true;
	}
	var AllowedChars = "0123456789";
	var PrevText = Field.value;
	var KeyCode = String.fromCharCode(code);
	if (AllowedChars.indexOf(KeyCode) < 0)
	{
		evt.cancelBubble = true;
		evt.returnValue = false;
		return false;
	}
	return true;
}

function CheckIntegerInput(Field)
{
	var ItemText = "" + Field.value;
	if (ItemText.length < 1) return false;
	var Val = Number2String(ItemText, 10, 0);
	Field.value = "" + (1 * Val);
	return true;
}

function FilterDecimalInput(evt, Field, Decimals)
// В целочисленные поля можно вводить только символы 0-9, запятую и точку.
{
	var code = 0;
	if (evt.keyCode) code = evt.keyCode;
	else if (evt.which) code = evt.which;
	else if (evt["keyCode"]) code = evt["keyCode"];
	else return false;
	if (!document.all)
	{
		if ((code < 32) || (code == 45) ||  
			((code >= 33) && (code <= 40)) ||
			((code >= 112) && (code <= 123)))	return true;
	}
	var AllowedChars = "0123456789.,";
	var PrevText = Field.value;
	var KeyCode = String.fromCharCode(code);
	if (AllowedChars.indexOf(KeyCode) < 0)
	{
		evt.cancelBubble = true;
		evt.returnValue = false;
		return false;
	}
	var HasDots = (PrevText.indexOf(AllowedChars.charAt(10)) >= 0);
	var HasCommas = (PrevText.indexOf(AllowedChars.charAt(11)) >= 0);
	if (((KeyCode == ".") || (KeyCode == ",")) && (HasDots || HasCommas))
	{
		evt.cancelBubble = true;
		evt.returnValue = false;
		return false;
	}
	if (PrevText.length > 0)
	{
		var Parts = PrevText.split(".");
		if (Parts.length < 2) Parts = PrevText.split(",");
		if (Parts.length > 1)
		{
			if (Parts[1].length >= Decimals) 
			{
				evt.cancelBubble = true;
				evt.returnValue = false;
				return false;
			}
		}
	}
	return true;
}

function IsValueInRange(Field, MinValue, MaxValue)
{
	if (!Field) return false;
	if (!Field.value) return false;
	var Value = 1.0 * Field.value;
	if (isNaN(Value)) return false;
	if ((Value >= MinValue) && (Value <= MaxValue)) return true;
	return false;
}

function ShowUserInfo(root, id)
{
	var w = 600;
	var h = 400;
	var l = (window.screen.width - w) / 2;
	var t = (window.screen.height - h) / 2;
	var wnd = window.open(root + "UserInfo.aspx?width=" + w + "&height=" + h + "&user=" + id + "", 
		"_blank", "width=" + w + ", height=" + h + ", left=" + l + ", top=" + t + 
		", directories=no, fullscreen=no, location=no, menubar=no, resizable=no, status=no, toolbar=no, scrollbars=yes");
	if (wnd)
	{
		wnd.title = "Информация о пользователе";
		wnd.focus();
	}
	return false;
}

