Ext.namespace('strictlyPHP');

/**
 * strictlyPHP Form Window
 */
strictlyPHP.FormWindow = function(config){
	strictlyPHP.FormWindow.superclass.constructor.call(this, config);
	this.resizable = false;
	this.modal = true;
	this.layout = 'form';
	
	this.on({
		//adds keymap for Enter to simulate the default button click after render
		show: {
			scope: this,
			fn: function(){
				if(this.mapenter)
				{
					keymap = new Ext.KeyMap(this.id, [{
				        key: Ext.EventObject.ENTER,
				        scope: this,
				        fn: function(){
				        	this.defaultButton.handler.call(this.defaultButton.scope);
				        }
				    }]);
				}
			}
		}
	});
}
Ext.extend(strictlyPHP.FormWindow, Ext.Window, {
	mapenter: false,
	setFormPanel: function(panel){
		this.formpanel = panel;
		this.add(panel);
	},
	getFormPanel: function(){
		return this.formpanel;
	}
});

loadJs = function(filename)
{
	loadScript(filename, null);
	//loadHeadTag({tag: 'script',type: 'text/javascript',src: filename});
}
loadCss = function(filename)
{
	loadStyleSheet(filename, null);
	//loadHeadTag({tag: 'link',rel: 'stylesheet',type: 'text/css',href: filename});
}
//loadHeadTag = function(config)
//{
//	var elHead = Ext.get('head');
//	arrTags = Ext.query('head');
//	if(!Ext.isEmpty(arrTags) && Ext.isArray(arrTags))
//	{
//		elHead = arrTags[0];
//		Ext.DomHelper.append(elHead, config);
//	}
//}
unloadJs = function(filename)
{
	unloadHeadTag('head/script[src$=' + filename + ']');
	arguments.callee.queue = {};
}
unloadCss = function(filename)
{
	unloadHeadTag('head/link[href$=' + filename + ']');
	arguments.callee.queue = {};
}
unloadHeadTag = function(query)
{
	arrTags = Ext.query(query);
	if(!Ext.isEmpty(arrTags))
	{
		Ext.each(arrTags, function(el){
			Ext.get(el).remove();
		});
	}
}

loadScript = function(url, callback) {
	var f = arguments.callee;
	if (!("queue" in f))
		f.queue = {};
	var queue =  f.queue;
	if (url in queue) { // script is already in the document
		if (callback) {
			if (queue[url]) // still loading
				queue[url].push(callback);
			else // loaded
				callback();
		}
		return;
	}
	queue[url] = callback ? [callback] : [];
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.onload = script.onreadystatechange = function() {
		if (script.readyState && script.readyState != "loaded" && script.readyState != "complete")
			return;
		script.onreadystatechange = script.onload = null;
		while (queue[url].length)
			queue[url].shift()();
		queue[url] = null;
	};
	script.src = url;
	document.getElementsByTagName("head")[0].appendChild(script);
}
loadStyleSheet = function(url, callback) {
	var f = arguments.callee;
	if (!("queue" in f))
		f.queue = {};
	var queue =  f.queue;
	if (url in queue) { // script is already in the document
		if (callback) {
			if (queue[url]) // still loading
				queue[url].push(callback);
			else // loaded
				callback();
		}
		return;
	}
	queue[url] = callback ? [callback] : [];
	var script = document.createElement("link");
	script.type = "text/css";
	script.rel = "stylesheet";
	script.media = "screen";
	script.onload = script.onreadystatechange = function() {
		if (script.readyState && script.readyState != "loaded" && script.readyState != "complete")
			return;
		script.onreadystatechange = script.onload = null;
		while (queue[url].length)
			queue[url].shift()();
		queue[url] = null;
	};
	script.href = url;
	document.getElementsByTagName("head")[0].appendChild(script);
}