
// document.getElementById
function ge()
{
  var ea;
  for( var i = 0; i < arguments.length; i++ ) {
    var e = arguments[i];
    if( typeof e == 'string' )
      e = document.getElementById(e);
    if( arguments.length == 1 )
      return e;
    if( !ea )
      ea = new Array();
    ea[ea.length] = e;
  }
  return ea;
}

// Скрывает/раскрывает блок
function toggle(node)
{
	f = ge(node);
	if(f.style.display == "none")
	{
		f.style.display = "block";	
	}
	else
	{
		f.style.display = "none";
	}
}

// Debug Print all object properties
function dp(object)
{
  var descString;
  for(var value in object)
    descString += (value + " => " + object[value] + "\n");
  if( descString != "" )
    alert(descString);
  else
    alert(object);
}

// Debug Print to div on page
function dpd(debugOutput)
{
  if( ge('debugout') ) {
    ge('debugout').style.overflow = "auto";
    ge('debugout').innerHTML = debugOutput + "<br>" + ge('debugout').innerHTML;
  }
}

// Скрывает блок
function hide(node)
{
	ge(node).style.display = "none";	
}

// Раскрывает блок
function show(node)
{
	ge(node).style.display = "block";	
}
//Функция получения get переменных
function GetGetVariables() {
	var tmp = new Array();      // два вспомагательных   
	var tmp2 = new Array();     // массива   
	var getparam = new Array();   
	var get = location.search;  // строка GET запроса   
	if(get != '') {   
	     tmp = (get.substr(1)).split('&');   // разделяем переменные   
	     for(var i=0,len=tmp.length; i < len; i++) {   
	         tmp2 = tmp[i].split('=');       // массив param будет содержать   
	         getparam[tmp2[0]] = tmp2[1];       // пары ключ(имя переменной)->значение   
	     } 
	}
	return getparam;   
}

//Функция перемещения lable в неведомую даль
//входящие данные:
//a - id input поля

function CleanLable(a)
{
	var p=a.previousSibling;
	if(p)
	{
		a.onblur=function()
		{
			if(!a.value)
			{
				p.style.top=""
			}
		};
		p.style.top="-9999px"
	}
}

function TestCleanLable(a)
{
	var l=a.value.length;
	if(l>0)
		CleanLable(a);
}

function FillLable(a)
{
	var p=a.previousSibling;
	p.style.top=""
}

if(document.documentElement)
	{document.documentElement.id="js"}


function Ajax(doneHandler, failHandler)
{
	newAjax = this;
	this.onDone = doneHandler;
	this.onFail = failHandler;
	this.transport = this.getTransport();
	this.transport.onreadystatechange = ajaxTrampoline(this);
}

Ajax.prototype.get = function (uri, query)
{
	if( typeof query != 'string' )
		query = ajaxArrayToQueryString(query);
	fullURI = uri+(query ? ('?'+query) : '');
	this.transport.open('GET', fullURI, true);
	this.transport.send('');
}

Ajax.prototype.post = function (uri, data)
{
	if( typeof data != 'string' )
		data = ajaxArrayToQueryString(data);
	data = "ajax_q=true&"+data;
	this.transport.open('POST', uri, true);
	this.transport.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	this.transport.send(data);
}

Ajax.prototype.stateDispatch = function ()
{
	if( this.transport.readyState == 4 ) 
	{
		if( this.transport.status >= 200 && this.transport.status < 300 && this.transport.responseText.length > 0 ) 
		{
			if( this.onDone ) 
			{
				if(this.text)
					this.onDone(this, this.transport.responseText);
				else
					this.onDone(this, this.transport.responseXML.documentElement);
			}
    	}
		else 
		{
     		if( this.onFail ) this.onFail(this);
    	}
	}
}

Ajax.prototype.getTransport = function ()
{
	var ajax = null;
	
	try { ajax = new XMLHttpRequest(); }
	catch(e) { ajax = null; }
  
	try { if(!ajax) ajax = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) { ajax = null; }
	
	try { if(!ajax) ajax = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch(e) { ajax = null; }
	return ajax;
}

function ajaxTrampoline(ajaxObject)
{
	return function () { ajaxObject.stateDispatch(); };
}

function ajaxArrayToQueryString(queryArray)
{
	var sep = '';
	var query = "";
  
	for( var key in queryArray ) 
	{
		query = query +
		sep + encodeURIComponent(key) + '=' + encodeURIComponent(queryArray[key]);
		sep = '&';
	}
	return query;
}

