var popOpen=0;
var zIndex=100;

function placeCenter(dv)
{
	var lr=layer(dv.id);	
	var lX=parseInt(getWindowWidth()/2)-lr.getWidth()/2+getScrollX();
	var lY=parseInt(getWindowHeight()/2)-lr.getHeight()/2+getScrollY();
	if(lX<0) lX=0;
	if(lY<0) lY=0;
	lr.moveTo(lX,lY);
}

function iPopup(idName,url,w,h)
{

	var holderDiv=document.getElementById('holder_'+idName);

	if(!holderDiv)
	{

// 	        var el = $('myBody');
//	        el.set('opacity',0);	

		popOpen=popOpen+1;	
		zIndex++;
		holderDiv = document.createElement("div");
		holderDiv.id='holder_'+idName;
		holderDiv.style.position="absolute";
		holderDiv.style.zIndex=zIndex;
		document.body.insertBefore(holderDiv,document.body.firstChild);
		holderDiv.innerHTML='<div id="'+idName+'" style="background:#ffffff;"></div>';
	}
	


//	var popupDiv=document.getElementById(idName);
//	if(!popupDiv)
//	{
//		popOpen=popOpen+1;	
//		zIndex++;
//		popupDiv = document.createElement("div");
//		popupDiv.id=idName;
//		popupDiv.style.position="absolute";
//		popupDiv.style.backgroundColor="#ffffff";
//		popupDiv.style.zIndex=zIndex;
//		document.body.insertBefore(popupDiv,document.body.firstChild);
//		popupDiv.innerHTML="&nbsp;";
//	}


//	if(w) {w=w+16; popupDiv.style.width=parseInt(w);}
//	if(h) {h=h+16; popupDiv.style.height=parseInt(h);}
	if(w) {holderDiv.style.width=parseInt(w+16);}
	if(h) {holderDiv.style.height=parseInt(h+16);}

	placeCenter(holderDiv);
//	showLoading(holderDiv.id);

	var req = new JsHttpRequest();
	req.onreadystatechange = function()
		{
		if (req.readyState == 4)
			{
//				hideLoading(holderDiv.id);

				holderDiv.style.width='';
				holderDiv.style.height='';

				if(req.responseText)
				{
					var ppt='';
					var ppt='<table border=0 cellpadding=10 cellspacing=0 bgcolor=#333333 style="border:10px solid #333333;"><tr><td valign=top bgcolor=#444444><div id="'+idName+'">';
					ppt=ppt+req.responseText;
					ppt=ppt+'</div></td></tr></table>';
					holderDiv.innerHTML=ppt;
				}

				var lr=layer(holderDiv.id);				
				holderDiv.style.width=lr.getWidth();
				holderDiv.style.height=lr.getHeight();
				placeCenter(holderDiv);


				if(req.responseJS)
				{

					if(req.responseJS.newWidth&&req.responseJS.newHeight)
					{
						holderDiv.style.width=parseInt(req.responseJS.newWidth)+16;
						holderDiv.style.height=parseInt(req.responseJS.newHeight)+16;
						placeCenter(holderDiv);
					}

					if(req.responseJS.commands)
					{
						for(var k=0;k<req.responseJS.commands.length;k++)
						{
							eval(req.responseJS.commands[k]+'("'+req.responseJS.params[k]+'")');
						}
					}

				}

			}
		}
	req.open(null, url, true);
	req.send();

}

function hidePopup(id)
{

	   popOpen=popOpen-1;
	   zIndex--;
	   if(popOpen==0)
	   {
//	   var el = $('myBody');
//	   el.set('opacity',1);		
//	   if(isMSIE) document.getElementById('myBody').style.height='100%';
	   }
   var Node = document.getElementById('holder_'+id);
   Node.parentNode.removeChild(Node);
}



function iPopup2(id,url,wd,ht)
{

  ndV=document.getElementById('r_'+id);
  if(!ndV)
  {
   ndV=document.createElement("div");
   ndV.innerHTML = "";
   ndV.id='r_'+id;
   ndV.style.position='absolute';
   ndV.style.backgroundImage='url("/img/dbb.gif")';
   ndV.style.zIndex=0;
  }



  newDiv=document.getElementById(id) ;
  if(!newDiv)
  {
  newDiv = document.createElement("div");
  newDiv.innerHTML = "";
  newDiv.id=id;
  newDiv.style.position='absolute';

//  newDiv.style.display='none';
  // add the newly created element and it's content into the DOM
  my_div = document.getElementById("pp");
  document.body.insertBefore(ndV,my_div);
  document.body.insertBefore(newDiv, my_div);

  } 



    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {


	document.getElementById('r_'+id).style.left=0;
	document.getElementById('r_'+id).style.top=0;
	document.getElementById('r_'+id).style.width='100%';
	document.getElementById('r_'+id).style.height=getScrollY()+getDocumentHeight()+ht/2;

	newDiv.style.width='';
				holderDiv.style.height='';



	var txt='<div style="position:absolute; width:'+(wd+5)+'px; height:'+(ht+8)+'px; border:10px solid #333333; background:#444444; padding:0px;">';
	txt+=req.responseText;
	txt+='</div>';

//  var txt='<table width='+(wd+14)+' height='+(ht+34)+' cellpadding=0 cellspacing=0 border=0>';
//  txt+='<tr><td width=7 height=7><img src="/img/c1.gif" width=7 height=7></td><td bgcolor=#b1c2ca width='+wd+'></td><td width=7><img src="/img/c2.gif" width=7 height=7></td></tr>';
//  txt+='<tr><td bgcolor=#b1c2ca></td><td onmousedown="dragStart(event,\''+id+'\');" height=20 width='+wd+' bgcolor=#b1c2ca align=left valign=top><table width='+wd+' cellpadding=0 cellspacing=0><tr><td><b>'+title+'</b></td><td align=right><a href="#" onclick="hidePopup(\''+id+'\');"><img src="/img/icons/close.gif" border=0></a></td></tr></table></td><td bgcolor=#b1c2ca></td></tr>';
//  txt+='<tr><td bgcolor=#b1c2ca></td><td width='+wd+' bgcolor=#ffffff align=left valign=top><div id="'+id+'_content" style="width:'+wd+'px; height:'+ht+'px; position:absolute; overflow:auto;">'+req.responseText+'</div></td><td bgcolor=#b1c2ca></td></tr>';
//  txt+='<tr><td width=7 height=7><img src="/img/c3.gif" width=7 height=7></td><td bgcolor=#b1c2ca></td><td width=7><img src="/img/c4.gif" width=7 height=7></td></tr>';
//  txt+='</table>';
  newDiv.innerHTML=txt;

	lr=layer(id);


	lft=getScrollX()+(getWindowWidth(false)/2)-(wd+14)/2;
	tp=getScrollY()+(getWindowHeight(false)/2)-(ht+34)/2;


	if(lft<0) lft=0;
	if(tp<0) tp=0;
	lr.moveTo(lft,tp);


 

        }
    }
    req.open(null, url, true);
    req.send();
}

function hidePopup2(id)
{
   var Node = document.getElementById(id);
   Node.parentNode.removeChild(Node);

   var Nde = document.getElementById('r_'+id);
   Nde.parentNode.removeChild(Nde);

}

function sendForm(frm,ly)
{
    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {

		if(!req.responseJS&&!req.responseText) {alert("Ответа не получено :("); frm.subm.disabled=false;}
		else
		{
		if(req.responseJS)
			{
				for(k=0;k<frm.length;k++)
				if(frm[k].type=='text'||frm[k].type=='textarea') frm[k].className='input';

				if(req.responseJS.forms)
				{

					var focusset=false;
					for(k=0;k<frm.length;k++)
					{
						for(i=0;i<req.responseJS.forms.length;i++)
						if(frm[k].name==req.responseJS.forms[i]&&(frm[k].type=='text'||frm[k].type=='textarea')) {frm[k].className='input_error'; if(!focusset) {frm[k].focus(); focusset=true;}}

					}
				}
				alert(req.responseJS.error);
				frm.subm.disabled=false;

			} else if(req.responseText) document.getElementById(ly).innerHTML = req.responseText;
		}
        }
    }

    req.open(null, '/__.php', true);
    req.send({'form':frm});

}