
var MSG_LOADING = "Loading... Please wait";
var MSG_READY = "Ready";
var REDIRECT_SESSION_EXPIRED_URL = '/account/login.srv?session_expired=1';
var REDIRECT_FORCE_LOGOUT_URL = '/error.srv';//'/login.page?force_logout=1';

//var MSG_CONNECTION_PROBLEM = "No connection with server";

function register(){
   doUpdate('/account/save-account.srv', 'workArea', Form.serialize("regForm"));
}
function doRequest(url, callbackFunctionObj)
{
    var myAjax = new Ajax.Request(
            url,
            {method: 'post', parameters: "", onComplete: callbackFunctionObj,
                on401: function (transport) {
                    document.location.href = REDIRECT_FORCE_LOGOUT_URL;
                },
                on403: function (transport) {
                    document.location.href = REDIRECT_SESSION_EXPIRED_URL;
                }

            }
            );
}

function defaultAjaxFailureReporter(request)
{
    //Dialog.alertDialogSimple("Error making ajax update");
}

function doUpdate(url, element, params, callBack)
{
    if ($(element))$(element).innerHTML = '<div align="center"><font color="black"><b>ЗАГРУЗКА...</b></font></div>';
    if (!params) var params = "";
    if (!callBack) var callbeckReporter = defaultAjaxFailureReporter;
    var myAjax = new Ajax.Updater(
        {success: element},
        url,
        {
        	method: 'post',
        	parameters: params,
            evalScripts: true,
            on401: function (transport) {
                document.location.href = REDIRECT_FORCE_LOGOUT_URL;
            },
            on403: function (transport) {
                document.location.href = REDIRECT_SESSION_EXPIRED_URL;
            },
		    onComplete: function()
		    {
                callbeckReporter();
            }
        }
    );
}

function getElementsByTagNames(list,obj)
{
    if (!obj) var obj = document;
    var tagNames = list.split(',');
    var resultArray = new Array();
    for (var i=0;i<tagNames.length;i++) {
        var tags = obj.getElementsByTagName(tagNames[i]);
        for (var j=0;j<tags.length;j++) {
            resultArray.push(tags[j]);
        }
    }
    var testNode = resultArray[0];
    if (!testNode) return [];
    if (testNode.sourceIndex) {
        resultArray.sort(function (a,b) {
                return a.sourceIndex - b.sourceIndex;
        });
    }
    else if (testNode.compareDocumentPosition) {
        resultArray.sort(function (a,b) {
                return 3 - (a.compareDocumentPosition(b) & 6);
        });
    }
    return resultArray;
}

var Common =
{
    explore: function (obj) {
        var str = "";
        var i = false;
        for (var p in obj) {
            if (i) {
                str += ", ";
            }
            str += "" + p;
            i = true;
        }
        return str;
    },

    exploreProperties: function (obj) {
        var str = "";
        var i = false;
        for (var p in obj) {
            if (i) {
                str += ", ";
            }
            str += "" + p + ": " + obj[p];
            i = true;
        }
        return str;
    }
}

var StrUtils =
{
	maxChars: function (str, maxChars, truncStr) {
		if ((maxChars > new String(truncStr)) ||
			(new String(str).length <= maxChars)) {
			return str;
		}
		return new String(str).substring(0, maxChars - new String(truncStr).length) + truncStr;
	}
}

/**
* Get position map {left: ..., top: ..., width: ..., height: ...} for an html `element`
*/
function getElementPosition(el)
{
    Position.prepare();
    var cumulativeOffset = Position.cumulativeOffset(el);
    var realOffset = Position.realOffset(el);

    var top = cumulativeOffset[1] - realOffset[1] + Position.deltaY;
    var left = cumulativeOffset[0] - realOffset[0] + Position.deltaX;
    var width = el.offsetWidth;
    var height = el.offsetHeight;

    return {left: left, top: top, width: width, height: height};
}


function showHideBox(id) {
	if ($(id).style.display == "none") $(id).style.display=""
	else $(id).style.display="none";
}

var lastItem = 0;
var menuClick = false;
var subMenuDisplayed = false;

function showTopMenu(item){
	hideTopMenu(lastItem);
	menuClick = true;
	lastItem = item;
	subMenuDisplayed = true;
	$("subTopMenu" + item).style.display = "block";
}

function hideTopMenu(){
	if (lastItem == 0) return true;
	if (!menuClick) {
		$("subTopMenu" + lastItem).style.display = "none";
		subMenuDisplayed = false;
	}
	menuClick = false;
}

function checkTopMenu(item){
	if (item == lastItem) return false;
	if (subMenuDisplayed) {
		$("subTopMenu" + lastItem).style.display = "none";
		$("subTopMenu" + item).style.display = "block";
		lastItem = item;
	}
}



function detectBrowser()
  {
  //bkt: detecting the users browser
  //alert("Browser information:\n\nNavigator code name: " + navigator.appCodeName + "\nNavigator name: " + navigator.appName + "\nVersion: " + parseFloat(navigator.appVersion) + "\nMinor version: " + navigator.appMinorVersion )
  switch (navigator.appName) {
      case "Microsoft Internet Explorer":
          if (navigator.appVersion.indexOf("MSIE 6.0") != -1) return "msieTrash";
          if (navigator.appVersion.indexOf("MSIE 7.0") != -1) return "msie7";
          else return "msieTrash";
          break
      case "Opera":
          return "opera";
          break
      case "Mozilla":
          return "mozilla";
          break
      case "Netscape":
          return "netscape";
          break
      default:
          return "msie"
          break;
  }

  }

function detectBrowser2(){
    if (window.XMLHttpRequest) {
         if(document.all){
            return "msie7";
         }else{
           //mozilla, safari, opera 9�etc
           return "mozilla";
         }
    } else {
      // IE6, older browsers
       return "msie"
    }
}

function isIe(){
     return(/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));
}

function getDocumentVisibleAreaOld()
{
    var _left = 0;
    var _top = 0;
    var _right = 0;
    var _bottom = 0;

    var cp = document.createElement("div");
    var s = cp.style;
    s.position = "absolute";
    s.right = s.bottom = s.width = s.height = "0px";
    document.body.appendChild(cp);
    var br = getElementPosition(cp);
    document.body.removeChild(cp);

    _right = br.left;
    _bottom = br.top;

    if (isIe()) {
        _top += document.body.scrollTop;
        _left += document.body.scrollLeft;
    } else {
        _top += window.scrollY;
    }

    _right += _left;
    _bottom += _top;

    return {left: _left, top: _top, right: _right, bottom: _bottom};
}

function getDocumentVisibleArea()
{
    var scrollX = self.pageYOffset ? self.pageXOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollLeft : document.body ? document.body.scrollLeft : null;
    var scrollY = self.pageYOffset ? self.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body ? document.body.scrollTop : null;
    //opera Netscape 6 Netscape 4x Mozilla
    if (window.innerWidth || window.innerHeight){
        docwidth = window.innerWidth;
        docheight = window.innerHeight;
    }
    //document.documentElement.clientHeight
    //IE Mozilla
    if (document.documentElement.clientWidth || document.documentElement.clientHeight){
        docwidth = document.documentElement.clientWidth;
        docheight = document.documentElement.clientHeight;
    }
    return {left: scrollX, top: scrollY, right: (scrollX + docwidth), bottom: (scrollY + docheight)};
}

function getScreenCenter(){
        var area = getDocumentVisibleArea();
        var x = Math.round((area.left + area.right) / 2);
        var y = Math.round((area.top + area.bottom) / 2);
        return {centerX: x, centerY: y};
}


function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight;

	return window.undefined;
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
	if (document.body) return document.body.clientWidth;
}

/**
 * Gets the real scroll top
 */

function getScrollTop() {
//	if (self.pageYOffset) // all except Explorer
//	{
//		return self.pageYOffset;
//	}
	if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}

function getScrollBottom() {
//	if (self.pageYOffset) // all except Explorer
//	{
//		return self.pageYOffset;
//	}
	if (document.documentElement && document.documentElement.scrollHeight)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollHeight;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollHeight;
	}
}

function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}

}

function howTall() {
     var docHeight =0;
      if (typeof document.height!= 'undefined') {
           docHeight = document.height;
      } else if (document.compatMode && document.compatMode!= 'BackCompat') {
        docHeight = document.documentElement.scrollHeight;
      } else if (document.body && typeof document.body.scrollHeight!= 'undefined') {
      docHeight = document.body.scrollHeight;
   }
    return docHeight;
}

// working with list box

function listBoxSelectAll(listBox)
{
    if (!listBox) return;
    for (var i=0; i<listBox.length; i++)
    {
        if (listBox.options[i]) listBox.options[i].selected = true;
    }
}

function listBoxDeselectAll(listBox)
{
    if (!listBox) return;
    for (var i=0; i<listBox.length; i++)
    {
        if (listBox.options[i]) listBox.options[i].selected = false;
    }
}

function listBoxRevertSelection(listBox)
{
    if (!listBox) return;
    for (var i=0; i<listBox.length; i++)
    {
        if (listBox.options[i]) listBox.options[i].selected = !listBox.options[i].selected;
    }
}

function listBoxDeleteAll(listBox)
{
    if (!listBox) return;
    while (listBox.options[0])
    {
        listBox.remove(0);
    }
}

function listBoxDeleteSelected(listBox)
{
    if (!listBox) return;
    for (var i=0; i<listBox.length; i++)
    {
        if (listBox.options[i] && listBox.options[i].selected)
        {
            listBox.remove(i);
            i--;
        }
    }
}

function listBoxMoveAll(listBoxFrom, listBoxTo)
{
    if (!listBoxFrom || !listBoxTo) return;
    for (var i=0; i<listBoxFrom.length; i++)
    {
        if (listBoxFrom.options[i])
        {
            var opt = document.createElement('option');
            opt.text = listBoxFrom.options[i].text;
            opt.value = listBoxFrom.options[i].value;
            try {
                listBoxTo.add(opt, null); // standards compliant; doesn't work in IE
            }
            catch(ex) {
                listBoxTo.add(opt); // IE only
            }
        }
    }
    while (listBoxFrom.options[0])
    {
        listBoxFrom.remove(0);
    }
}

function listBoxMoveSelected(listBoxFrom, listBoxTo)
{
    if (!listBoxFrom || !listBoxTo) return;
    for (var i=0; i<listBoxFrom.length; i++)
    {
        if (listBoxFrom.options[i] && listBoxFrom.options[i].selected)
        {
            var opt = document.createElement('option');
            opt.text = listBoxFrom.options[i].text;
            opt.value = listBoxFrom.options[i].value;
            var flag = true;
            for (var j=0; j<listBoxTo.length; j++)
            {
                if (opt.value == listBoxTo.options[j].value) flag = false;
            }
            if (flag) {
                try {
                    listBoxTo.add(opt, null); // standards compliant; doesn't work in IE
                }
                catch(ex) {
                    listBoxTo.add(opt); // IE only
                }
            }
        }
    }
    for (i=0; i<listBoxFrom.length; i++)
    {
        if (listBoxFrom.options[i] && listBoxFrom.options[i].selected)
        {
            listBoxFrom.remove(i);
            i--;
        }
    }
}

function showHelp(url){
    window.open(url, 'Help','toolbar=no,status=no,menubar=no');
}