//Misc functions

function chfr(id){
 ll=top.document.frames.frmset.cols;
 top.document.frames.frmset.cols=(ll=="0,*")?"120,*":"0,*";
 document.images.ccal.src=(ll=="0,*")?"images/bt/c2.gif":"images/bt/c1.gif";
}

function getelem(id){
 var elem=(ns)?document.getElementById(id):document.all[id]
 return elem
}

function hsh(id){
 currElement = getelem(id);
 currElement.style.display=(currElement.style.display=="none")?"block":"none";
 return (currElement.style.display=="none")?false:true;
}


function getpos(typ, which){
  if (ns) return (typ=="x")?document.layers[which].pageX:document.layers[which].pageY;
  else {
    var elem = (nnn)? document.getElementById(which) : document.all[which];
    var pos = (typ=="x")?elem.offsetLeft:elem.offsetTop;
    while (elem.offsetParent != null){
      elem = elem.offsetParent;
      pos += (typ=="x")?elem.offsetLeft:elem.offsetTop;
      if (elem.tagName == 'BODY') break;
    } return pos;
  }
}


function check(which){
 var frm=eval('document.forms.'+which);
 var la=0
 for (var e = 0; e < frm.elements.length; e++){
   var el = frm.elements[e];
   if (el.tc){
    if (!la && el.tc=='email'  && (el.value.indexOf("@")<0 || el.value.indexOf(".")<1)){ alert('Неверный email'); la=1; }
    if (!la && el.tc=='пароль') var password1=el.value;
    if (!la && el.tc=='пароль2' && password1!=el.value){ alert('Пароли не совпадают'); la=1; }
    if (!la && el.value==""){
      alert('Пустое поле "'+el.tc+'"');
      la=1;
    }
   }
 }
 if (!la) frm.submit();
}


function got_it(lr, fr){
//   alert(lr);
   llr=getelem(lr);
   llr.innerHTML = document.frames[fr].document.body.innerHTML;
   document.frames[fr].document.body.innerHTML='';

   frst=getelem('first');
   scnd=getelem('second');
   frst.style.display=(lr=='scf')?"none":"block";
   scnd.style.display=(lr!='scf')?"none":"block";
}

//Calendar
var target;
var target2;
var target3;

function showcal(x, y, targ, targ2, targ3){
  if (targ!=target) calendar.style.display='none';
  calendar.style.left=x;
  calendar.style.top=y;
  if (calendar.style.display=='none'){
    target = targ;
    target2 = targ2;
    target3 = targ3;
    calendar.style.display='block';
    document.frames.calendar_frame.change_selects();
  };
  else calendar.style.display='none';
};

//Generic windows stuff

var ie=document.all
var ns=document.getElementById&&!document.all


var z,x,y
var maxz=50;
var elemnum=1;
var clk=false;

function move(e){
 if (clk){
  z.style.left=ns? temp1+e.clientX-x: temp1+event.clientX-x
  z.style.top=ns? temp2+e.clientY-y : temp2+event.clientY-y
 }
  return false
}


function drags(e){
 if (!ie&&!ns) return
 var firedobj=ns? e.target : event.srcElement
 var topelement=ns? "HTML" : "BODY"

 while (firedobj.tagName!=topelement&&firedobj.className!="drag"&&firedobj.className!=="resize"){
  firedobj=ns? firedobj.parentNode : firedobj.parentElement
 }

 if (firedobj.className=="drag"||firedobj.className=="resize"){

  clk=true;
  if (firedobj.className=="drag"){
    vv=getelem(firedobj.par);
    maxz++
    firedobj.style.zIndex=maxz+1;
    firedobj.style.border='1px dotted black'
    aa=getelem("fr"+firedobj.frnum);
    taa=getelem("ft"+firedobj.frnum)
    if (taa.style.display=='block') firedobj.style.height=parseInt(aa.style.height)+47;
    firedobj.style.width=parseInt(firedobj.style.width)+44;
    vv.style.zIndex=maxz
  }
  else{
   bb=getelem("im"+firedobj.frnum);
   temp3=parseInt(aa.style.width+0)
   temp4=parseInt(aa.style.height+0)
  }

  document.onmousemove=move

  x=ns? e.clientX: event.clientX
  y=ns? e.clientY: event.clientY

  z=firedobj
  temp1=parseInt(z.style.left+0)
  temp2=parseInt(z.style.top+0)
  return false
 }
}

function inline(num){
 fti=getelem('ft'+num)
 imi=getelem('im'+num)
 fri=getelem('fr'+num)
 pix=getelem('lpic'+num)

 if (fti.style.display=='none'){
   fti.style.display='block';
   imi.style.width=parseInt(fri.style.width+0)-34;
 }
 else{
  fti.style.display='none';
  imi.style.width=60;
 }
}

function upp(){
 if (clk){
 if (z.className=="drag"){
  vv=getelem(z.par);
  vv.style.top=z.style.top;
  vv.style.left=z.style.left;
  clk=false
  z.style.border='0px dotted black';
  z.style.height=20;
  z.style.width=parseInt(z.style.width)-44;
 }
 if (z.className=="resize"){
    chx=temp3+event.clientX-x;
    chy=temp4+event.clientY-y;
    if (chx<0) chx=0;
    if (chy<0) chy=0;
    aa.style.width=""+chx+"px";
    aa.style.height=""+chy+"px";
    bb.style.width=""+(chx-35)+"px";
 }
 }
}

function createwindow(urla, title, width, height, sx, sy, color, background, num, toreload){
 if (!num) num=elemnum
 tmp=getelem("toppar"+num);
 if (!tmp){
      va=document.createElement('DIV');
      va.id='toppar'+num
      document.body.appendChild(va);
 }
 else{
   va=tmp;
   if (toreload!='0'){
     document.frames['fr'+num].document.location=urla;
 //    alert(vafr.src);
   }
 }
 if (va.innerHTML==''){
 va.innerHTML=''+
'<div id="m'+num+'" style="position: absolute; border: 1px solid black;left: '+sx+'; top: '+sy+'">'+
'  <table border=1 cellpadding=0 cellspacing=0>'+
'    <tr><td>'+
'         <table width=100% style="font-family: arial, helvetica; font-size: 8pt; color: '+color+'; background='+background+'">'+
'          <td width=100%>'+title+'</td>'+
'          <td><img style="cursor: hand;" onclick="inline('+num+')" src=images/inline.gif></td>'+
'          <td><img style="cursor: hand;" id="lpic'+num+'" onclick="deletewindow('+num+')" src=images/close.gif align=absmiddle></td>'+
'         </table>'+
'       </td>'+
'    </tr>'+
'    <tr id="ft'+num+'" style="display:block"><td>'+
'      <table width=100%>'+
'       <tr><td><iframe src="'+urla+'" frameborder="NO" id="fr'+num+'" style="width:'+width+'; height:'+height+'; border: 1px solid black"></iframe></td></tr>'+
'       <tr style="display: block" style="font-family: arial, helvetica; font-size: 8pt; color: '+color+'; background='+background+'"><td align=right><img style="cursor: hand;" src="images/resize.gif" border=0 class=resize frnum="'+num+'"></td></tr>'+
'      </table>'+
'    </td></tr>'+
'  </table>'+
'</div>'+
'<img id=im'+num+' frnum="'+num+'" par=m'+num+' class="drag" src=images/empty.gif style="width: '+(width-34)+'; height=20; cursor: hand; border: 0; position: absolute;left: '+sx+'; top: '+sy+'">';
 }
 else{
  tmp=getelem("m"+num);
  tmp2=getelem("im"+num);
  tmp.style.zIndex=maxz+1;tmp2.style.zIndex=maxz+2;maxz++
 }

 elemnum++
}

function deletewindow(num){
 var va=getelem('toppar'+num)
 va.innerHTML='';
}

document.onmousedown=drags
document.onmouseup=upp;

//Client-server part