//changes visibility of div
function showHide(div,flag,on, img,open,close){
    if(!flag) flag = 0;
    if(!document.getElementById(div)) return;
    s = document.getElementById(div).style;
    if(on){
        if(on!=1) res = 0;
        else res = 1;
    }
    else{
        if(flag) vis = (s.display != 'none');
        else vis = (s.display == 'block');
        res = vis ? 0 : 1;
    }
    s.display = res ? 'block' : 'none';
    
    //swap img
    image = document.getElementById(img);
    if(image){
        if(image.src && open && close){
            image.src = res ? open : close;
        }
    }
    
    return ((s.display=='block') ? 1 : 0);
}

//changes expansion of div
function expand(div,flag,on,hei){
    if(!flag) flag = 0;
    if(!hei) hei = 32;
    if(!document.getElementById(div)) return;
    s = document.getElementById(div).style;
    if(flag==1) vis = (s.overflow != 'auto');
    else vis = (s.overflow == 'visible');
        if(vis){
            s.overflow = 'auto';
            s.height = hei;
        }
        else{
            s.overflow = 'visible';
            s.height = 'auto';
        }
    if(on){
        if(on!=1) s.overflow = 'auto';
        else s.overflow = 'visible';
    }
}

//-------------------------

//set current date in date controls with prefix pre in form frm
function setToday(frm,pre){
    //alert(frm);
    //return;
    var d = new Date();
    var y = d.getYear();
    if(y<1900) y+=1900;
    mins = d.getMinutes();
    minz = mins-mins%10;
    if(frm[pre+'_hour'])   frm[pre+'_hour'].options[d.getHours()].selected = true;
    if(frm[pre+'_minute']) frm[pre+'_minute'].value = minz;
    if(frm[pre+'_day'])    frm[pre+'_day'].options[d.getDate()].selected = true;
    if(frm[pre+'_month'])  frm[pre+'_month'].options[d.getMonth()+1].selected = true;
    if(frm[pre+'_year'])   frm[pre+'_year'].value = y;
    return true;
}

//open window with image
function openImg(url){
    w = window.open(url,'image','toolbar=0,status=0,top=10,left=10,width=100,height=100,scrollbars=0,resizable=1,menubar=0');
    w.focus();
    //w.moveTo(10,10);
    //w.resizeTo(screen.availWidth,screen.availHeight);
    return false;
}

//open window with help
function openColorWin(url,w,h){
    if(!w) w = 185;
    if(!h) h = 135;

    x = screen.width-500;
    y = 200;
    /*
    x = event ? event.clientX : 100;
    y = event ? event.clientY : 100;
    if(window.innerHeight){
        if(y+h>window.innerHeight) y -= h;
        if(x+w>window.innerWidth) x -= w;
    }
    */
    win = window.open(url,'help','toolbar=0,status=0,top='+y+',left='+x+',width='+w+',height='+h+',scrollbars=0,resizable=1,menubar=0');
    win.focus();
    return false;
}

function openDateWin(url,f,i){
    var w = 280;
    var h = 185;
    var x = Math.floor(screen.width)/200-100;
    var y = 200;
    var val =
        document.forms[f].elements[i+'_year'].value+'-'+
        document.forms[f].elements[i+'_month'].value+'-'+
        document.forms[f].elements[i+'_day'].value;
    url += '?form_name='+f+'&amp;input_name='+i+'&amp;dat='+val;
    win = window.open(url,'help','toolbar=0,status=0,top='+y+',left='+x+',width='+w+',height='+h+',scrollbars=0,resizable=1,menubar=0');
    win.focus();
    return false;
}

function openWork(url){
    var w = window.open(url,'work','toolbar=0,status=0,top=50,left=100,width=583,height=550,scrollbars=1,resizable=1,menubar=0');
    w.focus();
    return false;
}

function swapImg(target,file){
    if(img=document.getElementById(target)) img.src = file;
}

function selectColor(c,f,i){
        window.opener.document.forms[f].elements[i].value = c;
        window.opener.document.forms[f].elements[i].style.background = c;
        self.close();
}

function selectDate(c,f,i){
    window.opener.document.forms[f].elements[i+'_year'].value = c.substr(0,4);
    window.opener.document.forms[f].elements[i+'_month'].value = parseInt(c.substr(5,2),10);
    window.opener.document.forms[f].elements[i+'_day'].value = parseInt(c.substr(8,2),10);
    self.close();
}

function checkForm(f){
    var msg = '';
    if(f.name.value.length<1) msg += 'Введите Ваше имя!\n';
    if(f.email.value.length<1) msg += 'Введите Ваш e-mail!\n';
    if(f.txt.value.length<1) msg += 'Введите сообщение!\n';
    if(msg.length>0){
        alert(msg);
        return false;
    }
    else return true;
}

function focusValue(obj,from,to){
    if(obj.value==from) obj.value = to;
}

//------------ popup

    var IE = document.all?true:false
    var tempX = 0
    var tempY = 0
    var tipVis = 0;
    var tip;
    var tip2Vis = 0;
    var tip2;


function initPopup(){
    if (!IE) document.captureEvents(Event.MOUSEMOVE)
    document.onmousemove = getMouseXY;
    tip = document.getElementById('tip');
    tip2 = document.getElementById('tipoff');
}

function showTip(text){
    if(tipVis>0) return;
    if(tip){
        tip.style.display = 'block';
        tip.innerHTML = text;
//		alert(tip.style.left);
        tipVis = 1;
    }
}

function showTip2(text){
    if(tip2Vis>0) return;
    if(tip2){
        tipoff.style.display = 'block';
        tipoff.innerHTML = text;
//		alert(tip.style.left);
        tip2Vis = 1;
    }
}

function hideTip2(){
    if(tip2){
        tipoff.style.display = 'none';
        tip2Vis = 0;
    }
}

function hideTip(){
    if(tip){
        tip.style.display = 'none';
        tipVis = 0;
    }
}

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }
  if(tempX+155>document.body.clientWidth) tempX = document.body.clientWidth-155;
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  //document.Show.MouseX.value = tempX
  //document.Show.MouseY.value = tempY

  if(tip){
    tip.style.left = ''+(tempX-5)+'px';
    tip.style.top = ''+(tempY+15)+'px';
  }

  if(tip2){
    tipoff.style.left = ''+(tempX-5)+'px';
    tipoff.style.top = ''+(tempY+15)+'px';
  }


  return true
}

//------------ glossary

function insertTerm(n,t){
    r = opener.document.selection.createRange();
    if(r.htmlText.length>0) n = r.htmlText;
    r.pasteHTML('<span class="term" onmouseover="showTip(\''+t+'\');" onmouseout="hideTip();">'+n+'</span>');
    self.close();
    return false;
}

