/*
 *	20.06.2005 17:27
 *	InputPlaceholder Class v 0.1a
 *
 *	30.06.2005 20:36
 *	InputPlaceholder Class v 0.1b
 *
 *	01.07.2005 18:30
 *	InputPlaceholder Class v 0.1c
 */

/*	Методы:
 *		отсутствуют
 *
 *	Свойства
 *		Input			Элемент формы, с которым работаем
 *		Value			Значение надписи по умолчанию
 *		CssFilled		Имя css-класса для отображения заполненого поля
 *		CssEmpty		Имя css-класса для отображения пустого поля
 */

/*	Конструктор
 *	Параметры:
 *		input			Элемент формы ( input[@type='text'] ), с которым работаем
 *		value			Значение надписи по умолчанию, т. е. тот самый placeholder
 *		cssFilled		Имя css-класса для отображения заполненого поля (применяется к input)
 *		cssEmpty		Имя css-класса для отображения пустого поля (применяется к input)
 */
/*
	Я не машина, я просто читаю исходники.
*/
function InputPlaceholder (input, value, cssFilled, cssEmpty)
{
	var thisCopy = this

	this.Input = input
	this.Value = value
	this.SaveOriginal = (input.value == value)
	this.CssFilled = cssFilled
	this.CssEmpty = cssEmpty

	this.setupEvent (this.Input, 'focus', function() {return thisCopy.onFocus()})
	this.setupEvent (this.Input, 'blur',  function() {return thisCopy.onBlur()})
	this.setupEvent (this.Input, 'keydown', function() {return thisCopy.onKeyDown()})

	if (input.value == '') this.onBlur();

	return this
}

InputPlaceholder.prototype.setupEvent = function (elem, eventType, handler)
{
	if (elem.attachEvent)
	{
		elem.attachEvent ('on' + eventType, handler)
	}

	if (elem.addEventListener)
	{
		elem.addEventListener (eventType, handler, false)
	}
}

InputPlaceholder.prototype.onFocus = function()
{
	if (!this.SaveOriginal &&  this.Input.value == this.Value)
	{
		this.Input.value = ''
	}
	else
	{
			this.Input.className = ''
	}
}

InputPlaceholder.prototype.onKeyDown = function()
{
	this.Input.className = ''
}

InputPlaceholder.prototype.onBlur = function()
{
	if (this.Input.value == '' || this.Input.value == this.Value)
	{
		this.Input.value = this.Value
		this.Input.className = this.CssEmpty
	}
	else
	{
		this.Input.className = this.CssFilled
	}
}

// округление до второго знака
function round2s(num) {
    return '' + Math.floor(num) + '.' + Math.floor(num * 10) % 10 + Math.round(num * 100) % 10;
}

//
//
// получить из строки число, вернуть NaN, null или число
function ParsePrice(el) {
    if (el.value == null) return null;
    // заменяем запятые на точки
    // так конечно было бы круче, но не работает в Konquer
//	var price = el.value.replace( /[,юб]/gi, '.' );
    var price = '', str=el.value, len=el.value.length;
    for (var i = 0; i < len; i++) {
        var c = str.charAt(i);
        if (',юЮбБ'.indexOf(c) >= 0) {
            price += '.';
        } else {
            price += c;
        }
    }

    el.value=round2s(price);
    if (el.value == '0.00') el.value = '';
 /*   if (price == null || price == '') {
        return null;
    } else if (!re_price.test(price)) {
        return NaN;
    } else {
        return price;
    }*/
}

function clearForm(oForm) {

  var elements = oForm.elements;

  oForm.reset();

  for(i=0; i<elements.length; i++) {

	field_type = elements[i].type.toLowerCase();

	switch(field_type) {

		case "text":
		case "password":
		case "textarea":
	        case "hidden":

			elements[i].value = "";
			break;

		case "radio":
		case "checkbox":
  			if (elements[i].checked) {
   				elements[i].checked = false;
			}
			break;

		case "select-one":
		case "select-multi":
            		elements[i].selectedIndex = -1;
			break;

		default:
			break;
	}
    }
}
