/**
* Global Ajazx loading screen
*/

Ext.ux.ajaxWithLoading = new Ext.data.Connection({

    listeners : {
        beforerequest : function()
        {
            Ext.MessageBox.show({
               title: 'Please wait',
               progressText: 'Processing Data...',
               width:300,
               progress:true,
               closable:false,
               animEl: 'body'
           });
        },
        requestcomplete : function()
        {
            Ext.MessageBox.hide();
        }
    }
});



//Ext.Ajax.on('beforerequest', ajax_loading_callback, this);
//Ext.Ajax.on('requestcomplete', ajax_loaded_callback, this);



/** Ext.ux.RepSelectBox
 **
 ** 	Extends: ComboBox
 **	Xtype:	 rep_select_box
 **
 ** This is a standard drop down that will provide a list of sub-reps.
 ** Uses document.repList to retrieve list of reps. Will reload
 ** targetStore on select (using parameter sales_rep_id for value) by default, if targetStore is set.
 **/
Ext.ux.RepSelectBox = Ext.extend(Ext.form.ComboBox, {
	mode: 'local',
	triggerAction: 'all',
	displayField: 'rep_name',
	valueField: 'sales_rep_id',
	hiddenName: 'sales_rep_id',
	listClass: 'x-combo-list-small',
	emptyText: 'Myself',
	targetStore: null,
	listeners: {
		'select': function(targetSelect) {
			application_data.target_sales_rep_id = targetSelect.getValue();
			if(this.targetStore != null) {
				this.targetStore.removeAll();
				this.targetStore.reload({ params: {sales_rep_id: application_data.target_sales_rep_id} });
			}
		}
	}
});

Ext.reg('rep_select_box', Ext.ux.RepSelectBox);

/** Ext.ux.RepSelectBox
 **
 ** 	Extends: ComboBox
 **	Xtype:	 rep_select_box
 **
 ** This is a standard drop down that will provide a list of sub-reps.
 ** Uses document.repList to retrieve list of reps. Will reload
 ** targetStore on select (using parameter sales_rep_id for value) by default, if targetStore is set.
 **/
Ext.ux.LeveledRepSelectBox = Ext.extend(Ext.form.ComboBox, {
	mode: 'local',
	triggerAction: 'all',
	displayField: 'rep_name',
	valueField: 'sales_rep_id',
	hiddenName: 'sales_rep_id',
	listClass: 'x-combo-list-small',
	emptyText: 'Myself',
	targetStore: null,
	listeners: {
		'select': function(targetSelect) {
			application_data.target_sales_rep_id = targetSelect.getValue();
			if(this.targetStore != null) {
				this.targetStore.removeAll();
				this.targetStore.reload({ params: {sales_rep_id: application_data.target_sales_rep_id} });

				this.store.reload({params: {sales_rep_id: application_data.target_sales_rep_id}});
			}
		}
	}
});

Ext.reg('leveled_rep_select_box', Ext.ux.LeveledRepSelectBox);

/** Ext.ux.ResSelectBox
 **
 ** Extends: ComboBox
 ** Xtype: res_select_box
 **
 ** Same as rep_select_box, but with Res ID
 **/
Ext.ux.ResSelectBox = Ext.extend(Ext.form.ComboBox, {
	mode: 'local',
	width: 115,
	triggerAction: 'all',
	displayField: 'reservation_display',
	valueField: 'reservation_id',
	listClass: 'x-combo-list-small',
	emptyText: 'Select a reservation',
	targetStore: null,
	listeners: {
		'select': function(targetSelect) {
			application_data.target_reservation_id = targetSelect.getValue();
			if(this.targetStore != null) {
				this.targetStore.reload({ params: {reservation_id: application_data.target_reservation_id } });
			}
		}
	}
});

Ext.reg('res_select_box', Ext.ux.ResSelectBox);

Ext.reg('rep_select_box', Ext.ux.RepSelectBox);
/** Ext.ux.ResSelectBox
 **
 ** Extends: ComboBox
 ** Xtype: res_select_box
 **
 ** Same as rep_select_box, but with Res ID
 **/
Ext.ux.LeaderResSelectBox = Ext.extend(Ext.form.ComboBox, {
	mode: 'local',
	width: 115,
	triggerAction: 'all',
	displayField: 'group_leader_name',
	hiddenField: 'group_leader_name',
	valueField: 'reservation_id',
	listClass: 'x-combo-list-small',
	emptyText: 'Select a reservation',
	targetStore: null,
	listeners: {
		'select': function(targetSelect) {
			application_data.target_reservation_id = targetSelect.getValue();
			if(this.targetStore != null) {
				this.targetStore.reload({ params: {reservation_id: application_data.target_reservation_id } });
			}
		}
	}
});

Ext.reg('leader_res_select_box', Ext.ux.LeaderResSelectBox);

/** Ext.ux.GroupBySelect
 **
 ** 	Extends: ComboBox
 **	Xtype: 	 group_select_box
 **
 ** This is the template for a dropdown that will change grouping for GroupViews.
 ** Does not have a default data store defined.
 ** 'targetStore' is the Ext.GroupingStore to be regrouped on select.
 **/
Ext.ux.GroupBySelect = Ext.extend(Ext.form.ComboBox, {
	mode:          'local',
	width: 115,
	editable:      false,
	triggerAction: 'all',
	displayField:  'group_name',
	valueField:    'group_value',
	listClass:     'x-combo-list-small',
	emptyText:     'Default',
	targetStore:   null,
	listeners:     {
		'select': function (targetSelect) {
			if(this.targetStore != null) {
				this.targetStore.groupBy(targetSelect.getValue(), true);
			}
		}
	}
});
Ext.reg('group_select_box', Ext.ux.GroupBySelect);

Ext.ux.SchoolSelectBox = Ext.extend(Ext.form.ComboBox, {
	mode: 'remote',
	displayField: 'display',
	valueField: 'value',
	listClass: 'x-combo-list-small',
	emptyText: 'Enter a School',
	targetStore: null,
	store: null
});
Ext.reg('school_select_box', Ext.ux.SchoolSelectBox);

Ext.ux.SchoolSelectBoxLocal = Ext.extend(Ext.form.ComboBox, {
	mode: 'local',
	displayField: 'display',
	valueField: 'value',
	listClass: 'x-combo-list-small',
	emptyText: 'Enter a School',
	targetStore: null,
	store: null
});
Ext.reg('school_select_box_local', Ext.ux.SchoolSelectBoxLocal);

Ext.ux.ActivityTypeSelectBox = Ext.extend(Ext.form.ComboBox, {
	mode: 'local',
	displayField: 'title',
	hiddenField: 'title',
	valueField: 'id',
	listClass: 'x-combo-list-small',
	editable: false,
	emptyText: 'Select a Type',
	triggerAction: 'all',
	targetStore: null
});
Ext.reg('activity_type_select_box', Ext.ux.ActivityTypeSelectBox);


Ext.ux.SupplyNumberBoxStupid = Ext.extend(Ext.form.ComboBox, {
	mode: 'local',
	triggerAction: 'all',
	displayField: 'display',
	valueField: 'display',
	hiddenField: 'display',
	allowBlank: false,
	emptyText: 'Select Quantity',
	maskRe: /[0-9]/,
	store: new Ext.data.SimpleStore({
		fields: ['display'],
		data: [
			['50']
		]
	})
});
Ext.reg('stupid_supply_number_select', Ext.ux.SupplyNumberBoxStupid);


Ext.ux.SupplyNumberBoxLarge = Ext.extend(Ext.form.ComboBox, {
	mode: 'local',
	triggerAction: 'all',
	displayField: 'display',
	valueField: 'display',
	hiddenField: 'display',
	allowBlank: false,
	allowBlank: false,
	emptyText: 'Select Quantity',
	maskRe: /[0-9]/,
	store: new Ext.data.SimpleStore({
		fields: ['display'],
		data: [
			['0'],
			['10'],
			['25'],
			['50'],
			['75'],
			['100'],
			['150'],
			['200'],
			['250'],
			['300'],
			['400'],
			['500']
		]
	})
});
Ext.reg('large_supply_number_select', Ext.ux.SupplyNumberBoxLarge);

Ext.ux.SupplyNumberBoxSmall = Ext.extend(Ext.form.ComboBox, {
	mode: 'local',
	triggerAction: 'all',
	displayField: 'display',
	valueField: 'display',
	hiddenField: 'display',
	allowBlank: false,
	emptyText: 'Select Quantity',
	editable: false,
	maskRe: /[0-9]/,
	store: new Ext.data.SimpleStore({
		fields: ['display'],
		data: [
			['0'],
			['1'],
			['2'],
			['3'],
			['4'],
			['5'],
			['6'],
			['7'],
			['8'],
			['9'],
			['10']
		]
	})
});
Ext.reg('small_supply_number_select', Ext.ux.SupplyNumberBoxSmall);

Ext.ux.StateSelectBox = Ext.extend(Ext.form.ComboBox, {
	mode: 'local',
	triggerAction: 'all',
	editable: false,
	emptyText: 'Choose a State',
	store: new Ext.data.SimpleStore({
		fields: ['initials', 'name'],
		data: [
	      	['AL', 'Alabama'],
			['AK', 'Alaska'],
			['AZ', 'Arizona'],
			['AR', 'Arkansas'],
			['CA', 'California'],
			['CO', 'Colorado'],
			['CT', 'Connecticut'],
			['DE', 'Delaware'],
			['DC', 'District of Columbia'],
			['FL', 'Florida'],
			['GA', 'Georgia'],
			['HI', 'Hawaii'],
			['ID', 'Idaho'],
			['IL', 'Illinois'],
			['IN', 'Indiana'],
			['IA', 'Iowa'],
			['KS', 'Kansas'],
			['KY', 'Kentucky'],
			['LA', 'Louisiana'],
			['ME', 'Maine'],
			['MD', 'Maryland'],
			['MA', 'Massachusetts'],
			['MI', 'Michigan'],
			['MN', 'Minnesota'],
			['MS', 'Mississippi'],
			['MO', 'Missouri'],
			['MT', 'Montana'],
			['NE', 'Nebraska'],
			['NV', 'Nevada'],
			['NH', 'New Hampshire'],
			['NJ', 'New Jersey'],
			['NM', 'New Mexico'],
			['NY', 'New York'],
			['NC', 'North Carolina'],
			['ND', 'North Dakota'],
			['OH', 'Ohio'],
			['OK', 'Oklahoma'],
			['OR', 'Oregon'],
			['PA', 'Pennsylvania'],
			['RI', 'Rhode Island'],
			['SC', 'South Carolina'],
			['SD', 'South Dakota'],
			['TN', 'Tennessee'],
			['TX', 'Texas'],
			['UT', 'Utah'],
			['VT', 'Vermont'],
			['VA', 'Virginia'],
			['WA', 'Washington'],
			['WV', 'West Virginia'],
			['WI', 'Wisconsin'],
			['WY', 'Wyoming']
		]
	}),
	valueField: 'initials',
	displayField: 'name',
	hiddenName: 'state'
});
Ext.reg('state_select_box', Ext.ux.StateSelectBox);



Ext.ux.GreekSelectBox = Ext.extend(Ext.form.ComboBox, {
	mode: 'local',
	width: 115,
	triggerAction: 'all',
	displayField: 'display',
	valueField: 'value',
	listClass: 'x-combo-list-small',
	emptyText: 'Select a Greek Organization',
	targetStore: null,
	listeners: {
		'select': function(targetSelect) {
			application_data.target_reservation_id = targetSelect.getValue();
			if(this.targetStore != null) {
				this.targetStore.reload({ params: {reservation_id: application_data.target_reservation_id } });
			}
		}
	}
});

Ext.reg('greek_select_box', Ext.ux.GreekSelectBox);

Ext.ux.NoteTypeSelectBox = Ext.extend(Ext.form.ComboBox, {
	mode: 'local',
	width: 115,
	triggerAction: 'all',
	displayField: 'display',
	valueField: 'value',
	listClass: 'x-combo-list-small',
	emptyText: 'Select a Note Type',
	targetStore: null
});

Ext.reg('note_type_select_box', Ext.ux.NoteTypeSelectBox);


//CUSTOM FUNCTIONS
function get_cookie(name)
{
	var pos = document.cookie.indexOf(name + '=');

	if(pos == -1)
	{
		return false;
	}
	else
	{
		var pos2 = document.cookie.indexOf(';', pos);

		if(pos2 == -1)
		{
			return unescape(document.cookie.substring(pos + name.length + 1));
		}
		else
		{
			return unescape(document.cookie.substring(pos + name.length + 1, pos2));
		}
	}
}

function set_cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function delete_cookie( name, path, domain ) {
if ( get_cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function open_window(url, window_name)
{
    try
    {
        var mywin = window.open(url, window_name);
    }
    catch (ignored)
    {
        return false;
    }
    return mywin;
}
