var bhv = {util: {}};


bhv.util.getXMLHttpRequest = function(){
  var xmlReq;
  if (window.XMLHttpRequest)
    xmlReq = new window.XMLHttpRequest();
  else
    try {
      xmlReq = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e) {
      try {
        xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e) {
        xmlReq = null;
      }
    }
  return xmlReq;
}

bhv.util.nullFunction = bhv.util.emptyFunction = function(){};

bhv.util.defaultError = function(){
if (typeof this.responseText != "undefined")
    alert("Îøèáêà:\n" + this.responseText);
else
    alert("Îøèáêà: XMLHttpRequest");
}

bhv.util.registreCallbackFunction = function(xmlHttpRequest, callback, onerror, callbackArgsArray){
    return function(){
        if(xmlHttpRequest.readyState == 4){
            if(! xmlHttpRequest.status || xmlHttpRequest.status >= 200 && xmlHttpRequest.status < 300 
                   || xmlHttpRequest.status == 304)
                callback.apply(xmlHttpRequest, callbackArgsArray);
            else
                if (typeof onerror == "function")
                    onerror.apply(xmlHttpRequest, callbackArgsArray);
                else
                    throw new Error("Îøèáêà ñîçäàíèÿ XMLHttpRequest")
            xmlHttpRequest.onreadystatechange = bhv.util.nullFunction;        
        };
    }
}

bhv.sendRequest = function(httpMethod, url, httpParams, async, callback, onerror, callbackArgsArray,
    contentType, headers){

 
if (! onerror)
    onerror = bhv.util.defaultError;

if (! callbackArgsArray)
    callbackArgsArray = [];

if (! contentType)
    contentType = "application/x-www-form-urlencoded";

if (! headers)
    headers = {};

var xmlHttpRequest = bhv.util.getXMLHttpRequest();

if (async)
    xmlHttpRequest.onreadystatechange = 
        bhv.util.registreCallbackFunction(xmlHttpRequest, callback, onerror, callbackArgsArray);

try{
    if (httpMethod.toLowerCase() == "get"){
        if (! httpParams)
            httpParams = "antiCache=" + Math.random();
        else
            httpParams = "antiCache=" + Math.random() + "&"+ httpParams;
        xmlHttpRequest.open("get", url + "?" + httpParams, async);
        xmlHttpRequest.setRequestHeader("Content-Type", contentType);
        for (var header in headers)
            xmlHttpRequest.setRequestHeader(header, headers[header]);
        xmlHttpRequest.send(null);
    }
    else{
        xmlHttpRequest.open("post", url, async);
        xmlHttpRequest.setRequestHeader("Content-Type", contentType);
        for (var header in headers)
            xmlHttpRequest.setRequestHeader(header, headers[header]);
        xmlHttpRequest.send(httpParams);
    }
}catch(e){
    xmlHttpRequest.onreadystatechange = bhv.util.emptyFunction;
    if (typeof onerror == "function")
        onerror.apply(xmlHttpRequest, callbackArgsArray);
    else
        throw new Error("Îøèáêà XMLHttpRequest")
}
    
if (! async)
    if (! xmlHttpRequest.status || xmlHttpRequest.status >= 200 && xmlHttpRequest.status < 300
        || xmlHttpRequest.status == 304)
            callback.apply(xmlHttpRequest, callbackArgsArray);
    else
        if (typeof onerror == "function")
            onerror.apply(xmlHttpRequest, callbackArgsArray);
        else
            throw new Error("Îøèáêà XMLHttpRequest")

}
//------------------------------------------------------------
bhv.sendScriptRequest = function(url, httpParams, callback, callbackArgsArray) {
  
  var currentScript =document.createElement("SCRIPT");// bhv.sendScriptRequest.free.pop();
  if (httpParams)
    httpParams="?rand=" + Math.random() + "&" + httpParams;
  else
    httpParams="?rand=" + Math.random();
  currentScript.bhv_readyState = false;
  currentScript.onload = bhv.util.scriptCallback(currentScript, callback, callbackArgsArray);
  currentScript.onreadystatechange = bhv.util.scriptCallback(currentScript, callback, callbackArgsArray);

  currentScript.src = url + httpParams;
  document.getElementsByTagName("script")[0].parentNode.appendChild(currentScript);
}

bhv.util.scriptCallback = function(currentScript, callback, callbackArgsArray){
  return function() {
      if (currentScript.bhv_readyState)
        return;
      if (! currentScript.readyState || currentScript.readyState == "loaded" || currentScript.readyState == "complete") {
        currentScript.bhv_readyState = true;
        callback.apply(currentScript, callbackArgsArray)
        currentScript.parentNode.removeChild(currentScript);
       }
    }
 
}
//------------------------------------------------------------
bhv.getElementData = function(parent, child){
if (! child)
    child = parent;
if (typeof child == "string")
    child = parent.getElementsByTagName(child)[0];
return child.firstChild.data;
// if undefined child - throw new Error()
}
//------------------------------------------------------------
bhv.key={};

bhv.key.BACKSPACE = 8;
bhv.key.TAB = 9;
bhv.key.ENTER = 13;
bhv.key.SHIFT = 16;
bhv.key.CTRL = 17;
bhv.key.ALT = 18;
bhv.key.PAUSE = 19;
bhv.key.CAPSLOOK = 18;
bhv.key.ESC = 27;

bhv.key.SPACE = 32;

bhv.key.PAGEUP	= 33;
bhv.key.PAGEDOWN = 34;
bhv.key.END = 35;
bhv.key.HOME = 36;

bhv.key.LEFT = 37;
bhv.key.UP = 38;
bhv.key.RIGHT = 39;
bhv.key.DOWN = 40;

bhv.key.PRINTSCREEN = 44;
bhv.key.INSERT = 45;
bhv.key.DELETE = 46;

bhv.key.F1 = 112;
bhv.key.F2 = 113;
bhv.key.F3 = 114;
bhv.key.F4 = 115;
bhv.key.F5 = 116;
bhv.key.F6 = 117;
bhv.key.F7 = 118;
bhv.key.F8 = 119;
bhv.key.F9 = 120;
bhv.key.F10 = 121;
bhv.key.F11 = 122;
bhv.key.F12 = 123;

//--------------------------------------------------------------------
bhv.isVisible=function(elem){
if (typeof elem == "string")
    elem=document.getElementByID(elem);
if (typeof elem != "object")
    return false;
if (elem.type=="hidden")
    return false;
var isNone=false
var isVisible=false
var isHidden=false
do{
    if (elem.style){
        isNone = elem.style.display == "none"
        if (! isHidden)
            isHidden = elem.style.visibility == "hidden";
        if (! isHidden && ! isVisible)
            isVisible = elem.style.visibility == "visible";

    }
    elem = elem.parentNode
} while (! isNone && elem)
return ! isNone && (! isHidden || isVisible)
}
//--------------------------------------------------------------------
bhv.selectPreviousInput=function(elem)
{
if (elem)
	elem.blur();
else
	return;
var allInput = document.getElementsByTagName("input");
var isNext = false;
if (allInput && allInput.length > 0)
	for (var i = allInput.length - 1; i >= 0; i--)
	try {
		if (isNext && bhv.isVisible(allInput[i]) && !allInput[i].disabled)
		{
			allInput[i].focus();
			return true;
		}
			
		if (! isNext && allInput[i] == elem)
			isNext = true;

	} catch (ex) {}

	elem.focus();
	
}
//--------------------------------------------------------------------
bhv.selectNextInput=function(elem)
{

if (elem)
	elem//.blur();
else
	return true;
var allInput = document.getElementsByTagName("input");
var isNext = false;
if (allInput && allInput.length > 0)
	for (var i = 0 ; i < allInput.length; i++)
	try {

		if (isNext && bhv.isVisible(allInput[i]) && !allInput[i].disabled)
		{
			allInput[i].focus();
			return true;
		}
			
		if (! isNext && allInput[i] == elem)
			isNext = true;
	} catch (ex) {}
	elem.focus();
	return true;
}

//---------------------------------------------------------------------------------
bhv.commandQueue={}



bhv.commandId = 0;

//----------------------------------------------------------------
bhv.callCommand=function(name, id){
    if (bhv.commandQueue[name] && bhv.commandQueue[name][id])
       var currentCommand = bhv.commandQueue[name][id];
    else
        return;

    delete bhv.commandQueue[name][id];
    currentCommand.command.apply(currentCommand.context, currentCommand.args);
    delete currentCommand.command;
    delete currentCommand.context;
    delete currentCommand.args;   

}

//------------------------------------------------------------------
bhv.setCommand=function(command, context, args, timeout, name){

var id = "id"+ (++bhv.commandId%1000);

if (! timeout && (timeout !== 0))
    timeout = 1000;

if (! name)
    name = "default";
else if (bhv.commandQueue[name])
    delete bhv.commandQueue[name];    


if (! bhv.commandQueue[name])
    bhv.commandQueue[name] = {};

bhv.commandQueue[name][id] = {};

bhv.commandQueue[name][id]["command"] = command;
bhv.commandQueue[name][id]["context"] = context;
bhv.commandQueue[name][id]["args"] = args;

setTimeout("bhv.callCommand('" + name+ "', '" + id + "')", timeout);

}
//------------------------------------------------------------------
bhv.unsetCommand=function(name){
bhv.commandQueue[name] = null;    
delete bhv.commandQueue[name];    
}

//---------------------------------------------------------------------------------
bhv.compareString = function(string0, string1){

if (typeof string0 != "string")
    return -1;

if (typeof string1 != "string")
    return -1;

string0 = string0.toUpperCase();
string1 = string1.toUpperCase();

var length = Math.max(string0.length, string1.length);

for (var i = 1; i <= length; i++)
    if (string0.substr(0,i) != string1.substr(0,i))
        return i - 1;
return length;
}

bhv.APPLICATION_FOLDER = null;

bhv.getApplicationFolder = function(){
  if (bhv.APPLICATION_FODER)
    return bhv.APPLICATION_FOLDER;

  var scripts = document.getElementsByTagName("SCRIPT");
  var indexOfRoot = -1;
  for (var i = 0; i < scripts.length; i++) {
    indexOfRoot = String(scripts[i].src).replace(/\\/g,'/').lastIndexOf('bhv/util.js');
    if (indexOfRoot >= 0){
        bhv.APPLICATION_FOLDER = new String(scripts[i].src).substring(0, indexOfRoot)
        return bhv.APPLICATION_FOLDER;
    }
  }
}


bhv.getAbsolutePath = function(path, relative) {

  path = path.replace(/\\/g, "/");
  if (path.substring(0, 1) == "/")
    return path;

  var current = document.location.pathname;
  current = current.replace(/\\/g, "/");
  current = current.substring(0, current.lastIndexOf("/") + 1);

  if (relative) {
    relative = relative.replace(/\\/g, "/");
    relative = relative.substring(0, relative.lastIndexOf("/") + 1);
    if (relative.substring(0, 1) == "/")
      current = relative;
    else 
      current = current + relative;
    }

  return current + path;
}

bhv.simpleRelocateSRC = function(htmlText, relative) {

var newText = "";
var symbol = "";
var beforURL = "";
var someURL = "";
var isTag = false;
var isSRC = false;
var isAttr = false;
var isURL = false;
for (var i = 0; i < htmlText.length; i++) {
   var symbol = htmlText.substring(i,i+1);
   if (symbol == "<"){
     isTag = true;
     isSRC = false;
     isAttr = false;
     newText += symbol;
   } else if (symbol == ">"){
     isTag = false;
     isSRC = false;
     isAttr = false;
     newText += symbol;
   } else if (isTag && htmlText.substring(i, i+4).toLowerCase() == " src"){
     isSRC = true;
     isAttr = false;
     newText += htmlText.substring(i, i+4);
     i += 3;
   } else if (isTag && htmlText.substring(i, i+5).toLowerCase() == " href"){
     isSRC = true;
     isAttr = false;
     newText += htmlText.substring(i, i+5);
     i += 4;
   } else if (isTag && isSRC && symbol == "="){
     isAttr = true;
     newText += symbol;
   } else if (isTag && isSRC && isAttr && symbol == '"') {
     var index = htmlText.indexOf('"', i+1);
     someURL = htmlText.substring(i+1, index);
     someURL = bhv.getAbsolutePath(someURL, relative);
     newText += ('"' + someURL + '"');
     i = index;
     isSRC=false;
     isAttr = false;
   } else if (isTag && isSRC && isAttr && symbol == "'") {
     var index = htmlText.indexOf("'", i+1);
     someURL = htmlText.substring(i+1, index);
     someURL = bhv.getAbsolutePath(someURL, relative);
     newText += ("'" + someURL + "'");
     i = index;
     isSRC=false;
     isAttr = false;
   } else if (isTag && isSRC && isAttr && symbol != " ") {
     var index = Math.min((htmlText+" ").indexOf(" ", i+1), htmlText.indexOf(">", i+1));
     someURL = htmlText.substring(i, index);
     someURL = bhv.getAbsolutePath(someURL, relative);
     newText += (" " + someURL + " ");
     i = index-1;
     isSRC = false;
     isAttr = false;
   } else
     newText +=symbol;
}
alert(newText)
return newText;
}


bhv.relocateSRC = function(htmlText, relative) {
  var newText = htmlText.replace(/(<[^>]*\s(src|href)\s*=\s*(\"|\'))(.*)(\3[^>]*>)/gi,"$1"+bhv.getAbsolutePath("$4",relative)+"$5");
  newText = newText.replace(/(<[^>]*\s(src|href)\s*=\s*)([^\s\"\'>]+)([^>]*>)/gi,"$1"+bhv.getAbsolutePath("$3",relative)+"$4");
  return newText;
}


bhv.top = function(element){
  var top = 0;
  try{
    top = element.offsetTop;
    while(element.offsetParent){
      element = element.offsetParent;
      top += element.offsetTop
    }
  } catch (ex){}

return top;
}

bhv.left = function(element){
  var left = 0;
  try{
    left = element.offsetLeft;
    while(element.offsetParent){
      element = element.offsetParent;
      left += element.offsetLeft
    }
  } catch (ex){}

return left;
}



document.write('<div id="bhv_contentPane" style="position:absolute;top:0;left:0;margin:0;padding:0;border:0"></div>')
bhv.contentPane = document.getElementById("bhv_contentPane");


