/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */
function dump(arr,level) {
	var dumped_text = "";
	if	(!level) {level = 0;}

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j+=1){ level_padding += "    ";}

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];

			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

//Date
var dateVar = new Date();
var month = dateVar.getMonth() + 1;
var monthStr;
if (month < 10){
	monthStr = '0' + month;
}
else {
	monthStr = '' + month;
}
var dayOfMonth = dateVar.getDate();
var dayOfMonthStr;
if (dayOfMonth < 10){
	dayOfMonthStr = '0' + dayOfMonth;
}
else {
	dayOfMonthStr = '' + dayOfMonth;
}
var dateStr = dateVar.getFullYear() + '-' + monthStr + '-' + dayOfMonthStr;

//Func called by html button
var subscribeWindowOpen;

Ext.onReady(function(){
	Ext.QuickTips.init();

	subscribeWindowOpen = function(firstName, email, sampleReq){

		var provinceOrStateComboBox = new Ext.form.ComboBox({
			fieldLabel: 'State or Province',
			labelStyle: 'color: black',
			id: 'samplesStateOrProvince',
			displayField: 'state',
			valueField: 'stateValue',
			width: 127,
			mode: 'local',
			editable: true,
			allowBlank: false,
			forceSelection: true,
			triggerAction: 'all',
			store: new Ext.data.SimpleStore({
				fields: ['stateValue', 'state'],
				data: [
					[2, 'Alberta'],
					[3, 'British Columbia'],
					[4, 'Manitoba'],
					[5, 'New Brunswick'],
					[6, 'Newfoundland'],
					[7, 'Northwest Territories'],
					[8, 'Nunavut'],
					[9, 'Nova Scotia'],
					[10, 'Ontario'],
					[11, 'Prince Edward Island'],
					[12, 'Quebec'],
					[13, 'Saskatchewan'],
					[14, 'Yukon'],
					[15, 'Alabama'],
					[16, 'Alaska'],
					[17, 'Arizona'],
					[18, 'Arkansas'],
					[19, 'California'],
					[20, 'Colorado'],
					[21, 'Connecticut'],
					[22, 'Delaware'],
					[23, 'District of Columbia'],
					[24, 'Florida'],
					[25, 'Georgia'],
					[26, 'Hawaii'],
					[27, 'Idaho'],
					[28, 'Illinois'],
					[29, 'Indiana'],
					[30, 'Iowa'],
					[31, 'Kansas'],
					[32, 'Kentucky'],
					[33, 'Louisiana'],
					[34, 'Maine'],
					[35, 'Maryland'],
					[36, 'Massachusetts'],
					[37, 'Michigan'],
					[38, 'Minnesota'],
					[39, 'Mississippi'],
					[40, 'Missouri'],
					[41, 'Montana'],
					[42, 'Nebraska'],
					[43, 'Nevada'],
					[44, 'New Hampshire'],
					[45, 'New Jersey'],
					[46, 'New Mexico'],
					[47, 'New York'],
					[48, 'North Carolina'],
					[49, 'North Dakota'],
					[50, 'Ohio'],
					[51, 'Oklahoma'],
					[52, 'Oregon'],
					[53, 'Pennsylvania'],
					[54, 'Rhode Island'],
					[55, 'South Carolina'],
					[56, 'South Dakota'],
					[57, 'Tennessee'],
					[58, 'Texas'],
					[59, 'Utah'],
					[60, 'Vermont'],
					[61, 'Virginia'],
					[62, 'Washington'],
					[63, 'West Virginia'],
					[64, 'Wisconsin'],
					[65, 'Wyoming']
				]
			})
		});

		var sampleCheckbox = new Ext.form.Checkbox({
			fieldLabel: 'Request Samples',
			labelStyle: 'color: black',
			id: 'subscribeReqSamples',
			checked: false
		});

		if (sampleReq == true){
			var panelSamples = new Ext.FormPanel({
				id: 'samplesPanel',
				defaultType: 'textfield',
				monitorValid: true,
				frame: true,
				items: [{
					fieldLabel: 'Email',
					id: 'samplesEmail',
					labelStyle: 'color: black',
					value: email,
					allowBlank: false
				},{
					fieldLabel: 'First Name',
					id: 'samplesFirstName',
					labelStyle: 'color: black',
					value: firstName,
					allowBlank: false
				},{
					fieldLabel: 'Last Name',
					labelStyle: 'color: black',
					id: 'samplesLastName',
					allowBlank: false
				},{
					fieldLabel: 'Company',
					labelStyle: 'color: black',
					id: 'samplesCompany'
				},{
					fieldLabel: 'Unit Number',
					labelStyle: 'color: black',
					id: 'samplesUnitNumber'
				},{
					fieldLabel: 'Street Address',
					labelStyle: 'color: black',
					id: 'samplesStreetAddress',
					allowBlank: false
				},{
					fieldLabel: 'City',
					labelStyle: 'color: black',
					id: 'samplesCity',
					allowBlank: false
				},
					provinceOrStateComboBox
				,{
					fieldLabel: 'Postal Code',
					labelStyle: 'color: black',
					id: 'samplesPostalCode',
					allowBlank: false
				},
					new Ext.form.ComboBox({
						fieldLabel: 'Country',
						labelStyle: 'color: black',
						id: 'samplesCountry',
						displayField: 'country',
						valueField: 'countryValue',
						width: 127,
						mode: 'local',
						editable: true,
						forceSelection: true,
						triggerAction: 'all',
						allowBlank: false,
						store: new Ext.data.SimpleStore({
							fields: ['countryValue', 'country'],
							data: [
								['2', 'Canada'],
								['3', 'USA']
							]
						})
				}),{
					fieldLabel: 'Fax',
					labelStyle: 'color: black',
					id: 'samplesFax'
				},{
					fieldLabel: 'Phone',
					labelStyle: 'color: black',
					id: 'samplesPhone',
					allowBlank: false
				},
					new Ext.form.Checkbox({
						fieldLabel: 'Request Pen Samples',
						labelStyle: 'color: black',
						id: 'samplesReqPens'
					})
				,
					new Ext.form.Checkbox({
						fieldLabel: 'Request Calendar Samples',
						labelStyle: 'color: black',
						id: 'samplesReqCalendar'
					})
				,
					new Ext.form.Checkbox({
						fieldLabel: 'Request Magnet Samples',
						labelStyle: 'color: black',
						id: 'samplesReqMagnet'
					})
				],
				buttons: [{
					id: 'samplesSubmitButton',
					text: 'Submit and Request Samples',
					formBind: true,
					handler: function(){
						//We need different sets of params depending on the number of samples
						//the user wants
						requestSamplePens = 'off';
						requestSampleCalendars = 'off';
						requestSampleMagnets = 'off';

						if (panelSamples.getComponent('samplesReqPens').getValue() === true){
							requestSamplePens = 'on';
						}
						if (panelSamples.getComponent('samplesReqCalendar').getValue() === true){
							requestSampleCalendars = 'on';
						}
						if (panelSamples.getComponent('samplesReqMagnet').getValue() === true){
							requestSampleMagnets = 'on';
						}

						samplesParams = {
							attribute13: panelSamples.getComponent('samplesFirstName').getValue(),
							attribute14: panelSamples.getComponent('samplesLastName').getValue(),
							attribute15: panelSamples.getComponent('samplesCompany').getValue(),
							attribute17: panelSamples.getComponent('samplesUnitNumber').getValue(),
							attribute18: panelSamples.getComponent('samplesStreetAddress').getValue(),
							attribute19: panelSamples.getComponent('samplesCity').getValue(),
							attribute20: '' + panelSamples.getComponent('samplesStateOrProvince').getValue(),//this is the province id
							attribute21: panelSamples.getComponent('samplesPostalCode').getValue(),
							attribute22: panelSamples.getComponent('samplesCountry').getValue(),
							attribute23: panelSamples.getComponent('samplesFax').getValue(),
							attribute24: panelSamples.getComponent('samplesPhone').getValue(),
							attribute25: '1', //id too
							attribute30: requestSamplePens,
							attribute31: requestSampleCalendars,
							attribute32: requestSampleMagnets,
							email: panelSamples.getComponent('samplesEmail').getValue(),
							emailconfirm: panelSamples.getComponent('samplesEmail').getValue(),
							htmlemail: '1',
							attribute16: 'on',
							attribute29: dateStr,
							'list[62]': "signup",
							'listname[62]': "RLPromo Mailing List With Samples",
							VerificationCodeX: '',
							subscribe: 'Subscribe and Request Samples'
						}

						Ext.Ajax.request({
							params: samplesParams,
							url: 'http://www.rlpromo.com/phplist/?p=subscribe&id=14',
							timeout: 60000,
							method: 'POST',
							callback: function(options,success,response){
								if (success === true){
									Ext.Msg.alert('Thanks for Subscribing',
									'Upon email confirmation ' +
									'a phone call will be made during regular ' +
									'business hours confirming your sample request');
									winSamples.close();
								}
								else {
									Ext.Msg.alert('Sorry! There was an error with our database',
									'Please email us at sales@pmbrand.com for samples or info');
									winSamples.close();
								}
							}
						});
					}
				}]
			});

			var winSamples = new Ext.Window({
				title: 'Please Enter Additional Info',
				x: 450,
				y: 100,
				width: 300,
				height: 530,
				layout: 'fit',
				closable: true,
				collapsible: false,
				resizable: false,
				draggable: true,
				items: [panelSamples]
			});
			winSamples.show();
		}
		else {
			Ext.Ajax.request({
				params: {
					attribute13: firstName,
					email: email,
					emailconfirm: email,
					htmlemail: '1',
					attribute16: 'on',
					attribute29: dateStr,
					'list[61]': "signup",
					'listname[61]': "RLPromo Mailing List",
					VerificationCodeX: '',
					subscribe: 'Subscribe to the Selected Newsletters'
				},
				url: 'http://www.rlpromo.com/phplist/?p=subscribe&id=13',
				timeout: 60000,
				method: 'POST',
				callback: function(options,success,response){
					if (success === true){
						Ext.Msg.alert('Thanks for Subscribing',
						'A confirmation email will be sent shortly');
					}
					else {
						Ext.Msg.alert('Sorry! There was an error with our database',
						'Please email us at sales@pmbrand.com for samples or info');
					}
				}
			});
		}
	};
});


/*
var panelSubscribe = new Ext.FormPanel({
	id: 'subscribePanel',
	defaultType: 'textfield',
	monitorValid: true,
	frame: true,
	labelWidth: 25,
	items: [{
		fieldLabel: 'First Name',
		labelStyle: 'color: black',
		id: 'subscribeFirstName',
		width: 75,
		allowBlank: false,
		regex: /^[A-Za-z]+$\b/,
		regexText: 'Invalid Name - First Name Only'
	},{
		fieldLabel: 'Email',
		labelStyle: 'color: black',
		id: 'subscribeEmail',
		width: 75,
		allowBlank: false,
		regex: /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/,
		regexText: 'Invalid Email'
	},
		sampleCheckbox
	],
	buttons: [{
		id: 'subscribeSubmitButton',
		text: 'Submit',
		formBind: true,
		labelWidth: 200,
		handler: function(){
			if (sampleCheckbox.getValue() === true){
				var panelSamples = new Ext.FormPanel({
					id: 'samplesPanel',
					defaultType: 'textfield',
					monitorValid: true,
					frame: true,
					items: [{
						fieldLabel: 'Email',
						id: 'samplesEmail',
						labelStyle: 'color: black',
						value: panelSubscribe.getComponent('subscribeEmail').getValue(),
						allowBlank: false
					},{
						fieldLabel: 'First Name',
						id: 'samplesFirstName',
						labelStyle: 'color: black',
						value: panelSubscribe.getComponent('subscribeFirstName').getValue(),
						allowBlank: false
					},{
						fieldLabel: 'Last Name',
						labelStyle: 'color: black',
						id: 'samplesLastName',
						allowBlank: false
					},{
						fieldLabel: 'Company',
						labelStyle: 'color: black',
						id: 'samplesCompany'
					},{
						fieldLabel: 'Unit Number',
						labelStyle: 'color: black',
						id: 'samplesUnitNumber'
					},{
						fieldLabel: 'Street Address',
						labelStyle: 'color: black',
						id: 'samplesStreetAddress',
						allowBlank: false
					},{
						fieldLabel: 'City',
						labelStyle: 'color: black',
						id: 'samplesCity',
						allowBlank: false
					},
						provinceOrStateComboBox
					,{
						fieldLabel: 'Postal Code',
						labelStyle: 'color: black',
						id: 'samplesPostalCode',
						allowBlank: false
					},
						new Ext.form.ComboBox({
							fieldLabel: 'Country',
							labelStyle: 'color: black',
							id: 'samplesCountry',
							displayField: 'country',
							valueField: 'countryValue',
							width: 127,
							mode: 'local',
							editable: true,
							forceSelection: true,
							triggerAction: 'all',
							allowBlank: false,
							store: new Ext.data.SimpleStore({
								fields: ['countryValue', 'country'],
								data: [
									['2', 'Canada'],
									['3', 'USA']
								]
							})
					}),{
						fieldLabel: 'Fax',
						labelStyle: 'color: black',
						id: 'samplesFax'
					},{
						fieldLabel: 'Phone',
						labelStyle: 'color: black',
						id: 'samplesPhone',
						allowBlank: false
					},
						new Ext.form.Checkbox({
							fieldLabel: 'Request Pen Samples',
							labelStyle: 'color: black',
							id: 'samplesReqPens'
						})
					,
						new Ext.form.Checkbox({
							fieldLabel: 'Request Calendar Samples',
							labelStyle: 'color: black',
							id: 'samplesReqCalendar'
						})
					,
						new Ext.form.Checkbox({
							fieldLabel: 'Request Magnet Samples',
							labelStyle: 'color: black',
							id: 'samplesReqMagnet'
						})
					],
					buttons: [{
						id: 'samplesSubmitButton',
						text: 'Submit and Request Samples',
						formBind: true,
						handler: function(){
							//We need different sets of params depending on the number of samples
							//the user wants
							requestSamplePens = 'off';
							requestSampleCalendars = 'off';
							requestSampleMagnets = 'off';

							if (panelSamples.getComponent('samplesReqPens').getValue() === true){
								requestSamplePens = 'on';
							}
							if (panelSamples.getComponent('samplesReqCalendar').getValue() === true){
								requestSampleCalendars = 'on';
							}
							if (panelSamples.getComponent('samplesReqMagnet').getValue() === true){
								requestSampleMagnets = 'on';
							}

							samplesParams = {
								attribute13: panelSamples.getComponent('samplesFirstName').getValue(),
								attribute14: panelSamples.getComponent('samplesLastName').getValue(),
								attribute15: panelSamples.getComponent('samplesCompany').getValue(),
								attribute17: panelSamples.getComponent('samplesUnitNumber').getValue(),
								attribute18: panelSamples.getComponent('samplesStreetAddress').getValue(),
								attribute19: panelSamples.getComponent('samplesCity').getValue(),
								attribute20: '' + panelSamples.getComponent('samplesStateOrProvince').getValue(),//this is the province id
								attribute21: panelSamples.getComponent('samplesPostalCode').getValue(),
								attribute22: panelSamples.getComponent('samplesCountry').getValue(),
								attribute23: panelSamples.getComponent('samplesFax').getValue(),
								attribute24: panelSamples.getComponent('samplesPhone').getValue(),
								attribute25: '1', //id too
								attribute30: requestSamplePens,
								attribute31: requestSampleCalendars,
								attribute32: requestSampleMagnets,
								email: panelSamples.getComponent('samplesEmail').getValue(),
								emailconfirm: panelSamples.getComponent('samplesEmail').getValue(),
								htmlemail: '1',
								attribute16: 'on',
								attribute29: dateStr,
								'list[15]': "signup",
								'listname[15]': "PmBrand Mailing List With Samples",
								VerificationCodeX: '',
								subscribe: 'Subscribe and Request Samples'
							}

							Ext.Ajax.request({
								params: samplesParams,
								url: 'http://www.pmbrand.com/phplist/?p=subscribe&id=7',
								timeout: 60000,
								method: 'POST',
								callback: function(options,success,response){
									if (success === true){
										Ext.Msg.alert('Thanks for Subscribing',
										'Upon email confirmation ' +
										'a phone call will be made during regular ' +
										'business hours confirming your sample request');
										winSamples.close();
									}
									else {
										Ext.Msg.alert('Sorry! There was an error with our database',
										'Please email us at sales@pmbrand.com for samples or info');
										winSamples.close();
									}
								}
							});
						}
					}]
				});

				var winSamples = new Ext.Window({
					title: 'Please Enter Additional Info',
					x: 450,
					y: 100,
					width: 300,
					height: 530,
					layout: 'fit',
					closable: true,
					collapsible: false,
					resizable: false,
					draggable: true,
					items: [panelSamples]
				});
				winSamples.show();
			}
			else {
				Ext.Ajax.request({
					params: {
						attribute13: panelSubscribe.getComponent('subscribeFirstName').getValue(),
						email: panelSubscribe.getComponent('subscribeEmail').getValue(),
						emailconfirm: panelSubscribe.getComponent('subscribeEmail').getValue(),
						htmlemail: '1',
						attribute16: 'on',
						attribute29: dateStr,
						'list[14]': "signup",
						'listname[14]': "PmBrand Mailing List",
						VerificationCodeX: '',
						subscribe: 'Subscribe to the PmBrand Mailing List'
					},
					url: 'http://www.pmbrand.com/phplist/?p=subscribe&id=4',
					timeout: 60000,
					method: 'POST',
					callback: function(options,success,response){
						if (success === true){
							Ext.Msg.alert('Thanks for Subscribing',
							'A confirmation email will be sent shortly');
						}
						else {
							Ext.Msg.alert('Sorry! There was an error with our database',
							'Please email us at sales@pmbrand.com for samples or info');
						}
					}
				});
			}
		}
	}]
});

var winSubscribe = new Ext.Window({
	title: 'Subscribe: Free Samples & More!',
	x: 559,
	y: 130,
	width: 209,
	height: 170,
	layout: 'fit',
	closable: false,
	collapsible: false,
	resizable: false,
	draggable: false,
	items: [panelSubscribe]
});
*/
//winSubscribe.show();