// Russkin Alexey, 2008, alexey.russkin@gmail.com
// v.1.2.1

var err_message = '';
var err_was = false;

function checkthis(object, err_msg){
	if(!object.id) object = $(object);

	if(!object.value){
		if (err_was) err_message += ', ';
		else err_message = 'Please, fill this field(s): ';
		err_message += err_msg;
// 		object.style.border = 'red solid 1px';
		err_was = true;
	}
// 	else object.style.border = '#708FAC solid 1px';
}

function check_notnull(object, err_msg){
	if(!object.id) object = $(object);

	if (!object.value || object.value == 0) {
		if (err_was) err_message += ', ';
		else err_message = 'Please, fill this field(s): ';
		err_message += err_msg;
// 		object.style.border = 'red solid 1px';
		err_was = true;
	}
// 	else object.style.border = '#708FAC solid 1px';
}

function check_email(object){
	if(!object.id) object = $(object);

	email_check = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;
	if(!email_check.test(object.value)){
		err_message += "\n" + 'Invalid E-Mail!';
// 		object.style.border = 'red solid 1px';
		err_was = true;
	}
// 	else object.style.border = '#708FAC solid 1px';
}

var ie4 = (document.all);
var nn6 = (document.getElementById && navigator.appName == "Netscape");
var nn4 = (document.layers);

/**
 * Check if key pressed is digit key 0-9
 * must be invoked in keypressed event handler for <input type="text"/> or
 * <textarea> elements
 */
 // Ex.: onkeypress='return isDigit(event);'
function isDigit(e){
	var c;
	var v;

	if(ie4) v = event.keyCode;
	else {
		v = e.which;
		c = String.fromCharCode(v);
	}

	if(v==0 || v==8) {
		// enable backspace & cursor keys
		return true;
	}
	else {
		c = String.fromCharCode(v);
		if(c>='0' && c<='9') return true;
	}
	return false;
}

function isDigitSigned(e){
	var c;
	var v;

	if(ie4) v = event.keyCode;
	else {
		v = e.which;
		c = String.fromCharCode(v);
	}

	if(v==0 || v==8) {
		// enable backspace & cursor keys
		return true;
	}
	else {
		c = String.fromCharCode(v);
		if(c>='0' && c<='9') return true;
		if(c == '-') return true;
	}
	return false;
}

/**
 * Similar to isDigit but validates if entered value is a float number (may contain no more than one decimal point)
 *
 */
// return isNumFloat(event, this)
function isNumFloat(e, el){
	var c;
	var v;

	if(ie4) v = event.keyCode;
	else{
		v = e.which;
		c = String.fromCharCode(v);
	}

	if(v == 0 || v == 8){
		// enable backspace & cursor keys
		return true;
	}
	else {
		c = String.fromCharCode(v);
		if(c >= '0' && c <= '9') return true;
		else if(c == '.') {
			s = new String(el.value);
			if(s.indexOf('.') == -1) return true;
		}
	}
	return false;
}

function showHint(object_id){
	// сохранение координат курсора мыши в переменные
	var _x = window.event.clientX;
	var _y = window.event.clientY;

	//смещение подскаки вправо и влево относительно координат мыши
	var _dx = 10;
	//смещение подскаки вверх и вниз относительно координат мыши
	var _dy = 10;

	var object = document.getElementById(object_id);

	if (object){
		if(_x < document.body.clientWidth - object.clientWidth - _dx) _x = _x + _dx;
		else _x = _x - _dx - object.clientWidth;

		if(_y < document.body.clientHeight - object.clientHeight - _dy) _y = _y + _dy;
		else _y = _y - _dy - object.clientHeight;

		object.style.left = _x;
		object.style.top = _y + document.body.scrollTop;//смещение подскази в зависимости от высоты прокрученной части документа
		object.style.visibility = 'visible';
	}
}

function hideHint(object_id){
	var object = document.getElementById(object_id);
	if(object) object.style.visibility = 'hidden';
}


if (window.$ == null){
	function $(obj){
		return document.getElementById(obj);
	}
}

function show(obj, show){
	if(show) $(obj).style.display = '';
	else $(obj).style.display = 'none';
}

function sw_show(obj){
	if($(obj).style.display != '') $(obj).style.display = '';
	else $(obj).style.display = 'none';
}

function insertOption(objSelect, N, text, value, defaultSelected, selected){
	var i = 0;

	// Создаём новую опцию
	objSelect.options[objSelect.length] = new Option('', '', false, false);

	// Сдвигаем опции вниз
	for(i = objSelect.length - 2; i >= N; i--){
		objSelect.options[i + 1].value = objSelect.options[i].value;
		objSelect.options[i + 1].innerHTML = objSelect.options[i].innerHTML;
	}

	// Вставляем опцию
	objSelect.options[N].innerHTML = text;
	objSelect.options[N].value = value;
	objSelect.options[N].defaultSelected = defaultSelected;
	objSelect.options[N].selected = selected;
}

// Открыть большую картинку
function open_big(str) {
	window.open('view.php?' + str, 'popup', 'width=400,height=300,top=20,left=240,scrollbars=yes');
}

// Все свойства объкта
function showProps(obj, objName){
	var result = "";
	for (var i in obj) {
		result += objName + "." + i + " = " + obj[i] + "<br />\n";
	}
	document.write(result);
}

// проверят коректность даты
function checkDateDay(day, month, year) {
	var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	if (!day || !month || !year) return false;

	// check for bisestile year
	if (year/4 == parseInt(year/4)) monthLength[1] = 29;

	if (month < 1 || month > 12) return false;

	if (day > monthLength[month-1]) return false;

	return true;
}

function in_array(str, arr){
	for(var i = 0; i < arr.length; i++){
		if(arr[i] == str) return true;
	}
	return false;
}