//json2
if(!this.JSON){JSON={};}
(function(){function f(n){return n<10?'0'+n:n;}
function m(n){while(n.toString().length<3)n='0'+n;while(n.toString().length<7)n=n+'0';return n;}
if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){return this.getUTCFullYear()+'-'+
f(this.getUTCMonth()+1)+'-'+
f(this.getUTCDate())+'T'+
f(this.getUTCHours())+':'+
f(this.getUTCMinutes())+':'+
f(this.getUTCSeconds())+'.'+
m(this.getUTCMilliseconds())+'Z';};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf();};}
var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapeable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;function quote(string){escapeable.lastIndex=0;return escapeable.test(string)?'"'+string.replace(escapeable,function(a){var c=meta[a];if(typeof c==='string'){return c;}
return'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);})+'"':'"'+string+'"';}
function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==='object'&&typeof value.toJSON==='function'){value=value.toJSON(key);}
if(typeof rep==='function'){value=rep.call(holder,key,value);}
switch(typeof value){case'string':return quote(value);case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null';}
gap+=indent;partial=[];if(typeof value.length==='number'&&!value.propertyIsEnumerable('length')){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||'null';}
v=partial.length===0?'[]':gap?'[\n'+gap+
partial.join(',\n'+gap)+'\n'+
mind+']':'['+partial.join(',')+']';gap=mind;return v;}
if(rep&&typeof rep==='object'){length=rep.length;for(i=0;i<length;i+=1){k=rep[i];if(typeof k==='string'){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}else{for(k in value){if(Object.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}
v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+
mind+'}':'{'+partial.join(',')+'}';gap=mind;return v;}}
if(typeof JSON.stringify!=='function'){JSON.stringify=function(value,replacer,space){var i;gap='';indent='';if(typeof space==='number'){for(i=0;i<space;i+=1){indent+=' ';}}else if(typeof space==='string'){indent=space;}
rep=replacer;if(replacer&&typeof replacer!=='function'&&(typeof replacer!=='object'||typeof replacer.length!=='number')){throw new Error('JSON.stringify');}
return str('',{'':value});};}
if(typeof JSON.parse!=='function'){JSON.parse=function(text,reviver){var j;function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==='object'){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{delete value[k];}}}}
return reviver.call(holder,key,value);}
text=text.replace(/[^"']\\\/Date\((-?\d+)\)\\\//g,function($0,$1,$2){return JSON.stringify(new Date(+$1));});cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+
('0000'+a.charCodeAt(0).toString(16)).slice(-4);});}
if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){j=eval('('+text+')');if(typeof reviver!=='function'){reviver=function(key,value)
{var a;if(typeof value==='string')
{ex=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.(\d{1,3})(\d{1,4})?)?Z$/;a=ex.exec(value);if(a!==null)
{if(!a[8]){a[8]='0';}
return new Date(Date.UTC(+a[1],+a[2]-1,+a[3],+a[4],+a[5],+a[6],+a[8]));}
a=/^\\?\/Date\((-?\d+)\)\\?\/$/.exec(value);if(a!==null)return new Date(+a[1]);}
return value;};}
return typeof reviver==='function'?walk({'':j},''):j;}
throw new SyntaxError('JSON.parse');};}})();
//=====================================================
function $() 
{
	var es = null;
	for( var i = 0; i < arguments.length; i++ )
	{
		var e = arguments[i];
		if(typeof(e)=='string') e = document.getElementById(e);
		if(arguments.length == 1) return e;
		es = es || new Array();
		es.push(e);
	}
	return es;
}
//================ CukeClient =================
function CukeClient( rootUrl, appSkin, cName, cLCID, pName, useCukeExt )
{
	if(window.parent&&window.parent.CukeInnerDialogLoaded)
	{
		this.CukeOpener = window.parent.CukeInnerDialogLoaded(window);
        this.AbsoluteHost = this.CukeOpener.AbsoluteHost;
		this.BrowserInfo = this.CukeOpener.BrowserInfo;
		this.RootUrl = this.RootPath = this.CukeOpener.RootUrl;
		if(this.CukeOpener.UseCukeExtension==1) this.UseCukeExtension = 1;
	}
	else
	{
		this.BrowserInfo = new Object();
		var a = navigator.userAgent.toLowerCase();
		if( this.BrowserInfo.IsIE = (a.indexOf("msie") != -1) )
			this.BrowserInfo.IsIEOld = (a.indexOf('msie 4')!=-1||a.indexOf('msie 5')!=-1);
		this.BrowserInfo.IsGecko	= !this.BrowserInfo.IsIE;
		this.BrowserInfo.IsSafari	= ( a.indexOf("safari") != -1 );
		this.BrowserInfo.IsNetscape	= ( a.indexOf("netscape") != -1 );
		try{this.BrowserInfo.ScreenWidth=screen.width;this.BrowserInfo.ScreenHeight=screen.height;}
		catch(e){this.BrowserInfo.ScreenWidth=800;this.BrowserInfo.ScreenHeight=600;}
		this.RootUrl = this.RootPath = rootUrl;
		if(useCukeExt==1)this.UseCukeExtension=1;
        var loc = document.location;
        this.AbsoluteHost = loc.href.substring(0,loc.href.indexOf(loc.pathname));
	}
	this.AppSkin = appSkin;
	this.Skin = rootUrl + appSkin;
	this.DefaultPortalName = xStr(pName) ? pName : '';
	this.CultureName = cName;
	this.LCID = cLCID;
	this.M = {};
	this.SyncDic = new SyncDic();
	this._s = new Array();
}
function CukeWebPage( id, fullName, grant, status )
{
	this.DObjID = id;
	this.FullName = fullName;
	this.PageGrantLevel = grant;
	this.Status = status;
	this.C = new Array();
	function CukeController(p,c,t,i)
	{
		this.Page=p;
		this.ContextName=c;
		this.TypeName=t;
		this.ID=i;
		this.ClientID='c_'+i;
		this.C={};
		this.Init=function(drv)
		{
			this.Driver = drv;
			return this;
		}
		this.PostBack = function( arg )
		{
			__doPostBack( this.ID, arg );
		}
	}
	this.FindController = function(c,t)
	{
		return this.C[c?(t?c+'.'+t:c):t];
	};
	this.AddController = function(c,t,i)
	{
		var l = new CukeController(this,c,t,i);
		this.C[this.C.length]=l;
		this.C[c?c+'.'+t:t]=l;
		if(i)this.C[i]=l;
	};
	this.M = {};
	function CukeWebModule( p, cid, i, n, g, s )
	{
		this.Page = p;
		this.ID = cid;
		this.ChildPrefixID = cid+'_';
		this.DObjID = i;
		this.Name = n;
		this.GrantLevel = g;
		this.Status = s;
		this.$ = function(id){return $(this.ChildPrefixID+id);}
		this.C={};
		var _idD;
		this.Init=function( drv, idData, initData )
		{
			this.Driver = drv;
			if(_idD=idData)
				if(initData)this.$(_idD).value=JSON.stringify(this.Data=initData);
				else this.Data = JSON.parse(this.$(_idD).value);
			else this.Data = {};
			return this;
		};
		this.UpdateData = function() {if(_idD)this.$(_idD).value=JSON.stringify(this.Data);}
		this.PostBack = function( arg )
		{
			__doPostBack( this.ID, arg );
		};
		
		this.SubM = {};
		function CukeSubWebModule( drv, module, cid, idData, initData )
        {
	        this.M = module;
	        this.ID = cid;
	        this.ChildPrefixID = cid+'_';
	        this.$ = function(id){return $(this.ChildPrefixID+id);}
	        this.Driver = drv;
	        if(idData)
		        if(initData)this.$(idData).value=JSON.stringify(this.Data=initData);
		        else this.Data = JSON.parse(this.$(idData).value);
	        else this.Data = {};
	        this.UpdateData = function(){if(idData)this.$(idData).value=JSON.stringify(this.Data);};
	        this.PostBack = function( arg )
	        {
		        __doPostBack( this.ID, arg );
	        };
        }
	    this.AddSubModule = function( drv, id, idData, initData )
	    {
		    return this.SubM[id] = new CukeSubWebModule( drv, this, this.ChildPrefixID+id, idData, initData );
	    };
	}
	this.AddModule = function( cid, id, name, grant, status )
	{
		return this.M[id] = this.M[name] = new CukeWebModule( this, cid, id, name, grant, status );
	};
}
CukeClient.prototype.AddString=function(resName,val)
{
	this._s[resName] = val;
};
CukeClient.prototype.Format=function(s)
{
  if(s)for(var i=1;i<arguments.length;++i)s=s.replace('{'+(i-1)+'}',arguments[i]);
  return s;
};
CukeClient.prototype.GetString=function(r)
{
  var s='';
  if(r.length>0)
  {
	if(r.charAt(0)=='!')s=r.substr(1);
	else s=this._s[r];
	if(s)for(var i=1;i<arguments.length;++i)s=s.replace('{'+(i-1)+'}',arguments[i]);
	
  }
  return s;
};
CukeClient.prototype._= function(r)
{
    return this.GetString(r);
}
CukeClient.prototype.LoadResource=function(r, callback)
{
    var c = this;
    return $j.getJSON( this.RootUrl + "CukeStd/Admin/Localization.ashx?ResName=" + r,
        function(data) {
            $j.each(data, function(k, v) { c.AddString(k,v); });
            if (typeof callback === 'function') callback();
        });
}
//How to return PageTitle without extratitle ??
CukeClient.prototype.GetPageTitle=function()
{ 
	return document.title; 
};
CukeClient.prototype.AssumeFullPageName=function(n)
{ 
	return xStrStartsWith(n,'Web.')||this.DefaultPortalName.length==0 ? n : 'Web.'+this.DefaultPortalName+'.'+n; 
};
CukeClient.prototype.IsFullPageName=function(n)
{ 
	return xStrStartsWith(n,'Web.'); 
};
CukeClient.prototype.LocalPageName=function(n)
{ 
	if(xStrStartsWith(n,'Web.')) 
	{
		n=n.substr(4);
		n=n.substr(n.indexOf('.')+1);
	}
	return n;
};
CukeClient.prototype.UrlCukePage=function(pageName,param,isDlg)
{ 
	if(!pageName||!pageName.length)pageName='MyHome';
	var u = this.AssumeFullPageName(pageName);
	if( this.UseCukeExtension ) {u+='.cuke?';if(isDlg)u='!'+u;} 
	else u = (isDlg?'CukeDlg':'Cuke')+'.aspx?cukePage='+u;
	if( xStr(param) ){if( param.charAt(0)!='&' ) param='&'+param; u+=param;}
	return u;
};
CukeClient.prototype.RootedUrlCukePage=function(pageName,param,isDlg)
{ 
	return this.RootUrl+this.UrlCukePage(pageName,param,isDlg);
};
CukeClient.prototype.ParseCukeUrl=function( u )
{
	var iP, r = null, p = null;
	if( (iP=u.search(/(\/|^)Cuke(Dlg)?\.aspx\?cukePage=/i)) >= 0 )
	{
		if(iP>0||u.charAt(0)=='/') r = u.substr(0,iP+1);
		iP=u.indexOf('=',iP)+1;
		if(u.charAt(iP)=='!')++iP;
		u = u.substr(iP);
		if( (iP=u.indexOf('&'))>0 ) 
		{
			p = u.substr(iP+1);
			u = u.substr(0,iP);
		}
	}
	else if( (iP=u.search(/\.cuke(\?|$)/i)) > 0 ) 
	{
		if(iP+6<u.length) p = u.substr(iP+6);
		u = u.substr(0,iP);
		iP = u.lastIndexOf('/')+1;
		if(iP>=1)
		{ 
			r = u.substr(0,iP);
			if(u.charAt(iP)=='!')++iP;
			u = u.substr(iP);
		}
	}
	else return null;
	return { Path:r, Page:this.AssumeFullPageName(u), Param:p };
};
// Handle 'Home' injection and .R. removing.
CukeClient.prototype.BeautifyCukePage=function( p )
{
	if(p!=null)
	{
		if(p.match(/^Web\.([^\.]*)\.R$/ )) p = p.substring(0,p.length-2)+'.Home';
		else p = p.replace( /^Web\.([^\.]*)\.R./, 'Web.$1.' );
		if( p.length==0 || p=='Web' ) p=null;
	}
	return p;
};
CukeClient.prototype.ApplyTemplate=function( Template, Container, OnError )
{ return Template.replace(/##\s*(.+?)\s*##/g,function(a,b){try{return eval(b);}catch(e){return OnError!=null?this.Format(OnError,b):'';}}); };
/*
	var rr;
	var C = new CukeClient('','Cuke');
	rr = C.ParseCukeUrl( '/CukeDlg.aspx?cukePage=Page.SubPage&FirstParam=1&tata=/jiji/' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	rr = C.ParseCukeUrl( 'cukedlg.aspx?CukePage=Page.SubPage&FirstParam=1&tata=/jiji/' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	rr = C.ParseCukeUrl( '/here/cukedlg.aspx?CukePage=Page.SubPage&FirstParam=1&tata=/jiji/' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	
	rr = C.ParseCukeUrl( 'http://localhost/CukePortal/Skins/Cuke/Web.Cuke.Public.cuke' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	rr = C.ParseCukeUrl( 'http://localhost/CukePortal/Skins/Cuke/!Web.Cuke.Public.cuke' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	rr = C.ParseCukeUrl( 'http://localhost/CukePortal/Skins/Cuke/Cuke.aspx?cukePage=Page.SubPage' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	rr = C.ParseCukeUrl( 'http://localhost/CukePortal/Skins/Cuke/Cuke.aspx?cukePage=!Page.SubPage' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	rr = C.ParseCukeUrl( 'http://localhost/CukePortal/Skins/Cuke/CukeDlg.aspx?cukePage=Page.SubPage' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	rr = C.ParseCukeUrl( 'http://localhost/CukePortal/Skins/Cuke/Web.Cuke.Public.cuke?OnlyParam' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	rr = C.ParseCukeUrl( 'http://localhost/CukePortal/Skins/Cuke/Web.Cuke.Public.cuke?&OnlyParam' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	rr = C.ParseCukeUrl( 'http://localhost/CukePortal/Skins/Cuke/!Web.Cuke.Public.cuke?&OnlyParam' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	rr = C.ParseCukeUrl( 'http://localhost/CukePortal/Skins/Cuke/Web.Cuke.Public.cuke?FirstParam&kdike=33&kjd=/de/&ml&' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	rr = C.ParseCukeUrl( 'http://localhost/CukePortal/Cuke.aspx?cukePage=Page.SubPage&FirstParam=1&tata=/jiji/' );
	alert( rr.Path + ' | ' + rr.Page + ' | ' + rr.Param );
	
	alert( 'Test OK' );
*/	
CukeClient.prototype.ParseUrlParams = function( u ) 
{
	var p = new Object(); 
	var m = /\?(.*?)$/.exec(u);
	if(!m||m.length<2||!m[1]) return p;
	var ar = m[1].split('&');
	if(!ar) return p; 
	var a,i=0;
	while(i<ar.length)
	{
		a = ar[i++].split("=");
		if(a.length>1) p[decodeURIComponent(a[0].replace(/\+/g,' '))] = decodeURIComponent(a[1].replace(/\+/g,' '));
	} 
	return p;
};





/* Artefact used to follow links into scripts */
CukeClient.prototype.EmbeddedUrl = function( url )
{
    return url;
}

/* Artefact used to follow links into scripts */
CukeClient.prototype.EU = function( url )
{
    return url;
}



/* 

SyncDic : Simple Value|Key Dictionary with Events.
Used to syncronize client controls on a value.
It allows client side communication between controls which don't know each other.

-- SyncDic.Val( name )
Get the current value associed to the named spot.

-- SyncDic.Val( name, value ) 
Stores the value in the named spot. Call all binded function if the value has changed.

-- SyncDic.Bind( name, func ) 
Binds an handler function to the change event of the value associed to the named spot.

-- SyncDic.UnBind( name, func ) 
UnBinds an handler function.

Ex: This will display 'OneControl' css classes into a TextBox.

    OneControl.mouseover( function() 
    { 
        Cuke.SyncDic.Val( 'SpotName', $(this).attr('class'); );
    });
    
    Cuke.SyncDic.Bind( 'SpotName', function(value, name) 
    { 
        $('.MyTextBox').html( value ); 
    });
*/

SyncDic = function()
{
    this.values = [];
    this.listeners = [];
}

SyncDic.prototype.Val = function( name, value )
{
    if (typeof value === 'undefined') return this.values[name];
    else 
    {
        if ( value != this.values[name] )
        {
            this.values[name] = value;
            var l = this.listeners[name];
            if (typeof l !== 'undefined' )
                for(var x=0;x<l.length;x++) if (typeof l[x] === 'function') l[x](value, name);
        }
    }
}

SyncDic.prototype.Bind = function( name, func )
{
    if (typeof this.listeners[name] === 'undefined') this.listeners[name] = [];
    if (typeof func === 'function') this.listeners[name].push( func );
}

SyncDic.prototype.UnBind = function( name, func )
{
    if (typeof this.listeners[name] !== 'undefined')
    {
        var i=0, n=[];
        for( var x=0;x<this.listeners[name];x++ )
        {
            if ( this.listeners[name][x] != func ) n[i++] = this.listeners[name][x];
        }
    }
}
