/*
	library of js function
	by Rodion Konnov
	created 24.01.2007
*/

// аналог пхп-шной print_r
Array.prototype.toString = Object.prototype.toString = function() {
  var cont = [];
  var addslashes = function(s) {
    // Использовать replace НЕЛЬЗЯ - в Опере
    // происходит зацикливание, т.к. из replace
    // зачем-то вызывается Object.toString().
    return s.split('\\').join('\\\\').split('"').join('\\"');
  };
  for (var k in this) {
    if (cont.length) cont[cont.length-1] += ",";
    var v = this[k];
    var vs = '';
    if (v.constructor == String)
      vs = '"' + addslashes(v) + '"';
    else
      vs = v.toString();
        if (this.constructor == Array)
      cont[cont.length];
        else
      cont[cont.length] = k + ": " + vs;
  }
  // Здесь тоже нельзя делать replace()!
  cont = "  " + cont.join("\n").split("\n").join("\n  ");
  var s = cont;
  if (this.constructor == Object) {
    s = "{\n"+cont+"\n}";
  } else if (this.constructor == Array) {
    s = "[\n"+cont+"\n]";
  }
  return s;
};

var agt=navigator.userAgent.toLowerCase();
var ie=(/msie/.test(agt));
var geco=(/gecko/.test(agt));
var opera=(/opera/.test(agt));
var konq=(/konqueror/.test(agt));
var safari=(/safari/.test(agt));
var mac=(/macintosh/.test(agt));

function init(value) {
	return (document.getElementById) ? 
		document.getElementById(value):document.all[value];
}

function init_o(value) {
	return window.opener.document.getElementById ? 
		window.opener.document.getElementById(value):window.opener.document.all[value];
}

function init_flash(value) {
	return (ie) ? window[value]:document[value];
}

function invert(id) {
	obj=init(id);
	if ( obj ) {
		if ( obj.style.display=="none" ){
			obj.style.display="block";
		} else {
			obj.style.display="none";
		}
	}
}

function invert_visible(id) {
	obj=init(id);
	if ( obj ) {
		if ( obj.style.visibility=="hidden" ){
			obj.style.visibility="visible";
			obj.style.position="static";
		} else {
			obj.style.visibility="hidden";
			obj.style.position="static";
		}
	}
}

function swap( id1, id2 ) {
	obj1=init(id1);
	obj2=init(id2);
	if ( obj1 && obj2 ) {
		invert(id1);
		invert(id2);
	}
}

function swap_visible( id1, id2 ) {
	obj1=init(id1);
	obj2=init(id2);
	if ( obj1 && obj2 ) {
		invert_visible(id1);
		invert_visible(id2);
	}
}
//event handler
function evt2obj(obj,evt,func) {
	var oldhandler = obj[evt];
	obj[evt] = (typeof obj[evt] != 'function') ? func : function(ev){oldhandler(ev);func(ev);};
}

function evt2id(id,evt,func) {
	obj=init(id);
	if (!obj) {
		return false;
	}
	evt2obj(obj,evt,func);
	return true;
}
// xmlHttp request hdl
function get_req(obj,func) {
	var req=new JsHttpRequest();
	if (func) {
		req.onreadystatechange = function() {
			if ( req.readyState == 4 ) {
				func(req.responseJS);
			}
		}
	}
	req.caching=false;
	req.loader='xml';
	req.open( 'post', '/hr.php', true );
	req.send( obj );
}

// location on current page
function obj_location(obj) {
	var loc={x:0,y:0};
	while( obj ) {
		loc.x+=obj.offsetLeft;
		loc.y+=obj.offsetTop;
		obj=obj.offsetParent;
	}
	return loc;
}

// preload images
function img_preload(imageArray) {
	var tempImages = new Array();
	for(var i=0;i<imageArray.length;i++){tempImages[i] = new Image();tempImages[i].src = imageArray[i];}
}
// change img under mouse-pointer
function img_hover(id,new_src) {
	var obj=init(id);
	var old_src=obj.src;
	obj.src=new_src;
	if ( ie ) {
		obj.onmouseout=function() {
			obj.src=old_src;
		}
	} else {
		this.onmouseout=function() {
			obj.src=old_src;
		}
	}
}
// cycle through a list of class names (usually used for css-fades)
function cycleThroughClassNames(id,obj,objClass,start,i) {
	if (i > 0) {
		obj.className = objClass+i;i--;timeouts[id] = setTimeout( function(){cycleThroughClassNames(id,obj,objClass,start,i)},90);
	} else {
		clearTimeout(timeouts[id]);
	}
}
// stop default action of an event
function stopDefaultAction(ev) {
	if (!ev) ev = window.event;
	(ev.stopPropagation) ? ev.stopPropagation() : ev.cancelBubble = true;
	(ev.preventDefault) ? ev.preventDefault() : ev.returnValue = false;
	return false;
}
// remove an element
function removeElement(el) {
	if (el.parentNode){el.parentNode.removeChild(el);}
}

// gen popup
var objWindow;
/*
intPos 1-random; 2-center; 3-100x100
intScroll 1-resizable
*/
function gen_popup( strUrl, intW, intH, intPos, intScroll ) {
	// get settings
	if ( isNaN( intW ) ) {
		intW=640;
	}
	if ( isNaN( intH ) ) {
		intH=430;
	}
	if(intPos==1) {
		LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-intW)):100;
		TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-intH)-75)):100;
	}
	if(intPos==2) {
		LeftPosition=(screen.width)?(screen.width-intW)/2:100;
		TopPosition=(screen.height)?(screen.height-intH)/2:100;
	}
	if ( isNaN( intPos )||intPos==3 ) {
		LeftPosition=100;
		TopPosition=100;
	}
	strScroll=',scrollbars=no,resizable=on';
	if (intScroll == 1) {
		strScroll=',scrollbars=yes,resizable=yes';
	}
	settings='width='+ intW + ',height='+ intH + ',top=' + TopPosition + ',left=' + LeftPosition + strScroll + ',location=no,directories=no,menubar=no,toolbar=no,status=no,dependent=no';
	// get popup
	if ( !objWindow||objWindow.closed ) {
		//alert(strUrl);
		objWindow=window.open( strUrl, 'popup', settings );
		objWindow.focus();
	} else {
		objWindow.focus();
	}
	return false;
}

function js_pager(numItems, numOnPage, itemsPerPage, currentPage, pageLinkFunc) {
	if (numItems <= itemsPerPage) {
		return '';
	}
	if ( numItems-((currentPage*itemsPerPage)+numOnPage)<numOnPage ) {
		itemsPerPageN=numItems-((currentPage*itemsPerPage)+numOnPage);
	} else {
		itemsPerPageN=itemsPerPage;
	}
	var html = '';
	var numPages = Math.ceil(numItems/itemsPerPage);
	// gen html
	html +='Results '+ ((currentPage*itemsPerPage)+1) +'-'+ ((currentPage*itemsPerPage)+numOnPage) +' of '+ numItems;
	if ( numItems>itemsPerPage ) {
		html +=' &nbsp;|&nbsp; <span class="c07">';
	}
	if (currentPage > 0) {
		html += '&laquo; <a href="'+ pageLinkFunc(currentPage-1) +'" class="c07">Previous '+ itemsPerPage +'</a>';
	}
	if ( currentPage > 0 && currentPage< (numPages-1) ) {
		html +=' &nbsp;|&nbsp; ';
	}
	if ( currentPage< (numPages-1) ) {
		html +='<a href="' + pageLinkFunc(currentPage+1) + '" class="c07">Next '+ itemsPerPageN +'</a> &raquo;';
	}
	if ( numItems>itemsPerPage ) {
		html +='</span>';
	}
	return '<div class="tBrd2 bcE4 p5 pl15">' + html + '</div>';
}

// error massages
function show_mess(type) {
	alert((mess[type]>''?mess[type]:mess['0']));
}

// checkbox selector
function toggle_checkbox(id,objCh) {
	var obj=init(id);
	if ( !obj ) {
		return false;
	}
	for( i=1; i<obj.elements.length; i++ ) {
		if ( obj.elements[i].type=='checkbox' ) {
			obj.elements[i].checked=objCh.checked
		}
	}
}

function toggle_multicheckbox(id,part,objCh) {
	var obj=init(id);
	if ( !obj ) {
		return false;
	}
	var pt='/' + part + '/.test(obj.elements[i].name)';
	for( var i=0; i<obj.elements.length; i++ ) {
		if ( obj.elements[i].type=='checkbox'&&eval(pt) ) {
			obj.elements[i].checked=objCh.checked
		}
	}
}