// JavaScript Document
// CLASS TOGGLER V2  (C) Peter Bondarevsky (28.01.2008)
// If an object have several class names (or one class name, or no class name) we can toggle one with this function (add if it's not or remove if it is)
// 		obj	-	an object applying function to
// 		classN - class name, which should be toggled
// 		togling  - specifying operation
//					toggle - TOGGLEs class (remove if it is, and add if it's not)
//					dell - only REMOVEs class if it is, and if it's not - do nothing
//					add - only ADDs class if it is, and if it's not - do nothing

function toggleClass(obj,classN,togling){
	function test(clN,value){
		var myRe= new RegExp("\\b"+value+"\\b");
		return myRe.test(clN)?true:false;
		}
	function RMclass(clN,value){
		var rmclass = new RegExp ('(.*)(\\b'+value+'\\b)(.*)');
		return rmclass.exec(clN)[1]+rmclass.exec(clN)[3];
		}
	function rm(){var a=RMclass(obj.className,classN); a.length==0?obj.removeAttribute($ifBrowser('MSIE')?'className':'class'):obj.className=a}
	function add(){obj.className += ' ' + classN}
	var k = test(obj.className,classN);
	switch (togling){
		case 'toggle':	if (k) 	{ rm() 	} else { add() }; 	break;
		case 'dell':	if (k) 	{ rm() 	}					break;
		case 'add':		if (!k) { add() }					break;
		}
};


//jx - ajax method
jx={http:false,format:'text',callback:function(data){},handler:false,error:false,opt:new Object(),
getHTTPObject:function(){var http=false;if(typeof ActiveXObject !='undefined'){try{http=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){
try{http=new ActiveXObject("Microsoft.XMLHTTP");}catch(E){http=false;}}
}else if(XMLHttpRequest){try{http=new XMLHttpRequest();}catch(e){http=false;}}return http;},
load:function(url,callback,format,method){this.init();if(!this.http||!url)return;
if(this.http.overrideMimeType)this.http.overrideMimeType('text/xml');this.callback=callback;
if(!method)var method="GET";if(!format)var format="text";this.format=format.toLowerCase();
method=method.toUpperCase();var ths=this;var now="uid="+new Date().getTime();url+=(url.indexOf("?")+1)?"&":"?";
url+=now;var parameters=null;if(method=="POST"){var parts=url.split("\?SPL\?");url=parts[0];parameters=parts[1];}
this.http.open(method,url,true);if(method=="POST"){
this.http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
this.http.setRequestHeader("Content-length",parameters.length);this.http.setRequestHeader("Connection","close");
}if(this.handler){this.http.onreadystatechange=this.handler;}else{this.http.onreadystatechange=function(){
if(!ths)return;var http=ths.http;if(http.readyState==4){if(http.status==200){var result="";
if(http.responseText)result=http.responseText;if(ths.format.charAt(0)=="j"){result=result.replace(/[\n\r]/g,"");
result=eval('('+result+')');}else if(ths.format.charAt(0)=="x"){result=http.responseXML;}
if(ths.callback)ths.callback(result);}else{
if(ths.opt.loadingIndicator)document.getElementsByTagName("body")[0].removeChild(ths.opt.loadingIndicator);
if(ths.opt.loading)document.getElementById(ths.opt.loading).style.display="none";
if(ths.error)ths.error(http.status);}}}}this.http.send(parameters);},
bind : function(user_options) {var opt = {'url':'','onSuccess':false,'onError':false,'format':"text",
'method':"GET",'update':"",'loading':"",'loadingIndicator':""}
for(var key in opt) {if(user_options[key]) {opt[key] = user_options[key];}}
this.opt = opt;if(!opt.url) return;if(opt.onError) this.error = opt.onError;var div = false;
if(opt.loadingIndicator) {div = document.createElement("div");
div.setAttribute("style","position:absolute;top:0px;left:0px;");div.setAttribute("class","loading-indicator");
div.innerHTML = opt.loadingIndicator;document.getElementsByTagName("body")[0].appendChild(div);
this.opt.loadingIndicator=div;}if(opt.loading) document.getElementById(opt.loading).style.display="block";
this.load(opt.url,function(data){if(opt.onSuccess) opt.onSuccess(data);
if(opt.update) document.getElementById(opt.update).innerHTML = data;
if(div) document.getElementsByTagName("body")[0].removeChild(div);
if(opt.loading) document.getElementById(opt.loading).style.display="none";
},opt.format,opt.method);},init : function() {this.http = this.getHTTPObject();}}

function hasClass(obj,value){
	var myRe= new RegExp("\\b"+value+"\\b");
	return myRe.test(obj.className)?true:false;
	};

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
};

function windowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
};


Array.prototype.findIndex = function(value){
	var ctr = "";
	for (var i=0; i < this.length; i++) {
	if (this[i] == value) {
	return i;
	}
	}
	return ctr;
};

Array.prototype.inArray = function(value){
	var ctr = false;
	for (var i=0; i < this.length; i++) {
		if (this[i] == value) {ctr = true}
	}
	return ctr;
};

Array.prototype.css = function(value){
	for (var i=0;i<this.length;i++) {
		this[i].style.cssText = value;
	}
};


function setCols71(obj) {
          var textarea = obj;
          textarea.setAttribute("cols", Math.floor(textarea.clientWidth / 6));
          setRows71(obj);
};

function setRows71(obj) {
          var textarea = obj;
          var cols = textarea.cols;
          var str = textarea.value;
          // windows - replace \r\n
          // mac - replace just \r
          // linux - is just \n
          str = str.replace(/\r\n?/, "\n");
          var lines = 2;
          var chars = 0;
          for (var i=0; i<str.length; i++) {
            var c = str.charAt(i);
            chars++;
            if (c == "\n" || chars == cols) {
              lines ++;
              chars = 0;
            }
          }
          textarea.setAttribute("rows", lines);
//          textarea.style.height = (lines - (navigator.userAgent.indexOf("Opera")!=-1 ? 0 : 1))*17 + "px";//fixing opera line count bug
          textarea.style.height = lines*(navigator.userAgent.indexOf("Opera")!=-1 ? 18 : 17) + "px";//fixing opera line count bug
};


function showHideForm(obj){
	obj.style.display = obj.style.display=='' || obj.style.display=='block' ? 'none' : 'block';
	}


/*$.addClass = function(classN){ toggleClass(this,classN,'add') };
$.removeCass = function(classN){ toggleClass(this,classN,'dell') };
$.toggleCass = function(classN){ toggleClass(this,classN,'toggle') };*/


main = function () {}

main.getElement = function(ev) {

	var f = main.is_ie ? window.event.srcElement : ev.currentTarget;

	while (f.nodeType != 1 || /^div$/i.test(f.tagName))
		f = f.parentNode;

	return f;
};

main.visTogg = function(o){
	o.style.visibility = o.style.visibility=='' || o.style.visibility=='hidden' ? 'visible' : 'hidden';
};


main.getTargetElement = function(ev) {
	var f = main.is_ie ? window.event.srcElement : ev.target;
	while (f.nodeType != 1)
		f = f.parentNode;
	return f;
};

main.addEvent = function(el, evname, func) {

	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C

		el.addEventListener(evname, func, true);
	} else {

		el["on" + evname] = func;
	}
};


main.removeEvent = function(el, evname, func) {
	if (el.detachEvent) { // IE
		el.detachEvent("on" + evname, func);
	} else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(evname, func, true);
	} else {
		el["on" + evname] = null;
	}
};


main.is_ie = ( /msie/i.test(navigator.userAgent) &&
		   !/opera/i.test(navigator.userAgent) );
main.is_ie5 = ( main.is_ie && /msie 5\.0/i.test(navigator.userAgent) );
main.is_opera = /opera/i.test(navigator.userAgent);
main.is_khtml = /Konqueror|Safari|Firefox| KHTML/i.test(navigator.userAgent);

function checker(obj,arr){
	for (i=0;i<arr.length;i++) {
		if (obj==arr[i]) { return true; break; }
		}
	};

function getAbsolutePos(obj){
	var top = 0;
	var left = 0;
	
	for (; obj; obj = obj.offsetParent) {
		top  += obj.offsetTop;
		left += obj.offsetLeft;
		//top  += obj.scrollTop;
		//left += obj.scrollLeft;
	}
	
	return [top,left];
}

function getMouseXY(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;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
	return [ posx, posy ]
}

var rmChildren = function (node) {
	while (node.hasChildNodes()) {
	node.removeChild(node.firstChild);
	}
};


var charCode;//returns curent charcode pressed. Compatible with IE, FF and Opera
function charCodeset(evt){
	charCode = (window.event) ? event.keyCode : evt.which;
	}

