String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

// Create a cookie with the specified name and value.
// The cookie expires at the end of the 20th century.
function SetCookie(sName, sValue) {
  date = new Date(2030,1,1);
  document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString()+"; path=/";
}

function GetCookie(sName) {
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++) {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) {
      return unescape(aCrumb[1]);
    }
  }
  // a cookie with the requested name does not exist
  return null;
}

// Delete the cookie with the specified name.
function DelCookie(sName) {
  document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function getElementsByClass(searchClass, pnode, tag) {

    function _GetElementsByClass(outArray, seed, needle) {
        while (seed) {
            if (seed.className==needle) outArray.push(seed);
            if (seed.nodeType == "1") {
                _GetElementsByClass(outArray, seed.firstChild, needle)
            }
            seed = seed.nextSibling;
        }
    }
    var outArray = new Array();
	if ( pnode == null ) pnode = document.documentElement; else pnode=pnode.firstChild;
    _GetElementsByClass(outArray, pnode, searchClass);
    return outArray;
}

String.prototype.replaceAll = function ( template, replacement ) {
	var str = this;
	occurance = str.indexOf( template );
    while ( occurance > -1 ) {
        str = str.replace( template, replacement );
        occurance = str.indexOf( template );
    }
    return str;
}

function getSimplePaginationString(currPage,totalPages,link,template){
	var result = "";
	if(totalPages>1){
		if(!template || tamplate == "")
		 template = "%PAGE%";
		if(!link || link == "")
		 link = '<a href="&page=%PAGE%">[%PAGE%]</a>';
		result += "<br/><center>Страница: ";
		for(var i=1;i<=totalPages;i++){
			if(i==currPage){
				result += "[" + i + "]";
			}else{
				result += link.replaceAll(template,i);
			}
			result += "&nbsp;";
		}
		result += "</center><br/>";
	}
	return result;
}
function stripHtml(str){
	str = str.replaceAll("&lt;","<");
	str = str.replaceAll("&gt;",">");
	str = str.replaceAll("&quot;","\"");
	str = str.replaceAll("&#039;","'");
	str = str.replaceAll("&amp;","&");
	str = str.replaceAll("&nbsp;"," ");
	str = str.replaceAll("<br>","\n");
	str = str.replaceAll("<br />","\n");
	str = str.replaceAll("<br/>","\n");
	str = str.replaceAll("<br />","\n");
	str = str.replaceAll("<BR>","\n");
	str = str.replaceAll("<BR/>","\n");
	str = str.replaceAll("<BR />","\n");
	return str;
}
/*
function getElementsByClass(searchClass,pnode,tag) {
	var classElements = new Array();
	if ( pnode == null ) pnode = document;
	if ( tag == null ) tag = '*';
	var els = pnode.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
	alert(els[i].className+'='+searchClass);
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}*/

function $() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string')
            element = document.getElementById(element);
        if (arguments.length == 1)
            return element;
        elements.push(element);
    }
    return elements;
}
function calc_chars(eln) {
	$(eln+'_chars').innerHTML = $('e_'+eln).value.length;
}
function setup_calc_chars(eln) {
	calc_chars(eln);
	$('e_'+eln).onchange=function(){calc_chars(eln)};
	$('e_'+eln).onkeypress=function(){calc_chars(eln)};
	$('e_'+eln).onkeyup=function(){calc_chars(eln)};
	$('e_'+eln).oncut=function(){calc_chars(eln)};
	$('e_'+eln).onpaste=function(){calc_chars(eln)};
}
function test_len(el,mn,mx,can_be_empty,name) {
	var el = $('e_'+el);
	if (can_be_empty && el.value.length==0) {
	} else if (el.value.length<mn) {
		lng.ne_menee_x_simvolov.replace("%num%",mn)
		alert(name+'. '+lng.ne_menee_x_simvolov.replace("%num%",mn)+'!');
		el.focus();
		return false;
	}
	if (el.value.length>mx) {
		alert(name+'. '+lng.ne_bolee_x_simvolov.replace("%num%",mx)+'!');
		el.focus();
		return false;
	}
	return true;
}
function toggle_display(id,disp_status){
	if ($(id)){
		var display_status = '';
		if(disp_status) display_status = disp_status;
		$(id).style.display = ($(id).style.display == 'none')?display_status:'none';
	} 
}