CukeClient.prototype.OpenNakedWindow=function(url,name,w,h,x,y)
{
	if( !w || w < 0 ) w = 0.67;
	if( w < 1 ) w *= this.BrowserInfo.ScreenWidth;
	if( !h || h < 0 ) h = 0.5;
	if( h < 1 ) h *= this.BrowserInfo.ScreenHeight;
	w = Math.ceil(w);
	h = Math.ceil(h);
	if( !xDef(x) ) x = (this.BrowserInfo.ScreenWidth-w)/2;
	if( !xDef(y) ) y = (this.BrowserInfo.ScreenHeight-h)/2;
	var s='resizable=yes,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=yes';
	s+=',width='+w+',height='+h+',left='+x+',top='+y;
	return window.open( url, name, s );
};
CukeClient.prototype.BigTextEdit=function(encR,lcid)
{
	if( lcid ) encR += '&lcid='+lcid;
	return this.OpenNakedWindow( this.RootPath+'Cuke/Localization/BigTextEdit.aspx?CWLabel='+encR );
};
CukeClient.prototype.OpenItemHelp=function(page,param)
{
	page = this.RootedUrlCukePage(page,param,true);
	if( !xDef(this._winHelp) || this._winHelp.closed ) this._winHelp = this.OpenNakedWindow( page, 'cukeWinHelp', 0.4, 0.4, 100, 100 );
	else this._winHelp.location.href = page;
	this._winHelp.focus();
	return this._winHelp;
};
CukeClient.prototype.OpenHelp=function(resName)
{
	return this.OpenItemHelp( 'I.Cuke.Runtime.ShowHelp', '&CWLabel='+resName );
};
// Dialog
CukeClient.prototype.OpenDlgUrl=function( url, successHandler, customValue, title, w, h, fixedSize, parentWindow )
{
	//alert('OpenDlgUrl: url='+url+' h='+successHandler+' d='+customValue+' w='+w);
	if( !parentWindow ) parentWindow = window;
	var o = new Object();
	o.Title = title;
	o.Page = url;
	o.CukeOpener = this;
	o.CustomValue = customValue;
	if( !w || w < 0 ) w = 0.67;
	if( w < 1 ) w *= this.BrowserInfo.ScreenWidth;
	if( !h ) h = -0.5;
	if( h < 0 ) 
	{
		o.NegHeight=1;
		h = -h;
	}
	if( h < 1 ) h *= this.BrowserInfo.ScreenHeight;
	w = Math.ceil(w);
	h = Math.ceil(h);
	var dlg = this.RootPath+'CukeDialog.htm';
	if( parentWindow.showModalDialog )
	{
		var r = parentWindow.showModalDialog( dlg, o, "dialogWidth:" + w + "px;dialogHeight:" + h + "px;help:no;scroll:no;status:no;resizable:"+(fixedSize?'0':'1'));
		this.CurDialog = null;
		if(xDef(r))
		{
			if(typeof(successHandler)=='function') successHandler(r,customValue);
			else if(typeof(successHandler)=='string')
			{
				if(xStrStartsWith(successHandler,'function'))successHandler='new '+successHandler;
				//TODO: replace this sureval with json sureval...
				r = '\''+r.replace(/\'/g,'\\\'')+'\'';
				if(xDef(customValue))
				{
					customValue = '\''+customValue.replace(/\'/g,'\\\'')+'\'';
					eval(successHandler+'('+r+','+customValue+')');
				}
				else eval(successHandler+'('+r+')');
			}
		}
	}
	else
	{
		var x = (this.BrowserInfo.ScreenWidth-w)/2;
		var y = (this.BrowserInfo.ScreenHeight-h)/2;
		var s  = "location=no,menubar=no,toolbar=no,dependent=yes,dialog=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable="+(fixedSize?'no':'yes')+",width="+w+",height="+h+",top="+y+",left="+x;
		if(typeof(successHandler)=='function') o.SuccessHandler=successHandler;
		else if(typeof(successHandler)=='string') o.SuccessHandler=eval(successHandler);
		var oD = parentWindow.open( '', 'CukeDlg_'+escape(url), s, true );
		if( oD )
		{
			oD.moveTo(x,y);
			oD.resizeTo(w,h);
			oD.focus();
			oD.location.href = dlg;
			oD.dialogArguments = o;
			parentWindow.CukeDialogInfo = o;
			try
			{
				window.top.captureEvents( Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS );
				window.top.parent.addEventListener( 'mousedown', this._MozFocus, true );
				window.top.parent.addEventListener( 'mouseup', this._MozFocus, true );
				window.top.parent.addEventListener( 'click', this._MozFocus, true );
				window.top.parent.addEventListener( 'focus', this._MozFocus, true );
			}
			catch(e){}
		}
	}
};
CukeClient.prototype._MozFocus=function()
{
	if( typeof(Cuke)!="object" ) return false;
	if( Cuke.CurDialog && !Cuke.CurDialog.closed ) Cuke.CurDialog.focus();
	else
	{
		Cuke.CurDialog = null;
		try
		{
			window.top.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS) ;
			window.top.parent.removeEventListener( 'onmousedown', Cuke._MozFocus, true ) ;
			window.top.parent.removeEventListener( 'mouseup', Cuke._MozFocus, true ) ;
			window.top.parent.removeEventListener( 'click', Cuke._MozFocus, true ) ;
			window.top.parent.removeEventListener( 'onfocus', Cuke._MozFocus, true ) ;
		}
		catch (e)
		{}
	}
	return false;
};
CukeClient.prototype.OpenDlg=function(pageName,params,successHandler,customValue,w,h)
{
	if( params && params.length > 0 )
	{ 
		if( params.charAt(0) != '&' ) params = '&'+params;
		pageName += params;
	}
	pageName='CukeDlg.aspx?cukePage='+pageName;
	this.OpenDlgUrl( pageName, successHandler, customValue, null, w, h );
};
CukeClient.prototype.GetSyncHttpRequest = function()
{
	if(this._xmlCaller)return this._xmlCaller;
	return this._xmlCaller = this.CreateHttpRequest();
};
CukeClient.prototype.CreateHttpRequest = function()
{
	if(!this.BrowserInfo.IsIE)return new XMLHttpRequest();
	try{return new ActiveXObject( 'MSXML2.XmlHttp' ); }
	catch(e){try{return new ActiveXObject( 'Microsoft.XMLHTTP' );}catch(e){alert(e);}} 
};
CukeClient.prototype.AjaxCall = function( u, asyncFunction, data )
{
	if( asyncFunction != null && !(typeof(asyncFunction)=='function') ){data=asyncFunction;asyncFunction=null;}
	var bGet = (data==null);
	var bAsync = (asyncFunction!=null);
	
	var h = bAsync ? this.CreateHttpRequest() : this.GetSyncHttpRequest();
	
	if( bGet ) h.open( "GET", u, bAsync ) 
	else 
	{
		h.open( "POST", u, bAsync );
		h.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	}
	if( bAsync )
	{	
		h.onreadystatechange = function() 
		{
			if( h.readyState == 4 )
			{
				if( h.status == 200 || h.status == 304 || h.status == 0 )
					asyncFunction( h.responseXML );
				else alert( 'XML request error: ' + h.statusText + ' (' + h.status + ')' );
			}
		}
	}
	bGet ? h.send( null ) : h.send( data );
	if( !bAsync )
	{
		if ( h.status == 200 || h.status == 304 || h.status == 0 ) return h.responseXML;
		else alert( 'XML request error: ' + h.statusText + ' (' + h.status + ')' ) ;
	}
};
CukeClient.prototype.GetUrl = function( u, asyncFunction )
{
	return Cuke.AjaxCall( u, asyncFunction );
};
CukeClient.prototype.PostUrl = function( u, asyncFunction, data )
{
	if( data == null ) data = '';
	else if( typeof(data) == 'object' )
	{
		var name, temp = Array();
		for(name in data)
			 if(typeof(data[name])!='function')temp[temp.length]=name+'='+encodeURIComponent(JSON.stringify(data[name]));
		data = temp.join('&');
	}
	return Cuke.AjaxCall( u, asyncFunction, data );
};
