﻿/*
Author: Serge Neroznaque.
Page Type: Client-side Include.
Page Purpose: Contains client-side global routines. Mainly custom form field validators.
Warning: The code is closed and the page shall not be changed by anyone except author.
*/
// <script>
function invalidName(strSubj)
{
	return (strSubj.charAt(0) == " " || strSubj.charAt(strSubj.length - 1) == " ");
}

function checkText(txtRef, strLabel, iLen) // iLen -- is optional parameter. useful when you want to check textareas
{
	if (strLabel == null) strLabel = "TEXT field"
	if (txtRef.value.length == 0) {
		alert("Требуется ввести значение для \"" + strLabel + "\" перед продолжением.");
		txtRef.focus();
		return false;
	}
	else if (invalidName(txtRef.value)) {
		alert("\"" + strLabel + "\" не может содержать пробелы в начале, либо в конце.");
		txtRef.focus();
		txtRef.select();
		return false;
	}
	else if (iLen != null && txtRef.value.length > parseInt(iLen)) {
		alert("Поле \"" + strLabel + "\" слишком длинное (" + txtRef.value.length + " символов).\Пожалуйста уменьшите длину до " + iLen + " символов.");
		txtRef.focus();
		txtRef.select();
		return false;
	}
	return true;
}

function checkNumber(txtRef, strLabel, allowFloat)
{
	var i;
	if (allowFloat) {
		for (i = 0; i < txtRef.value.length; i++){
			var arra = txtRef.value.split(".");
			if (arra.length > 2) {
				alert("Extra decimal divider characters in \"" + strLabel + "\" detected.\nThis field should contain digits and/or dot only.");
				txtRef.focus();
				txtRef.select();
				return false;
			}
			else if (!( txtRef.value.charAt(i) >= "0" && txtRef.value.charAt(i) <= "9" || txtRef.value.charAt(i) == ".")) {
				alert("Invalid characters in \"" + strLabel + "\" detected.\nThis field should contain digits and/or dot only.");
				txtRef.focus();
				txtRef.select();
				return false;
			}
		}
	}
	else {
		for (i = 0; i < txtRef.value.length; i++){
			if (!(txtRef.value.charAt(i) >= "0" && txtRef.value.charAt(i) <= "9")) {
				alert("Invalid characters in \"" + strLabel + "\" detected.\nThis field should contain digits only.");
				txtRef.focus();
				txtRef.select();
				return false;
			}
		}
	}
	return true;
}

// expects string as first parameter
function checkNumberModA(strValue, strLabel, allowFloat)
{
	var i;
	if (strValue == "") return false;

	if (allowFloat) {
		for (i = 0; i < strValue.length; i++){
			var arra = strValue.split(".");
			if (arra.length > 2) {
				return false;
			}
			else if (!( strValue.charAt(i) >= "0" && strValue.charAt(i) <= "9" || strValue.charAt(i) == ".")) {
				return false;
			}
		}
	}
	else {
		for (i = 0; i < strValue.length; i++){
			if (!(strValue.charAt(i) >= "0" && strValue.charAt(i) <= "9")) {
				return false;
			}
		}
	}
	return true;
}

function checkEmail(strValue, strLabel) // expect data in format nnn@nnn.nnn
{
	var objRegExp = new RegExp("^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+[.][A-Za-z]+$");
	 
	if (objRegExp.test(strValue))
		return true;
	alert("Обнаружен некорректный формат электронного адреса в поле \"" + strLabel + "\".");
	return false;
}


function validateForm(frmRef)
{
	for (var i = 0; i < frmRef.elements.length; i++)
		if (frmRef.elements[i].required && !checkText(frmRef.elements[i], frmRef.elements[i].required, frmRef.elements[i].maxlength))
			return false;
	if (frmRef.datIsCool)
		frmRef.datIsCool.value = "Yes";
	return true;
}

function validateFormEx(frmRef) // extends validateForm. also checks required <select>
{
	for (var i = 0; i < frmRef.elements.length; i++)
		if (frmRef.elements[i].required) {
			if (frmRef.elements[i].type.indexOf("select") != -1) {
				var valua = frmRef.elements[i].options[frmRef.elements[i].selectedIndex].value;
				if (valua == "" || valua == "0") {
					alert("Пожалуйста, выберите значение из \"" + frmRef.elements[i].required + "\".");
					frmRef.elements[i].focus();
					return false;
				}
			}
			else if (!checkText(frmRef.elements[i], frmRef.elements[i].required, frmRef.elements[i].maxlength))
				return false;
		}
	if (frmRef.datIsCool)
		frmRef.datIsCool.value = "Yes";
	return true;
}

function validateFormExModA(frmRef)
// extends validateFormEx. also invokes custom validator specified by "validator" expando
// custom validator must comply with format:
// function customValidator(strValue, strFieldLabel) { return true/false; }
{
	for (var i = 0; i < frmRef.elements.length; i++)
		if (frmRef.elements[i].required || (frmRef.elements[i].value && frmRef.elements[i].value.length > 0)) {
			if (frmRef.elements[i].validator) {
				if (!eval(frmRef.elements[i].validator + "(frmRef.elements[i].value, frmRef.elements[i].required)")) {
					frmRef.elements[i].focus();
					if (frmRef.elements[i].select)
						frmRef.elements[i].select();
					return false;
				}
			}
			else if (frmRef.elements[i].type.indexOf("select") != -1 && frmRef.elements[i].required != null) {
				var valua;
				var iInd = frmRef.elements[i].selectedIndex;

				if (iInd < 0)
					valua = "";
				else
					valua = frmRef.elements[i].options[iInd].value;

				if (valua == "" || valua == "0") {
					alert("Пожалуйста, выберите значение из \"" + frmRef.elements[i].required + "\".");
					frmRef.elements[i].focus();
					return false;
				}
			}
			else if (!checkText(frmRef.elements[i], frmRef.elements[i].required, frmRef.elements[i].maxlength))
				return false;
		}
	if (frmRef.datIsCool)
		frmRef.datIsCool.value = "Yes";
	return true;
}

// proxy for usage in expando denoting custom form field validator
function customDateValidator(strValue, strFieldLabel)
{
	if (strFieldLabel == null)
		strFieldLabel = "Поле с датой";
	if (!validateDate(strValue)) {
		alert("Неверный формат даты в поле: " + strFieldLabel + ".\nТребуемый формат dd.mm.yyyy.");
		return false;
	}
	return true
}

// proxy for usage in expando denoting custom form field validator
function customIntegerValidator(strValue, strFieldLabel)
{
	if (strFieldLabel == null)
		strFieldLabel = "Целочисленное поле";
	if (!checkNumberModA(strValue, strFieldLabel, false)) {
		alert("Неверное целочисленное значение в поле: " + strFieldLabel + ".");
		return false;
	}
	return true
}

// proxy for usage in expando denoting custom form field validator
function customFloatValidator(strValue, strFieldLabel)
{
	if (strFieldLabel == null)
		strFieldLabel = "Float field";
	if (!checkNumberModA(strValue, strFieldLabel, true)) {
		alert("Error validating float at field: " + strFieldLabel + ".");
		return false;
	}
	return true
}

function validateDate(strValue) // expect data in format dd.mm.yyyy
{
	if ((/^\d{1,2}(\.)\d{1,2}\1\d{4}$/).test(strValue)) {
		var aDate = strValue.split(".");
		var iYear = parseInt(aDate[2], 10);
		var iMonth = parseInt(aDate[1], 10);
		var iDay = parseInt(aDate[0], 10);

		var days = {'1' : 31, '2' : function (){return (iYear % 4 == 0 ? 29 : 28);}, '3' : 31, '4' : 30, '5' : 31, '6' : 30, '7' : 31,
			'8' : 31, '9' : 30, '10' : 31, '11' : 30, '12' : 31};

		if(iDay <= eval("days[iMonth]" + (iMonth == 2 ? "()" : "")) && iDay > 0) return true;
	}
	return false;
}

function timeValidator(strValue, strLabel)
{
	if (/^(([0-1][0-9])|(2[0-3])):[0-5][0-9]$/.test(strValue))
		return true;
	alert("Invalid time value in \"" + (strLabel == null || strLabel == '' ? 'Time' : strLabel) + "\" detected.\nRequired format: hh:mm.");
	return false;
}

function priceChecker(strValue, strLabel)
{
	if (/^\d+(,\d{2})?$/.test(strValue))
		return true;
	alert("Неверное значение цены в поле \"" + (strLabel == null || strLabel == '' ? 'Цена' : strLabel) + "\".\nПравильный пример: 19,99 или 28,00.");
	return false;
}

// borrowed from Utopia technologies
function selectShow(reallyShow, replaceWithFake)
{
	var daBody = document.body;
	var sels = daBody.all.tags("SELECT");
	if (sels.length > 0) {
		for (var i = 0; i < sels.length; i++) {
			sels[i].style.visibility = (reallyShow ? 'inherit' : 'hidden');
			if (reallyShow && sels[i].holder != null) {
				sels[i].holder.style.display = 'none';
				daBody.removeChild(sels[i].holder);
				sels[i].holder = null;
			}
			else if (replaceWithFake) {
				var outerSpan = document.createElement("span");
				var innerSpan = document.createElement("span");
				var indicSpan = document.createElement("span");
				var indicDiv = document.createElement("div");
				var r = sels[i].getBoundingClientRect();

				with (outerSpan.style) {
					borderLeft = borderTop = "threedshadow 1px solid";
					borderBottom = borderRight = "threedhighlight 1px solid";
					position = "absolute";
					top = r.top - 2;
					left = r.left - 2;
					width = r.right - r.left;
					height = r.bottom - r.top;
				}

				with (innerSpan.style) {
					borderLeft = borderTop = "threeddarkshadow 1px solid";
					borderBottom = borderRight = "threedlightshadow 1px solid";
					background = "window";
					paddingLeft = "3px";
					paddingTop = "1px";
					width = height = "100%";
					fontFamily = "sans-serif";
					overflow = "hidden";
					fontSize = "xx-small";
					if (sels[i].options.length > 0 && !(sels[i].size > 1))
						innerSpan.innerText = sels[i].options[sels[i].selectedIndex].text;
				}
				
				indicSpan.innerHTML = "6";
				with (indicSpan.style) {
					position = "absolute";
					fontFamily = "Webdings";
					fontSize = "8pt";
					fontWeight = "bold";
					width = "16px";
					height = "16px";
					top = "-2px";
					left = parseInt(outerSpan.style.width) - 17;
				}
				
				with (indicDiv.style) {
					background = "threedface";
					fontSize = "0";
					position = "absolute";
					width = "16px";
					height = "15px";
					top = "1px";
					left = parseInt(outerSpan.style.width) - 19;
					border = "thin outset";
				}
				
				sels[i].holder = outerSpan;
				outerSpan.appendChild(innerSpan);
				
				if (sels[i].size > 1) {
					// handle listbox and print the entries of box
					var aItems = new Array();
					for (var iItems = 0; iItems < sels[i].options.length; iItems++) {
						aItems[iItems] = sels[i].options[iItems].text;
					}
					innerSpan.innerHTML = aItems.join("<br />");
				}
				else {
					outerSpan.appendChild(indicDiv);
					outerSpan.appendChild(indicSpan);
				}
				
				daBody.appendChild(outerSpan);
			}
		}
	}
}

var wndHelper = null;
function erectWindow(wndRef, strUrl, iWid, iHei)
{
	var strTop 	= "";
	var strLeft = "";
	var strFeatures;
	var wndResult;
	
	if (wndRef != null) {
		wndRef.close();
	}

	if (window.screen) {
		strTop 	= ",top=" + String(parent.screen.height/2 - iHei/2);
		strLeft = ",left=" + String(parent.screen.width/2 - iWid/2);
	}
	strFeatures = "toolbar=no,location=no,directories=no,status=yes,menubar=no," +
			"resizable=yes,width=" + iWid + ",height=" + iHei + ",scrollbars=yes" + strTop + strLeft;

	wndResult = window.open(strUrl, "runobj_win", strFeatures);
	if ((document.window != null) && (!wndResult.opener))
		wndResult.opener = document.window;
	wndResult.focus();
	return wndResult;
}

function msieversion()
{
	var ua = window.navigator.userAgent;
	var msie = ua.indexOf('MSIE ');

	if (msie > 0) return parseInt (ua.substring (msie+5, ua.indexOf ('.', msie )));
	else return 0;
}

function ifShowSelects(show)
{
    var ver = msieversion();
    if (ver > 0 && ver < 7)
    {
        selectShow(show);
    }
}

function checkPartnersForm(frm)
{
	if (frm.ct.selectedIndex == 0)
	{
		alert("Пожалуйста, укажите тип дистрибьютора.");
		return false;
	}
	return true;
}

function popupBanner(name) {
var cookie = " " + document.cookie;
var search = " " + name + "=";
var setStr = null;
var offset = 0;
var end = 0;
if (cookie.length > 0) {
offset = cookie.indexOf(search);
if (offset != -1) {
offset += search.length;
end = cookie.indexOf(";", offset)
if (end == -1) {
end = cookie.length;
}
setStr = unescape(cookie.substring(offset, end));
}
}
//return(setStr);

		if (setStr == 'homes') {
		var newWindow=window.open('', 'maiden01', 'width=600, height=371, top=0, left=0');
		newWindow.document.open();
		newWindow.document.write('<html><head><link rel="stylesheet" type="text/css" href="http://moeller.kiev.ua/moeller/lib/schemes/original/css/styles.css" /></head><body onkeypress="if (event.keyCode == 27) window.close()"><img src="http://moeller.kiev.ua/moeller/lib/schemes/original/gfx/outline.jpg" /><button onclick="window.close()" style="top:347px;left:505px;position:absolute;cursor:hand">Закрыть</button></body></html>');
		newWindow.document.close();
		newWindow.focus();
	}
document.cookie="page=nohomes;";
};