Ext.BLANK_IMAGE_URL = '/js/ext/resources/images/default/s.gif';

function $(element) {
	var e = Ext.get(element);
	return e ? e.dom : null;
}

function $F(element) {
	return $(element).value;
}

// do not initialize any event before this
Ext.EventManager.onDocumentReady(function() {
	Ext.QuickTips.init();
	Ext.EventManager.on('aspnetForm', 'submit', Ext.form.FormPanel.validate);
	
	if (typeof(pageInit) == 'function') { pageInit(); }
	if (typeof(controlsInit) == 'function') { controlsInit(); }
	if (typeof(pageLoad) == 'function') { pageLoad(); }
	if (typeof(validateForm) == 'function') { Ext.EventManager.on('aspnetForm', 'submit', validateForm); }
});
	
Ext.apply(Ext.QuickTips.getQuickTip(), {
	dismissDelay: 10000,
	maxWidth: 200,
	minWidth: 100,
	showDelay: 50,
	trackMouse: true
});

Ext.apply(Ext.form.Field.prototype, {
	msgTarget: 'side',
	selectOnFocus: true,
	validationEvent: 'blur'
});

// custom alphanumeric validator
Ext.apply(Ext.form.VTypes, {
	'alphanumeric': function() {
				var re = /^[\dA-Za-z\s\.\-']*$/;
				return function(value) { return re.test(value); }
			}(),
	'alphanumericText':'This value contains invalid characters. Only letters, numbers, -, \, and spaces are allowed.',
	'alphanumericMask':/[\dA-Za-z\s\.\-']/i
});

// custom alpha validator
Ext.apply(Ext.form.VTypes, {
	'alphastrict': function() {
				var re = /^[A-Za-z]*$/;
				return function(value) { return re.test(value); }
			}(),
	'alphastrictText':'This value contains invalid characters. Only letters are allowed.',
	'alphastrictMask':/[A-Za-z]/i
});

// custom alpha validator
Ext.apply(Ext.form.VTypes, {
	'name': function() {
				var re = /^[A-Za-z\s'-]*$/;
				return function(value) { return re.test(value); }
			}(),
	'nameText':'This value contains invalid characters. Only letters, \', -, or spaces are allowed.',
	'nameMask':/[A-Za-z\s'-]/i
});

// custom time validator
Ext.apply(Ext.form.VTypes, {
	'time': function() {
				var re = /^([1-9]|1[0-9]):([0-5][0-9])(\s[A|P]M|[A|P]M)$/;
				return function(value) { return re.test(value); }
			}(),
	'timeText':'Not a valid time. Must be in the format "12:35 PM".'
});

// custom money validator
Ext.apply(Ext.form.VTypes, {
	'money': function() {
				var re = /^\$?\d+(,\d{3})*(\.\d{2})?$/;
				return function(value) { return re.test(value); }
			}(),
	'moneyText':'Not a valid amount. Must be in the format "$0.00".',
	'moneyMask':/[\$\d\.\,]/i
});

// custom zipcode validator
Ext.apply(Ext.form.VTypes, {
	'zipcode': function() {
				var re = /^\d{5}$/;
				return function(value) { return re.test(value); }
			}(),
	'zipcodeText':'This does not appear to be a valid zip code. Please enter your 5 digit zip code.',
	'zipcodeMask':/[\$\d]/i
});

// custom phonenumber validator
Ext.apply(Ext.form.VTypes, {
	'phonenumber': function() {
				var re = /^\(\d{3}\)\s?\d{3}-\d{4}(\sx\d+)?$/;
				var re2 = /^\d{3}-\d{3}-\d{4}(\sx\d+)?$/;
				var re3 = /^\d{3}.\d{3}.\d{4}(\sx\d+)?$/;
				return function(value) { return re.test(value) || re2.test(value) || re3.test(value); }
			}(),
	'phonenumberText':'This does not appear to be a valid phone number. Must be in the format "(555)555-5555 x555"',
	'phonenumberMask':/[\d\()\s-x.]/i
});

Ext.apply(Ext.form.FormPanel, {
	'controls': new Ext.util.MixedCollection(),
	'validate': function (e) {				
			var controlToFocus;
			Ext.form.FormPanel.controls.each(function(el) {						
				if (!el.validate())	{
					if (e) { e.preventDefault(); }
					if (controlToFocus == null) { controlToFocus = el; }
				}
			});
			
			if (controlToFocus) {
				controlToFocus.focus(true);
				return false;
			} else {
				// clear out empty text in case form is progromatically submitted
				Ext.form.FormPanel.controls.each(function(el) {
					if (el.getValue() == '' || el.getRawValue() == el.emptyText) { el.setRawValue(''); }
				});
				return true;
			}
		}
	}
);

Date.patterns = {
    ISO8601Long:"Y-m-d H:i:s",
    ISO8601Short:"Y-m-d",
    ShortDate: "n/j/Y",
    LongDate: "l, F d, Y",
    FullDateTime: "l, F d, Y g:i:s A",
    MonthDay: "F d",
    ShortTime: "g:i A",
    LongTime: "g:i:s A",
    SortableDateTime: "Y-m-d\\TH:i:s",
    UniversalSortableDateTime: "Y-m-d H:i:sO",
    YearMonth: "F, Y"
};

Ext.Ajax.on('beforerequest', function (conn, options) {
	// show cool busy widget here...
});

Ext.Ajax.on('requestcomplete', function (conn, response, options) {
	// hide cool busy widget here
});
