var NFW={version: '2', FunctionNS:{},PageNS:{}};
var NFWJS={
	'ds':'/',
	'path':{
		'tdir':'/.templates/1',
		'rdir':'',
		'idir':'',
		'mdir':'/modules'
	},
	page: {
		params: []
	}
};
/*...........................................................................*/
function NFW_Includes(){
	if (NFW_Includes.caller != NFW_Includes.getInstance)
		throw new Error("There is no public constructor for NFW_Includes.");
	this._includes=new Array();
};
NFW_Includes.__instance__=null;
NFW_Includes.getInstance=function(){
	if (this.__instance__==null)
		this.__instance__=new NFW_Includes();
	return this.__instance__;	
};
NFW.FunctionNS.Includes=NFW_Includes;
NFW.FunctionNS.Includes.prototype.Add=function(arr){
	this._includes[this._includes.length]=arr;
};
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
NFW.FunctionNS.Includes.prototype.SetLoaded=function(filename){
	for (var i=0;i<this._includes.length;i++){
		if(this._includes[i][0].filename==filename)this._includes[i][1]=true;
	}
};
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
NFW.FunctionNS.Includes.prototype.Completed=function(id){
	for (i=0;i<this._includes.length;i++){
		if(!this._includes[i][1])return false;
	}
	return true;
};
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
NFW.FunctionNS={
	ConfirmDelete:function(){
			return(confirm('This action will delete the record. Continue?'));
		},
	GetDaysInMonth:function(year,month){
		var d = new Date(year, month, 0);
		return(d.getDate());
	},
	GetFirstDayInWeek:function(year,month){
		var d = new Date(year, month-1, 1);
		return(d.getDay()+1);
	},
	GetNameOfMonthDay:function(year,month,day,limit){
		var weekday=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
		var d=new Date(year,month-1,day);
		dayname=weekday[d.getUTCDay()];
		if (limit==0) return(dayname);
		return String(dayname).substring(0,limit);
	},
	GetMonthName:function(monthnumber){
		var months=new Array('','January','February','March','April','May','June','July','August','September','October','November','December');
		return(months[monthnumber]);
	},
	GotoAnchor:function(link){
		var getAnchorPosition=function(anchorName) {
			if (document.layers){
				var anchor = document.anchors[anchorName];
				coords={ x: anchor.x, y: anchor.y };
			}else if (document.getElementById) {
				var anchor = document.anchors[anchorName];
				var coords = {x: 0, y: 0 };
				while (anchor) {
					coords.x += anchor.offsetLeft;
					coords.y += anchor.offsetTop;
					anchor = anchor.offsetParent;
				}
			}
			//alert(anchorName+': '+coords.x+'/'+coords.y);
			return coords;
		};
		//alert(link);
		if (link==undefined){
			anchor=document.location.hash.split('#')[1];
		}else{
			anchor=link.hash.substring(1);
		}
		if (document.layers) {
			var coords = getAnchorPosition(anchor);
			window.scrollTo(coords.x , coords.y);
			return false;
		}else if (!document.all && document.getElementById) {
			var coords = getAnchorPosition(anchor);
			window.scrollTo(coords.x , coords.y);
			return false;
		}else return true;
	}
};
/*...........................................................................*/
/*...........................................................................*/
/*...........................................................................*/
/*...........................................................................*/
function NFW_Scripts(json){
	
	if (NFW_Scripts.caller != NFW_Scripts.getInstance)
		throw new Error("There is no public constructor for NFW_Scripts.");
	
	this.includes=eval(json);
	this.Includes=new NFW.FunctionNS.Includes.getInstance();
};
NFW_Scripts.__instance__=null;
NFW_Scripts.getInstance=function(json){
	if (this.__instance__==null)
		this.__instance__=new NFW_Scripts(json);
	return this.__instance__;	
};
NFW.FunctionNS.Scripts=NFW_Scripts;
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
NFW.FunctionNS.Scripts.prototype.loadScripts=function(callback){
	this.callback=callback;
	for(i=0;i<this.includes.length;i++){
		if (/*this.includes[i][1] refers to browser type*/
			1!=1
			|| this.includes[i].browser==''
			/**
			 TODO: Browser specific controls need to be added.
			 */
		)this.Includes.Add([this.includes[i],false]);
	}
	for(var ctr=0;ctr<this.includes.length;ctr++) this.load(ctr);
};
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
NFW.FunctionNS.Scripts.prototype.load=function(i){
	
	switch(this.includes[i].type){
		case 'script':
			$.getIfModified(
				NFWJS.path.idir+'/'+this.includes[i].type+'s/'+this.includes[i].filename,
				function(data){
					eval(data);
					//$('head').append('<script type="text/javascript" language="javascript" id="'+this.includes[i].filename+'">'+data+'</script>');
				}
			);
			break;
		case 'module_script':
			$.getIfModified(
				NFWJS.path.mdir+'/'+this.includes[i].modulename+'/'+this.includes[i].filename,
				function(data){
					eval(data);
					//$('head').append('<script type="text/javascript" language="javascript" id="'+this.includes[i].filename+'">'+data+'</script>');
				}
			);
			break;
		case 'style':
			$.getIfModified(
				NFWJS.path.idir+'/'+this.includes[i].type+'s/'+this.includes[i].filename,
				function(data){
					eval(data);
					//$('head').append('<style type="text/css" id="'+this.includes[i].filename+'">'+data+'</style>');
				}
			);
			break;
	}
	this.tryToSetCompleted(this.includes[i].filename);
};
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
NFW.FunctionNS.Scripts.prototype.tryToSetCompleted=function(filename){
	this.Includes.SetLoaded(filename);
	//alert(scriptId+':'+this.Includes.Completed());
	if(this.Includes.Completed())
		if (this.callback!==null)this.callback();
};
/*...........................................................................*/
/*...........................................................................*/
/*...........................................................................*/
/*...........................................................................*/
function NFW_Error(desc,page,line,chr){
	//TODO: better error display is to be designed. 
	//alert('NFW_Error Handler\n\n'+msg);

	alert(
	 'JavaScript error occurred! \n'
	+'\nError description: \t'+desc
	+'\nPage address:      \t'+page
	+'\nLine number:       \t'+line
	);
	return true
};
NFW_Error._this__=null;
NFW_Error.getInstance=function(){
	if (this._this__==null)
		this._this__=new NFW_Error();
	return this.__instance__;	
};
NFW.FunctionNS.Error=NFW_Error;
//onerror=NFW.FunctionNS.Error;
/*...........................................................................*/
