Ext.namespace('Ext.ux');

Ext.ux.RRC = function(config) {
	Ext.apply(this, config);
	Ext.ux.RRC.superclass.constructor.call(this);
};

Ext.extend(Ext.ux.RRC, Ext.util.Observable, {
	
	states: [
		['AL', 'Alabama', 'The Heart of Dixie'],
		['AK', 'Alaska', 'The Land of the Midnight Sun'],
		['AZ', 'Arizona', 'The Grand Canyon State'],
		['AR', 'Arkansas', 'The Natural State'],
		['CA', 'California', 'The Golden State'],
		['CO', 'Colorado', 'The Mountain State'],
		['CT', 'Connecticut', 'The Constitution State'],
		['DE', 'Delaware', 'The First State'],
		['DC', 'District of Columbia', "The Nation's Capital"],
		['FL', 'Florida', 'The Sunshine State'],
		['GA', 'Georgia', 'The Peach State'],
		['HI', 'Hawaii', 'The Aloha State'],
		['ID', 'Idaho', 'Famous Potatoes'],
		['IL', 'Illinois', 'The Prairie State'],
		['IN', 'Indiana', 'The Hospitality State'],
		['IA', 'Iowa', 'The Corn State'],
		['KS', 'Kansas', 'The Sunflower State'],
		['KY', 'Kentucky', 'The Bluegrass State'],
		['LA', 'Louisiana', 'The Bayou State'],
		['ME', 'Maine', 'The Pine Tree State'],
		['MD', 'Maryland', 'Chesapeake State'],
		['MA', 'Massachusetts', 'The Spirit of America'],
		['MI', 'Michigan', 'Great Lakes State'],
		['MN', 'Minnesota', 'North Star State'],
		['MS', 'Mississippi', 'Magnolia State'],
		['MO', 'Missouri', 'Show Me State'],
		['MT', 'Montana', 'Big Sky Country'],
		['NE', 'Nebraska', 'Beef State'],
		['NV', 'Nevada', 'Silver State'],
		['NH', 'New Hampshire', 'Granite State'],
		['NJ', 'New Jersey', 'Garden State'],
		['NM', 'New Mexico', 'Land of Enchantment'],
		['NY', 'New York', 'Empire State'],
		['NC', 'North Carolina', 'First in Freedom'],
		['ND', 'North Dakota', 'Peace Garden State'],
		['OH', 'Ohio', 'The Heart of it All'],
		['OK', 'Oklahoma', 'Oklahoma is OK'],
		['OR', 'Oregon', 'Pacific Wonderland'],
		['PA', 'Pennsylvania', 'Keystone State'],
		['RI', 'Rhode Island', 'Ocean State'],
		['SC', 'South Carolina', 'Nothing Could be Finer'],
		['SD', 'South Dakota', 'Great Faces, Great Places'],
		['TN', 'Tennessee', 'Volunteer State'],
		['TX', 'Texas', 'Lone Star State'],
		['UT', 'Utah', 'Salt Lake State'],
		['VT', 'Vermont', 'Green Mountain State'],
		['VA', 'Virginia', 'Mother of States'],
		['WA', 'Washington', 'Green Tree State'],
		['WV', 'West Virginia', 'Mountain State'],
		['WI', 'Wisconsin', "America's Dairyland"],
		['WY', 'Wyoming', 'Like No Place on Earth']
	],
	
	cccard: [
		['Visa'],
		['Mastercard'],
		['Discover']
	],
	
	ccmonths: [
		['01'],
		['02'],
		['03'],
		['04'],
		['05'],
		['06'],
		['07'],
		['08'],
		['09'],
		['10'],
		['11'],
		['12']
	],

	ccyears: function() {
		var start = 2011;
		var years = new Array();
		
		for(var i = 0; i < 8; i++) {
			var year	= new Array();
			year[0]	= start + i;
			years[i]	= year;
		}
		
		return years;
	},
	
	paymentAcctNoValidate: function() {
		var part1 	= Ext.getCmp('account_number_1').getValue().toString();
		var part2 	= Ext.getCmp('account_number_2').getValue().toString();
		var submit	= true;
		
		if(part1.length != 2) {
			submit = false;
		}
		
		if(part2.length != 9) {
			submit = false;
		}
		
		if(!submit) {
			
			Ext.Msg.show({
				title:'Account Number',
				msg: 'The Account Number must be in XX-XXXXXXXXX with 2 digits for the 1st part and 9 digits for the ' +
					'2nd part.  Please review the sample statement and red circled area for an example ' +
					'account number.',
				buttons: Ext.Msg.OK,
				icon: Ext.MessageBox.WARNING
			});
			return false;
		} else {
			Ext.get('acctp1').dom.value = part1;
			Ext.get('acctp2').dom.value = part2;
			return true;
		}
	},
	
	paymentAcctValidate: function() {

		var form = Ext.getCmp('payment_panel_renderer');
		if(form){ // make sure they clicked a payment type
			if(form.getForm().isValid()){ // validate the form data

				var form_values = form.getForm().getValues(false);
				if(form_values.agree != undefined && form_values.agree == 1){ // validate the agree checkbox

					this.paymentSummary(form, form_values);

				} else {
					Ext.MessageBox.show({
						title: 'Agree to convience fee',
						msg: 'You must check the box \"I understand and agree to a $4.95 convenience fee. This amount will be added to my total payment.\"',
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.WARNING
					});
				}
			}
		} else {
			Ext.MessageBox.show({
				title: 'Select Payment Type',
				msg: 'Select a payment type to continue.',
				buttons: Ext.MessageBox.OK,
				icon: Ext.MessageBox.WARNING
			});
		}
	},
	
	paymentSummary: function(form, form_values) {

		var summary_items = [];
		
		var j = 0;
		for(var i in form_values){

			if(i == 'type') continue;

			if(i == 'agree') {
				summary_items[j] = {html: 'Convenience Fee:', 'style':'text-align:right;font-weight:bold;'};
				j++;
				summary_items[j] = {html: Ext.getCmp(i).fieldLabel};
				j++;
			} else {
				summary_items[j] = {'html': Ext.getCmp(i).fieldLabel+':', 'style':'text-align:right;font-weight:bold;'};
				j++;
				if(i == 'payment'){
					summary_items[j] = {'html': '$'+this.number_format(form_values[i], 2)};
				} else {
					summary_items[j] = {'html': form_values[i]};
				}
				j++;
			}
		}

		var win = new Ext.Window({
			title: 'Payment Summary',
			id: 'payment_summary_window',
			width: 450,
			height: 550,
			autoScroll: true,
			closable: false,
			frame: true,
			modal: true,
			items: new Ext.Panel({
				layout: 'table',
				frame: true,
				defaults: {
					bodyStyle:'padding:5px;color:black;border:none;'
				},
				layoutConfig: {
					columns: 2
				},
				items: summary_items
			}),
			tbar: [{
				text: 'Submit Payment',
				iconCls: 'icon_accept',
				handler: function() {
					win.close();
					rrc.paymentCommit();
				}
			}, '-', {
				text: 'Make payment changes',
				iconCls: 'icon_page_edit',
				handler: function() {
					win.close();
				}
			}, '-', {
				text: 'Cancel payment',
				iconCls: 'icon_cancel',
				handler: function() {
					win.close();
					window.location = "index.php";
					
				}
			}]
		});
		win.show();
	},
	
	paymentCommit: function() {

		var form = Ext.getCmp('payment_panel_renderer');

		form.getForm().submit({
			url: '_ajax/payment.submit.php',
			params: {
				account_number: Ext.getDom('account_number').value
			},
			method: 'POST',
			waitMsg: 'Submitting Payment...',
			success: function(form, action) {
				if(action.result.error == undefined){
					Ext.MessageBox.show({
						title: 'Thank you',
						msg: 'Your payment has been submitted.',
						buttons: Ext.MessageBox.OK,
						fn: function(btn, text){
							window.location = "index.php";
						},
						icon: Ext.MessageBox.INFO
					});
				} else {
					Ext.MessageBox.show({
						title: 'Sorry',
						msg: action.result.error,
						buttons: Ext.MessageBox.OK,
						fn: function(btn, text){
							window.location = "index.php";
						},
						icon: Ext.MessageBox.ERROR
					});
				}
			},
			failure: function(form, action) {
				Ext.MessageBox.show({
					title: 'Sorry',
					msg: 'An error occurred and your payment has NOT been submitted.<br />Please try again later.',
					buttons: Ext.MessageBox.OK,
					fn: function(btn, text){
						window.location = "index.php";
					},
					icon: Ext.MessageBox.ERROR
				});
			}
		});
	},
	
	paymentLoadPanel: function(type) {

		Ext.Ajax.request({
			url: '_ajax/loader.php',
			params: {
				script: 'payment.panel',
				type: type
			},
			waitMsg: 'Loading...',
			success: function(r,o) {
				eval(r.responseText);
			},
			failure: function(r,o) {
				
			},
			scripts: true
		});
	},

	number_format: function( number, decimals, dec_point, thousands_sep ) {
		// Format a number with grouped thousands
		// 
		// +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
		// +       version: 809.2411
		// +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
		// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +     bugfix by: Michael White (http://getsprink.com)
		// +     bugfix by: Benjamin Lupton
		// +     bugfix by: Allan Jensen (http://www.winternet.no)
		// +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
		// +     bugfix by: Howard Yeend
		// *     example 1: number_format(1234.5678, 2, '.', '');
		// *     returns 1: 1234.57     
	
		var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
		var d = dec_point == undefined ? "." : dec_point;
		var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
		var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
		
		return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
	},

	substr: function( f_string, f_start, f_length ) {
		// Return part of a string
		// 
		// +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_substr/
		// +       version: 810.819
		// +     original by: Martijn Wieringa
		// +     bugfixed by: T.Wild
		// +      tweaked by: Onno Marsman
		// *       example 1: substr('abcdef', 0, -1);
		// *       returns 1: 'abcde'
		// *       example 2: substr(2, 0, -6);
		// *       returns 2: ''
	
		f_string += '';
	
		if(f_start < 0) {
			f_start += f_string.length;
		}
	
		if(f_length == undefined) {
			f_length = f_string.length;
		} else if(f_length < 0){
			f_length += f_string.length;
		} else {
			f_length += f_start;
		}
	
		if(f_length < f_start) {
			f_length = f_start;
		}
	
		return f_string.substring(f_start, f_length);
	}

});
