/*
	Helpers.js
	
	Created: 12/17/2002
*/

var DEFINE_HELPERS = true;


//
//				String.format
//
//	Make formatting for current string object
//	Arguments:
//	  current string must have format string
//		#0# - first format argument
//		#1# - second format argument
//		#n# - ...
//	Example:
//		var sss = new String("Today is #0#. Hello, #1#!");
//		document.write(sss.format("6 sep", "Sergey"));
String.prototype.format = function()
{
	if( typeof(arguments[0]) == "undefined" ) return this;
	var result = this;
	var i=0;
	while( typeof(arguments[i]) != "undefined" )
	{
		result = result.replace( new RegExp("#"+i+"#","g"), arguments[i] );
		i++;
	}
	return result;
}

//
//			String.trimleft
//
//  Remove all left spaces from string
//  Return string without left spaces
String.prototype.trimleft = function()
{
	var reg = new RegExp( "^\\s{1,}", "" );
	var result = this;
	return result.replace( reg, '');
}

//
//			String.trimright
//
//  Remove all right spaces from string
//  Return string without right spaces
String.prototype.trimright = function()
{
	var reg = new RegExp( "\\s{1,}$", "" );
	var result = this;
	return result.replace( reg, '');
}

//
//			String.trim
//
//  Remove all left and all right spaces from string
//  Return string without left and right spaces
String.prototype.trim = function()
{
//	var result = this.trimleft();
//	result = result.trimleft().trimright();
	return this.trimleft().trimright();
}

//
//			Number.toFixed2
//
//  Round number value to specified decimals
Number.prototype.toFixed2 = function(prec)
{
	if(isNaN(this)) return this;
	var arg = 1;
	var z="";
	var p = parseInt(prec, 10);
	if(isNaN(p)) p=0;
	for( var i=0; i<p; i++ )
	{
		arg *= 10;
		z = z + "0";
	}
	var tmp = Math.round(this * arg)/arg;
	if( tmp.toString().indexOf(".") == -1 && p!=0 )
	{
		return tmp+"."+z;
	}
	return tmp;
}

//
//			SetFocus
//
//  Try set focus in control.
//  If control disabled or readOnly - return
//  If error occured in focus() method - return
//  Use this function for safe set focus in control. 
//  No errors if control is absent or is not visible.
//  Arguments:
//		control - control for set focus
/*public void*/function SetFocus( control )
{
	if (control==null || typeof(control)=="undefined") return;
	try
	{
		if (control.isDisabled || control.readOnly) return;
		control.focus();
	}
	catch(e)
	{
	}
}


// Function check the time
// If input param is null then return current time
// If input param in not null then will returned difference between old date and current
function CheckTime( oldTime )
{
	if (oldTime == null)
	{
		return new Date();
	}
	else
	{
		return (new Date()) - oldTime;
	}
}

// Change ID and Name for control
function ChangeIdAndName(control, name)
{
	control.id = name;
	control.removeAttribute("name", 0);
	control.setAttribute("name", name, 0);
}

// function for check maxlength in textarea
// make trim value if more then maxlength
// return true if all ok
// return false if value more then maxlength
/*private bool*/function _textareaCheckMaxLength(tarea)
{
	if (tarea==null)
	{
		return true;
	}
	var result = false;
	var maxL = parseInt(tarea.getAttribute("MAXLENGTH", 0), 10);
	if (isNaN(maxL))
	{
		result = true;
	}
	else
	{
		if (tarea.value.length<maxL-1)
		{
			result = true;
		}
		else if (tarea.value.length>maxL-1)
		{
			tarea.value = tarea.value.substr(0, maxL);
		}
	}
	return result;
}

// onkeypress event handler for textarea for check maximum length
/*private void*/function _textareaKeypress()
{
	event.returnValue = _textareaCheckMaxLength(event.srcElement);
}

// onpaste event handler for textarea for check maximum length
/*private void*/function _textareaPaste()
{
	var pasteData = window.clipboardData.getData("Text");
	var pasteRange = document.selection.createRange();
	pasteRange.text = pasteData;
	
	_textareaCheckMaxLength(event.srcElement);
	event.returnValue = false;
}

//
//			AddCheckMaxLen
//
// Add check max length to textarea control
// Arguments:
//		tarea - textarea control or string with textarea control ID
//		maxLen - maximum length for check
// Return:
//		true if succefull added, false if error
/*public bool*/function AddCheckMaxLen(tarea, maxLen)
{
	if (tarea==null)
	{
		return false;
	}
	var obj = tarea;
	if (typeof(tarea) == "string")
	{
		obj = document.all(tarea);
	}
	else if (typeof(tarea) != "object")
	{
		return false;
	}
	if (obj.tagName.toUpperCase() != "TEXTAREA")
	{
		return false;
	}
	obj.MAXLENGTH = maxLen;
	obj.onpaste = _textareaPaste;
	obj.onkeypress = _textareaKeypress;
	return true;
}
