function $$( element_id ){
	return document.getElementById( element_id );
}



/**
* Counter of HTML text field value.
* @author Popko Vitaliy
* @since 11.11.2008
* @http://zapoy.org
*/
function counterText( oTextarea, iMaxLength ) {
	if ( !iMaxLength ) iMaxLength = 2500;
	var aSplit 	= oTextarea.id.split( '_' );
	var sLang	= aSplit[0];
	var iLength = oTextarea.value.length;
	$$( sLang + '_counter').value = iLength;
	if ( iLength >= iMaxLength ) {
		oTextarea.value = oTextarea.value.substr(0, iMaxLength );
		$$( sLang + '_counter').value = iMaxLength;
	}
	
}


function ucfirst( str ) {    // Make a string&#039;s first character uppercase
    // 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 
    var f = str.charAt(0).toUpperCase();
    return f + str.substr(1, str.length-1);
}


function str_replace ( search, replace, subject ) {    // Replace all occurrences of the search string with the replacement string
    // 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
 
    if(!(replace instanceof Array)){
        replace=new Array(replace);
        if(search instanceof Array){//If search    is an array and replace    is a string, then this replacement string is used for every value of search
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }
 
    if(!(search instanceof Array))search=new Array(search);
    while(search.length>replace.length){//If replace    has fewer values than search , then an empty string is used for the rest of replacement values
        replace[replace.length]='';
    }
 
    if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
        for(k in subject){
            subject[k]=str_replace(search,replace,subject[k]);
        }
        return subject;
    }
 
    for(var k=0; k<search.length; k++){
        var i = subject.indexOf(search[k]);
        while(i>-1){
            subject = subject.replace(search[k], replace[k]);
            i = subject.indexOf(search[k],i);
        }
    }
 
    return subject;
 
}


Support = {
	
	eventXY: function(e) {
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}

		return [posx, posy]
	},
	
	cevent: function(e) {
		e = e || window.event;	
		if(e.preventDefault) e.preventDefault();
		if(e.stopPropagation) e.stopPropagation(); 
		e.cancelBubble = true;
		e.returnValue = false;
		return false;
	},
	
	gevent: function(obj, e, handler) {
		if (!obj._attachedEvents) obj._attachedEvents = new Array();
		//if (obj._attachedEvents[e])	this.removeEvent(obj, e);
		obj._attachedEvents[e] = handler;
		(obj.addEventListener) ? obj.addEventListener(e, handler, false) : obj.attachEvent('on' + e, handler);
	}
}

Function.prototype.bind = function(object) {
    var method = this;
    return function() {
        return method.apply(object, arguments);
    }
}


function clearClass(field)
{
	field.className = '';
}

function showHelp(field)
{
	if (typeof(__aFieldsHelp[field.name]) =='undefined') return false;
	field.className = '';
	var tdElement = document.getElementById(field.name);
	var div = document.getElementById('showHelp');
	
    //var iLeftPos  = getOffsetLeft(tdElement);
	var iTopPos   = getOffsetTop(tdElement);// + (tdElement.offsetHeight + 3);
	div.innerHTML = '<table width="100%"><tr><td class="title">'+__aFieldsHelp[field.name]['title']+'</td><td class="right"><img src="/img/close.gif" onclick="hideblock(\'showHelp\');"></td></tr><tr><td colspan="2">'+__aFieldsHelp[field.name]['descr']+'</td></tr></table>';
	div.style.marginLeft = '-'+ ( 310+parseInt(tdElement.offsetWidth) )+ 'px';
	div.style.display =  "inline";
	div.style.top  = (iTopPos)+"px";
	tdElement.appendChild(div);
	
	//div.style.left = (iLeftPos) - 310 + "px";
	//div.style.top  = (iTopPos)  - 30 + "px";

    
}


function getOffsetTop(obj)
{
	var top = obj.offsetTop;

	while( (obj = obj.offsetParent) != null )
	{
		top += obj.offsetTop;
	}

	return top;
}

function getOffsetLeft(obj)
{
	var top = obj.offsetLeft;

	while( (obj = obj.offsetParent) != null )
	{
		top += obj.offsetLeft;
	}

	return top;
}

function hideblock(el) {
	document.getElementById(el).style.display = 'none';
}


function runInputRegistration()
{
	var aFields = $$( 'tableRegister' ).getElementsByTagName('input');
		
	for ( var i = 0; i< aFields.length; i++ ) 
	{
		
		/*if(typeof (__ErrorFields[aFields[i].name]) != 'undefined' && (aFields[i].type == 'radio' || aFields[i].type == 'checkbox'))
		{
			if(!$$(aFields[i].name)) continue;
			var tdID = $$(tdIDName);
			tdID.className = 'pink';
		}*/
		if( typeof (__ErrorFields[aFields[i].name]) != 'undefined')
		{
			aFields[i].className += ' pink';
		}
		
		if ( aFields[i].type != 'text' &&  aFields[i].type != 'textarea' &&  aFields[i].type != 'hidden') continue;
		
		
		
		if( typeof(__aFieldsHelp[aFields[i].name]) !='undefined' ) 
		{
			
			aFields[i].onfocus = function(){showHelp(this);};
			
			var parentTD = aFields[i].parentNode;
			j = 0;
			while(parentTD.tagName!='TD')
			{
				j++;
				parentTD = parentTD.parentNode;
				if ( j >= 10 ) break;
			}
			j = 0;
			var titleTD = parentTD.previousSibling;
			while(titleTD.tagName!='TD') {
				j++;
				titleTD = titleTD.previousSibling;
				if ( j >= 10 ) break;
			}
			var myTD = titleTD.previousSibling;
			j = 0;
			while(myTD.tagName!='TD') {
				j++;
				myTD = myTD.previousSibling;
				if ( j >= 10 ) break;
			}
			myTD.id = aFields[i].name;
			myTD.innerHTML = '<img src="/img/detail.gif" title="Help" name="'+aFields[i].name+'" align="absmiddle" onclick="showHelp(this)">';
		}
	}
	
}