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;
}

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"
};

// do not initialize any event before this
Ext.onReady(function()
{
	Ext.QuickTips.init();
	
	Ext.apply(Ext.QuickTips.getQuickTip(), {
		dismissDelay: 10000,
		maxWidth: 200,
		minWidth: 100,
		showDelay: 50,
		trackMouse: true
	});

	Ext.form.Field.prototype.msgTarget = 'side';
	Ext.form.Field.prototype.selectOnFocus = true;
	Ext.form.Field.prototype.validationEvent = 'blur';

	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);
	}
});

// 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 name 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 numeric validator
Ext.apply(Ext.form.VTypes,
	{'numeric':function()
			{
				var re = /^[\d]*$/;
				return function(value) { return re.test(value); }
			}(),
	'numericText':'This value contains invalid characters. Only numbers allowed.',
	'numericMask':/[\d]/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 -- per matt bunch allow any characters
Ext.apply(Ext.form.VTypes,
	{'zipcode':function()
			{
				var re = /^.+$/;
				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.'
});

// custom phonenumber validatorv -- per matt bunch allow any characters
Ext.apply(Ext.form.VTypes,
	{'phonenumber':function()
			{
				var re = /^.+$/;
				return function(value) { return re.test(value); }
			}(),
	'phonenumberText':'This does not appear to be a valid phone number. Must be in the format "+1 (555)555-5555 x555 with the country code and extension being optional."'
});

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;
				}
			}
	}
);
