// возвращает строку состоящую из выбранных элементов в мультиселекте
function getValue()
{
    var val='';
    SelectArr = document.getElementById('filter').options;
    
    for ( var i in SelectArr)
    {
        if(SelectArr[i] != null)
        {
            if (SelectArr[i].selected)
            {
                if (val=='') { val = SelectArr[i].value; }
                else { val += ',' + SelectArr[i].value; }
            }
        }
    }
    return val;
}

// -----------------------------
// Отображаем с такими координатами.
// -----------------------------

function ShowLayer(lName, x, y, Showing)
{

var skin;

if(lName=='') return;

skin=document.getElementById(lName);

if(skin==null) return;

skin.style.left = x;
skin.style.top  = y;

skin.style.zIndex=2;
if(Showing) skin.style.visibility="visible"; else skin.style.visibility="hidden";

}


// -----------------------------
// Переход на URL
// -----------------------------

function GotoUrl(Url)
{

location.href = Url;

}


// -----------------------------
// Прячем слой.
// -----------------------------

function HideLayer(lName)
{

var skin;

if(lName=='') return;

skin=document.getElementById(lName);

if(skin==null) return;

//skin.style.zIndex=2;
skin.style.visibility="hidden";

}


// ------------------------------------------
// Set parent content
// ------------------------------------------

function SetParentLayerContent(lName, Content) 
    {

    // get skin
    var skin = parent.document.getElementById(lName);
    if(skin==null) return;
    skin.innerHTML=Content;

    }


// ---------------------------------------
// Show controller in iframe
// ---------------------------------------

function ShowInFramePos(x,y,path, elem)
{

var old_s = document.getElementById('controller_iframe');

if (old_s)
    {
    old_s.parentNode.removeChild(old_s);            
    }/**/


var s = document.createElement('iframe');

var wwidth=(window.innerWidth)?window.innerWidth:
    ((document.all)?document.body.offsetWidth:null);
var wheight=(window.innerHeight)?window.innerHeight:
    ((document.all)?document.body.offsetHeight:null);

s.src = path;
s.id = 'controller_iframe';
s.style.position = "absolute";
s.style.display = "block";
s.frameBorder = 0;
s.scrolling = "no";
s.style.left = x/wwidth*100 + '%';
s.style.top  = y/wheight*100 + '%';
s.style.zIndex  = 100;
s.width=250;
s.height=102;
s.style.backgroundColor = "#FFFFFF";
s.style.border = "0px";/**/

document.body.appendChild(s);
//elem.appendChild(s);

}

// ------------------------------------------
// Edit translation
// ------------------------------------------

function EditTranslation(Cnt)
    {
    ShowInFramePos(GetControlX(Cnt),GetControlY(Cnt),"/index.php/admin/translate_word/"+Cnt.id, Cnt)
    return(false);
    }

// -----------------------------
// Загружаем слой
// -----------------------------

function LoadLayer(lName, LoadFrom)
{

var link = LoadFrom + "&rand=" + Math.random();
var old_s = document.getElementById('dataloader');
if (old_s)
    {
    old_s.parentNode.removeChild(old_s);            
    }

var s = document.createElement('script');
s.setAttribute('type', 'text/javascript');
s.src = link;
s.id = 'dataloader';
document.getElementsByTagName('head')[0].appendChild(s);        
}


// open new window
function OpenNewWindow(href, text,width, height)
{
if(text)
    {
    if(confirm(text))
        {
    return window.open(href, '', 'width=' + width + ',height=' + height + ',scrollbars');
        }
    else
        {
        return false;
        }
    }
else
    {
    return window.open(href, '', 'width=' + width + ',height=' + height + ',scrollbars');
    }
}


// ------------------------------------------
// Resize window
// ------------------------------------------

function ResizeWindow(w,h) 
    {

    var height=0; var width=0;
    if (self.screen) 
    {
        width = screen.width
        height = screen.height
    }
    else if (self.java) 
    {
        var jkit = java.awt.Toolkit.getDefaultToolkit();
        var scrsize = jkit.getScreenSize();
        width = scrsize.width;
        height = scrsize.height;
    }
    height -= 180;
           
    if (parseInt(navigator.appVersion)>3) 
        {
        if (navigator.appName=='Netscape') 
            {
            if(height)
                {
                if(height > h+51) 
                    {
                    top.outerHeight= h+51;
                    top.outerWidth = w;
                    }
                else 
                    {
                    top.outerHeight= height;
                    top.outerWidth = w+14;
                    }
                }
            }
            else 
            {
//          top.resizeTo(400,300);
//            wd = 400-document.body.clientWidth;
//            hd = 340-document.body.clientHeight;
            //top.resizeTo(w+0,h+51);
            if(height > h+51) top.resizeTo(w+18,h+51);
            else top.resizeTo(w+18,height);
            }
        }
    }


//--------------------------------
//  show|hide element
//--------------------------------
function z(id)
{
if(document.getElementById(id) == null)
    {
    return;
    }

if(document.getElementById(id).style.display=='block')
    {
    document.getElementById(id).style.display='none';
    }
else
    {
    document.getElementById(id).style.display='block';
    }
}

//--------------------------------
//  show|hide element
//--------------------------------
function Banner(id,type,l,w,h)
{
if(id.value == type)
    {
      document.getElementById(l).style.display='none';
      document.getElementById(w).style.display='block';
      document.getElementById(h).style.display='block';

    }
else
    {
      document.getElementById(l).style.display='block';
      document.getElementById(w).style.display='none';
      document.getElementById(h).style.display='none';
    }



}

//--------------------------------
//  show|hide element in class
//--------------------------------
function zx(id)
{
var element;
if(document.getElementById(id) == null)
    {
    if(id.className == null)
        {
        return;
        }
    else
        {
        element = id;
        }
    }
else
    {
    element = document.getElementById(id);
    }

if(element.className=='hide')
    {
    element.className='show';
    }
else
    {
    element.className='hide';
    }
}



/**
 * Checks/unchecks all tables
 *
 * @param   string   the form name
 * @param   boolean  whether to check or to uncheck the element
 *
 * @return  boolean  always true
 */
function setCheckboxes(the_form, do_check, formname)
{
    var elts      = (typeof(document.forms[the_form].elements[formname]) != 'undefined')
                  ? document.forms[the_form].elements[formname]: 0;
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;

    if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            elts[i].checked = do_check;
        } // end for
    } else {
        elts.checked        = do_check;
    } // end if... else

    return true;
} // end of the 'setCheckboxes()' function


// -----------------------------
// Получаем положение контрола по X.
// -----------------------------
function GetControlX(Control) {
  var Tmp = Control;
  var X = 0;
  while (Tmp) {
    X += Tmp.offsetLeft;
    Tmp = Tmp.offsetParent;
  }
  return parseInt(X);
}


// -----------------------------
// Получаем положение контрола Y.
// -----------------------------
function GetControlY(Control)
{

var Tmp = Control;
var Y = 0;

while(Tmp)
    {
    Y += Tmp.offsetTop;
    Tmp = Tmp.offsetParent;
    }

return ( parseInt(Y) );

}

// -----------------------------
// Hide layer
// -----------------------------

function HideLayer(lName)
{

var skin;

skin=document.getElementById(lName);

if(skin==null) return;

skin.style.visibility="hidden";

}

// ---------------------------------------
// Create layer with content
// ---------------------------------------

function DestroyLayer(lName)
{

//alert(Data);

// Find control
var fnd = document.getElementById(lName);

// Check control
if(fnd!=null)
    {
    // Add new element to document
    document.body.removeChild(fnd);        
    }

}


// ---------------------------------------
// Create layer with content
// ---------------------------------------

function CreateLayer(lName, PosX, PosY, Data)
{

// Find control
var fnd = document.getElementById(lName);

// Check control
if(fnd!=null)
    {
    // Add new element to document
    document.body.removeChild(fnd);        
    }

// Create element
var s = document.createElement('div');

// Set id to new element
s.id = lName;

var wwidth=(window.innerWidth)?window.innerWidth:
    ((document.all)?document.body.offsetWidth:null);
var wheight=(window.innerHeight)?window.innerHeight:
    ((document.all)?document.body.offsetHeight:null);

// Vertical position
s.style.top = PosY/wheight*100 + '%';

// Horizontal position
s.style.left = PosX/wwidth*100 + '%';
s.style.position =  'absolute';
s.style.zIndex = 500;

// Set layer content
s.innerHTML = Data;

// Add new element to document
document.body.appendChild(s);        

if(s.clientWidth>0) 
    {

    // move window left
    if( parseInt(PosX) + parseInt(s.clientWidth) > document.body.clientWidth) 
        {
        var newx = document.body.clientWidth - s.clientWidth - 40;
        s.style.left = newx/wwidth*100 + '%';
        }
    }
    else
    {

    // move window left
    if( parseInt(PosX) + 4000 > document.body.clientWidth) 
        {
        var newx = document.body.clientWidth - 440;
        s.style.left = newx/wwidth*100 + '%';
        }

    }

//if(s.clientWidth);

}



// -----------------------------
// Set content to layer
// -----------------------------

function SetLayerContent(lName, Content)
{

var skin = document.getElementById(lName);

if(skin==null) return;
skin.innerHTML=Content;
}            

// -----------------------------
// Get layer position by X - local
// -----------------------------
function GetCY(cName)
{

// Get control
var Control = document.getElementById(cName);

// If have this control - return function result
if(Control==null) return(0);

var Tmp = Control;
var Y = 0;

while(Tmp)
    {
    Y += Tmp.offsetTop;
    Tmp = Tmp.offsetParent;
    }

return ( parseInt(Y) );
}

// ------------------------------------------
// Resize parent
// ------------------------------------------

function ResizeParentFrame(w,h, name) 
    {
    // Find frame
    var fr;

    if(name!='') fr = parent.document.getElementById(name); else fr = parent.document.getElementById("controller_iframe");

    if(fr==null) return;
    if( fr.offsetLeft + w > screen.availWidth)  fr.style.left = screen.availWidth  - w - 50;
    //if( fr.offsetTop + h  > screen.availHeight) fr.style.top  = screen.availHeight - h - 50;
    fr.style.width    = w;
    fr.style.height   = h;
    }

// -----------------------------
// Get layer position by Y
// -----------------------------
function GetCX(cName)
{

// Get control
var Control = document.getElementById(cName);

// If have this control - return function result
if(Control==null) return(0);

var Tmp = Control;
var X = 0;

while(Tmp)
    {
    X += Tmp.offsetLeft;
    Tmp = Tmp.offsetParent;
    }

return ( parseInt(X) );

}
// ---------------------------------------
// Show controller in iframe
// ---------------------------------------

function HideFrame()
{

var s = parent.document.getElementById('background_content');
if (s) 
    {
    s.style.display='none';
    s=null;
    }

var s = parent.document.getElementById('background_container');
if (s) 
    {
    s.style.display='none';
    s=null;
    }

}

