function stripZeros(inputStr)
{
	var result = inputStr;
	while ((result.substring(0,1)=="0") && (result.length>1))
		result = result.substring(1,result.length);
	return result;
}

function isEmpty(inputStr)
{
	if (inputStr=="" || inputStr==null) return true; else return false;
}

function isFloat(inputStr)
{
	return (!isNaN(parseFloat(inputStr)));
}

function isNumber(inputStr)
{
	for (var i=0; i<inputStr.length; i++)
	{
		var oneChar = inputStr.substring(i, i+1);
		if ((oneChar<"0" || oneChar>"9")) return false;
	}
	return true;
}

function inRange(inputStr, lo, hi)
{
	var num = parseInt(inputStr);
	if (num<lo || num>hi) return false;
	return true;
}

function focus(field)
{
	field.focus(); field.select();
}

function focusNoSelect(field)
{
	field.focus();
}

function checkIsFloat(object, label)
{
	if(!isFloat(object.value))
	{
		if(label != '')
			alert('Значение поля "'+label+'" должно быть числом! Например 0.15, 105 и т.п.');
		else
			alert('Значение поля должно быть числом! Например 0.15, 105 и т.п.');
		focus(object);		
		return false;
	}
	return true;
}

function isEmail(email)
{
  	var not_valid = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
  	var valid = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

  	return (!not_valid.test(email) && valid.test(email));
}

function checkIsEmail(object, label)
{
	if(!isEmail(object.value))
	{
		alert('Значение поля "'+label+'" должно быть адресом электронной почты! Например bill@microsoft.com');
		focus(object);		
		return false;
	}
	return true;
}

function checkMinLimit(object, label, minLimit)
{
	if(object.value.length < minLimit)
	{
		alert('Значение поля "'+label+'" не может быть меньше '+minLimit+' символов!');
		focus(object);		
		return false;
	}
	return true;
}

function checkMaxLimit(object, label, maxLimit)
{
	if(object.value.length > maxLimit)
	{
		alert('Значение поля "'+label+'" не может быть больше '+maxLimit+' символов!');
		focus(object);		
		return false;
	}
	return true;
}

function checkNoEmpty(object, label)
{
	if(isEmpty(object.value))
	{
		alert('Значение поля "'+label+'" не может быть пустым!');
		focus(object);		
		return false;
	}
	return true;
}

function checkDate(daySelect, monthSelect, yearSelect, dateHidden)
{
	input = stripZeros(monthSelect.options[monthSelect.selectedIndex].value);
 	if (isEmpty(input) || !isNumber(input))
	{
	   alert("Укажите месяц");
	   focusNoSelect(monthSelect);
	   return false;
	}
	month = input;

	input = stripZeros(daySelect.options[daySelect.selectedIndex].value);
	var monthMax = new Array(31,31,29,31,30,31,30,31,31,30,31,30,31)
	if (isEmpty(input) || !isNumber(input) || !inRange(input,1,monthMax[month]))
	{
		alert("Введите число от 1 до "+monthMax[month]);
		focusNoSelect(daySelect);
		return false;
	}
	day=input;

	input = stripZeros(yearSelect.value);
	if (isEmpty(input) || !isNumber(input))
	{
		alert("Укажите год");
		focusNoSelect(yearSelect);
		return false;
	}
	year=input;

	// format MySQL date '2001-10-05 9:00:00'
	dateHidden.value = year+"-"+month+"-"+day+" 00:00:00";
	return true;
}

function checkDateTime(daySelect, monthSelect, yearSelect, hourInput, minuteInput, dateHidden)
{
	input = stripZeros(monthSelect.options[monthSelect.selectedIndex].value);
 	if (isEmpty(input) || !isNumber(input))
	{
	   alert("Укажите месяц");
	   focusNoSelect(monthSelect);
	   return false;
	}
	month = input;

	input = stripZeros(daySelect.options[daySelect.selectedIndex].value);
	var monthMax = new Array(31,31,29,31,30,31,30,31,31,30,31,30,31)
	if (isEmpty(input) || !isNumber(input) || !inRange(input,1,monthMax[month]))
	{
		alert("Введите число от 1 до "+monthMax[month]);
		focusNoSelect(daySelect);
		return false;
	}
	day=input;

	input = stripZeros(yearSelect.value);
	if (isEmpty(input) || !isNumber(input))
	{
		alert("Укажите год");
		focusNoSelect(yearSelect);
		return false;
	}
	year=input;

	input = stripZeros(hourInput.value);
	if (isEmpty(input) || !isNumber(input) || !inRange(input,0,23))
	{
		alert("Укажите количество часов от 0 до 23");
		focusNoSelect(hourInput);
		return false;
	}
	hour=input;

	input = stripZeros(minuteInput.value);
	if (isEmpty(input) || !isNumber(input) || !inRange(input,0,59))
	{
		alert("Укажите количество минут от 0 до 59");
		focusNoSelect(minuteInput);
		return false;
	}
	minute=input;

	// format MySQL date '2001-10-05 9:00:00'
	dateHidden.value = year+"-"+month+"-"+day+" "+hour+":"+minute+":00";
	return true;
}
