function LoadXML(uri,method,post_str)
{
	var request = null;

	// MSXML 2
	if (!request)
	{
		try
		{
			request = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e){}
	}

	//MSXML 1
	if (!request)
	{
		try
		{
			request = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch (e){}
	}

	// Mozilla ? Opera 7.60
	if (!request)
	{
		try
		{
			request = new XMLHttpRequest();
		}
		catch (e){}
	}

	if (!request)
	{
		// ...
		return null;
	}
 
	//
	request.open(method, uri, false);	
	if (method=='GET') {
		request.send(null);
	} else {
		request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		request.send(post_str);
	}
//	if (request.responseXML.childNodes.length==0)
//		return null;//alert('xml...');
  
	//XML

	return request.responseText;
		
}
function block_click() {
	return false;
}
function check_key(e) {
	e = (e)?e:window.event;
	if(e.keyCode == '27')
	{
		p2 = window.top.document.getElementById("popup_div_id2")
		if(p2 != undefined) window.top.closePopup2();
			else window.top.closePopup();
	}
	return true;
}

function popup_form_submit(e) {
	e = (e)?e:window.event;
	form_obj = (e.target)?e.target:e.srcElement;
	page_popup = document.getElementById("admin_popup");

//	page_popup.innerHTML = LoadXML(url,'POST');
	var post_str = "";
	var url;
	form_children = new Array;
	form_children = form_obj.getElementsByTagName('input');
//	alert(form_obj.getElementsByTagName('input'));
	for (i=0;i<form_children.length;i++) {
		if (form_children[i].name == 'req_url') {
				url = form_children[i].value;
			} else	post_str += ((post_str == "")?"":"&")+form_children[i].name+"="+form_children[i].value;
	}
//	page_popup.innerHTML = url+"------"+post_str;
	page_popup.innerHTML = LoadXML(url,'POST',post_str);
	return false;
}

var popup_div,page_mask,page_content
	
function openPopup_common(url, width, height, scroll, arg_height_top_margin, arg_popup_div_id, arg_admin_mask_id, arg_admin_popup_id, arg_iframe_popup_id, arg_popup_level)
{

	//if "height" more then max "browserHeight" then "height" must be equal "browserHeight" and also enable scroll in popup
	var current_page_height = pageHeight();
	if ((height+arg_height_top_margin) > current_page_height)
	{
		height = current_page_height-arg_height_top_margin;
		scroll = true;
	}

	if (scroll==true)
		scroll='yes';
	else
		scroll='no';

	//For IE6 and lower jump to begin of page and disable scrolling
	isIE6 = (navigator.userAgent.indexOf('MSIE')>-1) ? true : false;
	if(isIE6 && scroll == 'yes' )
	{
		window.scroll(0,0);
		document.documentElement.style.overflow = "hidden";
		document.body.scroll='no';
	}

	var sels = document.getElementsByTagName('select');
	for (i=0; i<sels.length; i++)
	{
		sels[i].disabled = true;
	}

	if (document.getElementById("dhtmltooltip2")) hideddrivetip();

	var popup_div = document.createElement("div");
	popup_div.setAttribute("id", arg_popup_div_id);

	var page_mask = document.createElement("div");
	page_mask.setAttribute("id", arg_admin_mask_id);
	if(isIE6) {page_mask.style.position = 'absolute';}

	var page_popup = document.createElement("div");
	page_popup.setAttribute("id", arg_admin_popup_id);
	page_popup.style.width = width+'px';
	page_popup.style.height = height+'px';
	if(isIE6) {page_popup.style.position = 'absolute';}

	var page_frame = document.createElement("iframe");
	page_frame.setAttribute("id",arg_iframe_popup_id);
	page_frame.setAttribute("name",arg_iframe_popup_id);
	page_frame.setAttribute("width","100%");
	page_frame.setAttribute("height","100%");
	page_frame.setAttribute("frameBorder", "0");
	page_frame.setAttribute("src",url+((arg_popup_level==2)?"&popup2=true":""));
	page_frame.setAttribute("scrolling",scroll);

	page_popup.appendChild(page_frame);
	popup_div.appendChild(page_mask);
	popup_div.appendChild(page_popup);
	document.body.appendChild(popup_div);	

	setSizes();

	page_mask.oncontextmenu = block_click;
	window.document.onkeypress = check_key;
	window.onresize = setSizes;
}

function openPopup(url, x, y, width, height, scroll)
{
	openPopup_common(url, width, height, scroll, 5, "popup_div_id", "admin_mask", "admin_popup", "iframe_popup", 1);
}

function closePopup_common(reload, arg_popup_level, arg_popup_div_id, reload_only) 
{
	isIE6 = (navigator.userAgent.indexOf('MSIE 6.')>-1) ? true : false;
	if(isIE6)
	{
		document.documentElement.style.overflow = "auto";
	}

	var popup_div=document.getElementById(arg_popup_div_id);
	if (popup_div)
	{
		popup_div.parentNode.removeChild(popup_div);
	
		if(arg_popup_level == 1)
		{
			var page_content = document.getElementById("whole_page_content");	
			if (page_content)
			{
				page_content.style.overflow='';
				page_content.style.height = '';
				page_content.style.width = '';
				page_content.oncontextmenu = '';
			}
		}
		

		var sels = document.getElementsByTagName('select');
		for (i=0; i<sels.length; i++)
		{
			sels[i].disabled = false;
		}
	

		window.onresize = '';
		window.document.onkeypressed = '';
	

		if (reload=='yes')
		{
			if(arg_popup_level == 1)
			{
				window.top.location.reload();
			}
			if(arg_popup_level == 2)
			{
				if (reload_only == 'true')
				{
					var tmp_url = window.iframe_popup.document.getElementById('iframe_preview').src;
					window.iframe_popup.document.getElementById('iframe_preview').src = null;
					window.iframe_popup.document.getElementById('iframe_preview').src = tmp_url;
				}
				else
				{
					window.iframe_popup.location.reload();
				}
			}
		}
	}

}

function closePopup(reload) 
{
	closePopup_common(reload, 1, "popup_div_id", ""); 
}

function pageWidth() 
{
	return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;
}

function pageHeight() 
{
	return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
}

function setSizes_common(arg_page_mask_id, arg_page_popup_id, arg_height_top_margin)
{
	var page_mask = document.getElementById(arg_page_mask_id);
	var page_popup = document.getElementById(arg_page_popup_id);

	if (page_mask)
	{
		page_mask.style.height = pageHeight()+"px";
		page_mask.style.width = pageWidth()+"px";
	}
	if (page_popup)
	{
		var top = parseInt((pageHeight()-parseInt(page_popup.style.height))/2);
		var left = parseInt((pageWidth()-parseInt(page_popup.style.width))/2);
		if (top<arg_height_top_margin) top = arg_height_top_margin;
		if (left<arg_height_top_margin) left = arg_height_top_margin;
		page_popup.style.top = top+"px";
		page_popup.style.left = left+"px";
	}
}

function setSizes()
{
	var page_content = document.getElementById("whole_page_content");
	if (page_content)
	{
		page_content.style.height = pageHeight()+'px';
		page_content.style.width = pageWidth()+'px';
	}
	
	setSizes_common("admin_mask", "admin_popup", 5);
	setSizes_common("admin_mask2", "admin_popup2", 20);


}

function openPopup2(url, width, height, scroll)
{
	if (scroll==true)
		scroll='yes';
	else
		scroll='no';

	sels = document.getElementsByTagName('select');
	for (i=0; i<sels.length; i++)
	{
		sels[i].disabled = true;
	}


	popup_div = document.createElement("div");
	popup_div.setAttribute("id","popup_div_id2");
	popup_div.innerHTML = '<div id="admin_mask2">&nbsp;</div><div id="admin_popup2" style="width:'+width+'px;height:'+height+'px;"><iframe id="iframe_popup2" name="iframe_popup2" frameborder=0 width="100%" height="100%" src="'+url+'&popup2=true" scrolling="'+scroll+'"/></div>'; 
	document.body.appendChild(popup_div);

	page_mask = document.getElementById("admin_mask2");

	page_popup = document.getElementById("admin_popup2");
	if (page_popup)
	{
		var top = 20;
		var right = 20;
		page_popup.style.bottom = top+"px";
		page_popup.style.right = right+"px";
	}

	if (page_mask)
	{
		page_mask.style.height = pageHeight()+"px";
		page_mask.style.width = pageWidth()+"px";
	}

	page_mask.oncontextmenu = block_click;

}  
function closePopup2(reload) 
{
	popup_div=document.getElementById("popup_div_id2");
	popup_div.parentNode.removeChild(popup_div);

	page_content = document.getElementById("whole_page_content");
	
	page_popup = document.getElementById("admin_popup");
	sels = page_popup.getElementsByTagName('select');
	for (i=0; i<sels.length; i++)
	{
		sels[i].disabled = false;
	}


	if (reload=='yes')
	{
		window.iframe_popup.location.reload();
	}

}

function openPopup3(anchor, options) {
 
	var args = '';
 
	if (typeof(options) == 'undefined') { var options = new Object(); }
	if (typeof(options.name) == 'undefined') { options.name = 'win' + Math.round(Math.random()*100000); }
 
	if (typeof(options.height) != 'undefined' && typeof(options.fullscreen) == 'undefined') {
		args += "height=" + options.height + ",";
	}
 
	if (typeof(options.width) != 'undefined' && typeof(options.fullscreen) == 'undefined') {
		args += "width=" + options.width + ",";
	}
 
	if (typeof(options.fullscreen) != 'undefined') {
		args += "width=" + screen.availWidth + ",";
		args += "height=" + screen.availHeight + ",";
	}
 
	if (typeof(options.center) == 'undefined') {
		options.x = 0;
		options.y = 0;
		args += "screenx=" + options.x + ",";
		args += "screeny=" + options.y + ",";
		args += "left=" + options.x + ",";
		args += "top=" + options.y + ",";
	}
 
	if (typeof(options.center) != 'undefined' && typeof(options.fullscreen) == 'undefined') {
		options.y=Math.floor((screen.availHeight-(options.height || screen.height))/2)-(screen.height-screen.availHeight);
		options.x=Math.floor((screen.availWidth-(options.width || screen.width))/2)-(screen.width-screen.availWidth);
		args += "screenx=" + options.x + ",";
		args += "screeny=" + options.y + ",";
		args += "left=" + options.x + ",";
		args += "top=" + options.y + ",";
	}
 
	if (typeof(options.scrollbars) != 'undefined') { args += "scrollbars=1,"; }
	if (typeof(options.menubar) != 'undefined') { args += "menubar=1,"; }
	if (typeof(options.locationbar) != 'undefined') { args += "location=1,"; }
	if (typeof(options.resizable) != 'undefined') { args += "resizable=1,"; }
 
	var win = window.open(anchor, options.name, args);
	return false;
 
}

function support_popup()
{
	var p=location.pathname;
	if(p.length>0){
		if(p.charAt(p.length-1)!='/') p=p+'/';
		else p=p+'#/';
	}
	else p = '/admin/#/';
	var t=p.split('/');
	if(t.length>1)
	{
		var z=t.slice(0,t.length-2);
		p = z.join('/')+'/';
	}
	var r=openPopup3( p+"support.php", {name:'support', width:500, height:500, center:1} );
}

function OpenPopupSelf(_)
{
	//alert(_.href);
	if(_.href){
		var r=openPopup3( _.href, {name:'selfpopup', width:500, height:500, center:1, scrollbars:1, resizable:1 } );
	}
}
