﻿Ext.QuickTips.init();

Ext.state.Manager.setProvider(new Ext.state.CookieProvider());

Ext.Ajax.on('beforerequest', function(conn, options){ 
	var spinner = Ext.get('spinner');
	if (spinner != null)
		spinner.setVisible(true);
});

Ext.Ajax.on('requestcomplete', function(conn, options){ 
	var spinner = Ext.get('spinner');
	if (spinner != null)
		spinner.setVisible(false);
});

Ext.Ajax.on('requestexception', function(conn, response){
	var spinner = Ext.get('spinner');
	if (spinner != null)
		spinner.setVisible(false);

	var wndEditUserCfg = {
		title: 'Unhandled Exception!',
		width: 900,
		closable: true,
		html: response.responseText }

	Ext.applyIf(wndEditUserCfg, defModalWndCfg);

	new Ext.Window(wndEditUserCfg).show();
});

function DisableFormButtons(buttons)
{
	Ext.each(buttons, function(btn) { btn.disable(); } )
}

function EnableFormButtons(buttons)
{
	Ext.each(buttons, function(btn) { btn.enable(); } )
}

function ShowInfoMsg(msg){
	ShowInfoMsg(msg, null);
}

function ShowInfoMsg(msg, callback){
	ShowMsg(msg, callback, Ext.Msg.INFO);
}

function ShowErrorMsg(msg){
	ShowErrorMsg(msg, null);
}

function ShowErrorMsg(msg, callback){
	ShowMsg(msg, callback, Ext.Msg.ERROR);
}

function ShowWarningMsg(msg){
	ShowWarningMsg(msg, null);
}

function ShowWarningMsg(msg, callback){
	ShowMsg(msg, callback, Ext.Msg.WARNING);
}

function ShowMsg(msg){
	ShowMsg(msg, null, '');
}

function ShowMsg(msg, callback){
}

function ShowMsg(msg, callback, icon){
	Ext.Msg.show({
		title: '',
		msg: msg,
		fn: callback,
		buttons: Ext.MessageBox.OK,
		multiline: false,
		icon: icon});
}

function GetDateDiff(startDate, endDate, datePart){
	if (datePart == 1){ // days
		var ms = startDate.getElapsed(endDate);
		if (startDate > endDate)
			return Math.floor((0 - (ms / 1000 / 60 / 60 / 24)));
		else
			return Math.floor((ms / 1000 / 60 / 60 / 24));
	}
}

/* Fix, that prevents submitting field's emtpyText value */
Ext.form.Action.Submit.prototype.run = Ext.form.Action.Submit.prototype.run.createInterceptor(function(){
	this.form.items.each(function(item){
		if (item.el.getValue() == item.emptyText){
			item.el.dom.value = '';
		}
	});
});

Ext.form.Action.Submit.prototype.run = Ext.form.Action.Submit.prototype.run.createSequence(function(){
	this.form.items.each(function(item){
		if (item.el.getValue() == '' && item.emptyText){
			item.el.dom.value = item.emptyText;
		}
	});
});
/* ------------------------------------------------------ */

//Ext.form.ComboBox.prototype.editable = false;

// custom dateselect event for datefield
Ext.form.DateField.prototype.menuListeners.select = function(m, newValue){
	var oldValue = this.getValue();
	if (this.fireEvent('dateselect', this, newValue, oldValue)){
		this.setValue(newValue);
		this.getEl().highlight();
	}
}
/* --------------------------------------------------- */

Ext.form.Field.prototype.msgTarget = 'qtip';
Ext.form.Field.prototype.width = 300;
Ext.form.Field.prototype.validationDelay = 0;
Ext.form.Field.prototype.validateOnBlur = false;
Ext.form.DateField.prototype.format = 'n/j/Y';

Ext.grid.GridPanel.prototype.enableHdMenu = false;

var frmDefCfg = 
{
	labelAlign: 'top',
	frame: false,
	width: 300,
	buttonAlign: 'left',
	border: false
}

var defModalWndCfg = 
{
	width: 300,
	modal: true,
	resizable: false,
	closable: false,
	draggable: false,
	border: false,
	bodyBorder: false,
	autoHeight: true
}

/*
	parseUri 1.2.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/

function parseUri (str) {
	var o = parseUri.options,
		m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

var wndHelp;
var wndHelpCfg = {
	header: true,
	closable: true,
	draggable: true,
	stateId: "wndHelp",
	autoHeight: true,
	items: [{
		xtype: "iframepanel",
		id: "helpArea"
	}]
}
Ext.applyIf(wndHelpCfg, defModalWndCfg);

function ShowHelpWindow(url, width, height, title){
	if (width == undefined)
		width = 670;
	if (height == undefined)
		height = 530;
	if (title == undefined)
		title = "TrialSchedule - Help";

	wndHelp = new Ext.Window(wndHelpCfg);
	wndHelp.setTitle(title);
	wndHelp.setWidth(width);
	wndHelp.findById("helpArea").setHeight(height);
	wndHelp.show();

	Ext.getCmp("helpArea").setSrc(url, true);
}
