/**
 * 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;
}

Ext.onReady(function(){
	Ext.QuickTips.init();
	
	//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;
	
	/*Ext.Ajax.request({
		params: {
			attribute13: 'Clarence',//panelSubscribe.getComponent('subscribeFirstName').getValue(),
			email: 'cl_liu@interchange.ubc.ca',//panelSubscribe.getComponent('subscribeEmail').getValue()
			emailconfirm: 'cl_liu@interchange.ubc.ca',
			htmlemail: '1',
			attribute16: 'on',
			attribute29: '2009-02-16',
			'list[20]': "signup",
			'listname[20]': "Test List",
			VerificationCodeX: '',
			subscribe: 'Subscribe to the Selected Newsletters'
		},
		url: 'http://www.pmbrand.com/phplist/?p=subscribe&id=10',
		//url: 'http://www.demozoo.com/phplist/?p=subscribe&id=8',
		timeout: 60000,
		method: 'POST',
		callback: function(options,success,response){
			alert('callback');
			if (success === true){
				alert('Yay ' + response.responseText);
			}
			
			if (success === false){
				alert(response.responseText);
			}
		}
	});*/

	
	var sampleCheckbox = new Ext.form.Checkbox({
		fieldLabel: 'Samples',
		labelStyle: 'color: black; width: 45',
		id: 'subscribeReqSamples',
		checked: false
	});
	
	var panelSubscribe = new Ext.FormPanel({
		id: 'subscribePanel',
		defaultType: 'textfield',
		labelWidth: 25,
		monitorValid: true,
		frame: true,
		items: [{
			fieldLabel: 'Name',
			labelStyle: 'color: black; width: 45',
			id: 'subscribeFullName',
			autoWidth: false,
			width: 90,
			allowBlank: false//,
			//regex: /^[A-Za-z]+$\b/,
			//regexText: 'Invalid Name - First Name Only'
		},{
			fieldLabel: 'Email',
			labelStyle: 'color: black; width: 45',
			id: 'subscribeEmail',
			autoWidth: false,
			width: 90,
			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,
			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,
							regex: /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/,
							regexText: 'Invalid Email'
						},{
							fieldLabel: 'First Name',
							id: 'samplesFirstName',
							labelStyle: 'color: black',
							value: panelSubscribe.getComponent('subscribeFullName').getValue().split(" ")[0],
							allowBlank: false,
							regex: /^[A-Za-z]+$\b/,
							regexText: 'Invalid Name - First Name Only'
						},{
							fieldLabel: 'Last Name',
							labelStyle: 'color: black',
							id: 'samplesLastName',
							allowBlank: false,
							regex: /^[A-Za-z]+$\b/,
							regexText: 'Invalid Name - Last Name Only'
						},{
							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
						},
							new Ext.form.ComboBox({
								fieldLabel: 'State or Province',
								labelStyle: 'color: black',
								style: {"background-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'],
										[66, 'BC'],
										[67, 'VA'],
										[68, 'ON'],
										[69, 'AB'],
										[70, 'OK'],
										[71, 'NY'],
										[72, 'MB'],
										[73, 'MT'],
										[74, 'NJ'],
										[75, 'SK'],
										[76, 'PE'],
										[77, 'CA'],
										[78, 'QC'],
										[79, 'OR'],
										[80, 'TX'],
										[81, 'NS'],
										[82, 'OH'],
										[83, 'DE'],
										[84, 'IN'],
										[85, 'FL'],
										[86, 'NB'],
										[87, 'NL'],
										[88, 'NF'],
										[89, 'AL'],
										[90, 'YT'],
										[91, 'GA'],
										[92, 'IL'],
										[93, 'MS'],
										[94, 'KS'],
										[95, 'CO'],
										[96, 'AZ'],
										[97, 'NC'],
										[98, 'ID'],
										[99, 'IA'],
										[100, 'SC'],
										[101, 'MN'],
										[102, 'MI'],
										[103, 'MO'],
										[104, 'LA'],
										[105, 'WY'],
										[106, 'MA'],
										[107, 'PA'],
										[108, 'ALB'],
										[109, 'WA'],
										[110, 'DC'],
										[111, 'TN'],
										[112, 'NSW'],
										[113, 'MD'],
										[114, 'AK'],
										[115, 'SD'],
										[116, 'HI'],
										[117, 'PQ'],
										[118, 'NM'],
										[119, 'UT']
									]
								})
						}),{
							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[42]': "signup",
									'listname[42]': "BalloonBrand Canada",
									'list[43]': "signup",
									'listname[43]': "BalloonBrand Canada with Samples",
									VerificationCodeX: '',
									subscribe: 'Subscribe to the Selected Newsletters'
								}
								Ext.Ajax.request({
							
									params: samplesParams,
									url: 'http://www.balloonbrand.com/phplist/?p=subscribe&id=10',
									timeout: 30000,
									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',
						animateTarget: winSubscribe,
						x: 275,
						y: 150,
						width: 300,
						height: 530,
						layout: 'fit',
						closable: true,
						collapsible: false,
						resizable: false,
					    draggable: true,
					    items: [panelSamples]
					});
					winSamples.show();
				}
				else {
					Ext.Ajax.request({
						params: {
							attribute33: panelSubscribe.getComponent('subscribeFullName').getValue(),
							email: panelSubscribe.getComponent('subscribeEmail').getValue(),
							emailconfirm: panelSubscribe.getComponent('subscribeEmail').getValue(),
							htmlemail: '1',
							attribute16: 'on',
							attribute29: dateStr,
							'list[42]': "signup",
							'listname[42]': "BalloonBrand Canada",
							VerificationCodeX: '',
							subscribe: 'Subscribe to the Selected Newsletters'
						},
						url: 'http://www.balloonbrand.com/phplist/?p=subscribe&id=9',
						timeout: 30000,
						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 Now! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +  
			'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +  
			'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Free Samples & More!',
		x: 0,
		y: 365,
		width: 180,
		height: 175,
		layout: 'fit',
		closable: false,
		collapsible: false,
		resizable: false,
	    draggable: false,
	    items: [panelSubscribe]
	});
	winSubscribe.show();
});
