
// ..................................................................

 function validate1(){
    if (document.form1.code.value !='Zi8wL') {
      alert("Введите код, изображенный на рисунке."); 
	        document.form1.code.select();
			 document.form1.code.focus();
return false;}
else 
{return true;
}
}

function hideNotice()
{
	document.getElementById("ibs_notice").style.display = "none";
}

// ..................................................................

function showNotice()
{
	n_div = document.getElementById("ibs_notice");

	this_top = (document.body.clientHeight - 200) / 2;
	this_left = (document.body.clientWidth - 300) / 2;
	n_div.style.left = this_left + 'px';
	n_div.style.top  = this_top + 'px';
	n_div.style.display = "block";
	setTimeout("hideNotice()", 2000);
}

// ..................................................................

function showNoticeIndirect(message, is_error)
{
	n_div = document.getElementById("ibs_notice");
	n_div_text = document.getElementById("ibs_notice_text");

	this_top = (document.body.clientHeight - 200) / 2;
	this_left = (document.body.clientWidth - 300) / 2;
	n_div.style.left = this_left + 'px';
	n_div.style.top  = this_top + 'px';

	n_div_text.innerHTML = message;

	n_div.style.display = "block";
	setTimeout("hideNotice()", 2000);
}

// ..................................................................

function setCookie(name, value)
{
	eval("document.cookie = \"" + name + "=" + value + "; path=/; expires=Wed, 1 Jan 2050 00:00:00 GMT;\"");
}

// ..................................................................

function findObj(n, d)
{ 
	var p, i, x;

	if(!d) d = document;
	if((p = n.indexOf("?")) > 0 && parent.frames.length)
	{
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}

	for(i = 0; !x && i < d.forms.length; i++)
	{
		x = d.forms[i][n];
	}
	if(!x)
	{
		if(!(x = d[n]) && d.all)
		{
			x = d.all[n];
		}
		for(i = 0; !x && d.layers && i < d.layers.length; i++)
		{
			x = findObj(n, d.layers[i].document);
		}
		if(!x && document.getElementById)
		{
			x = document.getElementById(n);
		}
	}

	return x;
}

function EnableObject(obj_name, bEnable)
{
	obj = findObj(obj_name);
	if (obj)
	{
		obj.disabled = (bEnable) ? false : true;
	}
}

function DisplayObject(obj_name, bDisplay)
{
	obj = findObj(obj_name);
	if (obj)
	{
		obj.style.display = (bDisplay) ? "block" : "none";
	}
}

// ..................................................................

function openWindow(href, title, scroll, resizable, width, height, position)
{
	if(scroll != "yes")
	{
		scroll = "no";
	}

	if(resizable != "yes")
	{
		resizable = "no";
	}

	if(position != 'center')
	{
		xx = window.open(href, title, 'toolbar=no, scrollbars=' + scroll + ', menubar=no, directories=no, status=no, resizable=' + resizable + ', width=' + width + ', height=' + height);
	}
	else
	{
		this_top = (screen.height - height) / 2;
		this_left = (screen.width - width) / 2;
		xx = window.open(href, title, 'toolbar=no, scrollbars=' + scroll + ', menubar=no, directories=no, status=no, resizable=' + resizable + ', width=' + width + ', height=' + height + ', top=' + this_top + ', left=' + this_left);
	}

	xx.focus();

	return false;
}

// ..................................................................

function imagePopup(imageUrl, imageW, imageH, title)
{
	x3 = imageW;
	y3 = imageH;
	x4 = (screen.width < 2000) ? (screen.width / 2 - (x3 / 2)) : (screen.width / 4 - (x3 / 2));
	y4 = screen.height / 2 - (y3 / 2);
	mywin = window.open('', 'big', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,width=' + x3 + ',height=' + y3 + ',top=' + y4 + ',left=' + x4 + '');
	mywin.document.open();
	mywin.document.write("<html><head><title>"+title+"</title><style>body { overflow:-moz-scrollbars-none; }</style></head><body bgcolor=#ffffff leftmargin=0 topmargin=0 marginheight=0 marginwidth=0><a href='javascript:window.close()'><img src='" + imageUrl + "' border=0></a></body></html>");
	mywin.document.close();
	mywin.focus();
	return false;
}

// ..................................................................

function changeLocation(theURL)
{
	document.location.href = theURL;

	return false;
}

// ..................................................................

function confirm_submit(obj, str)
{
	form = findObj(obj);

	if(confirm(str))
	{
		form.submit();
	}
}

// ..................................................................

function formSetRequirements(obj, req, check, friendly_name)
{
	obj = findObj(obj);

	if(obj)
	{
		obj.validateReq		= req;
		obj.validateCheck	= check;

		if (friendly_name)
		{
			obj.friendly_name = friendly_name;
		}
	}
}

// ..................................................................

function formSetUnique(obj, unique)
{
	obj = findObj(obj);
	
	if(obj)
	{
		obj.validateCheckUnique	= true;
		obj.validateUnique	= unique;
	}
}

// ..................................................................

function formSetNotequal(obj1, obj2)
{
	obj1 = findObj(obj1);
	obj2 = findObj(obj2);
	
	if(obj1 && obj2)
	{
		obj1.validateNotequal = obj2;
	}
}

// ..................................................................

function formSetEqual(obj1, obj2)
{
	obj1 = findObj(obj1);
	obj2 = findObj(obj2);
	
	if(obj1 && obj2)
	{
		obj1.validateEqual = obj2;
	}
}

// ..................................................................

function formUpdate(obj, skip_obj)
{
	err = false;

	if(obj.method == 'post' || obj.method == 'get')
	{
		for(var i = 0; i < obj.elements.length; i++)
		{
			formUpdate(obj.elements[i]);
		}
		return;
	}
	if(obj.type == 'select-one')
	{
		val = obj.options[obj.selectedIndex].value;
	}
	else if(obj.type == 'radio')
	{
		for (i = 0; i < obj.length; i++)
		{
			if (obj.options[i].checked)
			{
				val = obj.options[i].value;
			}
		}
//		val = obj.value;
	}
	else
	{
		val = obj.value;
	}

	if(val == '' && obj.validateReq == true)
	{
		err = true;
	}

	if(err == false && val != '')
	{
		if(obj.validateCheck == 'url' && val.indexOf('http://') != 0 && val.indexOf('https://') != 0)
		{
			err = true;
			obj.ibs_error_message = "Неверный формат URL-адреса!";
		}
		else if(val == 'http://' || val == 'https://')
		{
			err = true;
			obj.ibs_error_message = "Неверный формат URL-адреса!";
		}

		if(obj.validateCheck == 'email')
		{
			var not_valid = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
			var valid = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

			if(val == "") err = true;
			else if(not_valid.test(val) || !valid.test(val)) err = true;

			if (err)
			{
				obj.ibs_error_message = "Неверный формат E-Mail адреса!";
			}
		}

		if(obj.validateCheck == 'date')
		{
			var valid = /(\d{2}.\d{2}.\d{4})/;

			if(val == "") err = true;
			else if(val.length != 10 || !valid.test(val)) err = true;

			if (err)
			{
				obj.ibs_error_message = "Неверный формат даты! Необходимо использовать следующий формат: дд.мм.гггг";
			}
		}

		if(obj.validateCheck == 'datetime')
		{
			var valid = /(\d{2}.\d{2}.\d{4} \d{2}:\d{2})/;

			if(val == "") err = true;
			else if(val.length != 16 || !valid.test(val)) err = true;

			if (err)
			{
				obj.ibs_error_message = "Неверный формат даты! Необходимо использовать следующий формат: дд.мм.гггг чч:мм";
			}
		}

		if(obj.validateCheck == 'time')
		{
			var valid = /(\d{2}:\d{2}:\d{2})/;

			if(val == "") err = true;
			else if(!valid.test(val)) err = true;

			if (err)
			{
				obj.ibs_error_message = "Неверный формат даты! Необходимо использовать следующий формат: дд.мм.гггг чч:мм";
			}
		}

		if(obj.validateCheck == 'oneword')
		{
			var not_valid = /( )/;
			var valid = /^([a-zA-Z0-9\-\.]+)$/;

			if(val == "") err = true;
			else if(not_valid.test(val) || !valid.test(val)) err = true;

			if (err)
			{
				obj.ibs_error_message = "Значение поля должно состоять только из символов латиницы, цифр, знаков подчеркивания и дефиса";
			}
		}

		if(obj.validateCheck == 'urlword')
		{
			var not_valid = /( )/;
			var valid = /^([a-zA-Z0-9\-\._]+)$/;

			if(not_valid.test(val) || !valid.test(val)) err = true;

			if (err)
			{
				obj.ibs_error_message = "Значение поля должно состоять только из символов латиницы, цифр, знаков подчеркивания и дефиса!";
			}
		}

		if(obj.validateCheck == 'natural' && (isNaN(val) || val <= 0))
		{
			err = true;
		}

		if(obj.validateCheck == 'number*' && (isNaN(val) && val != '*' || val < 0))
		{
			err = true;
		}

		if(obj.validateCheck == 'number+' && (isNaN(val) && val != '-'  || val < 0))
		{
			err = true;
		}

		if(obj.validateCheckUnique == true)
		{
			needle = obj.value.toLowerCase();
			haystack = obj.validateUnique.toLowerCase();

			if(haystack.indexOf('|' + needle + '|') > -1)
			{
				err = true;
			}
		}

		if(obj.validateNotequal)
		{
			obj2 = obj.validateNotequal;
			if(obj2.type == 'select-one')
			{
				val2 = obj2.options[obj2.selectedIndex].value;
			}
			else
			{
				val2 = obj2.value;
			}

			if(val == val2)
			{
				err = true;
			}
		}
		else if (obj.validateEqual)
		{
			obj2 = obj.validateEqual;
			if(obj2.type == 'select-one')
			{
				val2 = obj2.options[obj2.selectedIndex].value;
			}
			else
			{
				val2 = obj2.value;
			}

			if(val != val2)
			{
				err = true;
				obj.ibs_error_message = "Значения полей должны совпадать!";
			}
		}
	}

	if (!skip_obj)
	{
		var frm = obj.form;
		if (frm && frm.submit_button)
		{
			var btn = findObj(frm.submit_button);
			if (btn && frm.error_message_area)
			{
				btn.disabled = !formCheck(frm, true);
			}
			else if (btn)
			{
				btn.disabled = false;
			}
		}
	}

	return(err);
}

// ..................................................................

function formCheck(f, no_alerts)
{
	var noerrors = true;
	var first	 = false;
	var fields   = new Array();

	for(var i = 0; i < f.elements.length; i++)
	{
		if(f.elements[i].validateCheck || f.elements[i].validateReq || f.elements[i].validateEqual)
		{
			err = formUpdate(obj = f.elements[i], f.elements[i]);

			if(err)
			{
				if (first == false)
				{
					first = i;
				}

				noerrors = false;
			}
		}
	}

	if(noerrors == false)
	{
		if (f.error_message_area)
		{
			f.error_message_area.innerHTML = (f.elements[first].ibs_error_message) ? f.elements[first].ibs_error_message : ( (f.elements[first].friendly_name) ? ("Некорректно заполнено поле &laquo;" + f.elements[first].friendly_name + "&raquo;!") : "Некорректно заполнено поле!");
		}
		else if (!no_alerts)
		{
			alert((f.elements[first].ibs_error_message) ? f.elements[first].ibs_error_message : ( (f.elements[first].friendly_name) ? ("Некорректно заполнено поле \"" + f.elements[first].friendly_name + "\"!") : "Некорректно заполнено поле!"));
		}

		if(f.elements[first].type != 'select-one' && !no_alerts)
		{
			f.elements[first].select();
		}

		if(!no_alerts)
		{
			f.elements[first].focus();
		}
	}
	else if (f.error_message_area)
	{
		f.error_message_area.innerHTML = "";
	}

	return noerrors;
}

// ..................................................................

function CopyClipboard(obj)
{
	obj = findObj(obj);

	if(obj)
	{
		window.clipboardData.setData('Text', obj.value);
	}
}

// ..................................................................

function setColor(obj, clr)
{
	obj.style.backgroundColor = clr;
}

// ..................................................................

function preloadImage(src)
{
   var img=new Image();
   img.src = src;
}

// ..................................................................

function setPreviewFile(filepath, field_name, width)
{
	_filepath = new String(filepath);
	fileparts = _filepath.split("\\");
	if (fileparts.length > 0)
	{
		filename = new String(fileparts[fileparts.length-1]);
		_fileext = filename.split('.');
		if (_fileext.length > 0)
		{
			ext = _fileext[_fileext.length-1];
			obj = findObj(field_name);
			if (obj && ext == "gif" || ext == "jpg" || ext == "jpeg" || ext == "png")
			{
				obj.src=filepath;
				obj.style.width = (width > 0) ? width : 100;
			}
			else if (obj)
			{
				obj.src='';
				obj.style.width = 0;
			}
		}
	}
}

// ..................................................................

function openFromPopup(url)
{
	if (window.opener) 
	{ 
		window.opener.location.href = url; 
		window.opener.window.focus();
	} 
	else 
	{ 
		window.open(url); 
	}
	return false; 
}

// ..................................................................

function loadPNG(png, img, width, height, extra_param)
{
    var browser = new Browser();
    if (browser.png)
        document.write("<img border=0 src='" + png + "' width='" + width + "' height='" + height + "' " + extra_param + " />");
    else
        document.write("<img border=0 src='" + img + "' width='" + width + "' height='" + height + "' " + extra_param + " />");
}

// ..................................................................

function _debug(object)
{
	var allF = "";
	for (superVar in object)
	{
		allF += superVar + ";<br>\n ";
	}
	qqq = window.open();
	qqq.document.write(allF);
}

// ..................................................................

function set_asid(frm)
{
	frm["action_button"].disabled = true;
	frm["__asid"].value = __asid;
}

