//////////////////////////////////
// Error class prototype
//////////////////////////////////

/**
 * TErrors class provides a flexible error handling model.
 *
 * Warning! There must be only one object of this class (singleton).
 *
 * Install:
 * <code>
 *  <style type="text/css">
 *   .show_error { style of input where is error (style visible by 2 sec. after click on label }
 *   .hide_error, .error_field { style of input where is error (the label wasn't click }
 *   .label_rq_error { style of caption field with error }
 *  </style>
 * </code>
 *
 * Usage:
 * <code>
 *  <li>
 * 	 Uzupełnij pole <a href="javascript:error.show('code');">Kod aktywacyjny</a>   <!-- error.show('##FIELD_ID##'); -->
 * 	 <script type="text/javascript">error.add('code');</script>                    <!-- error.add('##FIELD_ID##'); -->
 *  </li>
 *
 *  (...)
 *
 *  <td class="label_rq"><span id="label_code">Kod aktywacyjny</span></td>         <!-- id="label_##FIELD_ID##" -->
 *  <td><input type="text" name="code" id="code" value="{$form.code}" /></td>      <!-- id="##FIELD_ID##" -->
 *
 *  (...)
 *
 *  <script type="text/javascript">error.setFields();</script>
 * </code>
 *
 * @copyright Copyright: MainSeek Sp. z o.o.
 * @author Rafal Zelazko <rzelazko (at) gmail (dot) com>
 * @version 1.0.1
 */
function TErrors()
{
	this.eIdx = 0;
	this.errorFields = new Array();
	this.add = TErrorsAdd;
	this.setFields = TErrorsSetFileds;
	this.show = TErrorsShow;
	this.hide = TErrorsHide;
	this.reset = TErrorsReset;
}

/**
 * Add error to error list.
 * Usage: <code>error.add(id);</code>
 *
 * @parm String id Id of filed where is error
 */
function TErrorsAdd(id)
{
	this.errorFields[this.eIdx] = new Array();
	this.errorFields[this.eIdx]['id'] = id;
	this.eIdx++;
}

/**
 * Change style of fields with errors and captions.
 * Called after all errors are added.
 * Usage: <code>error.setFields();</code>
 */
function TErrorsSetFileds()
{
	for(var i = 0; i < this.errorFields.length; i++)
	{
		var eInput	= document.getElementById(this.errorFields[i]['id']);
		var eLabel	= document.getElementById('label_' + this.errorFields[i]['id']);
		if(eInput != null)
		{
			this.errorFields[i]['inpt_cls'] = eInput.className;
			//eInput.className = 'error_field';
		}
		if(eLabel != null)
		{
			this.errorFields[i]['lbl_cls'] = eLabel.className;
			eLabel.className = 'label_rq_error';
		}
	}
}

/**
 * Highlight field with error.
 *
 * @param String inputName Highighted filed.
 */
function TErrorsShow(inputName)
{
	var eInput = document.getElementById(inputName);
	if(eInput)
	{
		eInput.scrollIntoView(true);
		setTimeout('this.TErrorsHide("' + inputName + '")', 2000);
		eInput.className = 'show_error';
		eInput.focus();
		if (eInput.select != null)
		{
			eInput.select();
		}
	}
	else
	{
		var eInput = dojo.widget.byId(inputName);
		eInput.textInputNode.scrollIntoView(true);
		eInput.textInputNode.focus();
		setTimeout('this.TErrorsHide("' + inputName + '")', 2000);
		eInput.textInputNode.className = 'show_error';
		eInput.textInputNode.style.width = eval(eInput.textInputNode.parentNode.offsetWidth-6) + 'px';
	}
}

/**
 * Unhighlight field with error.
 *
 * @param String inputName Highighted filed.
 */
function TErrorsHide(inputName)
{
	var eInput = document.getElementById(inputName);
	if(eInput)
	{
		if(eInput != null)
		{
			eInput.className = 'hide_error';
		}
	}
	else
	{
		var eInput = dojo.widget.byId(inputName);
		eInput.textInputNode.className = 'hide_error';
	}
}

/**
 * Reset all errors in form.
 */
function TErrorsReset()
{
	for(var i = 0; i < this.errorFields.length; i++)
	{
		var eInput	= document.getElementById(this.errorFields[i]['id']);
		var eLabel	= document.getElementById('label_' + this.errorFields[i]['id']);

		if(eInput && this.errorFields[i]['inpt_cls'])
		{
			eInput.className = this.errorFields[i]['inpt_cls'];
		}
		if(eLabel && this.errorFields[i]['lbl_cls'])
		{
			eLabel.className = this.errorFields[i]['lbl_cls'];
		}
	}

	var errLabels = document.getElementsByClassName('label_rq_error');
	for(var i = 0; i < errLabels.length; i++)
	{
		errLabels[i].className = '';
	}

	this.eIdx = 0;
	this.errorFields = new Array();
}

/**
 * New TErrors object.
 */
var error = new TErrors();
