//Настройки на броузер клиента
//============================
var brouser="";
var closetag="</div>";
var closevtag="</div>";

// setting trim function to String objects
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

// Mozilla
if(document.getElementById) {
    brouser="Mz";
}
// Microsoft IE
if(document.all) {
    brouser="IE";
}
// Netscape Navigator + Сибкон Коммуникатор
if(document.layers) {
    brouser="NN";
    closetag="</layer></layer>";
    closevtag="</layer></ilayer>";
}
// Опера версий 6.0 и 6.5
if(window.opera)brouser="Op";
//alert(brouser);
//==========================================
//Настройка операций JS для разных броузеров
// ======== Конструкторы тегов ========
function opentag(cid,cleft,ctop,cwidth,zind){
    if(!zind)zind=10;
    var opent;
    if(document.layers){
        opent ="<layer id=\"cid"+cid+"\" width=\""+cwidth+"px\" height=\""+cheight+
        "px\" bgcolor=\""+bcolor+"\" visibility=\"hidden\" position=\"absolute\" top=\""+ctop+
        "\" left=\""+cleft+"\">"+"<layer id=\"did"+cid+"\" left=\"1px\" top=\"1px\" width=\""+
        (cwidth-2)+"px\" height=\""+(cheight-2)+"px\" bgcolor=\""+pcolor+"\" onmouseover=\"openbl("+
        cid+",1)\" "+"onmouseout=\"closebl("+cid+",1)\">";
    } else {
        opent ="<div id=\"cid"+cid+"\" style=\"position:absolute;left:"+cleft+
        "px;top:"+ctop+"px;border:1px solid "+bcolor+";width:"+cwidth+"px;height:"+
        cheight+"px;padding-bottom:0px;font-size:"+fsizepx+"px;font-family:"+
        fface+";font-style:normal;font-weight:normal;background-color:"+pcolor+
        ";visibility:hidden;z-index:"+zind+";\" "+"onmouseover=\"openbl("+cid+",1)\" "+
        "onmouseout=\"closebl("+cid+",1)\">" ;
    }
    return opent;
}
function openvtag(cid,cwidth){
    var opent;
    if(document.layers){
        opent ="<ilayer id=\"cid"+cid+"\" width=\""+cwidth+"px\" height=\""+cheight+
        "px\" bgcolor=\""+bcolorm+"\">"+"<layer id=\"did"+cid+"\" left=\"1px\" "+
        "top=\"1px\" width=\""+(cwidth-2)+"px\" height=\""+(cheight-2)+"px\" bgcolor=\""+
        pcolor+"\" onmouseover=\"openbl("+cid+",0)\" "+"onmouseout=\"closebl("+cid+",1)\">";
    } else {
        /*
        opent ="<div id=\"cid"+cid+"\" style=\"width:"+cwidth+"px;height:"+
        cheight+"px;padding-bottom:0px;border:1px solid "+bcolorm+";font-size:"+fsizepx+
        "px;font-family:"+fface+";font-style:normal;font-weight:normal;backgro"+
        "und-color:"+pcolor+";\" "+"onmouseover=\"openbl("+cid+",0)\" "+
        "onmouseout=\"closebl("+cid+",1)\">" ;*/
        opent ="<div id=\"cid"+cid+"\" onmouseover=\"openbl("+cid+",0)\" "+
        "onmouseout=\"closebl("+cid+",1)\">" ;
    }
    return opent;
}
//Поиск объекта по его идентификатору
function getobj(idobj){
    var obj;
    switch (brouser){
    case "IE":
        obj=document.all[idobj];
        break;
    case "Op":
        obj=document.all[idobj];
        break;
    case "NN":
        obj=document.layers[idobj];
        break;
    case "Mz":
        obj=document.getElementById(idobj);
    }
    return obj;
}
//изменение атрибутов тега
function chattr(idn,cleft,ctop,cwidth){
    var obj=getobj("cid"+idn);
    if(!obj)return false;
    switch (brouser){
    case "IE":
        obj.style.left=cleft;
        obj.style.top=ctop;
        break;
    case "Op":
        obj.style.left=cleft;
        obj.style.top=ctop;
        break;
    case "NN":
        obj.left=cleft;
        obj.top=ctop;
        break;
    case "Mz":
        obj.style.left=cleft;
        obj.style.top=ctop;
        break;
    }
}

// Расчет координаты X элемента
function get_x(idn){
    var tx = 0;
    var obj=getobj("cid"+idn);
    switch (brouser){
    case "Mz":
        if (obj == null) return false;
        while(obj.tagName != "BODY"){
            tx = tx + obj.offsetLeft;
            obj = obj.parentNode;
        }
        tx=tx+1;
        break;
    case "IE":
        if (obj == null) return false;
        while(obj.tagName != "BODY"){
            tx = tx + obj.offsetLeft;
            obj = obj.parentElement;
        }
        tx=tx+1;
        break;
    case "NN":
        tx =  obj.pageX+1;
        break;
    case "Op":
        if (obj == null) return false;
        while(obj.tagName != "BODY"){
            tx = tx + obj.offsetLeft;
            obj = obj.parentElement;
        }
        tx = tx+1;
    }
    return tx;
}
// Расчет координаты Y элемента
function get_y(idn){
    var ty = 0;
    var obj=getobj("cid"+idn);
    switch (brouser){
    case "Mz":
        if (obj == null) return false;
        while(obj.tagName != "BODY"){
            if(obj.tagName!="TD") ty = ty + obj.offsetTop;
            obj = obj.parentNode;
        }
        ty=ty+cheight+4;
        break;
    case "IE":
        if (obj == null) return false;
        while(obj.tagName != "BODY"){
            if(obj.tagName!="TD") ty = ty + obj.offsetTop;
            obj = obj.parentElement;
        }
        ty=ty+cheight+2;
        break;
    case "NN":
        ty = obj.pageY+cheight+2;
        break;
    case "Op":
        if (obj == null) return false;
        while(obj.tagName != "BODY"){
            if(obj.tagName!="TD") ty = ty + obj.offsetTop;
            obj = obj.parentElement;
        }
        ty = ty + cheight + 4;
    }
    return ty;
}
// Смена цвета элемента
function changeColor(idbl,vColor){
    var obj=getobj("cid"+idbl);
    switch (brouser){
    case "Mz":
        obj.style.backgroundColor=vColor;
        break;
    case "IE":
        obj.style.backgroundColor=vColor;
        break;
    case "NN":
        obj.layers["did"+idbl].bgColor=vColor;
        break;
    case "Op":
        obj.style.backgroundColor=vColor;
}   }
// Показать элемент
function visibleBlock(idbl){
    var obj=getobj("cid"+idbl);
    switch (brouser){
    case "IE":
        obj.style.visibility="visible";
        break;
    case "Op":
        obj.style.visibility="visible";
        break;
    case "NN":
        obj.visibility="visible";
        break;
    case "Mz":
        obj.style.visibility="visible";
}   }
// Спрятать элемент
function hideBlock(idbl){
    var obj=getobj("cid"+idbl);
    switch (brouser){
    case "IE":
        obj.style.visibility="hidden";
        break;
    case "Op":
        obj.style.visibility="hidden";
        break;
    case "NN":
        obj.visibility="hide";
        break;
    case "Mz":
        obj.style.visibility="hidden";
}   }
//поправка для расчета X
function XCorrect(){
    var correction;
    switch (brouser){
    case "IE":
        correction=0;
        break;
    case "Op":
        correction=2;
        break;
    case "NN":
        correction=0;
        break;
    case "Mz":
        correction=2;
        break;
    }
    return correction;
}
//поправка для расчета Y
function YCorrect(){
    var correction;
    switch (brouser){
    case "IE":
        correction=0;
        break;
    case "Op":
        correction=2;
        break;
    case "NN":
        correction=0;
        break;
    case "Mz":
        correction=2;
        break;
    }
    return correction;
}

function openWin(URL, win_width, win_height, description)
{
  features = "width=" + win_width + ",height=" + win_height + ",status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=1"

  image = '<img src = "' + URL +  '">'
  title = "<html><head><title>" + description

  myWin= open( "", "View", features )

  myWin.document.open();

  myWin.document.write(title);
  myWin.document.write('</title></head><body style="margin:0">');
  myWin.document.write(image);
  myWin.document.write("</body></html>");

  myWin.document.close();
}

/*
    form checking and controls tips support
*/
function set_wrong(el, _wrong)
{
   el.style.color = _wrong ? "red" : "black" ;

   return _wrong ;
}
function set_tips(el, tips)
{
   el.style.color = tips!='' ? "#7E7E7E" : "black" ;
   el.value = tips ; 
}

function do_check_form(c_names, tips_map)
{
  var wrong_fields = 0 ;

  try {
      for(i in c_names) 
      {
        c_name = c_names[i] ;
        if (set_wrong(getobj(c_name+"_label"), 
                      get_value_no_tips(getobj(c_name), tips_map).trim()==""))
          ++wrong_fields ;
      }

      if (wrong_fields != 0)
        alert("Не все необходимые поля были заполнены.") ;
  } 
  catch(err) {
      alert("Error checking form: " + err.description) ;
      return false ;
  }

  return wrong_fields == 0 ;
}



function get_value_no_tips(obj, tips_map)
{
    if ((obj.name in tips_map) && obj.value==tips_map[obj.name])
        return ""
    return obj.value ;
}
function init_tips(obj, tips_map)
{
    if (obj.name in tips_map && 
        (obj.value=='' || obj.value==tips_map[obj.name]))
        set_tips(obj, tips_map[obj.name]) ; 
}
function onblur_tips(obj, tips_map)
{
    if (obj.value=='') 
        init_tips(obj, tips_map) ;
}
function onfocus_tips(obj, tips_map)
{
    if ((obj.name in tips_map) && obj.value==tips_map[obj.name])
        set_tips(obj, '') ; 
}
function on_form_reset(tips_map)
{                                       
    // we should make this after form be reset
    setTimeout('on_form_loaded(tips_array);', 50)
}
function on_form_loaded(tips_map)
{
    for(obj_name in tips_map)
        init_tips(getobj(obj_name), tips_map) ;
}
