
var g_agt		= navigator.userAgent.toLowerCase();
var is_opera	= (g_agt.indexOf("opera") != -1);
var is_ie		= (g_agt.indexOf("msie") != -1);


function exist(s)
{
	return $("#"+s)!=null;
}
function myInnerHTML(idname, html)
{
	if (exist(idname))
	{
		$("#"+idname).html(html);
	}
}

var bgIndex=10000;
var msgIndex=10001;

function dialog(title,w,h,url,divid,mid,auto)
{
	var width	= w;
	var height	= h;
	var title	= title;
	var auto	=auto;
	var divid	=divid;
	
	var mid		=mid;

	if(typeof arguments[4]== "undefined")
	{
		divid='1';
	}
	if(typeof arguments[5]== "undefined")
	{
		mid='-1';
	}
	if(typeof arguments[6]== "undefined")
	{
		auto=0;
	}

	var winbg_w=0;
    var winbg_h=0;
	
	if (is_ie)
	{
		winbg_w=width+20;
		winbg_h=height+20;
	}else
	{
		winbg_w=width+20;
		winbg_h=height+20;
	}
	
	bgIndex=bgIndex+2;
    msgIndex=msgIndex+2;
	
	delWinD(divid);

	var bgObj=document.createElement("div"); 
	bgObj.id="bgDiv"+divid; 
	bgObj.style.position="absolute"; 
	bgObj.style.zIndex=bgIndex; 

	var _scrollWidth=Math.max(document.body.offsetWidth,document.documentElement.offsetWidth); 
	var _scrollHeight=Math.max(Math.max(document.body.clientHeight,document.documentElement.clientHeight),Math.max(document.body.offsetHeight,document.documentElement.offsetHeight)); 
    var _sScrollTop =document.body.scrollTop+document.documentElement.scrollTop;
	

	var sClientWidth=document.body.offsetWidth;
	var sClientHeight=document.body.offsetHeight;
	var sScrollTop=document.body.scrollTop+document.documentElement.scrollTop;


	bgObj.style.width=sClientWidth+"px";  
	bgObj.style.height=(sClientHeight+sScrollTop)+"px";
	bgObj.style.top="0px"; 
	bgObj.style.left="0px"; 
	bgObj.style.background="#fff"; 
	bgObj.style.filter="alpha(opacity=20)"; 
	bgObj.style.opacity="0.2"; 

	
    document.body.appendChild(bgObj);

	var msgid="msgDiv"+divid;
	var msgObj=document.createElement("div");
    msgObj.id=msgid;
    msgObj.style.position = "absolute";
	
	var pos=middle(width);

	if(mid=='-1')
	{
		sleft	=parseInt(pos.left);
		sTop	=parseInt(pos.top);

	}else
	{
		sleft	=parseInt(pos.left)+50;
		sTop	=parseInt(pos.top)+40;
	}
	
	msgObj.style.left = sleft+"px";
    msgObj.style.background="#fff";
	msgObj.style.zIndex=msgIndex;
	msgObj.style.border="0px solid #000000";	
    msgObj.style.top = sTop+"px";
    msgObj.style.width = width + "px";
    msgObj.style.height =height + "px";
	
	var digname="dig_"+Math.floor(Math.random()*10000+1);


	var str='';
	str +='<div id="winFloat" class="winFloat" style="display:block;width:'+width+'px;height:'+height+'px;">';
	str	+=' <div class="winBg" style="width:'+width+'px;height:'+height+'px;"></div>';
	str	+=' <div class="winHeader">';
	str	+='   <h1>'+title+'</h1>';
	str +='	  <a href="javascript:void(0);" onclick="delWinD('+divid+');" title="close"><img src="/images/ico51.gif" alt="close" /></a>';
	str +='  </div>';
	auto=10;
	if(auto>0)
	{
		str +=' <div id="winbody'+divid+'" class="winBody" style="height:'+(height-34)+'px;overflow:auto;">';
	}else
	{
		str +=' <div id="winbody'+divid+'" class="winBody" style="height:'+(height-34)+'px;">';
	}
	str +='  </div>';
	str +='</div>';
	
	msgObj.innerHTML=str;
	
	document.body.appendChild(msgObj);
	
	$('#winbody'+divid).html('<font color="red">Loading...</font>');

	gethtml(url,'winbody'+divid);

}

function gethtml(url,divid)
{	
	var pars='t='+Math.random();
	var url = '/dialog/'+url;
	if(url.indexOf('?'))
	{
		url=url+"&"+pars;	
	}else
	{
		url=url+"?"+pars;	
	}

	$.ajax({
		   type: "get",
		   url: url,
		   cache:false,
		   dataType:"html",
		   error: function(){
				alert('Error loading data');
			},
		   success: function(data){
			$("#"+divid).html(data);
			//rd$(divid).innerHTML=data;
		   }
	});
}


function delWinD(divid)
{
	if($("#bgDiv"+divid))
	{
		$("#bgDiv"+divid).css("display","none");
		$("#bgDiv"+divid).remove();
   
	}
	if($("#msgDiv"+divid))
	{
		$("#msgDiv"+divid).css("display","none");
		$("#msgDiv"+divid).remove();
	}
}

function getpos(element)
{
	if(arguments.length!=1||element==null)
	{
		return null;
	}
	var elmt=element;
	var offsetTop=elmt.offsetTop;
	var offsetLeft=elmt.offsetLeft;
	var offsetWidth=elmt.offsetWidth;
	var offsetHeight=elmt.offsetHeight;
	while(elmt=elmt.offsetParent)
	{
		if(elmt.style.position=='absolute'||(elmt.style.overflow!='visible'&&elmt.style.overflow!=''))
		{
			break;
		}
		offsetTop+=elmt.offsetTop;
		offsetLeft+=elmt.offsetLeft;
	}
	return {
		top:offsetTop,left:offsetLeft,right:offsetWidth+offsetLeft,bottom:offsetHeight+offsetTop
	};
}

function middle(width)
{
	var sClientWidth=document.body.clientWidth;
	var sClientHeight=document.body.clientHeight;
	var sScrollTop=document.body.scrollTop+parent.document.documentElement.scrollTop;

	var sleft = (sClientWidth - width) / 2;
	var iTop = sScrollTop + 100;
	var sTop = iTop > 0 ? iTop : 0;

	return {
		top:sTop,left:sleft
	};
		
}

