var OnloadActions	=	new Array();
var AdminEntries	=	new Array();

var CurrentsParams	=	new Array();
var ID		=	0;

var InitAJAX	=	function(){
    var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
    if (window.ActiveXObject){
	for(var i=0; i<activexmodes.length; i++){
	  try{return new ActiveXObject(activexmodes[i])}catch(e){}
	}
    }
    else if (window.XMLHttpRequest)
    {
      return new XMLHttpRequest();
    }
    else
    {
      return false;
    }
  }
  var Ajax		=	InitAJAX();

function HoverDiv(Width,Height,fix)
{

  if(fix){document.body.style.overflow='hidden';}

  var Show =	function(Width,Height){

    var MyDim	=	document.createElement('div');
    
    MyDim.setAttribute('id','dim');
    MyDim.style.width		=	(document.documentElement.scrollWidth || document.body.scrollWidth)+'px';
    MyDim.style.height		=	(document.documentElement.scrollHeight || document.body.scrollHeight)+'px';
    MyDim.style.top		=	'0px';//(document.documentElement.scrollTop || document.body.scrollTop)+'px';
    MyDim.style.left		=	'0px';//(document.documentElement.scrollLeft || document.body.scrollLeft)+'px';

    document.body.appendChild(MyDim);

    var MyDiv =	document.createElement('div');

     MyDiv.setAttribute('id','admindiv_');
     MyDiv.style.top		=	(document.documentElement.scrollTop || document.body.scrollTop)+100+'px';

    document.body.appendChild(MyDiv); 

    var MsgDiv		=	document.createElement('div');
    MsgDiv.setAttribute('id','msg');

    MyDiv.appendChild(MsgDiv);

    var MySubDiv	=	document.createElement('div');
    MySubDiv.setAttribute('id','admindiv');



    document.getElementById('admindiv_').appendChild(MySubDiv);
    document.getElementById('dim').onclick		=	function(){Close();}
    document.getElementById('admindiv_').onclick	=	function(){}
  }

  var Close =	function()
  {
    document.body.style.overflow='auto';
    document.body.removeChild(document.getElementById('dim'));
    document.body.removeChild(document.getElementById('admindiv_'));
    window.location.replace(document.location.href);
  }

  Show(Width,Height);
}

function GenerateAndSendForm(fields_Values,action)
{
  var myform = document.createElement("form");
  myform.action = action;
  myform.method = 'post';

  for(index in fields_Values)
  {
    var myinput			=	document.createElement("input");
    myinput.style.height	=	'0';
    myinput.style.width		=	'0';
    myinput.style.visibility	=	'hidden';

    myinput.name 		=	index;
    myinput.value		=	fields_Values[index];
    myform.appendChild(myinput);
  }

document.body.appendChild(myform);
myform.submit();

}

function Login()
{
 HoverDiv('auto','auto',true);
 Ajax.open("GET",'/admin/panel.php?rand='+Math.random(),true);
  Ajax.send(null);
  Ajax.onreadystatechange	=	function(){
      if(Ajax.readyState==4){
	 document.getElementById("admindiv").innerHTML	=	Ajax.responseText;
      }
    }
}
function Logout()
{
  var fields = new Array();
  fields['logout']	=	'true';
  GenerateAndSendForm(fields,"");
}

function Message(Msg,Img,Type)
{
Img	=	(typeof(Img)!='undefined')?Img:'';
var MsgDiv		=	document.getElementById('msg');

  switch(Type)
  {
    case 'ERROR':
      MsgDiv.style.border	=	'1px dotted red';
      MsgDiv.style.color	=	'red';
      Img			=	(Img=='')?'/admin/images/error.png':Img;


    break;
    case 'WARN':
      MsgDiv.style.border	=	'1px dotted darkyellow';
      MsgDiv.style.color	=	'darkyellow';
      Img			=	(Img=='')?'/admin/images/warn.png':Img;


    break;
    case 'INFO':
      MsgDiv.style.border	=	'1px dotted darkblue';
      MsgDiv.style.color	=	'darkblue';
      Img			=	(Img=='')?'/admin/images/info.png':Img;


    break;
    case 'OKAY':
      MsgDiv.style.border	=	'1px dotted green';
      MsgDiv.style.color	=	'green';
      Img			=	(Img=='')?'/admin/images/okay.png':Img;

    break;
  }
  var ImgTag	=	document.createElement('img');
  with(ImgTag)
  {
    src				=	Img;
    style.cssFloat		=	'right';
    style.styleFloat		=	'right';
  }
  var ClearDiv			=	document.createElement('div');
  ClearDiv.style.clear		=	'both';
  MsgDiv.innerHTML		=	Msg;
  MsgDiv.appendChild(ImgTag);
  MsgDiv.appendChild(ClearDiv);
  MsgDiv.style.display		=	'block';
}

function ClearMessage()
{
  var MsgDiv		=	document.getElementById('msg');
  MsgDiv.innerHTML	=	'';
  MsgDiv.style.border	=	0;
  MsgDiv.style.display	=	'none';  
}


function AdminTrigger(id,init,action,fromframe)
{
  var content;

  if(typeof(fromframe)!='undefined')
  {
	 content	=	fromframe;
	 document.getElementById("admindiv").innerHTML	=	content['html'];
	 if(typeof(content['js']!='undefined') && content['js']!=''){eval(content['js'])}
	 if(content['msgText']!=''){Message(content['msgText'],content['msgImage'],content['msgType']);}
  }
  else
  {

 ID=id;
 if(init){HoverDiv(1000,'auto',false);}

 Ajax.open("POST",'/admin/panel.php?rand='+Math.random()+((typeof(action)!='undefined' && action!='')?'&action='+action:''),true);
 Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
  Ajax.send('json='+JSON.stringify(AdminEntries[id])+'');
  Ajax.onreadystatechange	=	function(){
      if(Ajax.readyState==4){

	 content							=	eval('('+Ajax.responseText+')');

	 document.getElementById("admindiv").innerHTML			=	content['html'];
	 if(typeof(content['js']!='undefined') && content['js']!=''){eval(content['js']);}
	 ClearMessage();
	 if(content['msgText']!=''){Message(content['msgText'],content['msgImage'],content['msgType']);}


	 var Width							=	document.getElementById("admindiv_").offsetWidth-2;

	 document.getElementById("admindiv_").style.left		=	((document.body.clientWidth || window.innerWidth)/2 -(Width/2))+'px';
 	 document.getElementById("admindiv_").style.width		=	document.getElementById("admindiv_").offsetWidth+'px';

      }
    }
   }

}

window.onload	=	function(){for(index in OnloadActions){eval('('+OnloadActions[index]+')');}}
