var xgWindow = new xgWindowInterface();


function xgWindowInterface()
{
		this.ShowModal = xgWindow_ShowModal;
		this.ShowInfo  = xgWindow_ShowInfo;
		this.ShowModalC = xgWindow_ShowModalC;
		this.ShowModalPrompt = xgWindow_ShowModalPrompt;
		this.ShowError = xgWindow_ShowError;

}

function xgWindow_ShowModal(text,ontop){
	var Params = new Object();
	Params.text = text;
	var dim = calculateTextDimensions(text);

	if (Browser.sName == "ie"){
		window.showModalDialog(xg.coreLocation+"/alert.xgs?txt="+text+"&width="+dim.width+"&height="+dim.height,Params,"center=true;status=no;dialogWidth:"+dim.width+"px;dialogHeight:"+dim.height+"px;help:off;scroll=no;");
	}
	else
	{
		var screenX = parseInt(screen.availWidth)/2-dim.width/2;
		var screenY = parseInt(screen.availHeight)/2-dim.height/2;
		var xW=window.open(xg.coreLocation+"/alert.xgs?txt="+text+"&width="+dim.width+"&height="+dim.height,Params,"center=true,width="+dim.width+",height="+dim.height+",modal=yes,screenX="+screenX+",screenY="+screenY);
	}

}

function xgWindow_ShowModalC(text)
{
	var ans = "";
	var Params = new Object();
	Params.text = text;
	var dim = calculateTextDimensions(text);
	if (Browser.sName == "ie")	{
		var url = xg.coreLocation+"confirm.xgs?txt="+text+"&width="+dim.width+"&height="+dim.height;
		ans = window.showModalDialog(url,Params,"center=true;status=no;dialogWidth:"+dim.width+"px;dialogHeight:"+dim.height+"px;help:off;scroll=no;");
	}
	else
	{
		var screenX = parseInt(screen.availWidth)/2-dim.width/2;
		var screenY = parseInt(screen.availHeight)/2-dim.height/2;
		var wina=window.open(xg.coreLocation+"/confirm.xgs?txt="+text+"&width="+dim.width+"&height="+dim.height,"Params","center=true,width="+dim.width+",height="+dim.height+",modal=yes,screenX="+screenX+",screenY="+screenY);
		if (wina.closed)
		{
			ans = wina.status;
		}
	}

	return ans;
}

function xgWindow_ShowModalPrompt(text)
{
	var ans = "";
	var Params = new Object();
	Params.text = text;
	var dim = calculateTextDimensions(text);
	dim.width=380;
	dim.height=130;
	if (Browser.sName == "ie"){
		ans = window.showModalDialog(xg.coreLocation+"prompt.xgs?txt="+text+"&width="+dim.width+"&height="+dim.height,Params,"center=true;status=no;dialogWidth:"+dim.width+"px;dialogHeight:"+dim.height+"px;help:off;scroll=no;");
	}
	else
	{
		var screenX = parseInt(screen.availWidth)/2-dim.width/2;
		var screenY = parseInt(screen.availHeight)/2-dim.height/2;
		var wina=window.open(xg.coreLocation+"/prompt.xgs?txt="+text+"&width="+dim.width+"&height="+dim.height,"Params","center=true,width="+dim.width+",height="+dim.height+",modal=yes,screenX="+screenX+",screenY="+screenY);
		ans = wina.status;
	}

	return ans;
}

//var walert = alert;
function xgWindow_ShowInfo(text){
	try{
		var _info = top.window.document.createElement("DIV");
		top.window.document.body.insertBefore(_info);
		_info.style.position = "absolute";
		_info.style.left = parseInt(screen.availWidth)/2-200;
		_info.style.top =parseInt(screen.availHeight)/2 - 50;
		_info.style.width = 300;
		_info.style.height = 100;
		_info.className = "xg_info_surface";
		thtml = "<table border=\"0\" width=\"100%\" height=\"100%\">";
		thtml +="<tr><td align=\"center\" valign=\"middle\">";
		thtml +="<span class=\"xg_info_text\">"+unescape(text)+"</span><br>";
		thtml+="<img src=\""+xg.serverLocation+"/xg/imgs/waiting.gif\" style=\" position:relative;top:3px;\">";
		thtml +="</td></tr></table>";
		_info.innerHTML = thtml;
		top._infow = _info;
		top._infok = 0;
		_startTimeout();
	}catch(e){

	}
}

function xgWindow_ShowError(text,ino){
		var Params = new Object();
		Params.text = text;
		var dim = calculateTextDimensions(text);
		var _width = dim.width+50;
		var _height = dim.height+50;
 window.showModalDialog(xg.coreLocation+"serror.xgs?txt="+escape(text)+"&infoid="+ino+"&width="+_width+"&height="+_height,Params,"center=true;status=no;dialogWidth:"+dim.width+"px;dialogHeight:"+dim.height+"px;help:off;scroll=no;");
}

function _startTimeout(){
	top._infok ++;
	if (top._infok<40){
			var _timer = window.setTimeout("_startTimeout()",30);
	}else{
			window.clearTimeout(_timer);
			try{
			top.window.document.body.removeChild(top._infow);
			}catch(e){}
	}
}

function alert1(Text1){
		Text1 = escape(Text1);
		var answer = xgWindow.ShowModal(Text1);
}

function error(text,err){
	xgWindow.ShowError(text,err);
}


function info(text){
	text = escape(text);
	xgWindow.ShowInfo(text);
}

function confirm1(text)
{
	var answer = xgWindow.ShowModalC(escape(text));
	return answer;
}

function prompt1(str){
	var answer = xgWindow.ShowModalPrompt(str);
	return answer;
}

function openWindow(src,xWidth,xHeight)
{
	var xW="";
	if (Browser.sName == "ie")
	{
		xW = window.showModalDialog(src,"","center=true;status=no;dialogWidth:"+xWidth+"px;dialogHeight:"+xHeight+"px;help:off;scroll=no;");
	}
	else
	{
		var screenX = parseInt(screen.availWidth)/2-xWidth/2;
		var screenY = parseInt(screen.availHeight)/2-xHeight/2;
		xW=window.open(src,"","center=true,width="+xWidth+",height="+xHeight+",modal=yes,screenX="+screenX+",screenY="+screenY);
	}
	return xW;
}



function calculateTextDimensions(text){
	var x = new Dimension();
	text = unescape(text);
	var textbox = document.createElement("DIV");

	if (Browser.sName == "ie"){
		document.body.insertBefore(textbox);
	}else{
		document.body.appendChild(textbox);
	}
	textbox.id = "calculate_dimensions_"+GenerateID();
    xgSetStyle (textbox,"position:absolute;word-wrap:true;left:0px;top:0px;width:1px;height:1px;overflow:visible;visibility:hidden;");
	textbox.className = "xg_core_error_title";
	text = replaceStr(text," ","&nbsp;");
	textbox.innerHTML=text;

	var textwidth;
	var textheight;
	var finalwidth;
	var finalheight;
	var minheight ;
	var minwidth;

	if (Browser.sName == "ie"){
	textwidth = textbox.scrollWidth;
	textheight = textbox.scrollHeight;
	minwidth=230;
	minheight=125;
	}else{
		textwidth=textbox.scrollWidth;
		textheight=textbox.scrollHeight;
		minwidth=230;
		minheight=50;
	}

	finalwidth = 100+textwidth;
	finalheight = 100+textheight;

	if (finalwidth<minwidth) finalwidth = minwidth;
	if (finalheight<minheight) finalheight = minheight;

	x.width=finalwidth;
	x.height=finalheight;
//	document.body.removeChild(textbox);
	return x;
}

function calculateTextSize(text){
	var x = new Dimension();
	text = unescape(text);
	var textbox = document.createElement("DIV");

	if (Browser.sName == "ie"){
		document.body.insertBefore(textbox);
	}else{
		document.body.appendChild(textbox);
	}
	textbox.id = "calculate_dimensions_"+GenerateID();
    xgSetStyle (textbox,"position:absolute;word-wrap:true;left:0px;top:0px;width:1px;height:1px;overflow:auto;visibility:hidden;");
	textbox.className = "xg_core_error_title";
	text = replaceStr(text," ","&nbsp;");
	textbox.innerHTML=text;

	var textwidth;
	var textheight;
	var finalwidth;
	var finalheight;
	var minheight ;
	var minwidth;

	if (Browser.sName == "ie"){
	textwidth = textbox.scrollWidth;
	textheight = textbox.scrollHeight;

	}else{
		textwidth=textbox.scrollWidth;
		textheight=textbox.scrollHeight;
	}

	finalwidth =textwidth;
	finalheight =textheight;

	x.width=finalwidth;
	x.height=finalheight;
	document.body.removeChild(textbox);
	return x;
}
